[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[462] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[462] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[462] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[136] | 24 | * Prefs.inc.php |
---|
[1] | 25 | * |
---|
[478] | 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. |
---|
[136] | 28 | * |
---|
[1] | 29 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[478] | 30 | * @version 3.0 |
---|
[481] | 31 | * @todo This class could really benefit from being refactored using the factory pattern, with backend storage mechanisms. |
---|
[462] | 32 | * |
---|
[477] | 33 | * Example of use (database storagetype): |
---|
[150] | 34 | --------------------------------------------------------------------- |
---|
[151] | 35 | // Load preferences for the user's session. |
---|
[150] | 36 | require_once 'codebase/lib/Prefs.inc.php'; |
---|
[153] | 37 | $prefs = new Prefs('my-namespace'); |
---|
[150] | 38 | $prefs->setParam(array( |
---|
[477] | 39 | 'storagetype' => ($auth->isLoggedIn() ? 'database' : 'session'), |
---|
[150] | 40 | 'user_id' => $auth->get('user_id'), |
---|
| 41 | )); |
---|
| 42 | $prefs->setDefaults(array( |
---|
| 43 | 'search_num_results' => 25, |
---|
| 44 | 'datalog_num_entries' => 25, |
---|
| 45 | )); |
---|
| 46 | $prefs->load(); |
---|
[151] | 47 | |
---|
| 48 | // Update preferences. Make sure to validate this input first! |
---|
| 49 | $prefs->set('search_num_results', getFormData('search_num_results')); |
---|
| 50 | $prefs->set('datalog_num_entries', getFormData('datalog_num_entries')); |
---|
| 51 | $prefs->save(); |
---|
[150] | 52 | --------------------------------------------------------------------- |
---|
[1] | 53 | */ |
---|
[502] | 54 | class Prefs |
---|
| 55 | { |
---|
[1] | 56 | |
---|
[136] | 57 | // Namespace of this instance of Prefs. |
---|
[484] | 58 | protected $_ns; |
---|
[1] | 59 | |
---|
[152] | 60 | // Configuration parameters for this object. |
---|
[484] | 61 | protected $_params = array( |
---|
[462] | 62 | |
---|
[481] | 63 | // Store preferences in one of the available storage mechanisms: session, cookie, database |
---|
| 64 | // This default should remain set to 'session' for legacy support. |
---|
| 65 | 'storagetype' => 'session', |
---|
| 66 | |
---|
| 67 | // This parameter is only used for legacy support, superceeded by the 'storagetype' setting. |
---|
[152] | 68 | // Enable database storage. If this is false, all prefs will live only as long as the session. |
---|
[477] | 69 | 'persistent' => null, |
---|
[462] | 70 | |
---|
[477] | 71 | // ---------------------------------------------------------- |
---|
| 72 | // Cookie-type settings. |
---|
| 73 | |
---|
| 74 | // Lifespan of the cookie. If set to an integer, interpreted as a timestamp (0 for 'when user closes browser'), otherwise as a strtotime-compatible value ('tomorrow', etc). |
---|
| 75 | 'cookie_expire' => '+10 years', |
---|
| 76 | |
---|
| 77 | // The path on the server in which the cookie will be available on. |
---|
| 78 | 'cookie_path' => null, |
---|
| 79 | |
---|
| 80 | // The domain that the cookie is available to. |
---|
| 81 | 'cookie_domain' => null, |
---|
| 82 | |
---|
| 83 | // ---------------------------------------------------------- |
---|
| 84 | // Database-type settings. |
---|
| 85 | |
---|
| 86 | // The current user_id for which to load/save database-backed preferences. |
---|
[149] | 87 | 'user_id' => null, |
---|
[462] | 88 | |
---|
[541] | 89 | // How long before we force a reload of the persistent prefs data? 300 = every five minutes. |
---|
| 90 | 'load_timeout' => 300, |
---|
[462] | 91 | |
---|
[481] | 92 | // Name of database table to store prefs. |
---|
[147] | 93 | 'db_table' => 'pref_tbl', |
---|
[146] | 94 | |
---|
| 95 | // Automatically create table and verify columns. Better set to false after site launch. |
---|
[396] | 96 | // This value is overwritten by the $app->getParam('db_create_tables') setting if it is available. |
---|
[146] | 97 | 'create_table' => true, |
---|
| 98 | ); |
---|
| 99 | |
---|
[1] | 100 | /** |
---|
| 101 | * Prefs constructor. |
---|
| 102 | */ |
---|
[478] | 103 | public function __construct($namespace='', array $params=null) |
---|
[1] | 104 | { |
---|
[477] | 105 | $app =& App::getInstance(); |
---|
[146] | 106 | |
---|
[154] | 107 | $this->_ns = $namespace; |
---|
[462] | 108 | |
---|
[146] | 109 | // Get create tables config from global context. |
---|
| 110 | if (!is_null($app->getParam('db_create_tables'))) { |
---|
| 111 | $this->setParam(array('create_table' => $app->getParam('db_create_tables'))); |
---|
| 112 | } |
---|
[478] | 113 | |
---|
| 114 | // Optional initial params. |
---|
| 115 | $this->setParam($params); |
---|
| 116 | |
---|
[481] | 117 | // Initialized the prefs array. |
---|
| 118 | if ('cookie' != $app->getParam('storagetype') && !isset($_SESSION['_prefs'][$this->_ns]['saved'])) { |
---|
| 119 | $this->clear(); |
---|
| 120 | } |
---|
| 121 | |
---|
[478] | 122 | // Run Prefs->save() upon script completion if we're using the database storagetype. |
---|
| 123 | // This only works if 'storagetype' is provided as a parameter to the constructor rather than via setParam() later. |
---|
| 124 | if ('database' == $this->getParam('storagetype')) { |
---|
| 125 | register_shutdown_function(array($this, 'save')); |
---|
| 126 | } |
---|
[1] | 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
[146] | 130 | * Setup the database table for this class. |
---|
| 131 | * |
---|
| 132 | * @access public |
---|
| 133 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 134 | * @since 04 Jun 2006 16:41:42 |
---|
| 135 | */ |
---|
[468] | 136 | public function initDB($recreate_db=false) |
---|
[146] | 137 | { |
---|
[477] | 138 | $app =& App::getInstance(); |
---|
| 139 | $db =& DB::getInstance(); |
---|
[146] | 140 | |
---|
| 141 | static $_db_tested = false; |
---|
| 142 | |
---|
| 143 | if ($recreate_db || !$_db_tested && $this->getParam('create_table')) { |
---|
| 144 | if ($recreate_db) { |
---|
| 145 | $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table')); |
---|
[201] | 146 | $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_INFO, __FILE__, __LINE__); |
---|
[146] | 147 | } |
---|
| 148 | $db->query("CREATE TABLE IF NOT EXISTS " . $db->escapeString($this->getParam('db_table')) . " ( |
---|
| 149 | user_id VARCHAR(32) NOT NULL DEFAULT '', |
---|
| 150 | pref_namespace VARCHAR(32) NOT NULL DEFAULT '', |
---|
| 151 | pref_key VARCHAR(64) NOT NULL DEFAULT '', |
---|
| 152 | pref_value TEXT, |
---|
| 153 | PRIMARY KEY (user_id, pref_namespace, pref_key) |
---|
| 154 | )"); |
---|
| 155 | |
---|
| 156 | if (!$db->columnExists($this->getParam('db_table'), array( |
---|
| 157 | 'user_id', |
---|
| 158 | 'pref_namespace', |
---|
| 159 | 'pref_key', |
---|
| 160 | 'pref_value', |
---|
| 161 | ), false, false)) { |
---|
| 162 | $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__); |
---|
| 163 | trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | $_db_tested = true; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Set the params of this object. |
---|
| 171 | * |
---|
| 172 | * @param array $params Array of param keys and values to set. |
---|
| 173 | */ |
---|
[478] | 174 | public function setParam(array $params=null) |
---|
[146] | 175 | { |
---|
[548] | 176 | $app =& App::getInstance(); |
---|
| 177 | |
---|
[477] | 178 | // CLI scripts can't use prefs stored in HTTP-based protocols. |
---|
[547] | 179 | if ($app->cli |
---|
[477] | 180 | && isset($params['storagetype']) |
---|
| 181 | && in_array($params['storagetype'], array('cookie', 'session'))) { |
---|
| 182 | $app->logMsg(sprintf('Storage type %s not available for CLI', $params['storagetype']), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | // Convert the legacy param 'persistent' to 'storagetype=database'. |
---|
[481] | 186 | // Old sites would set 'persistent' to true (use database) or false (use sessions). |
---|
| 187 | // If it is true, we set storagetype=database here. |
---|
| 188 | // If false, we rely on the default, sessions (which is assigned in the params). |
---|
[477] | 189 | if (isset($params['persistent']) && $params['persistent'] && !isset($params['storagetype'])) { |
---|
| 190 | $params['storagetype'] = 'database'; |
---|
| 191 | } |
---|
| 192 | |
---|
[146] | 193 | if (isset($params) && is_array($params)) { |
---|
| 194 | // Merge new parameters with old overriding only those passed. |
---|
| 195 | $this->_params = array_merge($this->_params, $params); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * Return the value of a parameter, if it exists. |
---|
| 201 | * |
---|
| 202 | * @access public |
---|
| 203 | * @param string $param Which parameter to return. |
---|
| 204 | * @return mixed Configured parameter value. |
---|
| 205 | */ |
---|
[468] | 206 | public function getParam($param) |
---|
[146] | 207 | { |
---|
[477] | 208 | $app =& App::getInstance(); |
---|
[462] | 209 | |
---|
[478] | 210 | if (array_key_exists($param, $this->_params)) { |
---|
[146] | 211 | return $this->_params[$param]; |
---|
| 212 | } else { |
---|
| 213 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 214 | return null; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
[462] | 219 | * Sets the default values for preferences. If a preference is not explicitly |
---|
[151] | 220 | * set, the value set here will be used. Can be called multiple times to merge additional |
---|
[481] | 221 | * defaults together. This is mostly only useful for the database storagetype, when you have |
---|
[478] | 222 | * values you want to use as default, and those are not stored to the database (so the defaults |
---|
| 223 | * can be changed later and apply to all users who haven't make s specific setting). |
---|
[481] | 224 | * For the cookie storagetype, using setDefaults just sets cookies but only if a cookie with |
---|
[478] | 225 | * the same name is not already set. |
---|
[1] | 226 | * |
---|
[462] | 227 | * @param array $defaults Array of key-value pairs |
---|
[1] | 228 | */ |
---|
[468] | 229 | public function setDefaults($defaults) |
---|
[1] | 230 | { |
---|
[478] | 231 | $app =& App::getInstance(); |
---|
| 232 | |
---|
[146] | 233 | if (isset($defaults) && is_array($defaults)) { |
---|
[478] | 234 | switch ($this->getParam('storagetype')) { |
---|
| 235 | case 'session': |
---|
| 236 | case 'database': |
---|
| 237 | $_SESSION['_prefs'][$this->_ns]['defaults'] = array_merge($_SESSION['_prefs'][$this->_ns]['defaults'], $defaults); |
---|
| 238 | break; |
---|
| 239 | |
---|
| 240 | case 'cookie': |
---|
| 241 | foreach ($defaults as $key => $val) { |
---|
| 242 | if (!$this->exists($key)) { |
---|
| 243 | $this->set($key, $val); |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | unset($key, $val); |
---|
| 247 | break; |
---|
| 248 | } |
---|
| 249 | } else { |
---|
| 250 | $app->logMsg(sprintf('Wrong data-type passed to Prefs->setDefaults().', null), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
[478] | 255 | * Store a key-value pair. |
---|
| 256 | * 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. |
---|
[1] | 257 | * |
---|
[152] | 258 | * @param string $key The name of the preference to modify. |
---|
| 259 | * @param string $val The new value for this preference. |
---|
[1] | 260 | */ |
---|
[468] | 261 | public function set($key, $val) |
---|
[1] | 262 | { |
---|
[151] | 263 | $app =& App::getInstance(); |
---|
[152] | 264 | |
---|
[477] | 265 | if (!is_string($key)) { |
---|
| 266 | $app->logMsg(sprintf('Key is not a string-compatible type (%s)', getDump($key)), LOG_NOTICE, __FILE__, __LINE__); |
---|
[151] | 267 | return false; |
---|
[149] | 268 | } |
---|
[477] | 269 | if ('' == trim($key)) { |
---|
| 270 | $app->logMsg(sprintf('Key is empty (along with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 271 | return false; |
---|
| 272 | } |
---|
| 273 | if (!is_scalar($val) && !is_array($val) && !is_object($val)) { |
---|
[479] | 274 | $app->logMsg(sprintf('Value is not a compatible data type (%s=%s)', $key, getDump($val)), LOG_WARNING, __FILE__, __LINE__); |
---|
[477] | 275 | return false; |
---|
| 276 | } |
---|
[462] | 277 | |
---|
[477] | 278 | switch ($this->getParam('storagetype')) { |
---|
[478] | 279 | // Both session and database prefs are saved in the session (for database, only temporarily until they are saved). |
---|
[477] | 280 | case 'session': |
---|
[478] | 281 | case 'database': |
---|
| 282 | // Set a saved preference if... |
---|
| 283 | // - there isn't a default. |
---|
| 284 | // - or the new value is different than the default |
---|
| 285 | // - or there is a previously existing saved key. |
---|
[480] | 286 | if (!(isset($_SESSION['_prefs'][$this->_ns]['defaults']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])) |
---|
[477] | 287 | || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val |
---|
[480] | 288 | || (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved']))) { |
---|
[477] | 289 | $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; |
---|
[478] | 290 | $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[477] | 291 | } else { |
---|
| 292 | $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 293 | } |
---|
| 294 | break; |
---|
| 295 | |
---|
| 296 | case 'cookie': |
---|
| 297 | $name = $this->_getCookieName($key); |
---|
| 298 | $val = json_encode($val); |
---|
| 299 | $app->setCookie($name, $val, $this->getParam('cookie_expire'), $this->getParam('cookie_path'), $this->getParam('cookie_domain')); |
---|
[478] | 300 | $_COOKIE[$name] = $val; |
---|
[477] | 301 | $app->logMsg(sprintf('Setting cookie preference %s => %s', $key, $val), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 302 | break; |
---|
[151] | 303 | } |
---|
[477] | 304 | |
---|
[1] | 305 | } |
---|
[42] | 306 | |
---|
[1] | 307 | /** |
---|
[462] | 308 | * Returns the value of the requested preference. Saved values take precedence, but if none is set |
---|
[151] | 309 | * a default value is returned, or if not that, null. |
---|
[1] | 310 | * |
---|
[136] | 311 | * @param string $key The name of the preference to retrieve. |
---|
[1] | 312 | * |
---|
| 313 | * @return string The value of the preference. |
---|
| 314 | */ |
---|
[468] | 315 | public function get($key) |
---|
[1] | 316 | { |
---|
[151] | 317 | $app =& App::getInstance(); |
---|
[477] | 318 | |
---|
| 319 | switch ($this->getParam('storagetype')) { |
---|
| 320 | case 'session': |
---|
| 321 | case 'database': |
---|
| 322 | if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])) { |
---|
| 323 | $app->logMsg(sprintf('Found %s in saved', $key), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 324 | return $_SESSION['_prefs'][$this->_ns]['saved'][$key]; |
---|
| 325 | } else if (isset($_SESSION['_prefs'][$this->_ns]['defaults']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])) { |
---|
| 326 | $app->logMsg(sprintf('Found %s in defaults', $key), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 327 | return $_SESSION['_prefs'][$this->_ns]['defaults'][$key]; |
---|
| 328 | } else { |
---|
| 329 | $app->logMsg(sprintf('Key not found in prefs cache: %s', $key), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 330 | return null; |
---|
| 331 | } |
---|
| 332 | break; |
---|
| 333 | |
---|
| 334 | case 'cookie': |
---|
| 335 | $name = $this->_getCookieName($key); |
---|
| 336 | if ($this->exists($key)) { |
---|
[526] | 337 | $val = json_decode($_COOKIE[$name], true); |
---|
[477] | 338 | $app->logMsg(sprintf('Found %s in cookie: %s', $key, getDump($val)), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 339 | return $val; |
---|
| 340 | } else { |
---|
| 341 | $app->logMsg(sprintf('Key not found in cookie: %s', $key), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 342 | return null; |
---|
| 343 | } |
---|
| 344 | break; |
---|
[151] | 345 | } |
---|
[1] | 346 | } |
---|
[42] | 347 | |
---|
[1] | 348 | /** |
---|
[152] | 349 | * To see if a preference has been set. |
---|
[1] | 350 | * |
---|
[136] | 351 | * @param string $key The name of the preference to check. |
---|
[151] | 352 | * @return boolean True if the preference isset and not empty false otherwise. |
---|
[1] | 353 | */ |
---|
[468] | 354 | public function exists($key) |
---|
[1] | 355 | { |
---|
[477] | 356 | switch ($this->getParam('storagetype')) { |
---|
| 357 | case 'session': |
---|
| 358 | case 'database': |
---|
[480] | 359 | return (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])); |
---|
[477] | 360 | |
---|
| 361 | case 'cookie': |
---|
| 362 | $name = $this->_getCookieName($key); |
---|
[480] | 363 | return (isset($_COOKIE) && array_key_exists($name, $_COOKIE)); |
---|
[477] | 364 | } |
---|
| 365 | |
---|
[1] | 366 | } |
---|
[42] | 367 | |
---|
[1] | 368 | /** |
---|
[478] | 369 | * Delete an existing preference value. This will also remove the value from the database, once save() is called. |
---|
[1] | 370 | * |
---|
[146] | 371 | * @param string $key The name of the preference to delete. |
---|
[1] | 372 | */ |
---|
[468] | 373 | public function delete($key) |
---|
[1] | 374 | { |
---|
[477] | 375 | $app =& App::getInstance(); |
---|
| 376 | |
---|
| 377 | switch ($this->getParam('storagetype')) { |
---|
| 378 | case 'session': |
---|
| 379 | case 'database': |
---|
| 380 | unset($_SESSION['_prefs'][$this->_ns]['saved'][$key]); |
---|
| 381 | break; |
---|
| 382 | |
---|
| 383 | case 'cookie': |
---|
| 384 | if ($this->exists($key)) { |
---|
| 385 | // Just set the existing value to an empty string, which expires in the past. |
---|
| 386 | $name = $this->_getCookieName($key); |
---|
| 387 | $app->setCookie($name, '', time() - 86400); |
---|
| 388 | // Also unset the received cookie value, so it is unavailable. |
---|
| 389 | unset($_COOKIE[$name]); |
---|
| 390 | } |
---|
| 391 | break; |
---|
| 392 | } |
---|
| 393 | |
---|
[1] | 394 | } |
---|
| 395 | |
---|
| 396 | /** |
---|
[478] | 397 | * Resets all existing values under this namespace. This should be executed with the same consideration as $auth->clear(), such as when logging out. |
---|
[1] | 398 | */ |
---|
[478] | 399 | public function clear($scope='all') |
---|
[1] | 400 | { |
---|
[478] | 401 | switch ($scope) { |
---|
[241] | 402 | case 'all' : |
---|
[477] | 403 | switch ($this->getParam('storagetype')) { |
---|
| 404 | case 'session': |
---|
| 405 | case 'database': |
---|
| 406 | $_SESSION['_prefs'][$this->_ns] = array( |
---|
| 407 | 'loaded' => false, |
---|
| 408 | 'load_datetime' => '1970-01-01', |
---|
| 409 | 'defaults' => array(), |
---|
| 410 | 'saved' => array(), |
---|
| 411 | ); |
---|
| 412 | break; |
---|
| 413 | case 'cookie': |
---|
| 414 | foreach ($_COOKIE as $key => $value) { |
---|
| 415 | // All cookie keys with our internal prefix. Use only the last part as the key. |
---|
[478] | 416 | if (preg_match('/^' . preg_quote(sprintf('_prefs-%s-', $this->_ns)) . '(.+)$/i', $key, $match)) { |
---|
[477] | 417 | $this->delete($match[1]); |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | break; |
---|
| 421 | } |
---|
[241] | 422 | break; |
---|
[478] | 423 | |
---|
[241] | 424 | case 'defaults' : |
---|
| 425 | $_SESSION['_prefs'][$this->_ns]['defaults'] = array(); |
---|
| 426 | break; |
---|
[478] | 427 | |
---|
[462] | 428 | case 'saved' : |
---|
| 429 | $_SESSION['_prefs'][$this->_ns]['saved'] = array(); |
---|
[241] | 430 | break; |
---|
| 431 | } |
---|
[1] | 432 | } |
---|
[462] | 433 | |
---|
[146] | 434 | /* |
---|
[151] | 435 | * Retrieves all prefs from the database and stores them in the $_SESSION. |
---|
[146] | 436 | * |
---|
| 437 | * @access public |
---|
[150] | 438 | * @param bool $force Set to always load from database, regardless if _isLoaded() or not. |
---|
[146] | 439 | * @return bool True if loading succeeded. |
---|
| 440 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 441 | * @version 1.0 |
---|
| 442 | * @since 04 Jun 2006 16:56:53 |
---|
| 443 | */ |
---|
[468] | 444 | public function load($force=false) |
---|
[146] | 445 | { |
---|
| 446 | $app =& App::getInstance(); |
---|
[477] | 447 | $db =& DB::getInstance(); |
---|
[462] | 448 | |
---|
[477] | 449 | // Skip this method if not using the db. |
---|
| 450 | if ('database' != $this->getParam('storagetype')) { |
---|
[478] | 451 | $app->logMsg('Prefs->load() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); |
---|
[477] | 452 | return true; |
---|
| 453 | } |
---|
[146] | 454 | |
---|
[150] | 455 | $this->initDB(); |
---|
| 456 | |
---|
[146] | 457 | // Prefs already loaded for this session. |
---|
[477] | 458 | if (!$force && $this->_isLoaded()) { |
---|
| 459 | return true; |
---|
| 460 | } |
---|
[146] | 461 | |
---|
| 462 | // User_id must not be empty. |
---|
| 463 | if ('' == $this->getParam('user_id')) { |
---|
[151] | 464 | $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[146] | 465 | return false; |
---|
| 466 | } |
---|
[462] | 467 | |
---|
[150] | 468 | // Clear existing cache. |
---|
[462] | 469 | $this->clear('saved'); |
---|
| 470 | |
---|
[151] | 471 | // Retrieve all prefs for this user and namespace. |
---|
[477] | 472 | $qid = $db->query(" |
---|
| 473 | SELECT pref_key, pref_value |
---|
| 474 | FROM " . $db->escapeString($this->getParam('db_table')) . " |
---|
| 475 | WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "' |
---|
| 476 | AND pref_namespace = '" . $db->escapeString($this->_ns) . "' |
---|
| 477 | LIMIT 10000 |
---|
| 478 | "); |
---|
| 479 | while (list($key, $val) = mysql_fetch_row($qid)) { |
---|
[462] | 480 | $_SESSION['_prefs'][$this->_ns]['saved'][$key] = unserialize($val); |
---|
[477] | 481 | } |
---|
[462] | 482 | |
---|
[477] | 483 | $app->logMsg(sprintf('Loaded %s prefs from database.', mysql_num_rows($qid)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[462] | 484 | |
---|
[477] | 485 | // Data loaded only once per session. |
---|
| 486 | $_SESSION['_prefs'][$this->_ns]['loaded'] = true; |
---|
[154] | 487 | $_SESSION['_prefs'][$this->_ns]['load_datetime'] = date('Y-m-d H:i:s'); |
---|
[462] | 488 | |
---|
[477] | 489 | return true; |
---|
[146] | 490 | } |
---|
[462] | 491 | |
---|
[146] | 492 | /* |
---|
[149] | 493 | * Returns true if the prefs had been loaded from the database into the $_SESSION recently. |
---|
| 494 | * This function is simply a check so the database isn't access every page load. |
---|
[462] | 495 | * |
---|
[146] | 496 | * @access private |
---|
| 497 | * @return bool True if prefs are loaded. |
---|
| 498 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 499 | * @version 1.0 |
---|
| 500 | * @since 04 Jun 2006 17:12:44 |
---|
| 501 | */ |
---|
[484] | 502 | protected function _isLoaded() |
---|
[146] | 503 | { |
---|
[477] | 504 | if ('database' != $this->getParam('storagetype')) { |
---|
[478] | 505 | $app->logMsg('Prefs->_isLoaded() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); |
---|
[477] | 506 | return true; |
---|
| 507 | } |
---|
| 508 | |
---|
[154] | 509 | if (isset($_SESSION['_prefs'][$this->_ns]['load_datetime']) |
---|
| 510 | && strtotime($_SESSION['_prefs'][$this->_ns]['load_datetime']) > time() - $this->getParam('load_timeout') |
---|
[462] | 511 | && isset($_SESSION['_prefs'][$this->_ns]['loaded']) |
---|
[154] | 512 | && true === $_SESSION['_prefs'][$this->_ns]['loaded']) { |
---|
[149] | 513 | return true; |
---|
| 514 | } else { |
---|
| 515 | return false; |
---|
| 516 | } |
---|
[146] | 517 | } |
---|
[462] | 518 | |
---|
[146] | 519 | /* |
---|
| 520 | * Saves all prefs stored in the $_SESSION into the database. |
---|
| 521 | * |
---|
| 522 | * @access public |
---|
| 523 | * @return bool True if prefs exist and were saved. |
---|
| 524 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 525 | * @version 1.0 |
---|
| 526 | * @since 04 Jun 2006 17:19:56 |
---|
| 527 | */ |
---|
[468] | 528 | public function save() |
---|
[146] | 529 | { |
---|
| 530 | $app =& App::getInstance(); |
---|
[477] | 531 | $db =& DB::getInstance(); |
---|
[462] | 532 | |
---|
[477] | 533 | // Skip this method if not using the db. |
---|
| 534 | if ('database' != $this->getParam('storagetype')) { |
---|
[478] | 535 | $app->logMsg('Prefs->save() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__); |
---|
[477] | 536 | return true; |
---|
| 537 | } |
---|
[462] | 538 | |
---|
[146] | 539 | // User_id must not be empty. |
---|
| 540 | if ('' == $this->getParam('user_id')) { |
---|
[151] | 541 | $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[146] | 542 | return false; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | $this->initDB(); |
---|
| 546 | |
---|
[462] | 547 | if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && is_array($_SESSION['_prefs'][$this->_ns]['saved']) && !empty($_SESSION['_prefs'][$this->_ns]['saved'])) { |
---|
[146] | 548 | // Delete old prefs from database. |
---|
| 549 | $db->query(" |
---|
| 550 | DELETE FROM " . $db->escapeString($this->getParam('db_table')) . " |
---|
[477] | 551 | WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "' |
---|
| 552 | AND pref_namespace = '" . $db->escapeString($this->_ns) . "' |
---|
[146] | 553 | "); |
---|
[462] | 554 | |
---|
[146] | 555 | // Insert new prefs. |
---|
| 556 | $insert_values = array(); |
---|
[462] | 557 | foreach ($_SESSION['_prefs'][$this->_ns]['saved'] as $key => $val) { |
---|
| 558 | $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", |
---|
| 559 | $db->escapeString($this->getParam('user_id')), |
---|
| 560 | $db->escapeString($this->_ns), |
---|
| 561 | $db->escapeString($key), |
---|
[158] | 562 | $db->escapeString(serialize($val)) |
---|
| 563 | ); |
---|
[146] | 564 | } |
---|
[159] | 565 | // TODO: after MySQL 5.0.23 is released this query could benefit from INSERT DELAYED. |
---|
[146] | 566 | $db->query(" |
---|
[462] | 567 | INSERT INTO " . $db->escapeString($this->getParam('db_table')) . " |
---|
[146] | 568 | (user_id, pref_namespace, pref_key, pref_value) |
---|
| 569 | VALUES " . join(', ', $insert_values) . " |
---|
| 570 | "); |
---|
[462] | 571 | |
---|
[151] | 572 | $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[146] | 573 | return true; |
---|
| 574 | } |
---|
[462] | 575 | |
---|
[146] | 576 | return false; |
---|
| 577 | } |
---|
[477] | 578 | |
---|
| 579 | /* |
---|
| 580 | * |
---|
| 581 | * |
---|
| 582 | * @access public |
---|
| 583 | * @param |
---|
| 584 | * @return |
---|
| 585 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 586 | * @version 1.0 |
---|
| 587 | * @since 02 May 2014 18:17:04 |
---|
| 588 | */ |
---|
[484] | 589 | protected function _getCookieName($key) |
---|
[477] | 590 | { |
---|
| 591 | $app =& App::getInstance(); |
---|
| 592 | |
---|
[478] | 593 | if (mb_strpos($key, sprintf('_prefs-%s', $this->_ns)) === 0) { |
---|
| 594 | $app->logMsg(sprintf('Invalid key name (%s). Leave off "_prefs-%s-" and it should work.', $key, $this->_ns), LOG_NOTICE, __FILE__, __LINE__); |
---|
[477] | 595 | } |
---|
[478] | 596 | // Use standardized class data names: _ + classname + namespace + variablekey |
---|
| 597 | return sprintf('_prefs-%s-%s', $this->_ns, $key); |
---|
[477] | 598 | } |
---|
[1] | 599 | } |
---|
| 600 | |
---|