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: Auth.php,v 1.2 2006-12-14 15:04:29 jp_milcent Exp $
21
 * @version    CVS: $Id: Auth.php,v 1.3 2007-11-19 15:10:59 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 28... Line 28...
28
define('AUTH_IDLED',                    -1);
28
define('AUTH_IDLED',                    -1);
29
/**
29
/**
30
 * Returned if session has expired
30
 * Returned if session has expired
31
 */
31
 */
32
define('AUTH_EXPIRED',                  -2);
32
define('AUTH_EXPIRED',                  -2);
33
/** 
33
/**
34
 * Returned if container is unable to authenticate user/password pair
34
 * Returned if container is unable to authenticate user/password pair
35
 */
35
 */
36
define('AUTH_WRONG_LOGIN',              -3);
36
define('AUTH_WRONG_LOGIN',              -3);
37
/**
37
/**
38
 * Returned if a container method is not supported.
38
 * Returned if a container method is not supported.
Line 46... Line 46...
46
 * Returned if checkAuthCallback says session should not continue.
46
 * Returned if checkAuthCallback says session should not continue.
47
 */
47
 */
48
define('AUTH_CALLBACK_ABORT',           -6);
48
define('AUTH_CALLBACK_ABORT',           -6);
Line 49... Line 49...
49
 
49
 
-
 
50
/**
-
 
51
 * Auth Log level - INFO
-
 
52
 */
-
 
53
define('AUTH_LOG_INFO',     6);
-
 
54
/**
-
 
55
 * Auth Log level - DEBUG
-
 
56
 */
-
 
57
define('AUTH_LOG_DEBUG',    7);
-
 
58
 
-
 
59
 
50
/**
60
/**
51
 * PEAR::Auth
61
 * PEAR::Auth
52
 *
62
 *
53
 * The PEAR::Auth class provides methods for creating an
63
 * The PEAR::Auth class provides methods for creating an
54
 * authentication system using PHP.
64
 * authentication system using PHP.
Line 57... Line 67...
57
 * @package    Auth
67
 * @package    Auth
58
 * @author     Martin Jansen <mj@php.net>
68
 * @author     Martin Jansen <mj@php.net>
59
 * @author     Adam Ashley <aashley@php.net>
69
 * @author     Adam Ashley <aashley@php.net>
60
 * @copyright  2001-2006 The PHP Group
70
 * @copyright  2001-2006 The PHP Group
61
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
71
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
62
 * @version    Release: 1.4.3  File: $Revision: 1.2 $
72
 * @version    Release: 1.5.4  File: $Revision: 1.3 $
63
 * @link       http://pear.php.net/package/Auth
73
 * @link       http://pear.php.net/package/Auth
64
 */
74
 */
65
class Auth {
75
class Auth {
Line 66... Line 76...
66
 
76
 
Line 124... Line 134...
124
     *
134
     *
125
     * @var   bool
135
     * @var   bool
126
     * @see   setShowlogin()
136
     * @see   setShowlogin()
127
     */
137
     */
128
    var $showLogin = true;
138
    var $showLogin = true;
129
    
139
 
130
    /**
140
    /**
131
      * Is Login Allowed from this page
141
      * Is Login Allowed from this page
132
      *
142
      *
133
      * @var  bool
143
      * @var  bool
134
      * @see setAllowLogin
144
      * @see setAllowLogin
Line 202... Line 212...
202
     */
212
     */
203
    var $version = "@version@";
213
    var $version = "@version@";
Line 204... Line 214...
204
 
214
 
205
    /**
215
    /**
206
     * Flag to use advanced security
216
     * Flag to use advanced security
207
     * When set extra checks will be made to see if the 
217
     * When set extra checks will be made to see if the
208
     * user's IP or useragent have changed across requests. 
218
     * user's IP or useragent have changed across requests.
209
     * Turned off by default to preserve BC.
219
     * Turned off by default to preserve BC.
210
     *
220
     *
211
     * @var boolean
221
     * @var boolean
212
     */     
222
     */
Line 213... Line 223...
213
    var $advancedsecurity = false;
223
    var $advancedsecurity = false;
214
 
224
 
215
    /**
225
    /**
Line 253... Line 263...
253
    /**
263
    /**
254
     * A hash to hold various superglobals as reference
264
     * A hash to hold various superglobals as reference
255
     * @var array
265
     * @var array
256
     */
266
     */
257
    var $authdata;
267
    var $authdata;
258
    
268
 
259
    /**
269
    /**
260
      * How many times has checkAuth been called
270
      * How many times has checkAuth been called
261
      * var int
271
      * @var int
262
      */
272
      */
263
    var $authChecks = 0;
273
    var $authChecks = 0;
Line -... Line 274...
-
 
274
 
-
 
275
    /**
-
 
276
     * PEAR::Log object
-
 
277
     *
-
 
278
     * @var object Log
-
 
279
     */
-
 
280
    var $logger = null;
-
 
281
 
-
 
282
    /**
-
 
283
     * Whether to enable logging of behaviour
-
 
284
     *
-
 
285
     * @var boolean
-
 
286
     */
-
 
287
    var $enableLogging = false;
-
 
288
 
-
 
289
    /**
-
 
290
     * Whether to regenerate session id everytime start is called
-
 
291
     *
-
 
292
     * @var boolean
-
 
293
     */
-
 
294
    var $regenerateSessionId = false;
264
 
295
 
265
    // }}}
296
    // }}}
Line 266... Line 297...
266
    // {{{ Auth() [constructor]
297
    // {{{ Auth() [constructor]
267
 
298
 
Line 319... Line 350...
319
            // Pass a reference to auth to the container, ugly but works
350
            // Pass a reference to auth to the container, ugly but works
320
            // this is used by the DB container to use method setAuthData not staticaly.
351
            // this is used by the DB container to use method setAuthData not staticaly.
321
            $this->storage->_auth_obj =& $this;
352
            $this->storage->_auth_obj =& $this;
322
        } else {
353
        } else {
323
            // $this->storage = $this->_factory($storageDriver, $options);
354
            // $this->storage = $this->_factory($storageDriver, $options);
324
            // 
355
            //
325
            $this->storage_driver = $storageDriver;
356
            $this->storage_driver = $storageDriver;
326
            $this->storage_options =& $options;
357
            $this->storage_options =& $options;
327
        }
358
        }
