source-function-authenticate_and_cache

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: /*********************************************************************
  3:  *
  4:  * Pure PHP radius class, WWW Authentication file to be required
  5:  *
  6:  * This file must be required before displaying any protected page.
  7:  * This file should be prepended automatically using the
  8:  *   PHP auto_prepend_file directive in a .htaccess file.
  9:  *
 10:  *
 11:  * LICENCE
 12:  *
 13: 
 14: 
 15:  *   (http://www.sysco.ch/)
 16:  *   All rights reserved.
 17:  *
 18:  *   This file is part of the Pure PHP radius class
 19:  *
 20:  *   Pure PHP radius class is free software; you can redistribute it and/or
 21:  *   modify it under the terms of the GNU Lesser General Public License as
 22:  *   published by the Free Software Foundation, either version 3 of the License,
 23:  *   or (at your option) any later version.
 24:  *
 25:  *   Pure PHP radius class is distributed in the hope that it will be useful,
 26:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 27:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 28:  *   GNU Lesser General Public License for more details.
 29:  *
 30:  *   You should have received a copy of the GNU Lesser General Public
 31:  *   License along with Pure PHP radius class.
 32:  *   If not, see <http://www.gnu.org/licenses/>.
 33:  *
 34:  *
 35:  * @author: SysCo/al
 36:  * @since CreationDate: 2008-01-06
 37: 
 38:  * @version $LastChangedRevision: 1.0 $
 39:  * @version $LastChangedDate: 2008-01-07 $
 40:  * @version $LastChangedBy: SysCo/al $
 41:  * @link $HeadURL: radius.www.authenticate.php $
 42:  * @link http://developer.sysco.ch/php/
 43:  * @link developer@sysco.ch
 44:  * Language: PHP 4.0.7 or higher
 45:  *
 46:  *
 47:  * Usage
 48:  *
 49:  *   require_once("radius.www.authenticate.php");
 50:  *   echo "User <strong>".$_SERVER['PHP_AUTH_USER']."</strong> authenticated.";
 51:  *
 52:  *
 53:  * External file needed
 54:  *
 55:  *   radius.class.php
 56:  *
 57:  *
 58:  * External file created
 59:  *
 60:  *   none.
 61:  *
 62:  *
 63:  * Change Log
 64:  *
 65:  *   2008-01-07 1.0   SysCo/al Initial release
 66:  *
 67:  *********************************************************************/
 68: 
 69: // 2010-08-09 Nicola Asuni: Code clean-up
 70: 
 71: require_once('radius.class.php');
 72: 
 73: function authenticate_and_cache($ip_radius_server, $shared_secret, $username, $password, $timeout=900) {
 74:     $result = FALSE;
 75:     $cache_unique_id = (isset($_SESSION['authentication_unique_id']) ? $_SESSION['authentication_unique_id'] : '');
 76:     if ($cache_unique_id != '') {
 77:         $cache_timestamp= $_SESSION[$cache_unique_id.'_authentication_timestamp'];
 78:         $cache_remote_addr = $_SESSION[$cache_unique_id.'_authentication_remote_addr'];
 79:         $cache_username = $_SESSION[$cache_unique_id.'_authentication_username'];
 80:     }
 81:     if (($cache_timestamp == 0) OR (($cache_timestamp + $timeout) < time()) OR ($cache_remote_addr != $_SERVER['REMOTE_ADDR']) OR ($cache_username != $username)) {
 82:         $radius = new Radius($ip_radius_server, $shared_secret);
 83:         $radius->SetDebugMode($php_debug_mode);
 84:         $result = $radius->AccessRequest($username, $password);
 85:         if ($result === TRUE) {
 86:             if ($cache_unique_id == '') {
 87:                 $cache_unique_id = md5(uniqid(rand(), true));
 88:             }
 89:             $_SESSION['authentication_unique_id'] = $cache_unique_id;
 90:             $_SESSION[$cache_unique_id.'_authentication_timestamp'] = time();
 91:             $_SESSION[$cache_unique_id.'_authentication_remote_addr'] = $_SERVER['REMOTE_ADDR'];
 92:             $_SESSION[$cache_unique_id.'_authentication_username'] = $username;
 93:         } else {
 94:             $_SESSION['authentication_unique_id'] = '';
 95:         }
 96:     } else {
 97:         $_SESSION[$cache_unique_id.'_authentication_timestamp'] = time();
 98:         $result = TRUE;
 99:     }
100:     return $result;
101: }
102: 
103: // Start session if not already done
104: if (session_id() == '') {
105:     session_start();
106: }
107: 
108: $php_auth_user = $_SERVER['PHP_AUTH_USER'];
109: $php_auth_pw = $_SERVER['PHP_AUTH_PW'];
110: $php_auth_realm = (isset($custom_auth_realm)?$custom_auth_realm:$_SERVER['SERVER_NAME']);
111: $php_auth_timeout = (isset($custom_auth_timeout)?$custom_auth_timeout:(15*60));
112: $php_ip_radius_server = (isset($custom_ip_radius_server)?$custom_ip_radius_server:'');
113: $php_shared_secret = (isset($custom_shared_secret)?$custom_shared_secret:'');
114: $php_debug_mode = (isset($custom_debug_mode)?(TRUE === $custom_debug_mode):FALSE);
115: 
116: if (('' == $php_auth_user) OR (!authenticate_and_cache($php_ip_radius_server, $php_shared_secret, $php_auth_user, $php_auth_pw, $php_auth_timeout))) {
117:     header('HTTP/1.0 401 Unauthorized');
118:     header('WWW-Authenticate: Basic realm="'.$php_auth_realm.'"');
119:     echo '<html>';
120:     echo '<head><title>401 Unauthorized access</title></head>';
121:     echo '<body>';
122:     echo '<h1>401 Unauthorized access</h1>';
123:     echo '<br />';
124:     echo 'You must login using your username and your password.';
125:     echo '</body>';
126:     echo '</html>';
127:     exit;
128: }
129: 
130: //============================================================+
131: // END OF FILE
132: //============================================================+
133: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy