[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/storage/ -> sfMySQLSessionStorage.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004-2006 Sean Kerr.
   7   * 
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * Provides support for session storage using a MySQL brand database.
  14   *
  15   * <b>Required parameters:</b>
  16   *
  17   * # <b>db_table</b> - [none] - The database table in which session data will be
  18   *                              stored.
  19   *
  20   * <b>Optional parameters:</b>
  21   *
  22   * # <b>db_id_col</b>    - [sess_id]   - The database column in which the
  23   *                                       session id will be stored.
  24   * # <b>db_data_col</b>  - [sess_data] - The database column in which the
  25   *                                       session data will be stored.
  26   * # <b>db_time_col</b>  - [sess_time] - The database column in which the
  27   *                                       session timestamp will be stored.
  28   * # <b>session_name</b> - [symfony]   - The name of the session.
  29   *
  30   * @package    symfony
  31   * @subpackage storage
  32   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  33   * @author     Sean Kerr <skerr@mojavi.org>
  34   * @version    SVN: $Id: sfMySQLSessionStorage.class.php 3329 2007-01-23 08:29:34Z fabien $
  35   */
  36  class sfMySQLSessionStorage extends sfSessionStorage
  37  {
  38    protected
  39      $resource = null;
  40  
  41    /**
  42     * Initializes this Storage instance.
  43     *
  44     * @param sfContext A sfContext instance
  45     * @param array   An associative array of initialization parameters
  46     *
  47     * @return boolean true, if initialization completes successfully, otherwise false
  48     *
  49     * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage
  50     */
  51    public function initialize($context, $parameters = null)
  52    {
  53      // disable auto_start
  54      $parameters['auto_start'] = false;
  55  
  56      // initialize the parent
  57      parent::initialize($context, $parameters);
  58  
  59      if (!$this->getParameterHolder()->has('db_table'))
  60      {
  61          // missing required 'db_table' parameter
  62          $error = 'Factory configuration file is missing required "db_table" parameter for the Storage category';
  63  
  64          throw new sfInitializationException($error);
  65      }
  66  
  67      // use this object as the session handler
  68      session_set_save_handler(array($this, 'sessionOpen'),
  69                               array($this, 'sessionClose'),
  70                               array($this, 'sessionRead'),
  71                               array($this, 'sessionWrite'),
  72                               array($this, 'sessionDestroy'),
  73                               array($this, 'sessionGC'));
  74  
  75      // start our session
  76      session_start();
  77    }
  78  
  79    /**
  80     * Closes a session.
  81     *
  82     * @return boolean true, if the session was closed, otherwise false
  83     */
  84    public function sessionClose()
  85    {
  86      // do nothing
  87      return true;
  88    }
  89  
  90    /**
  91     * Destroys a session.
  92     *
  93     * @param string A session ID
  94     *
  95     * @return boolean true, if the session was destroyed, otherwise an exception is thrown
  96     *
  97     * @throws <b>sfDatabaseException</b> If the session cannot be destroyed.
  98     */
  99    public function sessionDestroy($id)
 100    {
 101      // get table/column
 102      $db_table  = $this->getParameterHolder()->get('db_table');
 103      $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id');
 104  
 105      // cleanup the session id, just in case
 106      $id = mysql_escape_string($id);
 107  
 108      // delete the record associated with this id
 109      $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
 110  
 111      if (@mysql_query($sql, $this->resource))
 112      {
 113        return true;
 114      }
 115  
 116      // failed to destroy session
 117      $error = 'MySQLSessionStorage cannot destroy session id "%s"';
 118      $error = sprintf($error, $id);
 119  
 120      throw new sfDatabaseException($error);
 121    }
 122  
 123    /**
 124     * Cleans up old sessions.
 125     *
 126     * @param int The lifetime of a session
 127     *
 128     * @return boolean true, if old sessions have been cleaned, otherwise an exception is thrown
 129     *
 130     * @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned
 131     */
 132    public function sessionGC($lifetime)
 133    {
 134      // determine deletable session time
 135      $time = time() - $lifetime;
 136  
 137      // get table/column
 138      $db_table    = $this->getParameterHolder()->get('db_table');
 139      $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
 140  
 141      // delete the record associated with this id
 142      $sql = 'DELETE FROM '.$db_table.' '.
 143             'WHERE '.$db_time_col.' < '.$time;
 144  
 145      if (@mysql_query($sql, $this->resource))
 146      {
 147        return true;
 148      }
 149  
 150      // failed to cleanup old sessions
 151      $error = 'MySQLSessionStorage cannot delete old sessions';
 152  
 153      throw new sfDatabaseException($error);
 154    }
 155  
 156    /**
 157     * Opens a session.
 158     *
 159     * @param string
 160     * @param string
 161     *
 162     * @return boolean true, if the session was opened, otherwise an exception is thrown
 163     *
 164     * @throws <b>sfDatabaseException</b> If a connection with the database does not exist or cannot be created
 165     */
 166    public function sessionOpen($path, $name)
 167    {
 168      // what database are we using?
 169      $database = $this->getParameterHolder()->get('database', 'default');
 170  
 171      // get the database resource
 172      $this->resource = $this->getContext()
 173                             ->getDatabaseManager()
 174                             ->getDatabase($database)
 175                             ->getResource();
 176  
 177      return true;
 178    }
 179  
 180    /**
 181     * Reads a session.
 182     *
 183     * @param string A session ID
 184     *
 185     * @return boolean true, if the session was read, otherwise an exception is thrown
 186     *
 187     * @throws <b>sfDatabaseException</b> If the session cannot be read
 188     */
 189    public function sessionRead($id)
 190    {
 191      // get table/column
 192      $db_table    = $this->getParameterHolder()->get('db_table');
 193      $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data');
 194      $db_id_col   = $this->getParameterHolder()->get('db_id_col', 'sess_id');
 195      $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
 196  
 197      // cleanup the session id, just in case
 198      $id = mysql_escape_string($id);
 199  
 200      // delete the record associated with this id
 201      $sql = 'SELECT '.$db_data_col.' ' .
 202             'FROM '.$db_table.' ' .
 203             'WHERE '.$db_id_col.' = \''.$id.'\'';
 204  
 205      $result = @mysql_query($sql, $this->resource);
 206  
 207      if ($result != false && @mysql_num_rows($result) == 1)
 208      {
 209        // found the session
 210        $data = mysql_fetch_row($result);
 211  
 212        return $data[0];
 213      }
 214      else
 215      {
 216        // session does not exist, create it
 217        $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', ' .
 218               $db_data_col.', '.$db_time_col.') VALUES (' .
 219               '\''.$id.'\', \'\', '.time().')';
 220  
 221        if (@mysql_query($sql, $this->resource))
 222        {
 223          return '';
 224        }
 225  
 226        // can't create record
 227        $error = 'MySQLSessionStorage cannot create new record for id "%s"';
 228        $error = sprintf($error, $id);
 229  
 230        throw new sfDatabaseException($error);
 231      }
 232    }
 233  
 234    /**
 235     * Writes session data.
 236     *
 237     * @param string A session ID
 238     * @param string A serialized chunk of session data
 239     *
 240     * @return boolean true, if the session was written, otherwise an exception is thrown
 241     *
 242     * @throws <b>sfDatabaseException</b> If the session data cannot be written
 243     */
 244    public function sessionWrite($id, &$data)
 245    {
 246      // get table/column
 247      $db_table    = $this->getParameterHolder()->get('db_table');
 248      $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data');
 249      $db_id_col   = $this->getParameterHolder()->get('db_id_col', 'sess_id');
 250      $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');
 251  
 252      // cleanup the session id and data, just in case
 253      $id   = mysql_escape_string($id);
 254      $data = mysql_escape_string($data);
 255  
 256      // delete the record associated with this id
 257      $sql = 'UPDATE '.$db_table.' ' .
 258             'SET '.$db_data_col.' = \''.$data.'\', ' .
 259             $db_time_col.' = '.time().' ' .
 260             'WHERE '.$db_id_col.' = \''.$id.'\'';
 261  
 262      if (@mysql_query($sql, $this->resource))
 263      {
 264        return true;
 265      }
 266  
 267      // failed to write session data
 268      $error = 'MySQLSessionStorage cannot write session data for id "%s"';
 269      $error = sprintf($error, $id);
 270  
 271      throw new sfDatabaseException($error);
 272    }
 273  
 274    /**
 275     * Executes the shutdown procedure.
 276     *
 277     */
 278    public function shutdown()
 279    {
 280    }
 281  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7