| 1 | <?php |
|---|
| 2 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ |
|---|
| 3 | |
|---|
| 4 | /** |
|---|
| 5 | * The main include file for Auth package |
|---|
| 6 | * |
|---|
| 7 | * PHP versions 4 and 5 |
|---|
| 8 | * |
|---|
| 9 | * LICENSE: This source file is subject to version 3.01 of the PHP license |
|---|
| 10 | * that is available through the world-wide-web at the following URI: |
|---|
| 11 | * http://www.php.net/license/3_01.txt. If you did not receive a copy of |
|---|
| 12 | * the PHP License and are unable to obtain it through the web, please |
|---|
| 13 | * send a note to [email protected] so we can mail you a copy immediately. |
|---|
| 14 | * |
|---|
| 15 | * @category Authentication |
|---|
| 16 | * @package Auth |
|---|
| 17 | * @author Martin Jansen <[email protected]> |
|---|
| 18 | * @author Adam Ashley <[email protected]> |
|---|
| 19 | * @copyright 2001-2006 The PHP Group |
|---|
| 20 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 21 | * @version CVS: $Id$ |
|---|
| 22 | * @link http://pear.php.net/package/Auth |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Returned if session exceeds idle time |
|---|
| 27 | */ |
|---|
| 28 | define('AUTH_IDLED', -1); |
|---|
| 29 | /** |
|---|
| 30 | * Returned if session has expired |
|---|
| 31 | */ |
|---|
| 32 | define('AUTH_EXPIRED', -2); |
|---|
| 33 | /** |
|---|
| 34 | * Returned if container is unable to authenticate user/password pair |
|---|
| 35 | */ |
|---|
| 36 | define('AUTH_WRONG_LOGIN', -3); |
|---|
| 37 | /** |
|---|
| 38 | * Returned if a container method is not supported. |
|---|
| 39 | */ |
|---|
| 40 | define('AUTH_METHOD_NOT_SUPPORTED', -4); |
|---|
| 41 | /** |
|---|
| 42 | * Returned if new Advanced security system detects a breach |
|---|
| 43 | */ |
|---|
| 44 | define('AUTH_SECURITY_BREACH', -5); |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * PEAR::Auth |
|---|
| 48 | * |
|---|
| 49 | * The PEAR::Auth class provides methods for creating an |
|---|
| 50 | * authentication system using PHP. |
|---|
| 51 | * |
|---|
| 52 | * @category Authentication |
|---|
| 53 | * @package Auth |
|---|
| 54 | * @author Martin Jansen <[email protected]> |
|---|
| 55 | * @author Adam Ashley <[email protected]> |
|---|
| 56 | * @copyright 2001-2006 The PHP Group |
|---|
| 57 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
|---|
| 58 | * @version Release: 1.4.2 File: $Revision: 8718 $ |
|---|
| 59 | * @link http://pear.php.net/package/Auth |
|---|
| 60 | */ |
|---|
| 61 | class Auth { |
|---|
| 62 | |
|---|
| 63 | // {{{ properties |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Auth lifetime in seconds |
|---|
| 67 | * |
|---|
| 68 | * If this variable is set to 0, auth never expires |
|---|
| 69 | * |
|---|
| 70 | * @var integer |
|---|
| 71 | * @see setExpire(), checkAuth() |
|---|
| 72 | */ |
|---|
| 73 | var $expire = 0; |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Has the auth session expired? |
|---|
| 77 | * |
|---|
| 78 | * @var bool |
|---|
| 79 | * @see checkAuth() |
|---|
| 80 | */ |
|---|
| 81 | var $expired = false; |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Maximum idletime in seconds |
|---|
| 85 | * |
|---|
| 86 | * The difference to $expire is, that the idletime gets |
|---|
| 87 | * refreshed each time checkAuth() is called. If this |
|---|
| 88 | * variable is set to 0, idletime is never checked. |
|---|
| 89 | * |
|---|
| 90 | * @var integer |
|---|
| 91 | * @see setIdle(), checkAuth() |
|---|
| 92 | */ |
|---|
| 93 | var $idle = 0; |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Is the maximum idletime over? |
|---|
| 97 | * |
|---|
| 98 | * @var boolean |
|---|
| 99 | * @see checkAuth() |
|---|
| 100 | */ |
|---|
| 101 | var $idled = false; |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Storage object |
|---|
| 105 | * |
|---|
| 106 | * @var object |
|---|
| 107 | * @see Auth(), validateLogin() |
|---|
| 108 | */ |
|---|
| 109 | var $storage = ''; |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * User-defined function that creates the login screen |
|---|
| 113 | * |
|---|
| 114 | * @var string |
|---|
| 115 | */ |
|---|
| 116 | var $loginFunction = ''; |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Should the login form be displayed |
|---|
| 120 | * |
|---|
| 121 | * @var bool |
|---|
| 122 | * @see setShowlogin() |
|---|
| 123 | */ |
|---|
| 124 | var $showLogin = true; |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Is Login Allowed from this page |
|---|
| 128 | * |
|---|
| 129 | * @var bool |
|---|
| 130 | * @see setAllowLogin |
|---|
| 131 | */ |
|---|
| 132 | var $allowLogin = true; |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Current authentication status |
|---|
| 136 | * |
|---|
| 137 | * @var string |
|---|
| 138 | */ |
|---|
| 139 | var $status = ''; |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * Username |
|---|
| 143 | * |
|---|
| 144 | * @var string |
|---|
| 145 | */ |
|---|
| 146 | var $username = ''; |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Password |
|---|
| 150 | * |
|---|
| 151 | * @var string |
|---|
| 152 | */ |
|---|
| 153 | var $password = ''; |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Login callback function name |
|---|
| 157 | * |
|---|
| 158 | * @var string |
|---|
| 159 | * @see setLoginCallback() |
|---|
| 160 | */ |
|---|
| 161 | var $loginCallback = ''; |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * Failed Login callback function name |
|---|
| 165 | * |
|---|
| 166 | * @var string |
|---|
| 167 | * @see setFailedLoginCallback() |
|---|
| 168 | */ |
|---|
| 169 | var $loginFailedCallback = ''; |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Logout callback function name |
|---|
| 173 | * |
|---|
| 174 | * @var string |
|---|
| 175 | * @see setLogoutCallback() |
|---|
| 176 | */ |
|---|
| 177 | var $logoutCallback = ''; |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * Auth session-array name |
|---|
| 181 | * |
|---|
| 182 | * @var string |
|---|
| 183 | */ |
|---|
| 184 | var $_sessionName = '_authsession'; |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Package Version |
|---|
| 188 | * |
|---|
| 189 | * @var string |
|---|
| 190 | */ |
|---|
| 191 | var $version = "@version@"; |
|---|
| 192 | |
|---|
| 193 | /** |
|---|
| 194 | * Flag to use advanced security |
|---|
| 195 | * When set extra checks will be made to see if the |
|---|
| 196 | * user's IP or useragent have changed across requests. |
|---|
| 197 | * Turned off by default to preserve BC. |
|---|
| 198 | * |
|---|
| 199 | * @var boolean |
|---|
| 200 | */ |
|---|
| 201 | var $advancedsecurity = false; |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * Username key in POST array |
|---|
| 205 | * |
|---|
| 206 | * @var string |
|---|
| 207 | */ |
|---|
| 208 | var $_postUsername = 'username'; |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * Password key in POST array |
|---|
| 212 | * |
|---|
| 213 | * @var string |
|---|
| 214 | */ |
|---|
| 215 | var $_postPassword = 'password'; |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | * Holds a reference to the session auth variable |
|---|
| 219 | * @var array |
|---|
| 220 | */ |
|---|
| 221 | var $session; |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * Holds a reference to the global server variable |
|---|
| 225 | * @var array |
|---|
| 226 | */ |
|---|
| 227 | var $server; |
|---|
| 228 | |
|---|
| 229 | /** |
|---|
| 230 | * Holds a reference to the global post variable |
|---|
| 231 | * @var array |
|---|
| 232 | */ |
|---|
| 233 | var $post; |
|---|
| 234 | |
|---|
| 235 | /** |
|---|
| 236 | * Holds a reference to the global cookie variable |
|---|
| 237 | * @var array |
|---|
| 238 | */ |
|---|
| 239 | var $cookie; |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * A hash to hold various superglobals as reference |
|---|
| 243 | * @var array |
|---|
| 244 | */ |
|---|
| 245 | var $authdata; |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | * How many times has checkAuth been called |
|---|
| 249 | * var int |
|---|
| 250 | */ |
|---|
| 251 | var $authChecks = 0; |
|---|
| 252 | |
|---|
| 253 | // }}} |
|---|
| 254 | // {{{ Auth() [constructor] |
|---|
| 255 | |
|---|
| 256 | /** |
|---|
| 257 | * Constructor |
|---|
| 258 | * |
|---|
| 259 | * Set up the storage driver. |
|---|
| 260 | * |
|---|
| 261 | * @param string Type of the storage driver |
|---|
| 262 | * @param mixed Additional options for the storage driver |
|---|
| 263 | * (example: if you are using DB as the storage |
|---|
| 264 | * driver, you have to pass the dsn string here) |
|---|
| 265 | * |
|---|
| 266 | * @param string Name of the function that creates the login form |
|---|
| 267 | * @param boolean Should the login form be displayed if neccessary? |
|---|
| 268 | * @return void |
|---|
| 269 | */ |
|---|
| 270 | function Auth($storageDriver, $options = '', $loginFunction = '', $showLogin = true) |
|---|
| 271 | { |
|---|
| 272 | $this->applyAuthOptions($options); |
|---|
| 273 | |
|---|
| 274 | // Start the session suppress error if already started |
|---|
| 275 | if(!session_id()){ |
|---|
| 276 | @session_start(); |
|---|
| 277 | if(!session_id()) { |
|---|
| 278 | // Throw error |
|---|
| 279 | include_once 'PEAR.php'; |
|---|
| 280 | PEAR::throwError('Session could not be started by Auth, ' |
|---|
| 281 | .'possibly headers are already sent, try putting ' |
|---|
| 282 | .'ob_start in the beginning of your script'); |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | // Make Sure Auth session variable is there |
|---|
| 287 | if(!isset($_SESSION[$this->_sessionName])) { |
|---|
| 288 | $_SESSION[$this->_sessionName] = array(); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | // Assign Some globals to internal references, this will replace _importGlobalVariable |
|---|
| 292 | $this->session =& $_SESSION[$this->_sessionName]; |
|---|
| 293 | $this->server =& $_SERVER; |
|---|
| 294 | $this->post =& $_POST; |
|---|
| 295 | $this->cookie =& $_COOKIE; |
|---|
| 296 | |
|---|
| 297 | if ($loginFunction != '' && is_callable($loginFunction)) { |
|---|
| 298 | $this->loginFunction = $loginFunction; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | if (is_bool($showLogin)) { |
|---|
| 302 | $this->showLogin = $showLogin; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | if (is_object($storageDriver)) { |
|---|
| 306 | $this->storage =& $storageDriver; |
|---|
| 307 | // Pass a reference to auth to the container, ugly but works |
|---|
| 308 | // this is used by the DB container to use method setAuthData not staticaly. |
|---|
| 309 | $this->storage->_auth_obj =& $this; |
|---|
| 310 | } else { |
|---|
| 311 | // $this->storage = $this->_factory($storageDriver, $options); |
|---|
| 312 | // |
|---|
| 313 | $this->storage_driver = $storageDriver; |
|---|
| 314 | $this->storage_options =& $options; |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | // }}} |
|---|
| 319 | // {{{ applyAuthOptions() |
|---|
| 320 | |
|---|
| 321 | /** |
|---|
| 322 | * Set the Auth options |
|---|
| 323 | * |
|---|
| 324 | * Some options which are Auth specific will be applied |
|---|
| 325 | * the rest will be left for usage by the container |
|---|
| 326 | * |
|---|
| 327 | * @param array An array of Auth options |
|---|
| 328 | * @return array The options which were not applied |
|---|
| 329 | * @access private |
|---|
| 330 | */ |
|---|
| 331 | function &applyAuthOptions(&$options) |
|---|
| 332 | { |
|---|
| 333 | if(is_array($options)){ |
|---|
| 334 | if (!empty($options['sessionName'])) { |
|---|
| 335 | $this->_sessionName = $options['sessionName']; |
|---|
| 336 | unset($options['sessionName']); |
|---|
| 337 | } |
|---|
| 338 | if (isset($options['allowLogin'])) { |
|---|
| 339 | $this->allowLogin = $options['allowLogin']; |
|---|
| 340 | unset($options['allowLogin']); |
|---|
| 341 | } |
|---|
| 342 | if (!empty($options['postUsername'])) { |
|---|
| 343 | $this->_postUsername = $options['postUsername']; |
|---|
| 344 | unset($options['postUsername']); |
|---|
| 345 | } |
|---|
| 346 | if (!empty($options['postPassword'])) { |
|---|
| 347 | $this->_postPassword = $options['postPassword']; |
|---|
| 348 | unset($options['postPassword']); |
|---|
| 349 | } |
|---|
| 350 | if (isset($options['advancedsecurity'])) { |
|---|
| 351 | $this->advancedsecurity = $options['advancedsecurity']; |
|---|
| 352 | unset($options['advancedsecurity']); |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | return($options); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | // }}} |
|---|
| 359 | // {{{ _loadStorage() |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * Load Storage Driver if not already loaded |
|---|
| 363 | * |
|---|
| 364 | * Suspend storage instantiation to make Auth lighter to use |
|---|
| 365 | * for calls which do not require login |
|---|
| 366 | * |
|---|
| 367 | * @return bool True if the conainer is loaded, false if the container |
|---|
| 368 | * is already loaded |
|---|
| 369 | * @access private |
|---|
| 370 | */ |
|---|
| 371 | function _loadStorage() |
|---|
| 372 | { |
|---|
| 373 | if(!is_object($this->storage)) { |
|---|
| 374 | $this->storage =& $this->_factory($this->storage_driver, |
|---|
| 375 | $this->storage_options); |
|---|
| 376 | $this->storage->_auth_obj =& $this; |
|---|
| 377 | return(true); |
|---|
| 378 | } |
|---|
| 379 | return(false); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | // }}} |
|---|
| 383 | // {{{ _factory() |
|---|
| 384 | |
|---|
| 385 | /** |
|---|
| 386 | * Return a storage driver based on $driver and $options |
|---|
| 387 | * |
|---|
| 388 | * @static |
|---|
| 389 | * @param string $driver Type of storage class to return |
|---|
| 390 | * @param string $options Optional parameters for the storage class |
|---|
| 391 | * @return object Object Storage object |
|---|
| 392 | * @access private |
|---|
| 393 | */ |
|---|
| 394 | function &_factory($driver, $options = '') |
|---|
| 395 | { |
|---|
| 396 | $storage_class = 'Auth_Container_' . $driver; |
|---|
| 397 | include_once 'Auth/Container/' . $driver . '.php'; |
|---|
| 398 | $obj =& new $storage_class($options); |
|---|
| 399 | return $obj; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | // }}} |
|---|
| 403 | // {{{ assignData() |
|---|
| 404 | |
|---|
| 405 | /** |
|---|
| 406 | * Assign data from login form to internal values |
|---|
| 407 | * |
|---|
| 408 | * This function takes the values for username and password |
|---|
| 409 | * from $HTTP_POST_VARS/$_POST and assigns them to internal variables. |
|---|
| 410 | * If you wish to use another source apart from $HTTP_POST_VARS/$_POST, |
|---|
| 411 | * you have to derive this function. |
|---|
| 412 | * |
|---|
| 413 | * @global $HTTP_POST_VARS, $_POST |
|---|
| 414 | * @see Auth |
|---|
| 415 | * @return void |
|---|
| 416 | * @access private |
|---|
| 417 | */ |
|---|
| 418 | function assignData() |
|---|
| 419 | { |
|---|
| 420 | if ( isset($this->post[$this->_postUsername]) |
|---|
| 421 | && $this->post[$this->_postUsername] != '') { |
|---|
| 422 | $this->username = (get_magic_quotes_gpc() == 1 |
|---|
| 423 | ? stripslashes($this->post[$this->_postUsername]) |
|---|
| 424 | : $this->post[$this->_postUsername]); |
|---|
| 425 | } |
|---|
| 426 | if ( isset($this->post[$this->_postPassword]) |
|---|
| 427 | && $this->post[$this->_postPassword] != '') { |
|---|
| 428 | $this->password = (get_magic_quotes_gpc() == 1 |
|---|
| 429 | ? stripslashes($this->post[$this->_postPassword]) |
|---|
| 430 | : $this->post[$this->_postPassword] ); |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | // }}} |
|---|
| 435 | // {{{ start() |
|---|
| 436 | |
|---|
| 437 | /** |
|---|
| 438 | * Start new auth session |
|---|
| 439 | * |
|---|
| 440 | * @return void |
|---|
| 441 | * @access public |
|---|
| 442 | */ |
|---|
| 443 | function start() |
|---|
| 444 | { |
|---|
| 445 | $this->assignData(); |
|---|
| 446 | if (!$this->checkAuth() && $this->allowLogin) { |
|---|
| 447 | $this->login(); |
|---|
| 448 | } |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | // }}} |
|---|
| 452 | // {{{ login() |
|---|
| 453 | |
|---|
| 454 | /** |
|---|
| 455 | * Login function |
|---|
| 456 | * |
|---|
| 457 | * @return void |
|---|
| 458 | * @access private |
|---|
| 459 | */ |
|---|
| 460 | function login() |
|---|
| 461 | { |
|---|
| 462 | $login_ok = false; |
|---|
| 463 | $this->_loadStorage(); |
|---|
| 464 | |
|---|
| 465 | // Check if using challenge response |
|---|
| 466 | (isset($this->post['authsecret']) && $this->post['authsecret'] == 1) |
|---|
| 467 | ? $usingChap = true |
|---|
| 468 | : $usingChap = false; |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | // When the user has already entered a username, we have to validate it. |
|---|
| 472 | if (!empty($this->username)) { |
|---|
| 473 | if (true === $this->storage->fetchData($this->username, $this->password, $usingChap)) { |
|---|
| 474 | $this->session['challengekey'] = md5($this->username.$this->password); |
|---|
| 475 | $login_ok = true; |
|---|
| 476 | } |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | if (!empty($this->username) && $login_ok) { |
|---|
| 480 | $this->setAuth($this->username); |
|---|
| 481 | if (is_callable($this->loginCallback)) { |
|---|
| 482 | call_user_func_array($this->loginCallback, array($this->username, &$this)); |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | // If the login failed or the user entered no username, |
|---|
| 487 | // output the login screen again. |
|---|
| 488 | if (!empty($this->username) && !$login_ok) { |
|---|
| 489 | $this->status = AUTH_WRONG_LOGIN; |
|---|
| 490 | if (is_callable($this->loginFailedCallback)) { |
|---|
| 491 | call_user_func_array($this->loginFailedCallback, array($this->username, &$this)); |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | if ((empty($this->username) || !$login_ok) && $this->showLogin) { |
|---|
| 496 | if (is_callable($this->loginFunction)) { |
|---|
| 497 | call_user_func_array($this->loginFunction, array($this->username, $this->status, &$this)); |
|---|
| 498 | } else { |
|---|
| 499 | // BC fix Auth used to use drawLogin for this |
|---|
| 500 | // call is sub classes implement this |
|---|
| 501 | if (is_callable(array($this, 'drawLogin'))) { |
|---|
| 502 | return $this->drawLogin($this->username, $this); |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | // New Login form |
|---|
| 506 | include_once 'Auth/Frontend/Html.php'; |
|---|
| 507 | return Auth_Frontend_Html::render($this, $this->username); |
|---|
| 508 | } |
|---|
| 509 | } else { |
|---|
| 510 | return; |
|---|
| 511 | } |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | // }}} |
|---|
| 515 | // {{{ setExpire() |
|---|
| 516 | |
|---|
| 517 | /** |
|---|
| 518 | * Set the maximum expire time |
|---|
| 519 | * |
|---|
| 520 | * @param integer time in seconds |
|---|
| 521 | * @param bool add time to current expire time or not |
|---|
| 522 | * @return void |
|---|
| 523 | * @access public |
|---|
| 524 | */ |
|---|
| 525 | function setExpire($time, $add = false) |
|---|
| 526 | { |
|---|
| 527 | $add ? $this->expire += $time : $this->expire = $time; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | // }}} |
|---|
| 531 | // {{{ setIdle() |
|---|
| 532 | |
|---|
| 533 | /** |
|---|
| 534 | * Set the maximum idle time |
|---|
| 535 | * |
|---|
| 536 | * @param integer time in seconds |
|---|
| 537 | * @param bool add time to current maximum idle time or not |
|---|
| 538 | * @return void |
|---|
| 539 | * @access public |
|---|
| 540 | */ |
|---|
| 541 | function setIdle($time, $add = false) |
|---|
| 542 | { |
|---|
| 543 | $add ? $this->idle += $time : $this->idle = $time; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | // }}} |
|---|
| 547 | // {{{ setSessionName() |
|---|
| 548 | |
|---|
| 549 | /** |
|---|
| 550 | * Set name of the session to a customized value. |
|---|
| 551 | * |
|---|
| 552 | * If you are using multiple instances of PEAR::Auth |
|---|
| 553 | * on the same domain, you can change the name of |
|---|
| 554 | * session per application via this function. |
|---|
| 555 | * This will chnage the name of the session variable |
|---|
| 556 | * auth uses to store it's data in the session |
|---|
| 557 | * |
|---|
| 558 | * @param string New name for the session |
|---|
| 559 | * @return void |
|---|
| 560 | * @access public |
|---|
| 561 | */ |
|---|
| 562 | function setSessionName($name = 'session') |
|---|
| 563 | { |
|---|
| 564 | $this->_sessionName = '_auth_'.$name; |
|---|
| 565 | $this->session =& $_SESSION[$this->_sessionName]; |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | // }}} |
|---|
| 569 | // {{{ setShowLogin() |
|---|
| 570 | |
|---|
| 571 | /** |
|---|
| 572 | * Should the login form be displayed if neccessary? |
|---|
| 573 | * |
|---|
| 574 | * @param bool show login form or not |
|---|
| 575 | * @return void |
|---|
| 576 | * @access public |
|---|
| 577 | */ |
|---|
| 578 | function setShowLogin($showLogin = true) |
|---|
| 579 | { |
|---|
| 580 | $this->showLogin = $showLogin; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | // }}} |
|---|
| 584 | // {{{ setAllowLogin() |
|---|
| 585 | |
|---|
| 586 | /** |
|---|
| 587 | * Should the login form be displayed if neccessary? |
|---|
| 588 | * |
|---|
| 589 | * @param bool show login form or not |
|---|
| 590 | * @return void |
|---|
| 591 | * @access public |
|---|
| 592 | */ |
|---|
| 593 | function setAllowLogin($allowLogin = true) |
|---|
| 594 | { |
|---|
| 595 | $this->allowLogin = $allowLogin; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | // }}} |
|---|
| 599 | // {{{ setLoginCallback() |
|---|
| 600 | |
|---|
| 601 | /** |
|---|
| 602 | * Register a callback function to be called on user login. |
|---|
| 603 | * The function will receive two parameters, the username and a reference to the auth object. |
|---|
| 604 | * |
|---|
| 605 | * @param string callback function name |
|---|
| 606 | * @return void |
|---|
| 607 | * @see setLogoutCallback() |
|---|
| 608 | * @access public |
|---|
| 609 | */ |
|---|
| 610 | function setLoginCallback($loginCallback) |
|---|
| 611 | { |
|---|
| 612 | $this->loginCallback = $loginCallback; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | // }}} |
|---|
| 616 | // {{{ setFailedLoginCallback() |
|---|
| 617 | |
|---|
| 618 | /** |
|---|
| 619 | * Register a callback function to be called on failed user login. |
|---|
| 620 | * The function will receive a single parameter, the username and a reference to the auth object. |
|---|
| 621 | * |
|---|
| 622 | * @param string callback function name |
|---|
| 623 | * @return void |
|---|
| 624 | * @access public |
|---|
| 625 | */ |
|---|
| 626 | function setFailedLoginCallback($loginFailedCallback) |
|---|
| 627 | { |
|---|
| 628 | $this->loginFailedCallback = $loginFailedCallback; |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | // }}} |
|---|
| 632 | // {{{ setLogoutCallback() |
|---|
| 633 | |
|---|
| 634 | /** |
|---|
| 635 | * Register a callback function to be called on user logout. |
|---|
| 636 | * The function will receive three parameters, the username and a reference to the auth object. |
|---|
| 637 | * |
|---|
| 638 | * @param string callback function name |
|---|
| 639 | * @return void |
|---|
| 640 | * @see setLoginCallback() |
|---|
| 641 | * @access public |
|---|
| 642 | */ |
|---|
| 643 | function setLogoutCallback($logoutCallback) |
|---|
| 644 | { |
|---|
| 645 | $this->logoutCallback = $logoutCallback; |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | // }}} |
|---|
| 649 | // {{{ setAuthData() |
|---|
| 650 | |
|---|
| 651 | /** |
|---|
| 652 | * Register additional information that is to be stored |
|---|
| 653 | * in the session. |
|---|
| 654 | * |
|---|
| 655 | * @param string Name of the data field |
|---|
| 656 | * @param mixed Value of the data field |
|---|
| 657 | * @param boolean Should existing data be overwritten? (default |
|---|
| 658 | * is true) |
|---|
| 659 | * @return void |
|---|
| 660 | * @access public |
|---|
| 661 | */ |
|---|
| 662 | function setAuthData($name, $value, $overwrite = true) |
|---|
| 663 | { |
|---|
| 664 | if (!empty($this->session['data'][$name]) && $overwrite == false) { |
|---|
| 665 | return; |
|---|
| 666 | } |
|---|
| 667 | $this->session['data'][$name] = $value; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | // }}} |
|---|
| 671 | // {{{ getAuthData() |
|---|
| 672 | |
|---|
| 673 | /** |
|---|
| 674 | * Get additional information that is stored in the session. |
|---|
| 675 | * |
|---|
| 676 | * If no value for the first parameter is passed, the method will |
|---|
| 677 | * return all data that is currently stored. |
|---|
| 678 | * |
|---|
| 679 | * @param string Name of the data field |
|---|
| 680 | * @return mixed Value of the data field. |
|---|
| 681 | * @access public |
|---|
| 682 | */ |
|---|
| 683 | function getAuthData($name = null) |
|---|
| 684 | { |
|---|
| 685 | if (!isset($this->session['data'])) { |
|---|
| 686 | return null; |
|---|
| 687 | } |
|---|
| 688 | if(!isset($name)) { |
|---|
| 689 | return $this->session['data']; |
|---|
| 690 | } |
|---|
| 691 | if (isset($name) && isset($this->session['data'][$name])) { |
|---|
| 692 | return $this->session['data'][$name]; |
|---|
| 693 | } |
|---|
| 694 | return null; |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | // }}} |
|---|
| 698 | // {{{ setAuth() |
|---|
| 699 | |
|---|
| 700 | /** |
|---|
| 701 | * Register variable in a session telling that the user |
|---|
| 702 | * has logged in successfully |
|---|
| 703 | * |
|---|
| 704 | * @param string Username |
|---|
| 705 | * @return void |
|---|
| 706 | * @access public |
|---|
| 707 | */ |
|---|
| 708 | function setAuth($username) |
|---|
| 709 | { |
|---|
| 710 | |
|---|
| 711 | // #2021 - Change the session id to avoid session fixation attacks php 4.3.3 > |
|---|
| 712 | session_regenerate_id(true); |
|---|
| 713 | |
|---|
| 714 | if (!isset($this->session) || !is_array($this->session)) { |
|---|
| 715 | $this->session = array(); |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | if (!isset($this->session['data'])) { |
|---|
| 719 | $this->session['data'] = array(); |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | $this->session['sessionip'] = isset($this->server['REMOTE_ADDR']) |
|---|
| 723 | ? $this->server['REMOTE_ADDR'] |
|---|
| 724 | : ''; |
|---|
| 725 | $this->session['sessionuseragent'] = isset($this->server['HTTP_USER_AGENT']) |
|---|
| 726 | ? $this->server['HTTP_USER_AGENT'] |
|---|
| 727 | : ''; |
|---|
| 728 | |
|---|
| 729 | // This should be set by the container to something more safe |
|---|
| 730 | // Like md5(passwd.microtime) |
|---|
| 731 | if(empty($this->session['challengekey'])) { |
|---|
| 732 | $this->session['challengekey'] = md5($username.microtime()); |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | $this->session['challengecookie'] = md5($this->session['challengekey'].microtime()); |
|---|
| 736 | setcookie('authchallenge', $this->session['challengecookie']); |
|---|
| 737 | |
|---|
| 738 | $this->session['registered'] = true; |
|---|
| 739 | $this->session['username'] = $username; |
|---|
| 740 | $this->session['timestamp'] = time(); |
|---|
| 741 | $this->session['idle'] = time(); |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | // }}} |
|---|
| 745 | // {{{ setAdvancedSecurity() |
|---|
| 746 | |
|---|
| 747 | /** |
|---|
| 748 | * Enables advanced security checks |
|---|
| 749 | * |
|---|
| 750 | * Currently only ip change and useragent change |
|---|
| 751 | * are detected |
|---|
| 752 | * @todo Add challenge cookies - Create a cookie which changes every time |
|---|
| 753 | * and contains some challenge key which the server can verify with |
|---|
| 754 | * a session var cookie might need to be crypted (user pass) |
|---|
| 755 | * @param bool Enable or disable |
|---|
| 756 | * @return void |
|---|
| 757 | * @access public |
|---|
| 758 | */ |
|---|
| 759 | function setAdvancedSecurity($flag=true) |
|---|
| 760 | { |
|---|
| 761 | $this->advancedsecurity = $flag; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | // }}} |
|---|
| 765 | // {{{ checkAuth() |
|---|
| 766 | |
|---|
| 767 | /** |
|---|
| 768 | * Checks if there is a session with valid auth information. |
|---|
| 769 | * |
|---|
| 770 | * @access public |
|---|
| 771 | * @return boolean Whether or not the user is authenticated. |
|---|
| 772 | */ |
|---|
| 773 | function checkAuth() |
|---|
| 774 | { |
|---|
| 775 | $this->authChecks++; |
|---|
| 776 | if (isset($this->session)) { |
|---|
| 777 | // Check if authentication session is expired |
|---|
| 778 | if ( $this->expire > 0 |
|---|
| 779 | && isset($this->session['timestamp']) |
|---|
| 780 | && ($this->session['timestamp'] + $this->expire) < time()) { |
|---|
| 781 | $this->expired = true; |
|---|
| 782 | $this->status = AUTH_EXPIRED; |
|---|
| 783 | $this->logout(); |
|---|
| 784 | return false; |
|---|
| 785 | } |
|---|
| 786 | |
|---|
| 787 | // Check if maximum idle time is reached |
|---|
| 788 | if ( $this->idle > 0 |
|---|
| 789 | && isset($this->session['idle']) |
|---|
| 790 | && ($this->session['idle'] + $this->idle) < time()) { |
|---|
| 791 | $this->idled = true; |
|---|
| 792 | $this->status = AUTH_IDLED; |
|---|
| 793 | $this->logout(); |
|---|
| 794 | return false; |
|---|
| 795 | } |
|---|
| 796 | |
|---|
| 797 | if ( isset($this->session['registered']) |
|---|
| 798 | && isset($this->session['username']) |
|---|
| 799 | && $this->session['registered'] == true |
|---|
| 800 | && $this->session['username'] != '') { |
|---|
| 801 | Auth::updateIdle(); |
|---|
| 802 | |
|---|
| 803 | if ($this->advancedsecurity) { |
|---|
| 804 | |
|---|
| 805 | // Only Generate the challenge once |
|---|
| 806 | if($this->authChecks == 1) { |
|---|
| 807 | $this->session['challengecookieold'] = $this->session['challengecookie']; |
|---|
| 808 | $this->session['challengecookie'] = md5($this->session['challengekey'].microtime()); |
|---|
| 809 | setcookie('authchallenge', $this->session['challengecookie']); |
|---|
| 810 | } |
|---|
| 811 | |
|---|
| 812 | // Check for ip change |
|---|
| 813 | if ( isset($this->server['REMOTE_ADDR']) |
|---|
| 814 | && $this->session['sessionip'] != $this->server['REMOTE_ADDR']) { |
|---|
| 815 | // Check if the IP of the user has changed, if so we |
|---|
| 816 | // assume a man in the middle attack and log him out |
|---|
| 817 | $this->expired = true; |
|---|
| 818 | $this->status = AUTH_SECURITY_BREACH; |
|---|
| 819 | $this->logout(); |
|---|
| 820 | return false; |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | // Check for useragent change |
|---|
| 824 | if ( isset($this->server['HTTP_USER_AGENT']) |
|---|
| 825 | && $this->session['sessionuseragent'] != $this->server['HTTP_USER_AGENT']) { |
|---|
| 826 | // Check if the User-Agent of the user has changed, if |
|---|
| 827 | // so we assume a man in the middle attack and log him out |
|---|
| 828 | $this->expired = true; |
|---|
| 829 | $this->status = AUTH_SECURITY_BREACH; |
|---|
| 830 | $this->logout(); |
|---|
| 831 | return false; |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | // Check challenge cookie here, if challengecookieold is not set |
|---|
| 835 | // this is the first time and check is skipped |
|---|
| 836 | // TODO when user open two pages similtaneuly (open in new window,open |
|---|
| 837 | // in tab) auth breach is caused find out a way around that if possible |
|---|
| 838 | if ( isset($this->session['challengecookieold']) |
|---|
| 839 | && $this->session['challengecookieold'] != $this->cookie['authchallenge']) { |
|---|
| 840 | $this->expired = true; |
|---|
| 841 | $this->status = AUTH_SECURITY_BREACH; |
|---|
| 842 | $this->logout(); |
|---|
| 843 | $this->login(); |
|---|
| 844 | return false; |
|---|
| 845 | } |
|---|
| 846 | } |
|---|
| 847 | |
|---|
| 848 | return true; |
|---|
| 849 | } |
|---|
| 850 | } |
|---|
| 851 | return false; |
|---|
| 852 | } |
|---|
| 853 | |
|---|
| 854 | // }}} |
|---|
| 855 | // {{{ staticCheckAuth() [static] |
|---|
| 856 | |
|---|
| 857 | /** |
|---|
| 858 | * Statically checks if there is a session with valid auth information. |
|---|
| 859 | * |
|---|
| 860 | * @access public |
|---|
| 861 | * @see checkAuth |
|---|
| 862 | * @return boolean Whether or not the user is authenticated. |
|---|
| 863 | * @static |
|---|
| 864 | */ |
|---|
| 865 | function staticCheckAuth($options = null) |
|---|
| 866 | { |
|---|
| 867 | static $staticAuth; |
|---|
| 868 | if(!isset($staticAuth)) { |
|---|
| 869 | $staticAuth = new Auth('null', $options); |
|---|
| 870 | } |
|---|
| 871 | return $staticAuth->checkAuth(); |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | // }}} |
|---|
| 875 | // {{{ getAuth() |
|---|
| 876 | |
|---|
| 877 | /** |
|---|
| 878 | * Has the user been authenticated? |
|---|
| 879 | * |
|---|
| 880 | * @access public |
|---|
| 881 | * @return bool True if the user is logged in, otherwise false. |
|---|
| 882 | */ |
|---|
| 883 | function getAuth() |
|---|
| 884 | { |
|---|
| 885 | return $this->checkAuth(); |
|---|
| 886 | } |
|---|
| 887 | |
|---|
| 888 | // }}} |
|---|
| 889 | // {{{ logout() |
|---|
| 890 | |
|---|
| 891 | /** |
|---|
| 892 | * Logout function |
|---|
| 893 | * |
|---|
| 894 | * This function clears any auth tokens in the currently |
|---|
| 895 | * active session and executes the logout callback function, |
|---|
| 896 | * if any |
|---|
| 897 | * |
|---|
| 898 | * @access public |
|---|
| 899 | * @return void |
|---|
| 900 | */ |
|---|
| 901 | function logout() |
|---|
| 902 | { |
|---|
| 903 | if (is_callable($this->logoutCallback)) { |
|---|
| 904 | call_user_func_array($this->logoutCallback, array($this->session['username'], &$this)); |
|---|
| 905 | } |
|---|
| 906 | |
|---|
| 907 | $this->username = ''; |
|---|
| 908 | $this->password = ''; |
|---|
| 909 | |
|---|
| 910 | $this->session = null; |
|---|
| 911 | } |
|---|
| 912 | |
|---|
| 913 | // }}} |
|---|
| 914 | // {{{ updateIdle() |
|---|
| 915 | |
|---|
| 916 | /** |
|---|
| 917 | * Update the idletime |
|---|
| 918 | * |
|---|
| 919 | * @access private |
|---|
| 920 | * @return void |
|---|
| 921 | */ |
|---|
| 922 | function updateIdle() |
|---|
| 923 | { |
|---|
| 924 | $this->session['idle'] = time(); |
|---|
| 925 | } |
|---|
| 926 | |
|---|
| 927 | // }}} |
|---|
| 928 | // {{{ getUsername() |
|---|
| 929 | |
|---|
| 930 | /** |
|---|
| 931 | * Get the username |
|---|
| 932 | * |
|---|
| 933 | * @return string |
|---|
| 934 | * @access public |
|---|
| 935 | */ |
|---|
| 936 | function getUsername() |
|---|
| 937 | { |
|---|
| 938 | if (isset($this->session['username'])) { |
|---|
| 939 | return($this->session['username']); |
|---|
| 940 | } |
|---|
| 941 | return(''); |
|---|
| 942 | } |
|---|
| 943 | |
|---|
| 944 | // }}} |
|---|
| 945 | // {{{ getStatus() |
|---|
| 946 | |
|---|
| 947 | /** |
|---|
| 948 | * Get the current status |
|---|
| 949 | * |
|---|
| 950 | * @return string |
|---|
| 951 | * @access public |
|---|
| 952 | */ |
|---|
| 953 | function getStatus() |
|---|
| 954 | { |
|---|
| 955 | return $this->status; |
|---|
| 956 | } |
|---|
| 957 | |
|---|
| 958 | // }}} |
|---|
| 959 | // {{{ getPostUsernameField() |
|---|
| 960 | |
|---|
| 961 | /** |
|---|
| 962 | * Gets the post varible used for the username |
|---|
| 963 | * |
|---|
| 964 | * @return string |
|---|
| 965 | * @access public |
|---|
| 966 | */ |
|---|
| 967 | function getPostUsernameField() |
|---|
| 968 | { |
|---|
| 969 | return($this->_postUsername); |
|---|
| 970 | } |
|---|
| 971 | |
|---|
| 972 | // }}} |
|---|
| 973 | // {{{ getPostPasswordField() |
|---|
| 974 | |
|---|
| 975 | /** |
|---|
| 976 | * Gets the post varible used for the username |
|---|
| 977 | * |
|---|
| 978 | * @return string |
|---|
| 979 | * @access public |
|---|
| 980 | */ |
|---|
| 981 | function getPostPasswordField() |
|---|
| 982 | { |
|---|
| 983 | return($this->_postPassword); |
|---|
| 984 | } |
|---|
| 985 | |
|---|
| 986 | // }}} |
|---|
| 987 | // {{{ sessionValidThru() |
|---|
| 988 | |
|---|
| 989 | /** |
|---|
| 990 | * Returns the time up to the session is valid |
|---|
| 991 | * |
|---|
| 992 | * @access public |
|---|
| 993 | * @return integer |
|---|
| 994 | */ |
|---|
| 995 | function sessionValidThru() |
|---|
| 996 | { |
|---|
| 997 | if (!isset($this->session['idle'])) { |
|---|
| 998 | return 0; |
|---|
| 999 | } |
|---|
| 1000 | if ($this->idle == 0) { |
|---|
| 1001 | return 0; |
|---|
| 1002 | } |
|---|
| 1003 | return ($this->session['idle'] + $this->idle); |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | // }}} |
|---|
| 1007 | // {{{ listUsers() |
|---|
| 1008 | |
|---|
| 1009 | /** |
|---|
| 1010 | * List all users that are currently available in the storage |
|---|
| 1011 | * container |
|---|
| 1012 | * |
|---|
| 1013 | * @access public |
|---|
| 1014 | * @return array |
|---|
| 1015 | */ |
|---|
| 1016 | function listUsers() |
|---|
| 1017 | { |
|---|
| 1018 | $this->_loadStorage(); |
|---|
| 1019 | return $this->storage->listUsers(); |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| 1022 | // }}} |
|---|
| 1023 | // {{{ addUser() |
|---|
| 1024 | |
|---|
| 1025 | /** |
|---|
| 1026 | * Add user to the storage container |
|---|
| 1027 | * |
|---|
| 1028 | * @access public |
|---|
| 1029 | * @param string Username |
|---|
| 1030 | * @param string Password |
|---|
| 1031 | * @param mixed Additional parameters |
|---|
| 1032 | * @return mixed True on success, PEAR error object on error |
|---|
| 1033 | * and AUTH_METHOD_NOT_SUPPORTED otherwise. |
|---|
| 1034 | */ |
|---|
| 1035 | function addUser($username, $password, $additional = '') |
|---|
| 1036 | { |
|---|
| 1037 | $this->_loadStorage(); |
|---|
| 1038 | return $this->storage->addUser($username, $password, $additional); |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | // }}} |
|---|
| 1042 | // {{{ removeUser() |
|---|
| 1043 | |
|---|
| 1044 | /** |
|---|
| 1045 | * Remove user from the storage container |
|---|
| 1046 | * |
|---|
| 1047 | * @access public |
|---|
| 1048 | * @param string Username |
|---|
| 1049 | * @return mixed True on success, PEAR error object on error |
|---|
| 1050 | * and AUTH_METHOD_NOT_SUPPORTED otherwise. |
|---|
| 1051 | */ |
|---|
| 1052 | function removeUser($username) |
|---|
| 1053 | { |
|---|
| 1054 | $this->_loadStorage(); |
|---|
| 1055 | return $this->storage->removeUser($username); |
|---|
| 1056 | } |
|---|
| 1057 | |
|---|
| 1058 | // }}} |
|---|
| 1059 | // {{{ changePassword() |
|---|
| 1060 | |
|---|
| 1061 | /** |
|---|
| 1062 | * Change password for user in the storage container |
|---|
| 1063 | * |
|---|
| 1064 | * @access public |
|---|
| 1065 | * @param string Username |
|---|
| 1066 | * @param string The new password |
|---|
| 1067 | * @return mixed True on success, PEAR error object on error |
|---|
| 1068 | * and AUTH_METHOD_NOT_SUPPORTED otherwise. |
|---|
| 1069 | */ |
|---|
| 1070 | function changePassword($username, $password) |
|---|
| 1071 | { |
|---|
| 1072 | $this->_loadStorage(); |
|---|
| 1073 | return $this->storage->changePassword($username, $password); |
|---|
| 1074 | } |
|---|
| 1075 | |
|---|
| 1076 | // }}} |
|---|
| 1077 | |
|---|
| 1078 | } |
|---|
| 1079 | ?> |
|---|