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 12... Line 12...
12
 * the PHP License and are unable to obtain it through the web, please
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
14
 *
15
 * @category   Authentication
15
 * @category   Authentication
16
 * @package    Auth
16
 * @package    Auth
17
 * @author     Lorenzo Alberton <l.alberton@quipo.it> 
17
 * @author     Lorenzo Alberton <l.alberton@quipo.it>
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: MDB.php,v 1.2 2006-12-14 15:04:28 jp_milcent Exp $
21
 * @version    CVS: $Id: MDB.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
 * @since      File available since Release 1.2.3
23
 * @since      File available since Release 1.2.3
24
 */
24
 */
Line 25... Line 25...
25
 
25
 
Line 42... Line 42...
42
 * @package    Auth
42
 * @package    Auth
43
 * @author     Lorenzo Alberton <l.alberton@quipo.it>
43
 * @author     Lorenzo Alberton <l.alberton@quipo.it>
44
 * @author     Adam Ashley <aashley@php.net>
44
 * @author     Adam Ashley <aashley@php.net>
45
 * @copyright  2001-2006 The PHP Group
45
 * @copyright  2001-2006 The PHP Group
46
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
46
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
47
 * @version    Release: 1.4.3  File: $Revision: 1.2 $
47
 * @version    Release: 1.5.4  File: $Revision: 1.3 $
48
 * @link       http://pear.php.net/package/Auth
48
 * @link       http://pear.php.net/package/Auth
49
 * @since      Class available since Release 1.2.3
49
 * @since      Class available since Release 1.2.3
50
 */
50
 */
51
class Auth_Container_MDB extends Auth_Container
51
class Auth_Container_MDB extends Auth_Container
52
{
52
{
Line 107... Line 107...
107
     * @param  mixed DSN string | array | mdb object
107
     * @param  mixed DSN string | array | mdb object
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_MDB::_connect() called.', AUTH_LOG_DEBUG);
112
        if (is_string($dsn) || is_array($dsn)) {
113
        if (is_string($dsn) || is_array($dsn)) {
113
            $this->db =& MDB::connect($dsn, $this->options['db_options']);
114
            $this->db =& MDB::connect($dsn, $this->options['db_options']);
114
        } elseif (is_subclass_of($dsn, 'mdb_common')) {
115
        } elseif (is_subclass_of($dsn, 'mdb_common')) {
115
            $this->db = $dsn;
116
            $this->db = $dsn;
116
        } elseif (is_object($dsn) && MDB::isError($dsn)) {
117
        } elseif (is_object($dsn) && MDB::isError($dsn)) {
Line 177... Line 178...
177
     * @return mixed  a MDB_result object or MDB_OK on success, a MDB
178
     * @return mixed  a MDB_result object or MDB_OK on success, a MDB
178
     *                or PEAR error on failure
179
     *                or PEAR error on failure
179
     */
180
     */
180
    function query($query)
181
    function query($query)
181
    {
182
    {
-
 
183
        $this->log('Auth_Container_MDB::query() called.', AUTH_LOG_DEBUG);
182
        $err = $this->_prepare();
184
        $err = $this->_prepare();
183
        if ($err !== true) {
185
        if ($err !== true) {
184
            return $err;
186
            return $err;
185
        }
187
        }
186
        return $this->db->query($query);
188
        return $this->db->query($query);
Line 202... Line 204...
202
        $this->options['passwordcol'] = 'password';
204
        $this->options['passwordcol'] = 'password';
203
        $this->options['dsn']         = '';
205
        $this->options['dsn']         = '';
204
        $this->options['db_fields']   = '';
206
        $this->options['db_fields']   = '';
205
        $this->options['cryptType']   = 'md5';
207
        $this->options['cryptType']   = 'md5';
206
        $this->options['db_options']  = array();
208
        $this->options['db_options']  = array();
-
 
209
        $this->options['db_where']    = '';
207
        $this->options['auto_quote']  = true;
210
        $this->options['auto_quote']  = true;
208
    }
211
    }
Line 209... Line 212...
209
 
212
 
210
    // }}}
213
    // }}}
Line 259... Line 262...
259
            }
262
            }
260
        }
263
        }
