[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

/lib/Horde/SessionHandler/ -> mysql.php (source)

   1  <?php
   2  /**
   3   * SessionHandler:: implementation for MySQL (native).
   4   *
   5   * Required parameters:<pre>
   6   *   'hostspec'   - The hostname of the database server.
   7   *   'protocol'   - The communication protocol ('tcp', 'unix', etc.).
   8   *   'username'   - The username with which to connect to the database.
   9   *   'password'   - The password associated with 'username'.
  10   *   'database'   - The name of the database.
  11   *   'table'      - The name of the sessiondata table in 'database'.
  12   *   'rowlocking' - Whether to use row-level locking and transactions (InnoDB)
  13   *                  or table-level locking (MyISAM).</pre>
  14   *
  15   * Required for some configurations:<pre>
  16   *   'port'  The port on which to connect to the database.</pre>
  17   *
  18   * Optional parameters:<pre>
  19   *   'persistent'  Use persistent DB connections? (boolean)</pre>
  20   *
  21   * The table structure for the SessionHandler can be found in
  22   * horde/scripts/sql/horde_sessionhandler.sql.
  23   *
  24   * $Horde: framework/SessionHandler/SessionHandler/mysql.php,v 1.16.12.14 2006/03/29 19:06:46 jan Exp $
  25   *
  26   * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz>
  27   * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
  28   * Copyright 2006 Jan Schneider <jan@horde.org>
  29   *
  30   * See the enclosed file COPYING for license information (LGPL). If you
  31   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  32   *
  33   * @author  Mike Cochrame <mike@graftonhall.co.nz>
  34   * @author  Jan Schneider <jan@horde.org>
  35   * @since   Horde 3.0
  36   * @package Horde_SessionHandler
  37   */
  38  class SessionHandler_mysql extends SessionHandler {
  39  
  40      /**
  41       * Handle for the current database connection.
  42       *
  43       * @var resource
  44       */
  45      var $_db;
  46  
  47      /**
  48       * Are we connected to the SQL server.
  49       *
  50       * @var boolean
  51       */
  52      var $_connected = false;
  53  
  54      /**
  55       * Close the SessionHandler backend.
  56       *
  57       * @return boolean  True on success, false otherwise.
  58       */
  59      function close()
  60      {
  61          /* Disconnect from database. */
  62          if ($this->_connected) {
  63              $this->_connected = false;
  64              return @mysql_close($this->_db);
  65          }
  66  
  67          return true;
  68      }
  69  
  70      /**
  71       * Read the data for a particular session identifier from the
  72       * SessionHandler backend.
  73       *
  74       * @param string $id  The session identifier.
  75       *
  76       * @return string  The session data.
  77       */
  78      function read($id)
  79      {
  80          /* Make sure we have a valid database connection. */
  81          $this->_connect();
  82  
  83          /* Session timeout, don't rely on garbage collection. */
  84          $timeout = time() - ini_get('session.gc_maxlifetime');
  85  
  86          $query = sprintf('SELECT session_data FROM %s WHERE session_id = %s' .
  87                           ' AND session_lastmodified > %s',
  88                           $this->_params['table'],
  89                           $this->_quote($id),
  90                           $timeout);
  91  
  92          if (!empty($this->_params['rowlocking'])) {
  93              /* Start a transaction. */
  94              $result = @mysql_query('START TRANSACTION', $this->_db);
  95              $query .= ' FOR UPDATE';
  96          } else {
  97              $result = @mysql_query('LOCK TABLES ' . $this->_params['table'] . ' WRITE', $this->_db);
  98          }
  99          if (!$result) {
 100              return '';
 101          }
 102  
 103          /* Log the query at a DEBUG log level. */
 104          Horde::logMessage(sprintf('SQL Query by SessionHandler_mysql::read(): query = "%s"', $query),
 105                            __FILE__, __LINE__, PEAR_LOG_DEBUG);
 106  
 107          $result = @mysql_query($query, $this->_db);
 108          if (!$result) {
 109              Horde::logMessage('Error retrieving session data (id = ' . $id . ')',
 110                                __FILE__, __LINE__, PEAR_LOG_ERR);
 111              return '';
 112          }
 113  
 114          return @mysql_result($result, 0, 0);
 115      }
 116  
 117      /**
 118       * Write session data to the SessionHandler backend.
 119       *
 120       * @param string $id            The session identifier.
 121       * @param string $session_data  The session data.
 122       *
 123       * @return boolean  True on success, false otherwise.
 124       */
 125      function write($id, $session_data)
 126      {
 127          /* Make sure we have a valid database connection. */
 128          $this->_connect();
 129  
 130          /* Build the SQL query. */
 131          $query = sprintf('REPLACE INTO %s (session_id, session_data, session_lastmodified)' .
 132                           ' VALUES (%s, %s, %s)',
 133                           $this->_params['table'],
 134                           $this->_quote($id),
 135                           $this->_quote($session_data),
 136                           time());
 137  
 138          $result = @mysql_query($query, $this->_db);
 139          if (empty($this->_params['rowlocking'])) {
 140              @mysql_query('UNLOCK TABLES ' . $this->_params['table'], $this->_db);
 141          }
 142          if (!$result) {
 143              @mysql_query('ROLLBACK', $this->_db);
 144              Horde::logMessage('Error writing session data', __FILE__, __LINE__, PEAR_LOG_ERR);
 145              return false;
 146          }
 147  
 148          @mysql_query('COMMIT', $this->_db);
 149  
 150          return true;
 151      }
 152  
 153      /**
 154       * Destroy the data for a particular session identifier in the
 155       * SessionHandler backend.
 156       *
 157       * @param string $id  The session identifier.
 158       *
 159       * @return boolean  True on success, false otherwise.
 160       */
 161      function destroy($id)
 162      {
 163          /* Make sure we have a valid database connection. */
 164          $this->_connect();
 165  
 166          /* Build the SQL query. */
 167          $query = sprintf('DELETE FROM %s WHERE session_id = %s',
 168                           $this->_params['table'], $this->_quote($id));
 169  
 170          /* Log the query at a DEBUG log level. */
 171          Horde::logMessage(sprintf('SQL Query by SessionHandler_mysql::destroy(): query = "%s"', $query),
 172                            __FILE__, __LINE__, PEAR_LOG_DEBUG);
 173  
 174          /* Execute the query. */
 175          $result = @mysql_query($query, $this->_db);
 176          if (empty($this->_params['rowlocking'])) {
 177              @mysql_query('UNLOCK TABLES ' . $this->_params['table'], $this->_db);
 178          }
 179          if (!$result) {
 180              @mysql_query('ROLLBACK', $this->_db);
 181              Horde::logMessage('Failed to delete session (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
 182              return false;
 183          }
 184  
 185          @mysql_query('COMMIT', $this->_db);
 186  
 187          return true;
 188      }
 189  
 190      /**
 191       * Garbage collect stale sessions from the SessionHandler backend.
 192       *
 193       * @param integer $maxlifetime  The maximum age of a session.
 194       *
 195       * @return boolean  True on success, false otherwise.
 196       */
 197      function gc($maxlifetime = 300)
 198      {
 199          /* Make sure we have a valid database connection. */
 200          $this->_connect();
 201  
 202          /* Build the SQL query. */
 203          $query = sprintf('DELETE FROM %s WHERE session_lastmodified < %s',
 204                           $this->_params['table'], (int)(time() - $maxlifetime));
 205  
 206          /* Log the query at a DEBUG log level. */
 207          Horde::logMessage(sprintf('SQL Query by SessionHandler_mysql::gc(): query = "%s"', $query),
 208                            __FILE__, __LINE__, PEAR_LOG_DEBUG);
 209  
 210          /* Execute the query. */
 211          $result = @mysql_query($query, $this->_db);
 212          if (!$result) {
 213              Horde::logMessage('Error garbage collecting old sessions', __FILE__, __LINE__, PEAR_LOG_ERR);
 214              return false;
 215          }
 216  
 217          return @mysql_affected_rows($this->_db);
 218      }
 219  
 220      /**
 221       * Get a list of the valid session identifiers.
 222       *
 223       * @return array  A list of valid session identifiers.
 224       */
 225      function getSessionIDs()
 226      {
 227          /* Make sure we have a valid database connection. */
 228          $this->_connect();
 229  
 230          /* Session timeout, don't rely on garbage collection */
 231          $timeout = time() - ini_get('session.gc_maxlifetime');
 232  
 233          $query = sprintf('SELECT session_id FROM %s' .
 234                           ' WHERE session_lastmodified > %s',
 235                           $this->_params['table'],
 236                           $timeout);
 237  
 238          /* Log the query at a DEBUG log level. */
 239          Horde::logMessage(sprintf('SQL Query by SessionHandler_mysql::getSessionIDs(): query = "%s"', $query),
 240                            __FILE__, __LINE__, PEAR_LOG_DEBUG);
 241  
 242          $result = @mysql_query($query, $this->_db);
 243          if (!$result) {
 244              Horde::logMessage('Error getting session IDs',
 245                                __FILE__, __LINE__, PEAR_LOG_ERR);
 246              return false;
 247          }
 248  
 249          $sessions = array();
 250  
 251          while ($row = mysql_fetch_row($result))
 252              $sessions[] = $row[0];
 253  
 254          return $sessions;
 255      }
 256  
 257      /**
 258       * Escape a mysql string.
 259       *
 260       * @access private
 261       *
 262       * @param string $value  The string to quote.
 263       *
 264       * @return string  The quoted string.
 265       */
 266      function _quote($value)
 267      {
 268          switch (strtolower(gettype($value))) {
 269          case 'null':
 270              return 'NULL';
 271  
 272          case 'integer':
 273              return $value;
 274  
 275          case 'string':
 276          default:
 277              return "'" . @mysql_real_escape_string($value, $this->_db) . "'";
 278          }
 279      }
 280  
 281      /**
 282       * Attempts to open a connection to the SQL server.
 283       *
 284       * @access private
 285       */
 286      function _connect()
 287      {
 288          if ($this->_connected) {
 289              return;
 290          }
 291  
 292          Horde::assertDriverConfig($this->_params, 'sessionhandler',
 293              array('hostspec', 'username', 'database'),
 294              'session handler MySQL');
 295  
 296          if (empty($this->_params['password'])) {
 297              $this->_params['password'] = '';
 298          }
 299  
 300          if (empty($this->_params['table'])) {
 301              $this->_params['table'] = 'horde_sessionhandler';
 302          }
 303  
 304          if (empty($this->_params['persistent'])) {
 305              $connect = 'mysql_connect';
 306          } else {
 307              $connect = 'mysql_pconnect';
 308          }
 309  
 310          if (!$this->_db = @$connect($this->_params['hostspec'],
 311                                      $this->_params['username'],
 312                                      $this->_params['password'])) {
 313              Horde::fatal(PEAR::raiseError('Could not connect to database for SQL SessionHandler.'), __FILE__, __LINE__);
 314          }
 315  
 316          if (!@mysql_select_db($this->_params['database'], $this->_db)) {
 317              Horde::fatal(PEAR::raiseError(sprintf('Could not connect to table %s for SQL SessionHandler.', $this->_params['database']), __FILE__, __LINE__));
 318          }
 319  
 320          $this->_connected = true;
 321      }
 322  
 323  }


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