1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Provide a display for forum statistics |
||
5 | * |
||
6 | * Simple Machines Forum (SMF) |
||
7 | * |
||
8 | * @package SMF |
||
9 | * @author Simple Machines https://www.simplemachines.org |
||
10 | * @copyright 2020 Simple Machines and individual contributors |
||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1 RC2 |
||
14 | */ |
||
15 | |||
16 | if (!defined('SMF')) |
||
17 | die('No direct access...'); |
||
18 | |||
19 | /** |
||
20 | * Display some useful/interesting board statistics. |
||
21 | * |
||
22 | * gets all the statistics in order and puts them in. |
||
23 | * uses the Stats template and language file. (and main sub template.) |
||
24 | * requires the view_stats permission. |
||
25 | * accessed from ?action=stats. |
||
26 | */ |
||
27 | function DisplayStats() |
||
28 | { |
||
29 | global $txt, $scripturl, $modSettings, $context, $smcFunc; |
||
30 | |||
31 | isAllowedTo('view_stats'); |
||
32 | // Page disabled - redirect them out |
||
33 | if (empty($modSettings['trackStats'])) |
||
34 | fatal_lang_error('feature_disabled', true); |
||
35 | |||
36 | if (!empty($_REQUEST['expand'])) |
||
37 | { |
||
38 | $context['robot_no_index'] = true; |
||
39 | |||
40 | $month = (int) substr($_REQUEST['expand'], 4); |
||
41 | $year = (int) substr($_REQUEST['expand'], 0, 4); |
||
42 | if ($year > 1900 && $year < 2200 && $month >= 1 && $month <= 12) |
||
43 | $_SESSION['expanded_stats'][$year][] = $month; |
||
44 | } |
||
45 | elseif (!empty($_REQUEST['collapse'])) |
||
46 | { |
||
47 | $context['robot_no_index'] = true; |
||
48 | |||
49 | $month = (int) substr($_REQUEST['collapse'], 4); |
||
50 | $year = (int) substr($_REQUEST['collapse'], 0, 4); |
||
51 | if (!empty($_SESSION['expanded_stats'][$year])) |
||
52 | $_SESSION['expanded_stats'][$year] = array_diff($_SESSION['expanded_stats'][$year], array($month)); |
||
53 | } |
||
54 | |||
55 | // Handle the XMLHttpRequest. |
||
56 | if (isset($_REQUEST['xml'])) |
||
57 | { |
||
58 | // Collapsing stats only needs adjustments of the session variables. |
||
59 | if (!empty($_REQUEST['collapse'])) |
||
60 | obExit(false); |
||
61 | |||
62 | $context['sub_template'] = 'stats'; |
||
63 | $context['yearly'] = array(); |
||
64 | |||
65 | if (empty($month) || empty($year)) |
||
66 | return; |
||
67 | |||
68 | getDailyStats('YEAR(date) = {int:year} AND MONTH(date) = {int:month}', array('year' => $year, 'month' => $month)); |
||
69 | $context['yearly'][$year]['months'][$month]['date'] = array( |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() Comprehensibility
Best Practice
introduced
by
|
|||
70 | 'month' => sprintf('%02d', $month), |
||
71 | 'year' => $year, |
||
72 | ); |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | loadLanguage('Stats'); |
||
77 | loadTemplate('Stats'); |
||
78 | loadJavaScriptFile('stats.js', array('default_theme' => true, 'defer' => false, 'minimize' => true), 'smf_stats'); |
||
79 | |||
80 | // Build the link tree...... |
||
81 | $context['linktree'][] = array( |
||
82 | 'url' => $scripturl . '?action=stats', |
||
83 | 'name' => $txt['stats_center'] |
||
84 | ); |
||
85 | $context['page_title'] = $context['forum_name'] . ' - ' . $txt['stats_center']; |
||
86 | |||
87 | $context['show_member_list'] = allowedTo('view_mlist'); |
||
88 | |||
89 | // Get averages... |
||
90 | $result = $smcFunc['db_query']('', ' |
||
91 | SELECT |
||
92 | SUM(posts) AS posts, SUM(topics) AS topics, SUM(registers) AS registers, |
||
93 | SUM(most_on) AS most_on, MIN(date) AS date, SUM(hits) AS hits |
||
94 | FROM {db_prefix}log_activity', |
||
95 | array( |
||
96 | ) |
||
97 | ); |
||
98 | $row = $smcFunc['db_fetch_assoc']($result); |
||
99 | $smcFunc['db_free_result']($result); |
||
100 | |||
101 | // This would be the amount of time the forum has been up... in days... |
||
102 | $total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24)); |
||
103 | |||
104 | $context['average_posts'] = comma_format(round($row['posts'] / $total_days_up, 2)); |
||
105 | $context['average_topics'] = comma_format(round($row['topics'] / $total_days_up, 2)); |
||
106 | $context['average_members'] = comma_format(round($row['registers'] / $total_days_up, 2)); |
||
107 | $context['average_online'] = comma_format(round($row['most_on'] / $total_days_up, 2)); |
||
108 | $context['average_hits'] = comma_format(round($row['hits'] / $total_days_up, 2)); |
||
109 | |||
110 | $context['num_hits'] = comma_format($row['hits'], 0); |
||
111 | |||
112 | // How many users are online now. |
||
113 | $result = $smcFunc['db_query']('', ' |
||
114 | SELECT COUNT(*) |
||
115 | FROM {db_prefix}log_online', |
||
116 | array( |
||
117 | ) |
||
118 | ); |
||
119 | list ($context['users_online']) = $smcFunc['db_fetch_row']($result); |
||
120 | $smcFunc['db_free_result']($result); |
||
121 | |||
122 | // Statistics such as number of boards, categories, etc. |
||
123 | $result = $smcFunc['db_query']('', ' |
||
124 | SELECT COUNT(*) |
||
125 | FROM {db_prefix}boards AS b |
||
126 | WHERE b.redirect = {string:blank_redirect}', |
||
127 | array( |
||
128 | 'blank_redirect' => '', |
||
129 | ) |
||
130 | ); |
||
131 | list ($context['num_boards']) = $smcFunc['db_fetch_row']($result); |
||
132 | $smcFunc['db_free_result']($result); |
||
133 | |||
134 | $result = $smcFunc['db_query']('', ' |
||
135 | SELECT COUNT(*) |
||
136 | FROM {db_prefix}categories AS c', |
||
137 | array( |
||
138 | ) |
||
139 | ); |
||
140 | list ($context['num_categories']) = $smcFunc['db_fetch_row']($result); |
||
141 | $smcFunc['db_free_result']($result); |
||
142 | |||
143 | // Format the numbers nicely. |
||
144 | $context['users_online'] = comma_format($context['users_online']); |
||
145 | $context['num_boards'] = comma_format($context['num_boards']); |
||
146 | $context['num_categories'] = comma_format($context['num_categories']); |
||
147 | |||
148 | $context['num_members'] = comma_format($modSettings['totalMembers']); |
||
149 | $context['num_posts'] = comma_format($modSettings['totalMessages']); |
||
150 | $context['num_topics'] = comma_format($modSettings['totalTopics']); |
||
151 | $context['most_members_online'] = array( |
||
152 | 'number' => comma_format($modSettings['mostOnline']), |
||
153 | 'date' => timeformat($modSettings['mostDate']) |
||
154 | ); |
||
155 | $context['latest_member'] = &$context['common_stats']['latest_member']; |
||
156 | |||
157 | // Let's calculate gender stats only every four minutes. |
||
158 | $disabled_fields = isset($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array(); |
||
159 | if (!in_array('gender', $disabled_fields)) |
||
160 | { |
||
161 | if (($context['gender'] = cache_get_data('stats_gender', 240)) == null) |
||
162 | { |
||
163 | $result = $smcFunc['db_query']('', ' |
||
164 | SELECT COUNT(id_member) AS total_members, value AS gender |
||
165 | FROM {db_prefix}themes |
||
166 | WHERE variable = {string:gender_var} AND id_theme = {int:default_theme} |
||
167 | GROUP BY value', |
||
168 | array( |
||
169 | 'gender_var' => 'cust_gender', |
||
170 | 'default_theme' => 1, |
||
171 | ) |
||
172 | ); |
||
173 | $context['gender'] = array(); |
||
174 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
175 | { |
||
176 | $context['gender'][$row['gender']] = $row['total_members']; |
||
177 | } |
||
178 | $smcFunc['db_free_result']($result); |
||
179 | |||
180 | cache_put_data('stats_gender', $context['gender'], 240); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | $date = strftime('%Y-%m-%d', forum_time(false)); |
||
185 | |||
186 | // Members online so far today. |
||
187 | $result = $smcFunc['db_query']('', ' |
||
188 | SELECT most_on |
||
189 | FROM {db_prefix}log_activity |
||
190 | WHERE date = {date:today_date} |
||
191 | LIMIT 1', |
||
192 | array( |
||
193 | 'today_date' => $date, |
||
194 | ) |
||
195 | ); |
||
196 | list ($context['online_today']) = $smcFunc['db_fetch_row']($result); |
||
197 | $smcFunc['db_free_result']($result); |
||
198 | |||
199 | $context['online_today'] = comma_format((int) $context['online_today']); |
||
200 | |||
201 | // Poster top 10. |
||
202 | $members_result = $smcFunc['db_query']('', ' |
||
203 | SELECT id_member, real_name, posts |
||
204 | FROM {db_prefix}members |
||
205 | WHERE posts > {int:no_posts} |
||
206 | ORDER BY posts DESC |
||
207 | LIMIT 10', |
||
208 | array( |
||
209 | 'no_posts' => 0, |
||
210 | ) |
||
211 | ); |
||
212 | $context['stats_blocks']['posters'] = array(); |
||
213 | $max_num_posts = 1; |
||
214 | while ($row_members = $smcFunc['db_fetch_assoc']($members_result)) |
||
215 | { |
||
216 | $context['stats_blocks']['posters'][] = array( |
||
217 | 'name' => $row_members['real_name'], |
||
218 | 'id' => $row_members['id_member'], |
||
219 | 'num' => $row_members['posts'], |
||
220 | 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'], |
||
221 | 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>' |
||
222 | ); |
||
223 | |||
224 | if ($max_num_posts < $row_members['posts']) |
||
225 | $max_num_posts = $row_members['posts']; |
||
226 | } |
||
227 | $smcFunc['db_free_result']($members_result); |
||
228 | |||
229 | foreach ($context['stats_blocks']['posters'] as $i => $poster) |
||
230 | { |
||
231 | $context['stats_blocks']['posters'][$i]['percent'] = round(($poster['num'] * 100) / $max_num_posts); |
||
232 | $context['stats_blocks']['posters'][$i]['num'] = comma_format($context['stats_blocks']['posters'][$i]['num']); |
||
233 | } |
||
234 | |||
235 | // Board top 10. |
||
236 | $boards_result = $smcFunc['db_query']('', ' |
||
237 | SELECT id_board, name, num_posts |
||
238 | FROM {db_prefix}boards AS b |
||
239 | WHERE {query_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
240 | AND b.id_board != {int:recycle_board}' : '') . ' |
||
241 | AND b.redirect = {string:blank_redirect} |
||
242 | ORDER BY num_posts DESC |
||
243 | LIMIT 10', |
||
244 | array( |
||
245 | 'recycle_board' => $modSettings['recycle_board'], |
||
246 | 'blank_redirect' => '', |
||
247 | ) |
||
248 | ); |
||
249 | $context['stats_blocks']['boards'] = array(); |
||
250 | $max_num_posts = 1; |
||
251 | while ($row_board = $smcFunc['db_fetch_assoc']($boards_result)) |
||
252 | { |
||
253 | $context['stats_blocks']['boards'][] = array( |
||
254 | 'id' => $row_board['id_board'], |
||
255 | 'name' => $row_board['name'], |
||
256 | 'num' => $row_board['num_posts'], |
||
257 | 'href' => $scripturl . '?board=' . $row_board['id_board'] . '.0', |
||
258 | 'link' => '<a href="' . $scripturl . '?board=' . $row_board['id_board'] . '.0">' . $row_board['name'] . '</a>' |
||
259 | ); |
||
260 | |||
261 | if ($max_num_posts < $row_board['num_posts']) |
||
262 | $max_num_posts = $row_board['num_posts']; |
||
263 | } |
||
264 | $smcFunc['db_free_result']($boards_result); |
||
265 | |||
266 | foreach ($context['stats_blocks']['boards'] as $i => $board) |
||
267 | { |
||
268 | $context['stats_blocks']['boards'][$i]['percent'] = round(($board['num'] * 100) / $max_num_posts); |
||
269 | $context['stats_blocks']['boards'][$i]['num'] = comma_format($context['stats_blocks']['boards'][$i]['num']); |
||
270 | } |
||
271 | |||
272 | // Are you on a larger forum? If so, let's try to limit the number of topics we search through. |
||
273 | if ($modSettings['totalMessages'] > 100000) |
||
274 | { |
||
275 | $request = $smcFunc['db_query']('', ' |
||
276 | SELECT id_topic |
||
277 | FROM {db_prefix}topics |
||
278 | WHERE num_replies != {int:no_replies}' . ($modSettings['postmod_active'] ? ' |
||
279 | AND approved = {int:is_approved}' : '') . ' |
||
280 | ORDER BY num_replies DESC |
||
281 | LIMIT 100', |
||
282 | array( |
||
283 | 'no_replies' => 0, |
||
284 | 'is_approved' => 1, |
||
285 | ) |
||
286 | ); |
||
287 | $topic_ids = array(); |
||
288 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
289 | $topic_ids[] = $row['id_topic']; |
||
290 | $smcFunc['db_free_result']($request); |
||
291 | } |
||
292 | else |
||
293 | $topic_ids = array(); |
||
294 | |||
295 | // Topic replies top 10. |
||
296 | $topic_reply_result = $smcFunc['db_query']('', ' |
||
297 | SELECT m.subject, t.num_replies, t.id_board, t.id_topic, b.name |
||
298 | FROM {db_prefix}topics AS t |
||
299 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
300 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
301 | AND b.id_board != {int:recycle_board}' : '') . ') |
||
302 | WHERE {query_see_board}' . (!empty($topic_ids) ? ' |
||
303 | AND t.id_topic IN ({array_int:topic_list})' : ($modSettings['postmod_active'] ? ' |
||
304 | AND t.approved = {int:is_approved}' : '')) . ' |
||
305 | ORDER BY t.num_replies DESC |
||
306 | LIMIT 10', |
||
307 | array( |
||
308 | 'topic_list' => $topic_ids, |
||
309 | 'recycle_board' => $modSettings['recycle_board'], |
||
310 | 'is_approved' => 1, |
||
311 | ) |
||
312 | ); |
||
313 | $context['stats_blocks']['topics_replies'] = array(); |
||
314 | $max_num_replies = 1; |
||
315 | |||
316 | while ($row_topic_reply = $smcFunc['db_fetch_assoc']($topic_reply_result)) |
||
317 | { |
||
318 | censorText($row_topic_reply['subject']); |
||
319 | |||
320 | $context['stats_blocks']['topics_replies'][] = array( |
||
321 | 'id' => $row_topic_reply['id_topic'], |
||
322 | 'board' => array( |
||
323 | 'id' => $row_topic_reply['id_board'], |
||
324 | 'name' => $row_topic_reply['name'], |
||
325 | 'href' => $scripturl . '?board=' . $row_topic_reply['id_board'] . '.0', |
||
326 | 'link' => '<a href="' . $scripturl . '?board=' . $row_topic_reply['id_board'] . '.0">' . $row_topic_reply['name'] . '</a>' |
||
327 | ), |
||
328 | 'subject' => $row_topic_reply['subject'], |
||
329 | 'num' => $row_topic_reply['num_replies'], |
||
330 | 'href' => $scripturl . '?topic=' . $row_topic_reply['id_topic'] . '.0', |
||
331 | 'link' => '<a href="' . $scripturl . '?topic=' . $row_topic_reply['id_topic'] . '.0">' . $row_topic_reply['subject'] . '</a>' |
||
332 | ); |
||
333 | |||
334 | if ($max_num_replies < $row_topic_reply['num_replies']) |
||
335 | $max_num_replies = $row_topic_reply['num_replies']; |
||
336 | } |
||
337 | $smcFunc['db_free_result']($topic_reply_result); |
||
338 | |||
339 | foreach ($context['stats_blocks']['topics_replies'] as $i => $topic) |
||
340 | { |
||
341 | $context['stats_blocks']['topics_replies'][$i]['percent'] = round(($topic['num'] * 100) / $max_num_replies); |
||
342 | $context['stats_blocks']['topics_replies'][$i]['num'] = comma_format($context['stats_blocks']['topics_replies'][$i]['num']); |
||
343 | } |
||
344 | |||
345 | // Large forums may need a bit more prodding... |
||
346 | if ($modSettings['totalMessages'] > 100000) |
||
347 | { |
||
348 | $request = $smcFunc['db_query']('', ' |
||
349 | SELECT id_topic |
||
350 | FROM {db_prefix}topics |
||
351 | WHERE num_views != {int:no_views} |
||
352 | ORDER BY num_views DESC |
||
353 | LIMIT 100', |
||
354 | array( |
||
355 | 'no_views' => 0, |
||
356 | ) |
||
357 | ); |
||
358 | $topic_ids = array(); |
||
359 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
360 | $topic_ids[] = $row['id_topic']; |
||
361 | $smcFunc['db_free_result']($request); |
||
362 | } |
||
363 | else |
||
364 | $topic_ids = array(); |
||
365 | |||
366 | // Topic views top 10. |
||
367 | $topic_view_result = $smcFunc['db_query']('', ' |
||
368 | SELECT m.subject, t.num_views, t.id_board, t.id_topic, b.name |
||
369 | FROM {db_prefix}topics AS t |
||
370 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
371 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
372 | AND b.id_board != {int:recycle_board}' : '') . ') |
||
373 | WHERE {query_see_board}' . (!empty($topic_ids) ? ' |
||
374 | AND t.id_topic IN ({array_int:topic_list})' : ($modSettings['postmod_active'] ? ' |
||
375 | AND t.approved = {int:is_approved}' : '')) . ' |
||
376 | ORDER BY t.num_views DESC |
||
377 | LIMIT 10', |
||
378 | array( |
||
379 | 'topic_list' => $topic_ids, |
||
380 | 'recycle_board' => $modSettings['recycle_board'], |
||
381 | 'is_approved' => 1, |
||
382 | ) |
||
383 | ); |
||
384 | $context['stats_blocks']['topics_views'] = array(); |
||
385 | $max_num = 1; |
||
386 | while ($row_topic_views = $smcFunc['db_fetch_assoc']($topic_view_result)) |
||
387 | { |
||
388 | censorText($row_topic_views['subject']); |
||
389 | |||
390 | $context['stats_blocks']['topics_views'][] = array( |
||
391 | 'id' => $row_topic_views['id_topic'], |
||
392 | 'board' => array( |
||
393 | 'id' => $row_topic_views['id_board'], |
||
394 | 'name' => $row_topic_views['name'], |
||
395 | 'href' => $scripturl . '?board=' . $row_topic_views['id_board'] . '.0', |
||
396 | 'link' => '<a href="' . $scripturl . '?board=' . $row_topic_views['id_board'] . '.0">' . $row_topic_views['name'] . '</a>' |
||
397 | ), |
||
398 | 'subject' => $row_topic_views['subject'], |
||
399 | 'num' => $row_topic_views['num_views'], |
||
400 | 'href' => $scripturl . '?topic=' . $row_topic_views['id_topic'] . '.0', |
||
401 | 'link' => '<a href="' . $scripturl . '?topic=' . $row_topic_views['id_topic'] . '.0">' . $row_topic_views['subject'] . '</a>' |
||
402 | ); |
||
403 | |||
404 | if ($max_num < $row_topic_views['num_views']) |
||
405 | $max_num = $row_topic_views['num_views']; |
||
406 | } |
||
407 | $smcFunc['db_free_result']($topic_view_result); |
||
408 | |||
409 | foreach ($context['stats_blocks']['topics_views'] as $i => $topic) |
||
410 | { |
||
411 | $context['stats_blocks']['topics_views'][$i]['percent'] = round(($topic['num'] * 100) / $max_num); |
||
412 | $context['stats_blocks']['topics_views'][$i]['num'] = comma_format($context['stats_blocks']['topics_views'][$i]['num']); |
||
413 | } |
||
414 | |||
415 | // Try to cache this when possible, because it's a little unavoidably slow. |
||
416 | if (($members = cache_get_data('stats_top_starters', 360)) == null) |
||
417 | { |
||
418 | $request = $smcFunc['db_query']('', ' |
||
419 | SELECT id_member_started, COUNT(*) AS hits |
||
420 | FROM {db_prefix}topics' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
421 | WHERE id_board != {int:recycle_board}' : '') . ' |
||
422 | GROUP BY id_member_started |
||
423 | ORDER BY hits DESC |
||
424 | LIMIT 20', |
||
425 | array( |
||
426 | 'recycle_board' => $modSettings['recycle_board'], |
||
427 | ) |
||
428 | ); |
||
429 | $members = array(); |
||
430 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
431 | $members[$row['id_member_started']] = $row['hits']; |
||
432 | $smcFunc['db_free_result']($request); |
||
433 | |||
434 | cache_put_data('stats_top_starters', $members, 360); |
||
435 | } |
||
436 | |||
437 | if (empty($members)) |
||
438 | $members = array(0 => 0); |
||
439 | |||
440 | // Topic poster top 10. |
||
441 | $members_result = $smcFunc['db_query']('', ' |
||
442 | SELECT id_member, real_name |
||
443 | FROM {db_prefix}members |
||
444 | WHERE id_member IN ({array_int:member_list})', |
||
445 | array( |
||
446 | 'member_list' => array_keys($members), |
||
447 | ) |
||
448 | ); |
||
449 | $context['stats_blocks']['starters'] = array(); |
||
450 | $max_num = 1; |
||
451 | while ($row_members = $smcFunc['db_fetch_assoc']($members_result)) |
||
452 | { |
||
453 | $i = array_search($row_members['id_member'], array_keys($members)); |
||
454 | // skip all not top 10 |
||
455 | if ($i >= 10) |
||
456 | continue; |
||
457 | |||
458 | $context['stats_blocks']['starters'][$i] = array( |
||
459 | 'name' => $row_members['real_name'], |
||
460 | 'id' => $row_members['id_member'], |
||
461 | 'num' => $members[$row_members['id_member']], |
||
462 | 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'], |
||
463 | 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>' |
||
464 | ); |
||
465 | |||
466 | if ($max_num < $members[$row_members['id_member']]) |
||
467 | $max_num = $members[$row_members['id_member']]; |
||
468 | } |
||
469 | ksort($context['stats_blocks']['starters']); |
||
470 | $smcFunc['db_free_result']($members_result); |
||
471 | |||
472 | foreach ($context['stats_blocks']['starters'] as $i => $topic) |
||
473 | { |
||
474 | $context['stats_blocks']['starters'][$i]['percent'] = round(($topic['num'] * 100) / $max_num); |
||
475 | $context['stats_blocks']['starters'][$i]['num'] = comma_format($context['stats_blocks']['starters'][$i]['num']); |
||
476 | } |
||
477 | |||
478 | // Time online top 10. |
||
479 | $temp = cache_get_data('stats_total_time_members', 600); |
||
480 | $members_result = $smcFunc['db_query']('', ' |
||
481 | SELECT id_member, real_name, total_time_logged_in |
||
482 | FROM {db_prefix}members' . (!empty($temp) ? ' |
||
483 | WHERE id_member IN ({array_int:member_list_cached})' : '') . ' |
||
484 | ORDER BY total_time_logged_in DESC |
||
485 | LIMIT 20', |
||
486 | array( |
||
487 | 'member_list_cached' => $temp, |
||
488 | ) |
||
489 | ); |
||
490 | $context['stats_blocks']['time_online'] = array(); |
||
491 | $temp2 = array(); |
||
492 | $max_time_online = 1; |
||
493 | while ($row_members = $smcFunc['db_fetch_assoc']($members_result)) |
||
494 | { |
||
495 | $temp2[] = (int) $row_members['id_member']; |
||
496 | if (count($context['stats_blocks']['time_online']) >= 10) |
||
497 | continue; |
||
498 | |||
499 | // Figure out the days, hours and minutes. |
||
500 | $timeDays = floor($row_members['total_time_logged_in'] / 86400); |
||
501 | $timeHours = floor(($row_members['total_time_logged_in'] % 86400) / 3600); |
||
502 | |||
503 | // Figure out which things to show... (days, hours, minutes, etc.) |
||
504 | $timelogged = ''; |
||
505 | if ($timeDays > 0) |
||
506 | $timelogged .= $timeDays . $txt['total_time_logged_d']; |
||
507 | if ($timeHours > 0) |
||
508 | $timelogged .= $timeHours . $txt['total_time_logged_h']; |
||
509 | $timelogged .= floor(($row_members['total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_m']; |
||
510 | |||
511 | $context['stats_blocks']['time_online'][] = array( |
||
512 | 'id' => $row_members['id_member'], |
||
513 | 'name' => $row_members['real_name'], |
||
514 | 'num' => $timelogged, |
||
515 | 'seconds_online' => $row_members['total_time_logged_in'], |
||
516 | 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'], |
||
517 | 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>' |
||
518 | ); |
||
519 | |||
520 | if ($max_time_online < $row_members['total_time_logged_in']) |
||
521 | $max_time_online = $row_members['total_time_logged_in']; |
||
522 | } |
||
523 | $smcFunc['db_free_result']($members_result); |
||
524 | |||
525 | foreach ($context['stats_blocks']['time_online'] as $i => $member) |
||
526 | $context['stats_blocks']['time_online'][$i]['percent'] = round(($member['seconds_online'] * 100) / $max_time_online); |
||
527 | |||
528 | // Cache the ones we found for a bit, just so we don't have to look again. |
||
529 | if ($temp !== $temp2) |
||
530 | cache_put_data('stats_total_time_members', $temp2, 480); |
||
531 | |||
532 | // Likes. |
||
533 | if (!empty($modSettings['enable_likes'])) |
||
534 | { |
||
535 | // Liked messages top 10. |
||
536 | $context['stats_blocks']['liked_messages'] = array(); |
||
537 | $max_liked_message = 1; |
||
538 | $liked_messages = $smcFunc['db_query']('', ' |
||
539 | SELECT m.id_msg, m.subject, m.likes, m.id_board, m.id_topic, t.approved |
||
540 | FROM ( |
||
541 | SELECT n.id_msg, n.subject, n.likes, n.id_board, n.id_topic |
||
542 | FROM {db_prefix}messages as n |
||
543 | ORDER BY n.likes DESC |
||
544 | LIMIT 1000 |
||
545 | ) AS m |
||
546 | INNER JOIN {db_prefix}topics AS t ON (m.id_topic = t.id_topic) |
||
547 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
548 | AND b.id_board != {int:recycle_board}' : '') . ') |
||
549 | WHERE {query_see_board}' . ($modSettings['postmod_active'] ? ' |
||
550 | AND t.approved = {int:is_approved}' : '') . ' |
||
551 | ORDER BY m.likes DESC |
||
552 | LIMIT 10', |
||
553 | array( |
||
554 | 'recycle_board' => $modSettings['recycle_board'], |
||
555 | 'is_approved' => 1, |
||
556 | ) |
||
557 | ); |
||
558 | |||
559 | while ($row_liked_message = $smcFunc['db_fetch_assoc']($liked_messages)) |
||
560 | { |
||
561 | censorText($row_liked_message['subject']); |
||
562 | |||
563 | $context['stats_blocks']['liked_messages'][] = array( |
||
564 | 'id' => $row_liked_message['id_topic'], |
||
565 | 'subject' => $row_liked_message['subject'], |
||
566 | 'num' => $row_liked_message['likes'], |
||
567 | 'href' => $scripturl . '?msg=' . $row_liked_message['id_msg'], |
||
568 | 'link' => '<a href="' . $scripturl . '?msg=' . $row_liked_message['id_msg'] . '">' . $row_liked_message['subject'] . '</a>' |
||
569 | ); |
||
570 | |||
571 | if ($max_liked_message < $row_liked_message['likes']) |
||
572 | $max_liked_message = $row_liked_message['likes']; |
||
573 | } |
||
574 | $smcFunc['db_free_result']($liked_messages); |
||
575 | |||
576 | foreach ($context['stats_blocks']['liked_messages'] as $i => $liked_messages) |
||
577 | $context['stats_blocks']['liked_messages'][$i]['percent'] = round(($liked_messages['num'] * 100) / $max_liked_message); |
||
578 | |||
579 | // Liked users top 10. |
||
580 | $context['stats_blocks']['liked_users'] = array(); |
||
581 | $max_liked_users = 1; |
||
582 | $liked_users = $smcFunc['db_query']('', ' |
||
583 | SELECT m.id_member AS liked_user, COUNT(l.content_id) AS count, mem.real_name |
||
584 | FROM {db_prefix}user_likes AS l |
||
585 | INNER JOIN {db_prefix}messages AS m ON (l.content_id = m.id_msg) |
||
586 | INNER JOIN {db_prefix}members AS mem ON (m.id_member = mem.id_member) |
||
587 | WHERE content_type = {literal:msg} |
||
588 | AND m.id_member > {int:zero} |
||
589 | GROUP BY m.id_member, mem.real_name |
||
590 | ORDER BY count DESC |
||
591 | LIMIT 10', |
||
592 | array( |
||
593 | 'no_posts' => 0, |
||
594 | 'zero' => 0, |
||
595 | ) |
||
596 | ); |
||
597 | |||
598 | while ($row_liked_users = $smcFunc['db_fetch_assoc']($liked_users)) |
||
599 | { |
||
600 | $context['stats_blocks']['liked_users'][] = array( |
||
601 | 'id' => $row_liked_users['liked_user'], |
||
602 | 'num' => $row_liked_users['count'], |
||
603 | 'href' => $scripturl . '?action=profile;u=' . $row_liked_users['liked_user'], |
||
604 | 'name' => $row_liked_users['real_name'], |
||
605 | 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_liked_users['liked_user'] . '">' . $row_liked_users['real_name'] . '</a>', |
||
606 | ); |
||
607 | |||
608 | if ($max_liked_users < $row_liked_users['count']) |
||
609 | $max_liked_users = $row_liked_users['count']; |
||
610 | } |
||
611 | |||
612 | $smcFunc['db_free_result']($liked_users); |
||
613 | |||
614 | foreach ($context['stats_blocks']['liked_users'] as $i => $liked_users) |
||
615 | $context['stats_blocks']['liked_users'][$i]['percent'] = round(($liked_users['num'] * 100) / $max_liked_users); |
||
616 | } |
||
617 | |||
618 | // Activity by month. |
||
619 | $months_result = $smcFunc['db_query']('', ' |
||
620 | SELECT |
||
621 | YEAR(date) AS stats_year, MONTH(date) AS stats_month, SUM(hits) AS hits, SUM(registers) AS registers, SUM(topics) AS topics, SUM(posts) AS posts, MAX(most_on) AS most_on, COUNT(*) AS num_days |
||
622 | FROM {db_prefix}log_activity |
||
623 | GROUP BY stats_year, stats_month', |
||
624 | array() |
||
625 | ); |
||
626 | |||
627 | $context['yearly'] = array(); |
||
628 | while ($row_months = $smcFunc['db_fetch_assoc']($months_result)) |
||
629 | { |
||
630 | $ID_MONTH = $row_months['stats_year'] . sprintf('%02d', $row_months['stats_month']); |
||
631 | $expanded = !empty($_SESSION['expanded_stats'][$row_months['stats_year']]) && in_array($row_months['stats_month'], $_SESSION['expanded_stats'][$row_months['stats_year']]); |
||
632 | |||
633 | if (!isset($context['yearly'][$row_months['stats_year']])) |
||
634 | $context['yearly'][$row_months['stats_year']] = array( |
||
635 | 'year' => $row_months['stats_year'], |
||
636 | 'new_topics' => 0, |
||
637 | 'new_posts' => 0, |
||
638 | 'new_members' => 0, |
||
639 | 'most_members_online' => 0, |
||
640 | 'hits' => 0, |
||
641 | 'num_months' => 0, |
||
642 | 'months' => array(), |
||
643 | 'expanded' => false, |
||
644 | 'current_year' => $row_months['stats_year'] == date('Y'), |
||
645 | ); |
||
646 | |||
647 | $context['yearly'][$row_months['stats_year']]['months'][(int) $row_months['stats_month']] = array( |
||
648 | 'id' => $ID_MONTH, |
||
649 | 'date' => array( |
||
650 | 'month' => sprintf('%02d', $row_months['stats_month']), |
||
651 | 'year' => $row_months['stats_year'] |
||
652 | ), |
||
653 | 'href' => $scripturl . '?action=stats;' . ($expanded ? 'collapse' : 'expand') . '=' . $ID_MONTH . '#m' . $ID_MONTH, |
||
654 | 'link' => '<a href="' . $scripturl . '?action=stats;' . ($expanded ? 'collapse' : 'expand') . '=' . $ID_MONTH . '#m' . $ID_MONTH . '">' . $txt['months_titles'][(int) $row_months['stats_month']] . ' ' . $row_months['stats_year'] . '</a>', |
||
655 | 'month' => $txt['months_titles'][(int) $row_months['stats_month']], |
||
656 | 'year' => $row_months['stats_year'], |
||
657 | 'new_topics' => comma_format($row_months['topics']), |
||
658 | 'new_posts' => comma_format($row_months['posts']), |
||
659 | 'new_members' => comma_format($row_months['registers']), |
||
660 | 'most_members_online' => comma_format($row_months['most_on']), |
||
661 | 'hits' => comma_format($row_months['hits']), |
||
662 | 'num_days' => $row_months['num_days'], |
||
663 | 'days' => array(), |
||
664 | 'expanded' => $expanded |
||
665 | ); |
||
666 | |||
667 | $context['yearly'][$row_months['stats_year']]['new_topics'] += $row_months['topics']; |
||
668 | $context['yearly'][$row_months['stats_year']]['new_posts'] += $row_months['posts']; |
||
669 | $context['yearly'][$row_months['stats_year']]['new_members'] += $row_months['registers']; |
||
670 | $context['yearly'][$row_months['stats_year']]['hits'] += $row_months['hits']; |
||
671 | $context['yearly'][$row_months['stats_year']]['num_months']++; |
||
672 | $context['yearly'][$row_months['stats_year']]['expanded'] |= $expanded; |
||
673 | $context['yearly'][$row_months['stats_year']]['most_members_online'] = max($context['yearly'][$row_months['stats_year']]['most_members_online'], $row_months['most_on']); |
||
674 | } |
||
675 | |||
676 | krsort($context['yearly']); |
||
677 | |||
678 | $context['collapsed_years'] = array(); |
||
679 | foreach ($context['yearly'] as $year => $data) |
||
680 | { |
||
681 | // This gets rid of the filesort on the query ;). |
||
682 | krsort($context['yearly'][$year]['months']); |
||
683 | |||
684 | $context['yearly'][$year]['new_topics'] = comma_format($data['new_topics']); |
||
685 | $context['yearly'][$year]['new_posts'] = comma_format($data['new_posts']); |
||
686 | $context['yearly'][$year]['new_members'] = comma_format($data['new_members']); |
||
687 | $context['yearly'][$year]['most_members_online'] = comma_format($data['most_members_online']); |
||
688 | $context['yearly'][$year]['hits'] = comma_format($data['hits']); |
||
689 | |||
690 | // Keep a list of collapsed years. |
||
691 | if (!$data['expanded'] && !$data['current_year']) |
||
692 | $context['collapsed_years'][] = $year; |
||
693 | } |
||
694 | |||
695 | // Custom stats (just add a template_layer to add it to the template!) |
||
696 | call_integration_hook('integrate_forum_stats'); |
||
697 | |||
698 | if (empty($_SESSION['expanded_stats'])) |
||
699 | return; |
||
700 | |||
701 | $condition_text = array(); |
||
702 | $condition_params = array(); |
||
703 | foreach ($_SESSION['expanded_stats'] as $year => $months) |
||
704 | if (!empty($months)) |
||
705 | { |
||
706 | $condition_text[] = 'YEAR(date) = {int:year_' . $year . '} AND MONTH(date) IN ({array_int:months_' . $year . '})'; |
||
707 | $condition_params['year_' . $year] = $year; |
||
708 | $condition_params['months_' . $year] = $months; |
||
709 | } |
||
710 | |||
711 | // No daily stats to even look at? |
||
712 | if (empty($condition_text)) |
||
713 | return; |
||
714 | |||
715 | getDailyStats(implode(' OR ', $condition_text), $condition_params); |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * Loads the statistics on a daily basis in $context. |
||
720 | * called by DisplayStats(). |
||
721 | * |
||
722 | * @param string $condition_string An SQL condition string |
||
723 | * @param array $condition_parameters Parameters for $condition_string |
||
724 | */ |
||
725 | function getDailyStats($condition_string, $condition_parameters = array()) |
||
726 | { |
||
727 | global $context, $smcFunc; |
||
728 | |||
729 | // Activity by day. |
||
730 | $days_result = $smcFunc['db_query']('', ' |
||
731 | SELECT YEAR(date) AS stats_year, MONTH(date) AS stats_month, DAYOFMONTH(date) AS stats_day, topics, posts, registers, most_on, hits |
||
732 | FROM {db_prefix}log_activity |
||
733 | WHERE ' . $condition_string . ' |
||
734 | ORDER BY stats_day ASC', |
||
735 | $condition_parameters |
||
736 | ); |
||
737 | while ($row_days = $smcFunc['db_fetch_assoc']($days_result)) |
||
738 | $context['yearly'][$row_days['stats_year']]['months'][(int) $row_days['stats_month']]['days'][] = array( |
||
739 | 'day' => sprintf('%02d', $row_days['stats_day']), |
||
740 | 'month' => sprintf('%02d', $row_days['stats_month']), |
||
741 | 'year' => $row_days['stats_year'], |
||
742 | 'new_topics' => comma_format($row_days['topics']), |
||
743 | 'new_posts' => comma_format($row_days['posts']), |
||
744 | 'new_members' => comma_format($row_days['registers']), |
||
745 | 'most_members_online' => comma_format($row_days['most_on']), |
||
746 | 'hits' => comma_format($row_days['hits']) |
||
747 | ); |
||
748 | $smcFunc['db_free_result']($days_result); |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * This is the function which returns stats to simplemachines.org IF enabled! |
||
753 | * called by simplemachines.org. |
||
754 | * only returns anything if stats was enabled during installation. |
||
755 | * can also be accessed by the admin, to show what stats sm.org collects. |
||
756 | * does not return any data directly to sm.org, instead starts a new request for security. |
||
757 | * |
||
758 | * @link https://www.simplemachines.org/about/stats.php for more info. |
||
759 | */ |
||
760 | function SMStats() |
||
761 | { |
||
762 | global $modSettings, $user_info, $sourcedir; |
||
763 | |||
764 | // First, is it disabled? |
||
765 | if (empty($modSettings['enable_sm_stats']) || empty($modSettings['sm_stats_key'])) |
||
766 | die(); |
||
0 ignored issues
–
show
|
|||
767 | |||
768 | // Are we saying who we are, and are we right? (OR an admin) |
||
769 | if (!$user_info['is_admin'] && (!isset($_GET['sid']) || $_GET['sid'] != $modSettings['sm_stats_key'])) |
||
770 | die(); |
||
0 ignored issues
–
show
|
|||
771 | |||
772 | // Verify the referer... |
||
773 | if (!$user_info['is_admin'] && (!isset($_SERVER['HTTP_REFERER']) || md5($_SERVER['HTTP_REFERER']) != '746cb59a1a0d5cf4bd240e5a67c73085')) |
||
774 | die(); |
||
0 ignored issues
–
show
|
|||
775 | |||
776 | // Get some server versions. |
||
777 | require_once($sourcedir . '/Subs-Admin.php'); |
||
778 | $checkFor = array( |
||
779 | 'php', |
||
780 | 'db_server', |
||
781 | ); |
||
782 | $serverVersions = getServerVersions($checkFor); |
||
783 | |||
784 | // Get the actual stats. |
||
785 | $stats_to_send = array( |
||
786 | 'UID' => $modSettings['sm_stats_key'], |
||
787 | 'time_added' => time(), |
||
788 | 'members' => $modSettings['totalMembers'], |
||
789 | 'messages' => $modSettings['totalMessages'], |
||
790 | 'topics' => $modSettings['totalTopics'], |
||
791 | 'boards' => 0, |
||
792 | 'php_version' => $serverVersions['php']['version'], |
||
793 | 'database_type' => strtolower($serverVersions['db_engine']['version']), |
||
794 | 'database_version' => $serverVersions['db_server']['version'], |
||
795 | 'smf_version' => SMF_FULL_VERSION, |
||
796 | 'smfd_version' => $modSettings['smfVersion'], |
||
797 | ); |
||
798 | |||
799 | // Encode all the data, for security. |
||
800 | foreach ($stats_to_send as $k => $v) |
||
801 | $stats_to_send[$k] = urlencode($k) . '=' . urlencode($v); |
||
802 | |||
803 | // Turn this into the query string! |
||
804 | $stats_to_send = implode('&', $stats_to_send); |
||
805 | |||
806 | // If we're an admin, just plonk them out. |
||
807 | if ($user_info['is_admin']) |
||
808 | echo $stats_to_send; |
||
809 | else |
||
810 | { |
||
811 | // Connect to the collection script. |
||
812 | $fp = @fsockopen('www.simplemachines.org', 80, $errno, $errstr); |
||
813 | if ($fp) |
||
0 ignored issues
–
show
|
|||
814 | { |
||
815 | $length = strlen($stats_to_send); |
||
816 | |||
817 | $out = 'POST /smf/stats/collect_stats.php HTTP/1.1' . "\r\n"; |
||
818 | $out .= 'Host: www.simplemachines.org' . "\r\n"; |
||
819 | $out .= 'user-agent: '. SMF_USER_AGENT . "\r\n"; |
||
820 | $out .= 'content-type: application/x-www-form-urlencoded' . "\r\n"; |
||
821 | $out .= 'connection: Close' . "\r\n"; |
||
822 | $out .= 'content-length: ' . $length . "\r\n\r\n"; |
||
823 | $out .= $stats_to_send . "\r\n"; |
||
824 | fwrite($fp, $out); |
||
825 | fclose($fp); |
||
826 | } |
||
827 | } |
||
828 | |||
829 | // Die. |
||
830 | die('OK'); |
||
0 ignored issues
–
show
|
|||
831 | } |
||
832 | |||
833 | ?> |