PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/models/account.php

https://github.com/sony88/answion
PHP | 1828 lines | 1290 code | 303 blank | 235 comment | 211 complexity | 0fc1dbeb5d69536dd4da6120bb2e0823 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | Anwsion [#RELEASE_VERSION#]
  5. | ========================================
  6. | by Anwsion dev team
  7. | (c) 2011 - 2012 Anwsion Software
  8. | http://www.anwsion.com
  9. | ========================================
  10. | Support: zhengqiang@gmail.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. if (!defined('IN_ANWSION'))
  15. {
  16. die;
  17. }
  18. class account_class extends AWS_MODEL
  19. {
  20. /**
  21. * 未读系统通知
  22. */
  23. const NOTIFICATION_UNREAD = 'notification_unread';
  24. /**
  25. * 未读短信息
  26. */
  27. const NOTICE_UNREAD = 'notice_unread';
  28. /**
  29. * 回复问题数量
  30. */
  31. const ANSWER_COUNT = 'answer_count';
  32. /**
  33. * 受邀请参与问题计数
  34. */
  35. const INVITE_COUNT = 'invite_count';
  36. function get_source_hash($email)
  37. {
  38. return H::encode_hash(array(
  39. 'email' => $email
  40. ));
  41. }
  42. /**
  43. * 检查用户名是否已经存在
  44. * @param $username
  45. * @return rows
  46. */
  47. function check_username($username)
  48. {
  49. return $this->fetch_one('users', 'uid', "user_name = '" . $this->quote(trim($username)) . "' OR url_token = '" . $this->quote(trim($username)) . "'");
  50. }
  51. /**
  52. * 检查用户名中是否包含敏感词或用户信息保留字
  53. * @param unknown_type $username
  54. * @return boolean
  55. */
  56. function check_username_sensitive_words($username)
  57. {
  58. if (H::sensitive_word_exists($username, '', true))
  59. {
  60. return true;
  61. }
  62. if (!get_setting('censoruser'))
  63. {
  64. return false;
  65. }
  66. if ($censorusers = explode("\n", get_setting('censoruser')))
  67. {
  68. foreach ($censorusers as $name)
  69. {
  70. $name = trim($name);
  71. if (!$name)
  72. {
  73. continue;
  74. }
  75. if (preg_match('/(' . $name . ')/is', $username))
  76. {
  77. return true;
  78. }
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * 检查用户名是否已经存在
  85. * @param $username
  86. * @return rows
  87. */
  88. function check_uid($uid)
  89. {
  90. return $this->fetch_one('users', 'uid', 'uid = ' . intval($uid));
  91. }
  92. /**
  93. * 检查电子邮件地址是否已经存在
  94. * @param $email
  95. * @return int
  96. */
  97. function check_email($email)
  98. {
  99. if (! H::valid_email($email))
  100. {
  101. return TRUE;
  102. }
  103. return $this->fetch_one('users', 'uid', "email = '" . $this->quote($email) . "'");
  104. }
  105. /**
  106. * 正式表用户登录检查,错误返回FALSE,正确返回用户数据
  107. * @param $username
  108. * @param $password
  109. * @return
  110. */
  111. function check_login($username, $password)
  112. {
  113. if (!$username OR !$password)
  114. {
  115. return false;
  116. }
  117. if (H::valid_email($username))
  118. {
  119. $user_info = $this->get_user_info_by_email($username);
  120. }
  121. if (! $user_info)
  122. {
  123. if (! $user_info = $this->get_user_info_by_username($username))
  124. {
  125. return false;
  126. }
  127. }
  128. if (! $this->check_password($password, $user_info['password'], $user_info['salt']))
  129. {
  130. return false;
  131. }
  132. else
  133. {
  134. return $user_info;
  135. }
  136. }
  137. function check_hash_login($username, $password_md5)
  138. {
  139. if (!$username OR !$password_md5)
  140. {
  141. return false;
  142. }
  143. if (H::valid_email($username))
  144. {
  145. $user_info = $this->get_user_info_by_email($username);
  146. }
  147. if (! $user_info)
  148. {
  149. if (! $user_info = $this->get_user_info_by_username($username))
  150. {
  151. return false;
  152. }
  153. }
  154. if ( $password_md5 != $user_info['password'])
  155. {
  156. return false;
  157. }
  158. else
  159. {
  160. return $user_info;
  161. }
  162. }
  163. /**
  164. * 检验密码是否和数据库里面的密码相同
  165. *
  166. * @param string $password 新密码
  167. * @param string $db_password 数据库密码
  168. * @param string $salt 混淆码
  169. * @return bool
  170. */
  171. function check_password($password, $db_password, $salt)
  172. {
  173. $password = compile_password($password, $salt);
  174. if ($password == $db_password)
  175. {
  176. return true;
  177. }
  178. return false;
  179. }
  180. /**
  181. * 通过用户名获取用户信息
  182. * @param $username 用户名或邮箱地址
  183. * @return
  184. */
  185. function get_user_info_by_username($username, $attrb = false)
  186. {
  187. if ($uid = $this->fetch_one('users', 'uid', "user_name = '" . $this->quote($username) . "'"))
  188. {
  189. return $this->get_user_info_by_uid($uid, $attrb);
  190. }
  191. }
  192. /**
  193. * 通过用户邮箱获取用户信息
  194. * @param $email 用邮箱地址
  195. * @return row
  196. */
  197. function get_user_info_by_email($email)
  198. {
  199. if ($uid = $this->fetch_one('users', 'uid', "email = '" . $this->quote($email) . "'"))
  200. {
  201. return $this->get_user_info_by_uid($uid, $attrb);
  202. }
  203. }
  204. function get_user_info_by_url_token($url_token, $attrb = false)
  205. {
  206. if ($uid = $this->fetch_one('users', 'uid', "url_token = '" . $this->quote($url_token) . "'"))
  207. {
  208. return $this->get_user_info_by_uid($uid, $attrb);
  209. }
  210. }
  211. /**
  212. * 通过用户 uid 获取用户信息
  213. * @param $username
  214. * @return
  215. */
  216. function get_user_info_by_uid($uid, $attrib = false, $var_cache = true)
  217. {
  218. if (! $uid)
  219. {
  220. return false;
  221. }
  222. if ($var_cache)
  223. {
  224. static $users_info;
  225. if ($users_info[$uid . '_attrib'])
  226. {
  227. return $users_info[$uid . '_attrib'];
  228. }
  229. else if ($users_info[$uid])
  230. {
  231. return $users_info[$uid];
  232. }
  233. }
  234. if ($attrib)
  235. {
  236. $sql = "SELECT MEM.*, MEB.* FROM " . $this->get_table('users') . " AS MEM LEFT JOIN " . $this->get_table('users_attrib') . " AS MEB ON MEM.uid = MEB.uid WHERE MEM.uid = " . intval($uid);
  237. }
  238. else
  239. {
  240. $sql = "SELECT * FROM " . $this->get_table('users') . " WHERE uid = " . intval($uid);
  241. }
  242. if (! $user_info = $this->query_row($sql))
  243. {
  244. return false;
  245. }
  246. if (!$user_info['url_token'] AND $user_info['user_name'])
  247. {
  248. $user_info['url_token'] = urlencode($user_info['user_name']);
  249. }
  250. if ($attrib)
  251. {
  252. $users_info[$uid . '_attrib'] = $user_info;
  253. }
  254. else
  255. {
  256. $users_info[$uid] = $user_info;
  257. }
  258. return $user_info;
  259. }
  260. /**
  261. * 通过指量用户 uid 返回指量用户数据
  262. *
  263. * @param arrary $uids 用户 IDS
  264. * @param bool $attrib 是否返回附加表数据
  265. */
  266. function get_user_info_by_uids($uids, $attrib = false)
  267. {
  268. if (! is_array($uids) OR sizeof($uids) == 0)
  269. {
  270. return false;
  271. }
  272. array_walk_recursive($uids, 'intval_string');
  273. if ($attrib)
  274. {
  275. $sql = "SELECT MEM.*, MEB.* FROM " . $this->get_table('users') . " AS MEM LEFT JOIN " . $this->get_table('users_attrib') . " AS MEB ON MEM.uid = MEB.uid WHERE MEM.uid IN(" . implode(',', array_unique($uids)) . ")";
  276. }
  277. else
  278. {
  279. $sql = "SELECT * FROM " . $this->get_table('users') . " WHERE uid IN(" . implode(',', array_unique($uids)) . ")";
  280. }
  281. if ($user_info = $this->query_all($sql))
  282. {
  283. foreach($user_info as $key => $val)
  284. {
  285. if (!$val['url_token'])
  286. {
  287. $val['url_token'] = urlencode($val['user_name']);
  288. }
  289. $data[$val['uid']] = $val;
  290. }
  291. }
  292. return $data;
  293. }
  294. /**
  295. * 通过用户 uid 获取用户信息
  296. * @param $username
  297. * @return
  298. */
  299. function get_email_setting_by_uid($uid)
  300. {
  301. if (! $uid)
  302. {
  303. return false;
  304. }
  305. return $this->fetch_row('users_email_setting', 'uid = ' . intval($uid));
  306. }
  307. /**
  308. * 根据用户ID获取用户通知设置
  309. * @param $uid
  310. */
  311. function get_notification_setting_by_uid($uid)
  312. {
  313. $setting = $this->fetch_row('users_notification_setting', 'uid = ' . intval($uid));
  314. if (empty($setting))
  315. {
  316. return array('data' => array());
  317. }
  318. $setting['data'] = unserialize($setting['data']);
  319. if (empty($setting['data']))
  320. {
  321. $setting['data'] = array();
  322. }
  323. return $setting;
  324. }
  325. /**
  326. * 编辑邀请名额
  327. *
  328. * @param int $uid
  329. * @param int $value 正数为加 负数为减
  330. */
  331. function edit_invitation_available($uid, $value)
  332. {
  333. $uid = intval($uid);
  334. $value = intval($value);
  335. if (! $uid OR !$value)
  336. {
  337. return false;
  338. }
  339. //增加
  340. if ($value >= 1)
  341. {
  342. return $this->query("UPDATE " . $this->get_table('users') . " SET invitation_available = invitation_available + " . $value . " WHERE uid = " . $uid);
  343. }
  344. else if ($value < 1)
  345. {
  346. $value = $value * - 1;
  347. return $this->query("UPDATE " . $this->get_table('users') . " SET invitation_available = invitation_available - " . $value . " WHERE uid = " . $uid);
  348. }
  349. else
  350. {
  351. return false;
  352. }
  353. }
  354. function insert_user($username, $password, $email, $sex = 0, $mobile = null)
  355. {
  356. if ($username == '')
  357. {
  358. return false;
  359. }
  360. $salt = fetch_salt(4);
  361. if ($uid = $this->insert('users', array(
  362. 'user_name' => htmlspecialchars($username),
  363. 'password' => compile_password($password, $salt),
  364. 'salt' => $salt,
  365. 'email' => htmlspecialchars($email),
  366. 'sex' => intval($sex),
  367. 'mobile' => htmlspecialchars($mobile),
  368. 'reg_time' => time(),
  369. 'reg_ip' => ip2long(fetch_ip())
  370. )))
  371. {
  372. $this->insert('users_attrib', array(
  373. 'uid' => $uid
  374. ));
  375. $this->insert('users_email_setting', array(
  376. 'uid' => $uid
  377. ));
  378. $this->update_notification_setting_fields(null, $uid);
  379. //$this->model('search_index')->push_index('user', $username, $uid);
  380. }
  381. return $uid;
  382. }
  383. function user_register($user_name, $password, $email, $email_valid = false)
  384. {
  385. if ($uid = $this->insert_user($user_name, $password, $email))
  386. {
  387. if ($def_focus_uids_str = get_setting('def_focus_uids'))
  388. {
  389. $def_focus_uids = explode(',', $def_focus_uids_str);
  390. foreach ($def_focus_uids as $key => $val)
  391. {
  392. $this->model('follow')->user_follow_add($uid, $val);
  393. }
  394. }
  395. $group_id = (get_setting('register_email_reqire') == 'N' || $email_valid) ? 4 : 3;
  396. $this->update_users_fields(array(
  397. 'valid_email' => intval($email_valid),
  398. 'group_id' => $group_id,
  399. 'reputation_group' => 5,
  400. 'invitation_available' => get_setting('newer_invitation_num'),
  401. 'is_first_login' => 1
  402. ), $uid);
  403. $this->model('integral')->process($uid, 'REGISTER', get_setting('integral_system_config_register'), '初始资本');
  404. if ($email_valid)
  405. {
  406. $this->welcome_message($uid, $user_name, $email);
  407. }
  408. }
  409. return $uid;
  410. }
  411. function welcome_message($uid, $user_name, $email)
  412. {
  413. if (get_setting('welcome_message_email'))
  414. {
  415. load_class('core_mail')->send_mail(null, get_setting('site_name'), $email, $user_name, '欢迎来到 ' . get_setting('site_name'), str_replace(array('{username}', '{time}', '{sitename}'), array($user_name, date('Y-m-d H:i:s', time()), get_setting('site_name')), nl2br(get_setting('welcome_message_email'))));
  416. }
  417. if (get_setting('welcome_message_pm'))
  418. {
  419. $this->model('message')->send_message($uid, $uid, null, str_replace(array('{username}', '{time}', '{sitename}'), array($user_name, date('Y-m-d H:i:s', time()), get_setting('site_name')), get_setting('welcome_message_pm')), 0, 0);
  420. }
  421. }
  422. /**
  423. * 更新用户状态或字段
  424. * @param $update_data 字段
  425. * @param $userid 用户id
  426. * @return
  427. */
  428. function update_users_fields($update_data, $uid)
  429. {
  430. return $this->update('users', $update_data, 'uid = ' . intval($uid));
  431. }
  432. function update_user_name($user_name, $uid)
  433. {
  434. $this->update('user_name', array(
  435. 'user_name' => htmlspecialchars($user_name),
  436. ), 'uid = ' . intval($uid));
  437. //return $this->model('search_index')->push_index('user', $user_name, $uid);
  438. return true;
  439. }
  440. /**
  441. * 更新用户附加表状态或字段
  442. * @param $update_data 字段
  443. * @param $userid 用户id
  444. * @return
  445. */
  446. function update_users_attrib_fields($update_data, $uid)
  447. {
  448. return $this->update('users_attrib', $update_data, 'uid = ' . intval($uid));
  449. }
  450. /**
  451. * 更改用户密码
  452. *
  453. * @param $oldpassword 旧密码
  454. * @param $password 新密码
  455. * @param $userid 用户id
  456. * @param $salt 混淆码
  457. */
  458. function update_user_password($oldpassword, $password, $userid, $salt)
  459. {
  460. if ($salt == '')
  461. {
  462. return false;
  463. }
  464. $userid = intval($userid);
  465. if (! $userid)
  466. {
  467. return false;
  468. }
  469. $oldpassword = compile_password($oldpassword, $salt);
  470. if ($this->count('users', "uid = " . $userid . " AND password = '" . $this->quote($oldpassword) . "'") != 1)
  471. {
  472. return false;
  473. }
  474. return $this->update_user_password_ingore_oldpassword($password, $userid, $salt);
  475. }
  476. /**
  477. * 更改用户不用旧密码密码
  478. *
  479. * @param $password
  480. * @param $userid
  481. */
  482. function update_user_password_ingore_oldpassword($password, $uid, $salt)
  483. {
  484. if (!$salt OR !$password OR !$uid)
  485. {
  486. return false;
  487. }
  488. $update_data['password'] = compile_password($password, $salt);
  489. $update_data['salt'] = $salt;
  490. $this->update('users', $update_data, 'uid = ' . intval($uid));
  491. return true;
  492. }
  493. function clean_first_login($uid)
  494. {
  495. if (! $this->update('users', array(
  496. 'is_first_login' => 0
  497. ), 'uid = ' . intval($uid)))
  498. {
  499. return false;
  500. }
  501. else
  502. {
  503. return true;
  504. }
  505. }
  506. /**
  507. * 更新用户接收EMAIL设置
  508. *
  509. * @param $update_data 更新数组
  510. * @param $userid UID
  511. *
  512. * @return bool
  513. */
  514. function update_email_setting_fields($update_data, $uid)
  515. {
  516. if (!$uid)
  517. {
  518. return false;
  519. }
  520. if ($this->fetch_row('users_email_setting', 'uid = ' . intval($uid)))
  521. {
  522. $this->update('users_email_setting', $update_data, 'uid = ' . intval($uid));
  523. return true;
  524. }
  525. else
  526. {
  527. $update_data['uid'] = intval($uid);
  528. return $this->insert('users_email_setting', $update_data);
  529. }
  530. }
  531. /**
  532. * 更新用户最后登录时间
  533. * @param $userid 用户id
  534. * @param $login_time 登录时间戳(默认为当前时间,可为空)
  535. */
  536. function update_user_last_login($uid, $login_time = 0)
  537. {
  538. if (! $uid)
  539. {
  540. return false;
  541. }
  542. if (!$login_time)
  543. {
  544. $login_time = time();
  545. }
  546. $update_data['last_login'] = intval($login_time);
  547. $update_data['last_ip'] = ip2long(fetch_ip());
  548. return $this->update('users', $update_data, 'uid = ' . intval($uid));
  549. }
  550. /**
  551. * 更新用户通知设置
  552. *
  553. * @param $update_data 更新数组
  554. * @param $userid UID
  555. *
  556. * @return bool
  557. */
  558. function update_notification_setting_fields($update_data, $userid)
  559. {
  560. $userid = intval($userid);
  561. $user_setting = $this->fetch_row('users_notification_setting', 'uid = ' . $userid);
  562. if (empty($user_setting))
  563. {
  564. $update_data['uid'] = $userid;
  565. $this->insert('users_notification_setting', $update_data);
  566. }
  567. else
  568. {
  569. $this->update('users_notification_setting', $update_data, 'uid = ' . $userid);
  570. }
  571. return true;
  572. }
  573. /*
  574. public function add_user_online_time($uid, $online_time)
  575. {
  576. return $this->query("UPDATE " . get_table('users') . ' SET online_time = online_time + ' . intval($online_time) . ' WHERE uid = ' . intval($uid));
  577. }
  578. */
  579. /**
  580. * 修改用户所属的统计数值 [未读系统通知,未读短信息,粉丝数,观众数,问我数量,问题总数,回复问题数量,
  581. * 编辑过的数量,话题数量,比赛数量
  582. * @param int $state_type
  583. * @param int $state_num
  584. *
  585. * @return boolean
  586. */
  587. public function increase_user_statistics($state_type, $state_num = 1, $uid = null)
  588. {
  589. if (!$uid)
  590. {
  591. return false;
  592. }
  593. $state_array = array(
  594. self::ANSWER_COUNT,
  595. self::NOTICE_UNREAD,
  596. self::NOTIFICATION_UNREAD,
  597. self::INVITE_COUNT,
  598. );
  599. if (! in_array($state_type, $state_array))
  600. {
  601. return false;
  602. }
  603. //未读通知
  604. if ($state_type == self::NOTIFICATION_UNREAD)
  605. {
  606. return $this->update('users', array(
  607. $state_type => $this->model('notify')->get_notifications_unread_num($uid)
  608. ), 'uid = ' . intval($uid));
  609. }
  610. //未读私信
  611. if ($state_type == self::NOTICE_UNREAD)
  612. {
  613. return $this->update('users', array(
  614. $state_type => $this->model('message')->get_message_unread_num($uid)
  615. ), 'uid = ' . intval($uid));
  616. }
  617. //回复计数
  618. if ($state_type == self::ANSWER_COUNT)
  619. {
  620. $question_answer_count = $this->count('answer', 'uid = ' . intval($uid));
  621. return $this->update('users', array(
  622. $state_type => $question_answer_count
  623. ), 'uid = ' . intval($uid));
  624. }
  625. //受邀请参与问题计数
  626. if ($state_type == self::INVITE_COUNT)
  627. {
  628. $count = $this->count('question_invite', 'recipients_uid = ' . intval($uid));
  629. return $this->update('users', array(
  630. $state_type => $count
  631. ), 'uid = ' . intval($uid));
  632. }
  633. return $this->update('users', array(
  634. $state_type => ($state_type + $state_num)
  635. ), 'uid = ' . intval($uid));
  636. }
  637. /**
  638. * 设置登录时候的COOKIE信息
  639. *
  640. * @param $userid
  641. * @param $username
  642. * @param $password
  643. *
  644. * @return true
  645. */
  646. function setcookie_login($uid, $user_name, $password, $salt, $expire = null, $hash_password = true)
  647. {
  648. if (! $uid)
  649. {
  650. return false;
  651. }
  652. if (! $expire)
  653. {
  654. HTTP::set_cookie('_user_login', get_login_cookie_hash($user_name, $password, $salt, $uid, $hash_password));
  655. }
  656. else
  657. {
  658. $expire = time() + $expire;
  659. HTTP::set_cookie('_user_login', get_login_cookie_hash($user_name, $password, $salt, $uid, $hash_password), $expire);
  660. }
  661. return true;
  662. }
  663. /**
  664. * 设置退出时候的COOKIE信息
  665. * @param $userid
  666. * @param $username
  667. * @param $password
  668. * @param $expire
  669. * @return
  670. */
  671. function setcookie_logout()
  672. {
  673. HTTP::set_cookie('_user_login', '', time() - 3600);
  674. }
  675. public function logout()
  676. {
  677. $this->setcookie_logout();
  678. $this->setsession_logout();
  679. }
  680. function setsession_logout()
  681. {
  682. if (isset($_SESSION['client_info']))
  683. {
  684. unset($_SESSION['client_info']);
  685. }
  686. if (isset($_SESSION['permission']))
  687. {
  688. unset($_SESSION['permission']);
  689. }
  690. }
  691. function set_admin_login($uid)
  692. {
  693. $_SESSION['admin_login'] = H::encode_hash(array('uid' => $uid, 'UA' => $_SERVER['HTTP_USER_AGENT'], 'ip' => $_SERVER['REMOTE_ADDR']));
  694. }
  695. function admin_logout()
  696. {
  697. if (isset($_SESSION['admin_login']))
  698. {
  699. unset($_SESSION['admin_login']);
  700. }
  701. }
  702. /**
  703. * 检查用户名的字符
  704. * @param $username
  705. * @return
  706. */
  707. function check_username_char($username)
  708. {
  709. $flag = false;
  710. $length = strlen(iconv('UTF-8', 'gb2312', $username));
  711. $length_min = intval(get_setting('username_length_min'));
  712. $length_max = intval(get_setting('username_length_max'));
  713. if ($length < $length_min || $length > $length_max)
  714. {
  715. $flag = true;
  716. }
  717. switch(get_setting('username_rule'))
  718. {
  719. case '1' :
  720. if (!preg_match('/^[\x{4e00}-\x{9fa5}_a-zA-Z0-9]+$/u', $username) || $flag)
  721. {
  722. return '请输入 ' . ceil ($length_min/2) . ' -' . floor($length_max/2) . ' 个汉字或' . $length_min . '-' . $length_max . ' 个字母、数字';
  723. }
  724. break;
  725. case '2' :
  726. if (!preg_match("/^[a-zA-Z0-9_]+$/i", $username) || $flag)
  727. {
  728. return '请输入 ' . $length_min . '-' . $length_max . ' 个字母、数字或下划线';
  729. }
  730. break;
  731. case '3' :
  732. if (!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $username) || $flag)
  733. {
  734. return '请输入 ' . ceil ($length_min/2) . ' -' . floor($length_max/2) . ' 个汉字';
  735. }
  736. break;
  737. }
  738. return false;
  739. }
  740. /**
  741. *
  742. * 根据where条件批量获取用户
  743. * @param string $where
  744. * @param int $limit
  745. *
  746. * @return array
  747. */
  748. public function get_user_list($where = '', $limit = 10, $orderby = 'uid ASC')
  749. {
  750. if ($where)
  751. {
  752. $where = ' WHERE forbidden = 0 AND group_id <> 3 AND ' . $where;
  753. }
  754. return $this->query_all("SELECT uid FROM " . $this->get_table('users') . $where . " ORDER BY {$orderby} LIMIT " . $limit);
  755. }
  756. /**
  757. *
  758. * @param string $where
  759. * @param int $limit
  760. *
  761. * @return array
  762. */
  763. public function get_users_list($where, $limit = 10, $attrib = false, $exclude_self = true, $orderby = 'uid DESC')
  764. {
  765. if ($attrib)
  766. {
  767. if ($where)
  768. {
  769. $where = ' WHERE MEM.forbidden = 0 AND MEM.group_id <> 3 AND (' . $where . ')';
  770. }
  771. else
  772. {
  773. $where = ' WHERE MEM.forbidden = 0 AND MEM.group_id <> 3';
  774. }
  775. if ($exclude_self)
  776. {
  777. if ($where)
  778. {
  779. $where .= " AND MEM.uid <> " . USER::get_client_uid();
  780. }
  781. else
  782. {
  783. $where = " WHERE MEM.uid <> " . USER::get_client_uid();
  784. }
  785. }
  786. $result = $this->query_all("SELECT MEM.*, MEB.* FROM " . $this->get_table('users') . " MEM LEFT JOIN " . $this->get_table('users_attrib') . " AS MEB ON MEM.uid = MEB.uid " . $where . " ORDER BY MEM.{$orderby}", $limit);
  787. }
  788. else
  789. {
  790. if ($exclude_self)
  791. {
  792. if ($where)
  793. {
  794. $where .= ' AND forbidden = 0 AND group_id <> 3 AND uid <> ' . USER::get_client_uid();
  795. }
  796. else
  797. {
  798. $where = ' forbidden = 0 AND group_id <> 3 AND uid <> ' . USER::get_client_uid();
  799. }
  800. }
  801. $result = $this->fetch_all('users', $where, $orderby, $limit);
  802. }
  803. if ($result)
  804. {
  805. foreach ($result AS $key => $val)
  806. {
  807. if (!$val['url_token'] AND $val['user_name'])
  808. {
  809. $result[$key]['url_token'] = urlencode($val['user_name']);
  810. }
  811. }
  812. }
  813. return $result;
  814. }
  815. public function get_users_list_by_search($count = false, $search_data = null)
  816. {
  817. $where = array();
  818. $page = 0;
  819. $per_page = 0;
  820. $sort_key = 'uid';
  821. $order = 'DESC';
  822. if (is_array($search_data))
  823. {
  824. extract($search_data);
  825. }
  826. if ($user_name)
  827. {
  828. $where[] = "user_name LIKE '%" . $this->quote($user_name) . "%'";
  829. }
  830. if ($email)
  831. {
  832. $where[] = "email = '" . $this->quote($email) . "'";
  833. }
  834. if ($group_id)
  835. {
  836. $where[] = 'group_id = ' . intval($group_id);
  837. }
  838. if ($reg_date)
  839. {
  840. $reg_time = intval(strtotime($reg_date));
  841. $where[] = 'reg_time BETWEEN ' . $reg_time . ' AND ' . ($reg_time + 86400);
  842. }
  843. if ($last_login_date)
  844. {
  845. $last_login_time = intval(strtotime($last_login_date));
  846. $where[] = 'last_login BETWEEN ' . $last_login_time . ' AND ' . ($last_login_time + 86400);
  847. }
  848. if ($ip)
  849. {
  850. if (preg_match('/.*\.\\*$/i', $ip))
  851. {
  852. $ip_base = ip2long(str_replace('*', '0', $ip));
  853. $where[] = 'last_ip BETWEEN ' . $ip_base . ' AND ' . ($ip_base + 255);
  854. }
  855. else
  856. {
  857. $where[] = 'last_ip = ' . ip2long($ip);
  858. }
  859. }
  860. if ($integral_min || $integral_min == '0')
  861. {
  862. $where[] = 'integral >= ' . intval($integral_min);
  863. }
  864. if ($integral_max || $integral_max == '0')
  865. {
  866. $where[] = 'integral <= ' . intval($integral_max);
  867. }
  868. if ($reputation_min || $reputation_min == '0')
  869. {
  870. $where[] = 'reputation >= ' . intval($reputation_min);
  871. }
  872. if ($reputation_max || $reputation_max == '0')
  873. {
  874. $where[] = 'reputation <= ' . intval($reputation_max);
  875. }
  876. if ($answer_count_min || $answer_count_min == '0')
  877. {
  878. $where[] = 'answer_count >= ' . intval($answer_count_min);
  879. }
  880. if ($answer_count_max || $answer_count_max == '0')
  881. {
  882. $where[] = 'answer_count <= ' . intval($answer_count_max);
  883. }
  884. if ($job_id)
  885. {
  886. $where[] = 'job_id = ' . intval($job_id);
  887. }
  888. if ($province)
  889. {
  890. $where[] = "province = '" . $this->quote($province) . "'";
  891. }
  892. if ($city)
  893. {
  894. $where[] = "city = '" . $this->quote($city) . "'";
  895. }
  896. if ($birthday)
  897. {
  898. $birthday_time = intval(strtotime($birthday));
  899. $where[] = 'last_login BETWEEN ' . $birthday_time . ' AND ' . ($birthday_time + 86400);
  900. }
  901. if ($signature)
  902. {
  903. $attrib_list = $this->fetch_all('users_attrib', "signature LIKE '%" . $this->quote($signature) . "%'");
  904. $where[] = 'uid IN (' . implode(',', array_merge(array(0), fetch_array_value($attrib_list, 'uid'))) . ')';
  905. }
  906. if ($common_email)
  907. {
  908. $where[] = "common_email = '" . $this->quote($common_email) . "'";
  909. }
  910. if ($mobile)
  911. {
  912. $where[] = 'mobile = ' . $this->quote($mobile);
  913. }
  914. if ($qq)
  915. {
  916. $attrib_list = $this->fetch_all('users_attrib', 'qq = ' . intval($qq));
  917. $where[] = 'uid IN (' . implode(',', array_merge(array(0), fetch_array_value($attrib_list, 'uid'))) . ')';
  918. }
  919. if ($homepage)
  920. {
  921. $attrib_list = $this->fetch_all('users_attrib', "homepage LIKE '%" . $this->quote($homepage) . "%'");
  922. $where[] = 'uid IN (' . implode(',', array_merge(array(0), fetch_array_value($attrib_list, 'uid'))) . ')';
  923. }
  924. if ($school_name)
  925. {
  926. $edu_list = $this->fetch_all('education_experience', "school_name LIKE '%" . $this->quote($school_name) . "%'");
  927. $where[] = 'uid IN (' . implode(',', array_merge(array(0), fetch_array_value($edu_list, 'uid'))) . ')';
  928. }
  929. if ($departments)
  930. {
  931. $edu_list = $this->fetch_all('education_experience', "departments LIKE '%" . $this->quote($departments) . "%'");
  932. $where[] = 'uid IN (' . implode(',', array_merge(array(0), fetch_array_value($edu_list, 'uid'))) . ')';
  933. }
  934. if ($company_name)
  935. {
  936. $work_list = $this->fetch_all('work_experience', "company_name LIKE '%" . $this->quote($company_name) . "%' AND job_id = " . intval($company_job_id));
  937. $where[] = 'uid IN (' . implode(',', array_merge(array(0), fetch_array_value($work_list, 'uid'))) . ')';
  938. }
  939. if ($count)
  940. {
  941. return $this->count('users', implode(' AND ', $where));
  942. }
  943. if ($user_list = $this->fetch_page('users', implode(' AND ', $where), $sort_key . ' ' . $order, $page, $per_page))
  944. {
  945. foreach($user_list as $key => $val)
  946. {
  947. if (!$val['url_token'])
  948. {
  949. $user_list[$key]['url_token'] = rawurlencode($val['user_name']);
  950. }
  951. }
  952. return $user_list;
  953. }
  954. else
  955. {
  956. return array();
  957. }
  958. }
  959. /**
  960. * 批量获取多个话题关注的用户列表
  961. * @param $topics_array
  962. */
  963. public function get_users_list_by_topic_focus($topic_ids)
  964. {
  965. if ( !is_array($topic_ids) OR sizeof($topic_ids))
  966. {
  967. return false;
  968. }
  969. array_walk_recursive($topic_ids, 'intval_string');
  970. return $this->query_all("SELECT DISTINCT uid, topic_id FROM " . $this->get_table('topic_focus') . " WHERE topic_id IN(" . implode(",", $topic_ids) . ")");
  971. }
  972. /**
  973. *
  974. * 根据where条件获取用户数量
  975. * @param string $where
  976. * @param int $limit
  977. *
  978. * @return array
  979. */
  980. public function get_user_count($where = '')
  981. {
  982. return $this->count('users', $where);
  983. }
  984. /**
  985. * 获取个人动态
  986. */
  987. function get_user_actions($uid, $limit = 10, $actions = false, $this_uid = 0, $distint = true)
  988. {
  989. $this_uid = intval($this_uid);
  990. $action_question = ACTION_LOG::ADD_QUESTION;
  991. if (strstr($actions, ','))
  992. {
  993. $action_question = explode(',', $actions);
  994. array_walk_recursive($action_question, 'intval_string');
  995. $action_question = implode(',', $action_question);
  996. }
  997. else
  998. {
  999. $action_question = intval($actions);
  1000. }
  1001. if (!$uid)
  1002. {
  1003. $where[] = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND associate_action IN(" . $this->quote($action_question) . "))";
  1004. }
  1005. else
  1006. {
  1007. $where[] = "(associate_type = " . ACTION_LOG::CATEGORY_QUESTION . " AND uid = " . intval($uid) . " AND associate_action IN(" . $this->quote($action_question) . "))";
  1008. }
  1009. if ($this_uid == $uid)
  1010. {
  1011. $show_anonymous = true;
  1012. }
  1013. if ($distint)
  1014. {
  1015. $action_list = ACTION_LOG::get_actions_distint_by_where(implode($where, ' OR '), $limit, null, $show_anonymous);
  1016. }
  1017. else
  1018. {
  1019. $action_list = ACTION_LOG::get_action_by_where(implode($where, ' OR '), $limit, $show_anonymous);
  1020. }
  1021. //重组信息
  1022. foreach ($action_list as $key => $val)
  1023. {
  1024. $users_ids[] = $val['uid'];
  1025. switch ($val['associate_type'])
  1026. {
  1027. case ACTION_LOG::CATEGORY_QUESTION:
  1028. $question_ids[] = $val['associate_id'];
  1029. break;
  1030. }
  1031. }
  1032. if ($users_ids)
  1033. {
  1034. $action_list_users = $this->get_user_info_by_uids($users_ids, true);
  1035. }
  1036. if ($question_ids)
  1037. {
  1038. $action_questions_info = $this->model('question')->get_question_info_by_ids($question_ids);
  1039. if ($this_uid)
  1040. {
  1041. $action_questions_focus = $this->model('question')->has_focus_questions($question_ids, $this_uid);
  1042. }
  1043. else if ($uid)
  1044. {
  1045. $action_questions_focus = $this->model('question')->has_focus_questions($question_ids, $uid);
  1046. }
  1047. }
  1048. foreach ($action_list as $key => $val)
  1049. {
  1050. $action_list[$key]['user_info'] = $action_list_users[$val['uid']];
  1051. switch ($val['associate_type'])
  1052. {
  1053. case ACTION_LOG::CATEGORY_QUESTION :
  1054. $question_info = $action_questions_info[$val['associate_id']];
  1055. if (in_array($val['associate_action'], array(
  1056. ACTION_LOG::ADD_TOPIC,
  1057. ACTION_LOG::MOD_TOPIC,
  1058. ACTION_LOG::MOD_TOPIC_DESCRI,
  1059. ACTION_LOG::MOD_TOPIC_PIC,
  1060. ACTION_LOG::DELETE_TOPIC,
  1061. ACTION_LOG::ADD_TOPIC_FOCUS,
  1062. ACTION_LOG::DELETE_TOPIC_FOCUS,
  1063. ACTION_LOG::ADD_TOPIC_PARENT,
  1064. ACTION_LOG::DELETE_TOPIC_PARENT
  1065. )) AND $val['associate_attached'])
  1066. {
  1067. $topic_info = $this->model('topic')->get_topic_by_id($val['associate_attached']);
  1068. }
  1069. if (in_array($val['associate_action'], array(
  1070. ACTION_LOG::ADD_QUESTION
  1071. )) AND $question_info['has_attach'])
  1072. {
  1073. $question_info['attachs'] = $this->model('publish')->get_attach('question', $question_info['question_id'], 'min'); //获取附件
  1074. }
  1075. if ($val['uid'])
  1076. {
  1077. $question_info['last_action_str'] = ACTION_LOG::format_action_str($val['associate_action'], $val['uid'], $action_list_users[$val['uid']]['user_name'], $question_info, $topic_info);
  1078. }
  1079. if (in_array($val['associate_action'], array(
  1080. ACTION_LOG::ANSWER_QUESTION
  1081. )) AND $question_info['answer_count'] > 0)
  1082. {
  1083. $answer_list = $this->model('answer')->get_answer_by_id($val['associate_attached']);
  1084. }
  1085. else
  1086. {
  1087. $answer_list = null;
  1088. }
  1089. if (! empty($answer_list))
  1090. {
  1091. $user_info = $this->get_user_info_by_uid($answer_list['uid'], true);
  1092. $answer_list['user_name'] = $user_info['user_name'];
  1093. $answer_list['url_token'] = $user_info['url_token'];
  1094. $answer_list['signature'] = $user_info['signature'];
  1095. $answer_list['answer_content'] = strip_ubb($answer_list['answer_content']);
  1096. $question_info['answer_info'] = $answer_list;
  1097. if ($answer_list['has_attach'])
  1098. {
  1099. $answer_list['attachs'] = $this->model('publish')->get_attach('answer', $val['associate_attached'], 'min');
  1100. }
  1101. }
  1102. $action_list[$key]['has_focus'] = $action_questions_focus[$question_info['question_id']];
  1103. //还原到单个数组ROW里面
  1104. if ($question_info)
  1105. {
  1106. foreach ($question_info as $qkey => $qval)
  1107. {
  1108. if ($qkey == 'add_time')
  1109. {
  1110. continue;
  1111. }
  1112. $action_list[$key][$qkey] = $qval;
  1113. }
  1114. }
  1115. //$action_list[$key]['topics'] = $action_questions_topics[$question_info['question_id']];
  1116. break;
  1117. }
  1118. }
  1119. return $action_list;
  1120. }
  1121. public function get_user_recommend_v2($uid, $limit = 10)
  1122. {
  1123. if ($friends = $this->model('follow')->get_user_friends($uid, 100))
  1124. {
  1125. foreach ($friends as $key => $val)
  1126. {
  1127. $follow_uids[] = $val['friend_uid'];
  1128. $follow_users_array[$val['friend_uid']] = $val;
  1129. }
  1130. }
  1131. if (! $follow_uids)
  1132. {
  1133. return $this->get_users_list(false, $limit, true);
  1134. }
  1135. $users_ids = array();
  1136. if ($users_focus = $this->query_all("SELECT DISTINCT friend_uid, fans_uid FROM " . $this->get_table('user_follow') . " WHERE fans_uid IN(" . implode($follow_uids, ',') . ") ORDER BY follow_id DESC LIMIT " . $limit))
  1137. {
  1138. foreach ($users_focus as $key => $val)
  1139. {
  1140. $users_ids[] = $val['friend_uid'];
  1141. if (! isset($users_ids_rtype[$val['friend_uid']]))
  1142. {
  1143. $users_ids_rtype[$val['friend_uid']] = array(
  1144. 'type' => 'friend',
  1145. 'fans_uid' => $val['fans_uid']
  1146. ); //推荐类型
  1147. }
  1148. }
  1149. }
  1150. //取我关注的话题
  1151. if ($my_focus_topics = $this->model('topic')->get_focus_topic_list($uid, null))
  1152. {
  1153. foreach ($my_focus_topics as $key => $val)
  1154. {
  1155. $my_focus_topics_ids[] = $val['topic_id'];
  1156. $my_focus_topics_array[$val['topic_id']] = $val;
  1157. }
  1158. }
  1159. if ($my_focus_topics_ids)
  1160. {
  1161. $uids = $this->get_users_list_by_topic_focus($my_focus_topics_ids);
  1162. }
  1163. if ($uids)
  1164. {
  1165. foreach ($uids as $key => $val)
  1166. {
  1167. if (in_array($val['uid'], $users_ids))
  1168. {
  1169. continue;
  1170. }
  1171. $users_ids[$val['uid']] = $val['uid'];
  1172. if (! isset($users_ids_rtype[$val['friend_uid']]))
  1173. {
  1174. $users_ids_rtype[$val['uid']] = array(
  1175. "type" => "topic",
  1176. "topic_id" => $val['topic_id']
  1177. );
  1178. }
  1179. }
  1180. }
  1181. if (! $users_ids)
  1182. {
  1183. return $this->get_users_list("MEM.uid NOT IN (" . implode($follow_uids, ',') . ")", $limit, true);
  1184. }
  1185. $users = $this->query_all("SELECT MEM.*, MEB.signature
  1186. FROM " . $this->get_table('users') . " MEM
  1187. LEFT JOIN " . $this->get_table('users_attrib') . " AS MEB
  1188. ON MEM.uid = MEB.uid
  1189. WHERE (MEM.group_id <> 3 AND MEM.forbidden = 0) AND MEM.uid IN(" . implode($users_ids, ',') . ") AND MEM.uid NOT IN (" . implode($follow_uids, ',') . ") AND MEM.uid <> " . $uid . " ORDER BY MEM.uid DESC LIMIT " . $limit);
  1190. foreach ($users as $key => $val)
  1191. {
  1192. $users[$key]['rtype'] = $users_ids_rtype[$val['uid']];
  1193. if ($users_ids_rtype[$val['uid']]['type'] == "friend")
  1194. {
  1195. $users[$key]['friend_users'] = $follow_users_array[$users[$key]['rtype']['fans_uid']];
  1196. }
  1197. else if ($users_ids_rtype[$val['uid']]['type'] == "topic")
  1198. {
  1199. $users[$key]['topic_info'] = $my_focus_topics_array[$users[$key]['rtype']['topic_id']];
  1200. }
  1201. if (!$val['url_token'])
  1202. {
  1203. $users[$key]['url_token'] = urlencode($val['user_name']);
  1204. }
  1205. }
  1206. return $users;
  1207. }
  1208. /**
  1209. * 根据职位ID获取职位信息
  1210. */
  1211. function get_jobs_by_id($id)
  1212. {
  1213. if (!$id)
  1214. {
  1215. return false;
  1216. }
  1217. static $jobs_info;
  1218. if (!$jobs_info[$id])
  1219. {
  1220. $jobs_info[$id] = $this->fetch_row('jobs', 'id = ' . intval($id));
  1221. }
  1222. return $jobs_info[$id];
  1223. }
  1224. /**
  1225. * 获取头像目录文件地址
  1226. * @param $uid
  1227. * @param $size
  1228. * @param $return_type 0=返回全部 1=返回目录(a/b/c/) 2=返回文件名
  1229. * @return string
  1230. */
  1231. function get_avatar($uid, $size = 'min', $return_type = 0)
  1232. {
  1233. $size = in_array($size, array(
  1234. 'max',
  1235. 'mid',
  1236. 'min',
  1237. '50',
  1238. '150'
  1239. )) ? $size : 'real';
  1240. $uid = abs(intval($uid));
  1241. $uid = sprintf("%09d", $uid);
  1242. $dir1 = substr($uid, 0, 3);
  1243. $dir2 = substr($uid, 3, 2);
  1244. $dir3 = substr($uid, 5, 2);
  1245. if ($return_type == 1)
  1246. {
  1247. return $dir1 . '/' . $dir2 . '/' . $dir3 . '/';
  1248. }
  1249. if ($return_type == 2)
  1250. {
  1251. return substr($uid, - 2) . '_avatar_' . $size . '.jpg';
  1252. }
  1253. return $dir1 . '/' . $dir2 . '/' . $dir3 . '/' . substr($uid, - 2) . '_avatar_' . $size . '.jpg';
  1254. }
  1255. /**
  1256. * 删除用户头像
  1257. * @param unknown_type $uid
  1258. * @return boolean
  1259. */
  1260. function delete_avatar($uid)
  1261. {
  1262. if (!$uid)
  1263. {
  1264. return false;
  1265. }
  1266. $avatar = $this->get_avatar($uid);
  1267. foreach( AWS_APP::config()->get('image')->avatar_thumbnail as $key => $val)
  1268. {
  1269. @unlink(get_setting('upload_dir').'/avatar/' . $this->get_avatar($uid, $key, 1) . $this->get_avatar($uid, $key, 2));
  1270. }
  1271. return $this->update_users_fields(array('avatar_file' => ''), $uid);
  1272. }
  1273. function update_thanks_count($uid)
  1274. {
  1275. $counter = $this->sum('answer', 'thanks_count', 'uid = ' . intval($uid));
  1276. $counter += $this->sum('question', 'thanks_count', 'published_uid = ' . intval($uid));
  1277. return $this->update('users', array(
  1278. 'thanks_count' => $counter
  1279. ), "uid = " . intval($uid));
  1280. }
  1281. // 获取活跃用户 (非垃圾用户)
  1282. function get_activity_random_users($limit = 10, $extra_info = false, $uid_not_in = array())
  1283. {
  1284. // 好友 & 粉丝 > 5, 回复 > 5, 根据登陆时间, 倒序
  1285. if (sizeof($uid_not_in) > 0)
  1286. {
  1287. $not_in_query = ' AND uid NOT IN(' . implode($uid_not_in, ',') . ')';
  1288. }
  1289. if ($extra_info)
  1290. {
  1291. $sql = "SELECT uid FROM " . $this->get_table('users') . " WHERE fans_count > 5 AND friend_count > 5 AND answer_count > 1 " . $not_in_query . " ORDER BY last_login DESC LIMIT " . $limit;
  1292. if (! $rs = $this->query_all($sql))
  1293. {
  1294. return false;
  1295. }
  1296. foreach ($rs as $key => $val)
  1297. {
  1298. $user_id_array[] = $val['uid'];
  1299. }
  1300. if ($user_id_array)
  1301. {
  1302. return $this->get_user_info_by_uids($user_id_array, true);
  1303. }
  1304. return false;
  1305. }
  1306. return $this->fetch_all('users', "fans_count > 5 AND friend_count > 5 AND answer_count > 1 " . $not_in_query, 'last_login DESC', $limit);
  1307. }
  1308. function add_group($group_name, $reputation_lower, $reputation_higer, $reputation_factor)
  1309. {
  1310. $data = array(
  1311. 'type' => 1,
  1312. 'custom' => 1,
  1313. 'group_name' => $group_name,
  1314. 'reputation_lower' => $reputation_lower,
  1315. 'reputation_higer' => $reputation_higer,
  1316. 'reputation_factor' => $reputation_factor,
  1317. );
  1318. return $this->insert('users_group', $data);
  1319. }
  1320. function delete_group($group_id)
  1321. {
  1322. return $this->delete('users_group', 'group_id = ' . intval($group_id));
  1323. }
  1324. function update_group($group_id, $data)
  1325. {
  1326. return $this->update('users_group', $data, 'group_id = ' . intval($group_id));
  1327. }
  1328. function get_group_by_id($group_id, $field = null)
  1329. {
  1330. if (!$group_id)
  1331. {
  1332. return false;
  1333. }
  1334. static $groups;
  1335. if (isset($groups[$group_id]))
  1336. {
  1337. if ($field)
  1338. {
  1339. return $groups[$group_id][$field];
  1340. }
  1341. else
  1342. {
  1343. return $groups[$group_id];
  1344. }
  1345. }
  1346. $group = $this->fetch_row('users_group', 'group_id = ' . intval($group_id));
  1347. if ($group['permission'])
  1348. {
  1349. $group['permission'] = unserialize($group['permission']);
  1350. }
  1351. $groups[$group_id] = $group;
  1352. if ($field)
  1353. {
  1354. return $group[$field];
  1355. }
  1356. return $group;
  1357. }
  1358. function get_user_group_list($type = 0)
  1359. {
  1360. $group = array();
  1361. if ($users_groups = $this->fetch_all('users_group', 'type = ' . intval($type)))
  1362. {
  1363. foreach ($users_groups as $key => $val)
  1364. {
  1365. $group[$val['group_id']] = $val;
  1366. }
  1367. }
  1368. return $group;
  1369. }
  1370. function get_user_group_by_reputation($reputation, $field = null)
  1371. {
  1372. if ($mem_groups = $this->get_user_group_list(1))
  1373. {
  1374. foreach ($mem_groups as $key => $val)
  1375. {
  1376. if ((intval($reputation) >= intval($val['reputation_lower'])) && (intval($reputation) < intval($val['reputation_higer'])))
  1377. {
  1378. $group = $val;
  1379. break;
  1380. }
  1381. }
  1382. }
  1383. else // 若会员组为空,则返回为普通会员组
  1384. {
  1385. $system_groups = $this->get_user_group_list(0);
  1386. $group = $system_groups[4];
  1387. }
  1388. if ($field)
  1389. {
  1390. return $group[$field];
  1391. }
  1392. return $group;
  1393. }
  1394. function update_user_reputation_group($uid)
  1395. {
  1396. $user_info = $this->get_user_info_by_uid($uid);
  1397. $reputation_group = $this->get_user_group_by_reputation($user_info['reputation'], 'group_id');
  1398. if ($reputation_group != $user_info['reputation_group'])
  1399. {
  1400. return $this->update_users_fields(array(
  1401. 'reputation_group' => $reputation_group
  1402. ), $uid);
  1403. }
  1404. return false;
  1405. }
  1406. function get_user_group($group_id, $reputation_group = 0)
  1407. {
  1408. if ($group_id == 4)
  1409. {
  1410. $group_info = $this->model('account')->get_group_by_id($reputation_group);
  1411. }
  1412. if (!$group_info)
  1413. {
  1414. return $this->model('account')->get_group_by_id($group_id);
  1415. }
  1416. return $group_info;
  1417. }
  1418. function check_url_token($url_token, $uid)
  1419. {
  1420. return $this->count('users', "(url_token = '" . $this->quote($url_token) . "' OR user_name = '" . $this->quote($url_token) . "') AND uid != " . intval($uid));
  1421. }
  1422. function update_url_token($url_token, $uid)
  1423. {
  1424. return $this->update('users', array(
  1425. 'url_token' => $url_token,
  1426. 'url_token_update' => time()
  1427. ), 'uid = ' . intval($uid));
  1428. }
  1429. function get_users_search_by_name($name)
  1430. {
  1431. return $this->fetch_row('users_search', "name = '" . $this->quote($name) . "'");
  1432. }
  1433. function delete_users_search_by_id($id)
  1434. {
  1435. return $this->delete('users_search', 'id = ' . intval($id));
  1436. }
  1437. function save_users_search($name, $search_data)
  1438. {
  1439. return $this->insert('users_search', array(
  1440. 'name' => $name,
  1441. 'search_data' => serialize($search_data),
  1442. ));
  1443. }
  1444. function update_users_search($id, $search_data)
  1445. {
  1446. return $this->update('users_search', array('search_data' => serialize($search_data)), 'id = ' . intval($id));
  1447. }
  1448. function get_users_search_list()
  1449. {
  1450. if ($rs = $this->fetch_all('users_search', null, 'id DESC'))
  1451. {
  1452. foreach($rs as $key => $val)
  1453. {
  1454. if ($rs[$key]['search_data'] = unserialize($val['search_data']))
  1455. {
  1456. $url_param = array();
  1457. foreach($rs[$key]['search_data'] as $rkey => $rval)
  1458. {
  1459. $url_param[] = $rkey . '-' . $rval;
  1460. }
  1461. $rs[$key]['search_param'] = implode('__', $url_param);
  1462. }
  1463. }
  1464. return $rs;
  1465. }
  1466. else
  1467. {
  1468. return array();
  1469. }
  1470. }
  1471. function forbidden_user($uid, $status, $admin_uid)
  1472. {
  1473. if (!$uid)
  1474. {
  1475. return false;
  1476. }
  1477. $this->model('account')->update_users_fields(array(
  1478. 'forbidden' => intval($status)
  1479. ), intval($uid));
  1480. return $this->insert('users_forbidden', array(
  1481. 'uid' => intval($uid),
  1482. 'status' => intval($status),
  1483. 'admin_uid' => $admin_uid,
  1484. 'add_time' => time(),
  1485. ));
  1486. }
  1487. function get_forbidden_user_list($count = false, $order = 'uid DESC', $limit = 10)
  1488. {
  1489. if ($count)
  1490. {
  1491. return $this->count('users', 'forbidden = 1');
  1492. }
  1493. if ($user_list = $this->fetch_all('users', 'forbidden = 1', $order, $limit))
  1494. {
  1495. $uids = fetch_array_value($user_list, 'uid');
  1496. $users_forbidden = $this->fetch_all('users_forbidden', 'uid IN (' . implode(',', $uids) . ')', 'id DESC');
  1497. $admin_uids = fetch_array_value($users_forbidden, 'admin_uid');
  1498. $admin_user = $this->get_user_info_by_uids($admin_uids);
  1499. $forbidden_log = array();
  1500. foreach($users_forbidden as $key => $log)
  1501. {
  1502. if (!isset($forbidden_log[$log['uid']]))
  1503. {
  1504. $log['admin_info'] = $admin_user[$log['admin_uid']];
  1505. $forbidden_log[$log['uid']] = $log;
  1506. }
  1507. }
  1508. foreach ($user_list as $key => $user)
  1509. {
  1510. $user_list[$key]['forbidden_log'] = $forbidden_log[$user['uid']];
  1511. }
  1512. return $user_list;
  1513. }
  1514. else
  1515. {
  1516. return array();
  1517. }
  1518. }
  1519. public function set_default_timezone($time_zone, $uid)
  1520. {
  1521. return $this->update('users', array(
  1522. 'default_timezone' => htmlspecialchars($time_zone)
  1523. ), 'uid = ' . intval($uid));
  1524. }
  1525. }