328
    }
359
    }
Line 329... Line 360...
329
 
360
 
330
    // }}}
361
    // }}}
Line 331... Line 362...
331
    // {{{ applyAuthOptions()
362
    // {{{ applyAuthOptions()
332
 
363
 
333
    /**
364
    /**
334
      * Set the Auth options 
365
      * Set the Auth options
335
      *
366
      *
336
      * Some options which are Auth specific will be applied
367
      * Some options which are Auth specific will be applied
337
      * the rest will be left for usage by the container
368
      * the rest will be left for usage by the container
338
      * 
369
      *
339
      * @param array    An array of Auth options
370
      * @param array    An array of Auth options
340
      * @return array   The options which were not applied
371
      * @return array   The options which were not applied
341
      * @access private
372
      * @access private
Line 361... Line 392...
361
            }
392
            }
362
            if (isset($options['advancedsecurity'])) {
393
            if (isset($options['advancedsecurity'])) {
363
                $this->advancedsecurity = $options['advancedsecurity'];
394
                $this->advancedsecurity = $options['advancedsecurity'];
364
                unset($options['advancedsecurity']);
395
                unset($options['advancedsecurity']);
365
            }
396
            }
-
 
397
            if (isset($options['enableLogging'])) {
-
 
398
                $this->enableLogging = $options['enableLogging'];
-
 
399
                unset($options['enableLogging']);
-
 
400
            }
-
 
401
            if (isset($options['regenerateSessionId']) && is_bool($options['regenerateSessionId'])) {
-
 
402
                $this->regenerateSessionId = $options['regenerateSessionId'];
-
 
403
            }
366
        }
404
        }
367
        return($options);
405
        return($options);
368
    }
406
    }
Line 369... Line 407...
369
 
407
 
370
    // }}}
408
    // }}}
371
    // {{{ _loadStorage()
409
    // {{{ _loadStorage()
372
    
410
 
373
    /**
411
    /**
374
      * Load Storage Driver if not already loaded
412
      * Load Storage Driver if not already loaded
375
      *
413
      *
376
      * Suspend storage instantiation to make Auth lighter to use 
414
      * Suspend storage instantiation to make Auth lighter to use
377
      * for calls which do not require login
415
      * for calls which do not require login
378
      *
416
      *
379
      * @return bool    True if the conainer is loaded, false if the container
417
      * @return bool    True if the conainer is loaded, false if the container
380
      *                 is already loaded
418
      *                 is already loaded
381
      * @access private
419
      * @access private
382
      */
420
      */
383
    function _loadStorage()
421
    function _loadStorage()
