Stats.php 26 KB

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