123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- require_once 'Auth/Container.php';
- require_once 'adodb.inc.php';
- require_once 'adodb-pear.inc.php';
- require_once 'adodb-errorpear.inc.php';
- class Auth_Container_ADOdb extends Auth_Container
- {
-
- var $options = array();
-
- var $db = null;
- var $dsn = '';
-
-
- var $activeUser = '';
-
-
- function Auth_Container_ADOdb($dsn)
- {
- $this->_setDefaults();
-
- if (is_array($dsn)) {
- $this->_parseOptions($dsn);
- if (empty($this->options['dsn'])) {
- PEAR::raiseError('No connection parameters specified!');
- }
- } else {
-
- $this->options['dsn'] = $dsn;
- }
- }
-
-
-
- function _connect($dsn)
- {
- if (is_string($dsn) || is_array($dsn)) {
- if(!$this->db) {
- $this->db = &ADONewConnection($dsn);
- if( $err = ADODB_Pear_error() ) {
- return PEAR::raiseError($err);
- }
- }
-
- } else {
- return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
- 41,
- PEAR_ERROR_RETURN,
- null,
- null
- );
- }
-
- if(!$this->db) {
- return PEAR::raiseError(ADODB_Pear_error());
- } else {
- return true;
- }
- }
-
-
-
- function _prepare()
- {
- if(!$this->db) {
- $res = $this->_connect($this->options['dsn']);
- }
- return true;
- }
-
-
-
- function query($query)
- {
- $err = $this->_prepare();
- if ($err !== true) {
- return $err;
- }
- return $this->db->query($query);
- }
-
-
-
- function _setDefaults()
- {
- $this->options['db_type'] = 'mysql';
- $this->options['table'] = 'auth';
- $this->options['usernamecol'] = 'username';
- $this->options['passwordcol'] = 'password';
- $this->options['dsn'] = '';
- $this->options['db_fields'] = '';
- $this->options['cryptType'] = 'md5';
- }
-
-
-
- function _parseOptions($array)
- {
- foreach ($array as $key => $value) {
- if (isset($this->options[$key])) {
- $this->options[$key] = $value;
- }
- }
-
- if(!empty($this->options['db_fields'])){
- if(is_array($this->options['db_fields'])){
- $this->options['db_fields'] = join($this->options['db_fields'], ', ');
- }
- $this->options['db_fields'] = ', '.$this->options['db_fields'];
- }
- }
-
-
-
- function fetchData($username, $password)
- {
-
- $err = $this->_prepare();
- if ($err !== true) {
- return PEAR::raiseError($err->getMessage(), $err->getCode());
- }
-
- if(strstr($this->options['db_fields'], '*')){
- $sql_from = "*";
- }
- else{
- $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
- }
-
- $query = "SELECT ".$sql_from.
- " FROM ".$this->options['table'].
- " WHERE ".$this->options['usernamecol']." = " . $this->db->Quote($username);
-
- $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
- $rset = $this->db->Execute( $query );
- $res = $rset->fetchRow();
- if (DB::isError($res)) {
- return PEAR::raiseError($res->getMessage(), $res->getCode());
- }
- if (!is_array($res)) {
- $this->activeUser = '';
- return false;
- }
- if ($this->verifyPassword(trim($password, "\r\n"),
- trim($res[$this->options['passwordcol']], "\r\n"),
- $this->options['cryptType'])) {
-
- foreach ($res as $key => $value) {
- if ($key == $this->options['passwordcol'] ||
- $key == $this->options['usernamecol']) {
- continue;
- }
-
-
- if(is_object($this->_auth_obj)){
- $this->_auth_obj->setAuthData($key, $value);
- } else {
- Auth::setAuthData($key, $value);
- }
- }
- return true;
- }
- $this->activeUser = $res[$this->options['usernamecol']];
- return false;
- }
-
-
- function listUsers()
- {
- $err = $this->_prepare();
- if ($err !== true) {
- return PEAR::raiseError($err->getMessage(), $err->getCode());
- }
- $retVal = array();
-
- if(strstr($this->options['db_fields'], '*')){
- $sql_from = "*";
- }
- else{
- $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
- }
- $query = sprintf("SELECT %s FROM %s",
- $sql_from,
- $this->options['table']
- );
- $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
- if (DB::isError($res)) {
- return PEAR::raiseError($res->getMessage(), $res->getCode());
- } else {
- foreach ($res as $user) {
- $user['username'] = $user[$this->options['usernamecol']];
- $retVal[] = $user;
- }
- }
- return $retVal;
- }
-
-
-
- function addUser($username, $password, $additional = "")
- {
- if (function_exists($this->options['cryptType'])) {
- $cryptFunction = $this->options['cryptType'];
- } else {
- $cryptFunction = 'md5';
- }
- $additional_key = '';
- $additional_value = '';
- if (is_array($additional)) {
- foreach ($additional as $key => $value) {
- $additional_key .= ', ' . $key;
- $additional_value .= ", '" . $value . "'";
- }
- }
- $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
- $this->options['table'],
- $this->options['usernamecol'],
- $this->options['passwordcol'],
- $additional_key,
- $username,
- $cryptFunction($password),
- $additional_value
- );
- $res = $this->query($query);
- if (DB::isError($res)) {
- return PEAR::raiseError($res->getMessage(), $res->getCode());
- } else {
- return true;
- }
- }
-
-
-
- function removeUser($username)
- {
- $query = sprintf("DELETE FROM %s WHERE %s = '%s'",
- $this->options['table'],
- $this->options['usernamecol'],
- $username
- );
- $res = $this->query($query);
- if (DB::isError($res)) {
- return PEAR::raiseError($res->getMessage(), $res->getCode());
- } else {
- return true;
- }
- }
-
- }
- function showDbg( $string ) {
- print "<P>$string</P>";
- }
- function dump( $var, $str, $vardump = false ) {
- print "<H4>$str</H4><pre>";
- ( !$vardump ) ? ( print_r( $var )) : ( var_dump( $var ));
- print "</pre>";
- }
- ?>
|