public static function get() { // Connect if not already connected if (is_null(self::$db)) { self::$db = new PDO(self::$dsn, self::$user, self::$pass, self::$driverOpts); } // Return the connection return self::$db; }
public static function get() { if (is_null(self::$db)) { self::$db = new PDO(self::$dsn, self::$user, self::$pass); } //返回连接 self::$db->query('set names utf8'); return self::$db; }
public static function get() { // Connect if not already connected if (is_null(self::$db)) { try { // sprintf to get around the fact you can't create teh dsn dynamically by concatting vars. self::$db = new PDO(sprintf("mysql:host=%s;dbname=%s;charset=utf8", DB_SERVER, DB_NAME), DB_USER, DB_PASSWORD, self::$driverOpts); // set up PDO to throw exceptions so I can use try and catch blocks in the code http://www.kitebird.com/articles/php-pdo.html#TOC_5 self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo $e->getMessage(); } } // Return the connection return self::$db; }
public static function get($dbname) { // Connect if not already connected if (is_null(self::$db)) { #### GET DATABASE CREDENTIALS #### if (!file_exists($dbname)) { die("No database credentials file! for: {$dbname}"); } require_once $dbname; # Above web root $dbname = substr($dbname, strrpos($dbname, '/') + 1); $dbname = str_replace('.php', '', $dbname); # The name of the database is the file name minus its .php extension. self::$dbname = $dbname; self::$user = $user; self::$pass = $pass; ################################## self::$db = new PDO(self::$dsn . self::$dbname, self::$user, self::$pass, self::$driverOpts); } // End if. return self::$db; # Return the connection. }
function getTemplateCode($nodeID) { $db = DBCxn::Get(); // Get template code $sql = "SELECT content \n\t\t\t\t\tFROM cms_template AS t \n\t\t\t\t\tINNER JOIN cms_content AS c \n\t\t\t\t\t\tON c.templateID = t.ID\n\t\t\t\t\tWHERE c.nodeID = :nodeID AND c.published = 1\n\t\t\t\t\t\t;"; $query = $db->prepare($sql); $query->bindParam(':nodeID', $nodeID, PDO::PARAM_INT); $query->execute(); // If we have a row if ($query->rowCount() != 0) { $result = $query->fetch(); $templateCode = $result['content']; } else { // TO DO -- handle this better? error_log('Error getting template content for: nodeID = ' . $nodeID); } // close the conn $db = null; return $templateCode; }
function newUser($userName, $foreName, $lastName, $email) { // $db = DBCxn::Get(); try { // Generate a GUID for email validation $GUID = strtolower(sha1(SALT . trim(uniqid()))); $email = strtolower(trim($email)); $userName = trim($userName); $foreName = trim($foreName); $lastName = trim($lastName); $query = $db->prepare("INSERT INTO cms_user (username, active, foreName, lastName, email, passwordhash, created, lastUpdated) \n\t\t\t\tVALUES (:userName, 1, :foreName, :lastName, :email, UUID(), NOW(), NOW());"); $query->bindParam(':userName', $userName); $query->bindParam(':foreName', $foreName); $query->bindParam(':lastName', $lastName); $query->bindParam(':email', $email); $query->execute(); $this->userID = $db->lastInsertId(); $query = null; } catch (PDOException $e) { error_log('Error Creating User' . $e->getMessage()); $this->problems .= "The statement failed.\n"; $this->problems .= "getCode: " . $e->getCode() . "\n"; $this->problems .= "getMessage: " . $e->getMessage() . "\n"; } return true; }
<?php // 28/02/13 - Add new article to a subtopic require_once '../../../../../includes/classes/Db.php'; $link = DBCxn::get('../../../../../_db/help.php'); //die($link); $r = $link->prepare("INSERT INTO content (stopicid, insert_date, controllerid) VALUES (?, NOW(), ?)"); //print_r($r->errorInfo()); $r->execute(array($_POST['id'], 4)); if ($r->rowCount() == 1) { // ID for new article | poster name | current date and time echo $link->lastInsertId() . '|Bashir|' . date('l jS M Y, H:i'); // Get topic ID from subtopic ID: $r2 = $link->prepare("SELECT topicid FROM stopics WHERE stopic_id=?"); $r2->execute(array($_POST['id'])); list($topicid) = $r2->fetch(PDO::FETCH_NUM); // Finally update topic and subtopic tables $r2 = $link->prepare("UPDATE helptopics SET mod_datetime=NOW() WHERE topic_id=?"); $r2->execute(array($topicid)); $r2 = $link->prepare("UPDATE helpstopics SET mod_datetime=NOW() WHERE stopic_id=?"); $r2->execute(array($_POST['id'])); } // End if.
<?php $path = "../../../../../"; # public_html/apps/help/_inc/ajax/ //ini_set('display_errors', 1); require_once "{$path}includes/classes/help.class.php"; $help = new Help(); require_once "{$path}{$help->CLASSES_PATH}Db.php"; $link = DBCxn::get("{$path}_db/help.php"); require "{$path}{$help->CLASSES_PATH}Login.php"; # Login class. $login = new Login($help->sess_dir); $login->startsession(); if (!isset($_SESSION[$help->sessname])) { die('It was a bright sunny morning in July ...'); } /////////////////////////////////// // AND topic LIKE '%{$_POST['q']}%' $r = $link->prepare("SELECT t.topic, t.topic_id, t.hide, s.stopic_id, s.stopic, c.content_id, c.pintonav, c.title \n FROM topics AS t LEFT JOIN stopics AS s ON s.topicid = t.topic_id LEFT JOIN content AS c ON c.stopicid = s.stopic_id \n ORDER BY t.mod_datetime DESC, s.mod_datetime DESC"); $r->execute(array()); $arr_topicids = []; $arr_stopicids = []; $arr_contentds = []; $currtid = 0; $currstopicid = 0; while (list($t, $tid, $thide, $stid, $st, $ncontentid, $pintonav, $ncontent) = $r->fetch(PDO::FETCH_NUM)) { //echo "$currtid - $tid - $st - $currstopicid - $stid<br>"; # TESTING if ($currtid != $tid) { $arr_stopicids = []; } $arr_stopicids[$stid] = array($stid, $st);
function __construct() { //echo 'login Constructor called'; self::$db = DBCxn::Get(); }
public function display() { $link = DBCxn::get($this->_DB); # Link to database. $r_topics = $link->prepare("SELECT topic_id, topic FROM topics WHERE hide!=1 ORDER BY mod_datetime DESC"); $r_topics->execute(array()); if ($r_topics->rowCount() == 0) { die('No topics'); } // End if. require_once '../../../includes/classes/Document.php'; require_once '../../../includes/classes/Forms.php'; require_once '../../../includes/classes/Tables.php'; $document = new Document('Help', '_inc/styles/styles.css', array('/scripts/jquery/js/jquery-1.8.3.min.js', '/scripts/tinymce/jscripts/tiny_mce/jquery.tinymce.js', '_inc/js/goup.js', '_inc/js/help.js')); echo '<a href="#" class="btnNav">Nav</a> <a href="#" class="scrollup">Scroll</a> <div class="logout"><a href="?act=signout">Logout</a></div>'; $tables = new Tables(array('id' => 'helpsection'), "Topics: {$r_topics->rowCount()}"); # ID and Caption for start table tag. // Need a counter for the table columns: $i = 0; $topic_counter = 1; $items = 3; // Display each topic: while (list($topicid, $topic) = $r_topics->fetch(PDO::FETCH_NUM)) { // Do we need to start a new row? if ($i == 0) { echo "<tr valign=\"top\">\n"; } // End if. echo "<td>\n <h2 id=\"topic_{$topic_counter}\">{$topic_counter} {$topic}<span title=\"{$topicid}\">Hide</span></h2>\n"; //$stxt = ''; // 04/08/13 - If search text is submitted, filter results: //if (isset($_POST['stxt'])) { $stxt = " AND stopic like '%{$_POST['stxt']}%'"; } // End if. // Sub-topics: $r_stopics = $link->prepare("SELECT stopic_id, stopic FROM stopics WHERE topicid=? and hide!=1 ORDER BY mod_datetime DESC"); $r_stopics->execute(array($topicid)); // Scroll through subtopics while (list($stopicid, $stopic) = $r_stopics->fetch(PDO::FETCH_NUM)) { // Sub topic echo "<h3 id=\"s_{$stopicid}\">\n <a href=\"_inc/php/helpstopicren.php?stopicid={$stopicid}\" class=\"stopic_a\" title=\"Rename Sub topic\">{$stopic}</a></h3>\n"; //$stxt2 = ''; $stxt2 // 04/08/13 - If search text is submitted, filter results: //if (isset($_POST['stxt'])) { $stxt2 = " AND title LIKE '%{$_POST['stxt']}%'"; } // End if. // Get main articles in each subtopic: $r_articles = $link->prepare("SELECT\n c.content_id,\n c.mod_datetime,\n c.controllerid,\n c.title,\n c.content, \n c.pintonav, \n DATE_FORMAT(c.insert_date, '%a %D %b %Y, %H:%i') AS insert_dt,\n DATE_FORMAT(c.mod_datetime, '%a %D %b %Y, %H:%i') AS insert_dt,\n DATE_FORMAT(c.mod_datetime, '%a %D %b %Y') AS insert_dt2,\n u.name\n \n FROM content AS c \n INNER JOIN users AS u ON c.controllerid = u.controller_id\n \n WHERE c.stopicid=? AND c.parentid=0 ORDER BY c.mod_datetime DESC"); $r_articles->execute(array($stopicid)); if (!$r_articles) { print_r($r_articles->errorInfo()); } // End if. // Display new article link for subtopics with no articles if ($r_articles->rowCount() == 0) { echo "<p class=\"newarticle\" title2=\"{$stopicid}\"><a href=\"#\">New article</a></p>"; } // End if. $artnum = 1; //$hightlighttitle = ' - IMPORTANT'; while (list($acid, $amod, $acon, $atitle, $acontent, $pintonav, $ains, $aupd, $aupd3, $aposter) = $r_articles->fetch(PDO::FETCH_NUM)) { // Article title $aupd2 = "{$this->DaysAwayTo($aupd3)} days"; $aupdtxt = $aupd != '0000-00-00 00:00:00' ? "<br />Updated: [<strong>{$aupd}</strong> {$aupd2}]" : ''; //if (strpos($atitle, $hightlighttitle) != false) { if ($pintonav == 1) { //$atitle = str_replace( $hightlighttitle, '', $atitle); $atitle = "<span class=\"highlighted\">{$atitle}</span>"; } // End if. echo "<li class=\"article_li\" id=\"li_art{$acid}\">\n <a id=\"mainarticle{$acid}\" class=\"mainarticle\" title2=\"{$acid}\">{$artnum}. {$atitle}</a>\n \n <ul type=\"square\" id=\"replylist{$acid}\">"; // Get any replies: $r_articlereplies = $link->prepare("SELECT\n content.content_id,\n content.controllerid,\n content.title, \n content.pintonav, \n DATE_FORMAT(content.insert_date, '%a %D %b %Y, %H:%i') AS insert_dt,\n DATE_FORMAT(content.mod_datetime, '%a %D %b %Y, %H:%i') AS insert_dt,\n users.name\n FROM content\n \n INNER JOIN users ON content.controllerid = users.controller_id\n \n WHERE parentid=? ORDER BY mod_datetime DESC"); $r_articlereplies->execute(array($acid)); while (list($rcontentid, $rposterid, $rtitle, $pintonav, $rins, $rupd, $rposter) = $r_articlereplies->fetch(PDO::FETCH_NUM)) { $rupd2 = "{$this->DaysAwayTo($rupd)} days"; $updtxt = $rupd != '0000-00-00 00:00:00' ? "<br />Updated: [<strong>{$rupd}</strong> {$rupd2}]" : ''; // 31/05/15 - Highlight replies with 'Important in them.' if ($pintonav == 1) { //$rtitle = str_replace( $hightlighttitle, '', $rtitle); $rtitle = "<span style=\"background:yellow;color:#000;\">{$rtitle}</span>"; } // End if. echo "<li>\n <a id=\"subbarticle{$rcontentid}\" class=\"subbarticle\" title2=\"{$rcontentid}\">{$rtitle} ({$rupd2})</a>\n <!--<span id=\"rposter{$rcontentid}\" class=\"rposter\">By [{$rposter}] on [{$rins}]{$updtxt}</span>-->\n </li>"; } // End while. echo '' . "\n"; // What about replies to replies? does not work yet! echo "<p class=\"articleoption article{$stopicid}option\">\n <a id=\"reply{$acid}\" class=\"reply\" title2=\"{$acid}\">Reply</a>\n </p>"; echo "\n </ul></li>\n"; $artnum++; } // End while main articles // New article: echo "<p class=\"articleoption article{$stopicid}option newarticle\" title2=\"{$stopicid}\">\n <a>New article</a></p>\n"; } // End while stopics // New sub topic button echo "<p><a href=\"_inc/php/helpstopicnew.php?topicid={$topicid}\" class=\"newsubtopic\">New sub topic</a></p>\n"; echo ' </td>'; // Close topic table cell $i++; if ($i == $items) { echo "</tr>\n"; $i = 0; // Reset counter. } // End if. $topic_counter++; } // End while topics unset($tables); unset($document); }
function getSitemap() { $xmlBody = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> </urlset> '; if (!($xml = simplexml_load_string($xmlBody))) { error_log('Unable to load XML string'); return false; } $db = DBCxn::Get(); try { // TO DO - language is NOT considered $sql = 'SELECT pp.ID AS pathID, pp.path, pp.nodeID, c.ID AS contentID, td.nm AS nodeName, c.templateID, c.lastUpdated AS lastModified, ev1.value AS PageTitle, ev2.value AS SitemapChangeFreq, ev3.value AS SitemapPriority, ev4.value AS SitemapHide FROM `cms_page_path` AS pp INNER JOIN cms_tree_data AS td ON td.ID = pp.nodeID INNER JOIN cms_content AS c ON c.nodeID = pp.nodeID -- PageTitle LEFT OUTER JOIN (SELECT ID, templateID, name FROM cms_entity WHERE name = "PageTitle") AS e1 ON e1.templateID = c.templateID LEFT OUTER JOIN (SELECT ID, entityID, contentID, value FROM cms_entity_value_shorttext) AS ev1 ON ev1.entityID = e1.ID AND c.ID = ev1.contentID -- SitemapChangeFreq LEFT OUTER JOIN (SELECT ID, templateID, name FROM cms_entity WHERE name = "SitemapChangeFreq") AS e2 ON e2.templateID = c.templateID LEFT OUTER JOIN (SELECT ID, entityID, contentID, value FROM cms_entity_value_shorttext) AS ev2 ON ev2.entityID = e2.ID AND c.ID = ev2.contentID -- SitemapPriority LEFT OUTER JOIN (SELECT ID, templateID, name FROM cms_entity WHERE name = "SitemapPriority") AS e3 ON e3.templateID = c.templateID LEFT OUTER JOIN (SELECT ID, entityID, contentID, value FROM cms_entity_value_int) AS ev3 ON ev3.entityID = e3.ID AND c.ID = ev3.contentID -- SitemapHide LEFT OUTER JOIN (SELECT ID, templateID, name FROM cms_entity WHERE name = "SitemapHide") AS e4 ON e4.templateID = c.templateID LEFT OUTER JOIN (SELECT ID, entityID, contentID, value FROM cms_entity_value_shorttext) AS ev4 ON ev4.entityID = e4.ID AND c.ID = ev4.contentID WHERE (pp.type = 0 OR pp.type = 1) AND c.published = 1 AND c.languageID = 1 ;'; $query = $db->prepare($sql); // $query->bindParam(':languageID', $this->languageID, PDO::PARAM_INT); $query->execute(); // TO DO - if ever there are multiple sites then need to look this ID up? // If we have any rows if ($query->rowCount() != 0) { foreach ($query as $key => $result) { $urlXml = $xml->addChild("url", ""); $urlXml->addChild("loc", DOMAIN . '/' . $result['path']); $urlXml->addChild("lastmod", date(DATE_ATOM, strtotime($result['lastModified']))); if (isset($result['SitemapChangeFreq']) && $result['SitemapChangeFreq'] !== null) { $ChangeFreq = strtolower(trim($result['SitemapChangeFreq'])); $ChangeFreqArray = array("always", "hourly", "daily", "weekly", "monthly", "yearly", "never"); if (in_array($ChangeFreq, $ChangeFreqArray)) { $urlXml->addChild("changefreq", $ChangeFreq); } } if (isset($result['SitemapPriority']) && $result['SitemapPriority'] !== null) { $priority = number_format($result['SitemapPriority'] / 10, 1, '.', ''); if ($result['SitemapPriority'] > 10) { $priority = "1.0"; } elseif ($result['SitemapPriority'] < 1) { $priority = "0.5"; } $urlXml->addChild("priority", $priority); } } } } catch (PDOException $e) { error_log('Caught PDO exception in the getSitemap function'); error_log('Exception in the getSitemap getCode: ' . $e->getCode() . "\n" . $e->getMessage()); return false; } catch (Exception $e) { error_log('Caught unknown exception in the getSitemap function'); error_log('Exception in the getSitemap getCode: ' . $e->getCode() . "\n" . $e->getMessage()); return false; } $sitemapBody = $xml->asXML(); return $sitemapBody; }
function __construct() { // Init variables self::$db = DBCxn::Get(); }
public function dbConn($creds) { $this->link = DBCxn::get($creds); # Link to database. }
function newSection($templateID, $tabID) { // Stores the new section to the DB $db = DBCxn::Get(); // TO DO Validate input try { $db->beginTransaction(); // 1. insert into the entity table. $sql = "INSERT INTO cms_section (`template_tabID`) \n\t\t\t\t\t\t\t\t\tVALUES (:template_tabID)\n\t\t\t\t\t\t\t\t\t;"; $query = $db->prepare($sql); $query->bindParam(':template_tabID', $tabID, PDO::PARAM_INT); $query->execute(); $sectionID = $db->lastInsertId(); $db->commit(); // close the connection $db = null; return $sectionID; } catch (PDOException $e) { $db->rollBack(); error_log("Saving New Section failed.\n"); error_log("getCode: " . $e->getCode() . "\n"); error_log("getMessage: " . $e->getMessage() . "\n"); return false; } return false; }