|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file concerns itself with logging, whether in the database or files. |
|
5
|
|
|
* |
|
6
|
|
|
* @package ElkArte Forum |
|
7
|
|
|
* @copyright ElkArte Forum contributors |
|
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
|
9
|
|
|
* |
|
10
|
|
|
* This file contains code covered by: |
|
11
|
|
|
* copyright: 2011 Simple Machines (http://www.simplemachines.org) |
|
12
|
|
|
* |
|
13
|
|
|
* @version 2.0 dev |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use ElkArte\Cache\Cache; |
|
18
|
|
|
use ElkArte\Helper\FileFunctions; |
|
19
|
|
|
use ElkArte\Helper\Util; |
|
20
|
|
|
use ElkArte\Request; |
|
21
|
|
|
use ElkArte\User; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Put this user in the online log. |
|
25
|
|
|
* |
|
26
|
|
|
* @param bool $force = false |
|
27
|
|
|
*/ |
|
28
|
|
|
function writeLog($force = false) |
|
29
|
|
|
{ |
|
30
|
|
|
global $context, $modSettings, $settings, $topic, $board; |
|
31
|
|
|
|
|
32
|
|
|
// 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. |
|
33
|
|
|
if (!empty($settings['display_who_viewing']) && ($topic || $board)) |
|
34
|
|
|
{ |
|
35
|
|
|
// Take the opposite approach! |
|
36
|
|
|
$force = true; |
|
37
|
|
|
|
|
38
|
|
|
// Don't update for every page - this isn't wholly accurate but who cares. |
|
39
|
|
|
if ($topic) |
|
40
|
|
|
{ |
|
41
|
|
|
if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] === $topic) |
|
42
|
|
|
{ |
|
43
|
|
|
$force = false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$_SESSION['last_topic_id'] = $topic; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Are they a spider we should be tracking? Mode = 1 gets tracked on its spider check... |
|
51
|
|
|
if (!empty(User::$info->possibly_robot) && !empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
require_once(SUBSDIR . '/SearchEngines.subs.php'); |
|
54
|
|
|
logSpider(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Don't mark them as online more than every so often. |
|
58
|
|
|
if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force) |
|
59
|
|
|
{ |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($modSettings['who_enabled'])) |
|
64
|
|
|
{ |
|
65
|
|
|
$serialized = $_GET; |
|
66
|
|
|
|
|
67
|
|
|
// In the case of a dlattach action, session_var may not be set. |
|
68
|
|
|
if (!isset($context['session_var'])) |
|
69
|
|
|
{ |
|
70
|
|
|
$context['session_var'] = $_SESSION['session_var']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
unset($serialized['sesc'], $serialized[$context['session_var']]); |
|
74
|
|
|
$serialized = serialize($serialized); |
|
75
|
|
|
} |
|
76
|
|
|
else |
|
77
|
|
|
{ |
|
78
|
|
|
$serialized = ''; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Guests use 0, members use their session ID. |
|
82
|
|
|
$session_id = User::$info->is_guest ? 'ip' . User::$info->ip : session_id(); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$cache = Cache::instance(); |
|
85
|
|
|
|
|
86
|
|
|
// Grab the last all-of-Elk-specific log_online deletion time. |
|
87
|
|
|
$do_delete = $cache->get('log_online-update', 30) < time() - 30; |
|
88
|
|
|
|
|
89
|
|
|
require_once(SUBSDIR . '/Logging.subs.php'); |
|
90
|
|
|
|
|
91
|
|
|
// If the last click wasn't a long time ago, and there was a last click... |
|
92
|
|
|
if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= time() - $modSettings['lastActive'] * 20) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($do_delete) |
|
95
|
|
|
{ |
|
96
|
|
|
deleteLogOnlineInterval($session_id); |
|
97
|
|
|
|
|
98
|
|
|
// Cache when we did it last. |
|
99
|
|
|
$cache->put('log_online-update', time(), 30); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
updateLogOnline($session_id, $serialized); |
|
103
|
|
|
} |
|
104
|
|
|
else |
|
105
|
|
|
{ |
|
106
|
|
|
$_SESSION['log_time'] = 0; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Otherwise, we have to delete and insert. |
|
110
|
|
|
if (empty($_SESSION['log_time'])) |
|
111
|
|
|
{ |
|
112
|
|
|
insertdeleteLogOnline($session_id, $serialized, $do_delete); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Mark your session as being logged. |
|
116
|
|
|
$_SESSION['log_time'] = time(); |
|
117
|
|
|
|
|
118
|
|
|
// Well, they are online now. |
|
119
|
|
|
if (empty($_SESSION['timeOnlineUpdated'])) |
|
120
|
|
|
{ |
|
121
|
|
|
$_SESSION['timeOnlineUpdated'] = time(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Set their login time, if not already done within the last minute. |
|
125
|
|
|
if (ELK !== 'SSI' && !empty(User::$info->last_login) && User::$info->last_login < time() - 60) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
// We log IPs the request came with, around here |
|
128
|
|
|
$req = Request::instance(); |
|
129
|
|
|
|
|
130
|
|
|
// Don't count longer than 15 minutes. |
|
131
|
|
|
if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15) |
|
132
|
|
|
{ |
|
133
|
|
|
$_SESSION['timeOnlineUpdated'] = time(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
User::$settings->updateTotalTimeLoggedIn($_SESSION['timeOnlineUpdated']); |
|
137
|
|
|
require_once(SUBSDIR . '/Members.subs.php'); |
|
138
|
|
|
updateMemberData(User::$info->id, ['last_login' => time(), 'member_ip' => User::$info->ip, 'member_ip2' => $req->ban_ip(), 'total_time_logged_in' => User::$settings['total_time_logged_in']]); |
|
139
|
|
|
|
|
140
|
|
|
if ($cache->levelHigherThan(1)) |
|
141
|
|
|
{ |
|
142
|
|
|
$cache->put('user_settings-' . User::$info->id, User::$settings->toArray(), 60); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
User::$info->total_time_logged_in += time() - $_SESSION['timeOnlineUpdated']; |
|
146
|
|
|
$_SESSION['timeOnlineUpdated'] = time(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Logs the last database error into a file. |
|
152
|
|
|
* |
|
153
|
|
|
* What it does: |
|
154
|
|
|
* |
|
155
|
|
|
* - Attempts to use the backup file first, to store the last database error |
|
156
|
|
|
* - only updates db_last_error.txt if the first was successful. |
|
157
|
|
|
*/ |
|
158
|
|
|
function logLastDatabaseError() |
|
159
|
|
|
{ |
|
160
|
|
|
// Make a note of the last modified time in case someone does this before us |
|
161
|
|
|
$last_db_error_change = @filemtime(BOARDDIR . '/db_last_error.txt'); |
|
162
|
|
|
|
|
163
|
|
|
$fileFunc = FileFunctions::instance(); |
|
164
|
|
|
|
|
165
|
|
|
// Save the old file before we do anything |
|
166
|
|
|
$file = BOARDDIR . '/db_last_error.txt'; |
|
167
|
|
|
$dberror_backup_fail = !$fileFunc->isWritable(BOARDDIR . '/db_last_error_bak.txt') || !@copy($file, BOARDDIR . '/db_last_error_bak.txt'); |
|
168
|
|
|
$dberror_backup_fail = $dberror_backup_fail ? ($dberror_backup_fail) : !$fileFunc->fileExists(BOARDDIR . '/db_last_error_bak.txt') || filesize(BOARDDIR . '/db_last_error_bak.txt') === 0; |
|
169
|
|
|
|
|
170
|
|
|
clearstatcache(); |
|
171
|
|
|
if (filemtime(BOARDDIR . '/db_last_error.txt') === $last_db_error_change) |
|
172
|
|
|
{ |
|
173
|
|
|
// Write the change |
|
174
|
|
|
$write_db_change = time(); |
|
175
|
|
|
$written_bytes = file_put_contents(BOARDDIR . '/db_last_error.txt', $write_db_change, LOCK_EX); |
|
176
|
|
|
|
|
177
|
|
|
// Survey says ... |
|
178
|
|
|
if ($written_bytes !== strlen($write_db_change) && !$dberror_backup_fail) |
|
179
|
|
|
{ |
|
180
|
|
|
// Oops. maybe we have no more disk space left, or some other troubles, troubles... |
|
181
|
|
|
// Copy the file back and run for your life! |
|
182
|
|
|
@copy(BOARDDIR . '/db_last_error_bak.txt', BOARDDIR . '/db_last_error.txt'); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
return false; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return true; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Track Statistics. |
|
195
|
|
|
* |
|
196
|
|
|
* What it does: |
|
197
|
|
|
* |
|
198
|
|
|
* - Caches statistics changes, and flushes them if you pass nothing. |
|
199
|
|
|
* - If '+' is used as a value, it will be incremented. |
|
200
|
|
|
* - It does not actually commit the changes until the end of the page view. |
|
201
|
|
|
* - It depends on the trackStats setting. |
|
202
|
|
|
* |
|
203
|
34 |
|
* @param array $stats = array() array of array => direction (+/-) |
|
204
|
34 |
|
* |
|
205
|
|
|
* @return bool|array |
|
206
|
34 |
|
*/ |
|
207
|
|
|
function trackStats($stats = []) |
|
208
|
|
|
{ |
|
209
|
|
|
global $modSettings; |
|
210
|
|
|
static $cache_stats = []; |
|
211
|
34 |
|
|
|
212
|
|
|
if (empty($modSettings['trackStats'])) |
|
213
|
32 |
|
{ |
|
214
|
|
|
return false; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
2 |
|
if (!empty($stats)) |
|
218
|
|
|
{ |
|
219
|
|
|
return $cache_stats = array_merge($cache_stats, $stats); |
|
220
|
1 |
|
} |
|
221
|
1 |
|
|
|
222
|
|
|
if (empty($cache_stats)) |
|
223
|
1 |
|
{ |
|
224
|
|
|
return false; |
|
225
|
1 |
|
} |
|
226
|
|
|
|
|
227
|
|
|
$setStringUpdate = []; |
|
228
|
1 |
|
$insert_keys = []; |
|
229
|
|
|
|
|
230
|
1 |
|
$date = Util::strftime('%Y-%m-%d', forum_time(false)); |
|
231
|
|
|
$update_parameters = [ |
|
232
|
1 |
|
'current_date' => $date, |
|
233
|
|
|
]; |
|
234
|
1 |
|
|
|
235
|
|
|
foreach ($cache_stats as $field => $change) |
|
236
|
|
|
{ |
|
237
|
|
|
$setStringUpdate[] = $field . ' = ' . ($change === '+' ? $field . ' + 1' : '{int:' . $field . '}'); |
|
238
|
|
|
|
|
239
|
|
|
if ($change === '+') |
|
240
|
|
|
{ |
|
241
|
1 |
|
$cache_stats[$field] = 1; |
|
242
|
|
|
} |
|
243
|
|
|
else |
|
244
|
1 |
|
{ |
|
245
|
|
|
$update_parameters[$field] = $change; |
|
246
|
1 |
|
} |
|
247
|
1 |
|
|
|
248
|
|
|
$insert_keys[$field] = 'int'; |
|
249
|
|
|
} |
|
250
|
1 |
|
|
|
251
|
|
|
$setStringUpdate = implode(',', $setStringUpdate); |
|
252
|
1 |
|
|
|
253
|
|
|
require_once(SUBSDIR . '/Logging.subs.php'); |
|
254
|
|
|
updateLogActivity($update_parameters, $setStringUpdate, $insert_keys, $cache_stats, $date); |
|
255
|
|
|
|
|
256
|
|
|
// Don't do this again. |
|
257
|
|
|
$cache_stats = []; |
|
258
|
|
|
|
|
259
|
|
|
return true; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* This function logs a single action in the respective log. (database log) |
|
264
|
|
|
* |
|
265
|
|
|
* - You should use {@link logActions()} instead if you have multiple entries to add |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $action The action to log |
|
268
|
|
|
* @param string[] $extra = array() An array of extra data |
|
269
|
|
|
* @param string $log_type options: 'moderate', 'admin', ...etc. |
|
270
|
|
|
* |
|
271
|
4 |
|
* @return int |
|
272
|
|
|
* @example logAction('remove', array('starter' => $id_member_started)); |
|
273
|
4 |
|
* |
|
274
|
4 |
|
*/ |
|
275
|
4 |
|
function logAction($action, $extra = [], $log_type = 'moderate') |
|
276
|
|
|
{ |
|
277
|
|
|
// Set up the array and pass through to logActions |
|
278
|
|
|
return logActions([ |
|
279
|
|
|
[ |
|
280
|
|
|
'action' => $action, |
|
281
|
|
|
'log_type' => $log_type, |
|
282
|
|
|
'extra' => $extra, |
|
283
|
|
|
] |
|
284
|
|
|
] |
|
285
|
|
|
); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Log changes to the forum, such as moderation events or administrative changes. |
|
290
|
|
|
* |
|
291
|
|
|
* - This behaves just like logAction() did, except that it is designed to |
|
292
|
|
|
* log multiple actions at once. |
|
293
|
|
|
* |
|
294
|
|
|
* @event integrate_log_types allows adding additional log types for integrations |
|
295
|
|
|
* @param array $logs array of actions to log [] = array(action => log_type=> extra=>) |
|
296
|
|
|
* - action => A code for the log |
|
297
|
|
|
* - extra => An associated array of parameters for the item being logged. |
|
298
|
|
|
* This will include 'topic' for the topic id or message for the message id |
|
299
|
4 |
|
* - log_type => A string reflecting the type of log, moderate for moderation actions, |
|
300
|
|
|
* admin for administrative actions, user for user |
|
301
|
4 |
|
* |
|
302
|
|
|
* @return int the last logged ID |
|
303
|
4 |
|
*/ |
|
304
|
|
|
function logActions($logs) |
|
305
|
|
|
{ |
|
306
|
|
|
global $modSettings; |
|
307
|
|
|
|
|
308
|
4 |
|
$inserts = []; |
|
309
|
|
|
$log_types = [ |
|
310
|
|
|
'moderate' => 1, |
|
311
|
4 |
|
'user' => 2, |
|
312
|
|
|
'admin' => 3, |
|
313
|
4 |
|
]; |
|
314
|
|
|
|
|
315
|
|
|
call_integration_hook('integrate_log_types', [&$log_types]); |
|
316
|
|
|
|
|
317
|
|
|
foreach ($logs as $log) |
|
318
|
|
|
{ |
|
319
|
|
|
if (!isset($log_types[$log['log_type']])) |
|
320
|
|
|
{ |
|
321
|
|
|
continue; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
// Not if the log is off |
|
325
|
|
|
if (($log['log_type'] === 'moderate' && empty($modSettings['modlog_enabled'])) |
|
326
|
|
|
|| ($log['log_type'] === 'user' && empty($modSettings['userlog_enabled']))) |
|
327
|
|
|
{ |
|
328
|
|
|
continue; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
// Do we have something to log here, after all? |
|
332
|
|
|
if (!is_array($log['extra'])) |
|
333
|
|
|
{ |
|
334
|
|
|
trigger_error("logActions(): data is not an array with action '" . $log['action'] . "'", E_USER_NOTICE); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
// Pull out the parts we want to store separately, but also make sure that the data is proper |
|
338
|
|
|
if (isset($log['extra']['topic'])) |
|
339
|
|
|
{ |
|
340
|
|
|
if (!is_numeric($log['extra']['topic'])) |
|
341
|
|
|
{ |
|
342
|
|
|
trigger_error("logActions(): data's topic is not a number", E_USER_NOTICE); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
$topic_id = empty($log['extra']['topic']) ? 0 : (int) $log['extra']['topic']; |
|
346
|
|
|
unset($log['extra']['topic']); |
|
347
|
|
|
} |
|
348
|
|
|
else |
|
349
|
|
|
{ |
|
350
|
|
|
$topic_id = 0; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
if (isset($log['extra']['message'])) |
|
354
|
|
|
{ |
|
355
|
|
|
if (!is_numeric($log['extra']['message'])) |
|
356
|
|
|
{ |
|
357
|
|
|
trigger_error("logActions(): data's message is not a number", E_USER_NOTICE); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
$msg_id = empty($log['extra']['message']) ? 0 : (int) $log['extra']['message']; |
|
361
|
|
|
unset($log['extra']['message']); |
|
362
|
|
|
} |
|
363
|
|
|
else |
|
364
|
|
|
{ |
|
365
|
|
|
$msg_id = 0; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
// Is there an associated report on this? |
|
369
|
|
|
if (in_array($log['action'], ['move', 'remove', 'split', 'merge'])) |
|
370
|
|
|
{ |
|
371
|
|
|
require_once(SUBSDIR . '/Logging.subs.php'); |
|
372
|
|
|
if (loadLogReported($msg_id, $topic_id)) |
|
373
|
|
|
{ |
|
374
|
|
|
require_once(SUBSDIR . '/Moderation.subs.php'); |
|
375
|
|
|
updateSettings(['last_mod_report_action' => time()]); |
|
376
|
|
|
recountOpenReports(true, allowedTo('admin_forum')); |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
if (isset($log['extra']['member']) && !is_numeric($log['extra']['member'])) |
|
381
|
|
|
{ |
|
382
|
|
|
trigger_error("logActions(): data's member is not a number"); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
if (isset($log['extra']['board'])) |
|
386
|
|
|
{ |
|
387
|
|
|
if (!is_numeric($log['extra']['board'])) |
|
388
|
|
|
{ |
|
389
|
|
|
trigger_error("logActions(): data's board is not a number"); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$board_id = empty($log['extra']['board']) ? 0 : (int) $log['extra']['board']; |
|
393
|
|
|
unset($log['extra']['board']); |
|
394
|
|
|
} |
|
395
|
|
|
else |
|
396
|
|
|
{ |
|
397
|
|
|
$board_id = 0; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
if (isset($log['extra']['board_to'])) |
|
401
|
|
|
{ |
|
402
|
|
|
if (!is_numeric($log['extra']['board_to'])) |
|
403
|
|
|
{ |
|
404
|
|
|
trigger_error("logActions(): data's board_to is not a number"); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
if (empty($board_id)) |
|
408
|
|
|
{ |
|
409
|
|
|
$board_id = empty($log['extra']['board_to']) ? 0 : (int) $log['extra']['board_to']; |
|
410
|
|
|
unset($log['extra']['board_to']); |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
$memID = $log['extra']['member_affected'] ?? User::$info->id; |
|
|
|
|
|
|
415
|
|
|
|
|
416
|
|
|
$inserts[] = [ |
|
417
|
|
|
time(), $log_types[$log['log_type']], $memID, User::$info->ip, $log['action'], |
|
|
|
|
|
|
418
|
|
|
$board_id, $topic_id, $msg_id, serialize($log['extra']), |
|
419
|
|
|
]; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
if (!empty($inserts)) |
|
423
|
|
|
{ |
|
424
|
|
|
require_once(SUBSDIR . '/Logging.subs.php'); |
|
425
|
|
|
|
|
426
|
|
|
return insertLogActions($inserts); |
|
|
|
|
|
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
return 0; |
|
430
|
|
|
} |
|
431
|
|
|
|