Adodb Dokumentation  V5.14 8 Sept 2011
pear/Auth/Container/ADOdb.php
00001 <?php
00002 /* 
00003 V5.14 8 Sept 2011  (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
00004   Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence. See License.txt. 
00007   Set tabs to 4 for best viewing.
00008   
00009   Latest version is available at http://adodb.sourceforge.net
00010   
00011         Original Authors: Martin Jansen <mj#php.net>
00012         Richard Tango-Lowy <richtl#arscognita.com>                          
00013 */
00014 
00015 require_once 'Auth/Container.php';
00016 require_once 'adodb.inc.php';
00017 require_once 'adodb-pear.inc.php';
00018 require_once 'adodb-errorpear.inc.php';
00019 
00032 class Auth_Container_ADOdb extends Auth_Container
00033 {
00034 
00039     var $options = array();
00040 
00045     var $db = null;
00046     var $dsn = '';
00047         
00052     var $activeUser = '';
00053 
00054     // {{{ Constructor
00055 
00064     function Auth_Container_ADOdb($dsn)
00065     {
00066         $this->_setDefaults();
00067                 
00068         if (is_array($dsn)) {
00069             $this->_parseOptions($dsn);
00070 
00071             if (empty($this->options['dsn'])) {
00072                 PEAR::raiseError('No connection parameters specified!');
00073             }
00074         } else {
00075                 // Extract db_type from dsn string.
00076             $this->options['dsn'] = $dsn;
00077         }
00078     }
00079 
00080     // }}}
00081     // {{{ _connect()
00082 
00090      function _connect($dsn)
00091     {
00092         if (is_string($dsn) || is_array($dsn)) {
00093                 if(!$this->db) {
00094                         $this->db = ADONewConnection($dsn);
00095                         if( $err = ADODB_Pear_error() ) {
00096                                 return PEAR::raiseError($err);
00097                         }
00098                 }
00099                 
00100         } else {
00101             return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
00102                                     41,
00103                                     PEAR_ERROR_RETURN,
00104                                     null,
00105                                     null
00106                                     );
00107         }
00108         
00109         if(!$this->db) {
00110                 return PEAR::raiseError(ADODB_Pear_error());
00111         } else {
00112                 return true;
00113         }
00114     }
00115 
00116     // }}}
00117     // {{{ _prepare()
00118 
00128     function _prepare()
00129     {
00130         if(!$this->db) {
00131                 $res = $this->_connect($this->options['dsn']);                  
00132         }
00133         return true;
00134     }
00135 
00136     // }}}
00137     // {{{ query()
00138 
00151     function query($query)
00152     {
00153         $err = $this->_prepare();
00154         if ($err !== true) {
00155             return $err;
00156         }
00157         return $this->db->query($query);
00158     }
00159 
00160     // }}}
00161     // {{{ _setDefaults()
00162 
00169     function _setDefaults()
00170     {
00171         $this->options['db_type']       = 'mysql';
00172         $this->options['table']       = 'auth';
00173         $this->options['usernamecol'] = 'username';
00174         $this->options['passwordcol'] = 'password';
00175         $this->options['dsn']         = '';
00176         $this->options['db_fields']   = '';
00177         $this->options['cryptType']   = 'md5';
00178     }
00179 
00180     // }}}
00181     // {{{ _parseOptions()
00182 
00189     function _parseOptions($array)
00190     {
00191         foreach ($array as $key => $value) {
00192             if (isset($this->options[$key])) {
00193                 $this->options[$key] = $value;
00194             }
00195         }
00196 
00197         /* Include additional fields if they exist */
00198         if(!empty($this->options['db_fields'])){
00199             if(is_array($this->options['db_fields'])){
00200                 $this->options['db_fields'] = join($this->options['db_fields'], ', ');
00201             }
00202             $this->options['db_fields'] = ', '.$this->options['db_fields'];
00203         }
00204     }
00205 
00206     // }}}
00207     // {{{ fetchData()
00208 
00222     function fetchData($username, $password)
00223     {
00224         // Prepare for a database query
00225         $err = $this->_prepare();
00226         if ($err !== true) {
00227             return PEAR::raiseError($err->getMessage(), $err->getCode());
00228         }
00229 
00230         // Find if db_fields contains a *, i so assume all col are selected
00231         if(strstr($this->options['db_fields'], '*')){
00232             $sql_from = "*";
00233         }
00234         else{
00235             $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
00236         }
00237         
00238         $query = "SELECT ".$sql_from.
00239                 " FROM ".$this->options['table'].
00240                 " WHERE ".$this->options['usernamecol']." = " . $this->db->Quote($username);
00241         
00242         $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00243         $rset = $this->db->Execute( $query );
00244         $res = $rset->fetchRow();
00245 
00246         if (DB::isError($res)) {
00247             return PEAR::raiseError($res->getMessage(), $res->getCode());
00248         }
00249         if (!is_array($res)) {
00250             $this->activeUser = '';
00251             return false;
00252         }
00253         if ($this->verifyPassword(trim($password, "\r\n"),
00254                                   trim($res[$this->options['passwordcol']], "\r\n"),
00255                                   $this->options['cryptType'])) {
00256             // Store additional field values in the session
00257             foreach ($res as $key => $value) {
00258                 if ($key == $this->options['passwordcol'] ||
00259                     $key == $this->options['usernamecol']) {
00260                     continue;
00261                 }
00262                 // Use reference to the auth object if exists
00263                 // This is because the auth session variable can change so a static call to setAuthData does not make sence
00264                 if(is_object($this->_auth_obj)){
00265                     $this->_auth_obj->setAuthData($key, $value);
00266                 } else {
00267                     Auth::setAuthData($key, $value);
00268                 }
00269             }
00270 
00271             return true;
00272         }
00273 
00274         $this->activeUser = $res[$this->options['usernamecol']];
00275         return false;
00276     }
00277 
00278     // }}}
00279     // {{{ listUsers()
00280 
00281     function listUsers()
00282     {
00283         $err = $this->_prepare();
00284         if ($err !== true) {
00285             return PEAR::raiseError($err->getMessage(), $err->getCode());
00286         }
00287 
00288         $retVal = array();
00289 
00290         // Find if db_fileds contains a *, i so assume all col are selected
00291         if(strstr($this->options['db_fields'], '*')){
00292             $sql_from = "*";
00293         }
00294         else{
00295             $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
00296         }
00297 
00298         $query = sprintf("SELECT %s FROM %s",
00299                          $sql_from,
00300                          $this->options['table']
00301                          );
00302         $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
00303 
00304         if (DB::isError($res)) {
00305             return PEAR::raiseError($res->getMessage(), $res->getCode());
00306         } else {
00307             foreach ($res as $user) {
00308                 $user['username'] = $user[$this->options['usernamecol']];
00309                 $retVal[] = $user;
00310             }
00311         }
00312         return $retVal;
00313     }
00314 
00315     // }}}
00316     // {{{ addUser()
00317 
00328     function addUser($username, $password, $additional = "")
00329     {
00330         if (function_exists($this->options['cryptType'])) {
00331             $cryptFunction = $this->options['cryptType'];
00332         } else {
00333             $cryptFunction = 'md5';
00334         }
00335 
00336         $additional_key   = '';
00337         $additional_value = '';
00338 
00339         if (is_array($additional)) {
00340             foreach ($additional as $key => $value) {
00341                 $additional_key .= ', ' . $key;
00342                 $additional_value .= ", '" . $value . "'";
00343             }
00344         }
00345 
00346         $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
00347                          $this->options['table'],
00348                          $this->options['usernamecol'],
00349                          $this->options['passwordcol'],
00350                          $additional_key,
00351                          $username,
00352                          $cryptFunction($password),
00353                          $additional_value
00354                          );
00355 
00356         $res = $this->query($query);
00357 
00358         if (DB::isError($res)) {
00359            return PEAR::raiseError($res->getMessage(), $res->getCode());
00360         } else {
00361           return true;
00362         }
00363     }
00364 
00365     // }}}
00366     // {{{ removeUser()
00367 
00376     function removeUser($username)
00377     {
00378         $query = sprintf("DELETE FROM %s WHERE %s = '%s'",
00379                          $this->options['table'],
00380                          $this->options['usernamecol'],
00381                          $username
00382                          );
00383 
00384         $res = $this->query($query);
00385 
00386         if (DB::isError($res)) {
00387            return PEAR::raiseError($res->getMessage(), $res->getCode());
00388         } else {
00389           return true;
00390         }
00391     }
00392 
00393     // }}}
00394 }
00395 
00396 function showDbg( $string ) {
00397         print "
00398 -- $string</P>";
00399 }
00400 function dump( $var, $str, $vardump = false ) {
00401         print "<H4>$str</H4><pre>";
00402         ( !$vardump ) ? ( print_r( $var )) : ( var_dump( $var ));
00403         print "</pre>";
00404 }
00405 ?>