[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

/lib/Horde/ -> SessionObjects.php (source)

   1  <?php
   2  /**
   3   * The Horde_SessionObjects:: class provides a way for storing data
   4   * (usually, but not necessarily, objects) in the current user's
   5   * session.
   6   *
   7   * $Horde: framework/SessionObjects/SessionObjects.php,v 1.6.12.8 2006/01/01 21:28:35 jan Exp $
   8   *
   9   * Copyright 2003-2006 Chuck Hagenbuch <chuck@horde.org>
  10   *
  11   * See the enclosed file COPYING for license information (LGPL). If youq
  12   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  13   *
  14   * @author  Chuck Hagenbuch <chuck@horde.org>
  15   * @since   Horde 1.3
  16   * @package Horde_SessionObjects
  17   */
  18  class Horde_SessionObjects {
  19  
  20      /**
  21       * The list of oids to prune at the end of a request.
  22       *
  23       * @var array
  24       */
  25      var $_pruneList = null;
  26  
  27      /**
  28       * The name of the store.
  29       *
  30       * @var string
  31       */
  32      var $_name = 'horde_session_objects';
  33  
  34      /**
  35       * Allow store() to overwrite current objects?
  36       *
  37       * @var boolean
  38       */
  39      var $_overwrite = false;
  40  
  41      /**
  42       * The maximum number of objects that the store should hold.
  43       *
  44       * @var integer
  45       */
  46      var $_size = 20;
  47  
  48      /**
  49       * Returns a reference to the global Horde_SessionObjects object,
  50       * only creating it if it doesn't already exist.
  51       *
  52       * This method must be invoked as:
  53       *   $objectstore = &Horde_SessionObjects::singleton();
  54       *
  55       * @return Horde_SessionObjects  The Horde_SessionObjects instance.
  56       */
  57      function &singleton()
  58      {
  59          static $object;
  60  
  61          if (!isset($object)) {
  62              $object = new Horde_SessionObjects();
  63          }
  64  
  65          return $object;
  66      }
  67  
  68      /**
  69       * Constructor.
  70       *
  71       * @param array $params  The parameter array.
  72       * <pre>
  73       * Optional Parameters:
  74       * 'name'  --  The name of the session variable to store the objects in.
  75       * 'size'  --  The maximum size of the (non-prunable) object store.
  76       * </pre>
  77       */
  78      function Horde_SessionObjects($params = array())
  79      {
  80          if (isset($params['name'])) {
  81              $this->_name = $params['name'];
  82          }
  83  
  84          if (isset($params['size']) && is_int($params['size'])) {
  85              $this->_size = $params['size'];
  86          }
  87      }
  88  
  89      /**
  90       * Wrapper around store that will return the oid instead.
  91       *
  92       * @see store
  93       *
  94       * @param mixed $data     The data to store in the session store.
  95       * @param boolean $prune  If false, this object will not be pruned from the
  96       *                        store if the maximum store size is exceeded.
  97       *
  98       * @return string  The MD5 string representing the object's ID.
  99       */
 100      function storeOid($data, $prune = true)
 101      {
 102          $oid = $this->oid($data);
 103          $this->store($oid, $data, $prune);
 104          return $oid;
 105      }
 106  
 107      /**
 108       * Attempts to store an object in the session store.
 109       *
 110       * @param string $oid     Object ID used as the storage key.
 111       * @param mixed $data     The data to store in the session store.
 112       * @param boolean $prune  If false, this object will not be pruned from the
 113       *                        store if the maximum store size is exceeded.
 114       *
 115       * @return boolean  True on success, false on failure.
 116       */
 117      function store($oid, $data, $prune = true)
 118      {
 119          /* Set up object now. */
 120          $dataObject = array();
 121          $dataObject['data'] = serialize($data);
 122          $dataObject['prune'] = $prune;
 123  
 124          if (!isset($_SESSION[$this->_name])) {
 125              $_SESSION[$this->_name] = array();
 126              $_SESSION[$this->_name]['__prune'] = 0;
 127          }
 128  
 129          if ($this->_overwrite || !isset($_SESSION[$this->_name][$oid])) {
 130              $_SESSION[$this->_name][$oid] = $dataObject;
 131              if ($prune) {
 132                  $_SESSION[$this->_name]['__prune']++;
 133              }
 134          }
 135  
 136          /* Check for prunable Oids. */
 137          $this->_pruneOids();
 138  
 139          return true;
 140      }
 141  
 142      /**
 143       * Overwrites a current element in the object store.
 144       *
 145       * @param string $oid     Object ID used as the storage key.
 146       * @param mixed $data     The data to store in the session store.
 147       * @param boolean $prune  If false, this object will not be pruned from the
 148       *                        store if the maximum store size is exceeded.
 149       *
 150       * @return boolean  True on success, false on failure.
 151       */
 152      function overwrite($oid, $data, $prune = true)
 153      {
 154          $this->_overwrite = true;
 155          $success = $this->store($oid, $data, $prune);
 156          $this->_overwrite = false;
 157          return $success;
 158      }
 159  
 160      /**
 161       * Attempts to retrive an object from the store.
 162       *
 163       * @param string $oid   Object ID to query.
 164       * @param enum $type    NOT USED
 165       * @param integer $val  NOT USED
 166       *
 167       * @return mixed  The requested object, or false on failure.
 168       */
 169      function &query($oid, $type = null, $val = null)
 170      {
 171          if (!isset($_SESSION[$this->_name]) ||
 172              (is_null($oid) || !isset($_SESSION[$this->_name][$oid]))) {
 173              $object = false;
 174          } else {
 175              $object = unserialize($_SESSION[$this->_name][$oid]['data']);
 176          }
 177          return $object;
 178      }
 179  
 180      /**
 181       * Sets the prune flag on a store object.
 182       *
 183       * @param string $oid     The object ID.
 184       * @param boolean $prune  True to allow pruning, false for no pruning.
 185       */
 186      function setPruneFlag($oid, $prune)
 187      {
 188          if (isset($_SESSION[$this->_name][$oid]) &&
 189              ($_SESSION[$this->_name][$oid]['prune'] != $prune)) {
 190              $_SESSION[$this->_name][$oid]['prune'] = $prune;
 191              if ($prune) {
 192                  $_SESSION[$this->_name]['__prune']++;
 193              } else {
 194                  $_SESSION[$this->_name]['__prune']--;
 195              }
 196          }
 197      }
 198  
 199      /**
 200       * Generates an OID for an object.
 201       *
 202       * @param mixed $data  The data to store in the store.
 203       *
 204       * @return string $oid  An object ID to use as the storage key.
 205       */
 206      function oid($data)
 207      {
 208          return md5(serialize($data));
 209      }
 210  
 211      /**
 212       * Generate the list of prunable oids.
 213       *
 214       * @access private
 215       */
 216      function _pruneOids()
 217      {
 218          if (is_null($this->_pruneList) &&
 219              isset($_SESSION[$this->_name]['__prune']) &&
 220              ($_SESSION[$this->_name]['__prune'] > $this->_size)) {
 221              $this->_pruneList = array();
 222              foreach ($_SESSION[$this->_name] as $key => $val) {
 223                  if ($val['prune']) {
 224                      $this->_pruneList[] = $key;
 225                  }
 226              }
 227              register_shutdown_function(array(&$this, '_prune'));
 228          }
 229      }
 230  
 231      /**
 232       * Prune old store entries at request shutdown.
 233       *
 234       * @access private
 235       */
 236      function _prune()
 237      {
 238          $pruneOids = array_slice($this->_pruneList, 0, $_SESSION[$this->_name]['__prune'] - $this->_size);
 239          foreach ($pruneOids as $val) {
 240              unset($_SESSION[$this->_name][$val]);
 241          }
 242          $_SESSION[$this->_name]['__prune'] -= count($pruneOids);
 243      }
 244  
 245  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7