PageRenderTime 29ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/community.php

https://github.com/nopticon/hyd
PHP | 275 lines | 213 code | 56 blank | 6 comment | 35 complexity | 5833e20036a4bbac39c78df4b22feacb MD5 | raw file
Possible License(s): MIT
  1. <?php namespace App;
  2. class community {
  3. private $default_title = 'COMMUNITY';
  4. private $default_view = 'community';
  5. public function __construct() {
  6. return;
  7. }
  8. public function getTitle($default = '') {
  9. return !empty($this->title) ? $this->title : $this->default_title;
  10. }
  11. public function getTemplate($default = '') {
  12. return !empty($this->template) ? $this->template : $this->default_view;
  13. }
  14. public function run() {
  15. global $user;
  16. $this->founders();
  17. $this->team();
  18. $this->recent_members();
  19. $this->birthdays();
  20. v_style([
  21. 'MEMBERS_COUNT' => number_format(config('max_users'))
  22. ]);
  23. //
  24. // Online
  25. //
  26. $sql = 'SELECT u.user_id, u.username, u.username_base, u.user_type, u.user_hideuser, s.session_ip
  27. FROM _members u, _sessions s
  28. WHERE s.session_time >= ?
  29. AND u.user_id = s.session_user_id
  30. ORDER BY u.username ASC, s.session_ip ASC';
  31. $this->online(sql_filter($sql, ($user->time - (5 * 60))), 'online', 'MEMBERS_ONLINE');
  32. //
  33. // Today Online
  34. //
  35. $minutes = date('is', time());
  36. $timetoday = (time() - (60 * intval($minutes[0].$minutes[1])) - intval($minutes[2].$minutes[3])) - (3600 * $user->format_date(time(), 'H'));
  37. $sql = 'SELECT user_id, username, username_base, user_hideuser, user_type
  38. FROM _members
  39. WHERE user_type NOT IN (??)
  40. AND user_lastvisit >= ?
  41. AND user_lastvisit < ?
  42. ORDER BY username';
  43. $this->online(sql_filter($sql, USER_INACTIVE, $timetoday, ($timetoday + 86399)), 'online', 'MEMBERS_TODAY', 'MEMBERS_VISIBLE');
  44. return true;
  45. }
  46. public function founders() {
  47. global $cache, $user, $comments;
  48. if (!$founders = $cache->get('founders', [])) {
  49. $sql = 'SELECT user_id, username, username_base, user_avatar
  50. FROM _members
  51. WHERE user_type = ?
  52. AND username_base <> ?
  53. ORDER BY user_id';
  54. $result = sql_rowset(sql_filter($sql, USER_FOUNDER, 'rockrepublik'));
  55. $founders = w();
  56. foreach ($result as $row) {
  57. $founders[$row['user_id']] = $comments->user_profile($row);
  58. }
  59. $cache->save('founders', $founders);
  60. }
  61. foreach ($founders as $user_id => $data) {
  62. _style('founders', [
  63. 'REALNAME' => $data['username'],
  64. 'USERNAME' => $data['username'],
  65. 'AVATAR' => $data['user_avatar'],
  66. 'PROFILE' => $data['profile']
  67. ]);
  68. }
  69. return;
  70. }
  71. public function team() {
  72. global $cache, $comments;
  73. if (!$teams = $cache->get('team', [])) {
  74. $sql = 'SELECT *
  75. FROM _team
  76. WHERE team_show = 1
  77. ORDER BY team_order';
  78. if ($teams = sql_rowset($sql)) {
  79. $cache->save('team', $teams);
  80. }
  81. }
  82. if (!$team = $cache->get('team_members', [])) {
  83. $sql = 'SELECT DISTINCT t.*, m.user_id, m.username, m.username_base, m.user_avatar
  84. FROM _team_members t, _members m
  85. WHERE t.member_id = m.user_id
  86. ORDER BY m.username';
  87. if ($team = sql_rowset($sql)) {
  88. $cache->save('team_members', $team);
  89. }
  90. }
  91. foreach ($team as $i => $row) {
  92. if (!$i) _style('team');
  93. $profile = $comments->user_profile($row);
  94. if (!isset($profile['real_name'])) {
  95. $profile['real_name'] = '';
  96. }
  97. _style('team.row', [
  98. 'USERNAME' => $profile['username'],
  99. 'REALNAME' => $profile['real_name'],
  100. 'PROFILE' => $profile['profile'],
  101. 'AVATAR' => $profile['user_avatar']
  102. ]);
  103. }
  104. return;
  105. }
  106. public function online($sql, $block, $block_title, $unset_legend = false) {
  107. global $user;
  108. static $user_bots;
  109. if (!isset($user_bots)) {
  110. obtain_bots($bots);
  111. $bots = w();
  112. foreach ($bots as $row) {
  113. $user_bots[$row['user_id']] = true;
  114. }
  115. }
  116. foreach (w('last_user_id users_visible users_hidden users_guests users_bots last_ip users_online') as $v) {
  117. ${$v} = 0;
  118. }
  119. _style($block, [
  120. 'L_TITLE' => lang($block_title)
  121. ]);
  122. _style($block . '.members');
  123. $is_founder = $user->is('founder');
  124. $result = sql_rowset($sql);
  125. foreach ($result as $row) {
  126. if ($row['user_id'] != GUEST) {
  127. if ($row['user_id'] != $last_user_id) {
  128. $is_bot = isset($user_bots[$row['user_id']]);
  129. if (!$row['user_hideuser']) {
  130. $username = $row['username'];
  131. if ($is_bot) {
  132. $users_bots++;
  133. } else {
  134. $users_visible++;
  135. }
  136. } else {
  137. $username = '*' . $row['username'];
  138. $users_hidden++;
  139. }
  140. if (((!$row['user_hideuser'] || $is_founder) && !$is_bot) || ($is_bot && $is_founder)) {
  141. _style($block . '.members.item', [
  142. 'USERNAME' => $username,
  143. 'PROFILE' => s_link('m', $row['username_base'])
  144. ]);
  145. }
  146. }
  147. $last_user_id = $row['user_id'];
  148. } else {
  149. if ($row['session_ip'] != $last_ip) {
  150. $users_guests++;
  151. }
  152. $last_ip = $row['session_ip'];
  153. }
  154. }
  155. $users_total = $users_visible + $users_hidden + $users_guests + $users_bots;
  156. if (!($users_visible + $users_hidden) || (!$users_visible && $users_hidden)) {
  157. _style($block . '.members.none');
  158. }
  159. _style($block . '.legend');
  160. $online_ary = [
  161. 'MEMBERS_TOTAL' => $users_total,
  162. 'MEMBERS_VISIBLE' => $users_visible,
  163. 'MEMBERS_GUESTS' => $users_guests,
  164. 'MEMBERS_HIDDEN' => $users_hidden,
  165. 'MEMBERS_BOT' => $users_bots
  166. ];
  167. if ($unset_legend !== false) {
  168. unset($online_ary[$unset_legend]);
  169. }
  170. foreach ($online_ary as $lk => $vk) {
  171. if (!$vk && $lk != 'MEMBERS_TOTAL') {
  172. continue;
  173. }
  174. _style($block . '.legend.item', [
  175. 'L_MEMBERS' => lang($lk . (($vk != 1) ? '2' : '')),
  176. 'ONLINE_VALUE' => $vk
  177. ]);
  178. }
  179. return;
  180. }
  181. public function birthdays() {
  182. global $comments;
  183. $sql = "SELECT user_id, username, username_base, user_avatar
  184. FROM _members
  185. WHERE user_birthday LIKE ?
  186. AND user_type NOT IN (??)
  187. ORDER BY user_posts DESC, username";
  188. if (!$result = sql_rowset(sql_filter($sql, date('%md'), USER_INACTIVE))) {
  189. return false;
  190. }
  191. foreach ($result as $i => $row) {
  192. if (!$i) _style('birthday');
  193. $profile = $comments->user_profile($row);
  194. _style('birthday.row', [
  195. 'USERNAME' => $profile['username'],
  196. 'PROFILE' => $profile['profile'],
  197. 'AVATAR' => $profile['user_avatar']
  198. ]);
  199. }
  200. return true;
  201. }
  202. public function recent_members() {
  203. global $user;
  204. $sql = 'SELECT username, username_base
  205. FROM _members
  206. WHERE user_type NOT IN (??)
  207. ORDER BY user_regdate DESC
  208. LIMIT 10';
  209. $result = sql_rowset(sql_filter($sql, USER_INACTIVE));
  210. foreach ($result as $i => $row) {
  211. if (!$i) _style('recent_members');
  212. _style('recent_members.item', [
  213. 'USERNAME' => $row['username'],
  214. 'PROFILE' => s_link('m', $row['username_base'])
  215. ]);
  216. }
  217. return true;
  218. }
  219. }