Source for file MDB2.php
Documentation is available at MDB2.php
* @uses OpenID_Store_Interface
* @author Bill Shupp <hostmaster@shupp.org>
* @copyright 2009 Bill Shupp
* @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
* @link http://pearopenid.googlecode.com
require_once 'OpenID/Store/Interface.php';
require_once 'OpenID/Store/Exception.php';
require_once 'OpenID.php';
require_once 'OpenID/Discover.php';
require_once 'OpenID/Association.php';
require_once 'OpenID/Nonce.php';
* A first pass at SQL support via MDB2. This may have some MySQL specific things
* so it might get refactored a bit to support other DBs.
* @uses OpenID_Store_Interface
* @author Bill Shupp <hostmaster@shupp.org>
* @copyright 2009 Bill Shupp
* @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
* @link http://pearopenid.googlecode.com
* Table names which you can override in a child class
'nonce' =>
'OpenIDNonces',
'association' =>
'OpenIDAssociations',
'discovery' =>
'OpenIDDiscovery',
* Calls MDB2::factory(). Connections are lazy loaded upon queries.
* @param array $options Array of options to pass to MDB2::factory(). Note that
* you must also include the key 'dsn', which is used as
* the first argument to MDB2::factory(), and is not
* passed with the options argument.
* @throws OpenID_Store_Exception on error or missing DSN in options
if (!isset
($options['dsn'])) {
$this->db =
MDB2::factory($dsn, $options);
if (PEAR::isError($this->db)) {
throw
new OpenID_Store_Exception('Error connecting to DB', $this->db);
* @throws OpenID_Store_Exception on failure to create tables
* @return OpenID_Store_MDB2
$nonceCreate =
"CREATE TABLE {$this->tableNames['nonce']} (
uri VARCHAR(2047) NOT NULL,
nonce VARCHAR(100) NOT NULL,
created INTEGER NOT NULL,
UNIQUE (uri(255), nonce, created)
$assocCreate =
"CREATE TABLE {$this->tableNames['association']} (
uri VARCHAR(2047) NOT NULL,
assocHandle VARCHAR(255) NOT NULL,
sharedSecret BLOB NOT NULL,
created INTEGER NOT NULL,
expiresIn INTEGER NOT NULL,
assocType VARCHAR(64) NOT NULL,
PRIMARY KEY (uri(255), assocHandle)
$discoveryCreate =
"CREATE TABLE {$this->tableNames['discovery']} (
identifier VARCHAR(2047) NOT NULL,
serialized_discover BLOB NOT NULL,
expires INTEGER NOT NULL,
PRIMARY KEY (identifier(255))
$queries =
array($nonceCreate, $assocCreate, $discoveryCreate);
foreach ($queries as $sql) {
$result =
$this->db->exec($sql);
if (PEAR::isError($result)) {
'Error creating table', $result);
* A shortcut to handle the error checking of prepare()/execute() in one place.
* @param string $sql The SQL to prepare
* @param array $args The corresponding arguments
* @throws OpenID_Store_Exception on error
$prepared =
$this->db->prepare($sql);
if (PEAR::isError($prepared)) {
$result =
$prepared->execute($args);
if (PEAR::isError($result)) {
* Gets an instance of OpenID_Discover from the SQL server if it exists.
* @param string $identifier The user supplied identifier
* @return false on failure, OpenID_Discover on success
$sql =
"SELECT serialized_discover
FROM {$this->tableNames['discovery']}
if (!$result->numRows()) {
$row =
$result->fetchRow(MDB2_FETCHMODE_ASSOC);
* Adds discoverd infomation to the SQL server
* @param OpenID_Discover $discover The OpenID_Discover instance
* @param int $expire The time (in seconds) that the cached object
* @return OpenID_Store_MDB2
public function setDiscover(OpenID_Discover $discover, $expire =
3600)
$sql =
"REPLACE INTO {$this->tableNames['discovery']}
(identifier, serialized_discover, expires)
* Gets an association from the SQL server
* @param string $uri The OP Endpoint URL
* @param string $handle The association handle if available
* @return OpenID_Association on success, false on failure
$sql =
"SELECT * FROM {$this->tableNames['association']}
$sql =
"SELECT * FROM {$this->tableNames['association']}
WHERE uri = ? AND assocHandle = ?";
$params =
array($uri, $handle);
if (!$result->numRows()) {
$row =
$result->fetchRow(MDB2_FETCHMODE_ASSOC);
if (($row['expiresin'] +
$row['created']) <
time()) {
'expiresIn' =>
$row['expiresin'],
'created' =>
$row['created'],
'assocType' =>
$row['assoctype'],
'assocHandle' =>
$row['assochandle'],
'sharedSecret' =>
$row['sharedsecret']
* Sets an association in the SQL server
* @param OpenID_Association $association An instance of OpenID_Association
* @return OpenID_Store_MDB2
$sql =
"REPLACE INTO {$this->tableNames['association']}
(uri, assocHandle, sharedSecret, created, expiresIn, assocType)
VALUES (?, ?, ?, ?, ?, ?)";
$association->assocHandle,
$association->sharedSecret,
* Deletes an association from the SQL server
* @param string $uri The OP Endpoint URL
* @return OpenID_Store_MDB2
$sql =
"DELETE FROM {$this->tableNames['association']}
* Gets a nonce from the SQL server if it exists
* @param string $nonce The nonce to retrieve
* @param string $opURL The OP Endpoint URL that it is associated with
* @return string (nonce) on success, false on failure
public function getNonce($nonce, $opURL)
$sql =
"SELECT nonce FROM {$this->tableNames['nonce']}
if (!$result->numRows()) {
$row =
$result->fetchRow(MDB2_FETCHMODE_ASSOC);
* Sets a nonce in the SQL server
* @param string $nonce The nonce value to set
* @param string $opURL The OP Endpoint URL it is associated with
* @return OpenID_Store_MDB2
public function setNonce($nonce, $opURL)
$sql =
"INSERT INTO {$this->tableNames['nonce']}
* Deletes a nonce from the SQL server
* @param string $nonce The nonce value
* @param string $opURL The OP Endpoint URL it is associated with
* @return OpenID_Store_MDB2
$sql =
"DELETE FROM {$this->tableNames['nonce']}
Documentation generated on Tue, 15 Dec 2009 19:00:55 -0800 by phpDocumentor 1.4.3