/tuandaohang/System/Libraries/SP_Auth.php
PHP | 148 lines | 96 code | 24 blank | 28 comment | 17 complexity | 5e789a364aab199f940210f8cfdf5980 MD5 | raw file
- <?php
- class SP_Auth {
- var $LOGIN_FLAG = false;
-
- var $M_info = NULL;
- var $db;
-
- function SP_Auth() {
-
- $tmpconfig =& Loader::config();
- $this->config = $tmpconfig['auth'];
- $this->db =& Loader::database('db');
- if(isset($_COOKIE[$this->config['cookie_name']])){
- $auth = $this->authcode(
- $_COOKIE[$this->config['cookie_name']],
- 'DECODE',
- md5($this->config['auth_key'].$_SERVER['HTTP_USER_AGENT'])
- );
- $auth = explode("\t",$auth);
- $uid = isset($auth[1])?$auth[1]:0;
- $upass = isset($auth[0])?$auth[0]:'';
- $this->db->select($this->config['db_table'],'*',$this->config['db_uid']."='$uid'");
- $member_info = $this->db->getRow();
- if(!$member_info){
- $this->LOGIN_FLAG = false;
- }else{
- if($member_info[$this->config['db_upass']] == $upass){
- $this->LOGIN_FLAG = true;
- $this->M_info = $member_info;
- }else{
- $this->LOGIN_FLAG = false;
- }
- }
- }else{
- $this->LOGIN_FLAG = false;
- }
- }
-
- /**
- * ????????
- *
- * @return Bool
- */
- function isLogedin() {
- return $this->LOGIN_FLAG;
- }
-
- /**
- * ??????
- *
- * @param String $key
- * @param String $default
- * @return String
- */
- function getInfo($key,$default = '') {
- return isset ($this->M_info["$key"]) ? $this->M_info["$key"] : $default;
- }
-
- /**
- * ??????
- *
- * @param String $loginname
- * @param String $password
- * @param String $expire_time
- * @return Bool
- */
- function setLogin($loginname,$password,$expire_time = 0){
- $this->db->select($this->config['db_table'],'*',$this->config['db_loginname']."='$loginname'");
- $member_info = $this->db->getRow();
- if($member_info && $member_info[$this->config['db_upass']] == $password){
- $this->LOGIN_FLAG = true;
- $this->M_info = $member_info;
-
- $my_auth = $this->authcode(
- $password."\t".$member_info[$this->config['db_uid']],
- 'ENCODE',
- md5($this->config['auth_key'].$_SERVER['HTTP_USER_AGENT'])
- );
-
- setcookie($this->config['cookie_name'],$my_auth,$expire_time,$this->config['cookie_path'],$this->config['cookie_domain']);
- return true;
- }else{
- return false;
- }
- }
- function clearLogin(){
- setcookie($this->config['cookie_name'],'',- 86400 * 365,$this->config['cookie_path'],$this->config['cookie_domain']);
- }
- /**
- * ????
- *
- * @param String $string
- * @param String $operation
- * @param String $key
- * @return String
- */
- function authcode($string, $operation, $key) {
- $key = md5 ( $key );
- $key_length = strlen ( $key );
- $string = $operation == 'DECODE' ? base64_decode ( $string ) : substr ( md5 ( $string . $key ), 0, 8 ) . $string;
- $string_length = strlen ( $string );
- $rndkey = $box = array ();
- $result = '';
- for($i = 0; $i <= 255; $i ++) {
- $rndkey [$i] = ord ( $key [$i % $key_length] );
- $box [$i] = $i;
- }
- for($j = $i = 0; $i < 256; $i ++) {
- $j = ($j + $box [$i] + $rndkey [$i]) % 256;
- $tmp = $box [$i];
- $box [$i] = $box [$j];
- $box [$j] = $tmp;
- }
- for($a = $j = $i = 0; $i < $string_length; $i ++) {
- $a = ($a + 1) % 256;
- $j = ($j + $box [$a]) % 256;
- $tmp = $box [$a];
- $box [$a] = $box [$j];
- $box [$j] = $tmp;
- $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
- }
- if ($operation == 'DECODE') {
- if (substr ( $result, 0, 8 ) == substr ( md5 ( substr ( $result, 8 ) . $key ), 0, 8 )) {
- return substr ( $result, 8 );
- } else {
- return '';
- }
- } else {
- return str_replace ( '=', '', base64_encode ( $result ) );
- }
- }
- }