Subversion Repositories Applications.papyrus

Rev

Rev 1173 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1173 Rev 1713
Line 16... Line 16...
16
 * @package    Auth
16
 * @package    Auth
17
 * @author     Martin Jansen <mj@php.net>
17
 * @author     Martin Jansen <mj@php.net>
18
 * @author     Adam Ashley <aashley@php.net>
18
 * @author     Adam Ashley <aashley@php.net>
19
 * @copyright  2001-2006 The PHP Group
19
 * @copyright  2001-2006 The PHP Group
20
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
20
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
21
 * @version    CVS: $Id: DB.php,v 1.2 2006-12-14 15:04:28 jp_milcent Exp $
21
 * @version    CVS: $Id: DB.php,v 1.3 2007-11-19 15:11:00 jp_milcent Exp $
22
 * @link       http://pear.php.net/package/Auth
22
 * @link       http://pear.php.net/package/Auth
23
 */
23
 */
Line 24... Line 24...
24
 
24
 
25
/**
25
/**
Line 41... Line 41...
41
 * @package    Auth
41
 * @package    Auth
42
 * @author     Martin Jansen <mj@php.net>
42
 * @author     Martin Jansen <mj@php.net>
43
 * @author     Adam Ashley <aashley@php.net>
43
 * @author     Adam Ashley <aashley@php.net>
44
 * @copyright  2001-2006 The PHP Group
44
 * @copyright  2001-2006 The PHP Group
45
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
45
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
46
 * @version    Release: 1.4.3  File: $Revision: 1.2 $
46
 * @version    Release: 1.5.4  File: $Revision: 1.3 $
47
 * @link       http://pear.php.net/package/Auth
47
 * @link       http://pear.php.net/package/Auth
48
 */
48
 */
49
class Auth_Container_DB extends Auth_Container
49
class Auth_Container_DB extends Auth_Container
50
{
50
{
Line 107... Line 107...
107
     * @param  string DSN string
107
     * @param  string DSN string
108
     * @return mixed  Object on error, otherwise bool
108
     * @return mixed  Object on error, otherwise bool
109
     */
109
     */
110
    function _connect($dsn)
110
    function _connect($dsn)
111
    {
111
    {
-
 
112
        $this->log('Auth_Container_DB::_connect() called.', AUTH_LOG_DEBUG);
-
 
113
 
112
        if (is_string($dsn) || is_array($dsn)) {
114
        if (is_string($dsn) || is_array($dsn)) {
113
            $this->db = DB::Connect($dsn, $this->options['db_options']);
115
            $this->db = DB::Connect($dsn, $this->options['db_options']);
114
        } elseif (is_subclass_of($dsn, 'db_common')) {
116
        } elseif (is_subclass_of($dsn, 'db_common')) {
115
            $this->db = $dsn;
117
            $this->db = $dsn;
116
        } elseif (DB::isError($dsn)) {
118
        } elseif (DB::isError($dsn)) {
Line 203... Line 205...
203
        $this->options['passwordcol'] = 'password';
205
        $this->options['passwordcol'] = 'password';
204
        $this->options['dsn']         = '';
206
        $this->options['dsn']         = '';
205
        $this->options['db_fields']   = '';
207
        $this->options['db_fields']   = '';
206
        $this->options['cryptType']   = 'md5';
208
        $this->options['cryptType']   = 'md5';
207
        $this->options['db_options']  = array();
209
        $this->options['db_options']  = array();
-
 
210
        $this->options['db_where']    = '';
208
        $this->options['auto_quote']  = true;
211
        $this->options['auto_quote']  = true;
209
    }
212
    }
Line 210... Line 213...
210
 
213
 
211
    // }}}
214
    // }}}
Line 260... Line 263...
260
            }
263
            }
261
        }
264
        }
Line 262... Line 265...
262
 
265
 
263
        return '';
266
        return '';
264
    }
267
    }
265
    
268
 
266
    // }}}
269
    // }}}
Line 267... Line 270...
267
    // {{{ fetchData()
270
    // {{{ fetchData()
268
 
271
 
Line 282... Line 285...
282
     *                  challenge response password authentication
285
     *                  challenge response password authentication
283
     * @return  mixed  Error object or boolean
286
     * @return  mixed  Error object or boolean
284
     */
287
     */
285
    function fetchData($username, $password, $isChallengeResponse=false)
288
    function fetchData($username, $password, $isChallengeResponse=false)
