Completed
Push — release-2.1 ( 15f485...84547f )
by Colin
08:23
created
Sources/Logging.php 1 patch
Braces   +134 added lines, -96 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 3
14 14
  */
15 15
 
16
-if (!defined('SMF'))
16
+if (!defined('SMF')) {
17 17
 	die('No direct access...');
18
+}
18 19
 
19 20
 /**
20 21
  * Put this user in the online log.
@@ -33,8 +34,9 @@  discard block
 block discarded – undo
33 34
 		// Don't update for every page - this isn't wholly accurate but who cares.
34 35
 		if ($topic)
35 36
 		{
36
-			if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic)
37
-				$force = false;
37
+			if (isset($_SESSION['last_topic_id']) && $_SESSION['last_topic_id'] == $topic) {
38
+							$force = false;
39
+			}
38 40
 			$_SESSION['last_topic_id'] = $topic;
39 41
 		}
40 42
 	}
@@ -47,22 +49,24 @@  discard block
 block discarded – undo
47 49
 	}
48 50
 
49 51
 	// 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
+	if (!empty($_SESSION['log_time']) && $_SESSION['log_time'] >= (time() - 8) && !$force) {
53
+			return;
54
+	}
52 55
 
53 56
 	if (!empty($modSettings['who_enabled']))
54 57
 	{
55 58
 		$serialized = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']);
56 59
 
57 60
 		// 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'];
61
+		if (!isset($context['session_var'])) {
62
+					$context['session_var'] = $_SESSION['session_var'];
63
+		}
60 64
 
61 65
 		unset($serialized['sesc'], $serialized[$context['session_var']]);
62 66
 		$serialized = json_encode($serialized);
67
+	} else {
68
+			$serialized = '';
63 69
 	}
64
-	else
65
-		$serialized = '';
66 70
 
67 71
 	// Guests use 0, members use their session ID.
68 72
 	$session_id = $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id();
@@ -102,17 +106,18 @@  discard block
 block discarded – undo
102 106
 		);
103 107
 
104 108
 		// Guess it got deleted.
105
-		if ($smcFunc['db_affected_rows']() == 0)
109
+		if ($smcFunc['db_affected_rows']() == 0) {
110
+					$_SESSION['log_time'] = 0;
111
+		}
112
+	} else {
106 113
 			$_SESSION['log_time'] = 0;
107 114
 	}
108
-	else
109
-		$_SESSION['log_time'] = 0;
110 115
 
111 116
 	// Otherwise, we have to delete and insert.
112 117
 	if (empty($_SESSION['log_time']))
113 118
 	{
114
-		if ($do_delete || !empty($user_info['id']))
115
-			$smcFunc['db_query']('', '
119
+		if ($do_delete || !empty($user_info['id'])) {
120
+					$smcFunc['db_query']('', '
116 121
 				DELETE FROM {db_prefix}log_online
117 122
 				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 123
 				array(
@@ -120,6 +125,7 @@  discard block
 block discarded – undo
120 125
 					'log_time' => time() - $modSettings['lastActive'] * 60,
121 126
 				)
122 127
 			);
128
+		}
123 129
 
124 130
 		$smcFunc['db_insert']($do_delete ? 'ignore' : 'replace',
125 131
 			'{db_prefix}log_online',
@@ -133,21 +139,24 @@  discard block
 block discarded – undo
133 139
 	$_SESSION['log_time'] = time();
134 140
 
135 141
 	// Well, they are online now.
136
-	if (empty($_SESSION['timeOnlineUpdated']))
137
-		$_SESSION['timeOnlineUpdated'] = time();
142
+	if (empty($_SESSION['timeOnlineUpdated'])) {
143
+			$_SESSION['timeOnlineUpdated'] = time();
144
+	}
138 145
 
139 146
 	// Set their login time, if not already done within the last minute.
140 147
 	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 148
 	{
142 149
 		// Don't count longer than 15 minutes.
143
-		if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15)
144
-			$_SESSION['timeOnlineUpdated'] = time();
150
+		if (time() - $_SESSION['timeOnlineUpdated'] > 60 * 15) {
151
+					$_SESSION['timeOnlineUpdated'] = time();
152
+		}
145 153
 
146 154
 		$user_settings['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
147 155
 		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 156
 
149
-		if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
150
-			cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60);
157
+		if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) {
158
+					cache_put_data('user_settings-' . $user_info['id'], $user_settings, 60);
159
+		}
151 160
 
152 161
 		$user_info['total_time_logged_in'] += time() - $_SESSION['timeOnlineUpdated'];
153 162
 		$_SESSION['timeOnlineUpdated'] = time();
@@ -184,8 +193,7 @@  discard block
 block discarded – undo
184 193
 			// Oops. maybe we have no more disk space left, or some other troubles, troubles...
185 194
 			// Copy the file back and run for your life!
186 195
 			@copy($boarddir . '/db_last_error_bak.php', $boarddir . '/db_last_error.php');
187
-		}
188
-		else
196
+		} else
189 197
 		{
190 198
 			@touch($boarddir . '/' . 'Settings.php');
191 199
 			return true;
@@ -205,22 +213,27 @@  discard block
 block discarded – undo
205 213
 	global $db_cache, $db_count, $cache_misses, $cache_count_misses, $db_show_debug, $cache_count, $cache_hits, $smcFunc, $txt;
206 214
 
207 215
 	// 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;
216
+	if (!isset($db_show_debug) || $db_show_debug !== true || (isset($_GET['action']) && $_GET['action'] == 'viewquery')) {
217
+			return;
218
+	}
210 219
 
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();
220
+	if (empty($_SESSION['view_queries'])) {
221
+			$_SESSION['view_queries'] = 0;
222
+	}
223
+	if (empty($context['debug']['language_files'])) {
224
+			$context['debug']['language_files'] = array();
225
+	}
226
+	if (empty($context['debug']['sheets'])) {
227
+			$context['debug']['sheets'] = array();
228
+	}
217 229
 
218 230
 	$files = get_included_files();
219 231
 	$total_size = 0;
220 232
 	for ($i = 0, $n = count($files); $i < $n; $i++)
221 233
 	{
222
-		if (file_exists($files[$i]))
223
-			$total_size += filesize($files[$i]);
234
+		if (file_exists($files[$i])) {
235
+					$total_size += filesize($files[$i]);
236
+		}
224 237
 		$files[$i] = strtr($files[$i], array($boarddir => '.', $sourcedir => '(Sources)', $cachedir => '(Cache)', $settings['actual_theme_dir'] => '(Current Theme)'));
225 238
 	}
226 239
 
@@ -229,8 +242,9 @@  discard block
 block discarded – undo
229 242
 	{
230 243
 		foreach ($db_cache as $q => $qq)
231 244
 		{
232
-			if (!empty($qq['w']))
233
-				$warnings += count($qq['w']);
245
+			if (!empty($qq['w'])) {
246
+							$warnings += count($qq['w']);
247
+			}
234 248
 		}
235 249
 
236 250
 		$_SESSION['debug'] = &$db_cache;
@@ -251,12 +265,14 @@  discard block
 block discarded – undo
251 265
 	',(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 266
 	', $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 267
 
254
-	if (function_exists('memory_get_peak_usage'))
255
-		echo $txt['debug_memory_use'], ceil(memory_get_peak_usage() / 1024), $txt['debug_kb'], '<br>';
268
+	if (function_exists('memory_get_peak_usage')) {
269
+			echo $txt['debug_memory_use'], ceil(memory_get_peak_usage() / 1024), $txt['debug_kb'], '<br>';
270
+	}
256 271
 
257 272
 	// What tokens are active?
258
-	if (isset($_SESSION['token']))
259
-		echo $txt['debug_tokens'] . '<em>' . implode(',</em> <em>', array_keys($_SESSION['token'])), '</em>.<br>';
273
+	if (isset($_SESSION['token'])) {
274
+			echo $txt['debug_tokens'] . '<em>' . implode(',</em> <em>', array_keys($_SESSION['token'])), '</em>.<br>';
275
+	}
260 276
 
261 277
 	if (!empty($modSettings['cache_enable']) && !empty($cache_hits))
262 278
 	{
@@ -270,10 +286,12 @@  discard block
 block discarded – undo
270 286
 			$total_t += $cache_hit['t'];
271 287
 			$total_s += $cache_hit['s'];
272 288
 		}
273
-		if (!isset($cache_misses))
274
-			$cache_misses = array();
275
-		foreach ($cache_misses as $missed)
276
-			$missed_entries[] = $missed['d'] . ' ' . $missed['k'];
289
+		if (!isset($cache_misses)) {
290
+					$cache_misses = array();
291
+		}
292
+		foreach ($cache_misses as $missed) {
293
+					$missed_entries[] = $missed['d'] . ' ' . $missed['k'];
294
+		}
277 295
 
278 296
 		echo '
279 297
 	', $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,38 +302,44 @@  discard block
 block discarded – undo
284 302
 	<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>
285 303
 	<br>';
286 304
 
287
-	if ($_SESSION['view_queries'] == 1 && !empty($db_cache))
288
-		foreach ($db_cache as $q => $qq)
305
+	if ($_SESSION['view_queries'] == 1 && !empty($db_cache)) {
306
+			foreach ($db_cache as $q => $qq)
289 307
 		{
290 308
 			$is_select = strpos(trim($qq['q']), 'SELECT') === 0 || preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+SELECT .+$~s', trim($qq['q'])) != 0;
309
+	}
291 310
 			// Temporary tables created in earlier queries are not explainable.
292 311
 			if ($is_select)
293 312
 			{
294
-				foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
295
-					if (strpos(trim($qq['q']), $tmp) !== false)
313
+				foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp) {
314
+									if (strpos(trim($qq['q']), $tmp) !== false)
296 315
 					{
297 316
 						$is_select = false;
317
+				}
298 318
 						break;
299 319
 					}
300 320
 			}
301 321
 			// But actual creation of the temporary tables are.
302
-			elseif (preg_match('~^CREATE TEMPORARY TABLE .+?SELECT .+$~s', trim($qq['q'])) != 0)
303
-				$is_select = true;
322
+			elseif (preg_match('~^CREATE TEMPORARY TABLE .+?SELECT .+$~s', trim($qq['q'])) != 0) {
323
+							$is_select = true;
324
+			}
304 325
 
305 326
 			// Make the filenames look a bit better.
306
-			if (isset($qq['f']))
307
-				$qq['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $qq['f']);
327
+			if (isset($qq['f'])) {
328
+							$qq['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $qq['f']);
329
+			}
308 330
 
309 331
 			echo '
310 332
 	<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;', $smcFunc['htmlspecialchars'](ltrim($qq['q'], "\n\r")))) . ($is_select ? '</a></strong>' : '</strong>') . '<br>
311 333
 	&nbsp;&nbsp;&nbsp;';
312
-			if (!empty($qq['f']) && !empty($qq['l']))
313
-				echo sprintf($txt['debug_query_in_line'], $qq['f'], $qq['l']);
334
+			if (!empty($qq['f']) && !empty($qq['l'])) {
335
+							echo sprintf($txt['debug_query_in_line'], $qq['f'], $qq['l']);
336
+			}
314 337
 
315
-			if (isset($qq['s'], $qq['t']) && isset($txt['debug_query_which_took_at']))
316
-				echo sprintf($txt['debug_query_which_took_at'], round($qq['t'], 8), round($qq['s'], 8)) . '<br>';
317
-			elseif (isset($qq['t']))
318
-				echo sprintf($txt['debug_query_which_took'], round($qq['t'], 8)) . '<br>';
338
+			if (isset($qq['s'], $qq['t']) && isset($txt['debug_query_which_took_at'])) {
339
+							echo sprintf($txt['debug_query_which_took_at'], round($qq['t'], 8), round($qq['s'], 8)) . '<br>';
340
+			} elseif (isset($qq['t'])) {
341
+							echo sprintf($txt['debug_query_which_took'], round($qq['t'], 8)) . '<br>';
342
+			}
319 343
 			echo '
320 344
 	<br>';
321 345
 		}
@@ -340,12 +364,14 @@  discard block
 block discarded – undo
340 364
 	global $modSettings, $smcFunc;
341 365
 	static $cache_stats = array();
342 366
 
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;
367
+	if (empty($modSettings['trackStats'])) {
368
+			return false;
369
+	}
370
+	if (!empty($stats)) {
371
+			return $cache_stats = array_merge($cache_stats, $stats);
372
+	} elseif (empty($cache_stats)) {
373
+			return false;
374
+	}
349 375
 
350 376
 	$setStringUpdate = '';
351 377
 	$insert_keys = array();
@@ -358,10 +384,11 @@  discard block
 block discarded – undo
358 384
 		$setStringUpdate .= '
359 385
 			' . $field . ' = ' . ($change === '+' ? $field . ' + 1' : '{int:' . $field . '}') . ',';
360 386
 
361
-		if ($change === '+')
362
-			$cache_stats[$field] = 1;
363
-		else
364
-			$update_parameters[$field] = $change;
387
+		if ($change === '+') {
388
+					$cache_stats[$field] = 1;
389
+		} else {
390
+					$update_parameters[$field] = $change;
391
+		}
365 392
 		$insert_keys[$field] = 'int';
366 393
 	}
367 394
 
@@ -425,43 +452,50 @@  discard block
 block discarded – undo
425 452
 	);
426 453
 
427 454
 	// Make sure this particular log is enabled first...
428
-	if (empty($modSettings['modlog_enabled']))
429
-		unset ($log_types['moderate']);
430
-	if (empty($modSettings['userlog_enabled']))
431
-		unset ($log_types['user']);
432
-	if (empty($modSettings['adminlog_enabled']))
433
-		unset ($log_types['admin']);
455
+	if (empty($modSettings['modlog_enabled'])) {
456
+			unset ($log_types['moderate']);
457
+	}
458
+	if (empty($modSettings['userlog_enabled'])) {
459
+			unset ($log_types['user']);
460
+	}
461
+	if (empty($modSettings['adminlog_enabled'])) {
462
+			unset ($log_types['admin']);
463
+	}
434 464
 
435 465
 	call_integration_hook('integrate_log_types', array(&$log_types));
436 466
 
437 467
 	foreach ($logs as $log)
438 468
 	{
439
-		if (!isset($log_types[$log['log_type']]))
440
-			return false;
469
+		if (!isset($log_types[$log['log_type']])) {
470
+					return false;
471
+		}
441 472
 
442
-		if (!is_array($log['extra']))
443
-			trigger_error('logActions(): data is not an array with action \'' . $log['action'] . '\'', E_USER_NOTICE);
473
+		if (!is_array($log['extra'])) {
474
+					trigger_error('logActions(): data is not an array with action \'' . $log['action'] . '\'', E_USER_NOTICE);
475
+		}
444 476
 
445 477
 		// Pull out the parts we want to store separately, but also make sure that the data is proper
446 478
 		if (isset($log['extra']['topic']))
447 479
 		{
448
-			if (!is_numeric($log['extra']['topic']))
449
-				trigger_error('logActions(): data\'s topic is not a number', E_USER_NOTICE);
480
+			if (!is_numeric($log['extra']['topic'])) {
481
+							trigger_error('logActions(): data\'s topic is not a number', E_USER_NOTICE);
482
+			}
450 483
 			$topic_id = empty($log['extra']['topic']) ? 0 : (int) $log['extra']['topic'];
451 484
 			unset($log['extra']['topic']);
485
+		} else {
486
+					$topic_id = 0;
452 487
 		}
453
-		else
454
-			$topic_id = 0;
455 488
 
456 489
 		if (isset($log['extra']['message']))
457 490
 		{
458
-			if (!is_numeric($log['extra']['message']))
459
-				trigger_error('logActions(): data\'s message is not a number', E_USER_NOTICE);
491
+			if (!is_numeric($log['extra']['message'])) {
492
+							trigger_error('logActions(): data\'s message is not a number', E_USER_NOTICE);
493
+			}
460 494
 			$msg_id = empty($log['extra']['message']) ? 0 : (int) $log['extra']['message'];
461 495
 			unset($log['extra']['message']);
496
+		} else {
497
+					$msg_id = 0;
462 498
 		}
463
-		else
464
-			$msg_id = 0;
465 499
 
466 500
 		// @todo cache this?
467 501
 		// Is there an associated report on this?
@@ -488,23 +522,26 @@  discard block
 block discarded – undo
488 522
 			$smcFunc['db_free_result']($request);
489 523
 		}
490 524
 
491
-		if (isset($log['extra']['member']) && !is_numeric($log['extra']['member']))
492
-			trigger_error('logActions(): data\'s member is not a number', E_USER_NOTICE);
525
+		if (isset($log['extra']['member']) && !is_numeric($log['extra']['member'])) {
526
+					trigger_error('logActions(): data\'s member is not a number', E_USER_NOTICE);
527
+		}
493 528
 
494 529
 		if (isset($log['extra']['board']))
495 530
 		{
496
-			if (!is_numeric($log['extra']['board']))
497
-				trigger_error('logActions(): data\'s board is not a number', E_USER_NOTICE);
531
+			if (!is_numeric($log['extra']['board'])) {
532
+							trigger_error('logActions(): data\'s board is not a number', E_USER_NOTICE);
533
+			}
498 534
 			$board_id = empty($log['extra']['board']) ? 0 : (int) $log['extra']['board'];
499 535
 			unset($log['extra']['board']);
536
+		} else {
537
+					$board_id = 0;
500 538
 		}
501
-		else
502
-			$board_id = 0;
503 539
 
504 540
 		if (isset($log['extra']['board_to']))
505 541
 		{
506
-			if (!is_numeric($log['extra']['board_to']))
507
-				trigger_error('logActions(): data\'s board_to is not a number', E_USER_NOTICE);
542
+			if (!is_numeric($log['extra']['board_to'])) {
543
+							trigger_error('logActions(): data\'s board_to is not a number', E_USER_NOTICE);
544
+			}
508 545
 			if (empty($board_id))
509 546
 			{
510 547
 				$board_id = empty($log['extra']['board_to']) ? 0 : (int) $log['extra']['board_to'];
@@ -512,10 +549,11 @@  discard block
 block discarded – undo
512 549
 			}
513 550
 		}
514 551
 
515
-		if (isset($log['extra']['member_affected']))
516
-			$memID = $log['extra']['member_affected'];
517
-		else
518
-			$memID = $user_info['id'];
552
+		if (isset($log['extra']['member_affected'])) {
553
+					$memID = $log['extra']['member_affected'];
554
+		} else {
555
+					$memID = $user_info['id'];
556
+		}
519 557
 
520 558
 		$inserts[] = array(
521 559
 			time(), $log_types[$log['log_type']], $memID, $user_info['ip'], $log['action'],
Please login to merge, or discard this patch.