user.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: user.php 1174 2014-11-03 04:38:12Z hypowang $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('UC_USER_CHECK_USERNAME_FAILED', -1);
  9. define('UC_USER_USERNAME_BADWORD', -2);
  10. define('UC_USER_USERNAME_EXISTS', -3);
  11. define('UC_USER_EMAIL_FORMAT_ILLEGAL', -4);
  12. define('UC_USER_EMAIL_ACCESS_ILLEGAL', -5);
  13. define('UC_USER_EMAIL_EXISTS', -6);
  14. define('UC_USER_MOBILE_FORMAT_ILLEGAL', -7);
  15. define('UC_USER_MOBILE_ACCESS_ILLEGAL', -8);
  16. define('UC_USER_MOBILE_EXISTS', -9);
  17. class usercontrol extends base
  18. {
  19. function __construct()
  20. {
  21. $this->usercontrol();
  22. }
  23. function usercontrol()
  24. {
  25. parent::__construct();
  26. $this->load('user');
  27. $this->app = $this->cache['apps'][UC_APPID];
  28. }
  29. // 同步注册
  30. function onsynregister()
  31. {
  32. $this->init_input();
  33. $uid = $this->input('uid');
  34. $password = $this->input('password');
  35. if ($this->app['synlogin'])
  36. {
  37. if ($this->user = $_ENV['user']->get_user_by_uid($uid))
  38. {
  39. $params = 'action=synregister' .
  40. '&username=' . $this->user['username'] .
  41. '&uid=' . $this->user['uid'] .
  42. '&password=' . $password .
  43. '&email=' . $this->user['email'] .
  44. '&mobile=' . $this->user['mobile'] .
  45. '&time=' . $this->time;
  46. $synstr = '';
  47. foreach ($this->cache['apps'] as $appid => $app)
  48. {
  49. if ($app['synlogin'] && $app['appid'] != $this->app['appid'])
  50. {
  51. $synstr .= '<script type="text/javascript" src="' . $app['url'] . '/api/uc.php?time=' . $this->time . '&code=' . urlencode($this->authcode($params, 'ENCODE', $app['authkey'])) . '"></script>';
  52. }
  53. }
  54. return $synstr;
  55. }
  56. }
  57. return '';
  58. }
  59. // 同步登录
  60. function onsynlogin()
  61. {
  62. $this->init_input();
  63. $uid = $this->input('uid');
  64. if ($this->app['synlogin'])
  65. {
  66. if ($this->user = $_ENV['user']->get_user_by_uid($uid))
  67. {
  68. $params = 'action=synlogin' .
  69. '&username=' . $this->user['username'] .
  70. '&uid=' . $this->user['uid'] .
  71. '&password=' . $this->user['password'] .
  72. '&time=' . $this->time;
  73. $synstr = '';
  74. foreach ($this->cache['apps'] as $appid => $app)
  75. {
  76. if ($app['synlogin'] && $app['appid'] != $this->app['appid'])
  77. {
  78. $synstr .= '<script type="text/javascript" src="' . $app['url'] . '/api/uc.php?time=' . $this->time . '&code=' . urlencode($this->authcode($params, 'ENCODE', $app['authkey'])) . '"></script>';
  79. }
  80. }
  81. return $synstr;
  82. }
  83. }
  84. return '';
  85. }
  86. // 同步退出
  87. function onsynlogout()
  88. {
  89. $this->init_input();
  90. if ($this->app['synlogin'])
  91. {
  92. $synstr = '';
  93. foreach ($this->cache['apps'] as $appid => $app)
  94. {
  95. if ($app['synlogin'] && $app['appid'] != $this->app['appid'])
  96. {
  97. $synstr .= '<script type="text/javascript" src="' . $app['url'] . '/api/uc.php?time=' . $this->time . '&code=' . urlencode($this->authcode('action=synlogout&time=' . $this->time, 'ENCODE', $app['authkey'])) . '"></script>';
  98. }
  99. }
  100. return $synstr;
  101. }
  102. return '';
  103. }
  104. // 同步注册
  105. function onregister()
  106. {
  107. $this->init_input();
  108. $username = $this->input('username');
  109. $password = $this->input('password');
  110. $email = $this->input('email');
  111. $mobile = $this->input('mobile');
  112. $extend = $this->input('extend');
  113. $extend = $extend ? $extend : [];
  114. if (($status = $this->_check_username($username)) < 0)
  115. {
  116. return $status;
  117. }
  118. if ($email && ($status = $this->_check_email($email)) < 0)
  119. {
  120. return $status;
  121. }
  122. if ($mobile && ($status = $this->_check_mobile($mobile)) < 0)
  123. {
  124. return $status;
  125. }
  126. $uid = $_ENV['user']->add_user($username, $password, $email, $mobile, 0, $extend);
  127. return $uid;
  128. }
  129. // 编辑ucenter中信息
  130. function onedit()
  131. {
  132. $this->init_input();
  133. $uid = intval($this->input('uid'));
  134. $username = $this->input('username');
  135. $password = $this->input('password');
  136. $email = $this->input('email');
  137. $mobile = $this->input('mobile');
  138. $extend = $this->input('extend');
  139. $extend = $extend ? $extend : [];
  140. $status = $_ENV['user']->edit_user($username, $password, $email, $mobile, $uid, $extend);
  141. if ($status > 0)
  142. {
  143. if (@include UC_ROOT . './data/cache/apps.php')
  144. {
  145. if (count($_CACHE['apps']) > 1)
  146. {
  147. //手动调用一次同步到所有应用
  148. uc_api_post('user', 'edit', $this->input);
  149. }
  150. }
  151. return $status;
  152. $this->load('note');
  153. $_ENV['note']->add('updateinfo', http_build_query($this->input));
  154. }
  155. return $status;
  156. }
  157. // 登录ucenter账号
  158. function onlogin()
  159. {
  160. $this->init_input();
  161. $username = $this->input('username');
  162. $password = $this->input('password');
  163. $isuid = $this->input('isuid'); //0用户名 1UID 2邮箱 3手机
  164. if ($isuid == 1)
  165. {
  166. $user = $_ENV['user']->get_user_by_uid($username);
  167. }
  168. elseif ($isuid == 2)
  169. {
  170. $user = $_ENV['user']->get_user_by_email($username);
  171. }
  172. elseif ($isuid == 3)
  173. {
  174. $user = $_ENV['user']->get_user_by_mobile($username);
  175. }
  176. else
  177. {
  178. $user = $_ENV['user']->get_user_by_username($username);
  179. }
  180. $passwordmd5 = preg_match('/^\w{32}$/', $password) ? $password : md5($password);
  181. if (empty($user))
  182. {
  183. $status = -1;
  184. }
  185. elseif ($user['password'] != md5($passwordmd5 . $user['salt']))
  186. {
  187. $status = -2;
  188. }
  189. else
  190. {
  191. $status = $user['uid'];
  192. }
  193. $merge = $status != -1 && !$isuid && $_ENV['user']->check_mergeuser($username) ? 1 : 0;
  194. return [
  195. 'status' => $status,
  196. 'uid' => isset($user['uid']) ? $user['uid'] : 0,
  197. 'username' => isset($user['username']) ? $user['username'] : '',
  198. 'password' => $password,
  199. 'email' => isset($user['email']) ? $user['email'] : '',
  200. 'mobile' => isset($user['mobile']) ? $user['mobile'] : '',
  201. 'merge' => $merge
  202. ];
  203. }
  204. function onlogincheck()
  205. {
  206. $this->init_input();
  207. $username = $this->input('username');
  208. $ip = $this->input('ip');
  209. return $_ENV['user']->can_do_login($username, $ip);
  210. }
  211. function oncheck_email()
  212. {
  213. $this->init_input();
  214. $email = $this->input('email');
  215. return $this->_check_email($email);
  216. }
  217. function oncheck_username()
  218. {
  219. $this->init_input();
  220. $username = $this->input('username');
  221. if (($status = $this->_check_username($username)) < 0)
  222. {
  223. return $status;
  224. }
  225. else
  226. {
  227. return 1;
  228. }
  229. }
  230. function onget_user()
  231. {
  232. $this->init_input();
  233. $username = $this->input('username');
  234. $isuid = $this->input('isuid'); //0用户名 1UID 2邮箱 3手机
  235. if ($isuid == 1)
  236. {
  237. $member = $_ENV['user']->get_user_by_uid($username);
  238. }
  239. else if ($isuid == 2)
  240. {
  241. $member = $_ENV['user']->get_user_by_email($username);
  242. }
  243. else if ($isuid == 3)
  244. {
  245. $member = $_ENV['user']->get_user_by_mobile($username);
  246. }
  247. else
  248. {
  249. $member = $_ENV['user']->get_user_by_username($username);
  250. }
  251. if ($member)
  252. {
  253. // 加载扩展表
  254. $memberfields = $this->db->fetch_first("SELECT * FROM " . UC_DBTABLEPRE . "memberfields WHERE uid='{$member['uid']}'");
  255. $memberfields = $memberfields ? $memberfields : [];
  256. $member = [
  257. 'uid' => $member['uid'],
  258. 'username' => $member['username'],
  259. 'email' => $member['email'],
  260. 'mobile' => $member['mobile']
  261. ];
  262. return array_merge($member, $memberfields);
  263. }
  264. else
  265. {
  266. return 0;
  267. }
  268. }
  269. function ongetprotected()
  270. {
  271. $this->init_input();
  272. $protectedmembers = $this->db->fetch_all("SELECT uid,username FROM " . UC_DBTABLEPRE . "protectedmembers GROUP BY username");
  273. return $protectedmembers;
  274. }
  275. function ondelete()
  276. {
  277. $this->init_input();
  278. $uid = $this->input('uid');
  279. return $_ENV['user']->delete_user($uid);
  280. }
  281. function onaddprotected()
  282. {
  283. $this->init_input();
  284. $username = $this->input('username');
  285. $admin = $this->input('admin');
  286. $appid = $this->app['appid'];
  287. $usernames = (array) $username;
  288. foreach ($usernames as $username)
  289. {
  290. $user = $_ENV['user']->get_user_by_username($username);
  291. $uid = $user['uid'];
  292. $this->db->query("REPLACE INTO " . UC_DBTABLEPRE . "protectedmembers SET uid='$uid', username='$username', appid='$appid', dateline='{$this->time}', admin='$admin'", 'SILENT');
  293. }
  294. return $this->db->errno() ? -1 : 1;
  295. }
  296. function ondeleteprotected()
  297. {
  298. $this->init_input();
  299. $username = $this->input('username');
  300. $appid = $this->app['appid'];
  301. $usernames = (array) $username;
  302. foreach ($usernames as $username)
  303. {
  304. $this->db->query("DELETE FROM " . UC_DBTABLEPRE . "protectedmembers WHERE username='$username' AND appid='$appid'");
  305. }
  306. return $this->db->errno() ? -1 : 1;
  307. }
  308. function onmerge()
  309. {
  310. $this->init_input();
  311. $uid = $this->input('uid');
  312. $oldusername = $this->input('oldusername');
  313. $newusername = $this->input('newusername');
  314. $password = $this->input('password');
  315. $email = $this->input('email');
  316. $mobile = $this->input('mobile');
  317. $extend = $this->input('extend');
  318. $extend = $extend ? $extend : [];
  319. if (($status = $this->_check_username($newusername)) < 0)
  320. {
  321. return $status;
  322. }
  323. $uid = $_ENV['user']->add_user($newusername, $password, $email, $mobile, $uid, $extend);
  324. $this->db->query("DELETE FROM " . UC_DBTABLEPRE . "mergemembers WHERE appid='" . $this->app['appid'] . "' AND username='$oldusername'");
  325. return $uid;
  326. }
  327. function onmerge_remove()
  328. {
  329. $this->init_input();
  330. $username = $this->input('username');
  331. $this->db->query("DELETE FROM " . UC_DBTABLEPRE . "mergemembers WHERE appid='" . $this->app['appid'] . "' AND username='$username'");
  332. return NULL;
  333. }
  334. function _check_username($username)
  335. {
  336. $username = addslashes(trim(stripslashes($username)));
  337. if (!$_ENV['user']->check_username($username))
  338. {
  339. return UC_USER_CHECK_USERNAME_FAILED;
  340. }
  341. elseif (!$_ENV['user']->check_usernamecensor($username))
  342. {
  343. return UC_USER_USERNAME_BADWORD;
  344. }
  345. elseif ($_ENV['user']->check_usernameexists($username))
  346. {
  347. return UC_USER_USERNAME_EXISTS;
  348. }
  349. return 1;
  350. }
  351. function _check_email($email, $username = '')
  352. {
  353. if (empty($this->settings))
  354. {
  355. $this->settings = $this->cache('settings');
  356. }
  357. if (!$_ENV['user']->check_emailformat($email))
  358. {
  359. return UC_USER_EMAIL_FORMAT_ILLEGAL;
  360. }
  361. elseif (!$_ENV['user']->check_emailaccess($email))
  362. {
  363. return UC_USER_EMAIL_ACCESS_ILLEGAL;
  364. }
  365. elseif (!$this->settings['doublee'] && $_ENV['user']->check_emailexists($email, $username))
  366. {
  367. return UC_USER_EMAIL_EXISTS;
  368. }
  369. else
  370. {
  371. return 1;
  372. }
  373. }
  374. function _check_mobile($mobile, $username = '')
  375. {
  376. if (empty($this->settings))
  377. {
  378. $this->settings = $this->cache('settings');
  379. }
  380. if (!$_ENV['user']->check_mobileformat($mobile))
  381. {
  382. return UC_USER_EMAIL_FORMAT_ILLEGAL;
  383. }
  384. elseif (!$_ENV['user']->check_mobileaccess($mobile))
  385. {
  386. return UC_USER_EMAIL_ACCESS_ILLEGAL;
  387. }
  388. elseif (!$this->settings['doublee'] && $_ENV['user']->check_mobileexists($mobile, $username))
  389. {
  390. return UC_USER_EMAIL_EXISTS;
  391. }
  392. else
  393. {
  394. return 1;
  395. }
  396. }
  397. function onuploadavatar()
  398. {
  399. }
  400. function onrectavatar()
  401. {
  402. }
  403. function flashdata_decode($s)
  404. {
  405. }
  406. }