Overview

Packages

  • Token

Classes

  • Horde_Token
  • Horde_Token_Base
  • Horde_Token_Exception
  • Horde_Token_Exception_Expired
  • Horde_Token_Exception_Invalid
  • Horde_Token_Exception_Used
  • Horde_Token_File
  • Horde_Token_Null
  • Horde_Token_Sql
  • Horde_Token_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Token tracking implementation for PHP's PEAR database abstraction layer.
  4:  *
  5:  * The table structure for the tokens is as follows:
  6:  * <pre>
  7:  * CREATE TABLE horde_tokens (
  8:  *     token_address    VARCHAR(100) NOT NULL,
  9:  *     token_id         VARCHAR(32) NOT NULL,
 10:  *     token_timestamp  BIGINT NOT NULL,
 11:  *
 12:  *     PRIMARY KEY (token_address, token_id)
 13:  * );
 14:  * </pre>
 15:  *
 16:  * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
 17:  *
 18:  * See the enclosed file COPYING for license information (LGPL). If you
 19:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 20:  *
 21:  * @author   Max Kalika <max@horde.org>
 22:  * @category Horde
 23:  * @package  Token
 24:  */
 25: class Horde_Token_Sql extends Horde_Token_Base
 26: {
 27:     /**
 28:      * Handle for the database connection.
 29:      *
 30:      * @var Horde_Db_Adapter
 31:      */
 32:     protected $_db;
 33: 
 34:     /**
 35:      * Constructor.
 36:      *
 37:      * @see Horde_Token_Base::__construct() for more parameters.
 38:      *
 39:      * @param array $params  Required parameters:
 40:      * - db (Horde_Db_Adapter): The DB instance.
 41:      * Optional parameters:
 42:      * - table (string): The name of the tokens table.
 43:      *                   DEFAULT: 'horde_tokens'
 44:      * </pre>
 45:      *
 46:      * @throws Horde_Token_Exception
 47:      */
 48:     public function __construct($params = array())
 49:     {
 50:         if (!isset($params['db'])) {
 51:             throw new Horde_Token_Exception('Missing db parameter.');
 52:         }
 53:         $this->_db = $params['db'];
 54:         unset($params['db']);
 55: 
 56:         $params = array_merge(array(
 57:             'table' => 'horde_tokens',
 58:         ), $params);
 59: 
 60:         parent::__construct($params);
 61:     }
 62: 
 63:     /**
 64:      * Delete all expired connection IDs.
 65:      *
 66:      * @throws Horde_Token_Exception
 67:      */
 68:     public function purge()
 69:     {
 70:         /* Build SQL query. */
 71:         $query = 'DELETE FROM ' . $this->_params['table']
 72:             . ' WHERE token_timestamp < ?';
 73: 
 74:         $values = array(time() - $this->_params['timeout']);
 75: 
 76:         /* Return an error if the update fails. */
 77:         try {
 78:             $this->_db->delete($query, $values);
 79:         } catch (Horde_Db_Exception $e) {
 80:             throw new Horde_Token_Exception($e);
 81:         }
 82:     }
 83: 
 84:     /**
 85:      * Does the token exist?
 86:      *
 87:      * @param string $tokenID  Token ID.
 88:      *
 89:      * @return boolean  True if the token exists.
 90:      * @throws Horde_Token_Exception
 91:      */
 92:     public function exists($tokenID)
 93:     {
 94:         /* Build SQL query. */
 95:         $query = 'SELECT token_id FROM ' . $this->_params['table']
 96:             . ' WHERE token_address = ? AND token_id = ?';
 97: 
 98:         $values = array($this->_encodeRemoteAddress(), $tokenID);
 99: 
100:         try {
101:             return $this->_db->selectValue($query, $values);
102:         } catch (Horde_Db_Exception $e) {
103:             return false;
104:         }
105:     }
106: 
107:     /**
108:      * Add a token ID.
109:      *
110:      * @param string $tokenID  Token ID to add.
111:      *
112:      * @throws Horde_Token_Exception
113:      */
114:     public function add($tokenID)
115:     {
116:         /* Build SQL query. */
117:         $query = 'INSERT INTO ' . $this->_params['table']
118:             . ' (token_address, token_id, token_timestamp)'
119:             . ' VALUES (?, ?, ?)';
120: 
121:         $values = array($this->_encodeRemoteAddress(), $tokenID, time());
122: 
123:         try {
124:             $this->_db->insert($query, $values);
125:         } catch (Horde_Db_Exception $e) {
126:             throw new Horde_Token_Exception($e);
127:         }
128:     }
129: 
130: }
131: 
API documentation generated by ApiGen