- Timestamp:
- Oct 3, 2018 6:25:19 PM (6 years ago)
- Location:
- trunk/lib
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r615 r644 96 96 // This value is overwritten by the $app->getParam('db_create_tables') setting if it is available. 97 97 'create_table' => true, 98 99 // Original namespace set during __construct(). 100 // 'namespace' => '', 98 101 ); 99 102 … … 106 109 107 110 $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)); 108 114 109 115 // Get create tables config from global context. … … 114 120 // Optional initial params. 115 121 $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 }121 122 122 123 // Run Prefs->save() upon script completion if we're using the database storagetype. … … 191 192 } 192 193 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 193 213 if (isset($params) && is_array($params)) { 194 214 // Merge new parameters with old overriding only those passed. … … 213 233 $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); 214 234 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(); 215 251 } 216 252 } … … 230 266 { 231 267 $app =& App::getInstance(); 268 269 $this->_init(); 232 270 233 271 if (isset($defaults) && is_array($defaults)) { … … 275 313 return false; 276 314 } 315 316 $this->_init(); 277 317 278 318 switch ($this->getParam('storagetype')) { … … 317 357 $app =& App::getInstance(); 318 358 359 $this->_init(); 360 319 361 switch ($this->getParam('storagetype')) { 320 362 case 'session': … … 352 394 public function exists($key) 353 395 { 396 $this->_init(); 397 354 398 switch ($this->getParam('storagetype')) { 355 399 case 'session': … … 372 416 { 373 417 $app =& App::getInstance(); 418 419 $this->_init(); 374 420 375 421 switch ($this->getParam('storagetype')) { … … 394 440 /** 395 441 * 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) 398 445 { 399 446 $app =& App::getInstance(); … … 431 478 } 432 479 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 } 434 486 } 435 487 … … 457 509 $this->initDB(); 458 510 511 $this->_init(); 512 459 513 // Prefs already loaded for this session. 460 514 if (!$force && $this->_isLoaded()) { … … 476 530 FROM " . $db->escapeString($this->getParam('db_table')) . " 477 531 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')) . "' 479 533 LIMIT 10000 480 534 "); … … 509 563 } 510 564 565 $this->_init(); 566 511 567 if (isset($_SESSION['_prefs'][$this->_ns]['load_datetime']) 512 568 && strtotime($_SESSION['_prefs'][$this->_ns]['load_datetime']) > time() - $this->getParam('load_timeout') … … 528 584 * @since 04 Jun 2006 17:19:56 529 585 */ 530 public function save( )586 public function save($allow_empty=false) 531 587 { 532 588 $app =& App::getInstance(); … … 547 603 $this->initDB(); 548 604 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']))) { 550 608 // Delete old prefs from database. 551 609 $db->query(" 552 610 DELETE FROM " . $db->escapeString($this->getParam('db_table')) . " 553 611 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')) . "' 555 613 "); 556 614 … … 560 618 $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", 561 619 $db->escapeString($this->getParam('user_id')), 562 $db->escapeString($this-> _ns),620 $db->escapeString($this->getParam('namespace')), 563 621 $db->escapeString($key), 564 622 $db->escapeString(serialize($val)) 565 623 ); 566 624 } 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 575 635 return true; 576 636 } … … 597 657 } 598 658 // Use standardized class data names: _ + classname + namespace + variablekey 659 // (namespace = namespace + user_id) 599 660 return sprintf('_prefs-%s-%s', $this->_ns, $key); 600 661 } -
trunk/lib/Utilities.inc.php
r623 r644 147 147 $output .= sprintf("%s%s\n", $indent, $var); 148 148 } 149 return $output;149 return preg_replace(['/^[ \t]+$/', '/\n\n\n+/'], ['', "\n\n"], $output); 150 150 } 151 151
Note: See TracChangeset
for help on using the changeset viewer.