Source for file DB.php
Documentation is available at DB.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Martin Jansen <mj@php.net> |
// +----------------------------------------------------------------------+
// $Id: DB.php,v 1.45 2004/07/04 16:52:20 yavo Exp $
require_once 'Auth/Container.php';
* Storage driver for fetching login data from a database
* This storage driver can use all databases which are supported
* by the PEAR DB abstraction layer to fetch login data.
* @author Martin Jansen <mj@php.net>
* @version $Revision: 1.45 $
* Additional options for the storage container
* User that is currently selected from the DB.
* Constructor of the container class
* Initate connection to the database via PEAR::DB
* @param string Connection data or DB object
* @return object Returns an error object if something went wrong
$this->_parseOptions ($dsn);
if (empty ($this->options['dsn'])) {
PEAR ::raiseError ('No connection parameters specified!');
* Connect to database by using the given DSN string
* @param string DSN string
* @return mixed Object on error, otherwise bool
$this->db = DB ::Connect ($dsn, $this->options['db_options']);
} elseif (DB ::isError ($dsn)) {
return PEAR ::raiseError ($dsn->getMessage (), $dsn->getCode ());
return PEAR ::raiseError ('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__ ,
if (DB ::isError ($this->db) || PEAR ::isError ($this->db)) {
return PEAR ::raiseError ($this->db->getMessage (), $this->db->getCode ());
* Prepare database connection
* This function checks if we have already opened a connection to
* the database. If that's not the case, a new connection is opened.
* @return mixed True or a DB error object.
if (!DB ::isConnection ($this->db)) {
$res = $this->_connect ($this->options['dsn']);
if (DB ::isError ($res) || PEAR ::isError ($res)) {
* Prepare query to the database
* This function checks if we have already opened a connection to
* the database. If that's not the case, a new connection is opened.
* After that the query is passed to the database.
* @param string Query string
* @return mixed a DB_result object or DB_OK on success, a DB
* or PEAR error on failure
$err = $this->_prepare ();
return $this->db->query ($query);
* Set some default options
$this->options['usernamecol'] = 'username';
$this->options['passwordcol'] = 'password';
$this->options['cryptType'] = 'md5';
$this->options['db_options'] = array ();
* Parse options passed to the container class
function _parseOptions ($array)
foreach ($array as $key => $value) {
/* Include additional fields if they exist */
if (!empty ($this->options['db_fields'])) {
* Get user information from database
* This function uses the given username to fetch
* the corresponding login data from the database
* table. If an account that matches the passed username
* and password is found, the function returns true.
* Otherwise it returns false.
* @param boolean If true password is secured using an md5 hash
* the frontend and auth are responsible for making sure the container supports
* challenge responce password authenthication
* @return mixed Error object or boolean
function fetchData($username, $password, $isChallengeResponce=false ) {
//print "Container_DB::fetchData($username, $password, $isChallengeResponce) <br/>\n";
// Prepare for a database query
$err = $this->_prepare ();
return PEAR ::raiseError ($err->getMessage (), $err->getCode ());
// Find if db_fields contains a *, if so assume all col are selected
$sql_from = $this->options['usernamecol'] . ", ". $this->options['passwordcol']. $this->options['db_fields'];
Old Style, removed to go around the oci8
http://pear.php.net/bugs/bug.php?id=206
$query = "SELECT ! FROM ! WHERE ! = ?";
$this->options['usernamecol'],
$query = "SELECT ". $sql_from.
" WHERE ". $this->options['usernamecol']. " = '". $this->db->quoteString ($username). "'";
$res = $this->db->getRow ($query, null , DB_FETCHMODE_ASSOC );
#print "SQL: $query <br/>\n";
return PEAR ::raiseError ($res->getMessage (), $res->getCode ());
// Perform trimming here before the hashihg
$password = trim($password, "\r\n");
// If using Challeneg Responce md5 the pass with the secret
if($isChallengeResponce) {
//print " Orig Password [{$res[$this->options['passwordcol']]}]<br/>\n";
//print " Challenge [{$this->_auth_obj->session['loginchallenege']}]<br/>\n";
$res[$this->options['passwordcol']] = md5($res[$this->options['passwordcol']]. $this->_auth_obj->session ['loginchallenege']);
// UGLY cannot avoid without modifying verifyPassword
if($this->options['cryptType'] == 'md5') {
//print " Hashed Password [{$res[$this->options['passwordcol']]}]<br/>\n";
$res[$this->options['passwordcol']],
// Store additional field values in the session
foreach ($res as $key => $value) {
if ($key == $this->options['passwordcol'] ||
$key == $this->options['usernamecol']) {
// Use reference to the auth object if exists
// This is because the auth session variable can change so a static call to setAuthData does not make sence
$this->_auth_obj->setAuthData ($key, $value);
$err = $this->_prepare ();
return PEAR ::raiseError ($err->getMessage (), $err->getCode ());
// Find if db_fields contains a *, if so assume all col are selected
$sql_from = $this->options['usernamecol'] . ", ". $this->options['passwordcol']. $this->options['db_fields'];
$query = sprintf("SELECT %s FROM %s",
$res = $this->db->getAll ($query, null , DB_FETCHMODE_ASSOC );
return PEAR ::raiseError ($res->getMessage (), $res->getCode ());
foreach ($res as $user) {
$user['username'] = $user[$this->options['usernamecol']];
* Add user to the storage container
* @param mixed Additional information that are stored in the DB
* @return mixed True on success, otherwise error object
function addUser($username, $password, $additional = "")
if (isset ($this->options['cryptType']) && $this->options['cryptType'] == 'none') {
$cryptFunction = 'strval';
$cryptFunction = $this->options['cryptType'];
$password = $cryptFunction($password);
foreach ($additional as $key => $value) {
$additional_key .= ', ' . $key;
$additional_value .= ", '" . $value . "'";
$query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
$res = $this->query ($query);
return PEAR ::raiseError ($res->getMessage (), $res->getCode ());
* Remove user from the storage container
* @return mixed True on success, otherwise error object
$query = sprintf("DELETE FROM %s WHERE %s = '%s'",
$res = $this->query ($query);
return PEAR ::raiseError ($res->getMessage (), $res->getCode ());
* Change password for user in the storage container
* @param string The new password (plain text)
if (isset ($this->options['cryptType']) && $this->options['cryptType'] == 'none') {
$cryptFunction = 'strval';
$cryptFunction = $this->options['cryptType'];
$password = $cryptFunction($password);
$query = sprintf("UPDATE %s SET %s = '%s' WHERE %s = '%s'",
$res = $this->query ($query);
return PEAR ::raiseError ($res->getMessage (), $res->getCode ());
if( $this->options['cryptType'] == 'md5' || $this->options['cryptType'] == 'none' || $this->options['cryptType'] == '' ) {
return($this->options['cryptType']);
Documentation generated on Mon, 11 Mar 2019 13:52:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|