Changeset 719 for trunk/lib


Ignore:
Timestamp:
Mar 9, 2020 3:13:45 AM (4 years ago)
Author:
anonymous
Message:

Update Prefs to use PDO

Location:
trunk/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/DB.inc.php

    r712 r719  
    172172        // Connect to database. Always create a new link to the server.
    173173        // Connection errors are suppressed so we can do our own error management below.
    174         if ($this->dbh = @mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {
     174        if ($this->dbh = mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {
    175175            // Select database
    176176            mysql_select_db($this->getParam('db_name'), $this->dbh);
  • trunk/lib/PDO.inc.php

    r718 r719  
    242242
    243243        if (!$this->_connected) {
    244             $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    245             return false;
    246         }
     244            throw new \Exception(sprintf('No DB connection to run %s', __METHOD__));
     245        }
     246
    247247        $this->_connected = false;
    248248        $this->dbh = null;
     249
    249250        return true;
    250251    }
     
    280281
    281282        if (!$this->_connected) {
    282             $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    283             return false;
     283            throw new \Exception(sprintf('No DB connection to run %s', __METHOD__));
    284284        }
    285285
     
    374374
    375375        if (!$this->_connected) {
    376             $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    377             return false;
     376            throw new \Exception(sprintf('No DB connection to run %s', __METHOD__));
    378377        }
    379378
     
    431430
    432431        if (!$this->_connected) {
    433             $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    434             return false;
     432            throw new \Exception(sprintf('No DB connection to run %s', __METHOD__));
    435433        }
    436434
     
    527525
    528526        if (!$this->_connected) {
    529             $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    530             return false;
     527            throw new \Exception(sprintf('No DB connection to run %s', __METHOD__));
    531528        }
    532529
     
    558555
    559556        if (!$this->_connected) {
    560             $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);
    561             return false;
     557            throw new \Exception(sprintf('No DB connection to run %s', __METHOD__));
    562558        }
    563559
  • trunk/lib/Prefs.inc.php

    r669 r719  
    121121        $this->setParam($params);
    122122
    123         // Run Prefs->save() upon script completion if we're using the database storagetype.
    124         // This only works if 'storagetype' is provided as a parameter to the constructor rather than via setParam() later.
    125         if ('database' == $this->getParam('storagetype')) {
    126             register_shutdown_function(array($this, 'save'));
     123        if (isset($params['save_on_shutdown']) && $params['save_on_shutdown']) {
     124            // Run Prefs->save() upon script completion if we're using the database storagetype.
     125            // This only works if:
     126            // - 'storagetype' is provided as a parameter to the constructor rather than via setParam() later.
     127            // - $app->stop() is not called at the end of the script (which would close the PDO connection before the shutdown function runs).
     128            if ('database' == $this->getParam('storagetype')) {
     129                register_shutdown_function(array($this, 'save'));
     130            }
    127131        }
    128132    }
     
    138142    {
    139143        $app =& App::getInstance();
    140         $db =& DB::getInstance();
     144        $pdo =& \Strangecode\Codebase\PDO::getInstance();
    141145
    142146        static $_db_tested = false;
     
    144148        if ($recreate_db || !$_db_tested && $this->getParam('create_table')) {
    145149            if ($recreate_db) {
    146                 $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
     150                $pdo->query(sprintf("DROP TABLE IF EXISTS `%s`", $pdo->sanitizeIdentifier($this->getParam('db_table'))));
    147151                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_INFO, __FILE__, __LINE__);
    148152            }
    149             $db->query("CREATE TABLE IF NOT EXISTS " . $db->escapeString($this->getParam('db_table')) . " (
    150                 user_id VARCHAR(32) NOT NULL DEFAULT '',
    151                 pref_namespace VARCHAR(32) NOT NULL DEFAULT '',
    152                 pref_key VARCHAR(64) NOT NULL DEFAULT '',
    153                 pref_value TEXT,
    154                 PRIMARY KEY (user_id, pref_namespace, pref_key)
    155             )");
    156 
    157             if (!$db->columnExists($this->getParam('db_table'), array(
     153            $stmt = $pdo->query(sprintf("
     154                CREATE TABLE IF NOT EXISTS `%s` (
     155                    `user_id` VARCHAR(32) NOT NULL DEFAULT '',
     156                    `pref_namespace` VARCHAR(32) NOT NULL DEFAULT '',
     157                    `pref_key` VARCHAR(64) NOT NULL DEFAULT '',
     158                    `pref_value` TEXT,
     159                    PRIMARY KEY (`user_id`, `pref_namespace`, `pref_key`)
     160                )
     161            ", $pdo->sanitizeIdentifier($this->getParam('db_table'))));
     162
     163            if (!$pdo->columnExists($this->getParam('db_table'), array(
    158164                'user_id',
    159165                'pref_namespace',
     
    328334            || (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved']))) {
    329335                $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val;
    330                 $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
     336                $app->logMsg(sprintf('Setting session/database preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
    331337            } else {
    332                 $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
     338                $app->logMsg(sprintf('Not setting session/database preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
    333339            }
    334340            break;
     
    499505    {
    500506        $app =& App::getInstance();
    501         $db =& DB::getInstance();
     507        $pdo =& \Strangecode\Codebase\PDO::getInstance();
    502508
    503509        // Skip this method if not using the db.
     
    526532
    527533        // Retrieve all prefs for this user and namespace.
    528         $qid = $db->query("
    529             SELECT pref_key, pref_value
    530             FROM " . $db->escapeString($this->getParam('db_table')) . "
    531             WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "'
    532             AND pref_namespace = '" . $db->escapeString($this->getParam('namespace')) . "'
    533             LIMIT 10000
    534         ");
    535         while (list($key, $val) = mysql_fetch_row($qid)) {
     534        $stmt = $pdo->prepare(sprintf("SELECT `pref_key`, `pref_value` FROM `%s` WHERE `user_id` = ? AND `pref_namespace` = ? LIMIT 100000", $pdo->sanitizeIdentifier($this->getParam('db_table'))));
     535        $stmt->execute([$this->getParam('user_id'), $this->getParam('namespace')]);
     536        while (list($key, $val) = $stmt->fetch(\PDO::FETCH_NUM)) {
    536537            $_SESSION['_prefs'][$this->_ns]['saved'][$key] = unserialize($val);
    537538        }
    538539
    539         $app->logMsg(sprintf('Loaded %s prefs from database.', mysql_num_rows($qid)), LOG_DEBUG, __FILE__, __LINE__);
     540        $app->logMsg(sprintf('Loaded %s prefs from database.', sizeof($_SESSION['_prefs'][$this->_ns]['saved'])), LOG_DEBUG, __FILE__, __LINE__);
    540541
    541542        // Data loaded only once per session.
     
    587588    {
    588589        $app =& App::getInstance();
    589         $db =& DB::getInstance();
     590        $pdo =& \Strangecode\Codebase\PDO::getInstance();
    590591
    591592        // Skip this method if not using the db.
     
    607608        if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && is_array($_SESSION['_prefs'][$this->_ns]['saved']) && ($allow_empty || !empty($_SESSION['_prefs'][$this->_ns]['saved']))) {
    608609            // Delete old prefs from database.
    609             $db->query("
    610                 DELETE FROM " . $db->escapeString($this->getParam('db_table')) . "
    611                 WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "'
    612                 AND pref_namespace = '" . $db->escapeString($this->getParam('namespace')) . "'
    613             ");
     610            $stmt = $pdo->prepare(sprintf("DELETE FROM `%s` WHERE `user_id` = ? AND `pref_namespace` = ?", $pdo->sanitizeIdentifier($this->getParam('db_table'))));
     611            $stmt->execute([$this->getParam('user_id'), $this->getParam('namespace')]);
    614612
    615613            // Insert new prefs.
    616614            $insert_values = array();
    617615            foreach ($_SESSION['_prefs'][$this->_ns]['saved'] as $key => $val) {
    618                 $insert_values[] = sprintf("('%s', '%s', '%s', '%s')",
    619                     $db->escapeString($this->getParam('user_id')),
    620                     $db->escapeString($this->getParam('namespace')),
    621                     $db->escapeString($key),
    622                     $db->escapeString(serialize($val))
     616                $insert_values[] = sprintf("(%s, %s, %s, %s)",
     617                    $pdo->quote($this->getParam('user_id')),
     618                    $pdo->quote($this->getParam('namespace')),
     619                    $pdo->quote($key),
     620                    $pdo->quote(serialize($val))
    623621                );
    624622            }
    625623            if (!empty($insert_values)) {
    626                 // TODO: after MySQL 5.0.23 is released this query could benefit from INSERT DELAYED.
    627                 $db->query("
    628                     INSERT INTO " . $db->escapeString($this->getParam('db_table')) . "
    629                     (user_id, pref_namespace, pref_key, pref_value)
    630                     VALUES " . join(', ', $insert_values) . "
    631                 ");
     624                $stmt = $pdo->query(sprintf("
     625                    INSERT INTO `%s`
     626                    (`user_id`, `pref_namespace`, `pref_key`, `pref_value`)
     627                    VALUES
     628                    %s
     629                ", $pdo->sanitizeIdentifier($this->getParam('db_table')), join(', ', $insert_values)));
    632630                $app->logMsg(sprintf('Saved %s prefs to database for user_id %s.', sizeof($insert_values), $this->getParam('user_id')), LOG_DEBUG, __FILE__, __LINE__);
    633631            }
Note: See TracChangeset for help on using the changeset viewer.