1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file concerns itself with logging, whether in the database or files. |
||
5 | * |
||
6 | * Simple Machines Forum (SMF) |
||
7 | * |
||
8 | * @package SMF |
||
9 | * @author Simple Machines https://www.simplemachines.org |
||
10 | * @copyright 2022 Simple Machines and individual contributors |
||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1.0 |
||
14 | */ |
||
15 | |||
16 | if (!defined('SMF')) |
||
17 | die('No direct access...'); |
||
18 | |||
19 | /** |
||
20 | * Put this user in the online log. |
||
21 | * |
||
22 | * @param bool $force Whether to force logging the data |
||
23 | */ |
||
24 | function writeLog($force = false) |
||
25 | { |
||
26 | global $user_info, $user_settings, $context, $modSettings, $settings, $topic, $board, $smcFunc, $sourcedir, $cache_enable; |
||
27 | |||
28 | // 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. |
||
29 | if (!empty($settings['display_who_viewing']) && ($topic || $board)) |
||
30 | { |
||
31 | // Take the opposite approach! |
||
32 | $force = true; |
||
33 | // Don't update for every page - this isn't wholly accurate but who cares. |
||
34 | if ($topic) |
||
35 | { |
||
36 | if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic) |
||
37 | $force = false; |
||
38 | $_SESSION['last_topic_id'] = $topic; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | // Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check... |
||
43 | if (!empty($user_info['possibly_robot']) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1) |
||
44 | { |
||
45 | require_once($sourcedir . '/ManageSearchEngines.php'); |
||
46 | logSpider(); |
||
47 | } |
||
48 | |||
49 | // Don't mark them as online more than every so often. |
||
50 | if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force) |
||
51 | return; |
||
52 | |||
53 | if (!empty($modSettings['who_enabled'])) |
||
54 | { |
||
55 | $encoded_get = truncate_array($_GET) + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']); |
||
56 | |||
57 | // In the case of a dlattach action, session_var may not be set. |
||
58 | if (!isset($context['session_var'])) |
||
59 | $context['session_var'] = $_SESSION['session_var']; |
||
60 | |||
61 | unset($encoded_get['sesc'], $encoded_get[$context['session_var']]); |
||
62 | $encoded_get = $smcFunc['json_encode']($encoded_get); |
||
63 | } |
||
64 | else |
||
65 | $encoded_get = ''; |
||
66 | |||
67 | // Guests use 0, members use their session ID. |
||
68 | $session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id(); |
||
69 | |||
70 | // Grab the last all-of-SMF-specific log_online deletion time. |
||
71 | $do_delete = cache_get_data('log_online-update', 30) < time() - 30; |
||
72 | |||
73 | // If the last click wasn't a long time ago, and there was a last click... |
||
74 | if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20) |
||
75 | { |
||
76 | if ($do_delete) |
||
77 | { |
||
78 | $smcFunc['db_query']('delete_log_online_interval', ' |
||
79 | DELETE FROM {db_prefix}log_online |
||
80 | WHERE log_time < {int:log_time} |
||
81 | AND session != {string:session}', |
||
82 | array( |
||
83 | 'log_time' => time() - $modSettings['lastActive'] * 60, |
||
84 | 'session' => $session_id, |
||
85 | ) |
||
86 | ); |
||
87 | |||
88 | // Cache when we did it last. |
||
89 | cache_put_data('log_online-update', time(), 30); |
||
90 | } |
||
91 | |||
92 | $smcFunc['db_query']('', ' |
||
93 | UPDATE {db_prefix}log_online |
||
94 | SET log_time = {int:log_time}, ip = {inet:ip}, url = {string:url} |
||
95 | WHERE session = {string:session}', |
||
96 | array( |
||
97 | 'log_time' => time(), |
||
98 | 'ip' => $user_info['ip'], |
||
99 | 'url' => $encoded_get, |
||
100 | 'session' => $session_id, |
||
101 | ) |
||
102 | ); |
||
103 | |||
104 | // Guess it got deleted. |
||
105 | if ($smcFunc['db_affected_rows']() == 0) |
||
106 | $_SESSION['log_time'] = 0; |
||
107 | } |
||
108 | else |
||
109 | $_SESSION['log_time'] = 0; |
||
110 | |||
111 | // Otherwise, we have to delete and insert. |
||
112 | if (empty($_SESSION['log_time'])) |
||
113 | { |
||
114 | if ($do_delete || !empty($user_info['id'])) |
||
115 | $smcFunc['db_query']('', ' |
||
116 | DELETE FROM {db_prefix}log_online |
||
117 | WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'), |
||
118 | array( |
||
119 | 'current_member' => $user_info['id'], |
||
120 | 'log_time' => time() - $modSettings['lastActive'] * 60, |
||
121 | ) |
||
122 | ); |
||
123 | |||
124 | $smcFunc['db_insert']($do_delete ? 'ignore' : 'replace', |
||
125 | '{db_prefix}log_online', |
||
126 | array('session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'inet', 'url' => 'string'), |
||
127 | array($session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), $user_info['ip'], $encoded_get), |
||
128 | array('session') |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | // Mark your session as being logged. |
||
133 | $_SESSION['log_time'] = time(); |
||
134 | |||
135 | // Well, they are online now. |
||
136 | if (empty($_SESSION['timeOnlineUpdated'])) |
||
137 | $_SESSION['timeOnlineUpdated'] = time(); |
||
138 | |||
139 | // Set their login time, if not already done within the last minute. |
||
140 | if (SMF != 'SSI' && !empty($user_info['last_login']) && $user_info['last_login'] < time() - 60 && (!isset($_REQUEST['action']) || !in_array($_REQUEST['action'], array('.xml', 'login2', 'logintfa')))) |
||
141 | { |
||
142 | // Don't count longer than 15 minutes. |
||
143 | if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15) |
||
144 | $_SESSION['timeOnlineUpdated'] = time(); |
||
145 | |||
146 | $user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated']; |
||
147 | 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'])); |
||
148 | |||
149 | if (!empty($cache_enable) && $cache_enable >= 2) |
||
150 | cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60); |
||
151 | |||
152 | $user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated']; |
||
153 | $_SESSION['timeOnlineUpdated'] = time(); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Logs the last database error into a file. |
||
159 | * Attempts to use the backup file first, to store the last database error |
||
160 | * and only update db_last_error.php if the first was successful. |
||
161 | */ |
||
162 | function logLastDatabaseError() |
||
163 | { |
||
164 | global $boarddir, $cachedir; |
||
165 | |||
166 | // Make a note of the last modified time in case someone does this before us |
||
167 | $last_db_error_change = @filemtime($cachedir . '/db_last_error.php'); |
||
168 | |||
169 | // save the old file before we do anything |
||
170 | $file = $cachedir . '/db_last_error.php'; |
||
171 | $dberror_backup_fail = !@is_writable($cachedir . '/db_last_error_bak.php') || !@copy($file, $cachedir . '/db_last_error_bak.php'); |
||
172 | $dberror_backup_fail = !$dberror_backup_fail ? (!file_exists($cachedir . '/db_last_error_bak.php') || filesize($cachedir . '/db_last_error_bak.php') === 0) : $dberror_backup_fail; |
||
173 | |||
174 | clearstatcache(); |
||
175 | if (filemtime($cachedir . '/db_last_error.php') === $last_db_error_change) |
||
176 | { |
||
177 | // Write the change |
||
178 | $write_db_change = '<' . '?' . "php\n" . '$db_last_error = ' . time() . ';' . "\n" . '?' . '>'; |
||
179 | $written_bytes = file_put_contents($cachedir . '/db_last_error.php', $write_db_change, LOCK_EX); |
||
180 | |||
181 | // survey says ... |
||
182 | if ($written_bytes !== strlen($write_db_change) && !$dberror_backup_fail) |
||
183 | { |
||
184 | // Oops. maybe we have no more disk space left, or some other troubles, troubles... |
||
185 | // Copy the file back and run for your life! |
||
186 | @copy($cachedir . '/db_last_error_bak.php', $cachedir . '/db_last_error.php'); |
||
187 | } |
||
188 | else |
||
189 | { |
||
190 | @touch($boarddir . '/' . 'Settings.php'); |
||
191 | return true; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | return false; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * This function shows the debug information tracked when $db_show_debug = true |
||
200 | * in Settings.php |
||
201 | */ |
||
202 | function displayDebug() |
||
203 | { |
||
204 | global $context, $scripturl, $boarddir, $sourcedir, $cachedir, $settings, $modSettings; |
||
205 | global $db_cache, $db_count, $cache_misses, $cache_count_misses, $db_show_debug, $cache_count, $cache_hits, $smcFunc, $txt, $cache_enable; |
||
206 | |||
207 | // Add to Settings.php if you want to show the debugging information. |
||
208 | if (!isset($db_show_debug) || $db_show_debug !== true || (isset($_GET['action']) && $_GET['action'] == 'viewquery')) |
||
209 | return; |
||
210 | |||
211 | if (empty($_SESSION['view_queries'])) |
||
212 | $_SESSION['view_queries'] = 0; |
||
213 | if (empty($context['debug']['language_files'])) |
||
214 | $context['debug']['language_files'] = array(); |
||
215 | if (empty($context['debug']['sheets'])) |
||
216 | $context['debug']['sheets'] = array(); |
||
217 | |||
218 | $files = get_included_files(); |
||
219 | $total_size = 0; |
||
220 | for ($i = 0, $n = count($files); $i < $n; $i++) |
||
221 | { |
||
222 | if (file_exists($files[$i])) |
||
223 | $total_size += filesize($files[$i]); |
||
224 | $files[$i] = strtr($files[$i], array($boarddir => '.', $sourcedir => '(Sources)', $cachedir => '(Cache)', $settings['actual_theme_dir'] => '(Current Theme)')); |
||
225 | } |
||
226 | |||
227 | $warnings = 0; |
||
228 | if (!empty($db_cache)) |
||
229 | { |
||
230 | foreach ($db_cache as $q => $query_data) |
||
231 | { |
||
232 | if (!empty($query_data['w'])) |
||
233 | $warnings += count($query_data['w']); |
||
234 | } |
||
235 | |||
236 | $_SESSION['debug'] = &$db_cache; |
||
237 | } |
||
238 | |||
239 | // Gotta have valid HTML ;). |
||
240 | $temp = ob_get_contents(); |
||
241 | ob_clean(); |
||
242 | |||
243 | echo preg_replace('~</body>\s*</html>~', '', $temp), ' |
||
244 | <div class="smalltext" style="text-align: left; margin: 1ex;"> |
||
245 | ', $txt['debug_browser'], $context['browser_body_id'], ' <em>(', implode('</em>, <em>', array_reverse(array_keys($context['browser'], true))), ')</em><br> |
||
246 | ', $txt['debug_templates'], count($context['debug']['templates']), ': <em>', implode('</em>, <em>', $context['debug']['templates']), '</em>.<br> |
||
247 | ', $txt['debug_subtemplates'], count($context['debug']['sub_templates']), ': <em>', implode('</em>, <em>', $context['debug']['sub_templates']), '</em>.<br> |
||
248 | ', $txt['debug_language_files'], count($context['debug']['language_files']), ': <em>', implode('</em>, <em>', $context['debug']['language_files']), '</em>.<br> |
||
249 | ', $txt['debug_stylesheets'], count($context['debug']['sheets']), ': <em>', implode('</em>, <em>', $context['debug']['sheets']), '</em>.<br> |
||
250 | ', $txt['debug_hooks'], empty($context['debug']['hooks']) ? 0 : count($context['debug']['hooks']) . ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_hooks\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_hooks" style="display: none;"><em>' . implode('</em>, <em>', $context['debug']['hooks']), '</em></span>)', '<br> |
||
251 | ', (isset($context['debug']['instances']) ? ($txt['debug_instances'] . (empty($context['debug']['instances']) ? 0 : count($context['debug']['instances'])) . ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_instances\').style.display = \'inline\'; this.style.display = \'none\'; return false;">' . $txt['debug_show'] . '</a><span id="debug_instances" style="display: none;"><em>' . implode('</em>, <em>', array_keys($context['debug']['instances'])) . '</em></span>)' . '<br>') : ''), ' |
||
252 | ', $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>'; |
||
253 | |||
254 | if (function_exists('memory_get_peak_usage')) |
||
255 | echo $txt['debug_memory_use'], ceil(memory_get_peak_usage() / 1024), $txt['debug_kb'], '<br>'; |
||
256 | |||
257 | // What tokens are active? |
||
258 | if (isset($_SESSION['token'])) |
||
259 | echo $txt['debug_tokens'] . '<em>' . implode(',</em> <em>', array_keys($_SESSION['token'])), '</em>.<br>'; |
||
260 | |||
261 | if (!empty($cache_enable) && !empty($cache_hits)) |
||
262 | { |
||
263 | $missed_entries = array(); |
||
264 | $entries = array(); |
||
265 | $total_t = 0; |
||
266 | $total_s = 0; |
||
267 | foreach ($cache_hits as $cache_hit) |
||
268 | { |
||
269 | $entries[] = $cache_hit['d'] . ' ' . $cache_hit['k'] . ': ' . sprintf($txt['debug_cache_seconds_bytes'], comma_format($cache_hit['t'], 5), $cache_hit['s']); |
||
270 | $total_t += $cache_hit['t']; |
||
271 | $total_s += $cache_hit['s']; |
||
272 | } |
||
273 | if (!isset($cache_misses)) |
||
274 | $cache_misses = array(); |
||
275 | foreach ($cache_misses as $missed) |
||
276 | $missed_entries[] = $missed['d'] . ' ' . $missed['k']; |
||
277 | |||
278 | echo ' |
||
279 | ', $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> |
||
280 | ', $txt['debug_cache_misses'], $cache_count_misses, ': (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_cache_misses_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_cache_misses_info" style="display: none;"><em>', implode('</em>, <em>', $missed_entries), '</em></span>)<br>'; |
||
281 | } |
||
282 | |||
283 | echo ' |
||
284 | <a href="', $scripturl, '?action=viewquery" target="_blank" rel="noopener">', $warnings == 0 ? sprintf($txt['debug_queries_used'], (int) $db_count) : sprintf($txt['debug_queries_used_and_warnings'], (int) $db_count, $warnings), '</a><br> |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
285 | <br>'; |
||
286 | |||
287 | if ($_SESSION['view_queries'] == 1 && !empty($db_cache)) |
||
288 | foreach ($db_cache as $q => $query_data) |
||
289 | { |
||
290 | $is_select = strpos(trim($query_data['q']), 'SELECT') === 0 || preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+SELECT .+$~s', trim($query_data['q'])) != 0 || strpos(trim($query_data['q']), 'WITH') === 0; |
||
291 | // Temporary tables created in earlier queries are not explainable. |
||
292 | if ($is_select) |
||
293 | { |
||
294 | foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp) |
||
295 | if (strpos(trim($query_data['q']), $tmp) !== false) |
||
296 | { |
||
297 | $is_select = false; |
||
298 | break; |
||
299 | } |
||
300 | } |
||
301 | // But actual creation of the temporary tables are. |
||
302 | elseif (preg_match('~^CREATE TEMPORARY TABLE .+?SELECT .+$~s', trim($query_data['q'])) != 0) |
||
303 | $is_select = true; |
||
304 | |||
305 | // Make the filenames look a bit better. |
||
306 | if (isset($query_data['f'])) |
||
307 | $query_data['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $query_data['f']); |
||
308 | |||
309 | echo ' |
||
310 | <strong>', $is_select ? '<a href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '" target="_blank" rel="noopener" style="text-decoration: none;">' : '', nl2br(str_replace("\t", ' ', $smcFunc['htmlspecialchars'](ltrim($query_data['q'], "\n\r")))) . ($is_select ? '</a></strong>' : '</strong>') . '<br> |
||
311 | '; |
||
312 | if (!empty($query_data['f']) && !empty($query_data['l'])) |
||
313 | echo sprintf($txt['debug_query_in_line'], $query_data['f'], $query_data['l']); |
||
314 | |||
315 | if (isset($query_data['s'], $query_data['t']) && isset($txt['debug_query_which_took_at'])) |
||
316 | echo sprintf($txt['debug_query_which_took_at'], round($query_data['t'], 8), round($query_data['s'], 8)) . '<br>'; |
||
317 | elseif (isset($query_data['t'])) |
||
318 | echo sprintf($txt['debug_query_which_took'], round($query_data['t'], 8)) . '<br>'; |
||
319 | echo ' |
||
320 | <br>'; |
||
321 | } |
||
322 | |||
323 | echo ' |
||
324 | <a href="' . $scripturl . '?action=viewquery;sa=hide">', $txt['debug_' . (empty($_SESSION['view_queries']) ? 'show' : 'hide') . '_queries'], '</a> |
||
325 | </div></body></html>'; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Track Statistics. |
||
330 | * Caches statistics changes, and flushes them if you pass nothing. |
||
331 | * If '+' is used as a value, it will be incremented. |
||
332 | * It does not actually commit the changes until the end of the page view. |
||
333 | * It depends on the trackStats setting. |
||
334 | * |
||
335 | * @param array $stats An array of data |
||
336 | * @return bool Whether or not the info was updated successfully |
||
337 | */ |
||
338 | function trackStats($stats = array()) |
||
339 | { |
||
340 | global $modSettings, $smcFunc; |
||
341 | static $cache_stats = array(); |
||
342 | |||
343 | if (empty($modSettings['trackStats'])) |
||
344 | return false; |
||
345 | if (!empty($stats)) |
||
346 | return $cache_stats = array_merge($cache_stats, $stats); |
||
347 | elseif (empty($cache_stats)) |
||
348 | return false; |
||
349 | |||
350 | $setStringUpdate = ''; |
||
351 | $insert_keys = array(); |
||
352 | $date = smf_strftime('%Y-%m-%d', time()); |
||
353 | $update_parameters = array( |
||
354 | 'current_date' => $date, |
||
355 | ); |
||
356 | foreach ($cache_stats as $field => $change) |
||
357 | { |
||
358 | $setStringUpdate .= ' |
||
359 | ' . $field . ' = ' . ($change === '+' ? $field . ' + 1' : '{int:' . $field . '}') . ','; |
||
360 | |||
361 | if ($change === '+') |
||
362 | $cache_stats[$field] = 1; |
||
363 | else |
||
364 | $update_parameters[$field] = $change; |
||
365 | $insert_keys[$field] = 'int'; |
||
366 | } |
||
367 | |||
368 | $smcFunc['db_query']('', ' |
||
369 | UPDATE {db_prefix}log_activity |
||
370 | SET' . substr($setStringUpdate, 0, -1) . ' |
||
371 | WHERE date = {date:current_date}', |
||
372 | $update_parameters |
||
373 | ); |
||
374 | if ($smcFunc['db_affected_rows']() == 0) |
||
375 | { |
||
376 | $smcFunc['db_insert']('ignore', |
||
377 | '{db_prefix}log_activity', |
||
378 | array_merge($insert_keys, array('date' => 'date')), |
||
379 | array_merge($cache_stats, array($date)), |
||
380 | array('date') |
||
381 | ); |
||
382 | } |
||
383 | |||
384 | // Don't do this again. |
||
385 | $cache_stats = array(); |
||
386 | |||
387 | return true; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * This function logs an action to the database. It is a |
||
392 | * thin wrapper around {@link logActions()}. |
||
393 | * |
||
394 | * @example logAction('remove', array('starter' => $id_member_started)); |
||
395 | * |
||
396 | * @param string $action A code for the report; a list of such strings |
||
397 | * can be found in Modlog.{language}.php (modlog_ac_ strings) |
||
398 | * @param array $extra An associated array of parameters for the |
||
399 | * item being logged. Typically this will include 'topic' for the topic's id. |
||
400 | * @param string $log_type A string reflecting the type of log. |
||
401 | * |
||
402 | * @return int The ID of the row containing the logged data |
||
403 | */ |
||
404 | function logAction($action, array $extra = array(), $log_type = 'moderate') |
||
405 | { |
||
406 | return logActions(array(array( |
||
407 | 'action' => $action, |
||
408 | 'log_type' => $log_type, |
||
409 | 'extra' => $extra, |
||
410 | ))); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Log changes to the forum, such as moderation events or administrative |
||
415 | * changes. This behaves just like {@link logAction()} in SMF 2.0, except |
||
416 | * that it is designed to log multiple actions at once. |
||
417 | * |
||
418 | * SMF uses three log types: |
||
419 | * |
||
420 | * - `user` for actions executed that aren't related to |
||
421 | * moderation (e.g. signature or other changes from the profile); |
||
422 | * - `moderate` for moderation actions (e.g. topic changes); |
||
423 | * - `admin` for administrative actions. |
||
424 | * |
||
425 | * @param array $logs An array of log data |
||
426 | * |
||
427 | * @return int The last logged ID |
||
428 | */ |
||
429 | function logActions(array $logs) |
||
430 | { |
||
431 | global $modSettings, $user_info, $smcFunc, $sourcedir, $txt; |
||
432 | |||
433 | $inserts = array(); |
||
434 | $log_types = array( |
||
435 | 'moderate' => 1, |
||
436 | 'user' => 2, |
||
437 | 'admin' => 3, |
||
438 | ); |
||
439 | $always_log = array('agreement_accepted', 'policy_accepted', 'agreement_updated', 'policy_updated'); |
||
440 | |||
441 | call_integration_hook('integrate_log_types', array(&$log_types, &$always_log)); |
||
442 | |||
443 | foreach ($logs as $log) |
||
444 | { |
||
445 | if (!isset($log_types[$log['log_type']]) && (empty($modSettings[$log['log_type'] . 'log_enabled']) || !in_array($log['action'], $always_log))) |
||
446 | continue; |
||
447 | |||
448 | if (!is_array($log['extra'])) |
||
449 | { |
||
450 | loadLanguage('Errors'); |
||
451 | trigger_error(sprintf($txt['logActions_not_array'], $log['action']), E_USER_NOTICE); |
||
452 | } |
||
453 | |||
454 | // Pull out the parts we want to store separately, but also make sure that the data is proper |
||
455 | if (isset($log['extra']['topic'])) |
||
456 | { |
||
457 | if (!is_numeric($log['extra']['topic'])) |
||
458 | { |
||
459 | loadLanguage('Errors'); |
||
460 | trigger_error($txt['logActions_topic_not_numeric'], E_USER_NOTICE); |
||
461 | } |
||
462 | $topic_id = empty($log['extra']['topic']) ? 0 : (int) $log['extra']['topic']; |
||
463 | unset($log['extra']['topic']); |
||
464 | } |
||
465 | else |
||
466 | $topic_id = 0; |
||
467 | |||
468 | if (isset($log['extra']['message'])) |
||
469 | { |
||
470 | if (!is_numeric($log['extra']['message'])) |
||
471 | { |
||
472 | loadLanguage('Errors'); |
||
473 | trigger_error($txt['logActions_message_not_numeric'], E_USER_NOTICE); |
||
474 | } |
||
475 | $msg_id = empty($log['extra']['message']) ? 0 : (int) $log['extra']['message']; |
||
476 | unset($log['extra']['message']); |
||
477 | } |
||
478 | else |
||
479 | $msg_id = 0; |
||
480 | |||
481 | // @todo cache this? |
||
482 | // Is there an associated report on this? |
||
483 | if (in_array($log['action'], array('move', 'remove', 'split', 'merge'))) |
||
484 | { |
||
485 | $request = $smcFunc['db_query']('', ' |
||
486 | SELECT id_report |
||
487 | FROM {db_prefix}log_reported |
||
488 | WHERE {raw:column_name} = {int:reported} |
||
489 | LIMIT 1', |
||
490 | array( |
||
491 | 'column_name' => !empty($msg_id) ? 'id_msg' : 'id_topic', |
||
492 | 'reported' => !empty($msg_id) ? $msg_id : $topic_id, |
||
493 | ) |
||
494 | ); |
||
495 | |||
496 | // Alright, if we get any result back, update open reports. |
||
497 | if ($smcFunc['db_num_rows']($request) > 0) |
||
498 | { |
||
499 | require_once($sourcedir . '/Subs-ReportedContent.php'); |
||
500 | updateSettings(array('last_mod_report_action' => time())); |
||
501 | recountOpenReports('posts'); |
||
502 | } |
||
503 | $smcFunc['db_free_result']($request); |
||
504 | } |
||
505 | |||
506 | if (isset($log['extra']['member']) && !is_numeric($log['extra']['member'])) |
||
507 | { |
||
508 | loadLanguage('Errors'); |
||
509 | trigger_error($txt['logActions_member_not_numeric'], E_USER_NOTICE); |
||
510 | } |
||
511 | |||
512 | if (isset($log['extra']['board'])) |
||
513 | { |
||
514 | if (!is_numeric($log['extra']['board'])) |
||
515 | { |
||
516 | loadLanguage('Errors'); |
||
517 | trigger_error($txt['logActions_board_not_numeric'], E_USER_NOTICE); |
||
518 | } |
||
519 | $board_id = empty($log['extra']['board']) ? 0 : (int) $log['extra']['board']; |
||
520 | unset($log['extra']['board']); |
||
521 | } |
||
522 | else |
||
523 | $board_id = 0; |
||
524 | |||
525 | if (isset($log['extra']['board_to'])) |
||
526 | { |
||
527 | if (!is_numeric($log['extra']['board_to'])) |
||
528 | { |
||
529 | loadLanguage('Errors'); |
||
530 | trigger_error($txt['logActions_board_to_not_numeric'], E_USER_NOTICE); |
||
531 | } |
||
532 | if (empty($board_id)) |
||
533 | { |
||
534 | $board_id = empty($log['extra']['board_to']) ? 0 : (int) $log['extra']['board_to']; |
||
535 | unset($log['extra']['board_to']); |
||
536 | } |
||
537 | } |
||
538 | |||
539 | if (isset($log['extra']['member_affected'])) |
||
540 | $memID = $log['extra']['member_affected']; |
||
541 | else |
||
542 | $memID = $user_info['id']; |
||
543 | |||
544 | if (isset($user_info['ip'])) |
||
545 | $memIP = $user_info['ip']; |
||
546 | else |
||
547 | $memIP = 'null'; |
||
548 | |||
549 | $inserts[] = array( |
||
550 | time(), $log_types[$log['log_type']], $memID, $memIP, $log['action'], |
||
551 | $board_id, $topic_id, $msg_id, $smcFunc['json_encode']($log['extra']), |
||
552 | ); |
||
553 | } |
||
554 | |||
555 | $id_action = $smcFunc['db_insert']('', |
||
556 | '{db_prefix}log_actions', |
||
557 | array( |
||
558 | 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'inet', 'action' => 'string', |
||
559 | 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534', |
||
560 | ), |
||
561 | $inserts, |
||
562 | array('id_action'), |
||
563 | 1 |
||
564 | ); |
||
565 | |||
566 | return $id_action; |
||
567 | } |
||
568 | |||
569 | ?> |