Changeset 644 for trunk/lib


Ignore:
Timestamp:
Oct 3, 2018 6:25:19 PM (6 years ago)
Author:
anonymous
Message:

Include user_id in namespace, add private init() method

Location:
trunk/lib
Files:
2 edited

Legend:

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

    r615 r644  
    9696        // This value is overwritten by the $app->getParam('db_create_tables') setting if it is available.
    9797        'create_table' => true,
     98
     99        // Original namespace set during __construct().
     100        // 'namespace' => '',
    98101    );
    99102
     
    106109
    107110        $this->_ns = $namespace;
     111
     112        // Save the original namespace for the DB pref_namespace column. $this->_ns, used in the SESSION variable, will change based on the user_id.
     113        $this->setParam(array('namespace' => $namespace));
    108114
    109115        // Get create tables config from global context.
     
    114120        // Optional initial params.
    115121        $this->setParam($params);
    116 
    117         // Initialized the prefs array.
    118         if ('cookie' != $app->getParam('storagetype') && !isset($_SESSION['_prefs'][$this->_ns]['saved'])) {
    119             $this->clear();
    120         }
    121122
    122123        // Run Prefs->save() upon script completion if we're using the database storagetype.
     
    191192        }
    192193
     194        // Append the user_id to the namespace to keep separate collections for different users.
     195        if (isset($params['user_id']) && $params['user_id']) {
     196            // $this->getParam('namespace') should always be available since it is set in __construct().
     197            $this->_ns = sprintf('%s-%s', $this->getParam('namespace'), $params['user_id']);
     198        }
     199
     200        // Check max DB string lengths.
     201        if ((isset($params['storagetype']) && $params['storagetype'] == 'database') || $this->getParam('storagetype') == 'database') {
     202            if (isset($params['user_id']) && mb_strlen($params['user_id']) > 32) {
     203                $app->logMsg(sprintf('Prefs user_id param longer than 32 characters: %s', $params['user_id']), LOG_ERR, __FILE__, __LINE__);
     204            }
     205            if (isset($params['namespace']) && mb_strlen($params['namespace']) > 32) {
     206                $app->logMsg(sprintf('Prefs namespace longer than 32 characters: %s', $this->_ns), LOG_ERR, __FILE__, __LINE__);
     207            }
     208            if (isset($params['pref_key']) && mb_strlen($params['pref_key']) > 64) {
     209                $app->logMsg(sprintf('Prefs pref_key param longer than 64 characters: %s', $params['pref_key']), LOG_ERR, __FILE__, __LINE__);
     210            }
     211        }
     212
    193213        if (isset($params) && is_array($params)) {
    194214            // Merge new parameters with old overriding only those passed.
     
    213233            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
    214234            return null;
     235        }
     236    }
     237
     238    /*
     239    * Setup the SESSION storage array. This method is called at the beginning of each method that accesses $_SESSION['_prefs'].
     240    *
     241    * @access   public
     242    * @param
     243    * @return
     244    * @author   Quinn Comendant <quinn@strangecode.com>
     245    * @since    02 Oct 2018 15:35:09
     246    */
     247    private function _init()
     248    {
     249        if ('cookie' != $this->getParam('storagetype') && !isset($_SESSION['_prefs'][$this->_ns])) {
     250            $this->clear();
    215251        }
    216252    }
     
    230266    {
    231267        $app =& App::getInstance();
     268
     269        $this->_init();
    232270
    233271        if (isset($defaults) && is_array($defaults)) {
     
    275313            return false;
    276314        }
     315
     316        $this->_init();
    277317
    278318        switch ($this->getParam('storagetype')) {
     
    317357        $app =& App::getInstance();
    318358
     359        $this->_init();
     360
    319361        switch ($this->getParam('storagetype')) {
    320362        case 'session':
     
    352394    public function exists($key)
    353395    {
     396        $this->_init();
     397
    354398        switch ($this->getParam('storagetype')) {
    355399        case 'session':
     
    372416    {
    373417        $app =& App::getInstance();
     418
     419        $this->_init();
    374420
    375421        switch ($this->getParam('storagetype')) {
     
    394440    /**
    395441     * Resets all existing values under this namespace. This should be executed with the same consideration as $auth->clear(), such as when logging out.
    396      */
    397     public function clear($scope='all')
     442     * Set $save true to persist the clear action to the storage (i.e., erase all stored prefs for this user).
     443     */
     444    public function clear($scope='all', $save=false)
    398445    {
    399446        $app =& App::getInstance();
     
    431478        }
    432479
    433         $app->logMsg(sprintf('Cleared %s %s prefs', $scope, $this->_ns), LOG_DEBUG, __FILE__, __LINE__);
     480        if ($save) {
     481            $this->save(true);
     482            $app->logMsg(sprintf('Deleted all %s %s prefs for user_id %s', $this->getParam('storagetype'), $this->_ns, $this->getParam('user_id')), LOG_INFO, __FILE__, __LINE__);
     483        } else {
     484            $app->logMsg(sprintf('Cleared %s %s prefs', $scope, $this->_ns), LOG_DEBUG, __FILE__, __LINE__);
     485        }
    434486    }
    435487
     
    457509        $this->initDB();
    458510
     511        $this->_init();
     512
    459513        // Prefs already loaded for this session.
    460514        if (!$force && $this->_isLoaded()) {
     
    476530            FROM " . $db->escapeString($this->getParam('db_table')) . "
    477531            WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "'
    478             AND pref_namespace = '" . $db->escapeString($this->_ns) . "'
     532            AND pref_namespace = '" . $db->escapeString($this->getParam('namespace')) . "'
    479533            LIMIT 10000
    480534        ");
     
    509563        }
    510564
     565        $this->_init();
     566
    511567        if (isset($_SESSION['_prefs'][$this->_ns]['load_datetime'])
    512568        && strtotime($_SESSION['_prefs'][$this->_ns]['load_datetime']) > time() - $this->getParam('load_timeout')
     
    528584    * @since    04 Jun 2006 17:19:56
    529585    */
    530     public function save()
     586    public function save($allow_empty=false)
    531587    {
    532588        $app =& App::getInstance();
     
    547603        $this->initDB();
    548604
    549         if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && is_array($_SESSION['_prefs'][$this->_ns]['saved']) && !empty($_SESSION['_prefs'][$this->_ns]['saved'])) {
     605        $this->_init();
     606
     607        if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && is_array($_SESSION['_prefs'][$this->_ns]['saved']) && ($allow_empty || !empty($_SESSION['_prefs'][$this->_ns]['saved']))) {
    550608            // Delete old prefs from database.
    551609            $db->query("
    552610                DELETE FROM " . $db->escapeString($this->getParam('db_table')) . "
    553611                WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "'
    554                 AND pref_namespace = '" . $db->escapeString($this->_ns) . "'
     612                AND pref_namespace = '" . $db->escapeString($this->getParam('namespace')) . "'
    555613            ");
    556614
     
    560618                $insert_values[] = sprintf("('%s', '%s', '%s', '%s')",
    561619                    $db->escapeString($this->getParam('user_id')),
    562                     $db->escapeString($this->_ns),
     620                    $db->escapeString($this->getParam('namespace')),
    563621                    $db->escapeString($key),
    564622                    $db->escapeString(serialize($val))
    565623                );
    566624            }
    567             // TODO: after MySQL 5.0.23 is released this query could benefit from INSERT DELAYED.
    568             $db->query("
    569                 INSERT INTO " . $db->escapeString($this->getParam('db_table')) . "
    570                 (user_id, pref_namespace, pref_key, pref_value)
    571                 VALUES " . join(', ', $insert_values) . "
    572             ");
    573 
    574             $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
     625            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                ");
     632                $app->logMsg(sprintf('Saved %s prefs to database for user_id %s.', sizeof($insert_values), $this->getParam('user_id')), LOG_DEBUG, __FILE__, __LINE__);
     633            }
     634
    575635            return true;
    576636        }
     
    597657        }
    598658        // Use standardized class data names: _ + classname + namespace + variablekey
     659        // (namespace = namespace + user_id)
    599660        return sprintf('_prefs-%s-%s', $this->_ns, $key);
    600661    }
  • trunk/lib/Utilities.inc.php

    r623 r644  
    147147        $output .= sprintf("%s%s\n", $indent, $var);
    148148    }
    149     return $output;
     149    return preg_replace(['/^[ \t]+$/', '/\n\n\n+/'], ['', "\n\n"], $output);
    150150}
    151151
Note: See TracChangeset for help on using the changeset viewer.