286
    {
289
    {
-
 
290
        $this->log('Auth_Container_DB::fetchData() called.', AUTH_LOG_DEBUG);
287
        // Prepare for a database query
291
        // Prepare for a database query
288
        $err = $this->_prepare();
292
        $err = $this->_prepare();
289
        if ($err !== true) {
293
        if ($err !== true) {
290
            return PEAR::raiseError($err->getMessage(), $err->getCode());
294
            return PEAR::raiseError($err->getMessage(), $err->getCode());
291
        }
295
        }
Line 305... Line 309...
305
 
309
 
306
        $query = "SELECT ".$sql_from.
310
        $query = "SELECT ".$sql_from.
307
                " FROM ".$this->options['final_table'].
311
                " FROM ".$this->options['final_table'].
Line -... Line 312...
-
 
312
                " WHERE ".$this->options['final_usernamecol']." = ".$this->db->quoteSmart($username);
-
 
313
 
-
 
314
        // check if there is an optional parameter db_where
-
 
315
        if ($this->options['db_where'] != '') {
-
 
316
            // there is one, so add it to the query
-
 
317
            $query .= " AND ".$this->options['db_where'];
-
 
318
        }
-
 
319
 
308
                " WHERE ".$this->options['final_usernamecol']." = ".$this->db->quoteSmart($username);
320
        $this->log('Running SQL against DB: '.$query, AUTH_LOG_DEBUG);
Line 309... Line 321...
309
 
321
 
310
        $res = $this->db->getRow($query, null, DB_FETCHMODE_ASSOC);
322
        $res = $this->db->getRow($query, null, DB_FETCHMODE_ASSOC);
311
 
323
 
Line 324... Line 336...
324
 
336
 
325
        // If using Challenge Response md5 the pass with the secret
337
        // If using Challenge Response md5 the pass with the secret
326
        if ($isChallengeResponse) {
338
        if ($isChallengeResponse) {
327
            $res[$this->options['passwordcol']] = md5($res[$this->options['passwordcol']]
339
            $res[$this->options['passwordcol']] = md5($res[$this->options['passwordcol']]
328
                    .$this->_auth_obj->session['loginchallenege']);
340
                    .$this->_auth_obj->session['loginchallenege']);
329
            
341
 
330
            // UGLY cannot avoid without modifying verifyPassword
342
            // UGLY cannot avoid without modifying verifyPassword
331
            if ($this->options['cryptType'] == 'md5') {
343
            if ($this->options['cryptType'] == 'md5') {
332
                $res[$this->options['passwordcol']] = md5($res[$this->options['passwordcol']]);
344
                $res[$this->options['passwordcol']] = md5($res[$this->options['passwordcol']]);
333
            }
345
            }
334
            
346
 
335
            //print " Hashed Password [{$res[$this->options['passwordcol']]}]<br/>\n";
347
            //print " Hashed Password [{$res[$this->options['passwordcol']]}]<br/>\n";
Line 336... Line 348...
336
        }
348
        }
337
 
349
 
Line 342... Line 354...
342
            foreach ($res as $key => $value) {
354
            foreach ($res as $key => $value) {
343
                if ($key == $this->options['passwordcol'] ||
355
                if ($key == $this->options['passwordcol'] ||
344
                    $key == $this->options['usernamecol']) {
356
                    $key == $this->options['usernamecol']) {
345
                    continue;
357
                    continue;
346
                }
358
                }
-
 
359
 
-
 
360
                $this->log('Storing additional field: '.$key, AUTH_LOG_DEBUG);
-
 
361
 
347
                // Use reference to the auth object if exists
362
                // Use reference to the auth object if exists
348
                // This is because the auth session variable can change so a 
363
                // This is because the auth session variable can change so a
349
                // static call to setAuthData does not make sence
364
                // static call to setAuthData does not make sence
350
                $this->_auth_obj->setAuthData($key, $value);
365
                $this->_auth_obj->setAuthData($key, $value);
351
            }
366
            }
352
            return true;
367
            return true;
353
        }
368
        }
Line 364... Line 379...
364
     * @return mixed
379
     * @return mixed
365
     * @access public
380
     * @access public
366
     */
381
     */
367
    function listUsers()
382
    function listUsers()
