Subs-MembersOnline.php 8.6 KB

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