Logging.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * This file concerns itself with logging, whether in the database or files.
  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. * Put this user in the online log.
  18. *
  19. * @param bool $force = false
  20. */
  21. function writeLog($force = false)
  22. {
  23. global $user_info, $user_settings, $context, $modSettings, $settings, $topic, $board, $smcFunc, $sourcedir;
  24. // If we are showing who is viewing a topic, let's see if we are, and force an update if so - to make it accurate.
  25. if (!empty($settings['display_who_viewing']) && ($topic || $board))
  26. {
  27. // Take the opposite approach!
  28. $force = true;
  29. // Don't update for every page - this isn't wholly accurate but who cares.
  30. if ($topic)
  31. {
  32. if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic)
  33. $force = false;
  34. $_SESSION['last_topic_id'] = $topic;
  35. }
  36. }
  37. // Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check...
  38. if (!empty($user_info['possibly_robot']) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1)
  39. {
  40. require_once($sourcedir . '/ManageSearchEngines.php');
  41. logSpider();
  42. }
  43. // Don't mark them as online more than every so often.
  44. if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force)
  45. return;
  46. if (!empty($modSettings['who_enabled']))
  47. {
  48. $serialized = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']);
  49. // In the case of a dlattach action, session_var may not be set.
  50. if (!isset($context['session_var']))
  51. $context['session_var'] = $_SESSION['session_var'];
  52. unset($serialized['sesc'], $serialized[$context['session_var']]);
  53. $serialized = serialize($serialized);
  54. }
  55. else
  56. $serialized = '';
  57. // Guests use 0, members use their session ID.
  58. $session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id();
  59. // Grab the last all-of-SMF-specific log_online deletion time.
  60. $do_delete = cache_get_data('log_online-update', 30) < time() - 30;
  61. // If the last click wasn't a long time ago, and there was a last click...
  62. if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20)
  63. {
  64. if ($do_delete)
  65. {
  66. $smcFunc['db_query']('delete_log_online_interval', '
  67. DELETE FROM {db_prefix}log_online
  68. WHERE log_time < {int:log_time}
  69. AND session != {string:session}',
  70. array(
  71. 'log_time' => time() - $modSettings['lastActive'] * 60,
  72. 'session' => $session_id,
  73. )
  74. );
  75. // Cache when we did it last.
  76. cache_put_data('log_online-update', time(), 30);
  77. }
  78. $smcFunc['db_query']('', '
  79. UPDATE {db_prefix}log_online
  80. SET log_time = {int:log_time}, ip = IFNULL(INET_ATON({string:ip}), 0), url = {string:url}
  81. WHERE session = {string:session}',
  82. array(
  83. 'log_time' => time(),
  84. 'ip' => $user_info['ip'],
  85. 'url' => $serialized,
  86. 'session' => $session_id,
  87. )
  88. );
  89. // Guess it got deleted.
  90. if ($smcFunc['db_affected_rows']() == 0)
  91. $_SESSION['log_time'] = 0;
  92. }
  93. else
  94. $_SESSION['log_time'] = 0;
  95. // Otherwise, we have to delete and insert.
  96. if (empty($_SESSION['log_time']))
  97. {
  98. if ($do_delete || !empty($user_info['id']))
  99. $smcFunc['db_query']('', '
  100. DELETE FROM {db_prefix}log_online
  101. WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'),
  102. array(
  103. 'current_member' => $user_info['id'],
  104. 'log_time' => time() - $modSettings['lastActive'] * 60,
  105. )
  106. );
  107. $smcFunc['db_insert']($do_delete ? 'ignore' : 'replace',
  108. '{db_prefix}log_online',
  109. array('session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'raw', 'url' => 'string'),
  110. array($session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), 'IFNULL(INET_ATON(\'' . $user_info['ip'] . '\'), 0)', $serialized),
  111. array('session')
  112. );
  113. }
  114. // Mark your session as being logged.
  115. $_SESSION['log_time'] = time();
  116. // Well, they are online now.
  117. if (empty($_SESSION['timeOnlineUpdated']))
  118. $_SESSION['timeOnlineUpdated'] = time();
  119. // Set their login time, if not already done within the last minute.
  120. if (SMF != 'SSI' && !empty($user_info['last_login']) && $user_info['last_login'] < time() - 60)
  121. {
  122. // Don't count longer than 15 minutes.
  123. if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15)
  124. $_SESSION['timeOnlineUpdated'] = time();
  125. $user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
  126. updateMemberData($user_info['id'], array('last_login' => time(), 'member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP'], 'total_time_logged_in' => $user_settings['total_time_logged_in']));
  127. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  128. cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60);
  129. $user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
  130. $_SESSION['timeOnlineUpdated'] = time();
  131. }
  132. }
  133. /**
  134. * Logs the last database error into a file.
  135. * Attempts to use the backup file first, to store the last database error
  136. * and only update Settings.php if the first was successful.
  137. */
  138. function logLastDatabaseError()
  139. {
  140. global $boarddir;
  141. // Find out this way if we can even write things on this filesystem.
  142. // In addition, store things first in the backup file
  143. $last_settings_change = @filemtime($boarddir . '/Settings.php');
  144. // Make sure the backup file is there...
  145. $file = $boarddir . '/Settings_bak.php';
  146. if ((!file_exists($file) || filesize($file) == 0) && !copy($boarddir . '/Settings.php', $file))
  147. return false;
  148. // ...and writable!
  149. if (!is_writable($file))
  150. {
  151. chmod($file, 0755);
  152. if (!is_writable($file))
  153. {
  154. chmod($file, 0775);
  155. if (!is_writable($file))
  156. {
  157. chmod($file, 0777);
  158. if (!is_writable($file))
  159. return false;
  160. }
  161. }
  162. }
  163. // Put the new timestamp.
  164. $data = file_get_contents($file);
  165. $data = preg_replace('~\$db_last_error = \d+;~', '$db_last_error = ' . time() . ';', $data);
  166. // Open the backup file for writing
  167. if ($fp = @fopen($file, 'w'))
  168. {
  169. // Reset the file buffer.
  170. set_file_buffer($fp, 0);
  171. // Update the file.
  172. $t = flock($fp, LOCK_EX);
  173. $bytes = fwrite($fp, $data);
  174. flock($fp, LOCK_UN);
  175. fclose($fp);
  176. // Was it a success?
  177. // ...only relevant if we're still dealing with the same good ole' settings file.
  178. clearstatcache();
  179. if (($bytes == strlen($data)) && (filemtime($boarddir . '/Settings.php') === $last_settings_change))
  180. {
  181. // This is our new Settings file...
  182. // At least this one is an atomic operation
  183. @copy($file, $boarddir . '/Settings.php');
  184. return true;
  185. }
  186. else
  187. {
  188. // Oops. Someone might have been faster
  189. // or we have no more disk space left, troubles, troubles...
  190. // Copy the file back and run for your life!
  191. @copy($boarddir . '/Settings.php', $file);
  192. }
  193. }
  194. return false;
  195. }
  196. /**
  197. * This function shows the debug information tracked when $db_show_debug = true
  198. * in Settings.php
  199. */
  200. function displayDebug()
  201. {
  202. global $context, $scripturl, $boarddir, $modSettings, $boarddir;
  203. global $db_cache, $db_count, $db_show_debug, $cache_count, $cache_hits, $txt;
  204. // Add to Settings.php if you want to show the debugging information.
  205. if (!isset($db_show_debug) || $db_show_debug !== true || (isset($_GET['action']) && $_GET['action'] == 'viewquery') || WIRELESS)
  206. return;
  207. if (empty($_SESSION['view_queries']))
  208. $_SESSION['view_queries'] = 0;
  209. if (empty($context['debug']['language_files']))
  210. $context['debug']['language_files'] = array();
  211. if (empty($context['debug']['sheets']))
  212. $context['debug']['sheets'] = array();
  213. $files = get_included_files();
  214. $total_size = 0;
  215. for ($i = 0, $n = count($files); $i < $n; $i++)
  216. {
  217. if (file_exists($files[$i]))
  218. $total_size += filesize($files[$i]);
  219. $files[$i] = strtr($files[$i], array($boarddir => '.'));
  220. }
  221. $warnings = 0;
  222. if (!empty($db_cache))
  223. {
  224. foreach ($db_cache as $q => $qq)
  225. {
  226. if (!empty($qq['w']))
  227. $warnings += count($qq['w']);
  228. }
  229. $_SESSION['debug'] = &$db_cache;
  230. }
  231. // Gotta have valid HTML ;).
  232. $temp = ob_get_contents();
  233. ob_clean();
  234. echo preg_replace('~</body>\s*</html>~', '', $temp), '
  235. <div class="smalltext" style="text-align: left; margin: 1ex;">
  236. ', $txt['debug_templates'], count($context['debug']['templates']), ': <em>', implode('</em>, <em>', $context['debug']['templates']), '</em>.<br />
  237. ', $txt['debug_subtemplates'], count($context['debug']['sub_templates']), ': <em>', implode('</em>, <em>', $context['debug']['sub_templates']), '</em>.<br />
  238. ', $txt['debug_language_files'], count($context['debug']['language_files']), ': <em>', implode('</em>, <em>', $context['debug']['language_files']), '</em>.<br />
  239. ', $txt['debug_stylesheets'], count($context['debug']['sheets']), ': <em>', implode('</em>, <em>', $context['debug']['sheets']), '</em>.<br />
  240. ', $txt['debug_files_included'], count($files), ' - ', round($total_size / 1024), $txt['debug_kb'], ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_include_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_include_info" style="display: none;"><em>', implode('</em>, <em>', $files), '</em></span>)<br />';
  241. if (!empty($modSettings['cache_enable']) && !empty($cache_hits))
  242. {
  243. $entries = array();
  244. $total_t = 0;
  245. $total_s = 0;
  246. foreach ($cache_hits as $cache_hit)
  247. {
  248. $entries[] = $cache_hit['d'] . ' ' . $cache_hit['k'] . ': ' . sprintf($txt['debug_cache_seconds_bytes'], comma_format($cache_hit['t'], 5), $cache_hit['s']);
  249. $total_t += $cache_hit['t'];
  250. $total_s += $cache_hit['s'];
  251. }
  252. echo '
  253. ', $txt['debug_cache_hits'], $cache_count, ': ', sprintf($txt['debug_cache_seconds_bytes_total'], comma_format($total_t, 5), comma_format($total_s)), ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_cache_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_cache_info" style="display: none;"><em>', implode('</em>, <em>', $entries), '</em></span>)<br />';
  254. }
  255. echo '
  256. <a href="', $scripturl, '?action=viewquery" target="_blank" class="new_win">', $warnings == 0 ? sprintf($txt['debug_queries_used'], (int) $db_count) : sprintf($txt['debug_queries_used_and_warnings'], (int) $db_count, $warnings), '</a><br />
  257. <br />';
  258. if ($_SESSION['view_queries'] == 1 && !empty($db_cache))
  259. foreach ($db_cache as $q => $qq)
  260. {
  261. $is_select = strpos(trim($qq['q']), 'SELECT') === 0 || preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+SELECT .+$~s', trim($qq['q'])) != 0;
  262. // Temporary tables created in earlier queries are not explainable.
  263. if ($is_select)
  264. {
  265. foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
  266. if (strpos(trim($qq['q']), $tmp) !== false)
  267. {
  268. $is_select = false;
  269. break;
  270. }
  271. }
  272. // But actual creation of the temporary tables are.
  273. elseif (preg_match('~^CREATE TEMPORARY TABLE .+?SELECT .+$~s', trim($qq['q'])) != 0)
  274. $is_select = true;
  275. // Make the filenames look a bit better.
  276. if (isset($qq['f']))
  277. $qq['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $qq['f']);
  278. echo '
  279. <strong>', $is_select ? '<a href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '" target="_blank" class="new_win" style="text-decoration: none;">' : '', nl2br(str_replace("\t", '&nbsp;&nbsp;&nbsp;', htmlspecialchars(ltrim($qq['q'], "\n\r")))) . ($is_select ? '</a></strong>' : '</strong>') . '<br />
  280. &nbsp;&nbsp;&nbsp;';
  281. if (!empty($qq['f']) && !empty($qq['l']))
  282. echo sprintf($txt['debug_query_in_line'], $qq['f'], $qq['l']);
  283. if (isset($qq['s'], $qq['t']) && isset($txt['debug_query_which_took_at']))
  284. echo sprintf($txt['debug_query_which_took_at'], round($qq['t'], 8), round($qq['s'], 8)) . '<br />';
  285. elseif (isset($qq['t']))
  286. echo sprintf($txt['debug_query_which_took'], round($qq['t'], 8)) . '<br />';
  287. echo '
  288. <br />';
  289. }
  290. echo '
  291. <a href="' . $scripturl . '?action=viewquery;sa=hide">', $txt['debug_' . (empty($_SESSION['view_queries']) ? 'show' : 'hide') . '_queries'], '</a>
  292. </div></body></html>';
  293. }
  294. /**
  295. * Track Statistics.
  296. * Caches statistics changes, and flushes them if you pass nothing.
  297. * If '+' is used as a value, it will be incremented.
  298. * It does not actually commit the changes until the end of the page view.
  299. * It depends on the trackStats setting.
  300. *
  301. * @param array $stats = array()
  302. * @return bool|array
  303. */
  304. function trackStats($stats = array())
  305. {
  306. global $modSettings, $smcFunc;
  307. static $cache_stats = array();
  308. if (empty($modSettings['trackStats']))
  309. return false;
  310. if (!empty($stats))
  311. return $cache_stats = array_merge($cache_stats, $stats);
  312. elseif (empty($cache_stats))
  313. return false;
  314. $setStringUpdate = '';
  315. $insert_keys = array();
  316. $date = strftime('%Y-%m-%d', forum_time(false));
  317. $update_parameters = array(
  318. 'current_date' => $date,
  319. );
  320. foreach ($cache_stats as $field => $change)
  321. {
  322. $setStringUpdate .= '
  323. ' . $field . ' = ' . ($change === '+' ? $field . ' + 1' : '{int:' . $field . '}') . ',';
  324. if ($change === '+')
  325. $cache_stats[$field] = 1;
  326. else
  327. $update_parameters[$field] = $change;
  328. $insert_keys[$field] = 'int';
  329. }
  330. $smcFunc['db_query']('', '
  331. UPDATE {db_prefix}log_activity
  332. SET' . substr($setStringUpdate, 0, -1) . '
  333. WHERE date = {date:current_date}',
  334. $update_parameters
  335. );
  336. if ($smcFunc['db_affected_rows']() == 0)
  337. {
  338. $smcFunc['db_insert']('ignore',
  339. '{db_prefix}log_activity',
  340. array_merge($insert_keys, array('date' => 'date')),
  341. array_merge($cache_stats, array($date)),
  342. array('date')
  343. );
  344. }
  345. // Don't do this again.
  346. $cache_stats = array();
  347. return true;
  348. }
  349. /**
  350. * This function logs an action in the respective log. (database log)
  351. * @example logAction('remove', array('starter' => $id_member_started));
  352. *
  353. * @param string $action
  354. * @param array $extra = array()
  355. * @param string $log_type, options 'moderate', 'admin', ...etc.
  356. */
  357. function logAction($action, $extra = array(), $log_type = 'moderate')
  358. {
  359. global $modSettings, $user_info, $smcFunc, $sourcedir;
  360. $log_types = array(
  361. 'moderate' => 1,
  362. 'user' => 2,
  363. 'admin' => 3,
  364. );
  365. // No point in doing anything, if the log isn't even enabled.
  366. if (empty($modSettings['modlog_enabled']) || !isset($log_types[$log_type]))
  367. return false;
  368. if (!is_array($extra))
  369. trigger_error('logAction(): data is not an array with action \'' . $action . '\'', E_USER_NOTICE);
  370. // Pull out the parts we want to store separately, but also make sure that the data is proper
  371. if (isset($extra['topic']))
  372. {
  373. if (!is_numeric($extra['topic']))
  374. trigger_error('logAction(): data\'s topic is not a number', E_USER_NOTICE);
  375. $topic_id = empty($extra['topic']) ? '0' : (int)$extra['topic'];
  376. unset($extra['topic']);
  377. }
  378. else
  379. $topic_id = '0';
  380. if (isset($extra['message']))
  381. {
  382. if (!is_numeric($extra['message']))
  383. trigger_error('logAction(): data\'s message is not a number', E_USER_NOTICE);
  384. $msg_id = empty($extra['message']) ? '0' : (int)$extra['message'];
  385. unset($extra['message']);
  386. }
  387. else
  388. $msg_id = '0';
  389. // @todo cache this?
  390. // Is there an associated report on this?
  391. if (in_array($action, array('move', 'remove', 'split', 'merge')))
  392. {
  393. $request = $smcFunc['db_query']('', '
  394. SELECT id_report
  395. FROM {db_prefix}log_reported
  396. WHERE {raw:column_name} = {int:reported}
  397. LIMIT 1',
  398. array(
  399. 'column_name' => !empty($msg_id) ? 'id_msg' : 'id_topic',
  400. 'reported' => !empty($msg_id) ? $msg_id : $topic_id,
  401. ));
  402. // Alright, if we get any result back, update open reports.
  403. if ($smcFunc['db_num_rows']($request) > 0)
  404. {
  405. require_once($sourcedir . '/ModerationCenter.php');
  406. updateSettings(array('last_mod_report_action' => time()));
  407. recountOpenReports();
  408. }
  409. $smcFunc['db_free_result']($request);
  410. }
  411. if (isset($extra['member']) && !is_numeric($extra['member']))
  412. trigger_error('logAction(): data\'s member is not a number', E_USER_NOTICE);
  413. if (isset($extra['board']))
  414. {
  415. if (!is_numeric($extra['board']))
  416. trigger_error('logAction(): data\'s board is not a number', E_USER_NOTICE);
  417. $board_id = empty($extra['board']) ? '0' : (int)$extra['board'];
  418. unset($extra['board']);
  419. }
  420. else
  421. $board_id = '0';
  422. if (isset($extra['board_to']))
  423. {
  424. if (!is_numeric($extra['board_to']))
  425. trigger_error('logAction(): data\'s board_to is not a number', E_USER_NOTICE);
  426. if (empty($board_id))
  427. {
  428. $board_id = empty($extra['board_to']) ? '0' : (int)$extra['board_to'];
  429. unset($extra['board_to']);
  430. }
  431. }
  432. if (isset($extra['member_affected']))
  433. $memID = $extra['member_affected'];
  434. else
  435. $memID = $user_info['id'];
  436. $smcFunc['db_insert']('',
  437. '{db_prefix}log_actions',
  438. array(
  439. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  440. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  441. ),
  442. array(
  443. time(), $log_types[$log_type], $memID, $user_info['ip'], $action,
  444. $board_id, $topic_id, $msg_id, serialize($extra),
  445. ),
  446. array('id_action')
  447. );
  448. return $smcFunc['db_insert_id']('{db_prefix}log_actions', 'id_action');
  449. }
  450. ?>