Changeset 478


Ignore:
Timestamp:
May 3, 2014 9:13:05 PM (10 years ago)
Author:
anonymous
Message:

Using array_key_exists() instead of isset() where null may be a used value. Added params to Prefs constructor. Include setDefault functionality for cookie storage types. Finished cookie storagetype of Prefs.

Location:
trunk/lib
Files:
22 edited

Legend:

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

    r468 r478  
    114114        $app =& App::getInstance();
    115115
    116         if (isset($this->_params[$param])) {
     116        if (array_key_exists($param, $this->_params)) {
    117117            return $this->_params[$param];
    118118        } else {
  • trunk/lib/App.inc.php

    r477 r478  
    230230        if ($param === null) {
    231231            return $this->_params;
    232         } else if (isset($this->_params[$param])) {
     232        } else if (array_key_exists($param, $this->_params)) {
    233233            return $this->_params[$param];
    234234        } else {
     
    802802        }
    803803        foreach ($query_key as $k) {
    804             if (isset($this->_carry_queries[$k])) {
     804            if (array_key_exists($k, $this->_carry_queries)) {
    805805                // Remove the value of the specified query argument from the _carry_queries array.
    806806                $this->logMsg(sprintf('Dropping carried query: %s => %s', $k, $this->_carry_queries[$k]), LOG_DEBUG, __FILE__, __LINE__);
    807807                unset($this->_carry_queries[$k]);
    808808            }
    809             if ($unset && isset($_REQUEST[$k])) {
     809            if ($unset && array_key_exists($k, $_REQUEST)) {
    810810                unset($_REQUEST[$k], $_GET[$k], $_POST[$k], $_COOKIE[$k]);
    811811            }
  • trunk/lib/Auth_File.inc.php

    r468 r478  
    119119        $app = &App::getInstance();
    120120
    121         if (isset($this->_params[$param])) {
     121        if (array_key_exists($param, $this->_params)) {
    122122            return $this->_params[$param];
    123123        } else {
  • trunk/lib/Auth_SQL.inc.php

    r468 r478  
    278278        $app =& App::getInstance();
    279279
    280         if (isset($this->_params[$param])) {
     280        if (array_key_exists($param, $this->_params)) {
    281281            return $this->_params[$param];
    282282        } else {
  • trunk/lib/AuthorizeNet.inc.php

    r468 r478  
    176176        $app =& App::getInstance();
    177177
    178         if (isset($this->_params[$param])) {
     178        if (array_key_exists($param, $this->_params)) {
    179179            return $this->_params[$param];
    180180        } else {
  • trunk/lib/CSS.inc.php

    r473 r478  
    7171        $app =& App::getInstance();
    7272
    73         if (isset($this->_params[$param])) {
     73        if (array_key_exists($param, $this->_params)) {
    7474            return $this->_params[$param];
    7575        } else {
  • trunk/lib/Cache.inc.php

    r468 r478  
    131131        $app =& App::getInstance();
    132132
    133         if (isset($this->_params[$param])) {
     133        if (array_key_exists($param, $this->_params)) {
    134134            return $this->_params[$param];
    135135        } else {
  • trunk/lib/Cart.inc.php

    r468 r478  
    116116        $app =& App::getInstance();
    117117   
    118         if (isset($this->_params[$param])) {
     118        if (array_key_exists($param, $this->_params)) {
    119119            return $this->_params[$param];
    120120        } else {
  • trunk/lib/Currency.inc.php

    r468 r478  
    9898        $app =& App::getInstance();
    9999   
    100         if (isset($this->_params[$param])) {
     100        if (array_key_exists($param, $this->_params)) {
    101101            return $this->_params[$param];
    102102        } else {
  • trunk/lib/DB.inc.php

    r468 r478  
    122122        $app =& App::getInstance();
    123123
    124         if (isset($this->_params[$param])) {
     124        if (array_key_exists($param, $this->_params)) {
    125125            return $this->_params[$param];
    126126        } else {
  • trunk/lib/Email.inc.php

    r472 r478  
    149149        $app =& App::getInstance();
    150150
    151         if (isset($this->_params[$param])) {
     151        if (array_key_exists($param, $this->_params)) {
    152152            return $this->_params[$param];
    153153        } else {
  • trunk/lib/FormValidator.inc.php

    r468 r478  
    100100        $app =& App::getInstance();
    101101
    102         if (isset($this->_params[$param])) {
     102        if (array_key_exists($param, $this->_params)) {
    103103            return $this->_params[$param];
    104104        } else {
  • trunk/lib/ImageThumb.inc.php

    r477 r478  
    164164        $app =& App::getInstance();
    165165
    166         if (isset($this->_params[$param])) {
     166        if (array_key_exists($param, $this->_params)) {
    167167            return $this->_params[$param];
    168168        } else {
  • trunk/lib/JS.inc.php

    r473 r478  
    7171        $app =& App::getInstance();
    7272
    73         if (isset($this->_params[$param])) {
     73        if (array_key_exists($param, $this->_params)) {
    7474            return $this->_params[$param];
    7575        } else {
  • trunk/lib/Lock.inc.php

    r468 r478  
    162162        $app =& App::getInstance();
    163163
    164         if (isset($this->_params[$param])) {
     164        if (array_key_exists($param, $this->_params)) {
    165165            return $this->_params[$param];
    166166        } else {
  • trunk/lib/Navigation.inc.php

    r475 r478  
    114114        $app =& App::getInstance();
    115115
    116         if (isset($this->_params[$param])) {
     116        if (array_key_exists($param, $this->_params)) {
    117117            return $this->_params[$param];
    118118        } else {
  • trunk/lib/PEdit.inc.php

    r468 r478  
    144144        $app =& App::getInstance();
    145145
    146         if (isset($this->_params[$param])) {
     146        if (array_key_exists($param, $this->_params)) {
    147147            return $this->_params[$param];
    148148        } else {
  • trunk/lib/PayPal.inc.php

    r468 r478  
    257257        $app =& App::getInstance();
    258258   
    259         if (isset($this->_params[$param])) {
     259        if (array_key_exists($param, $this->_params)) {
    260260            return $this->_params[$param];
    261261        } else {
  • trunk/lib/Prefs.inc.php

    r477 r478  
    2424 * Prefs.inc.php
    2525 *
    26  * Prefs provides an API for saving arbitrary values in a user's session.
    27  * Session prefs can be stored into a database with the optional save() and load() methods.
     26 * Prefs provides an API for saving arbitrary values in a user's session, in cookies, and in the database.
     27 * Prefs can be stored into a database with the optional save() and load() methods.
    2828 *
    2929 * @author  Quinn Comendant <quinn@strangecode.com>
    30  * @version 2.1
     30 * @version 3.0
    3131 *
    3232 * Example of use (database storagetype):
     
    9898     * Prefs constructor.
    9999     */
    100     public function __construct($namespace='')
     100    public function __construct($namespace='', array $params=null)
    101101    {
    102102        $app =& App::getInstance();
    103103
    104104        $this->_ns = $namespace;
    105 
    106         // Initialized the prefs array.
    107         if (!isset($_SESSION['_prefs'][$this->_ns]['saved'])) {
    108             $this->clear();
    109         }
    110105
    111106        // Get create tables config from global context.
    112107        if (!is_null($app->getParam('db_create_tables'))) {
    113108            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
     109        }
     110
     111        // Optional initial params.
     112        $this->setParam($params);
     113
     114        // Run Prefs->save() upon script completion if we're using the database storagetype.
     115        // This only works if 'storagetype' is provided as a parameter to the constructor rather than via setParam() later.
     116        if ('database' == $this->getParam('storagetype')) {
     117            register_shutdown_function(array($this, 'save'));
    114118        }
    115119    }
     
    160164     * @param  array $params   Array of param keys and values to set.
    161165     */
    162     public function setParam($params=null)
     166    public function setParam(array $params=null)
    163167    {
    164168        // CLI scripts can't use prefs stored in HTTP-based protocols.
     
    191195        $app =& App::getInstance();
    192196
    193         if (isset($this->_params[$param])) {
     197        if (array_key_exists($param, $this->_params)) {
    194198            return $this->_params[$param];
    195199        } else {
     
    202206     * Sets the default values for preferences. If a preference is not explicitly
    203207     * set, the value set here will be used. Can be called multiple times to merge additional
    204      * defaults together.
     208     * defaults together. This is mostly only useful for the database storetype, when you have
     209     * values you want to use as default, and those are not stored to the database (so the defaults
     210     * can be changed later and apply to all users who haven't make s specific setting).
     211     * For the cookie storetype, using setDefaults just sets cookies but only if a cookie with
     212     * the same name is not already set.
    205213     *
    206214     * @param  array $defaults  Array of key-value pairs
     
    208216    public function setDefaults($defaults)
    209217    {
     218        $app =& App::getInstance();
     219
    210220        if (isset($defaults) && is_array($defaults)) {
    211             $_SESSION['_prefs'][$this->_ns]['defaults'] = array_merge($_SESSION['_prefs'][$this->_ns]['defaults'], $defaults);
    212         }
    213     }
    214 
    215     /**
    216      * Store a key-value pair in the session. If the value is different than what is set by setDefaults
    217      * the value will be scheduled to be saved in the database.
    218      * This function determines what data is saved to the database. Ensure clean values!
     221            switch ($this->getParam('storagetype')) {
     222            case 'session':
     223            case 'database':
     224                $_SESSION['_prefs'][$this->_ns]['defaults'] = array_merge($_SESSION['_prefs'][$this->_ns]['defaults'], $defaults);
     225                break;
     226
     227            case 'cookie':
     228                foreach ($defaults as $key => $val) {
     229                    if (!$this->exists($key)) {
     230                        $this->set($key, $val);
     231                    }
     232                }
     233                unset($key, $val);
     234                break;
     235            }
     236        } else {
     237            $app->logMsg(sprintf('Wrong data-type passed to Prefs->setDefaults().', null), LOG_NOTICE, __FILE__, __LINE__);
     238        }
     239    }
     240
     241    /**
     242     * Store a key-value pair.
     243     * When using the database storagetype, if the value is different than what is set by setDefaults the value will be scheduled to be saved in the database.
    219244     *
    220245     * @param  string $key          The name of the preference to modify.
     
    238263        }
    239264
    240         // Set a saved preference if...
    241         // - there isn't a default.
    242         // - or the new value is different than the default
    243         // - or there is a previously existing saved key.
    244265        switch ($this->getParam('storagetype')) {
     266        // Both session and database prefs are saved in the session (for database, only temporarily until they are saved).
    245267        case 'session':
    246         case 'database': // DB prefs are saved in the session temporarily until they are saved.
    247             if (!isset($_SESSION['_prefs'][$this->_ns]['defaults'][$key])
     268        case 'database':
     269            // Initialized the prefs array.
     270            if (!isset($_SESSION['_prefs'][$this->_ns]['saved'])) {
     271                $this->clear();
     272            }
     273            // Set a saved preference if...
     274            // - there isn't a default.
     275            // - or the new value is different than the default
     276            // - or there is a previously existing saved key.
     277            if (!array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])
    248278            || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val
    249             || isset($_SESSION['_prefs'][$this->_ns]['saved'][$key])) {
     279            || array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])) {
    250280                $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val;
    251                 $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
     281                $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
    252282            } else {
    253283                $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);
     
    259289            $val = json_encode($val);
    260290            $app->setCookie($name, $val, $this->getParam('cookie_expire'), $this->getParam('cookie_path'), $this->getParam('cookie_domain'));
     291            $_COOKIE[$name] = $val;
    261292            $app->logMsg(sprintf('Setting cookie preference %s => %s', $key, $val), LOG_DEBUG, __FILE__, __LINE__);
    262293            break;
     
    321352        case 'cookie':
    322353            $name = $this->_getCookieName($key);
    323             return isset($_COOKIE[$name]);
    324         }
    325 
    326     }
    327 
    328     /**
    329      * Clear a set preference value. This will also remove the value from the database.
     354            return array_key_exists($name, $_COOKIE);
     355        }
     356
     357    }
     358
     359    /**
     360     * Delete an existing preference value. This will also remove the value from the database, once save() is called.
    330361     *
    331362     * @param string $key       The name of the preference to delete.
     
    355386
    356387    /**
    357      * Resets the $_SESSION cache. This should be executed with the same consideration
    358      * as $auth->clear(), such as when logging out.
    359      */
    360     public function clear($focus='all')
    361     {
    362 
    363         switch ($focus) {
     388     * Resets all existing values under this namespace. This should be executed with the same consideration as $auth->clear(), such as when logging out.
     389     */
     390    public function clear($scope='all')
     391    {
     392        switch ($scope) {
    364393        case 'all' :
    365394            switch ($this->getParam('storagetype')) {
     
    376405                foreach ($_COOKIE as $key => $value) {
    377406                    // All cookie keys with our internal prefix. Use only the last part as the key.
    378                     if (preg_match('/^' . preg_quote(sprintf('strangecode-%s-', $this->_ns)) . '(.+)$/i', $key, $match)) {
     407                    if (preg_match('/^' . preg_quote(sprintf('_prefs-%s-', $this->_ns)) . '(.+)$/i', $key, $match)) {
    379408                        $this->delete($match[1]);
    380409                    }
     
    383412            }
    384413            break;
     414
    385415        case 'defaults' :
    386416            $_SESSION['_prefs'][$this->_ns]['defaults'] = array();
    387417            break;
     418
    388419        case 'saved' :
    389420            $_SESSION['_prefs'][$this->_ns]['saved'] = array();
     
    409440        // Skip this method if not using the db.
    410441        if ('database' != $this->getParam('storagetype')) {
     442            $app->logMsg('Prefs->load() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__);
    411443            return true;
    412444        }
     
    462494    {
    463495        if ('database' != $this->getParam('storagetype')) {
     496            $app->logMsg('Prefs->_isLoaded() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__);
    464497            return true;
    465498        }
     
    491524        // Skip this method if not using the db.
    492525        if ('database' != $this->getParam('storagetype')) {
     526            $app->logMsg('Prefs->save() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__);
    493527            return true;
    494528        }
     
    548582        $app =& App::getInstance();
    549583
    550         if (mb_strpos($key, sprintf('strangecode-%s', $this->_ns)) === 0) {
    551             $app->logMsg(sprintf('Invalid key name (%s). Leave off "strangecode-%s-" and it should work.', $key, $this->_ns), LOG_NOTICE, __FILE__, __LINE__);
    552         }
    553         return sprintf('strangecode-%s-%s', $this->_ns, $key);
     584        if (mb_strpos($key, sprintf('_prefs-%s', $this->_ns)) === 0) {
     585            $app->logMsg(sprintf('Invalid key name (%s). Leave off "_prefs-%s-" and it should work.', $key, $this->_ns), LOG_NOTICE, __FILE__, __LINE__);
     586        }
     587        // Use standardized class data names: _ + classname + namespace + variablekey
     588        return sprintf('_prefs-%s-%s', $this->_ns, $key);
    554589    }
    555590}
  • trunk/lib/SpellCheck.inc.php

    r468 r478  
    141141        $app =& App::getInstance();
    142142   
    143         if (isset($this->_params[$param])) {
     143        if (array_key_exists($param, $this->_params)) {
    144144            return $this->_params[$param];
    145145        } else {
  • trunk/lib/Upload.inc.php

    r477 r478  
    122122        $app =& App::getInstance();
    123123
    124         if (isset($this->_params[$param])) {
     124        if (array_key_exists($param, $this->_params)) {
    125125            return $this->_params[$param];
    126126        } else {
  • trunk/lib/Version.inc.php

    r468 r478  
    175175        $app =& App::getInstance();
    176176
    177         if (isset($this->_params[$param])) {
     177        if (array_key_exists($param, $this->_params)) {
    178178            return $this->_params[$param];
    179179        } else {
Note: See TracChangeset for help on using the changeset viewer.