[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[136] | 24 | * Auth_File.inc.php |
---|
| 25 | * |
---|
| 26 | * The Auth_File class provides a htpasswd file implementation for |
---|
[1] | 27 | * authentication. |
---|
| 28 | * |
---|
| 29 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[103] | 30 | * @version 1.2 |
---|
[1] | 31 | */ |
---|
[103] | 32 | |
---|
| 33 | // Usage example: |
---|
| 34 | // $auth = new Auth_File(); |
---|
| 35 | // $auth->setParam(array( |
---|
| 36 | // 'htpasswd_file' => COMMON_BASE . '/global/site_users.htpasswd', |
---|
| 37 | // 'login_timeout' => 21600, |
---|
| 38 | // 'idle_timeout' => 3600, |
---|
| 39 | // 'login_url' => '/login.php' |
---|
| 40 | // )); |
---|
[65] | 41 | |
---|
| 42 | // Available encryption types for class Auth_SQL. |
---|
| 43 | define('AUTH_ENCRYPT_MD5', 'md5'); |
---|
| 44 | define('AUTH_ENCRYPT_CRYPT', 'crypt'); |
---|
| 45 | define('AUTH_ENCRYPT_SHA1', 'sha1'); |
---|
| 46 | define('AUTH_ENCRYPT_PLAINTEXT', 'plaintext'); |
---|
| 47 | |
---|
[1] | 48 | class Auth_File { |
---|
[136] | 49 | |
---|
| 50 | // Namespace of this auth object. |
---|
| 51 | var $_ns; |
---|
| 52 | |
---|
| 53 | // Parameters to be specified by setParam(). |
---|
[103] | 54 | var $_params = array(); |
---|
| 55 | var $_default_params = array( |
---|
| 56 | |
---|
| 57 | // Full path to htpasswd file. |
---|
| 58 | 'htpasswd_file' => null, |
---|
| 59 | |
---|
| 60 | // The type of encryption to use for passwords stored in the db_table. Use one of the AUTH_ENCRYPT_* types specified above. |
---|
[65] | 61 | 'encryption_type' => AUTH_ENCRYPT_CRYPT, |
---|
[103] | 62 | |
---|
| 63 | // The URL to the login script. |
---|
| 64 | 'login_url' => '/', |
---|
| 65 | |
---|
| 66 | // The maximum amount of time a user is allowed to be logged in. They will be forced to login again if they expire. |
---|
| 67 | // This applies to admins and users. In seconds. 21600 seconds = 6 hours. |
---|
| 68 | 'login_timeout' => 21600, |
---|
| 69 | |
---|
| 70 | // The maximum amount of time a user is allowed to be idle before their session expires. They will be forced to login again if they expire. |
---|
| 71 | // This applies to admins and users. In seconds. 3600 seconds = 1 hour. |
---|
| 72 | 'idle_timeout' => 3600, |
---|
| 73 | |
---|
| 74 | // An array of IP blocks that are bypass the remote_ip comparison check. Useful for dynamic IPs or those behind proxy servers. |
---|
| 75 | 'trusted_networks' => array(), |
---|
[1] | 76 | ); |
---|
[103] | 77 | |
---|
| 78 | // Associative array of usernames to hashed passwords. |
---|
[65] | 79 | var $_users = array(); |
---|
[1] | 80 | |
---|
| 81 | /** |
---|
| 82 | * Constructs a new htpasswd authentication object. |
---|
| 83 | * |
---|
| 84 | * @access public |
---|
| 85 | * |
---|
| 86 | * @param optional array $params A hash containing parameters. |
---|
| 87 | */ |
---|
[158] | 88 | function Auth_File($namespace='') |
---|
[1] | 89 | { |
---|
[154] | 90 | $this->_ns = $namespace; |
---|
[1] | 91 | |
---|
[103] | 92 | // Initialize default parameters. |
---|
| 93 | $this->setParam($this->_default_params); |
---|
[1] | 94 | } |
---|
| 95 | |
---|
| 96 | /** |
---|
[65] | 97 | * Set the params of an auth object. |
---|
| 98 | * |
---|
| 99 | * @param array $params Array of parameter keys and value to set. |
---|
| 100 | * @return bool true on success, false on failure |
---|
| 101 | */ |
---|
| 102 | function setParam($params) |
---|
| 103 | { |
---|
| 104 | if (isset($params) && is_array($params)) { |
---|
| 105 | // Merge new parameters with old overriding only those passed. |
---|
| 106 | $this->_params = array_merge($this->_params, $params); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * Return the value of a parameter, if it exists. |
---|
| 112 | * |
---|
| 113 | * @access public |
---|
| 114 | * @param string $param Which parameter to return. |
---|
| 115 | * @return mixed Configured parameter value. |
---|
| 116 | */ |
---|
| 117 | function getParam($param) |
---|
| 118 | { |
---|
[136] | 119 | $app =& App::getInstance(); |
---|
| 120 | |
---|
[65] | 121 | if (isset($this->_params[$param])) { |
---|
| 122 | return $this->_params[$param]; |
---|
| 123 | } else { |
---|
[146] | 124 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
[65] | 125 | return null; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * Clear any authentication tokens in the current session. A.K.A. logout. |
---|
| 131 | * |
---|
| 132 | * @access public |
---|
| 133 | */ |
---|
[149] | 134 | function clear() |
---|
[65] | 135 | { |
---|
[154] | 136 | $_SESSION['_auth_file'][$this->_ns] = array('authenticated' => false); |
---|
[65] | 137 | } |
---|
| 138 | |
---|
[103] | 139 | |
---|
[65] | 140 | /** |
---|
[103] | 141 | * Sets a variable into a registered auth session. |
---|
| 142 | * |
---|
| 143 | * @access public |
---|
| 144 | * @param mixed $key Which value to set. |
---|
| 145 | * @param mixed $val Value to set variable to. |
---|
| 146 | */ |
---|
[148] | 147 | function set($key, $val) |
---|
[103] | 148 | { |
---|
[154] | 149 | if (!isset($_SESSION['_auth_file'][$this->_ns]['user_data'])) { |
---|
| 150 | $_SESSION['_auth_file'][$this->_ns]['user_data'] = array(); |
---|
[103] | 151 | } |
---|
[154] | 152 | $_SESSION['_auth_file'][$this->_ns]['user_data'][$key] = $val; |
---|
[103] | 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Returns a specified value from a registered auth session. |
---|
| 157 | * |
---|
| 158 | * @access public |
---|
| 159 | * @param mixed $key Which value to return. |
---|
| 160 | * @param mixed $default Value to return if key not found in user_data. |
---|
| 161 | * @return mixed Value stored in session. |
---|
| 162 | */ |
---|
[148] | 163 | function get($key, $default='') |
---|
[103] | 164 | { |
---|
[154] | 165 | if (isset($_SESSION['_auth_file'][$this->_ns][$key])) { |
---|
| 166 | return $_SESSION['_auth_file'][$this->_ns][$key]; |
---|
| 167 | } else if (isset($_SESSION['_auth_file'][$this->_ns]['user_data'][$key])) { |
---|
| 168 | return $_SESSION['_auth_file'][$this->_ns]['user_data'][$key]; |
---|
[103] | 169 | } else { |
---|
| 170 | return $default; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | /** |
---|
[1] | 174 | * Find out if a set of login credentials are valid. Only supports |
---|
| 175 | * htpasswd files with DES passwords right now. |
---|
| 176 | * |
---|
[65] | 177 | * @access public |
---|
[1] | 178 | * |
---|
[65] | 179 | * @param string $username The username to check. |
---|
| 180 | * @param array $password The password to compare to username. |
---|
[1] | 181 | * |
---|
| 182 | * @return boolean Whether or not the credentials are valid. |
---|
| 183 | */ |
---|
[65] | 184 | function authenticate($username, $password) |
---|
[1] | 185 | { |
---|
[136] | 186 | $app =& App::getInstance(); |
---|
| 187 | |
---|
[65] | 188 | if ('' == trim($password)) { |
---|
[136] | 189 | $app->logMsg(_("No password provided for authentication."), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 190 | return false; |
---|
| 191 | } |
---|
[103] | 192 | |
---|
| 193 | // Load users file. |
---|
| 194 | $this->_loadHTPasswdFile(); |
---|
[1] | 195 | |
---|
[65] | 196 | if (!isset($this->_users[$username])) { |
---|
[136] | 197 | $app->logMsg(_("User ID provided does not exist."), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 198 | return false; |
---|
| 199 | } |
---|
| 200 | |
---|
[103] | 201 | if ($this->_encrypt($password, $this->_users[$username]) != $this->_users[$username]) { |
---|
[136] | 202 | $app->logMsg(sprintf('Authentication failed for user %s', $username), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 203 | return false; |
---|
| 204 | } |
---|
[103] | 205 | |
---|
| 206 | // Authentication successful! |
---|
| 207 | return true; |
---|
[1] | 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * If user passes authentication create authenticated session. |
---|
| 212 | * |
---|
[65] | 213 | * @access public |
---|
[1] | 214 | * |
---|
[65] | 215 | * @param string $username The username to check. |
---|
| 216 | * @param array $password The password to compare to username. |
---|
[1] | 217 | * |
---|
| 218 | * @return boolean Whether or not the credentials are valid. |
---|
| 219 | */ |
---|
[65] | 220 | function login($username, $password) |
---|
[1] | 221 | { |
---|
[247] | 222 | $username = mb_strtolower(trim($username)); |
---|
[42] | 223 | |
---|
[149] | 224 | $this->clear(); |
---|
[1] | 225 | |
---|
[103] | 226 | if (!$this->authenticate($username, $password)) { |
---|
| 227 | // No login: failed authentication! |
---|
| 228 | return false; |
---|
[1] | 229 | } |
---|
[103] | 230 | |
---|
[154] | 231 | $_SESSION['_auth_file'][$this->_ns] = array( |
---|
[103] | 232 | 'authenticated' => true, |
---|
| 233 | 'username' => $username, |
---|
| 234 | 'login_datetime' => date('Y-m-d H:i:s'), |
---|
| 235 | 'last_access_datetime' => date('Y-m-d H:i:s'), |
---|
| 236 | 'remote_ip' => getRemoteAddr() |
---|
| 237 | ); |
---|
| 238 | |
---|
| 239 | // We're logged-in! |
---|
| 240 | return true; |
---|
[1] | 241 | } |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * Test if user has a currently logged-in session. |
---|
| 245 | * - authentication flag set to true |
---|
[65] | 246 | * - username not empty |
---|
[1] | 247 | * - total logged-in time is not greater than login_timeout |
---|
| 248 | * - idle time is not greater than idle_timeout |
---|
| 249 | * - remote address is the same as the login remote address. |
---|
| 250 | * |
---|
| 251 | * @access public |
---|
| 252 | */ |
---|
| 253 | function isLoggedIn() |
---|
| 254 | { |
---|
[136] | 255 | $app =& App::getInstance(); |
---|
| 256 | |
---|
[103] | 257 | // Some users will access from networks with a changing IP number (i.e. behind a proxy server). These users must be allowed entry by adding their IP to the list of trusted_networks. |
---|
| 258 | if ($trusted_net = ipInRange(getRemoteAddr(), $this->_params['trusted_networks'])) { |
---|
| 259 | $user_in_trusted_network = true; |
---|
[154] | 260 | $app->logMsg(sprintf('User %s accessing from trusted network %s', $_SESSION['_auth_file'][$this->_ns]['username'], $trusted_net), LOG_DEBUG, __FILE__, __LINE__); |
---|
[103] | 261 | } else if (preg_match('/proxy.aol.com$/i', getRemoteAddr(true))) { |
---|
| 262 | $user_in_trusted_network = true; |
---|
[154] | 263 | $app->logMsg(sprintf('User %s accessing from trusted network proxy.aol.com', $_SESSION['_auth_file'][$this->_ns]['username']), LOG_DEBUG, __FILE__, __LINE__); |
---|
[103] | 264 | } else { |
---|
| 265 | $user_in_trusted_network = false; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | // Test login with information stored in session. Skip IP matching for users from trusted networks. |
---|
[154] | 269 | if (isset($_SESSION['_auth_file'][$this->_ns]) |
---|
| 270 | && true === $_SESSION['_auth_file'][$this->_ns]['authenticated'] |
---|
| 271 | && !empty($_SESSION['_auth_file'][$this->_ns]['username']) |
---|
| 272 | && strtotime($_SESSION['_auth_file'][$this->_ns]['login_datetime']) > time() - $this->_params['login_timeout'] |
---|
| 273 | && strtotime($_SESSION['_auth_file'][$this->_ns]['last_access_datetime']) > time() - $this->_params['idle_timeout'] |
---|
| 274 | && ($_SESSION['_auth_file'][$this->_ns]['remote_ip'] == getRemoteAddr() || $user_in_trusted_network) |
---|
[103] | 275 | ) { |
---|
| 276 | // User is authenticated! |
---|
[154] | 277 | $_SESSION['_auth_file'][$this->_ns]['last_access_datetime'] = date('Y-m-d H:i:s'); |
---|
[103] | 278 | return true; |
---|
[154] | 279 | } else if (isset($_SESSION['_auth_file'][$this->_ns]) && true === $_SESSION['_auth_file'][$this->_ns]['authenticated']) { |
---|
| 280 | if (strtotime($_SESSION['_auth_file'][$this->_ns]['last_access_datetime']) > time() - 43200) { |
---|
[103] | 281 | // Only raise message if last session is less than 12 hours old. |
---|
[136] | 282 | $app->raiseMsg(_("Your session has closed. You need to log-in again."), MSG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 283 | } |
---|
[103] | 284 | |
---|
| 285 | // Log the reason for login expiration. |
---|
| 286 | $expire_reasons = array(); |
---|
[154] | 287 | if (empty($_SESSION['_auth_file'][$this->_ns]['username'])) { |
---|
[103] | 288 | $expire_reasons[] = 'username not found'; |
---|
| 289 | } |
---|
[154] | 290 | if (strtotime($_SESSION['_auth_file'][$this->_ns]['login_datetime']) <= time() - $this->_params['login_timeout']) { |
---|
[103] | 291 | $expire_reasons[] = 'login_timeout expired'; |
---|
| 292 | } |
---|
[154] | 293 | if (strtotime($_SESSION['_auth_file'][$this->_ns]['last_access_datetime']) <= time() - $this->_params['idle_timeout']) { |
---|
[103] | 294 | $expire_reasons[] = 'idle_timeout expired'; |
---|
| 295 | } |
---|
[154] | 296 | if ($_SESSION['_auth_file'][$this->_ns]['remote_ip'] != getRemoteAddr() && !$user_in_trusted_network) { |
---|
| 297 | $expire_reasons[] = sprintf('remote_ip not matched (%s != %s)', $_SESSION['_auth_file'][$this->_ns]['remote_ip'], getRemoteAddr()); |
---|
[103] | 298 | } |
---|
[154] | 299 | $app->logMsg(sprintf('User %s session expired: %s', $_SESSION['_auth_file'][$this->_ns]['username'], join(', ', $expire_reasons)), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 300 | } |
---|
[103] | 301 | |
---|
[1] | 302 | return false; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /** |
---|
[66] | 306 | * Redirect user to login page if they are not logged in. |
---|
| 307 | * |
---|
| 308 | * @param string $message The text description of a message to raise. |
---|
| 309 | * @param int $type The type of message: MSG_NOTICE, |
---|
| 310 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
| 311 | * @param string $file __FILE__. |
---|
| 312 | * @param string $line __LINE__. |
---|
| 313 | * @access public |
---|
| 314 | */ |
---|
| 315 | function requireLogin($message='', $type=MSG_NOTICE, $file=null, $line=null) |
---|
| 316 | { |
---|
[136] | 317 | $app =& App::getInstance(); |
---|
| 318 | |
---|
[66] | 319 | if (!$this->isLoggedIn()) { |
---|
[103] | 320 | // Display message for requiring login. (RaiseMsg will ignore empty strings.) |
---|
[136] | 321 | $app->raiseMsg($message, $type, $file, $line); |
---|
[66] | 322 | |
---|
| 323 | // Login scripts must have the same 'login' tag for boomerangURL verification/manipulation. |
---|
[136] | 324 | $app->setBoomerangURL(absoluteMe(), 'login'); |
---|
| 325 | $app->dieURL($this->_params['login_url']); |
---|
[66] | 326 | } |
---|
| 327 | } |
---|
[103] | 328 | |
---|
[209] | 329 | /** |
---|
[334] | 330 | * Wrapper function for compatibility with lib/Lock.inc.php. |
---|
[209] | 331 | * |
---|
| 332 | * @param string $username Username to return. |
---|
| 333 | * @return string Username, or false if none found. |
---|
| 334 | */ |
---|
| 335 | function getUsername($username) |
---|
| 336 | { |
---|
| 337 | if ('' != $username) { |
---|
| 338 | return $username; |
---|
| 339 | } else { |
---|
| 340 | return false; |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | |
---|
[103] | 344 | /* |
---|
| 345 | * Reads the configured htpasswd file into the _users array. |
---|
| 346 | * |
---|
| 347 | * @access public |
---|
| 348 | * @return false on error, true on success. |
---|
| 349 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 350 | * @version 1.0 |
---|
| 351 | * @since 18 Apr 2006 18:17:48 |
---|
| 352 | */ |
---|
| 353 | function _loadHTPasswdFile() |
---|
| 354 | { |
---|
[136] | 355 | $app =& App::getInstance(); |
---|
| 356 | |
---|
[103] | 357 | static $users = null; |
---|
| 358 | |
---|
| 359 | if (!file_exists($this->_params['htpasswd_file'])) { |
---|
[136] | 360 | $app->logMsg(sprintf('htpasswd file missing or not specified: %s', $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__); |
---|
[103] | 361 | return false; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | if (!isset($users)) { |
---|
| 365 | if (false === ($users = file($this->_params['htpasswd_file']))) { |
---|
[141] | 366 | $app->logMsg(sprintf('Could not read htpasswd file: %s', $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__); |
---|
[103] | 367 | return false; |
---|
| 368 | } |
---|
| 369 | } |
---|
[66] | 370 | |
---|
[103] | 371 | if (is_array($users)) { |
---|
| 372 | foreach ($users as $u) { |
---|
| 373 | list($user, $pass) = explode(':', $u, 2); |
---|
| 374 | $this->_users[trim($user)] = trim($pass); |
---|
| 375 | } |
---|
| 376 | return true; |
---|
| 377 | } |
---|
| 378 | return false; |
---|
| 379 | } |
---|
| 380 | |
---|
[66] | 381 | /** |
---|
[1] | 382 | * Hash a given password according to the configured encryption |
---|
| 383 | * type. |
---|
| 384 | * |
---|
[65] | 385 | * @param string $password The password to encrypt. |
---|
| 386 | * @param string $encrypted_password The currently encrypted password to use as salt, if needed. |
---|
[1] | 387 | * |
---|
| 388 | * @return string The hashed password. |
---|
| 389 | */ |
---|
[65] | 390 | function _encrypt($password, $encrypted_password=null) |
---|
[1] | 391 | { |
---|
| 392 | switch ($this->_params['encryption_type']) { |
---|
[65] | 393 | case AUTH_ENCRYPT_PLAINTEXT : |
---|
| 394 | return $password; |
---|
[1] | 395 | break; |
---|
| 396 | |
---|
[65] | 397 | case AUTH_ENCRYPT_SHA1 : |
---|
| 398 | return sha1($password); |
---|
| 399 | break; |
---|
[1] | 400 | |
---|
[65] | 401 | case AUTH_ENCRYPT_MD5 : |
---|
| 402 | return md5($password); |
---|
[1] | 403 | break; |
---|
[65] | 404 | |
---|
| 405 | case AUTH_ENCRYPT_CRYPT : |
---|
[42] | 406 | default : |
---|
[65] | 407 | return crypt($password, $encrypted_password); |
---|
| 408 | break; |
---|
[1] | 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
[65] | 412 | } // end class |
---|
[1] | 413 | ?> |
---|