cvs: pear /Auth/Container DB.php

From: Date: Thu, 01 Nov 2001 14:42:22 +0000
Subject: cvs: pear /Auth/Container DB.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1229@lists.php.net to get a copy of this message
mj Thu Nov 1 09:42:22 2001 EDT Modified files: /pear/Auth/Container DB.php Log: * Make it possible to pass other names for the table, the column containing the username and for the column name containing the password to the constructor of the DB container. Index: pear/Auth/Container/DB.php diff -u pear/Auth/Container/DB.php:1.3 pear/Auth/Container/DB.php:1.4 --- pear/Auth/Container/DB.php:1.3 Fri Aug 24 17:19:15 2001 +++ pear/Auth/Container/DB.php Thu Nov 1 09:42:22 2001 @@ -16,7 +16,7 @@ // | Authors: Martin Jansen <mj@php.net> | // +----------------------------------------------------------------------+ // -// $Id: DB.php,v 1.3 2001/08/24 21:19:15 mj Exp $ +// $Id: DB.php,v 1.4 2001/11/01 14:42:22 mj Exp $ // require_once "Auth/Container.php"; @@ -30,23 +30,22 @@ * * @author Martin Jansen <mj@php.net> * @package Auth + * @version $Revision: 1.4 $ */ class Auth_Container_DB extends Auth_Container { /** - * Name of the database table - * @var string - * @see fetch_data + * Additional options for the storage container + * @var array */ - var $table = "auth"; + var $options = array(); /** - * Name of the column where the username is stored - * @var string - * @see fetch_data + * DB object + * @var object */ - var $usernameCol = "username"; + var $db = null; // {{{ Constructor @@ -58,22 +57,25 @@ * @param $dsn string connection data or DB object * @return object Returns an error object if something went wrong */ - function Auth_Container_DB($dsn) + function Auth_Container_DB($dsn) { + $this->_setDefaults(); - if (is_string($dsn)) { + if (is_array($dsn)) { - $this->db = DB::Connect($dsn); + $this->_parseOptions($dsn); - if (DB::isError($this->db)) { - return new DB_Error($this->db->code,PEAR_ERROR_DIE); + if ($this->options['dsn'] != "") { + $this->_connect($this->options['dsn']); + } else { + return new DB_Error("No connection parameters specified!"); } - + } else if (is_string($dsn)) { + $this->_connect($dsn); } elseif (is_object($dsn) && DB::isError($dsn)) { - echo "The given param was not valid in file ".__FILE__." at line ".__LINE__."<br>\n"; - return new DB_Error($dsn->code,PEAR_ERROR_DIE); + return new DB_Error($dsn->code, PEAR_ERROR_DIE); } // if parent class is db_common, then it's already a connected identifier @@ -82,7 +84,67 @@ } else { - return new PEAR_Error("The given dsn was not valid in file ".__FILE__." at line ".__LINE__,41,PEAR_ERROR_RETURN,null,null); + return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__, + 41, + PEAR_ERROR_RETURN, + null, + null + ); + } + } + + // }}} + // {{{ _connect + + /** + * Connect to database by using the given DSN string + * + * @access private + * @param string DSN string + * @return mixed Object on error, otherwise bool + */ + function _connect($dsn) + { + $this->db = DB::Connect($dsn); + + if (DB::isError($this->db)) { + return new DB_Error($this->db->code, PEAR_ERROR_DIE); + } else { + return true; + } + } + + // }}} + // {{{ _setDefaults + + /** + * Set some default options + * + * @access private + */ + function _setDefaults() + { + $this->options['table'] = "auth"; + $this->options['usernamecol'] = "username"; + $this->options['passwordcol'] = "password"; + $this->options['dsn'] = ""; + } + + // }}} + // {{{ _parseOptions + + /** + * Parse options passed to the container class + * + * @access private + * @param array + */ + function _parseOptions($array) + { + foreach ($array as $key => $value) { + if (isset($this->options[$key])) { + $this->options[$key] = $value; + } } } @@ -102,26 +164,28 @@ * @param string Password * @return mixed Error object or boolean */ - function fetchData($username,$password) - { - - $query = sprintf("SELECT username FROM %s - WHERE username = '%s' - AND password = '%s'", - $this->table, + function fetchData($username, $password) + { + $query = sprintf("SELECT %s FROM %s + WHERE %s = '%s' + AND %s = '%s'", + $this->options['usernamecol'], + $this->options['table'], + $this->options['usernamecol'], $this->db->quoteString($username), + $this->options['passwordcol'], md5($password) ); - + $res = $this->db->query($query); if (DB::isError($res)) { - return new DB_Error($res->code,PEAR_ERROR_DIE); + return new DB_Error($res->code, PEAR_ERROR_DIE); } else { $entry = $res->FetchRow(DB_FETCHMODE_ASSOC); if (is_array($entry)) { - Auth::setAuth($entry['username']); + Auth::setAuth($entry[$this->options['usernamecol']]); $res->free(); return true;

« previous php.pear.cvs (#1229) next »