Stats.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <?php
  2. /**
  3. * Provide a display for forum statistics
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2013 Simple Machines and individual 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('No direct access...');
  16. /**
  17. * Display some useful/interesting board statistics.
  18. *
  19. * gets all the statistics in order and puts them in.
  20. * uses the Stats template and language file. (and main sub template.)
  21. * requires the view_stats permission.
  22. * accessed from ?action=stats.
  23. */
  24. function DisplayStats()
  25. {
  26. global $txt, $scripturl, $modSettings, $user_info, $context, $smcFunc;
  27. isAllowedTo('view_stats');
  28. // Page disabled - redirect them out
  29. if (empty($modSettings['trackStats']))
  30. fatal_lang_error('feature_disabled', true);
  31. if (!empty($_REQUEST['expand']))
  32. {
  33. $context['robot_no_index'] = true;
  34. $month = (int) substr($_REQUEST['expand'], 4);
  35. $year = (int) substr($_REQUEST['expand'], 0, 4);
  36. if ($year > 1900 && $year < 2200 && $month >= 1 && $month <= 12)
  37. $_SESSION['expanded_stats'][$year][] = $month;
  38. }
  39. elseif (!empty($_REQUEST['collapse']))
  40. {
  41. $context['robot_no_index'] = true;
  42. $month = (int) substr($_REQUEST['collapse'], 4);
  43. $year = (int) substr($_REQUEST['collapse'], 0, 4);
  44. if (!empty($_SESSION['expanded_stats'][$year]))
  45. $_SESSION['expanded_stats'][$year] = array_diff($_SESSION['expanded_stats'][$year], array($month));
  46. }
  47. // Handle the XMLHttpRequest.
  48. if (isset($_REQUEST['xml']))
  49. {
  50. // Collapsing stats only needs adjustments of the session variables.
  51. if (!empty($_REQUEST['collapse']))
  52. obExit(false);
  53. $context['sub_template'] = 'stats';
  54. getDailyStats('YEAR(date) = {int:year} AND MONTH(date) = {int:month}', array('year' => $year, 'month' => $month));
  55. $context['yearly'][$year]['months'][$month]['date'] = array(
  56. 'month' => sprintf('%02d', $month),
  57. 'year' => $year,
  58. );
  59. return;
  60. }
  61. loadLanguage('Stats');
  62. loadTemplate('Stats');
  63. // Build the link tree......
  64. $context['linktree'][] = array(
  65. 'url' => $scripturl . '?action=stats',
  66. 'name' => $txt['stats_center']
  67. );
  68. $context['page_title'] = $context['forum_name'] . ' - ' . $txt['stats_center'];
  69. $context['show_member_list'] = allowedTo('view_mlist');
  70. // Get averages...
  71. $result = $smcFunc['db_query']('', '
  72. SELECT
  73. SUM(posts) AS posts, SUM(topics) AS topics, SUM(registers) AS registers,
  74. SUM(most_on) AS most_on, MIN(date) AS date, SUM(hits) AS hits
  75. FROM {db_prefix}log_activity',
  76. array(
  77. )
  78. );
  79. $row = $smcFunc['db_fetch_assoc']($result);
  80. $smcFunc['db_free_result']($result);
  81. // This would be the amount of time the forum has been up... in days...
  82. $total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24));
  83. $context['average_posts'] = comma_format(round($row['posts'] / $total_days_up, 2));
  84. $context['average_topics'] = comma_format(round($row['topics'] / $total_days_up, 2));
  85. $context['average_members'] = comma_format(round($row['registers'] / $total_days_up, 2));
  86. $context['average_online'] = comma_format(round($row['most_on'] / $total_days_up, 2));
  87. $context['average_hits'] = comma_format(round($row['hits'] / $total_days_up, 2));
  88. $context['num_hits'] = comma_format($row['hits'], 0);
  89. // How many users are online now.
  90. $result = $smcFunc['db_query']('', '
  91. SELECT COUNT(*)
  92. FROM {db_prefix}log_online',
  93. array(
  94. )
  95. );
  96. list ($context['users_online']) = $smcFunc['db_fetch_row']($result);
  97. $smcFunc['db_free_result']($result);
  98. // Statistics such as number of boards, categories, etc.
  99. $result = $smcFunc['db_query']('', '
  100. SELECT COUNT(*)
  101. FROM {db_prefix}boards AS b
  102. WHERE b.redirect = {string:blank_redirect}',
  103. array(
  104. 'blank_redirect' => '',
  105. )
  106. );
  107. list ($context['num_boards']) = $smcFunc['db_fetch_row']($result);
  108. $smcFunc['db_free_result']($result);
  109. $result = $smcFunc['db_query']('', '
  110. SELECT COUNT(*)
  111. FROM {db_prefix}categories AS c',
  112. array(
  113. )
  114. );
  115. list ($context['num_categories']) = $smcFunc['db_fetch_row']($result);
  116. $smcFunc['db_free_result']($result);
  117. // Format the numbers nicely.
  118. $context['users_online'] = comma_format($context['users_online']);
  119. $context['num_boards'] = comma_format($context['num_boards']);
  120. $context['num_categories'] = comma_format($context['num_categories']);
  121. $context['num_members'] = comma_format($modSettings['totalMembers']);
  122. $context['num_posts'] = comma_format($modSettings['totalMessages']);
  123. $context['num_topics'] = comma_format($modSettings['totalTopics']);
  124. $context['most_members_online'] = array(
  125. 'number' => comma_format($modSettings['mostOnline']),
  126. 'date' => timeformat($modSettings['mostDate'])
  127. );
  128. $context['latest_member'] = &$context['common_stats']['latest_member'];
  129. // Male vs. female ratio - let's calculate this only every four minutes.
  130. if (($context['gender'] = cache_get_data('stats_gender', 240)) == null)
  131. {
  132. $result = $smcFunc['db_query']('', '
  133. SELECT COUNT(*) AS total_members, gender
  134. FROM {db_prefix}members
  135. GROUP BY gender',
  136. array(
  137. )
  138. );
  139. $context['gender'] = array();
  140. while ($row = $smcFunc['db_fetch_assoc']($result))
  141. {
  142. // Assuming we're telling... male or female?
  143. if (!empty($row['gender']))
  144. $context['gender'][$row['gender'] == 2 ? 'females' : 'males'] = $row['total_members'];
  145. }
  146. $smcFunc['db_free_result']($result);
  147. // Set these two zero if the didn't get set at all.
  148. if (empty($context['gender']['males']))
  149. $context['gender']['males'] = 0;
  150. if (empty($context['gender']['females']))
  151. $context['gender']['females'] = 0;
  152. // Try and come up with some "sensible" default states in case of a non-mixed board.
  153. if ($context['gender']['males'] == $context['gender']['females'])
  154. $context['gender']['ratio'] = '1:1';
  155. elseif ($context['gender']['males'] == 0)
  156. $context['gender']['ratio'] = '0:1';
  157. elseif ($context['gender']['females'] == 0)
  158. $context['gender']['ratio'] = '1:0';
  159. elseif ($context['gender']['males'] > $context['gender']['females'])
  160. $context['gender']['ratio'] = round($context['gender']['males'] / $context['gender']['females'], 1) . ':1';
  161. elseif ($context['gender']['females'] > $context['gender']['males'])
  162. $context['gender']['ratio'] = '1:' . round($context['gender']['females'] / $context['gender']['males'], 1);
  163. cache_put_data('stats_gender', $context['gender'], 240);
  164. }
  165. $date = strftime('%Y-%m-%d', forum_time(false));
  166. // Members online so far today.
  167. $result = $smcFunc['db_query']('', '
  168. SELECT most_on
  169. FROM {db_prefix}log_activity
  170. WHERE date = {date:today_date}
  171. LIMIT 1',
  172. array(
  173. 'today_date' => $date,
  174. )
  175. );
  176. list ($context['online_today']) = $smcFunc['db_fetch_row']($result);
  177. $smcFunc['db_free_result']($result);
  178. $context['online_today'] = comma_format((int) $context['online_today']);
  179. // Poster top 10.
  180. $members_result = $smcFunc['db_query']('', '
  181. SELECT id_member, real_name, posts
  182. FROM {db_prefix}members
  183. WHERE posts > {int:no_posts}
  184. ORDER BY posts DESC
  185. LIMIT 10',
  186. array(
  187. 'no_posts' => 0,
  188. )
  189. );
  190. $context['top_posters'] = array();
  191. $max_num_posts = 1;
  192. while ($row_members = $smcFunc['db_fetch_assoc']($members_result))
  193. {
  194. $context['top_posters'][] = array(
  195. 'name' => $row_members['real_name'],
  196. 'id' => $row_members['id_member'],
  197. 'num_posts' => $row_members['posts'],
  198. 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'],
  199. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>'
  200. );
  201. if ($max_num_posts < $row_members['posts'])
  202. $max_num_posts = $row_members['posts'];
  203. }
  204. $smcFunc['db_free_result']($members_result);
  205. foreach ($context['top_posters'] as $i => $poster)
  206. {
  207. $context['top_posters'][$i]['post_percent'] = round(($poster['num_posts'] * 100) / $max_num_posts);
  208. $context['top_posters'][$i]['num_posts'] = comma_format($context['top_posters'][$i]['num_posts']);
  209. }
  210. // Board top 10.
  211. $boards_result = $smcFunc['db_query']('', '
  212. SELECT id_board, name, num_posts
  213. FROM {db_prefix}boards AS b
  214. WHERE {query_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  215. AND b.id_board != {int:recycle_board}' : '') . '
  216. AND b.redirect = {string:blank_redirect}
  217. ORDER BY num_posts DESC
  218. LIMIT 10',
  219. array(
  220. 'recycle_board' => $modSettings['recycle_board'],
  221. 'blank_redirect' => '',
  222. )
  223. );
  224. $context['top_boards'] = array();
  225. $max_num_posts = 1;
  226. while ($row_board = $smcFunc['db_fetch_assoc']($boards_result))
  227. {
  228. $context['top_boards'][] = array(
  229. 'id' => $row_board['id_board'],
  230. 'name' => $row_board['name'],
  231. 'num_posts' => $row_board['num_posts'],
  232. 'href' => $scripturl . '?board=' . $row_board['id_board'] . '.0',
  233. 'link' => '<a href="' . $scripturl . '?board=' . $row_board['id_board'] . '.0">' . $row_board['name'] . '</a>'
  234. );
  235. if ($max_num_posts < $row_board['num_posts'])
  236. $max_num_posts = $row_board['num_posts'];
  237. }
  238. $smcFunc['db_free_result']($boards_result);
  239. foreach ($context['top_boards'] as $i => $board)
  240. {
  241. $context['top_boards'][$i]['post_percent'] = round(($board['num_posts'] * 100) / $max_num_posts);
  242. $context['top_boards'][$i]['num_posts'] = comma_format($context['top_boards'][$i]['num_posts']);
  243. }
  244. // Are you on a larger forum? If so, let's try to limit the number of topics we search through.
  245. if ($modSettings['totalMessages'] > 100000)
  246. {
  247. $request = $smcFunc['db_query']('', '
  248. SELECT id_topic
  249. FROM {db_prefix}topics
  250. WHERE num_replies != {int:no_replies}' . ($modSettings['postmod_active'] ? '
  251. AND approved = {int:is_approved}' : '') . '
  252. ORDER BY num_replies DESC
  253. LIMIT 100',
  254. array(
  255. 'no_replies' => 0,
  256. 'is_approved' => 1,
  257. )
  258. );
  259. $topic_ids = array();
  260. while ($row = $smcFunc['db_fetch_assoc']($request))
  261. $topic_ids[] = $row['id_topic'];
  262. $smcFunc['db_free_result']($request);
  263. }
  264. else
  265. $topic_ids = array();
  266. // Topic replies top 10.
  267. $topic_reply_result = $smcFunc['db_query']('', '
  268. SELECT m.subject, t.num_replies, t.id_board, t.id_topic, b.name
  269. FROM {db_prefix}topics AS t
  270. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  271. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  272. AND b.id_board != {int:recycle_board}' : '') . ')
  273. WHERE {query_see_board}' . (!empty($topic_ids) ? '
  274. AND t.id_topic IN ({array_int:topic_list})' : ($modSettings['postmod_active'] ? '
  275. AND t.approved = {int:is_approved}' : '')) . '
  276. ORDER BY t.num_replies DESC
  277. LIMIT 10',
  278. array(
  279. 'topic_list' => $topic_ids,
  280. 'recycle_board' => $modSettings['recycle_board'],
  281. 'is_approved' => 1,
  282. )
  283. );
  284. $context['top_topics_replies'] = array();
  285. $max_num_replies = 1;
  286. while ($row_topic_reply = $smcFunc['db_fetch_assoc']($topic_reply_result))
  287. {
  288. censorText($row_topic_reply['subject']);
  289. $context['top_topics_replies'][] = array(
  290. 'id' => $row_topic_reply['id_topic'],
  291. 'board' => array(
  292. 'id' => $row_topic_reply['id_board'],
  293. 'name' => $row_topic_reply['name'],
  294. 'href' => $scripturl . '?board=' . $row_topic_reply['id_board'] . '.0',
  295. 'link' => '<a href="' . $scripturl . '?board=' . $row_topic_reply['id_board'] . '.0">' . $row_topic_reply['name'] . '</a>'
  296. ),
  297. 'subject' => $row_topic_reply['subject'],
  298. 'num_replies' => $row_topic_reply['num_replies'],
  299. 'href' => $scripturl . '?topic=' . $row_topic_reply['id_topic'] . '.0',
  300. 'link' => '<a href="' . $scripturl . '?topic=' . $row_topic_reply['id_topic'] . '.0">' . $row_topic_reply['subject'] . '</a>'
  301. );
  302. if ($max_num_replies < $row_topic_reply['num_replies'])
  303. $max_num_replies = $row_topic_reply['num_replies'];
  304. }
  305. $smcFunc['db_free_result']($topic_reply_result);
  306. foreach ($context['top_topics_replies'] as $i => $topic)
  307. {
  308. $context['top_topics_replies'][$i]['post_percent'] = round(($topic['num_replies'] * 100) / $max_num_replies);
  309. $context['top_topics_replies'][$i]['num_replies'] = comma_format($context['top_topics_replies'][$i]['num_replies']);
  310. }
  311. // Large forums may need a bit more prodding...
  312. if ($modSettings['totalMessages'] > 100000)
  313. {
  314. $request = $smcFunc['db_query']('', '
  315. SELECT id_topic
  316. FROM {db_prefix}topics
  317. WHERE num_views != {int:no_views}
  318. ORDER BY num_views DESC
  319. LIMIT 100',
  320. array(
  321. 'no_views' => 0,
  322. )
  323. );
  324. $topic_ids = array();
  325. while ($row = $smcFunc['db_fetch_assoc']($request))
  326. $topic_ids[] = $row['id_topic'];
  327. $smcFunc['db_free_result']($request);
  328. }
  329. else
  330. $topic_ids = array();
  331. // Topic views top 10.
  332. $topic_view_result = $smcFunc['db_query']('', '
  333. SELECT m.subject, t.num_views, t.id_board, t.id_topic, b.name
  334. FROM {db_prefix}topics AS t
  335. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  336. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  337. AND b.id_board != {int:recycle_board}' : '') . ')
  338. WHERE {query_see_board}' . (!empty($topic_ids) ? '
  339. AND t.id_topic IN ({array_int:topic_list})' : ($modSettings['postmod_active'] ? '
  340. AND t.approved = {int:is_approved}' : '')) . '
  341. ORDER BY t.num_views DESC
  342. LIMIT 10',
  343. array(
  344. 'topic_list' => $topic_ids,
  345. 'recycle_board' => $modSettings['recycle_board'],
  346. 'is_approved' => 1,
  347. )
  348. );
  349. $context['top_topics_views'] = array();
  350. $max_num_views = 1;
  351. while ($row_topic_views = $smcFunc['db_fetch_assoc']($topic_view_result))
  352. {
  353. censorText($row_topic_views['subject']);
  354. $context['top_topics_views'][] = array(
  355. 'id' => $row_topic_views['id_topic'],
  356. 'board' => array(
  357. 'id' => $row_topic_views['id_board'],
  358. 'name' => $row_topic_views['name'],
  359. 'href' => $scripturl . '?board=' . $row_topic_views['id_board'] . '.0',
  360. 'link' => '<a href="' . $scripturl . '?board=' . $row_topic_views['id_board'] . '.0">' . $row_topic_views['name'] . '</a>'
  361. ),
  362. 'subject' => $row_topic_views['subject'],
  363. 'num_views' => $row_topic_views['num_views'],
  364. 'href' => $scripturl . '?topic=' . $row_topic_views['id_topic'] . '.0',
  365. 'link' => '<a href="' . $scripturl . '?topic=' . $row_topic_views['id_topic'] . '.0">' . $row_topic_views['subject'] . '</a>'
  366. );
  367. if ($max_num_views < $row_topic_views['num_views'])
  368. $max_num_views = $row_topic_views['num_views'];
  369. }
  370. $smcFunc['db_free_result']($topic_view_result);
  371. foreach ($context['top_topics_views'] as $i => $topic)
  372. {
  373. $context['top_topics_views'][$i]['post_percent'] = round(($topic['num_views'] * 100) / $max_num_views);
  374. $context['top_topics_views'][$i]['num_views'] = comma_format($context['top_topics_views'][$i]['num_views']);
  375. }
  376. // Try to cache this when possible, because it's a little unavoidably slow.
  377. if (($members = cache_get_data('stats_top_starters', 360)) == null)
  378. {
  379. $request = $smcFunc['db_query']('', '
  380. SELECT id_member_started, COUNT(*) AS hits
  381. FROM {db_prefix}topics' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  382. WHERE id_board != {int:recycle_board}' : '') . '
  383. GROUP BY id_member_started
  384. ORDER BY hits DESC
  385. LIMIT 20',
  386. array(
  387. 'recycle_board' => $modSettings['recycle_board'],
  388. )
  389. );
  390. $members = array();
  391. while ($row = $smcFunc['db_fetch_assoc']($request))
  392. $members[$row['id_member_started']] = $row['hits'];
  393. $smcFunc['db_free_result']($request);
  394. cache_put_data('stats_top_starters', $members, 360);
  395. }
  396. if (empty($members))
  397. $members = array(0 => 0);
  398. // Topic poster top 10.
  399. $members_result = $smcFunc['db_query']('top_topic_starters', '
  400. SELECT id_member, real_name
  401. FROM {db_prefix}members
  402. WHERE id_member IN ({array_int:member_list})
  403. ORDER BY FIND_IN_SET(id_member, {string:top_topic_posters})
  404. LIMIT 10',
  405. array(
  406. 'member_list' => array_keys($members),
  407. 'top_topic_posters' => implode(',', array_keys($members)),
  408. )
  409. );
  410. $context['top_starters'] = array();
  411. $max_num_topics = 1;
  412. while ($row_members = $smcFunc['db_fetch_assoc']($members_result))
  413. {
  414. $context['top_starters'][] = array(
  415. 'name' => $row_members['real_name'],
  416. 'id' => $row_members['id_member'],
  417. 'num_topics' => $members[$row_members['id_member']],
  418. 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'],
  419. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>'
  420. );
  421. if ($max_num_topics < $members[$row_members['id_member']])
  422. $max_num_topics = $members[$row_members['id_member']];
  423. }
  424. $smcFunc['db_free_result']($members_result);
  425. foreach ($context['top_starters'] as $i => $topic)
  426. {
  427. $context['top_starters'][$i]['post_percent'] = round(($topic['num_topics'] * 100) / $max_num_topics);
  428. $context['top_starters'][$i]['num_topics'] = comma_format($context['top_starters'][$i]['num_topics']);
  429. }
  430. // Time online top 10.
  431. $temp = cache_get_data('stats_total_time_members', 600);
  432. $members_result = $smcFunc['db_query']('', '
  433. SELECT id_member, real_name, total_time_logged_in
  434. FROM {db_prefix}members' . (!empty($temp) ? '
  435. WHERE id_member IN ({array_int:member_list_cached})' : '') . '
  436. ORDER BY total_time_logged_in DESC
  437. LIMIT 20',
  438. array(
  439. 'member_list_cached' => $temp,
  440. )
  441. );
  442. $context['top_time_online'] = array();
  443. $temp2 = array();
  444. $max_time_online = 1;
  445. while ($row_members = $smcFunc['db_fetch_assoc']($members_result))
  446. {
  447. $temp2[] = (int) $row_members['id_member'];
  448. if (count($context['top_time_online']) >= 10)
  449. continue;
  450. // Figure out the days, hours and minutes.
  451. $timeDays = floor($row_members['total_time_logged_in'] / 86400);
  452. $timeHours = floor(($row_members['total_time_logged_in'] % 86400) / 3600);
  453. // Figure out which things to show... (days, hours, minutes, etc.)
  454. $timelogged = '';
  455. if ($timeDays > 0)
  456. $timelogged .= $timeDays . $txt['totalTimeLogged5'];
  457. if ($timeHours > 0)
  458. $timelogged .= $timeHours . $txt['totalTimeLogged6'];
  459. $timelogged .= floor(($row_members['total_time_logged_in'] % 3600) / 60) . $txt['totalTimeLogged7'];
  460. $context['top_time_online'][] = array(
  461. 'id' => $row_members['id_member'],
  462. 'name' => $row_members['real_name'],
  463. 'time_online' => $timelogged,
  464. 'seconds_online' => $row_members['total_time_logged_in'],
  465. 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'],
  466. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>'
  467. );
  468. if ($max_time_online < $row_members['total_time_logged_in'])
  469. $max_time_online = $row_members['total_time_logged_in'];
  470. }
  471. $smcFunc['db_free_result']($members_result);
  472. foreach ($context['top_time_online'] as $i => $member)
  473. $context['top_time_online'][$i]['time_percent'] = round(($member['seconds_online'] * 100) / $max_time_online);
  474. // Cache the ones we found for a bit, just so we don't have to look again.
  475. if ($temp !== $temp2)
  476. cache_put_data('stats_total_time_members', $temp2, 480);
  477. // Activity by month.
  478. $months_result = $smcFunc['db_query']('', '
  479. SELECT
  480. 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
  481. FROM {db_prefix}log_activity
  482. GROUP BY stats_year, stats_month',
  483. array()
  484. );
  485. $context['yearly'] = array();
  486. while ($row_months = $smcFunc['db_fetch_assoc']($months_result))
  487. {
  488. $ID_MONTH = $row_months['stats_year'] . sprintf('%02d', $row_months['stats_month']);
  489. $expanded = !empty($_SESSION['expanded_stats'][$row_months['stats_year']]) && in_array($row_months['stats_month'], $_SESSION['expanded_stats'][$row_months['stats_year']]);
  490. if (!isset($context['yearly'][$row_months['stats_year']]))
  491. $context['yearly'][$row_months['stats_year']] = array(
  492. 'year' => $row_months['stats_year'],
  493. 'new_topics' => 0,
  494. 'new_posts' => 0,
  495. 'new_members' => 0,
  496. 'most_members_online' => 0,
  497. 'hits' => 0,
  498. 'num_months' => 0,
  499. 'months' => array(),
  500. 'expanded' => false,
  501. 'current_year' => $row_months['stats_year'] == date('Y'),
  502. );
  503. $context['yearly'][$row_months['stats_year']]['months'][(int) $row_months['stats_month']] = array(
  504. 'id' => $ID_MONTH,
  505. 'date' => array(
  506. 'month' => sprintf('%02d', $row_months['stats_month']),
  507. 'year' => $row_months['stats_year']
  508. ),
  509. 'href' => $scripturl . '?action=stats;' . ($expanded ? 'collapse' : 'expand') . '=' . $ID_MONTH . '#m' . $ID_MONTH,
  510. 'link' => '<a href="' . $scripturl . '?action=stats;' . ($expanded ? 'collapse' : 'expand') . '=' . $ID_MONTH . '#m' . $ID_MONTH . '">' . $txt['months'][(int) $row_months['stats_month']] . ' ' . $row_months['stats_year'] . '</a>',
  511. 'month' => $txt['months'][(int) $row_months['stats_month']],
  512. 'year' => $row_months['stats_year'],
  513. 'new_topics' => comma_format($row_months['topics']),
  514. 'new_posts' => comma_format($row_months['posts']),
  515. 'new_members' => comma_format($row_months['registers']),
  516. 'most_members_online' => comma_format($row_months['most_on']),
  517. 'hits' => comma_format($row_months['hits']),
  518. 'num_days' => $row_months['num_days'],
  519. 'days' => array(),
  520. 'expanded' => $expanded
  521. );
  522. $context['yearly'][$row_months['stats_year']]['new_topics'] += $row_months['topics'];
  523. $context['yearly'][$row_months['stats_year']]['new_posts'] += $row_months['posts'];
  524. $context['yearly'][$row_months['stats_year']]['new_members'] += $row_months['registers'];
  525. $context['yearly'][$row_months['stats_year']]['hits'] += $row_months['hits'];
  526. $context['yearly'][$row_months['stats_year']]['num_months']++;
  527. $context['yearly'][$row_months['stats_year']]['expanded'] |= $expanded;
  528. $context['yearly'][$row_months['stats_year']]['most_members_online'] = max($context['yearly'][$row_months['stats_year']]['most_members_online'], $row_months['most_on']);
  529. }
  530. krsort($context['yearly']);
  531. $context['collapsed_years'] = array();
  532. foreach ($context['yearly'] as $year => $data)
  533. {
  534. // This gets rid of the filesort on the query ;).
  535. krsort($context['yearly'][$year]['months']);
  536. $context['yearly'][$year]['new_topics'] = comma_format($data['new_topics']);
  537. $context['yearly'][$year]['new_posts'] = comma_format($data['new_posts']);
  538. $context['yearly'][$year]['new_members'] = comma_format($data['new_members']);
  539. $context['yearly'][$year]['most_members_online'] = comma_format($data['most_members_online']);
  540. $context['yearly'][$year]['hits'] = comma_format($data['hits']);
  541. // Keep a list of collapsed years.
  542. if (!$data['expanded'] && !$data['current_year'])
  543. $context['collapsed_years'][] = $year;
  544. }
  545. if (empty($_SESSION['expanded_stats']))
  546. return;
  547. $condition_text = array();
  548. $condition_params = array();
  549. foreach ($_SESSION['expanded_stats'] as $year => $months)
  550. if (!empty($months))
  551. {
  552. $condition_text[] = 'YEAR(date) = {int:year_' . $year . '} AND MONTH(date) IN ({array_int:months_' . $year . '})';
  553. $condition_params['year_' . $year] = $year;
  554. $condition_params['months_' . $year] = $months;
  555. }
  556. // No daily stats to even look at?
  557. if (empty($condition_text))
  558. return;
  559. getDailyStats(implode(' OR ', $condition_text), $condition_params);
  560. // Custom stats (just add a template_layer to add it to the template!)
  561. call_integration_hook('integrate_forum_stats');
  562. }
  563. /**
  564. * Loads the statistics on a daily basis in $context.
  565. * called by DisplayStats().
  566. * @param string $condition_string
  567. * @param array $condition_parameters = array()
  568. */
  569. function getDailyStats($condition_string, $condition_parameters = array())
  570. {
  571. global $context, $smcFunc;
  572. // Activity by day.
  573. $days_result = $smcFunc['db_query']('', '
  574. SELECT YEAR(date) AS stats_year, MONTH(date) AS stats_month, DAYOFMONTH(date) AS stats_day, topics, posts, registers, most_on, hits
  575. FROM {db_prefix}log_activity
  576. WHERE ' . $condition_string . '
  577. ORDER BY stats_day ASC',
  578. $condition_parameters
  579. );
  580. while ($row_days = $smcFunc['db_fetch_assoc']($days_result))
  581. $context['yearly'][$row_days['stats_year']]['months'][(int) $row_days['stats_month']]['days'][] = array(
  582. 'day' => sprintf('%02d', $row_days['stats_day']),
  583. 'month' => sprintf('%02d', $row_days['stats_month']),
  584. 'year' => $row_days['stats_year'],
  585. 'new_topics' => comma_format($row_days['topics']),
  586. 'new_posts' => comma_format($row_days['posts']),
  587. 'new_members' => comma_format($row_days['registers']),
  588. 'most_members_online' => comma_format($row_days['most_on']),
  589. 'hits' => comma_format($row_days['hits'])
  590. );
  591. $smcFunc['db_free_result']($days_result);
  592. }
  593. /**
  594. * This is the function which returns stats to simplemachines.org IF enabled!
  595. * called by simplemachines.org.
  596. * only returns anything if stats was enabled during installation.
  597. * can also be accessed by the admin, to show what stats sm.org collects.
  598. * does not return any data directly to sm.org, instead starts a new request for security.
  599. *
  600. * @link http://www.simplemachines.org/about/stats.php for more info.
  601. */
  602. function SMStats()
  603. {
  604. global $modSettings, $user_info, $forum_version, $sourcedir;
  605. // First, is it disabled?
  606. if (empty($modSettings['allow_sm_stats']))
  607. die();
  608. // Are we saying who we are, and are we right? (OR an admin)
  609. if (!$user_info['is_admin'] && (!isset($_GET['sid']) || $_GET['sid'] != $modSettings['allow_sm_stats']))
  610. die();
  611. // Verify the referer...
  612. if (!$user_info['is_admin'] && (!isset($_SERVER['HTTP_REFERER']) || md5($_SERVER['HTTP_REFERER']) != '746cb59a1a0d5cf4bd240e5a67c73085'))
  613. die();
  614. // Get some server versions.
  615. require_once($sourcedir . '/Subs-Admin.php');
  616. $checkFor = array(
  617. 'php',
  618. 'db_server',
  619. );
  620. $serverVersions = getServerVersions($checkFor);
  621. // Get the actual stats.
  622. $stats_to_send = array(
  623. 'UID' => $modSettings['allow_sm_stats'],
  624. 'time_added' => time(),
  625. 'members' => $modSettings['totalMembers'],
  626. 'messages' => $modSettings['totalMessages'],
  627. 'topics' => $modSettings['totalTopics'],
  628. 'boards' => 0,
  629. 'php_version' => $serverVersions['php']['version'],
  630. 'database_type' => strtolower($serverVersions['db_server']['title']),
  631. 'database_version' => $serverVersions['db_server']['version'],
  632. 'smf_version' => $forum_version,
  633. 'smfd_version' => $modSettings['smfVersion'],
  634. );
  635. // Encode all the data, for security.
  636. foreach ($stats_to_send as $k => $v)
  637. $stats_to_send[$k] = urlencode($k) . '=' . urlencode($v);
  638. // Turn this into the query string!
  639. $stats_to_send = implode('&', $stats_to_send);
  640. // If we're an admin, just plonk them out.
  641. if ($user_info['is_admin'])
  642. echo $stats_to_send;
  643. else
  644. {
  645. // Connect to the collection script.
  646. $fp = @fsockopen('www.simplemachines.org', 80, $errno, $errstr);
  647. if ($fp)
  648. {
  649. $length = strlen($stats_to_send);
  650. $out = 'POST /smf/stats/collect_stats.php HTTP/1.1' . "\r\n";
  651. $out .= 'Host: www.simplemachines.org' . "\r\n";
  652. $out .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
  653. $out .= 'Content-Length: ' . $length . "\r\n\r\n";
  654. $out .= $stats_to_send . "\r\n";
  655. $out .= 'Connection: Close' . "\r\n\r\n";
  656. fwrite($fp, $out);
  657. fclose($fp);
  658. }
  659. }
  660. // Die.
  661. die('OK');
  662. }
  663. ?>