Subs-MembersOnline.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file currently only holds the function for showing a list of online
  15. users used by the board index and SSI. In the future it'll also contain
  16. functions used by the Who's online page.
  17. array getMembersOnlineStats(array membersOnlineOptions)
  18. - retrieve a list and several other statistics of the users currently
  19. online on the forum.
  20. - used by the board index and SSI.
  21. - also returns the membergroups of the users that are currently online.
  22. - (optionally) hides members that chose to hide their online presense.
  23. */
  24. // Retrieve a list and several other statistics of the users currently online.
  25. function getMembersOnlineStats($membersOnlineOptions)
  26. {
  27. global $smcFunc, $context, $scripturl, $user_info, $modSettings, $txt;
  28. // The list can be sorted in several ways.
  29. $allowed_sort_options = array(
  30. 'log_time',
  31. 'real_name',
  32. 'show_online',
  33. 'online_color',
  34. 'group_name',
  35. );
  36. // Default the sorting method to 'most recent online members first'.
  37. if (!isset($membersOnlineOptions['sort']))
  38. {
  39. $membersOnlineOptions['sort'] = 'log_time';
  40. $membersOnlineOptions['reverse_sort'] = true;
  41. }
  42. // Not allowed sort method? Bang! Error!
  43. elseif (!in_array($membersOnlineOptions['sort'], $allowed_sort_options))
  44. trigger_error('Sort method for getMembersOnlineStats() function is not allowed', E_USER_NOTICE);
  45. // Initialize the array that'll be returned later on.
  46. $membersOnlineStats = array(
  47. 'users_online' => array(),
  48. 'list_users_online' => array(),
  49. 'online_groups' => array(),
  50. 'num_guests' => 0,
  51. 'num_spiders' => 0,
  52. 'num_buddies' => 0,
  53. 'num_users_hidden' => 0,
  54. 'num_users_online' => 0,
  55. );
  56. // Get any spiders if enabled.
  57. $spiders = array();
  58. $spider_finds = array();
  59. if (!empty($modSettings['show_spider_online']) && ($modSettings['show_spider_online'] < 3 || allowedTo('admin_forum')) && !empty($modSettings['spider_name_cache']))
  60. $spiders = unserialize($modSettings['spider_name_cache']);
  61. // Load the users online right now.
  62. $request = $smcFunc['db_query']('', '
  63. SELECT
  64. lo.id_member, lo.log_time, lo.id_spider, mem.real_name, mem.member_name, mem.show_online,
  65. mg.online_color, mg.id_group, mg.group_name
  66. FROM {db_prefix}log_online AS lo
  67. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
  68. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_mem_group} THEN mem.id_post_group ELSE mem.id_group END)',
  69. array(
  70. 'reg_mem_group' => 0,
  71. )
  72. );
  73. while ($row = $smcFunc['db_fetch_assoc']($request))
  74. {
  75. if (empty($row['real_name']))
  76. {
  77. // Do we think it's a spider?
  78. if ($row['id_spider'] && isset($spiders[$row['id_spider']]))
  79. {
  80. $spider_finds[$row['id_spider']] = isset($spider_finds[$row['id_spider']]) ? $spider_finds[$row['id_spider']] + 1 : 1;
  81. $membersOnlineStats['num_spiders']++;
  82. }
  83. // Guests are only nice for statistics.
  84. $membersOnlineStats['num_guests']++;
  85. continue;
  86. }
  87. elseif (empty($row['show_online']) && empty($membersOnlineOptions['show_hidden']))
  88. {
  89. // Just increase the stats and don't add this hidden user to any list.
  90. $membersOnlineStats['num_users_hidden']++;
  91. continue;
  92. }
  93. // Some basic color coding...
  94. if (!empty($row['online_color']))
  95. $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>';
  96. else
  97. $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  98. // Buddies get counted and highlighted.
  99. $is_buddy = in_array($row['id_member'], $user_info['buddies']);
  100. if ($is_buddy)
  101. {
  102. $membersOnlineStats['num_buddies']++;
  103. $link = '<strong>' . $link . '</strong>';
  104. }
  105. // A lot of useful information for each member.
  106. $membersOnlineStats['users_online'][$row[$membersOnlineOptions['sort']] . $row['member_name']] = array(
  107. 'id' => $row['id_member'],
  108. 'username' => $row['member_name'],
  109. 'name' => $row['real_name'],
  110. 'group' => $row['id_group'],
  111. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  112. 'link' => $link,
  113. 'is_buddy' => $is_buddy,
  114. 'hidden' => empty($row['show_online']),
  115. 'is_last' => false,
  116. );
  117. // This is the compact version, simply implode it to show.
  118. $membersOnlineStats['list_users_online'][$row[$membersOnlineOptions['sort']] . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link;
  119. // Store all distinct (primary) membergroups that are shown.
  120. if (!isset($membersOnlineStats['online_groups'][$row['id_group']]))
  121. $membersOnlineStats['online_groups'][$row['id_group']] = array(
  122. 'id' => $row['id_group'],
  123. 'name' => $row['group_name'],
  124. 'color' => $row['online_color']
  125. );
  126. }
  127. $smcFunc['db_free_result']($request);
  128. // If there are spiders only and we're showing the detail, add them to the online list - at the bottom.
  129. if (!empty($spider_finds) && $modSettings['show_spider_online'] > 1)
  130. foreach ($spider_finds as $id => $count)
  131. {
  132. $link = $spiders[$id] . ($count > 1 ? ' (' . $count . ')' : '');
  133. $sort = $membersOnlineOptions['sort'] = 'log_time' && $membersOnlineOptions['reverse_sort'] ? 0 : 'zzz_';
  134. $membersOnlineStats['users_online'][$sort . $spiders[$id]] = array(
  135. 'id' => 0,
  136. 'username' => $spiders[$id],
  137. 'name' => $link,
  138. 'group' => $txt['spiders'],
  139. 'href' => '',
  140. 'link' => $link,
  141. 'is_buddy' => false,
  142. 'hidden' => false,
  143. 'is_last' => false,
  144. );
  145. $membersOnlineStats['list_users_online'][$sort . $spiders[$id]] = $link;
  146. }
  147. // Time to sort the list a bit.
  148. if (!empty($membersOnlineStats['users_online']))
  149. {
  150. // Determine the sort direction.
  151. $sortFunction = empty($membersOnlineOptions['reverse_sort']) ? 'ksort' : 'krsort';
  152. // Sort the two lists.
  153. $sortFunction($membersOnlineStats['users_online']);
  154. $sortFunction($membersOnlineStats['list_users_online']);
  155. // Mark the last list item as 'is_last'.
  156. $userKeys = array_keys($membersOnlineStats['users_online']);
  157. $membersOnlineStats['users_online'][end($userKeys)]['is_last'] = true;
  158. }
  159. // Also sort the membergroups.
  160. ksort($membersOnlineStats['online_groups']);
  161. // Hidden and non-hidden members make up all online members.
  162. $membersOnlineStats['num_users_online'] = count($membersOnlineStats['users_online']) + $membersOnlineStats['num_users_hidden'] - (isset($modSettings['show_spider_online']) && $modSettings['show_spider_online'] > 1 ? count($spider_finds) : 0);
  163. return $membersOnlineStats;
  164. }
  165. // Check if the number of users online is a record and store it.
  166. function trackStatsUsersOnline($total_users_online)
  167. {
  168. global $modSettings, $smcFunc;
  169. $settingsToUpdate = array();
  170. // More members on now than ever were? Update it!
  171. if (!isset($modSettings['mostOnline']) || $total_users_online >= $modSettings['mostOnline'])
  172. $settingsToUpdate = array(
  173. 'mostOnline' => $total_users_online,
  174. 'mostDate' => time()
  175. );
  176. $date = strftime('%Y-%m-%d', forum_time(false));
  177. // No entry exists for today yet?
  178. if (!isset($modSettings['mostOnlineUpdated']) || $modSettings['mostOnlineUpdated'] != $date)
  179. {
  180. $request = $smcFunc['db_query']('', '
  181. SELECT most_on
  182. FROM {db_prefix}log_activity
  183. WHERE date = {date:date}
  184. LIMIT 1',
  185. array(
  186. 'date' => $date,
  187. )
  188. );
  189. // The log_activity hasn't got an entry for today?
  190. if ($smcFunc['db_num_rows']($request) === 0)
  191. {
  192. $smcFunc['db_insert']('ignore',
  193. '{db_prefix}log_activity',
  194. array('date' => 'date', 'most_on' => 'int'),
  195. array($date, $total_users_online),
  196. array('date')
  197. );
  198. }
  199. // There's an entry in log_activity on today...
  200. else
  201. {
  202. list ($modSettings['mostOnlineToday']) = $smcFunc['db_fetch_row']($request);
  203. if ($total_users_online > $modSettings['mostOnlineToday'])
  204. trackStats(array('most_on' => $total_users_online));
  205. $total_users_online = max($total_users_online, $modSettings['mostOnlineToday']);
  206. }
  207. $smcFunc['db_free_result']($request);
  208. $settingsToUpdate['mostOnlineUpdated'] = $date;
  209. $settingsToUpdate['mostOnlineToday'] = $total_users_online;
  210. }
  211. // Highest number of users online today?
  212. elseif ($total_users_online > $modSettings['mostOnlineToday'])
  213. {
  214. trackStats(array('most_on' => $total_users_online));
  215. $settingsToUpdate['mostOnlineToday'] = $total_users_online;
  216. }
  217. if (!empty($settingsToUpdate))
  218. updateSettings($settingsToUpdate);
  219. }
  220. ?>