KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00022 abstract class Auth2 00023 { 00029 protected $_error = null; 00030 00036 protected $_login = 'login'; 00037 00043 protected $_pass = 'password'; 00044 00050 protected $_algos = 'md5'; 00051 00057 protected $_key = 'jt2D14KIdRs7LA=='; 00058 00064 protected $_checkSession = TRUE; 00065 00071 protected static $_defaultAdapter = 'model'; 00072 00078 public function setLogin($field) 00079 { 00080 $this->_login = $field; 00081 } 00082 00088 public function setPass($field) 00089 { 00090 $this->_pass = $field; 00091 } 00092 00098 public function setKey($key) 00099 { 00100 $this->_key = $key; 00101 } 00102 00109 public function identify () 00110 { 00111 if ($this->isValid()) { 00112 return TRUE; 00113 } else { 00114 // check 00115 if (isset($_POST['mode']) && $_POST['mode'] === 'auth') { 00116 //data was posted. 00117 return $this->_check($_POST[$this->_login], $_POST[$this->_pass]); 00118 } else { 00119 //FAIL 00120 return FALSE; 00121 } 00122 } 00123 } 00124 00132 abstract protected function _check ($username, $password); 00133 00140 public function logout () 00141 { 00142 Session::set($this->_key, FALSE); 00143 session_destroy(); 00144 } 00145 00151 public function isValid() 00152 { 00153 session_regenerate_id(); 00154 00155 if($this->_checkSession){ 00156 $this->_checkSession(); 00157 } 00158 00159 return Session::has($this->_key) && Session::get($this->_key) === TRUE; 00160 } 00161 00166 private function _checkSession() 00167 { 00168 Session::set('USERAGENT', $_SERVER['HTTP_USER_AGENT']); 00169 Session::set('REMOTEADDR', $_SERVER['REMOTE_ADDR']); 00170 00171 if ($_SERVER['REMOTE_ADDR'] !== Session::get('REMOTEADDR') || 00172 $_SERVER['HTTP_USER_AGENT'] !== Session::get('USERAGENT')){ 00173 session_destroy(); 00174 } 00175 } 00176 00182 public function setCheckSession($check) 00183 { 00184 $this->_checkSession = $check; 00185 } 00186 00192 public function setAlgos($algos, $salt = NULL) 00193 { 00194 $this->_algos = $algos; 00195 } 00196 00202 public function getError () 00203 { 00204 return $this->_error; 00205 } 00206 00212 public function setError ($error) 00213 { 00214 $this->_error = $error; 00215 } 00216 00221 public static function log($msg) 00222 { 00223 $date = date('Y-m-d', strtotime('now')); 00224 Logger::custom('AUTH', $msg, "auth-$date.log"); 00225 } 00226 00232 public static function factory($adapter = NULL) 00233 { 00234 if(!$adapter) { 00235 $adapter = self::$_defaultAdapter; 00236 } 00237 00238 require_once CORE_PATH . "libs/auth2/adapters/{$adapter}_auth.php"; 00239 $class = $adapter.'auth'; 00240 00241 return new $class; 00242 } 00243 00249 public static function setDefault ($adapter) 00250 { 00251 self::$_defaultAdapter = $adapter; 00252 } 00253 }