Changeset 478
- Timestamp:
- May 3, 2014 9:13:05 PM (10 years ago)
- Location:
- trunk/lib
- Files:
-
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/ACL.inc.php
r468 r478 114 114 $app =& App::getInstance(); 115 115 116 if ( isset($this->_params[$param])) {116 if (array_key_exists($param, $this->_params)) { 117 117 return $this->_params[$param]; 118 118 } else { -
trunk/lib/App.inc.php
r477 r478 230 230 if ($param === null) { 231 231 return $this->_params; 232 } else if ( isset($this->_params[$param])) {232 } else if (array_key_exists($param, $this->_params)) { 233 233 return $this->_params[$param]; 234 234 } else { … … 802 802 } 803 803 foreach ($query_key as $k) { 804 if ( isset($this->_carry_queries[$k])) {804 if (array_key_exists($k, $this->_carry_queries)) { 805 805 // Remove the value of the specified query argument from the _carry_queries array. 806 806 $this->logMsg(sprintf('Dropping carried query: %s => %s', $k, $this->_carry_queries[$k]), LOG_DEBUG, __FILE__, __LINE__); 807 807 unset($this->_carry_queries[$k]); 808 808 } 809 if ($unset && isset($_REQUEST[$k])) {809 if ($unset && array_key_exists($k, $_REQUEST)) { 810 810 unset($_REQUEST[$k], $_GET[$k], $_POST[$k], $_COOKIE[$k]); 811 811 } -
trunk/lib/Auth_File.inc.php
r468 r478 119 119 $app = &App::getInstance(); 120 120 121 if ( isset($this->_params[$param])) {121 if (array_key_exists($param, $this->_params)) { 122 122 return $this->_params[$param]; 123 123 } else { -
trunk/lib/Auth_SQL.inc.php
r468 r478 278 278 $app =& App::getInstance(); 279 279 280 if ( isset($this->_params[$param])) {280 if (array_key_exists($param, $this->_params)) { 281 281 return $this->_params[$param]; 282 282 } else { -
trunk/lib/AuthorizeNet.inc.php
r468 r478 176 176 $app =& App::getInstance(); 177 177 178 if ( isset($this->_params[$param])) {178 if (array_key_exists($param, $this->_params)) { 179 179 return $this->_params[$param]; 180 180 } else { -
trunk/lib/CSS.inc.php
r473 r478 71 71 $app =& App::getInstance(); 72 72 73 if ( isset($this->_params[$param])) {73 if (array_key_exists($param, $this->_params)) { 74 74 return $this->_params[$param]; 75 75 } else { -
trunk/lib/Cache.inc.php
r468 r478 131 131 $app =& App::getInstance(); 132 132 133 if ( isset($this->_params[$param])) {133 if (array_key_exists($param, $this->_params)) { 134 134 return $this->_params[$param]; 135 135 } else { -
trunk/lib/Cart.inc.php
r468 r478 116 116 $app =& App::getInstance(); 117 117 118 if ( isset($this->_params[$param])) {118 if (array_key_exists($param, $this->_params)) { 119 119 return $this->_params[$param]; 120 120 } else { -
trunk/lib/Currency.inc.php
r468 r478 98 98 $app =& App::getInstance(); 99 99 100 if ( isset($this->_params[$param])) {100 if (array_key_exists($param, $this->_params)) { 101 101 return $this->_params[$param]; 102 102 } else { -
trunk/lib/DB.inc.php
r468 r478 122 122 $app =& App::getInstance(); 123 123 124 if ( isset($this->_params[$param])) {124 if (array_key_exists($param, $this->_params)) { 125 125 return $this->_params[$param]; 126 126 } else { -
trunk/lib/Email.inc.php
r472 r478 149 149 $app =& App::getInstance(); 150 150 151 if ( isset($this->_params[$param])) {151 if (array_key_exists($param, $this->_params)) { 152 152 return $this->_params[$param]; 153 153 } else { -
trunk/lib/FormValidator.inc.php
r468 r478 100 100 $app =& App::getInstance(); 101 101 102 if ( isset($this->_params[$param])) {102 if (array_key_exists($param, $this->_params)) { 103 103 return $this->_params[$param]; 104 104 } else { -
trunk/lib/ImageThumb.inc.php
r477 r478 164 164 $app =& App::getInstance(); 165 165 166 if ( isset($this->_params[$param])) {166 if (array_key_exists($param, $this->_params)) { 167 167 return $this->_params[$param]; 168 168 } else { -
trunk/lib/JS.inc.php
r473 r478 71 71 $app =& App::getInstance(); 72 72 73 if ( isset($this->_params[$param])) {73 if (array_key_exists($param, $this->_params)) { 74 74 return $this->_params[$param]; 75 75 } else { -
trunk/lib/Lock.inc.php
r468 r478 162 162 $app =& App::getInstance(); 163 163 164 if ( isset($this->_params[$param])) {164 if (array_key_exists($param, $this->_params)) { 165 165 return $this->_params[$param]; 166 166 } else { -
trunk/lib/Navigation.inc.php
r475 r478 114 114 $app =& App::getInstance(); 115 115 116 if ( isset($this->_params[$param])) {116 if (array_key_exists($param, $this->_params)) { 117 117 return $this->_params[$param]; 118 118 } else { -
trunk/lib/PEdit.inc.php
r468 r478 144 144 $app =& App::getInstance(); 145 145 146 if ( isset($this->_params[$param])) {146 if (array_key_exists($param, $this->_params)) { 147 147 return $this->_params[$param]; 148 148 } else { -
trunk/lib/PayPal.inc.php
r468 r478 257 257 $app =& App::getInstance(); 258 258 259 if ( isset($this->_params[$param])) {259 if (array_key_exists($param, $this->_params)) { 260 260 return $this->_params[$param]; 261 261 } else { -
trunk/lib/Prefs.inc.php
r477 r478 24 24 * Prefs.inc.php 25 25 * 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. 28 28 * 29 29 * @author Quinn Comendant <quinn@strangecode.com> 30 * @version 2.130 * @version 3.0 31 31 * 32 32 * Example of use (database storagetype): … … 98 98 * Prefs constructor. 99 99 */ 100 public function __construct($namespace='' )100 public function __construct($namespace='', array $params=null) 101 101 { 102 102 $app =& App::getInstance(); 103 103 104 104 $this->_ns = $namespace; 105 106 // Initialized the prefs array.107 if (!isset($_SESSION['_prefs'][$this->_ns]['saved'])) {108 $this->clear();109 }110 105 111 106 // Get create tables config from global context. 112 107 if (!is_null($app->getParam('db_create_tables'))) { 113 108 $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')); 114 118 } 115 119 } … … 160 164 * @param array $params Array of param keys and values to set. 161 165 */ 162 public function setParam( $params=null)166 public function setParam(array $params=null) 163 167 { 164 168 // CLI scripts can't use prefs stored in HTTP-based protocols. … … 191 195 $app =& App::getInstance(); 192 196 193 if ( isset($this->_params[$param])) {197 if (array_key_exists($param, $this->_params)) { 194 198 return $this->_params[$param]; 195 199 } else { … … 202 206 * Sets the default values for preferences. If a preference is not explicitly 203 207 * 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. 205 213 * 206 214 * @param array $defaults Array of key-value pairs … … 208 216 public function setDefaults($defaults) 209 217 { 218 $app =& App::getInstance(); 219 210 220 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. 219 244 * 220 245 * @param string $key The name of the preference to modify. … … 238 263 } 239 264 240 // Set a saved preference if...241 // - there isn't a default.242 // - or the new value is different than the default243 // - or there is a previously existing saved key.244 265 switch ($this->getParam('storagetype')) { 266 // Both session and database prefs are saved in the session (for database, only temporarily until they are saved). 245 267 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']) 248 278 || $_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'])) { 250 280 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; 251 281 $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); 252 282 } else { 253 283 $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); … … 259 289 $val = json_encode($val); 260 290 $app->setCookie($name, $val, $this->getParam('cookie_expire'), $this->getParam('cookie_path'), $this->getParam('cookie_domain')); 291 $_COOKIE[$name] = $val; 261 292 $app->logMsg(sprintf('Setting cookie preference %s => %s', $key, $val), LOG_DEBUG, __FILE__, __LINE__); 262 293 break; … … 321 352 case 'cookie': 322 353 $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. 330 361 * 331 362 * @param string $key The name of the preference to delete. … … 355 386 356 387 /** 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) { 364 393 case 'all' : 365 394 switch ($this->getParam('storagetype')) { … … 376 405 foreach ($_COOKIE as $key => $value) { 377 406 // 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)) { 379 408 $this->delete($match[1]); 380 409 } … … 383 412 } 384 413 break; 414 385 415 case 'defaults' : 386 416 $_SESSION['_prefs'][$this->_ns]['defaults'] = array(); 387 417 break; 418 388 419 case 'saved' : 389 420 $_SESSION['_prefs'][$this->_ns]['saved'] = array(); … … 409 440 // Skip this method if not using the db. 410 441 if ('database' != $this->getParam('storagetype')) { 442 $app->logMsg('Prefs->load() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); 411 443 return true; 412 444 } … … 462 494 { 463 495 if ('database' != $this->getParam('storagetype')) { 496 $app->logMsg('Prefs->_isLoaded() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); 464 497 return true; 465 498 } … … 491 524 // Skip this method if not using the db. 492 525 if ('database' != $this->getParam('storagetype')) { 526 $app->logMsg('Prefs->save() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); 493 527 return true; 494 528 } … … 548 582 $app =& App::getInstance(); 549 583 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); 554 589 } 555 590 } -
trunk/lib/SpellCheck.inc.php
r468 r478 141 141 $app =& App::getInstance(); 142 142 143 if ( isset($this->_params[$param])) {143 if (array_key_exists($param, $this->_params)) { 144 144 return $this->_params[$param]; 145 145 } else { -
trunk/lib/Upload.inc.php
r477 r478 122 122 $app =& App::getInstance(); 123 123 124 if ( isset($this->_params[$param])) {124 if (array_key_exists($param, $this->_params)) { 125 125 return $this->_params[$param]; 126 126 } else { -
trunk/lib/Version.inc.php
r468 r478 175 175 $app =& App::getInstance(); 176 176 177 if ( isset($this->_params[$param])) {177 if (array_key_exists($param, $this->_params)) { 178 178 return $this->_params[$param]; 179 179 } else {
Note: See TracChangeset
for help on using the changeset viewer.