Advertisement
Guest User

class.bertasecurity.php

a guest
Jul 16th, 2010
1,566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.45 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  ==================================================================================================================================
  5.    
  6.     CLASS BertaSecurity
  7.    
  8.     Manages security and ip-tracking operations:
  9.          (*) Log-in and log-out                
  10.          (*) Session-based authentification    
  11.          (*) Protected areas                   
  12.    
  13.  ==================================================================================================================================
  14. */
  15.  
  16. class BertaSecurity {
  17.    
  18.     const BERTASECURITY_ERROR_SESSION_VARIABLE = 1;     // session variable corrupt
  19.     const BERTASECURITY_ERROR_SESSION_EXPIRED = 2;          // session expired
  20.     const BERTASECURITY_ERROR_SESSION_IP_CONFLICT = 3;      // ip address has changed
  21.     const BERTASECURITY_ERROR_LOGIN_VARIABLE = 4;           // login variables corrupt or empty
  22.     const BERTASECURITY_ERROR_LOGIN_INCORRECT = 5;          // login user and password incorrect
  23.    
  24.     public $authExpiresSeconds; // session idle time
  25.     public $authUseAuthentification = false;
  26.    
  27.     public $authentificated = false; // if
  28.     public $userLoggedIn = false;
  29.    
  30.     public $user;               // array of all user information available in the database (id, ident, nick, email, etc.)
  31.    
  32.     public $accessIP;               // array containing ip address by bytes
  33.     public $accessIPStr = '';
  34.    
  35.     public $errAuth = 0;    // the reason (id), why autentification failed;
  36.     public $errLogin = 0;   // the reason (id), why login failed;
  37.    
  38.     public function BertaSecurity($authEnvironment = 'site', $authExpiresSeconds = 21600) {
  39.         $this->authExpiresSeconds = $authExpiresSeconds;
  40.         $this->authUseAuthentification = true;
  41.        
  42.         $this->authentificated = $this->authUseAuthentification ? $this->authentificate() : true;
  43.        
  44.        
  45.         // todo - change relying on userLoggedIn to a new environment variable
  46.         if($authEnvironment == 'site') {
  47.             $this->userLoggedIn = false;
  48.         }
  49.     }
  50.    
  51.    
  52.     public function getAccessIP() {
  53.         $this->accessIPStr = $_SERVER["REMOTE_ADDR"];
  54.         if(preg_match("/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/", $this->accessIPStr, $ipRegs)) {
  55.             $this->accessIP = array((int) $ipRegs[1],
  56.                                     (int) $ipRegs[2],
  57.                                     (int) $ipRegs[3],
  58.                                     (int) $ipRegs[4]);
  59.             return $this->accessIP;
  60.         } else
  61.             return false;
  62.     }
  63.    
  64.    
  65.    
  66.    
  67.     // ------------------------------------------------------------------------------------------------------------------------------
  68.     // --    Login and authentification    ------------------------------------------------------------------------------------------
  69.     // ------------------------------------------------------------------------------------------------------------------------------
  70.    
  71.    
  72.     public function authentificate() {
  73.         //echo Berta::$options['SITE_ABS_ROOT'];
  74.         session_name(BertaUtils::canonizeString('berta_' . Berta::$options['version'] . '_' . Berta::$options['SITE_ABS_ROOT'], '_', ''));
  75.         session_start();
  76.         $curTime = time();
  77.        
  78.         //var_dump($_SESSION);
  79.        
  80.         //echo $curTime - $_SESSION['_berta__user']['last_access'];
  81.        
  82.         if(isset($_SESSION['_berta__user']) && is_array($_SESSION['_berta__user'])) {
  83.             if(($curTime - $_SESSION['_berta__user']['last_access'] <= $this->authExpiresSeconds)) {
  84.                 if($_SESSION['_berta__user']['last_ip'] == $_SERVER['REMOTE_ADDR']) {
  85.                    
  86.                     $_SESSION['_berta__user']['last_access'] = $curTime;
  87.                     $this->user = $_SESSION['_berta__user'];
  88.                     $this->userLoggedIn = true;
  89.                    
  90.                     if(!empty($_REQUEST['_security_reload_user']))
  91.                         $this->updateUserSettings($this->user);
  92.                        
  93.                     return $this->userLoggedIn = true;
  94.                    
  95.                 } else {
  96.                     $this->destroy(self::BERTASECURITY_ERROR_SESSION_IP_CONFLICT); // ip conflict
  97.                     return $this->userLoggedIn = false;
  98.                 }
  99.             } else {
  100.                 $this->destroy(self::BERTASECURITY_ERROR_SESSION_EXPIRED);
  101.                 return $this->userLoggedIn = false;
  102.             }
  103.        
  104.         } elseif(isset($_SESSION['_berta__user']) && !is_array($_SESSION['_berta__user'])) {
  105.             $this->destroy(self::BERTASECURITY_ERROR_SESSION_VARIABLE);
  106.             return $this->userLoggedIn = false;
  107.        
  108.         } else {
  109.             return $this->userLoggedIn = false;
  110.         }
  111.     }
  112.    
  113.    
  114.    
  115.     public function goToLoginPage($loginPageRelativeURL) {
  116.         $qS = $this->errAuth ? "?autherror=" . $this->errAuth : "";
  117.         if(headers_sent()) {
  118.             echo '<script language="javascript" type="text/javascript">window.location="' . $loginPageRelativeURL . $qS . '";</script>';
  119.             echo '<p>Please wait... (or <a href="' . $loginPageRelativeURL . $qS . '">click here</a> if nothing happens)</p>';
  120.         } else
  121.             header('Location: ' . $loginPageRelativeURL . $qS);
  122.        
  123.         exit;
  124.     }
  125.    
  126.    
  127.     public function login($name, $pass, $realName, $realPass) {
  128.         if($name && $pass) {
  129.             if($name == $realName && $pass == $realPass) {
  130.                 $this->destroy();
  131.                 session_start();
  132.                 $this->updateUserSettings(array('name' => $realName));
  133.                
  134.                 return $this->userLoggedIn = true;
  135.                
  136.             } else {
  137.                 $this->errLogin = self::BERTASECURITY_ERROR_LOGIN_INCORRECT;    // wrong creditentials
  138.                 return false;
  139.             }
  140.    
  141.         } else {
  142.             $this->errLogin = self::BERTASECURITY_ERROR_LOGIN_VARIABLE; // no identification supplied
  143.             return false;
  144.         }
  145.        
  146.     }
  147.    
  148.    
  149.     public function destroy($authErrNo = false) {
  150.         if(isset($_SESSION['_berta__user'])) unset($_SESSION['_berta__user']);
  151.         @session_destroy();
  152.         $this->user = array();
  153.         //echo $authErrNo;
  154.        
  155.         return true;
  156.     }
  157.    
  158.    
  159.     public function updateUserSettings($user = false) {
  160.        
  161.         if(isset($user["last_access_sec"])) $this->user["prev_access"] = $user["last_access_sec"];
  162.         if(isset($user["last_ip"])) $this->user["prev_ip"] = $user["last_ip"];
  163.         $this->user = array_merge($user, array(
  164.                           "user_name" => $user["name"] ? $user["name"] : $user['nickname'],
  165.                           "login_time" => time(),
  166.                           "last_access" => time(),
  167.                           "last_ip" => $_SERVER['REMOTE_ADDR']));
  168.        
  169.         $_SESSION['_berta__user'] = $this->user;
  170.     }
  171.    
  172.    
  173.    
  174.    
  175.    
  176.    
  177.    
  178.    
  179.     // ------------------------------------------------------------------------------------------------------------------------------
  180.     // --    Misc    ----------------------------------------------------------------------------------------------------------------
  181.     // ------------------------------------------------------------------------------------------------------------------------------
  182.    
  183.    
  184.     public function getError($errType, $errId) {
  185.         switch($errType) {
  186.             case "auth":
  187.                 switch($errId) {
  188.                     case self::BERTASECURITY_ERROR_SESSION_VARIABLE: return "Please check whether your browser supports cookies!";
  189.                     case self::BERTASECURITY_ERROR_SESSION_EXPIRED: return "The max idle time is " . round($this->authExpiresSeconds / 60) . " minutes.";
  190.                     case self::BERTASECURITY_ERROR_SESSION_IP_CONFLICT: return "Your IP address has changed... any idea why?";
  191.                     default: return "Unknown error (id: $errId).";
  192.                 }
  193.             case "login":
  194.                 switch($errId) {
  195.                     case self::BERTASECURITY_ERROR_LOGIN_VARIABLE: return "Pardon?";
  196.                     case self::BERTASECURITY_ERROR_LOGIN_INCORRECT:
  197.                         $arr = array("Pardon?");
  198.                         return $arr[array_rand($arr)];
  199.                     case 8: return "You have been deactivated.";
  200.                     default: return "Unknown error ($errId)";
  201.                 }
  202.         }
  203.     }
  204.    
  205.    
  206.    
  207.    
  208.    
  209.    
  210.    
  211.  
  212.  
  213.  
  214.    
  215.     function dispNocacheHeaders() {
  216.         if(!headers_sent()) {
  217.             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  218.             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  219.             header("Cache-Control: no-store, no-cache, must-revalidate");
  220.             header("Cache-Control: post-check=0, pre-check=0", false);
  221.             header("Pragma: no-cache");
  222.             return true;
  223.         }
  224.         return false;
  225.     }
  226.    
  227.    
  228. }
  229.  
  230. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement