/ecerp/System/Libraries/SP_Auth.php
PHP | 160 lines | 103 code | 27 blank | 30 comment | 21 complexity | 8a8d8e9087b37a6845c6cae54aebb091 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']);
- //update operators update_time
- $this->db->update($this->config['db_table'], $this->config['db_loginname'] . "='$loginname'", array('update_time'=>date('Y-m-d H:i:s')));
- $this->db->query();
- 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
- * @param Int $expiry
- * @return String
- */
- function authcode($string, $operation = 'DECODE', $key, $expiry = 0) {
- $ckey_length = 4;
- $key = md5($key);
- $keya = md5(substr($key, 0, 16));
- $keyb = md5(substr($key, 16, 16));
- $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
- $cryptkey = $keya.md5($keya.$keyc);
- $key_length = strlen($cryptkey);
- $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
- $string_length = strlen($string);
- $result = '';
- $box = range(0, 255);
- $rndkey = array();
- for($i = 0; $i <= 255; $i++) {
- $rndkey[$i] = ord($cryptkey[$i % $key_length]);
- }
- 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, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
- return substr($result, 26);
- } else {
- return '';
- }
- } else {
- return $keyc.str_replace('=', '', base64_encode($result));
- }
- }
- }