384
    {
422
    {
385
        if(!is_object($this->storage)) {
423
        if(!is_object($this->storage)) {
386
            $this->storage =& $this->_factory($this->storage_driver, 
424
            $this->storage =& $this->_factory($this->storage_driver,
387
                    $this->storage_options);
425
                    $this->storage_options);
-
 
426
            $this->storage->_auth_obj =& $this;
388
            $this->storage->_auth_obj =& $this;
427
            $this->log('Loaded storage container ('.$this->storage_driver.')', AUTH_LOG_DEBUG);
389
            return(true);
428
            return(true);
390
        }
429
        }
391
        return(false);
430
        return(false);
Line 427... Line 466...
427
     * @return void
466
     * @return void
428
     * @access private
467
     * @access private
429
     */
468
     */
430
    function assignData()
469
    function assignData()
431
    {
470
    {
-
 
471
        $this->log('Auth::assignData() called.', AUTH_LOG_DEBUG);
-
 
472
 
432
        if (   isset($this->post[$this->_postUsername]) 
473
        if (   isset($this->post[$this->_postUsername])
433
            && $this->post[$this->_postUsername] != '') {
474
            && $this->post[$this->_postUsername] != '') {
434
            $this->username = (get_magic_quotes_gpc() == 1 
475
            $this->username = (get_magic_quotes_gpc() == 1
435
                    ? stripslashes($this->post[$this->_postUsername]) 
476
                    ? stripslashes($this->post[$this->_postUsername])
436
                    : $this->post[$this->_postUsername]);
477
                    : $this->post[$this->_postUsername]);
437
        }
478
        }
438
        if (   isset($this->post[$this->_postPassword]) 
479
        if (   isset($this->post[$this->_postPassword])
439
            && $this->post[$this->_postPassword] != '') {
480
            && $this->post[$this->_postPassword] != '') {
440
            $this->password = (get_magic_quotes_gpc() == 1 
481
            $this->password = (get_magic_quotes_gpc() == 1
441
                    ? stripslashes($this->post[$this->_postPassword]) 
482
                    ? stripslashes($this->post[$this->_postPassword])
442
                    : $this->post[$this->_postPassword] );
483
                    : $this->post[$this->_postPassword] );
443
        }
484
        }
444
    }
485
    }
Line 445... Line 486...
445
 
486
 
Line 452... Line 493...
452
     * @return void
493
     * @return void
453
     * @access public
494
     * @access public
454
     */
495
     */
455
    function start()
496
    function start()
456
    {
497
    {
-
 
498
        $this->log('Auth::start() called.', AUTH_LOG_DEBUG);
-
 
499
 
-
 
500
        // #10729 - Regenerate session id here if we are generating it on every
-
 
501
        //          page load.
-
 
502
        if ($this->regenerateSessionId) {
-
 
503
            session_regenerate_id(true);
-
 
504
        }
-
 
505
 
457
        $this->assignData();
506
        $this->assignData();
458
        if (!$this->checkAuth() && $this->allowLogin) {
507
        if (!$this->checkAuth() && $this->allowLogin) {
459
            $this->login();
508
            $this->login();
460
        }
509
        }
461
    }
510
    }
Line 469... Line 518...
469
     * @return void
518
     * @return void
470
     * @access private
519
     * @access private
471
     */
520
     */
472
    function login()
521
    function login()
473
    {
522
    {
-
 
523
        $this->log('Auth::login() called.', AUTH_LOG_DEBUG);
-
 
524
 
474
        $login_ok = false;
525
        $login_ok = false;
475
        $this->_loadStorage();
526
        $this->_loadStorage();
476
        
527
 
477
        // Check if using challenge response
528
        // Check if using challenge response
478
        (isset($this->post['authsecret']) && $this->post['authsecret'] == 1) 
529
        (isset($this->post['authsecret']) && $this->post['authsecret'] == 1)
479
            ? $usingChap = true 
530
            ? $usingChap = true
480
            : $usingChap = false;
531
            : $usingChap = false;
Line 481... Line 532...
481
 
532
 
482
        
533
 
483
        // When the user has already entered a username, we have to validate it.
534
        // When the user has already entered a username, we have to validate it.
484
        if (!empty($this->username)) {
535
        if (!empty($this->username)) {
485
            if (true === $this->storage->fetchData($this->username, $this->password, $usingChap)) {
536
            if (true === $this->storage->fetchData($this->username, $this->password, $usingChap)) {
486
                $this->session['challengekey'] = md5($this->username.$this->password);
537
                $this->session['challengekey'] = md5($this->username.$this->password);
-
 
538
                $login_ok = true;
487
                $login_ok = true;
539
                $this->log('Successful login.', AUTH_LOG_INFO);
488
            }
540
            }
Line 489... Line 541...
489
        }
541
        }
490
 
542
 
491
        if (!empty($this->username) && $login_ok) {
543
        if (!empty($this->username) && $login_ok) {
-
 
544
            $this->setAuth($this->username);
492
            $this->setAuth($this->username);
545
            if (is_callable($this->loginCallback)) {
493
            if (is_callable($this->loginCallback)) {
546
                $this->log('Calling loginCallback ('.$this->loginCallback.').', AUTH_LOG_DEBUG);
494
                call_user_func_array($this->loginCallback, array($this->username, &$this));
547
                call_user_func_array($this->loginCallback, array($this->username, &$this));
Line 495... Line 548...
495
            }
548
            }
496
        }
549
        }
497
 
550
 
-
 
551
        // If the login failed or the user entered no username,
498
        // If the login failed or the user entered no username, 
552
        // output the login screen again.
499
        // output the login screen again.
553
        if (!empty($this->username) && !$login_ok) {
-
 
554
            $this->log('Incorrect login.', AUTH_LOG_INFO);
500
        if (!empty($this->username) && !$login_ok) {
555
            $this->status = AUTH_WRONG_LOGIN;
501
            $this->status = AUTH_WRONG_LOGIN;
556
            if (is_callable($this->loginFailedCallback)) {
502
            if (is_callable($this->loginFailedCallback)) {
557
                $this->log('Calling loginFailedCallback ('.$this->loginFailedCallback.').', AUTH_LOG_DEBUG);
Line 503... Line 558...
503
                call_user_func_array($this->loginFailedCallback, array($this->username, &$this));
558
                call_user_func_array($this->loginFailedCallback, array($this->username, &$this));
-
 
559
            }
504
            }
560
        }
-
 
561
 
505
        }
562
        if ((empty($this->username) || !$login_ok) && $this->showLogin) {
506
 
563
            $this->log('Rendering Login Form.', AUTH_LOG_INFO);
507
        if ((empty($this->username) || !$login_ok) && $this->showLogin) {
564
            if (is_callable($this->loginFunction)) {
508
            if (is_callable($this->loginFunction)) {
565
                $this->log('Calling loginFunction ('.$this->loginFunction.').', AUTH_LOG_DEBUG);
509
                call_user_func_array($this->loginFunction, array($this->username, $this->status, &$this));
566
                call_user_func_array($this->loginFunction, array($this->username, $this->status, &$this));
-
 
567
            } else {
510
            } else {
568
                // BC fix Auth used to use drawLogin for this
511
                // BC fix Auth used to use drawLogin for this
569
                // call is sub classes implement this
Line -... Line 570...
-
 
570
                if (is_callable(array($this, 'drawLogin'))) {
-
 
571
                    $this->log('Calling Auth::drawLogin()', AUTH_LOG_DEBUG);
512
                // call is sub classes implement this
572
                    return $this->drawLogin($this->username, $this);
513
                if (is_callable(array($this, 'drawLogin'))) {
573
                }
514
                    return $this->drawLogin($this->username, $this);
574
 
515
                }
575
                $this->log('Using default Auth_Frontend_Html', AUTH_LOG_DEBUG);
516
 
576
 
Line 562... Line 622...
562
     * Set name of the session to a customized value.
622
     * Set name of the session to a customized value.
563
     *
623
     *
564
     * If you are using multiple instances of PEAR::Auth
624
     * If you are using multiple instances of PEAR::Auth
565
     * on the same domain, you can change the name of
625
     * on the same domain, you can change the name of
566
     * session per application via this function.
626
     * session per application via this function.
567
     * This will chnage the name of the session variable 
627
     * This will chnage the name of the session variable
568
     * auth uses to store it's data in the session
628
     * auth uses to store it's data in the session
569
     *
629
     *
570
     * @param  string New name for the session
630
     * @param  string New name for the session
571
     * @return void
631
     * @return void
572
     * @access public
632
     * @access public
573
     */
633
     */
574
    function setSessionName($name = 'session')
634
    function setSessionName($name = 'session')
575
    {
635
    {
576
        $this->_sessionName = '_auth_'.$name;
636
        $this->_sessionName = '_auth_'.$name;
-
 
637
        // Make Sure Auth session variable is there
-
 
638
        if(!isset($_SESSION[$this->_sessionName])) {
-
 
639
            $_SESSION[$this->_sessionName] = array();
-
 
640
        }
577
        $this->session =& $_SESSION[$this->_sessionName];
641
        $this->session =& $_SESSION[$this->_sessionName];
578
    }
642
    }
Line 579... Line 643...
579
 
643
 
580
    // }}}
644
    // }}}
Line 624... Line 688...
624
        $this->checkAuthCallback = $checkAuthCallback;
688
        $this->checkAuthCallback = $checkAuthCallback;
625
    }
689
    }
Line 626... Line 690...
626
 
690
 
627
    // }}}
691
    // }}}
628
    // {{{ setLoginCallback()
692
    // {{{ setLoginCallback()
629
    
693
 
630
    /**
694
    /**
631
     * Register a callback function to be called on user login.
695
     * Register a callback function to be called on user login.
632
     * The function will receive two parameters, the username and a reference to the auth object.
696
     * The function will receive two parameters, the username and a reference to the auth object.
633
     *
697
     *
Line 711... Line 775...
711
     */
775
     */
712
    function getAuthData($name = null)
776
    function getAuthData($name = null)
713
    {
777
    {
714
        if (!isset($this->session['data'])) {
778
        if (!isset($this->session['data'])) {
715
            return null;
779
            return null;
716
        }    
780
        }
717
        if(!isset($name)) {
781
        if(!isset($name)) {
718
            return $this->session['data'];
782
            return $this->session['data'];
719
        }
783
        }
720
        if (isset($name) && isset($this->session['data'][$name])) {
784
        if (isset($name) && isset($this->session['data'][$name])) {
721
            return $this->session['data'][$name];
785
            return $this->session['data'][$name];
722
        }
786
        }
723
        return null;        
787
        return null;
724
    }
788
    }
Line 725... Line 789...
725
 
789
 
726
    // }}}
790
    // }}}
Line 734... Line 798...
734
     * @return void
798
     * @return void
735
     * @access public
799
     * @access public
736
     */
800
     */
737
    function setAuth($username)
801
    function setAuth($username)
738
    {
802
    {
-
 
803
        $this->log('Auth::setAuth() called.', AUTH_LOG_DEBUG);
739
    
804
 
-
 
805
        // #10729 - Regenerate session id here only if generating at login only
-
 
806
        //          Don't do it if we are regenerating on every request so we don't
-
 
807
        //          regenerate it twice in one request.
-
 
808
        if (!$this->regenerateSessionId) {
740
        // #2021 - Change the session id to avoid session fixation attacks php 4.3.3 > 
809
            // #2021 - Change the session id to avoid session fixation attacks php 4.3.3 >
741
        session_regenerate_id(true);
810
            session_regenerate_id(true);
-
 
811
        }
Line 742... Line 812...
742
 
812
 
743
        if (!isset($this->session) || !is_array($this->session)) {
813
        if (!isset($this->session) || !is_array($this->session)) {
744
            $this->session = array();
814
            $this->session = array();
Line 745... Line 815...
745
        }
815
        }
746
 
816
 
747
        if (!isset($this->session['data'])) {
817
        if (!isset($this->session['data'])) {
Line 748... Line 818...
748
            $this->session['data'] = array();
818
            $this->session['data'] = array();
749
        }
819
        }
-
 
820
 
-
 
821
        $this->session['sessionip'] = isset($this->server['REMOTE_ADDR'])
-
 
822
            ? $this->server['REMOTE_ADDR']
750
 
823
            : '';
751
        $this->session['sessionip'] = isset($this->server['REMOTE_ADDR']) 
824
        $this->session['sessionuseragent'] = isset($this->server['HTTP_USER_AGENT'])
752
            ? $this->server['REMOTE_ADDR'] 
825
            ? $this->server['HTTP_USER_AGENT']
753
            : '';
826
            : '';
Line 754... Line 827...
754
        $this->session['sessionuseragent'] = isset($this->server['HTTP_USER_AGENT']) 
827
        $this->session['sessionforwardedfor'] = isset($this->server['HTTP_X_FORWARDED_FOR'])
755
            ? $this->server['HTTP_USER_AGENT'] 
828
            ? $this->server['HTTP_X_FORWARDED_FOR']
756
            : '';
829
            : '';
Line 770... Line 843...
770
        $this->session['idle']       = time();
843
        $this->session['idle']       = time();
771
    }
844
    }
Line 772... Line 845...
772
 
845
 
773
    // }}}
846
    // }}}
774
    // {{{ setAdvancedSecurity()
847
    // {{{ setAdvancedSecurity()
775
    
848
 
776
    /**
849
    /**
777
      * Enables advanced security checks
850
      * Enables advanced security checks
778
      *
851
      *
779
      * Currently only ip change and useragent change 
852
      * Currently only ip change and useragent change
780
      * are detected
853
      * are detected
781
      * @todo Add challenge cookies - Create a cookie which changes every time 
854
      * @todo Add challenge cookies - Create a cookie which changes every time
782
      *       and contains some challenge key which the server can verify with
855
      *       and contains some challenge key which the server can verify with
783
      *       a session var cookie might need to be crypted (user pass)
856
      *       a session var cookie might need to be crypted (user pass)
784
      * @param bool Enable or disable
857
      * @param bool Enable or disable
785
      * @return void
858
      * @return void
Line 799... Line 872...
799
     * @access public
872
     * @access public
800
     * @return boolean  Whether or not the user is authenticated.
873
     * @return boolean  Whether or not the user is authenticated.
801
     */
874
     */
802
    function checkAuth()
875
    function checkAuth()
803
    {
876
    {
-
 
877
        $this->log('Auth::checkAuth() called.', AUTH_LOG_DEBUG);
804
        $this->authChecks++;
878
        $this->authChecks++;
805
        if (isset($this->session)) {
879
        if (isset($this->session)) {
806
            // Check if authentication session is expired
880
            // Check if authentication session is expired
807
            if (   $this->expire > 0
881
            if (   $this->expire > 0
808
                && isset($this->session['timestamp'])
882
                && isset($this->session['timestamp'])
809
                && ($this->session['timestamp'] + $this->expire) < time()) {
883
                && ($this->session['timestamp'] + $this->expire) < time()) {
-
 
884
                $this->log('Session Expired', AUTH_LOG_INFO);
810
                $this->expired = true;
885
                $this->expired = true;
811
                $this->status = AUTH_EXPIRED;
886
                $this->status = AUTH_EXPIRED;
812
                $this->logout();
887
                $this->logout();
813
                return false;
888
                return false;
814
            }
889
            }
Line 815... Line 890...
815
 
890
 
816
            // Check if maximum idle time is reached
891
            // Check if maximum idle time is reached
817
            if (   $this->idle > 0
892
            if (   $this->idle > 0
818
                && isset($this->session['idle']) 
893
                && isset($this->session['idle'])
-
 
894
                && ($this->session['idle'] + $this->idle) < time()) {
819
                && ($this->session['idle'] + $this->idle) < time()) {
895
                $this->log('Session Idle Time Reached', AUTH_LOG_INFO);
820
                $this->idled = true;
896
                $this->idled = true;
821
                $this->status = AUTH_IDLED;
897
                $this->status = AUTH_IDLED;
822
                $this->logout();
898
                $this->logout();
823
                return false;
899
                return false;
Line 824... Line 900...
824
            }
900
            }
825
 
901
 
826
            if (   isset($this->session['registered']) 
902
            if (   isset($this->session['registered'])
827
                && isset($this->session['username']) 
903
                && isset($this->session['username'])
828
                && $this->session['registered'] == true 
904
                && $this->session['registered'] == true
Line 829... Line 905...
829
                && $this->session['username'] != '') {
905
                && $this->session['username'] != '') {
-
 
906
                Auth::updateIdle();
830
                Auth::updateIdle();
907
 
831
 
908
                if ($this->advancedsecurity) {
832
                if ($this->advancedsecurity) {
909
                    $this->log('Advanced Security Mode Enabled.', AUTH_LOG_DEBUG);
-
 
910
 
833
                    
911
                    // Only Generate the challenge once
834
                    // Only Generate the challenge once
912
                    if($this->authChecks == 1) {
835
                    if($this->authChecks == 1) {
913
                        $this->log('Generating new Challenge Cookie.', AUTH_LOG_DEBUG);
836
                        $this->session['challengecookieold'] = $this->session['challengecookie'];
914
                        $this->session['challengecookieold'] = $this->session['challengecookie'];
837
                        $this->session['challengecookie'] = md5($this->session['challengekey'].microtime());
915
                        $this->session['challengecookie'] = md5($this->session['challengekey'].microtime());
838
                        setcookie('authchallenge', $this->session['challengecookie']);
916
                        setcookie('authchallenge', $this->session['challengecookie']);
839
                    }
917
                    }
840
                    
918
 
-
 
919
                    // Check for ip change
841
                    // Check for ip change
920
                    if (   isset($this->server['REMOTE_ADDR'])
842
                    if (   isset($this->server['REMOTE_ADDR']) 
921
                        && $this->session['sessionip'] != $this->server['REMOTE_ADDR']) {
843
                        && $this->session['sessionip'] != $this->server['REMOTE_ADDR']) {
922
                        $this->log('Security Breach. Remote IP Address changed.', AUTH_LOG_INFO);
844
                        // Check if the IP of the user has changed, if so we 
923
                        // Check if the IP of the user has changed, if so we
845
                        // assume a man in the middle attack and log him out
924
                        // assume a man in the middle attack and log him out
846
                        $this->expired = true;
925
                        $this->expired = true;
847
                        $this->status = AUTH_SECURITY_BREACH;
926
                        $this->status = AUTH_SECURITY_BREACH;
848
                        $this->logout();
927
                        $this->logout();
-
 
928
                        return false;
-
 
929
                    }
-
 
930
 
-
 
931
                    // Check for ip change (if connected via proxy)
-
 
932
                    if (   isset($this->server['HTTP_X_FORWARDED_FOR'])
-
 
933
                        && $this->session['sessionforwardedfor'] != $this->server['HTTP_X_FORWARDED_FOR']) {
-
 
934
                        $this->log('Security Breach. Forwarded For IP Address changed.', AUTH_LOG_INFO);
-
 
935
                        // Check if the IP of the user connecting via proxy has
-
 
936
                        // changed, if so we assume a man in the middle attack
-
 
937
                        // and log him out.
-
 
938
                        $this->expired = true;
-
 
939
                        $this->status = AUTH_SECURITY_BREACH;
-
 
940
                        $this->logout();
849
                        return false;
941
                        return false;
850
                    }
942
                    }
851
                    
943
 
-
 
944
                    // Check for useragent change
852
                    // Check for useragent change
945
                    if (   isset($this->server['HTTP_USER_AGENT'])
853
                    if (   isset($this->server['HTTP_USER_AGENT']) 
946
                        && $this->session['sessionuseragent'] != $this->server['HTTP_USER_AGENT']) {
854
                        && $this->session['sessionuseragent'] != $this->server['HTTP_USER_AGENT']) {
947
                        $this->log('Security Breach. User Agent changed.', AUTH_LOG_INFO);
855
                        // Check if the User-Agent of the user has changed, if 
948
                        // Check if the User-Agent of the user has changed, if
856
                        // so we assume a man in the middle attack and log him out
949
                        // so we assume a man in the middle attack and log him out
857
                        $this->expired = true;
950
                        $this->expired = true;
858
                        $this->status = AUTH_SECURITY_BREACH;
951
                        $this->status = AUTH_SECURITY_BREACH;
859
                        $this->logout();
952
                        $this->logout();
860
                        return false;
953
                        return false;
861
                    }
954
                    }
862
    
955
 
863
                    // Check challenge cookie here, if challengecookieold is not set 
956
                    // Check challenge cookie here, if challengecookieold is not set
864
                    // this is the first time and check is skipped
957
                    // this is the first time and check is skipped
865
                    // TODO when user open two pages similtaneuly (open in new window,open 
958
                    // TODO when user open two pages similtaneuly (open in new window,open
-
 
959
                    // in tab) auth breach is caused find out a way around that if possible
866
                    // in tab) auth breach is caused find out a way around that if possible
960
                    if (   isset($this->session['challengecookieold'])
867
                    if (   isset($this->session['challengecookieold']) 
961
                        && $this->session['challengecookieold'] != $this->cookie['authchallenge']) {
868
                        && $this->session['challengecookieold'] != $this->cookie['authchallenge']) {
962
                        $this->log('Security Breach. Challenge Cookie mismatch.', AUTH_LOG_INFO);
869
                        $this->expired = true;
963
                        $this->expired = true;
870
                        $this->status = AUTH_SECURITY_BREACH;
964
                        $this->status = AUTH_SECURITY_BREACH;
871
                        $this->logout();
965
                        $this->logout();
872
                        $this->login();
966
                        $this->login();
Line 873... Line 967...
873
                        return false;
967
                        return false;
-
 
968
                    }
874
                    }
969
                }
875
                }
970
 
-
 
971
                if (is_callable($this->checkAuthCallback)) {
876
 
972
                    $this->log('Calling checkAuthCallback ('.$this->checkAuthCallback.').', AUTH_LOG_DEBUG);
877
                if (is_callable($this->checkAuthCallback)) {
973
                    $checkCallback = call_user_func_array($this->checkAuthCallback, array($this->username, &$this));
878
                    $checkCallback = call_user_func_array($this->checkAuthCallback, array($this->username, &$this));
974
                    if ($checkCallback == false) {
879
                    if ($checkCallback == false) {
975
                        $this->log('checkAuthCallback failed.', AUTH_LOG_INFO);
880
                        $this->expired = true;
976
                        $this->expired = true;
881
                        $this->status = AUTH_CALLBACK_ABORT;
977
                        $this->status = AUTH_CALLBACK_ABORT;
Line -... Line 978...
-
 
978
                        $this->logout();
882
                        $this->logout();
979
                        return false;
883
                        return false;
980
                    }
884
                    }
981
                }
-
 
982
 
885
                }
983
                $this->log('Session OK.', AUTH_LOG_INFO);
886
 
984
                return true;
Line 887... Line 985...
887
                return true;
985
            }
888
            }
986
        }
Line 905... Line 1003...
905
    {
1003
    {
906
        static $staticAuth;
1004
        static $staticAuth;
907
        if(!isset($staticAuth)) {
1005
        if(!isset($staticAuth)) {
908
            $staticAuth = new Auth('null', $options);
1006
            $staticAuth = new Auth('null', $options);
909
        }
1007
        }
-
 
1008
        $staticAuth->log('Auth::staticCheckAuth() called', AUTH_LOG_DEBUG);
910
        return $staticAuth->checkAuth();
1009
        return $staticAuth->checkAuth();
911
    }
1010
    }
Line 912... Line 1011...
912
 
1011
 
913
    // }}}
1012
    // }}}
Line 919... Line 1018...
919
     * @access public
1018
     * @access public
920
     * @return bool  True if the user is logged in, otherwise false.
1019
     * @return bool  True if the user is logged in, otherwise false.
921
     */
1020
     */
922
    function getAuth()
1021
    function getAuth()
923
    {
1022
    {
-
 
1023
        $this->log('Auth::getAuth() called.', AUTH_LOG_DEBUG);
924
        return $this->checkAuth();
1024
        return $this->checkAuth();
925
    }
1025
    }
Line 926... Line 1026...
926
 
1026
 
927
    // }}}
1027
    // }}}
Line 937... Line 1037...
937
     * @access public
1037
     * @access public
938
     * @return void
1038
     * @return void
939
     */
1039
     */
940
    function logout()
1040
    function logout()
941
    {
1041
    {
-
 
1042
        $this->log('Auth::logout() called.', AUTH_LOG_DEBUG);
-
 
1043
 
942
        if (is_callable($this->logoutCallback)) {
1044
        if (is_callable($this->logoutCallback) && isset($this->session['username'])) {
-
 
1045
            $this->log('Calling logoutCallback ('.$this->logoutCallback.').', AUTH_LOG_DEBUG);
943
            call_user_func_array($this->logoutCallback, array($this->session['username'], &$this));
1046
            call_user_func_array($this->logoutCallback, array($this->session['username'], &$this));
944
        }
1047
        }
Line 945... Line 1048...
945
 
1048
 
946
        $this->username = '';
1049
        $this->username = '';
947
        $this->password = '';
1050
        $this->password = '';
948
        
1051
 
949
        $this->session = null;
1052
        $this->session = null;
Line 950... Line 1053...
950
    }
1053
    }
951
 
1054
 
Line 994... Line 1097...
994
        return $this->status;
1097
        return $this->status;
995
    }
1098
    }
Line 996... Line 1099...
996
 
1099
 
997
    // }}}
1100
    // }}}
998
    // {{{ getPostUsernameField()
1101
    // {{{ getPostUsernameField()
999
    
1102
 
1000
    /**
1103
    /**
1001
     * Gets the post varible used for the username
1104
     * Gets the post varible used for the username
1002
     * 
1105
     *
1003
     * @return string
1106
     * @return string
1004
     * @access public
1107
     * @access public
1005
     */
1108
     */
1006
    function getPostUsernameField()
1109
    function getPostUsernameField()
Line 1011... Line 1114...
1011
    // }}}
1114
    // }}}
1012
    // {{{ getPostPasswordField()
1115
    // {{{ getPostPasswordField()
Line 1013... Line 1116...
1013
 
1116
 
1014
    /**
1117
    /**
1015
     * Gets the post varible used for the username
1118
     * Gets the post varible used for the username
1016
     * 
1119
     *
1017
     * @return string
1120
     * @return string
1018
     * @access public
1121
     * @access public
1019
     */
1122
     */
1020
    function getPostPasswordField()
1123
    function getPostPasswordField()
Line 1052... Line 1155...
1052
     * @access public
1155
     * @access public
1053
     * @return array
1156
     * @return array
1054
     */
1157
     */
1055
    function listUsers()
1158
    function listUsers()
1056
    {
1159
    {
-
 
1160
        $this->log('Auth::listUsers() called.', AUTH_LOG_DEBUG);
1057
        $this->_loadStorage();
1161
        $this->_loadStorage();
1058
        return $this->storage->listUsers();
1162
        return $this->storage->listUsers();
1059
    }
1163
    }
Line 1060... Line 1164...
1060
 
1164
 
Line 1071... Line 1175...
1071
     * @return mixed  True on success, PEAR error object on error
1175
     * @return mixed  True on success, PEAR error object on error
1072
     *                and AUTH_METHOD_NOT_SUPPORTED otherwise.
1176
     *                and AUTH_METHOD_NOT_SUPPORTED otherwise.
1073
     */
1177
     */
1074
    function addUser($username, $password, $additional = '')
1178
    function addUser($username, $password, $additional = '')
1075
    {
1179
    {
-
 
1180
        $this->log('Auth::addUser() called.', AUTH_LOG_DEBUG);
1076
        $this->_loadStorage();
1181
        $this->_loadStorage();
1077
        return $this->storage->addUser($username, $password, $additional);
1182
        return $this->storage->addUser($username, $password, $additional);
1078
    }
1183
    }
Line 1079... Line 1184...
1079
 
1184
 
Line 1088... Line 1193...
1088
     * @return mixed  True on success, PEAR error object on error
1193
     * @return mixed  True on success, PEAR error object on error
1089
     *                and AUTH_METHOD_NOT_SUPPORTED otherwise.
1194
     *                and AUTH_METHOD_NOT_SUPPORTED otherwise.
1090
     */
1195
     */
1091
    function removeUser($username)
1196
    function removeUser($username)
1092
    {
1197
    {
-
 
1198
        $this->log('Auth::removeUser() called.', AUTH_LOG_DEBUG);
1093
        $this->_loadStorage();
1199
        $this->_loadStorage();
1094
        return $this->storage->removeUser($username);
1200
        return $this->storage->removeUser($username);
1095
    }
1201
    }
Line 1096... Line 1202...
1096
 
1202
 
Line 1100... Line 1206...
1100
    /**
1206
    /**
1101
     * Change password for user in the storage container
1207
     * Change password for user in the storage container
1102
     *
1208
     *
1103
     * @access public
1209
     * @access public
1104
     * @param string Username
1210
     * @param string Username
1105
     * @param string The new password 
1211
     * @param string The new password
1106
     * @return mixed True on success, PEAR error object on error
1212
     * @return mixed True on success, PEAR error object on error
1107
     *               and AUTH_METHOD_NOT_SUPPORTED otherwise.
1213
     *               and AUTH_METHOD_NOT_SUPPORTED otherwise.
1108
     */
1214
     */
1109
    function changePassword($username, $password)
1215
    function changePassword($username, $password)
1110
    {
1216
    {
-
 
1217
        $this->log('Auth::changePassword() called', AUTH_LOG_DEBUG);
1111
        $this->_loadStorage();
1218
        $this->_loadStorage();
1112
        return $this->storage->changePassword($username, $password);
1219
        return $this->storage->changePassword($username, $password);
1113
    }
1220
    }
Line 1114... Line 1221...
1114
 
1221
 
-
 
1222
    // }}}
-
 
1223
    // {{{ log()
-
 
1224
 
-
 
1225
    /**
-
 
1226
     * Log a message from the Auth system
-
 
1227
     *
-
 
1228
     * @access public
-
 
1229
     * @param string The message to log
-
 
1230
     * @param string The log level to log the message under. See the Log documentation for more info.
-
 
1231
     * @return boolean
-
 
1232
     */
-
 
1233
    function log($message, $level = AUTH_LOG_DEBUG)
-
 
1234
    {
-
 
1235
        if (!$this->enableLogging) return false;
-
 
1236
 
-
 
1237
        $this->_loadLogger();
-
 
1238
 
-
 
1239
        $this->logger->log('AUTH: '.$message, $level);
-
 
1240
    }
-
 
1241
 
-
 
1242
    // }}}
-
 
1243
    // {{{ _loadLogger()
-
 
1244
 
-
 
1245
    /**
-
 
1246
      * Load Log object if not already loaded
-
 
1247
      *
-
 
1248
      * Suspend logger instantiation to make Auth lighter to use
-
 
1249
      * for calls which do not require logging
-
 
1250
      *
-
 
1251
      * @return bool    True if the logger is loaded, false if the logger
-
 
1252
      *                 is already loaded
-
 
1253
      * @access private
-
 
1254
      */
-
 
1255
    function _loadLogger()
-
 
1256
    {
-
 
1257
        if(is_null($this->logger)) {
-
 
1258
            if (!class_exists('Log')) {
-
 
1259
                include_once 'Log.php';
-
 
1260
            }
-
 
1261
            $this->logger =& Log::singleton('null',
-
 
1262
                    null,
-
 
1263
                    'auth['.getmypid().']',
-
 
1264
                    array(),
-
 
1265
                    AUTH_LOG_DEBUG);
-
 
1266
            return(true);
-
 
1267
        }
-
 
1268
        return(false);
-
 
1269
    }
-
 
1270
 
-
 
1271
    // }}}
-
 
1272
    // {{{ attachLogObserver()
-
 
1273
 
-
 
1274
    /**
-
 
1275
     * Attach an Observer to the Auth Log Source
-
 
1276
     *
-
 
1277
     * @param object Log_Observer A Log Observer instance
-
 
1278
     * @return boolean
-
 
1279
     */
-
 
1280
    function attachLogObserver(&$observer) {
-
 
1281
 
-
 
1282
        $this->_loadLogger();
-
 
1283
 
-
 
1284
        return $this->logger->attach($observer);
-
 
1285
 
-
 
1286
    }
-
 
1287
 
Line 1115... Line 1288...
1115
    // }}}
1288
    // }}}
1116
 
1289