[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/session/src/ -> lmbSessionDbStorage.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/session/src/lmbSessionStorage.interface.php');
  10  lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
  11  lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
  12  
  13  /**

  14   * lmbSessionDbStorage store session data in database.

  15   * sys_session db table used to store session data.

  16   * The structure of sys_session db table can be found in limb/session/init/ folder.

  17   * @todo Check client ip while reading session.

  18   * @todo Allow to set any db table name to store session data in.

  19   * @see lmbSessionStartupFilter

  20   * @version $Id: lmbSessionDbStorage.class.php 5945 2007-06-06 08:31:43Z pachanga $

  21   * @package session
  22   */
  23  class lmbSessionDbStorage implements lmbSessionStorage
  24  {
  25    /**

  26     * @var lmbSimpleDb facade to work with database

  27     */
  28    protected $db;
  29    /**

  30     * @var integer maximum session life time

  31     */
  32    protected $max_life_time = null;
  33  
  34    /**

  35     *  Constructor.

  36     *  @param lmbDbConnection database connection object

  37     *  @param integer maximum session life time

  38     */
  39    function __construct($db_connection, $max_life_time = null)
  40    {
  41      $this->max_life_time = $max_life_time;
  42  
  43      $this->db = new lmbSimpleDb($db_connection);
  44    }
  45  
  46    /**

  47     * @see lmbSessionStorage :: install()

  48     * @return void

  49     */
  50    function install()
  51    {
  52      session_set_save_handler(
  53       array($this, 'storageOpen'),
  54       array($this, 'storageClose'),
  55       array($this, 'storageRead'),
  56       array($this, 'storageWrite'),
  57       array($this, 'storageDestroy'),
  58       array($this, 'storageGc')
  59      );
  60    }
  61  
  62    /**

  63     * Opens session storage

  64     * Does nothing and returns true

  65     * @return boolean

  66     */
  67    function storageOpen()
  68    {
  69      return true;
  70    }
  71  
  72    /**

  73     * Closes session storage

  74     * Does nothing and returns true

  75     * @return boolean

  76     */
  77    function storageClose()
  78    {
  79      return true;
  80    }
  81  
  82    /**

  83     * Read a single row from <b>sys_session</b> db table and returns <b>session_data</b> column

  84     * @param string session ID

  85     * @return mixed

  86     */
  87    function storageRead($session_id)
  88    {
  89      $rs = $this->db->select('sys_session', new lmbSQLFieldCriteria('session_id', $session_id));
  90      $rs->rewind();
  91      if($rs->valid())
  92        return $rs->current()->get('session_data');
  93      else
  94        return false;
  95    }
  96  
  97    /**

  98     * Creates new or updates existing row in <b>sys_session</b> db table

  99     * @param string session ID

 100     * @param mixed session data

 101     * @return void

 102     */
 103    function storageWrite($session_id, $value)
 104    {
 105      $crit = new lmbSQLFieldCriteria('session_id', $session_id);
 106      $rs = $this->db->select('sys_session', $crit);
 107  
 108      $data = array('last_activity_time' => time(),
 109                    'session_data' => $value);
 110  
 111      if($rs->count() > 0)
 112        $this->db->update('sys_session', $data, $crit);
 113      else
 114      {
 115        $data['session_id'] = "{$session_id}";
 116        $this->db->insert('sys_session', $data, null);
 117      }
 118    }
 119  
 120    /**

 121     * Removed a row from <b>sys_session</b> db table

 122     * @param string session ID

 123     * @return void

 124     */
 125    function storageDestroy($session_id)
 126    {
 127      $this->db->delete('sys_session',
 128                        new lmbSQLFieldCriteria('session_id', $session_id));
 129    }
 130  
 131    /**

 132     * Checks if storage is still valid. If session if not valid - removes it's row from <b>sys_session</b> db table

 133     * Prefers class attribute {@link $max_life_time} if it's not NULL.

 134     * @param integer system session max life time

 135     * @return void

 136     */
 137    function storageGc($max_life_time)
 138    {
 139      if($this->max_life_time)
 140        $max_life_time = $this->max_life_time;
 141  
 142      $this->db->delete('sys_session',
 143                        new lmbSQLFieldCriteria('last_activity_time', time() - $max_life_time, lmbSQLFieldCriteria::LESS));
 144    }
 145  }
 146  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7