PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/nova/core/user.class.php

http://xklog.googlecode.com/
PHP | 270 lines | 134 code | 25 blank | 111 comment | 35 complexity | e4ddaea0a62384c87b6b457f0e06bb1c MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. !defined('IN_NOVA') && exit('Access Denied!');
  3. class User {
  4. private $user;
  5. public function __construct() {
  6. // ??????
  7. $this->user = array(
  8. // ID
  9. 'uid' => 0,
  10. // ???
  11. 'username' => '',
  12. // ???
  13. 'group' => 0,
  14. // ??
  15. 'email' => '',
  16. // ?? IP
  17. 'ip' => Request::get_ip(),
  18. // ????
  19. 'registime' => 0,
  20. // ??????
  21. 'logintime' => PHP_TIME,
  22. // ??????
  23. 'actiontime' => PHP_TIME,
  24. // ??????
  25. 'posttime' => 0,
  26. // ??????
  27. 'searchtime' => 0,
  28. );
  29. }
  30. function __set( $name, $value ) {
  31. $this->set( $name, $value );
  32. }
  33. function __get( $name ) {
  34. return $this->get( $name );
  35. }
  36. // ?????????
  37. public function set( $index, $value ) {
  38. $this->user[$index] = $value;
  39. }
  40. // ??????
  41. public function get( $index ) {
  42. //[DEBUG]
  43. global $log;
  44. //[/DEBUG]
  45. if( !isset( $this->user[$index] ) ) {
  46. //[DEBUG]
  47. if( defined( 'APP_DEBUG' ) && APP_DEBUG === TRUE ) {
  48. $log->add( '[' . __FILE__ . '] [' . __LINE__ . '] ' . L( '_USE_ILLEGAL_INDEX_' ) . ' ' . $index, E_USER_NOTICE );
  49. }
  50. //[/DEBUG]
  51. return NULL;
  52. }
  53. return $this->user[$index];
  54. }
  55. // ????
  56. // ????0 ?????????-1 ??????-2 ????
  57. public function login( $username, $password, $remember = 0 ) {
  58. global $db;
  59. if ( $username == '' || $password == '' ) {
  60. return 0;
  61. }
  62. $result = $db->fetch_one_array( "SELECT * FROM `" . DB_PREFIX . "user` WHERE u_username = '$username'" );
  63. if ( !$result ) {
  64. return -1;
  65. }
  66. if ( $result['u_password'] != strtolower( md5( APP_PREFIX . $password ) ) && $result['u_password'] != $password ) {
  67. return -2;
  68. }
  69. $this->user['uid'] = $result['u_id'];
  70. $this->user['username'] = $result['u_username'];
  71. $this->user['group'] = $result['u_group'];
  72. $this->user['email'] = $result['u_email'];
  73. $this->user['actiontime'] = PHP_TIME;
  74. // ???????????? COOKIE
  75. if( $remember != -1 ) {
  76. $expire = $remember ? PHP_TIME + ( 365 * 24 * 3600 ) : 0;
  77. $app_path = str_replace( ' ','%20', APP_PATH );
  78. setcookie( APP_PREFIX . 'username', $result['u_username'], $expire, $app_path );
  79. setcookie( APP_PREFIX . 'password', $result['u_password'], $expire, $app_path );
  80. }
  81. $db->query( "UPDATE `" . DB_PREFIX . "user` SET u_ip = '" . $this->user['ip'] . "',u_time = '" . PHP_TIME . "' WHERE u_id = " . $this->user['uid'] );
  82. return 1;
  83. }
  84. //????
  85. public function logout() {
  86. $app_path = str_replace( ' ','%20', APP_PATH );
  87. setcookie( APP_PREFIX . 'username', NULL, 0, $app_path );
  88. setcookie( APP_PREFIX . 'password', NULL, 0, $app_path );
  89. return 1;
  90. }
  91. // ??????
  92. public function is_login() {
  93. if( $this->group > 0 ){
  94. return true;
  95. }else{
  96. return false;
  97. }
  98. }
  99. // ???????????
  100. public function is_admin() {
  101. if( $this->group > 7 ){
  102. return true;
  103. }else{
  104. return false;
  105. }
  106. }
  107. // ???????????
  108. public function is_super_admin() {
  109. global $cache;
  110. if( $this->group > 7 && $cache->config['enable'] == 0 ) return true;
  111. if ( $this->group > 7 && $_SESSION['user_group'] == 10 ) return true;
  112. return false;
  113. }
  114. //?????????
  115. //????0 ??????-1 ???????-2 ???????
  116. public function check_username( $username, $check_filter = true ) {
  117. global $cache;
  118. // ????
  119. if ( strlen( $username ) < 3 || strlen( $username ) > 20 ) {
  120. return 0;
  121. }
  122. // ??????
  123. $badchars = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n",'#','$','(',')','%','@','+','?',';','^');
  124. foreach ($badchars as $cha) {
  125. if (strpos($username,$cha) !== false) {
  126. return -1;
  127. }
  128. }
  129. if( $check_filter ) {
  130. // ???????
  131. $banname = split( "\|", $cache->config['namefilter'] );
  132. if ( count( $banname ) ) {
  133. foreach ( $banname as $val ) {
  134. if ( empty( $val ) ) continue;
  135. if ( !( strpos( strtolower( $username ), strtolower( $val ) ) === false) ) {
  136. return -2;
  137. }
  138. }
  139. }
  140. }
  141. return 1;
  142. }
  143. // ?????????
  144. //????0 ??????-1 ??????
  145. public function check_password( $passwd ) {
  146. // ????
  147. if ( strlen( $passwd ) < 5 || strlen( $passwd ) > 20 ) {
  148. return 0;
  149. }
  150. //??????
  151. if( !ereg("^[_a-zA-Z0-9]*$",$passwd) ) return -1;
  152. return 1;
  153. }
  154. //??email????
  155. public function check_email($email) {
  156. if ( preg_match('/^[0-9a-z]+[0-9a-z_\.\-]*@[0-9a-z\-]+(\.[a-z]{2,4}){1,2}$/i', $email ) ) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. // ?????????
  162. public function check_index( $weburl ) {
  163. if(!ereg("^http://[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$",$weburl)) {
  164. return false;
  165. }
  166. return true;
  167. }
  168. // ??????
  169. /*public function online() {
  170. global $cache,$db;
  171. if( $this->online != NULL ) {
  172. return $this->online;
  173. }
  174. $now_time = PHP_TIME;
  175. $now_online = array();
  176. $user_login = FALSE;
  177. $user_name = $this->user['username'];
  178. // ??????????? (?1200??20??)
  179. $online_time = 1200;
  180. // ??? ip
  181. $user_ip = $this->user['ip'];
  182. if( defined( 'CACHE_ENABLE' ) && CACHE_ENABLE === TRUE && CACHE_TYPE != 'VOID' ) {
  183. if( $cache->online != NULL ) {
  184. // ?????????
  185. foreach( $cache->online as $row => $value ) {
  186. if( ( $now_time - $value['time'] ) <= $online_time ) {
  187. $now_online[$value['ip']]['username'] = $value['username'];
  188. $now_online[$value['ip']]['time'] = $value['time'];
  189. $now_online[$value['ip']]['ip'] = $value['ip'];
  190. if( $value['ip'] == $user_ip ){
  191. $user_login = TRUE;
  192. }
  193. }
  194. }
  195. }
  196. // ????
  197. $now_online[$user_ip]['username'] = $user_name;
  198. $now_online[$user_ip]['time'] = $now_time;
  199. $now_online[$user_ip]['ip'] = $user_ip;
  200. if( !$user_login ) {
  201. // ????
  202. $db->query( "UPDATE `" . DB_PREFIX . "set` SET visitnum = visitnum + 1,todaynum = todaynum + 1" );
  203. }
  204. $online_num = count( $now_online );
  205. $cache->set( 'online', $now_online, $timeout = 0 );
  206. } else {
  207. $online = array();
  208. $online = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'online`' );
  209. // ?????????
  210. foreach( $online as $row => $value ) {
  211. if( ( $now_time - $value['time'] ) <= $online_time ) {
  212. $now_online[$row]['username'] = $value['username'];
  213. $now_online[$row]['ip'] = $value['ip'];
  214. $now_online[$row]['time'] = $value['time'];
  215. if( $value['ip'] == $user_ip ){
  216. $user_login = true;
  217. }
  218. } else {
  219. $db->query( "DELETE FROM `" . DB_PREFIX . "online` WHERE ip = '" . $value['ip'] . "'" );
  220. }
  221. }
  222. $online_num = count( $now_online );
  223. if( $user_login ) {
  224. // ????
  225. $db->query( "UPDATE `" . DB_PREFIX . "online` SET time=$now_time,username='$user_name' WHERE ip = '$user_ip'" );
  226. } else {
  227. // ????
  228. $db->query( "INSERT INTO `" . DB_PREFIX . "online` (`username`,`ip`,`time`) VALUES ('$user_name','$user_ip',$now_time)" );
  229. $db->query( "UPDATE `" . DB_PREFIX . "set` SET visitnum = visitnum + 1,todaynum = todaynum + 1" );
  230. $online_num ++;
  231. }
  232. }
  233. $this->online = $online_num;
  234. return $online_num;
  235. }*/
  236. // ----------------------------------------
  237. // ???????
  238. }
  239. ?>