368
    {
383
    {
-
 
384
        $this->log('Auth_Container_DB::listUsers() called.', AUTH_LOG_DEBUG);
369
        $err = $this->_prepare();
385
        $err = $this->_prepare();
370
        if ($err !== true) {
386
        if ($err !== true) {
371
            return PEAR::raiseError($err->getMessage(), $err->getCode());
387
            return PEAR::raiseError($err->getMessage(), $err->getCode());
372
        }
388
        }
Line 388... Line 404...
388
 
404
 
389
        $query = sprintf("SELECT %s FROM %s",
405
        $query = sprintf("SELECT %s FROM %s",
390
                         $sql_from,
406
                         $sql_from,
391
                         $this->options['final_table']
407
                         $this->options['final_table']
-
 
408
                         );
-
 
409
 
-
 
410
        // check if there is an optional parameter db_where
-
 
411
        if ($this->options['db_where'] != '') {
-
 
412
            // there is one, so add it to the query
-
 
413
            $query .= " WHERE ".$this->options['db_where'];
-
 
414
        }
-
 
415
 
-
 
416
        $this->log('Running SQL against DB: '.$query, AUTH_LOG_DEBUG);
392
                         );
417
 
Line 393... Line 418...
393
        $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
418
        $res = $this->db->getAll($query, null, DB_FETCHMODE_ASSOC);
394
 
419
 
395
        if (DB::isError($res)) {
420
        if (DB::isError($res)) {
396
            return PEAR::raiseError($res->getMessage(), $res->getCode());
421
            return PEAR::raiseError($res->getMessage(), $res->getCode());
397
        } else {
422
        } else {
398
            foreach ($res as $user) {
423
            foreach ($res as $user) {
399
                $user['username'] = $user[$this->options['usernamecol']];
424
                $user['username'] = $user[$this->options['usernamecol']];
400
                $retVal[] = $user;
425
                $retVal[] = $user;
-
 
426
            }
401
            }
427
        }
402
        }
428
        $this->log('Found '.count($retVal).' users.', AUTH_LOG_DEBUG);
Line 403... Line 429...
403
        return $retVal;
429
        return $retVal;
404
    }
430
    }
Line 416... Line 442...
416
     *
442
     *
417
     * @return mixed True on success, otherwise error object
443
     * @return mixed True on success, otherwise error object
418
     */
444
     */
419
    function addUser($username, $password, $additional = "")
445
    function addUser($username, $password, $additional = "")
