Stats.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This function has only one job: providing a display for forum statistics.
  15. As such, it has only one function:
  16. void DisplayStats()
  17. - gets all the statistics in order and puts them in.
  18. - uses the Stats template and language file. (and main sub template.)
  19. - requires the view_stats permission.
  20. - accessed from ?action=stats.
  21. void getDailyStats(string $condition)
  22. - called by DisplayStats().
  23. - loads the statistics on a daily basis in $context.
  24. void SMStats()
  25. - called by simplemachines.org.
  26. - only returns anything if stats was enabled during installation.
  27. - can also be accessed by the admin, to show what stats sm.org collects.
  28. - does not return any data directly to sm.org, instead starts a new request for security.
  29. */
  30. // Display some useful/interesting board statistics.
  31. function DisplayStats()
  32. {
  33. global $txt, $scripturl, $modSettings, $user_info, $context, $smcFunc;
  34. isAllowedTo('view_stats');
  35. if (!empty($_REQUEST['expand']))
  36. {
  37. $context['robot_no_index'] = true;
  38. $month = (int) substr($_REQUEST['expand'], 4);
  39. $year = (int) substr($_REQUEST['expand'], 0, 4);
  40. if ($year > 1900 && $year < 2200 && $month >= 1 && $month <= 12)
  41. $_SESSION['expanded_stats'][$year][] = $month;
  42. }
  43. elseif (!empty($_REQUEST['collapse']))
  44. {
  45. $context['robot_no_index'] = true;
  46. $month = (int) substr($_REQUEST['collapse'], 4);
  47. $year = (int) substr($_REQUEST['collapse'], 0, 4);
  48. if (!empty($_SESSION['expanded_stats'][$year]))
  49. $_SESSION['expanded_stats'][$year] = array_diff($_SESSION['expanded_stats'][$year], array($month));
  50. }
  51. // Handle the XMLHttpRequest.
  52. if (isset($_REQUEST['xml']))
  53. {
  54. // Collapsing stats only needs adjustments of the session variables.
  55. if (!empty($_REQUEST['collapse']))
  56. obExit(false);
  57. $context['sub_template'] = 'stats';
  58. getDailyStats('YEAR(date) = {int:year} AND MONTH(date) = {int:month}', array('year' => $year, 'month' => $month));
  59. $context['yearly'][$year]['months'][$month]['date'] = array(
  60. 'month' => sprintf('%02d', $month),
  61. 'year' => $year,
  62. );
  63. return;
  64. }
  65. loadLanguage('Stats');
  66. loadTemplate('Stats');
  67. // Build the link tree......
  68. $context['linktree'][] = array(
  69. 'url' => $scripturl . '?action=stats',
  70. 'name' => $txt['stats_center']
  71. );
  72. $context['page_title'] = $context['forum_name'] . ' - ' . $txt['stats_center'];
  73. $context['show_member_list'] = allowedTo('view_mlist');
  74. // Get averages...
  75. $result = $smcFunc['db_query']('', '
  76. SELECT
  77. SUM(posts) AS posts, SUM(topics) AS topics, SUM(registers) AS registers,
  78. SUM(most_on) AS most_on, MIN(date) AS date, SUM(hits) AS hits
  79. FROM {db_prefix}log_activity',
  80. array(
  81. )
  82. );
  83. $row = $smcFunc['db_fetch_assoc']($result);
  84. $smcFunc['db_free_result']($result);
  85. // This would be the amount of time the forum has been up... in days...
  86. $total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24));
  87. $context['average_posts'] = comma_format(round($row['posts'] / $total_days_up, 2));
  88. $context['average_topics'] = comma_format(round($row['topics'] / $total_days_up, 2));
  89. $context['average_members'] = comma_format(round($row['registers'] / $total_days_up, 2));
  90. $context['average_online'] = comma_format(round($row['most_on'] / $total_days_up, 2));
  91. $context['average_hits'] = comma_format(round($row['hits'] / $total_days_up, 2));
  92. $context['num_hits'] = comma_format($row['hits'], 0);
  93. // How many users are online now.
  94. $result = $smcFunc['db_query']('', '
  95. SELECT COUNT(*)
  96. FROM {db_prefix}log_online',
  97. array(
  98. )
  99. );
  100. list ($context['users_online']) = $smcFunc['db_fetch_row']($result);
  101. $smcFunc['db_free_result']($result);
  102. // Statistics such as number of boards, categories, etc.
  103. $result = $smcFunc['db_query']('', '
  104. SELECT COUNT(*)
  105. FROM {db_prefix}boards AS b
  106. WHERE b.redirect = {string:blank_redirect}',
  107. array(
  108. 'blank_redirect' => '',
  109. )
  110. );
  111. list ($context['num_boards']) = $smcFunc['db_fetch_row']($result);
  112. $smcFunc['db_free_result']($result);
  113. $result = $smcFunc['db_query']('', '
  114. SELECT COUNT(*)
  115. FROM {db_prefix}categories AS c',
  116. array(
  117. )
  118. );
  119. list ($context['num_categories']) = $smcFunc['db_fetch_row']($result);
  120. $smcFunc['db_free_result']($result);
  121. // Format the numbers nicely.
  122. $context['users_online'] = comma_format($context['users_online']);
  123. $context['num_boards'] = comma_format($context['num_boards']);
  124. $context['num_categories'] = comma_format($context['num_categories']);
  125. $context['num_members'] = comma_format($modSettings['totalMembers']);
  126. $context['num_posts'] = comma_format($modSettings['totalMessages']);
  127. $context['num_topics'] = comma_format($modSettings['totalTopics']);
  128. $context['most_members_online'] = array(
  129. 'number' => comma_format($modSettings['mostOnline']),
  130. 'date' => timeformat($modSettings['mostDate'])
  131. );
  132. $context['latest_member'] = &$context['common_stats']['latest_member'];
  133. // Male vs. female ratio - let's calculate this only every four minutes.
  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. $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. }
  565. function getDailyStats($condition_string, $condition_parameters = array())
  566. {
  567. global $context, $smcFunc;
  568. // Activity by day.
  569. $days_result = $smcFunc['db_query']('', '
  570. SELECT YEAR(date) AS stats_year, MONTH(date) AS stats_month, DAYOFMONTH(date) AS stats_day, topics, posts, registers, most_on, hits
  571. FROM {db_prefix}log_activity
  572. WHERE ' . $condition_string . '
  573. ORDER BY stats_day ASC',
  574. $condition_parameters
  575. );
  576. while ($row_days = $smcFunc['db_fetch_assoc']($days_result))
  577. $context['yearly'][$row_days['stats_year']]['months'][(int) $row_days['stats_month']]['days'][] = array(
  578. 'day' => sprintf('%02d', $row_days['stats_day']),
  579. 'month' => sprintf('%02d', $row_days['stats_month']),
  580. 'year' => $row_days['stats_year'],
  581. 'new_topics' => comma_format($row_days['topics']),
  582. 'new_posts' => comma_format($row_days['posts']),
  583. 'new_members' => comma_format($row_days['registers']),
  584. 'most_members_online' => comma_format($row_days['most_on']),
  585. 'hits' => comma_format($row_days['hits'])
  586. );
  587. $smcFunc['db_free_result']($days_result);
  588. }
  589. // This is the function which returns stats to simplemachines.org IF enabled!
  590. // See http://www.simplemachines.org/about/stats.php for more info.
  591. function SMStats()
  592. {
  593. global $modSettings, $user_info, $forum_version, $sourcedir;
  594. // First, is it disabled?
  595. if (empty($modSettings['allow_sm_stats']))
  596. die();
  597. // Are we saying who we are, and are we right? (OR an admin)
  598. if (!$user_info['is_admin'] && (!isset($_GET['sid']) || $_GET['sid'] != $modSettings['allow_sm_stats']))
  599. die();
  600. // Verify the referer...
  601. if (!$user_info['is_admin'] && (!isset($_SERVER['HTTP_REFERER']) || md5($_SERVER['HTTP_REFERER']) != '746cb59a1a0d5cf4bd240e5a67c73085'))
  602. die();
  603. // Get some server versions.
  604. require_once($sourcedir . '/Subs-Admin.php');
  605. $checkFor = array(
  606. 'php',
  607. 'db_server',
  608. );
  609. $serverVersions = getServerVersions($checkFor);
  610. // Get the actual stats.
  611. $stats_to_send = array(
  612. 'UID' => $modSettings['allow_sm_stats'],
  613. 'time_added' => time(),
  614. 'members' => $modSettings['totalMembers'],
  615. 'messages' => $modSettings['totalMessages'],
  616. 'topics' => $modSettings['totalTopics'],
  617. 'boards' => 0,
  618. 'php_version' => $serverVersions['php']['version'],
  619. 'database_type' => strtolower($serverVersions['db_server']['title']),
  620. 'database_version' => $serverVersions['db_server']['version'],
  621. 'smf_version' => $forum_version,
  622. 'smfd_version' => $modSettings['smfVersion'],
  623. );
  624. // Encode all the data, for security.
  625. foreach ($stats_to_send as $k => $v)
  626. $stats_to_send[$k] = urlencode($k) . '=' . urlencode($v);
  627. // Turn this into the query string!
  628. $stats_to_send = implode('&', $stats_to_send);
  629. // If we're an admin, just plonk them out.
  630. if ($user_info['is_admin'])
  631. echo $stats_to_send;
  632. else
  633. {
  634. // Connect to the collection script.
  635. $fp = @fsockopen('www.simplemachines.org', 80, $errno, $errstr);
  636. if ($fp)
  637. {
  638. $length = strlen($stats_to_send);
  639. $out = 'POST /smf/stats/collect_stats.php HTTP/1.1' . "\r\n";
  640. $out .= 'Host: www.simplemachines.org' . "\r\n";
  641. $out .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
  642. $out .= 'Content-Length: ' . $length . "\r\n\r\n";
  643. $out .= $stats_to_send . "\r\n";
  644. $out .= 'Connection: Close' . "\r\n\r\n";
  645. fwrite($fp, $out);
  646. fclose($fp);
  647. }
  648. }
  649. // Die.
  650. die('OK');
  651. }
  652. ?>