PageRenderTime 27ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/tuandaohang/System/Libraries/SP_Auth.php

http://phpfor.googlecode.com/
PHP | 148 lines | 96 code | 24 blank | 28 comment | 17 complexity | 5e789a364aab199f940210f8cfdf5980 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. return true;
  74. }else{
  75. return false;
  76. }
  77. }
  78. function clearLogin(){
  79. setcookie($this->config['cookie_name'],'',- 86400 * 365,$this->config['cookie_path'],$this->config['cookie_domain']);
  80. }
  81. /**
  82. * ????
  83. *
  84. * @param String $string
  85. * @param String $operation
  86. * @param String $key
  87. * @return String
  88. */
  89. function authcode($string, $operation, $key) {
  90. $key = md5 ( $key );
  91. $key_length = strlen ( $key );
  92. $string = $operation == 'DECODE' ? base64_decode ( $string ) : substr ( md5 ( $string . $key ), 0, 8 ) . $string;
  93. $string_length = strlen ( $string );
  94. $rndkey = $box = array ();
  95. $result = '';
  96. for($i = 0; $i <= 255; $i ++) {
  97. $rndkey [$i] = ord ( $key [$i % $key_length] );
  98. $box [$i] = $i;
  99. }
  100. for($j = $i = 0; $i < 256; $i ++) {
  101. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  102. $tmp = $box [$i];
  103. $box [$i] = $box [$j];
  104. $box [$j] = $tmp;
  105. }
  106. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  107. $a = ($a + 1) % 256;
  108. $j = ($j + $box [$a]) % 256;
  109. $tmp = $box [$a];
  110. $box [$a] = $box [$j];
  111. $box [$j] = $tmp;
  112. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  113. }
  114. if ($operation == 'DECODE') {
  115. if (substr ( $result, 0, 8 ) == substr ( md5 ( substr ( $result, 8 ) . $key ), 0, 8 )) {
  116. return substr ( $result, 8 );
  117. } else {
  118. return '';
  119. }
  120. } else {
  121. return str_replace ( '=', '', base64_encode ( $result ) );
  122. }
  123. }
  124. }