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