00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 require_once 'Auth/Container.php';
00024 require_once 'adodb.inc.php';
00025 require_once 'adodb-pear.inc.php';
00026 require_once 'adodb-errorpear.inc.php';
00027
00040 class Auth_Container_ADOdb extends Auth_Container
00041 {
00042
00047 var $options = array();
00048
00053 var $db = null;
00054 var $dsn = '';
00055
00060 var $activeUser = '';
00061
00062
00063
00072 function Auth_Container_ADOdb($dsn)
00073 {
00074 $this->_setDefaults();
00075
00076 if (is_array($dsn)) {
00077 $this->_parseOptions($dsn);
00078
00079 if (empty($this->options['dsn'])) {
00080 PEAR::raiseError('No connection parameters specified!');
00081 }
00082 } else {
00083
00084 $this->options['dsn'] = $dsn;
00085 }
00086 }
00087
00088
00089
00090
00098 function _connect($dsn)
00099 {
00100 if (is_string($dsn) || is_array($dsn)) {
00101 if(!$this->db) {
00102 $this->db = &ADONewConnection($dsn);
00103 if( $err = ADODB_Pear_error() ) {
00104 return PEAR::raiseError($err);
00105 }
00106 }
00107
00108 } else {
00109 return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
00110 41,
00111 PEAR_ERROR_RETURN,
00112 null,
00113 null
00114 );
00115 }
00116
00117 if(!$this->db) {
00118 return PEAR::raiseError(ADODB_Pear_error());
00119 } else {
00120 return true;
00121 }
00122 }
00123
00124
00125
00126
00136 function _prepare()
00137 {
00138 if(!$this->db) {
00139 $res = $this->_connect($this->options['dsn']);
00140 }
00141 return true;
00142 }
00143
00144
00145
00146
00159 function query($query)
00160 {
00161 $err = $this->_prepare();
00162 if ($err !== true) {
00163 return $err;
00164 }
00165 return $this->db->query($query);
00166 }
00167
00168
00169
00170
00177 function _setDefaults()
00178 {
00179 $this->options['db_type'] = 'mysql';
00180 $this->options['table'] = 'auth';
00181 $this->options['usernamecol'] = 'username';
00182 $this->options['passwordcol'] = 'password';
00183 $this->options['dsn'] = '';
00184 $this->options['db_fields'] = '';
00185 $this->options['cryptType'] = 'md5';
00186 }
00187
00188
00189
00190
00197 function _parseOptions($array)
00198 {
00199 foreach ($array as $key => $value) {
00200 if (isset($this->options[$key])) {
00201 $this->options[$key] = $value;
00202 }
00203 }
00204
00205
00206 if(!empty($this->options['db_fields'])){
00207 if(is_array($this->options['db_fields'])){
00208 $this->options['db_fields'] = join($this->options['db_fields'], ', ');
00209 }
00210 $this->options['db_fields'] = ', '.$this->options['db_fields'];
00211 }
00212 }
00213
00214
00215
00216
00230 function fetchData($username, $password)
00231 {
00232
00233 $err = $this->_prepare();
00234 if ($err !== true) {
00235 return PEAR::raiseError($err->getMessage(), $err->getCode());
00236 }
00237
00238
00239 if(strstr($this->options['db_fields'], '*')){
00240 $sql_from = "*";
00241 }
00242 else{
00243 $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
00244 }
00245
00246 $query = "SELECT ".$sql_from.
00247 " FROM ".$this->options['table'].
00248 " WHERE ".$this->options['usernamecol']." = " . $this->db->Quote($username);
00249
00250 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00251 $rset = $this->db->Execute( $query );
00252 $res = $rset->fetchRow();
00253
00254 if (DB::isError($res)) {
00255 return PEAR::raiseError($res->getMessage(), $res->getCode());
00256 }
00257 if (!is_array($res)) {
00258 $this->activeUser = '';
00259 return false;
00260 }
00261 if ($this->verifyPassword(trim($password, "\r\n"),
00262 trim($res[$this->options['passwordcol']], "\r\n"),
00263 $this->options['cryptType'])) {
00264
00265 foreach ($res as $key => $value) {
00266 if ($key == $this->options['passwordcol'] ||
00267 $key == $this->options['usernamecol']) {
00268 continue;
00269 }
00270
00271
00272 if(is_object($this->_auth_obj)){
00273 $this->_auth_obj->setAuthData($key, $value);
00274 } else {
00275 Auth::setAuthData($key, $value);
00276 }
00277 }
00278
00279 return true;
00280 }
00281
00282 $this->activeUser = $res[$this->options['usernamecol']];
00283 return false;
00284 }
00285
00286
00287
00288
00289 function listUsers()
00290 {
00291 $err = $this->_prepare();
00292 if ($err !== true) {
00293 return PEAR::raiseError($err->getMessage(), $err->getCode());
00294 }
00295
00296 $retVal = array();
00297
00298
00299 if(strstr($this->options['db_fields'], '*')){
00300 $sql_from = "*";
00301 }
00302 else{
00303 $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
00304 }
00305
00306 $query = sprintf("SELECT %s FROM %s",
00307 $sql_from,
00308 $this->options['table']
00309 );
00310 $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
00311
00312 if (DB::isError($res)) {
00313 return PEAR::raiseError($res->getMessage(), $res->getCode());
00314 } else {
00315 foreach ($res as $user) {
00316 $user['username'] = $user[$this->options['usernamecol']];
00317 $retVal[] = $user;
00318 }
00319 }
00320 return $retVal;
00321 }
00322
00323
00324
00325
00336 function addUser($username, $password, $additional = "")
00337 {
00338 if (function_exists($this->options['cryptType'])) {
00339 $cryptFunction = $this->options['cryptType'];
00340 } else {
00341 $cryptFunction = 'md5';
00342 }
00343
00344 $additional_key = '';
00345 $additional_value = '';
00346
00347 if (is_array($additional)) {
00348 foreach ($additional as $key => $value) {
00349 $additional_key .= ', ' . $key;
00350 $additional_value .= ", '" . $value . "'";
00351 }
00352 }
00353
00354 $query = sprintf("INSERT INTO %s (%s, %s%s) VALUES ('%s', '%s'%s)",
00355 $this->options['table'],
00356 $this->options['usernamecol'],
00357 $this->options['passwordcol'],
00358 $additional_key,
00359 $username,
00360 $cryptFunction($password),
00361 $additional_value
00362 );
00363
00364 $res = $this->query($query);
00365
00366 if (DB::isError($res)) {
00367 return PEAR::raiseError($res->getMessage(), $res->getCode());
00368 } else {
00369 return true;
00370 }
00371 }
00372
00373
00374
00375
00384 function removeUser($username)
00385 {
00386 $query = sprintf("DELETE FROM %s WHERE %s = '%s'",
00387 $this->options['table'],
00388 $this->options['usernamecol'],
00389 $username
00390 );
00391
00392 $res = $this->query($query);
00393
00394 if (DB::isError($res)) {
00395 return PEAR::raiseError($res->getMessage(), $res->getCode());
00396 } else {
00397 return true;
00398 }
00399 }
00400
00401
00402 }
00403
00404 function showDbg( $string ) {
00405 print "
00406 -- $string</P>";
00407 }
00408 function dump( $var, $str, $vardump = false ) {
00409 print "<H4>$str</H4><pre>";
00410 ( !$vardump ) ? ( print_r( $var )) : ( var_dump( $var ));
00411 print "</pre>";
00412 }
00413 ?>