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