Changeset 462
- Timestamp:
- Feb 7, 2014 7:17:49 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Prefs.inc.php
r396 r462 4 4 * For details visit the project site: <http://trac.strangecode.com/codebase/> 5 5 * Copyright 2001-2012 Strangecode, LLC 6 * 6 * 7 7 * This file is part of The Strangecode Codebase. 8 8 * … … 11 11 * Free Software Foundation, either version 3 of the License, or (at your option) 12 12 * any later version. 13 * 13 * 14 14 * The Strangecode Codebase is distributed in the hope that it will be useful, but 15 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 17 * details. 18 * 18 * 19 19 * You should have received a copy of the GNU General Public License along with 20 20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. … … 24 24 * Prefs.inc.php 25 25 * 26 * Prefs provides an API for saving arbitrary values in a user's session. 26 * Prefs provides an API for saving arbitrary values in a user's session. 27 27 * Session 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 30 * @version 2.1 31 * 31 * 32 32 * Example of use: 33 33 --------------------------------------------------------------------- … … 59 59 // Configuration parameters for this object. 60 60 var $_params = array( 61 61 62 62 // Enable database storage. If this is false, all prefs will live only as long as the session. 63 63 'persistent' => false, 64 64 65 65 // The current user_id for which to load/save persistent preferences. 66 66 'user_id' => null, 67 67 68 68 // How long before we force a reload of the persistent prefs data? 3600 = once every hour. 69 69 'load_timeout' => 3600, 70 70 71 71 // Name of database table to store persistent prefs. 72 72 'db_table' => 'pref_tbl', … … 85 85 86 86 $this->_ns = $namespace; 87 87 88 88 // Initialized the prefs array. 89 89 if (!isset($_SESSION['_prefs'][$this->_ns])) { … … 160 160 { 161 161 $app =& App::getInstance(); 162 162 163 163 if (isset($this->_params[$param])) { 164 164 return $this->_params[$param]; … … 170 170 171 171 /** 172 * Sets the default values for preferences. If a preference is not explicitly 172 * Sets the default values for preferences. If a preference is not explicitly 173 173 * set, the value set here will be used. Can be called multiple times to merge additional 174 174 * defaults together. 175 175 * 176 * @param array $defaults Array of key-value pairs 176 * @param array $defaults Array of key-value pairs 177 177 */ 178 178 function setDefaults($defaults) … … 200 200 return false; 201 201 } 202 203 // Set a persistentpreference if...202 203 // Set a saved preference if... 204 204 // - there isn't a default. 205 // - the new value is different than the default 206 // - there is a previously existing persistent key. 207 if (!isset($_SESSION['_prefs'][$this->_ns]['defaults'][$key]) || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val || isset($_SESSION['_prefs'][$this->_ns]['persistent'][$key])) { 208 $_SESSION['_prefs'][$this->_ns]['persistent'][$key] = $val; 205 // - or the new value is different than the default 206 // - or there is a previously existing saved key. 207 if (!isset($_SESSION['_prefs'][$this->_ns]['defaults'][$key]) 208 || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val 209 || isset($_SESSION['_prefs'][$this->_ns]['saved'][$key])) { 210 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; 209 211 $app->logMsg(sprintf('Setting preference %s => %s', $key, truncate(getDump($val, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); 210 212 } else { … … 214 216 215 217 /** 216 * Returns the value of the requested preference. Persistentvalues take precedence, but if none is set218 * Returns the value of the requested preference. Saved values take precedence, but if none is set 217 219 * a default value is returned, or if not that, null. 218 220 * … … 224 226 { 225 227 $app =& App::getInstance(); 226 if (isset($_SESSION['_prefs'][$this->_ns][' persistent']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['persistent'])) {227 $app->logMsg(sprintf('Found %s in persistent', $key), LOG_DEBUG, __FILE__, __LINE__);228 return $_SESSION['_prefs'][$this->_ns][' persistent'][$key];228 if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])) { 229 $app->logMsg(sprintf('Found %s in saved', $key), LOG_DEBUG, __FILE__, __LINE__); 230 return $_SESSION['_prefs'][$this->_ns]['saved'][$key]; 229 231 } else if (isset($_SESSION['_prefs'][$this->_ns]['defaults']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])) { 230 232 $app->logMsg(sprintf('Found %s in defaults', $key), LOG_DEBUG, __FILE__, __LINE__); … … 244 246 function exists($key) 245 247 { 246 return array_key_exists($key, $_SESSION['_prefs'][$this->_ns][' persistent']);247 } 248 249 /** 250 * Clear a set preference value. This will also remove the value from the database. 248 return array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved']); 249 } 250 251 /** 252 * Clear a set preference value. This will also remove the value from the database. 251 253 * 252 254 * @param string $key The name of the preference to delete. … … 254 256 function delete($key) 255 257 { 256 unset($_SESSION['_prefs'][$this->_ns][' persistent'][$key]);257 } 258 259 /** 260 * Resets the $_SESSION cache. This should be executed with the same consideration 258 unset($_SESSION['_prefs'][$this->_ns]['saved'][$key]); 259 } 260 261 /** 262 * Resets the $_SESSION cache. This should be executed with the same consideration 261 263 * as $auth->clear(), such as when logging out. 262 264 */ … … 269 271 'load_datetime' => '1970-01-01', 270 272 'defaults' => array(), 271 ' persistent' => array(),273 'saved' => array(), 272 274 ); 273 275 break; … … 277 279 break; 278 280 279 case ' persistent' :280 $_SESSION['_prefs'][$this->_ns][' persistent'] = array();281 case 'saved' : 282 $_SESSION['_prefs'][$this->_ns]['saved'] = array(); 281 283 break; 282 284 } 283 285 } 284 286 285 287 /* 286 288 * Retrieves all prefs from the database and stores them in the $_SESSION. … … 297 299 $app =& App::getInstance(); 298 300 $db =& DB::getInstance(); 299 301 300 302 // Skip this method if not using the db. 301 303 if (true !== $this->getParam('persistent')) { … … 315 317 return false; 316 318 } 317 319 318 320 // Clear existing cache. 319 $this->clear(' persistent');320 321 $this->clear('saved'); 322 321 323 // Retrieve all prefs for this user and namespace. 322 324 $qid = $db->query(" … … 328 330 "); 329 331 while (list($key, $val) = mysql_fetch_row($qid)) { 330 $_SESSION['_prefs'][$this->_ns][' persistent'][$key] = unserialize($val);332 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = unserialize($val); 331 333 } 332 334 333 335 $app->logMsg(sprintf('Loaded %s prefs from database.', mysql_num_rows($qid)), LOG_DEBUG, __FILE__, __LINE__); 334 336 335 337 // Data loaded only once per session. 336 338 $_SESSION['_prefs'][$this->_ns]['loaded'] = true; 337 339 $_SESSION['_prefs'][$this->_ns]['load_datetime'] = date('Y-m-d H:i:s'); 338 340 339 341 return true; 340 342 } 341 343 342 344 /* 343 345 * Returns true if the prefs had been loaded from the database into the $_SESSION recently. 344 346 * This function is simply a check so the database isn't access every page load. 345 * 347 * 346 348 * @access private 347 349 * @return bool True if prefs are loaded. … … 354 356 if (isset($_SESSION['_prefs'][$this->_ns]['load_datetime']) 355 357 && strtotime($_SESSION['_prefs'][$this->_ns]['load_datetime']) > time() - $this->getParam('load_timeout') 356 && isset($_SESSION['_prefs'][$this->_ns]['loaded']) 358 && isset($_SESSION['_prefs'][$this->_ns]['loaded']) 357 359 && true === $_SESSION['_prefs'][$this->_ns]['loaded']) { 358 360 return true; … … 361 363 } 362 364 } 363 365 364 366 /* 365 367 * Saves all prefs stored in the $_SESSION into the database. … … 375 377 $app =& App::getInstance(); 376 378 $db =& DB::getInstance(); 377 379 378 380 // Skip this method if not using the db. 379 381 if (true !== $this->getParam('persistent')) { 380 382 return true; 381 383 } 382 384 383 385 // User_id must not be empty. 384 386 if ('' == $this->getParam('user_id')) { … … 389 391 $this->initDB(); 390 392 391 if (isset($_SESSION['_prefs'][$this->_ns][' persistent']) && is_array($_SESSION['_prefs'][$this->_ns]['persistent']) && !empty($_SESSION['_prefs'][$this->_ns]['persistent'])) {393 if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && is_array($_SESSION['_prefs'][$this->_ns]['saved']) && !empty($_SESSION['_prefs'][$this->_ns]['saved'])) { 392 394 // Delete old prefs from database. 393 395 $db->query(" … … 396 398 AND pref_namespace = '" . $db->escapeString($this->_ns) . "' 397 399 "); 398 400 399 401 // Insert new prefs. 400 402 $insert_values = array(); 401 foreach ($_SESSION['_prefs'][$this->_ns][' persistent'] as $key => $val) {402 $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", 403 $db->escapeString($this->getParam('user_id')), 404 $db->escapeString($this->_ns), 405 $db->escapeString($key), 403 foreach ($_SESSION['_prefs'][$this->_ns]['saved'] as $key => $val) { 404 $insert_values[] = sprintf("('%s', '%s', '%s', '%s')", 405 $db->escapeString($this->getParam('user_id')), 406 $db->escapeString($this->_ns), 407 $db->escapeString($key), 406 408 $db->escapeString(serialize($val)) 407 409 ); … … 409 411 // TODO: after MySQL 5.0.23 is released this query could benefit from INSERT DELAYED. 410 412 $db->query(" 411 INSERT INTO " . $db->escapeString($this->getParam('db_table')) . " 413 INSERT INTO " . $db->escapeString($this->getParam('db_table')) . " 412 414 (user_id, pref_namespace, pref_key, pref_value) 413 415 VALUES " . join(', ', $insert_values) . " 414 416 "); 415 417 416 418 $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__); 417 419 return true; 418 420 } 419 421 420 422 return false; 421 423 } -
trunk/services/login.php
r396 r462 4 4 * For details visit the project site: <http://trac.strangecode.com/codebase/> 5 5 * Copyright 2001-2012 Strangecode, LLC 6 * 6 * 7 7 * This file is part of The Strangecode Codebase. 8 8 * … … 11 11 * Free Software Foundation, either version 3 of the License, or (at your option) 12 12 * any later version. 13 * 13 * 14 14 * The Strangecode Codebase is distributed in the hope that it will be useful, but 15 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 17 * details. 18 * 18 * 19 19 * You should have received a copy of the GNU General Public License along with 20 20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. … … 32 32 $app->sslOn(); 33 33 34 $frm['username'] = getFormdata('username'); 34 require_once 'codebase/lib/Prefs.inc.php'; 35 $login_prefs = new Prefs('login'); 36 $login_prefs->setDefaults(array('username' => '')); 37 38 if ('' != $login_prefs->get('username')) { 39 $frm['remember_me'] = '1'; 40 } 41 42 $frm['username'] = getFormdata('username', $login_prefs->get('username')); 35 43 $frm['password'] = getFormdata('password'); 36 44 … … 39 47 40 48 if ($auth->login($frm['username'], $frm['password'])) { 49 if (getFormData('remember_me')) { 50 $login_prefs->set('username', getFormData('username')); 51 } else { 52 $login_prefs->set('username', ''); 53 } 41 54 $app->raiseMsg(_("You are now logged in."), MSG_SUCCESS, __FILE__, __LINE__); 42 55 $app->dieBoomerangURL('login'); -
trunk/services/templates/login_form.ihtml
r421 r462 17 17 </td> 18 18 </tr> 19 <tr class="sc-form-row"> 20 <td class="sc-right"> </td> 21 <td> 22 <label class="sc-label-secondary"><input type="checkbox" name="remember_me" id="remember_me" value="1"<?php frmChecked($frm['remember_me']) ?> /> <?php echo _("Remember my username"); ?></label> 23 </td> 24 </tr> 19 25 <tr> 20 26 <td> </td> … … 23 29 </table> 24 30 </form> 31 <script type="text/javascript" charset="utf-8"> 32 /* <![CDATA[ */ 33 $(document).ready(function() { 34 if ($('input[name="username"]').val() != '') { 35 $('input[name="password"]').focus(); 36 } else { 37 $('input[name="username"]').focus(); 38 } 39 }); 40 /* ]]> */ 41 </script>
Note: See TracChangeset
for help on using the changeset viewer.