Line 261... Line 264...
261
 
264
 
262
        return '';
265
        return '';
263
    }
266
    }
264
    
267
 
265
    // }}}
268
    // }}}
Line 266... Line 269...
266
    // {{{ fetchData()
269
    // {{{ fetchData()
267
 
270
 
Line 281... Line 284...
281
     *                  challenge response password authentication
284
     *                  challenge response password authentication
282
     * @return  mixed  Error object or boolean
285
     * @return  mixed  Error object or boolean
283
     */
286
     */
284
    function fetchData($username, $password, $isChallengeResponse=false)
287
    function fetchData($username, $password, $isChallengeResponse=false)
285
    {
288
    {
-
 
289
        $this->log('Auth_Container_MDB::fetchData() called.', AUTH_LOG_DEBUG);
286
        // Prepare for a database query
290
        // Prepare for a database query
287
        $err = $this->_prepare();
291
        $err = $this->_prepare();
288
        if ($err !== true) {
292
        if ($err !== true) {
289
            return PEAR::raiseError($err->getMessage(), $err->getCode());
293
            return PEAR::raiseError($err->getMessage(), $err->getCode());
290
        }
294
        }
Line 307... Line 311...
307
                         $this->options['final_table'],
311
                         $this->options['final_table'],
308
                         $this->options['final_usernamecol'],
312
                         $this->options['final_usernamecol'],
309
                         $this->db->getTextValue($username)
313
                         $this->db->getTextValue($username)
310
                         );
314
                         );
Line -... Line 315...
-
 
315
 
-
 
316
        // check if there is an optional parameter db_where
-
 
317
        if ($this->options['db_where'] != '') {
-
 
318
            // there is one, so add it to the query
-
 
319
            $query .= " AND ".$this->options['db_where'];
-
 
320
        }
-
 
321
 
-
 
322
        $this->log('Running SQL against MDB: '.$query, AUTH_LOG_DEBUG);
311
 
323
 
Line 312... Line 324...
312
        $res = $this->db->getRow($query, null, null, null, MDB_FETCHMODE_ASSOC);
324
        $res = $this->db->getRow($query, null, null, null, MDB_FETCHMODE_ASSOC);
313
 
325
 
314
        if (MDB::isError($res) || PEAR::isError($res)) {
326
        if (MDB::isError($res) || PEAR::isError($res)) {
Line 320... Line 332...
320
        }
332
        }
Line 321... Line 333...
321
 
333
 
322
        // Perform trimming here before the hashing
334
        // Perform trimming here before the hashing
323
        $password = trim($password, "\r\n");
335
        $password = trim($password, "\r\n");
324
        $res[$this->options['passwordcol']] = trim($res[$this->options['passwordcol']], "\r\n");
336
        $res[$this->options['passwordcol']] = trim($res[$this->options['passwordcol']], "\r\n");
325
        
337
 
326
        // If using Challenge Response md5 the pass with the secret
338
        // If using Challenge Response md5 the pass with the secret
