Stats.php 27 KB

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