PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ecerp/System/Libraries/SP_Auth.php

http://phpfor.googlecode.com/
PHP | 160 lines | 103 code | 27 blank | 30 comment | 21 complexity | 8a8d8e9087b37a6845c6cae54aebb091 MD5 | raw file
  1. <?php
  2. class SP_Auth {
  3. var $LOGIN_FLAG = false;
  4. var $M_info = NULL;
  5. var $db;
  6. function SP_Auth() {
  7. $tmpconfig =& Loader::config();
  8. $this->config = $tmpconfig['auth'];
  9. $this->db =& Loader::database('db');
  10. if(isset($_COOKIE[$this->config['cookie_name']])){
  11. $auth = $this->authcode(
  12. $_COOKIE[$this->config['cookie_name']],
  13. 'DECODE',
  14. md5($this->config['auth_key']/*.$_SERVER['HTTP_USER_AGENT']*/)
  15. );
  16. $auth = explode("\t",$auth);
  17. $uid = isset($auth[1])?$auth[1]:0;
  18. $upass = isset($auth[0])?$auth[0]:'';
  19. $this->db->select($this->config['db_table'],'*',$this->config['db_uid']."='$uid'");
  20. $member_info = $this->db->getRow();
  21. if(!$member_info){
  22. $this->LOGIN_FLAG = false;
  23. }else{
  24. if($member_info[$this->config['db_upass']] == $upass){
  25. $this->LOGIN_FLAG = true;
  26. $this->M_info = $member_info;
  27. }else{
  28. $this->LOGIN_FLAG = false;
  29. }
  30. }
  31. }else{
  32. $this->LOGIN_FLAG = false;
  33. }
  34. }
  35. /**
  36. * ????????
  37. *
  38. * @return Bool
  39. */
  40. function isLogedin() {
  41. return $this->LOGIN_FLAG;
  42. }
  43. /**
  44. * ??????
  45. *
  46. * @param String $key
  47. * @param String $default
  48. * @return String
  49. */
  50. function getInfo($key,$default = '') {
  51. return isset ($this->M_info["$key"]) ? $this->M_info["$key"] : $default;
  52. }
  53. /**
  54. * ??????
  55. *
  56. * @param String $loginname
  57. * @param String $password
  58. * @param String $expire_time
  59. * @return Bool
  60. */
  61. function setLogin($loginname,$password,$expire_time = 0){
  62. $this->db->select($this->config['db_table'], '*', $this->config['db_loginname'] . "='$loginname'");
  63. $member_info = $this->db->getRow();
  64. if($member_info && $member_info[$this->config['db_upass']] == $password){
  65. $this->LOGIN_FLAG = true;
  66. $this->M_info = $member_info;
  67. $my_auth = $this->authcode(
  68. $password."\t".$member_info[$this->config['db_uid']],
  69. 'ENCODE',
  70. md5($this->config['auth_key']/*.$_SERVER['HTTP_USER_AGENT']*/)
  71. );
  72. setcookie($this->config['cookie_name'],$my_auth,$expire_time,$this->config['cookie_path'],$this->config['cookie_domain']);
  73. //update operators update_time
  74. $this->db->update($this->config['db_table'], $this->config['db_loginname'] . "='$loginname'", array('update_time'=>date('Y-m-d H:i:s')));
  75. $this->db->query();
  76. return true;
  77. }else{
  78. return false;
  79. }
  80. }
  81. function clearLogin(){
  82. setcookie($this->config['cookie_name'],'',- 86400 * 365,$this->config['cookie_path'],$this->config['cookie_domain']);
  83. }
  84. /**
  85. * ????
  86. *
  87. * @param String $string
  88. * @param String $operation
  89. * @param String $key
  90. * @param Int $expiry
  91. * @return String
  92. */
  93. function authcode($string, $operation = 'DECODE', $key, $expiry = 0) {
  94. $ckey_length = 4;
  95. $key = md5($key);
  96. $keya = md5(substr($key, 0, 16));
  97. $keyb = md5(substr($key, 16, 16));
  98. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  99. $cryptkey = $keya.md5($keya.$keyc);
  100. $key_length = strlen($cryptkey);
  101. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  102. $string_length = strlen($string);
  103. $result = '';
  104. $box = range(0, 255);
  105. $rndkey = array();
  106. for($i = 0; $i <= 255; $i++) {
  107. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  108. }
  109. for($j = $i = 0; $i < 256; $i++) {
  110. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  111. $tmp = $box[$i];
  112. $box[$i] = $box[$j];
  113. $box[$j] = $tmp;
  114. }
  115. for($a = $j = $i = 0; $i < $string_length; $i++) {
  116. $a = ($a + 1) % 256;
  117. $j = ($j + $box[$a]) % 256;
  118. $tmp = $box[$a];
  119. $box[$a] = $box[$j];
  120. $box[$j] = $tmp;
  121. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  122. }
  123. if($operation == 'DECODE') {
  124. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  125. return substr($result, 26);
  126. } else {
  127. return '';
  128. }
  129. } else {
  130. return $keyc.str_replace('=', '', base64_encode($result));
  131. }
  132. }
  133. }