327
        if ($isChallengeResponse) {
339
        if ($isChallengeResponse) {
328
            $res[$this->options['passwordcol']] =
340
            $res[$this->options['passwordcol']] =
329
                md5($res[$this->options['passwordcol']].$this->_auth_obj->session['loginchallenege']);
341
                md5($res[$this->options['passwordcol']].$this->_auth_obj->session['loginchallenege']);
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
        
347
 
336
        if ($this->verifyPassword($password,
348
        if ($this->verifyPassword($password,
337
                                  $res[$this->options['passwordcol']],
349
                                  $res[$this->options['passwordcol']],
338
                                  $this->options['cryptType'])) {
350
                                  $this->options['cryptType'])) {
339
            // Store additional field values in the session
351
            // Store additional field values in the session
340
            foreach ($res as $key => $value) {
352
            foreach ($res as $key => $value) {
341
                if ($key == $this->options['passwordcol'] ||
353
                if ($key == $this->options['passwordcol'] ||
342
                    $key == $this->options['usernamecol']) {
354
                    $key == $this->options['usernamecol']) {
343
                    continue;
355
                    continue;
-
 
356
                }
-
 
357
 
344
                }
358
                $this->log('Storing additional field: '.$key, AUTH_LOG_DEBUG);
345
                // Use reference to the auth object if exists
359
                // Use reference to the auth object if exists
346
                // This is because the auth session variable can change so a static
360
                // This is because the auth session variable can change so a static
347
                // call to setAuthData does not make sense
361
                // call to setAuthData does not make sense
348
                $this->_auth_obj->setAuthData($key, $value);
362
                $this->_auth_obj->setAuthData($key, $value);
Line 363... Line 377...
363
     * @return mixed array|PEAR_Error
377
     * @return mixed array|PEAR_Error
364
     * @access public
378
     * @access public
365
     */
379
     */
366
    function listUsers()
380
    function listUsers()
367
    {
381
    {
-
 
382
        $this->log('Auth_Container_MDB::listUsers() called.', AUTH_LOG_DEBUG);
368
        $err = $this->_prepare();
383
        $err = $this->_prepare();
369
        if ($err !== true) {
384
        if ($err !== true) {
370
            return PEAR::raiseError($err->getMessage(), $err->getCode());
385
            return PEAR::raiseError($err->getMessage(), $err->getCode());
371
        }
386
        }
Line 377... Line 392...
377
            && strstr($this->options['db_fields'], '*')) {
392
            && strstr($this->options['db_fields'], '*')) {
378
            $sql_from = '*';
393
            $sql_from = '*';
379
        } else {
394
        } else {
380
            $sql_from = $this->options['final_usernamecol']
395
            $sql_from = $this->options['final_usernamecol']
381
                .', '.$this->options['final_passwordcol'];
396
                .', '.$this->options['final_passwordcol'];
382
            
397
 
383
            if (strlen($fields = $this->_quoteDBFields()) > 0) {
398
            if (strlen($fields = $this->_quoteDBFields()) > 0) {
384
                $sql_from .= ', '.$fields;
399
                $sql_from .= ', '.$fields;
385
            }
400
            }
386
        }
401
        }
Line 387... Line 402...
387
 
402
 
388
        $query = sprintf('SELECT %s FROM %s',
403
        $query = sprintf('SELECT %s FROM %s',
389
                         $sql_from,
404
                         $sql_from,
390
                         $this->options['final_table']
405
                         $this->options['final_table']
Line -... Line 406...
-
 
406
                         );
-
 
407
 
-
 
408
        // check if there is an optional parameter db_where
-
 
409
        if ($this->options['db_where'] != '') {
-
 
410
            // there is one, so add it to the query
-
 
411
            $query .= " WHERE ".$this->options['db_where'];
-
 
412
        }
-
 
413
 
391
                         );
414
        $this->log('Running SQL against MDB: '.$query, AUTH_LOG_DEBUG);
Line 392... Line 415...
392
 
415
 
393
        $res = $this->db->getAll($query, null, null, null, MDB_FETCHMODE_ASSOC);
416
        $res = $this->db->getAll($query, null, null, null, MDB_FETCHMODE_ASSOC);
394
 
417
 
395
        if (MDB::isError($res)) {
418
        if (MDB::isError($res)) {
396
            return PEAR::raiseError($res->getMessage(), $res->getCode());
419
            return PEAR::raiseError($res->getMessage(), $res->getCode());
397
        } else {
420
        } else {
398
            foreach ($res as $user) {
421
            foreach ($res as $user) {
399
                $user['username'] = $user[$this->options['usernamecol']];
422
                $user['username'] = $user[$this->options['usernamecol']];
-
 
423
                $retVal[] = $user;
400
                $retVal[] = $user;
424
            }
401
            }
425
        }
Line 402... Line 426...
402
        }
426
        $this->log('Found '.count($retVal).' users.', AUTH_LOG_DEBUG);
403
        return $retVal;
427
        return $retVal;
Line 416... Line 440...
416
     *
440
     *
417
     * @return mixed True on success, otherwise error object
441
     * @return mixed True on success, otherwise error object
418
     */
442
     */
419
    function addUser($username, $password, $additional = "")
443
    function addUser($username, $password, $additional = "")
420
    {
444
    {
-
 
445
        $this->log('Auth_Container_MDB::addUser() called.', AUTH_LOG_DEBUG);
421
        $err = $this->_prepare();
446
        $err = $this->_prepare();
422
        if ($err !== true) {
447
        if ($err !== true) {
423
            return PEAR::raiseError($err->getMessage(), $err->getCode());
448
            return PEAR::raiseError($err->getMessage(), $err->getCode());
424
        }
449
        }
Line 455... Line 480...
455
                         $this->db->getTextValue($username),
480
                         $this->db->getTextValue($username),
456
                         $this->db->getTextValue($password),
481
                         $this->db->getTextValue($password),
457
                         $additional_value
482
                         $additional_value
458
                         );
483
                         );
Line -... Line 484...
-
 
484
 
-
 
485
        $this->log('Running SQL against MDB: '.$query, AUTH_LOG_DEBUG);
459
 
486
 
Line 460... Line 487...
460
        $res = $this->query($query);
487
        $res = $this->query($query);
461
 
488
 
462
        if (MDB::isError($res)) {
489
        if (MDB::isError($res)) {
Line 476... Line 503...
476
     *
503
     *
477
     * @return mixed True on success, otherwise error object
504
     * @return mixed True on success, otherwise error object
478
     */
505
     */
479
    function removeUser($username)
506
    function removeUser($username)
480
    {
507
    {
-
 
508
        $this->log('Auth_Container_MDB::removeUser() called.', AUTH_LOG_DEBUG);
481
        $err = $this->_prepare();
509
        $err = $this->_prepare();
482
        if ($err !== true) {
510
        if ($err !== true) {
483
            return PEAR::raiseError($err->getMessage(), $err->getCode());
511
            return PEAR::raiseError($err->getMessage(), $err->getCode());
484
        }
512
        }
Line 487... Line 515...
487
                         $this->options['final_table'],
515
                         $this->options['final_table'],
488
                         $this->options['final_usernamecol'],
516
                         $this->options['final_usernamecol'],
489
                         $this->db->getTextValue($username)
517
                         $this->db->getTextValue($username)
490
                         );
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
            $query .= " AND ".$this->options['db_where'];
-
 
524
        }
-
 
525
 
-
 
526
        $this->log('Running SQL against MDB: '.$query, AUTH_LOG_DEBUG);
491
 
527
 
Line 492... Line 528...
492
        $res = $this->query($query);
528
        $res = $this->query($query);
493
 
529
 
494
        if (MDB::isError($res)) {
530
        if (MDB::isError($res)) {
Line 506... Line 542...
506
     * @param string Username
542
     * @param string Username
507
     * @param string The new password (plain text)
543
     * @param string The new password (plain text)
508
     */
544
     */
509
    function changePassword($username, $password)
545
    function changePassword($username, $password)
510
    {
546
    {
-
 
547
        $this->log('Auth_Container_MDB::changePassword() called.', AUTH_LOG_DEBUG);
511
        $err = $this->_prepare();
548
        $err = $this->_prepare();
512
        if ($err !== true) {
549
        if ($err !== true) {
513
            return PEAR::raiseError($err->getMessage(), $err->getCode());
550
            return PEAR::raiseError($err->getMessage(), $err->getCode());
514
        }
551
        }
Line 529... Line 566...
529
                         $this->db->getTextValue($password),
566
                         $this->db->getTextValue($password),
530
                         $this->options['final_usernamecol'],
567
                         $this->options['final_usernamecol'],
531
                         $this->db->getTextValue($username)
568
                         $this->db->getTextValue($username)
532
                         );
569
                         );
Line -... Line 570...
-
 
570
 
-
 
571
        // check if there is an optional parameter db_where
-
 
572
        if ($this->options['db_where'] != '') {
-
 
573
            // there is one, so add it to the query
-
 
574
            $query .= " AND ".$this->options['db_where'];
-
 
575
        }
-
 
576
 
-
 
577
        $this->log('Running SQL against MDB: '.$query, AUTH_LOG_DEBUG);
533
 
578
 
Line 534... Line 579...
534
        $res = $this->query($query);
579
        $res = $this->query($query);
535
 
580
 
536
        if (MDB::isError($res)) {
581
        if (MDB::isError($res)) {