2
0

Stats.php 27 KB

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