Changeset 462


Ignore:
Timestamp:
Feb 7, 2014 7:17:49 PM (10 years ago)
Author:
anonymous
Message:

Added 'Remember me' checkbox to login form. Renamed 'persistent' key to 'saved' in Prefs.inc.php.

Location:
trunk
Files:
3 edited

Legend:

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

    r396 r462  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    2424 * Prefs.inc.php
    2525 *
    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.
    2727 * Session prefs can be stored into a database with the optional save() and load() methods.
    2828 *
    2929 * @author  Quinn Comendant <quinn@strangecode.com>
    3030 * @version 2.1
    31  * 
     31 *
    3232 * Example of use:
    3333---------------------------------------------------------------------
     
    5959    // Configuration parameters for this object.
    6060    var $_params = array(
    61        
     61
    6262        // Enable database storage. If this is false, all prefs will live only as long as the session.
    6363        'persistent' => false,
    64        
     64
    6565        // The current user_id for which to load/save persistent preferences.
    6666        'user_id' => null,
    67        
     67
    6868        // How long before we force a reload of the persistent prefs data? 3600 = once every hour.
    6969        'load_timeout' => 3600,
    70        
     70
    7171        // Name of database table to store persistent prefs.
    7272        'db_table' => 'pref_tbl',
     
    8585
    8686        $this->_ns = $namespace;
    87        
     87
    8888        // Initialized the prefs array.
    8989        if (!isset($_SESSION['_prefs'][$this->_ns])) {
     
    160160    {
    161161        $app =& App::getInstance();
    162    
     162
    163163        if (isset($this->_params[$param])) {
    164164            return $this->_params[$param];
     
    170170
    171171    /**
    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
    173173     * set, the value set here will be used. Can be called multiple times to merge additional
    174174     * defaults together.
    175175     *
    176      * @param  array $defaults  Array of key-value pairs 
     176     * @param  array $defaults  Array of key-value pairs
    177177     */
    178178    function setDefaults($defaults)
     
    200200            return false;
    201201        }
    202        
    203         // Set a persistent preference if...
     202
     203        // Set a saved preference if...
    204204        // - 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;
    209211            $app->logMsg(sprintf('Setting preference %s => %s', $key, truncate(getDump($val, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__);
    210212        } else {
     
    214216
    215217    /**
    216      * Returns the value of the requested preference. Persistent values take precedence, but if none is set
     218     * Returns the value of the requested preference. Saved values take precedence, but if none is set
    217219     * a default value is returned, or if not that, null.
    218220     *
     
    224226    {
    225227        $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];
    229231        } else if (isset($_SESSION['_prefs'][$this->_ns]['defaults']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])) {
    230232            $app->logMsg(sprintf('Found %s in defaults', $key), LOG_DEBUG, __FILE__, __LINE__);
     
    244246    function exists($key)
    245247    {
    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.
    251253     *
    252254     * @param string $key       The name of the preference to delete.
     
    254256    function delete($key)
    255257    {
    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
    261263     * as $auth->clear(), such as when logging out.
    262264     */
     
    269271                'load_datetime' => '1970-01-01',
    270272                'defaults' => array(),
    271                 'persistent' => array(),
     273                'saved' => array(),
    272274            );
    273275            break;
     
    277279            break;
    278280
    279         case 'persistent' :
    280             $_SESSION['_prefs'][$this->_ns]['persistent'] = array();
     281        case 'saved' :
     282            $_SESSION['_prefs'][$this->_ns]['saved'] = array();
    281283            break;
    282284        }
    283285    }
    284    
     286
    285287    /*
    286288    * Retrieves all prefs from the database and stores them in the $_SESSION.
     
    297299        $app =& App::getInstance();
    298300        $db =& DB::getInstance();
    299        
     301
    300302        // Skip this method if not using the db.
    301303        if (true !== $this->getParam('persistent')) {
     
    315317            return false;
    316318        }
    317        
     319
    318320        // Clear existing cache.
    319         $this->clear('persistent');
    320        
     321        $this->clear('saved');
     322
    321323        // Retrieve all prefs for this user and namespace.
    322324        $qid = $db->query("
     
    328330        ");
    329331        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);
    331333        }
    332        
     334
    333335        $app->logMsg(sprintf('Loaded %s prefs from database.', mysql_num_rows($qid)), LOG_DEBUG, __FILE__, __LINE__);
    334        
     336
    335337        // Data loaded only once per session.
    336338        $_SESSION['_prefs'][$this->_ns]['loaded'] = true;
    337339        $_SESSION['_prefs'][$this->_ns]['load_datetime'] = date('Y-m-d H:i:s');
    338        
     340
    339341        return true;
    340342    }
    341    
     343
    342344    /*
    343345    * Returns true if the prefs had been loaded from the database into the $_SESSION recently.
    344346    * This function is simply a check so the database isn't access every page load.
    345     * 
     347    *
    346348    * @access   private
    347349    * @return   bool    True if prefs are loaded.
     
    354356        if (isset($_SESSION['_prefs'][$this->_ns]['load_datetime'])
    355357        && 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'])
    357359        && true === $_SESSION['_prefs'][$this->_ns]['loaded']) {
    358360            return true;
     
    361363        }
    362364    }
    363    
     365
    364366    /*
    365367    * Saves all prefs stored in the $_SESSION into the database.
     
    375377        $app =& App::getInstance();
    376378        $db =& DB::getInstance();
    377        
     379
    378380        // Skip this method if not using the db.
    379381        if (true !== $this->getParam('persistent')) {
    380382            return true;
    381383        }
    382        
     384
    383385        // User_id must not be empty.
    384386        if ('' == $this->getParam('user_id')) {
     
    389391        $this->initDB();
    390392
    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'])) {
    392394            // Delete old prefs from database.
    393395            $db->query("
     
    396398                AND pref_namespace = '" . $db->escapeString($this->_ns) . "'
    397399            ");
    398            
     400
    399401            // Insert new prefs.
    400402            $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),
    406408                    $db->escapeString(serialize($val))
    407409                );
     
    409411            // TODO: after MySQL 5.0.23 is released this query could benefit from INSERT DELAYED.
    410412            $db->query("
    411                 INSERT INTO " . $db->escapeString($this->getParam('db_table')) . " 
     413                INSERT INTO " . $db->escapeString($this->getParam('db_table')) . "
    412414                (user_id, pref_namespace, pref_key, pref_value)
    413415                VALUES " . join(', ', $insert_values) . "
    414416            ");
    415            
     417
    416418            $app->logMsg(sprintf('Saved %s prefs to database.', sizeof($insert_values)), LOG_DEBUG, __FILE__, __LINE__);
    417419            return true;
    418420        }
    419        
     421
    420422        return false;
    421423    }
  • trunk/services/login.php

    r396 r462  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    3232$app->sslOn();
    3333
    34 $frm['username'] = getFormdata('username');
     34require_once 'codebase/lib/Prefs.inc.php';
     35$login_prefs = new Prefs('login');
     36$login_prefs->setDefaults(array('username' => ''));
     37
     38if ('' != $login_prefs->get('username')) {
     39    $frm['remember_me'] = '1';
     40}
     41
     42$frm['username'] = getFormdata('username', $login_prefs->get('username'));
    3543$frm['password'] = getFormdata('password');
    3644
     
    3947
    4048    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        }
    4154        $app->raiseMsg(_("You are now logged in."), MSG_SUCCESS, __FILE__, __LINE__);
    4255        $app->dieBoomerangURL('login');
  • trunk/services/templates/login_form.ihtml

    r421 r462  
    1717        </td>
    1818    </tr>
     19    <tr class="sc-form-row">
     20        <td class="sc-right">&nbsp;</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>
    1925    <tr>
    2026        <td>&nbsp;</td>
     
    2329</table>
    2430</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.