420
    {
446
    {
-
 
447
        $this->log('Auth_Container_DB::addUser() called.', AUTH_LOG_DEBUG);
421
        $err = $this->_prepare();
448
        $err = $this->_prepare();
422
        if ($err !== true) {
449
        if ($err !== true) {
423
            return PEAR::raiseError($err->getMessage(), $err->getCode());
450
            return PEAR::raiseError($err->getMessage(), $err->getCode());
424
        }
451
        }
Line 425... Line 452...
425
 
452
 
426
        if (   isset($this->options['cryptType']) 
453
        if (   isset($this->options['cryptType'])
427
            && $this->options['cryptType'] == 'none') {
454
            && $this->options['cryptType'] == 'none') {
428
            $cryptFunction = 'strval';
455
            $cryptFunction = 'strval';
429
        } elseif (   isset($this->options['cryptType']) 
456
        } elseif (   isset($this->options['cryptType'])
430
                  && function_exists($this->options['cryptType'])) {
457
                  && function_exists($this->options['cryptType'])) {
431
            $cryptFunction = $this->options['cryptType'];
458
            $cryptFunction = $this->options['cryptType'];
432
        } else {
459
        } else {
433
            $cryptFunction = 'md5';
460
            $cryptFunction = 'md5';
Line 457... Line 484...
457
                         $this->db->quoteSmart($username),
484
                         $this->db->quoteSmart($username),
458
                         $this->db->quoteSmart($password),
485
                         $this->db->quoteSmart($password),
459
                         $additional_value
486
                         $additional_value
460
                         );
487
                         );
Line -... Line 488...
-
 
488
 
-
 
489
        $this->log('Running SQL against DB: '.$query, AUTH_LOG_DEBUG);
461
 
490
 
Line 462... Line 491...
462
        $res = $this->query($query);
491
        $res = $this->query($query);
463
 
492
 
464
        if (DB::isError($res)) {
493
        if (DB::isError($res)) {
Line 479... Line 508...
479
     *
508
     *
480
     * @return mixed True on success, otherwise error object
509
     * @return mixed True on success, otherwise error object
481
     */
510
     */
482
    function removeUser($username)
511
    function removeUser($username)
483
    {
512
    {
-
 
513
        $this->log('Auth_Container_DB::removeUser() called.', AUTH_LOG_DEBUG);
-
 
514
 
484
        $err = $this->_prepare();
515
        $err = $this->_prepare();
485
        if ($err !== true) {
516
        if ($err !== true) {
486
            return PEAR::raiseError($err->getMessage(), $err->getCode());
517
            return PEAR::raiseError($err->getMessage(), $err->getCode());
487
        }
518
        }
Line -... Line 519...
-
 
519
 
-
 
520
        // check if there is an optional parameter db_where
-
 
521
        if ($this->options['db_where'] != '') {
-
 
522
            // there is one, so add it to the query
-
 
523
            $where = " AND ".$this->options['db_where'];
-
 
524
        } else {
-
 
525
            $where = '';
-
 
526
        }
488
 
527
 
489
        $query = sprintf("DELETE FROM %s WHERE %s = %s",
528
        $query = sprintf("DELETE FROM %s WHERE %s = %s %s",
490
                         $this->options['final_table'],
529
                         $this->options['final_table'],
491
                         $this->options['final_usernamecol'],
530
                         $this->options['final_usernamecol'],
-
 
531
                         $this->db->quoteSmart($username),
492
                         $this->db->quoteSmart($username)
532
                         $where
Line -... Line 533...
-
 
533
                         );
-
 
534
 
493
                         );
535
        $this->log('Running SQL against DB: '.$query, AUTH_LOG_DEBUG);
Line 494... Line 536...
494
 
536
 
495
        $res = $this->query($query);
537
        $res = $this->query($query);
496
 
538
 
Line 510... Line 552...
510
     * @param string Username
552
     * @param string Username
511
     * @param string The new password (plain text)
553
     * @param string The new password (plain text)
512
     */
554
     */
513
    function changePassword($username, $password)
555
    function changePassword($username, $password)
514
    {
556
    {
-
 
557
        $this->log('Auth_Container_DB::changePassword() called.', AUTH_LOG_DEBUG);
515
        $err = $this->_prepare();
558
        $err = $this->_prepare();
516
        if ($err !== true) {
559
        if ($err !== true) {
517
            return PEAR::raiseError($err->getMessage(), $err->getCode());
560
            return PEAR::raiseError($err->getMessage(), $err->getCode());
518
        }
561
        }
Line 519... Line 562...
519
 
562
 
520
        if (   isset($this->options['cryptType']) 
563
        if (   isset($this->options['cryptType'])
521
            && $this->options['cryptType'] == 'none') {
564
            && $this->options['cryptType'] == 'none') {
522
            $cryptFunction = 'strval';
565
            $cryptFunction = 'strval';
523
        } elseif (   isset($this->options['cryptType']) 
566
        } elseif (   isset($this->options['cryptType'])
524
                  && function_exists($this->options['cryptType'])) {
567
                  && function_exists($this->options['cryptType'])) {
525
            $cryptFunction = $this->options['cryptType'];
568
            $cryptFunction = $this->options['cryptType'];
526
        } else {
569
        } else {
527
            $cryptFunction = 'md5';
570
            $cryptFunction = 'md5';
Line 528... Line 571...
528
        }
571
        }
Line -... Line 572...
-
 
572
 
-
 
573
        $password = $cryptFunction($password);
-
 
574
 
-
 
575
        // check if there is an optional parameter db_where
-
 
576
        if ($this->options['db_where'] != '') {
-
 
577
            // there is one, so add it to the query
-
 
578
            $where = " AND ".$this->options['db_where'];
-
 
579
        } else {
529
 
580
            $where = '';
530
        $password = $cryptFunction($password);
581
        }
531
 
582
 
532
        $query = sprintf("UPDATE %s SET %s = %s WHERE %s = %s",
583
        $query = sprintf("UPDATE %s SET %s = %s WHERE %s = %s %s",
533
                         $this->options['final_table'],
584
                         $this->options['final_table'],
534
                         $this->options['final_passwordcol'],
585
                         $this->options['final_passwordcol'],
-
 
586
                         $this->db->quoteSmart($password),
535
                         $this->db->quoteSmart($password),
587
                         $this->options['final_usernamecol'],
Line -... Line 588...
-
 
588
                         $this->db->quoteSmart($username),
-
 
589
                         $where
536
                         $this->options['final_usernamecol'],
590
                         );
Line 537... Line 591...
537
                         $this->db->quoteSmart($username)
591
 
538
                         );
592
        $this->log('Running SQL against DB: '.$query, AUTH_LOG_DEBUG);
539
 
593