[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

/lib/Horde/Token/ -> sql.php (source)

   1  <?php
   2  /**
   3   * Token tracking implementation for PHP's PEAR database abstraction layer.
   4   *
   5   * Required parameters:<pre>
   6   *   'phptype'   The database type (ie. 'pgsql', 'mysql', etc.).</pre>
   7   *
   8   * Required by some database implementations:<pre>
   9   *   'database'  The name of the database.
  10   *   'hostspec'  The hostname of the database server.
  11   *   'username'  The username with which to connect to the database.
  12   *   'password'  The password associated with 'username'.
  13   *   'options'   Additional options to pass to the database.
  14   *   'tty'       The TTY on which to connect to the database.
  15   *   'port'      The port on which to connect to the database.</pre>
  16   *
  17   * Optional parameters:<pre>
  18   *   'table'     The name of the tokens table in 'database'.
  19   *               Defaults to 'horde_tokens'.
  20   *   'timeout'   The period (in seconds) after which an id is purged.
  21   *               Defaults to 86400 (i.e. 24 hours).</pre>
  22   *
  23   * The table structure for the tokens is as follows:
  24   *
  25   * <pre>
  26   * CREATE TABLE horde_tokens (
  27   *     token_address    VARCHAR(100) NOT NULL,
  28   *     token_id         VARCHAR(32) NOT NULL,
  29   *     token_timestamp  BIGINT NOT NULL,
  30   *
  31   *     PRIMARY KEY (token_address, token_id)
  32   * );
  33   * </pre>
  34   *
  35   * $Horde: framework/Token/Token/sql.php,v 1.23.6.12 2006/01/18 19:40:51 ben Exp $
  36   *
  37   * Copyright 1999-2006 Max Kalika <max@horde.org>
  38   *
  39   * See the enclosed file COPYING for license information (LGPL). If you
  40   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  41   *
  42   * @author  Max Kalika <max@horde.org>
  43   * @since   Horde 1.3
  44   * @package Horde_Token
  45   */
  46  class Horde_Token_sql extends Horde_Token {
  47  
  48      /**
  49       * Handle for the current database connection.
  50       *
  51       * @var DB
  52       */
  53      var $_db = '';
  54  
  55      /**
  56       * Boolean indicating whether or not we're connected to the SQL
  57       * server.
  58       *
  59       * @var boolean
  60       */
  61      var $_connected = false;
  62  
  63      /**
  64       * Constructs a new SQL connection object.
  65       *
  66       * @param array $params  A hash containing connection parameters.
  67       */
  68      function Horde_Token_sql($params = array())
  69      {
  70          $this->_params = $params;
  71  
  72          /* Set timeout to 24 hours if not specified. */
  73          if (!isset($this->_params['timeout'])) {
  74              $this->_params['timeout'] = 86400;
  75          }
  76      }
  77  
  78      /**
  79       * Deletes all expired connection id's from the SQL server.
  80       *
  81       * @return boolean  True on success, a PEAR_Error object on failure.
  82       */
  83      function purge()
  84      {
  85          if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
  86              return $result;
  87          }
  88  
  89          /* Build SQL query. */
  90          $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE ';
  91          $query .= 'token_timestamp < ?';
  92  
  93          $values = array(time() - $this->_params['timeout']);
  94  
  95          $result = $this->_db->query($query, $values);
  96  
  97          /* Return an error if the update fails, too. */
  98          if (is_a($result, 'PEAR_Error')) {
  99              return $result;
 100          }
 101  
 102          return true;
 103      }
 104  
 105      function exists($tokenID)
 106      {
 107          if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
 108              return false;
 109          }
 110  
 111          /* Build SQL query. */
 112          $query  = 'SELECT token_id FROM ' . $this->_params['table'];
 113          $query .= ' WHERE token_address = ?';
 114          $query .= ' AND token_id = ?';
 115  
 116          $values = array($this->encodeRemoteAddress(), $tokenID);
 117  
 118          $result = $this->_db->getOne($query, $values);
 119          if (is_a($result, 'PEAR_Error')) {
 120              return false;
 121          } else {
 122              return !empty($result);
 123          }
 124      }
 125  
 126      function add($tokenID)
 127      {
 128          if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
 129              return $result;
 130          }
 131  
 132          /* Build SQL query. */
 133          $query  = 'INSERT INTO ' . $this->_params['table'] . ' (token_address, token_id, token_timestamp) ';
 134          $query .= 'VALUES (?, ?, ?)';
 135  
 136          $values = array($this->encodeRemoteAddress(), $tokenID, time());
 137  
 138          $result = $this->_db->query($query, $values);
 139          if (is_a($result, 'PEAR_Error')) {
 140              return $result;
 141          }
 142  
 143          return true;
 144      }
 145  
 146      /**
 147       * Opens a connection to the SQL server.
 148       *
 149       * @return boolean  True on success, a PEAR_Error object on failure.
 150       */
 151      function _connect()
 152      {
 153          if ($this->_connected) {
 154              return true;
 155          }
 156  
 157          $result = Util::assertDriverConfig($this->_params, array('phptype'),
 158                                             'token SQL', array('driver' => 'token'));
 159          if (is_a($result, 'PEAR_Error')) {
 160              return $result;
 161          }
 162  
 163          if (!isset($this->_params['database'])) {
 164              $this->_params['database'] = '';
 165          }
 166          if (!isset($this->_params['username'])) {
 167              $this->_params['username'] = '';
 168          }
 169          if (!isset($this->_params['password'])) {
 170              $this->_params['password'] = '';
 171          }
 172          if (!isset($this->_params['hostspec'])) {
 173              $this->_params['hostspec'] = '';
 174          }
 175          if (!isset($this->_params['table'])) {
 176              $this->_params['table'] = 'horde_tokens';
 177          }
 178  
 179          /* Connect to the SQL server using the supplied parameters. */
 180          require_once 'DB.php';
 181          $this->_db = &DB::connect($this->_params,
 182                                    array('persistent' => !empty($this->_params['persistent'])));
 183          if (is_a($this->_db, 'PEAR_Error')) {
 184              return $this->_db;
 185          }
 186  
 187          // Set DB portability options.
 188          switch ($this->_db->phptype) {
 189          case 'mssql':
 190              $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
 191              break;
 192          default:
 193              $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
 194          }
 195  
 196          $this->_connected = true;
 197  
 198          return true;
 199      }
 200  
 201      /**
 202       * Disconnect from the SQL server and clean up the connection.
 203       *
 204       * @return boolean  True on success, a PEAR_Error object on failure.
 205       */
 206      function _disconnect()
 207      {
 208          if ($this->_connected) {
 209              $this->_connected = false;
 210              return $this->_db->disconnect();
 211          }
 212  
 213          return true;
 214      }
 215  
 216  }


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