Completed
Push — release-2.1 ( ecae71...61d523 )
by Colin
11:30
created
Sources/Load.php 1 patch
Braces   +795 added lines, -599 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 4
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
  * Load the $modSettings array.
@@ -25,13 +26,14 @@  discard block
 block discarded – undo
25 26
 	global $cache_enable, $sourcedir, $context;
26 27
 
27 28
 	// Most database systems have not set UTF-8 as their default input charset.
28
-	if (!empty($db_character_set))
29
-		$smcFunc['db_query']('', '
29
+	if (!empty($db_character_set)) {
30
+			$smcFunc['db_query']('', '
30 31
 			SET NAMES {string:db_character_set}',
31 32
 			array(
32 33
 				'db_character_set' => $db_character_set,
33 34
 			)
34 35
 		);
36
+	}
35 37
 
36 38
 	// We need some caching support, maybe.
37 39
 	loadCacheAccelerator();
@@ -46,28 +48,36 @@  discard block
 block discarded – undo
46 48
 			)
47 49
 		);
48 50
 		$modSettings = array();
49
-		if (!$request)
50
-			display_db_error();
51
-		while ($row = $smcFunc['db_fetch_row']($request))
52
-			$modSettings[$row[0]] = $row[1];
51
+		if (!$request) {
52
+					display_db_error();
53
+		}
54
+		while ($row = $smcFunc['db_fetch_row']($request)) {
55
+					$modSettings[$row[0]] = $row[1];
56
+		}
53 57
 		$smcFunc['db_free_result']($request);
54 58
 
55 59
 		// Do a few things to protect against missing settings or settings with invalid values...
56
-		if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999)
57
-			$modSettings['defaultMaxTopics'] = 20;
58
-		if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999)
59
-			$modSettings['defaultMaxMessages'] = 15;
60
-		if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999)
61
-			$modSettings['defaultMaxMembers'] = 30;
62
-		if (empty($modSettings['defaultMaxListItems']) || $modSettings['defaultMaxListItems'] <= 0 || $modSettings['defaultMaxListItems'] > 999)
63
-			$modSettings['defaultMaxListItems'] = 15;
60
+		if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999) {
61
+					$modSettings['defaultMaxTopics'] = 20;
62
+		}
63
+		if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999) {
64
+					$modSettings['defaultMaxMessages'] = 15;
65
+		}
66
+		if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999) {
67
+					$modSettings['defaultMaxMembers'] = 30;
68
+		}
69
+		if (empty($modSettings['defaultMaxListItems']) || $modSettings['defaultMaxListItems'] <= 0 || $modSettings['defaultMaxListItems'] > 999) {
70
+					$modSettings['defaultMaxListItems'] = 15;
71
+		}
64 72
 
65 73
 		// We excpiclity do not use $smcFunc['json_decode'] here yet, as $smcFunc is not fully loaded.
66
-		if (!is_array($modSettings['attachmentUploadDir']))
67
-			$modSettings['attachmentUploadDir'] = smf_json_decode($modSettings['attachmentUploadDir'], true);
74
+		if (!is_array($modSettings['attachmentUploadDir'])) {
75
+					$modSettings['attachmentUploadDir'] = smf_json_decode($modSettings['attachmentUploadDir'], true);
76
+		}
68 77
 
69
-		if (!empty($cache_enable))
70
-			cache_put_data('modSettings', $modSettings, 90);
78
+		if (!empty($cache_enable)) {
79
+					cache_put_data('modSettings', $modSettings, 90);
80
+		}
71 81
 	}
72 82
 
73 83
 	$modSettings['cache_enable'] = $cache_enable;
@@ -87,8 +97,9 @@  discard block
 block discarded – undo
87 97
 		};
88 98
 	$fix_utf8mb4 = function($string) use ($utf8, $smcFunc)
89 99
 	{
90
-		if (!$utf8 || $smcFunc['db_mb4'])
91
-			return $string;
100
+		if (!$utf8 || $smcFunc['db_mb4']) {
101
+					return $string;
102
+		}
92 103
 
93 104
 		$i = 0;
94 105
 		$len = strlen($string);
@@ -100,18 +111,15 @@  discard block
 block discarded – undo
100 111
 			{
101 112
 				$new_string .= $string[$i];
102 113
 				$i++;
103
-			}
104
-			elseif ($ord < 224)
114
+			} elseif ($ord < 224)
105 115
 			{
106 116
 				$new_string .= $string[$i] . $string[$i + 1];
107 117
 				$i += 2;
108
-			}
109
-			elseif ($ord < 240)
118
+			} elseif ($ord < 240)
110 119
 			{
111 120
 				$new_string .= $string[$i] . $string[$i + 1] . $string[$i + 2];
112 121
 				$i += 3;
113
-			}
114
-			elseif ($ord < 248)
122
+			} elseif ($ord < 248)
115 123
 			{
116 124
 				// Magic happens.
117 125
 				$val = (ord($string[$i]) & 0x07) << 18;
@@ -155,8 +163,7 @@  discard block
 block discarded – undo
155 163
 			{
156 164
 				$result = array_search($needle, array_slice($haystack_arr, $offset));
157 165
 				return is_int($result) ? $result + $offset : false;
158
-			}
159
-			else
166
+			} else
160 167
 			{
161 168
 				$needle_arr = preg_split('~(' . $ent_list . '|.)~' . ($utf8 ? 'u' : '') . '', $ent_check($needle), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
162 169
 				$needle_size = count($needle_arr);
@@ -165,8 +172,9 @@  discard block
 block discarded – undo
165 172
 				while ((int) $result === $result)
166 173
 				{
167 174
 					$offset += $result;
168
-					if (array_slice($haystack_arr, $offset, $needle_size) === $needle_arr)
169
-						return $offset;
175
+					if (array_slice($haystack_arr, $offset, $needle_size) === $needle_arr) {
176
+											return $offset;
177
+					}
170 178
 					$result = array_search($needle_arr[0], array_slice($haystack_arr, ++$offset));
171 179
 				}
172 180
 				return false;
@@ -204,8 +212,9 @@  discard block
 block discarded – undo
204 212
 			$string = $ent_check($string);
205 213
 			preg_match('~^(' . $ent_list . '|.){' . $smcFunc['strlen'](substr($string, 0, $length)) . '}~' . ($utf8 ? 'u' : ''), $string, $matches);
206 214
 			$string = $matches[0];
207
-			while (strlen($string) > $length)
208
-				$string = preg_replace('~(?:' . $ent_list . '|.)$~' . ($utf8 ? 'u' : ''), '', $string);
215
+			while (strlen($string) > $length) {
216
+							$string = preg_replace('~(?:' . $ent_list . '|.)$~' . ($utf8 ? 'u' : ''), '', $string);
217
+			}
209 218
 			return $string;
210 219
 		},
211 220
 		'ucfirst' => $utf8 ? function($string) use (&$smcFunc)
@@ -215,8 +224,9 @@  discard block
 block discarded – undo
215 224
 		'ucwords' => $utf8 ? function($string) use (&$smcFunc)
216 225
 		{
217 226
 			$words = preg_split('~([\s\r\n\t]+)~', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
218
-			for ($i = 0, $n = count($words); $i < $n; $i += 2)
219
-				$words[$i] = $smcFunc['ucfirst']($words[$i]);
227
+			for ($i = 0, $n = count($words); $i < $n; $i += 2) {
228
+							$words[$i] = $smcFunc['ucfirst']($words[$i]);
229
+			}
220 230
 			return implode('', $words);
221 231
 		} : 'ucwords',
222 232
 		'json_decode' => 'smf_json_decode',
@@ -224,16 +234,17 @@  discard block
 block discarded – undo
224 234
 	);
225 235
 
226 236
 	// Setting the timezone is a requirement for some functions.
227
-	if (isset($modSettings['default_timezone']) && in_array($modSettings['default_timezone'], timezone_identifiers_list()))
228
-		date_default_timezone_set($modSettings['default_timezone']);
229
-	else
237
+	if (isset($modSettings['default_timezone']) && in_array($modSettings['default_timezone'], timezone_identifiers_list())) {
238
+			date_default_timezone_set($modSettings['default_timezone']);
239
+	} else
230 240
 	{
231 241
 		// Get PHP's default timezone, if set
232 242
 		$ini_tz = ini_get('date.timezone');
233
-		if (!empty($ini_tz))
234
-			$modSettings['default_timezone'] = $ini_tz;
235
-		else
236
-			$modSettings['default_timezone'] = '';
243
+		if (!empty($ini_tz)) {
244
+					$modSettings['default_timezone'] = $ini_tz;
245
+		} else {
246
+					$modSettings['default_timezone'] = '';
247
+		}
237 248
 
238 249
 		// If date.timezone is unset, invalid, or just plain weird, make a best guess
239 250
 		if (!in_array($modSettings['default_timezone'], timezone_identifiers_list()))
@@ -251,22 +262,26 @@  discard block
 block discarded – undo
251 262
 		if (($modSettings['load_average'] = cache_get_data('loadavg', 90)) == null)
252 263
 		{
253 264
 			$modSettings['load_average'] = @file_get_contents('/proc/loadavg');
254
-			if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) != 0)
255
-				$modSettings['load_average'] = (float) $matches[1];
256
-			elseif (($modSettings['load_average'] = @`uptime`) != null && preg_match('~load average[s]?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) != 0)
257
-				$modSettings['load_average'] = (float) $matches[1];
258
-			else
259
-				unset($modSettings['load_average']);
265
+			if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) != 0) {
266
+							$modSettings['load_average'] = (float) $matches[1];
267
+			} elseif (($modSettings['load_average'] = @`uptime`) != null && preg_match('~load average[s]?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) != 0) {
268
+							$modSettings['load_average'] = (float) $matches[1];
269
+			} else {
270
+							unset($modSettings['load_average']);
271
+			}
260 272
 
261
-			if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0)
262
-				cache_put_data('loadavg', $modSettings['load_average'], 90);
273
+			if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0) {
274
+							cache_put_data('loadavg', $modSettings['load_average'], 90);
275
+			}
263 276
 		}
264 277
 
265
-		if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0)
266
-			call_integration_hook('integrate_load_average', array($modSettings['load_average']));
278
+		if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0) {
279
+					call_integration_hook('integrate_load_average', array($modSettings['load_average']));
280
+		}
267 281
 
268
-		if (!empty($modSettings['loadavg_forum']) && !empty($modSettings['load_average']) && $modSettings['load_average'] >= $modSettings['loadavg_forum'])
269
-			display_loadavg_error();
282
+		if (!empty($modSettings['loadavg_forum']) && !empty($modSettings['load_average']) && $modSettings['load_average'] >= $modSettings['loadavg_forum']) {
283
+					display_loadavg_error();
284
+		}
270 285
 	}
271 286
 
272 287
 	// Is post moderation alive and well? Everywhere else assumes this has been defined, so let's make sure it is.
@@ -287,8 +302,9 @@  discard block
 block discarded – undo
287 302
 	if (defined('SMF_INTEGRATION_SETTINGS'))
288 303
 	{
289 304
 		$integration_settings = $smcFunc['json_decode'](SMF_INTEGRATION_SETTINGS, true);
290
-		foreach ($integration_settings as $hook => $function)
291
-			add_integration_function($hook, $function, '', false);
305
+		foreach ($integration_settings as $hook => $function) {
306
+					add_integration_function($hook, $function, '', false);
307
+		}
292 308
 	}
293 309
 
294 310
 	// Any files to pre include?
@@ -298,8 +314,9 @@  discard block
 block discarded – undo
298 314
 		foreach ($pre_includes as $include)
299 315
 		{
300 316
 			$include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir));
301
-			if (file_exists($include))
302
-				require_once($include);
317
+			if (file_exists($include)) {
318
+							require_once($include);
319
+			}
303 320
 		}
304 321
 	}
305 322
 
@@ -392,9 +409,9 @@  discard block
 block discarded – undo
392 409
 				break;
393 410
 			}
394 411
 		}
412
+	} else {
413
+			$id_member = 0;
395 414
 	}
396
-	else
397
-		$id_member = 0;
398 415
 
399 416
 	if (empty($id_member) && isset($_COOKIE[$cookiename]))
400 417
 	{
@@ -402,8 +419,9 @@  discard block
 block discarded – undo
402 419
 		$cookie_data = $smcFunc['json_decode']($_COOKIE[$cookiename], true, false);
403 420
 
404 421
 		// Legacy format (for recent 2.0 --> 2.1 upgrades)
405
-		if (empty($cookie_data))
406
-			$cookie_data = safe_unserialize($_COOKIE[$cookiename]);
422
+		if (empty($cookie_data)) {
423
+					$cookie_data = safe_unserialize($_COOKIE[$cookiename]);
424
+		}
407 425
 
408 426
 		list($id_member, $password, $login_span, $cookie_domain, $cookie_path) = array_pad((array) $cookie_data, 5, '');
409 427
 
@@ -411,16 +429,17 @@  discard block
 block discarded – undo
411 429
 
412 430
 		// Make sure the cookie is set to the correct domain and path
413 431
 		require_once($sourcedir . '/Subs-Auth.php');
414
-		if (array($cookie_domain, $cookie_path) !== url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])))
415
-			setLoginCookie((int) $login_span - time(), $id_member);
416
-	}
417
-	elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && ($_SESSION['USER_AGENT'] == $_SERVER['HTTP_USER_AGENT'] || !empty($modSettings['disableCheckUA'])))
432
+		if (array($cookie_domain, $cookie_path) !== url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']))) {
433
+					setLoginCookie((int) $login_span - time(), $id_member);
434
+		}
435
+	} elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && ($_SESSION['USER_AGENT'] == $_SERVER['HTTP_USER_AGENT'] || !empty($modSettings['disableCheckUA'])))
418 436
 	{
419 437
 		// @todo Perhaps we can do some more checking on this, such as on the first octet of the IP?
420 438
 		$cookie_data = $smcFunc['json_decode']($_SESSION['login_' . $cookiename], true);
421 439
 
422
-		if (empty($cookie_data))
423
-			$cookie_data = safe_unserialize($_SESSION['login_' . $cookiename]);
440
+		if (empty($cookie_data)) {
441
+					$cookie_data = safe_unserialize($_SESSION['login_' . $cookiename]);
442
+		}
424 443
 
425 444
 		list($id_member, $password, $login_span) = array_pad((array) $cookie_data, 3, '');
426 445
 		$id_member = !empty($id_member) && strlen($password) == 128 && (int) $login_span > time() ? (int) $id_member : 0;
@@ -445,30 +464,34 @@  discard block
 block discarded – undo
445 464
 			$user_settings = $smcFunc['db_fetch_assoc']($request);
446 465
 			$smcFunc['db_free_result']($request);
447 466
 
448
-			if (!empty($modSettings['force_ssl']) && $image_proxy_enabled && stripos($user_settings['avatar'], 'http://') !== false && empty($user_info['possibly_robot']))
449
-				$user_settings['avatar'] = get_proxied_url($user_settings['avatar']);
467
+			if (!empty($modSettings['force_ssl']) && $image_proxy_enabled && stripos($user_settings['avatar'], 'http://') !== false && empty($user_info['possibly_robot'])) {
468
+							$user_settings['avatar'] = get_proxied_url($user_settings['avatar']);
469
+			}
450 470
 
451
-			if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
452
-				cache_put_data('user_settings-' . $id_member, $user_settings, 60);
471
+			if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) {
472
+							cache_put_data('user_settings-' . $id_member, $user_settings, 60);
473
+			}
453 474
 		}
454 475
 
455 476
 		// Did we find 'im?  If not, junk it.
456 477
 		if (!empty($user_settings))
457 478
 		{
458 479
 			// As much as the password should be right, we can assume the integration set things up.
459
-			if (!empty($already_verified) && $already_verified === true)
460
-				$check = true;
480
+			if (!empty($already_verified) && $already_verified === true) {
481
+							$check = true;
482
+			}
461 483
 			// SHA-512 hash should be 128 characters long.
462
-			elseif (strlen($password) == 128)
463
-				$check = hash_salt($user_settings['passwd'], $user_settings['password_salt']) == $password;
464
-			else
465
-				$check = false;
484
+			elseif (strlen($password) == 128) {
485
+							$check = hash_salt($user_settings['passwd'], $user_settings['password_salt']) == $password;
486
+			} else {
487
+							$check = false;
488
+			}
466 489
 
467 490
 			// Wrong password or not activated - either way, you're going nowhere.
468 491
 			$id_member = $check && ($user_settings['is_activated'] == 1 || $user_settings['is_activated'] == 11) ? (int) $user_settings['id_member'] : 0;
492
+		} else {
493
+					$id_member = 0;
469 494
 		}
470
-		else
471
-			$id_member = 0;
472 495
 
473 496
 		// If we no longer have the member maybe they're being all hackey, stop brute force!
474 497
 		if (!$id_member)
@@ -497,8 +520,9 @@  discard block
 block discarded – undo
497 520
 
498 521
 					list ($tfamember, $tfasecret) = array_pad((array) $tfa_data, 2, '');
499 522
 
500
-					if (!isset($tfamember, $tfasecret) || (int) $tfamember != $id_member)
501
-						$tfasecret = null;
523
+					if (!isset($tfamember, $tfasecret) || (int) $tfamember != $id_member) {
524
+											$tfasecret = null;
525
+					}
502 526
 				}
503 527
 
504 528
 				// They didn't finish logging in before coming here? Then they're no one to us.
@@ -520,10 +544,12 @@  discard block
 block discarded – undo
520 544
 		// Are we forcing 2FA? Need to check if the user groups actually require 2FA
521 545
 		elseif (!empty($modSettings['tfa_mode']) && $modSettings['tfa_mode'] >= 2 && $id_member && empty($user_settings['tfa_secret']))
522 546
 		{
523
-			if ($modSettings['tfa_mode'] == 2) //only do this if we are just forcing SOME membergroups
547
+			if ($modSettings['tfa_mode'] == 2) {
548
+				//only do this if we are just forcing SOME membergroups
524 549
 			{
525 550
 				//Build an array of ALL user membergroups.
526 551
 				$full_groups = array($user_settings['id_group']);
552
+			}
527 553
 				if (!empty($user_settings['additional_groups']))
528 554
 				{
529 555
 					$full_groups = array_merge($full_groups, explode(',', $user_settings['additional_groups']));
@@ -543,15 +569,17 @@  discard block
 block discarded – undo
543 569
 				);
544 570
 				$row = $smcFunc['db_fetch_assoc']($request);
545 571
 				$smcFunc['db_free_result']($request);
572
+			} else {
573
+							$row['total'] = 1;
546 574
 			}
547
-			else
548
-				$row['total'] = 1; //simplifies logics in the next "if"
575
+			//simplifies logics in the next "if"
549 576
 
550 577
 			$area = !empty($_REQUEST['area']) ? $_REQUEST['area'] : '';
551 578
 			$action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
552 579
 
553
-			if ($row['total'] > 0 && !in_array($action, array('profile', 'logout')) || ($action == 'profile' && $area != 'tfasetup'))
554
-				redirectexit('action=profile;area=tfasetup;forced');
580
+			if ($row['total'] > 0 && !in_array($action, array('profile', 'logout')) || ($action == 'profile' && $area != 'tfasetup')) {
581
+							redirectexit('action=profile;area=tfasetup;forced');
582
+			}
555 583
 		}
556 584
 	}
557 585
 
@@ -588,33 +616,37 @@  discard block
 block discarded – undo
588 616
 				updateMemberData($id_member, array('id_msg_last_visit' => (int) $modSettings['maxMsgID'], 'last_login' => time(), 'member_ip' => $_SERVER['REMOTE_ADDR'], 'member_ip2' => $_SERVER['BAN_CHECK_IP']));
589 617
 				$user_settings['last_login'] = time();
590 618
 
591
-				if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
592
-					cache_put_data('user_settings-' . $id_member, $user_settings, 60);
619
+				if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) {
620
+									cache_put_data('user_settings-' . $id_member, $user_settings, 60);
621
+				}
593 622
 
594
-				if (!empty($modSettings['cache_enable']))
595
-					cache_put_data('user_last_visit-' . $id_member, $_SESSION['id_msg_last_visit'], 5 * 3600);
623
+				if (!empty($modSettings['cache_enable'])) {
624
+									cache_put_data('user_last_visit-' . $id_member, $_SESSION['id_msg_last_visit'], 5 * 3600);
625
+				}
596 626
 			}
627
+		} elseif (empty($_SESSION['id_msg_last_visit'])) {
628
+					$_SESSION['id_msg_last_visit'] = $user_settings['id_msg_last_visit'];
597 629
 		}
598
-		elseif (empty($_SESSION['id_msg_last_visit']))
599
-			$_SESSION['id_msg_last_visit'] = $user_settings['id_msg_last_visit'];
600 630
 
601 631
 		$username = $user_settings['member_name'];
602 632
 
603
-		if (empty($user_settings['additional_groups']))
604
-			$user_info = array(
633
+		if (empty($user_settings['additional_groups'])) {
634
+					$user_info = array(
605 635
 				'groups' => array($user_settings['id_group'], $user_settings['id_post_group'])
606 636
 			);
607
-		else
608
-			$user_info = array(
637
+		} else {
638
+					$user_info = array(
609 639
 				'groups' => array_merge(
610 640
 					array($user_settings['id_group'], $user_settings['id_post_group']),
611 641
 					explode(',', $user_settings['additional_groups'])
612 642
 				)
613 643
 			);
644
+		}
614 645
 
615 646
 		// Because history has proven that it is possible for groups to go bad - clean up in case.
616
-		foreach ($user_info['groups'] as $k => $v)
617
-			$user_info['groups'][$k] = (int) $v;
647
+		foreach ($user_info['groups'] as $k => $v) {
648
+					$user_info['groups'][$k] = (int) $v;
649
+		}
618 650
 
619 651
 		// This is a logged in user, so definitely not a spider.
620 652
 		$user_info['possibly_robot'] = false;
@@ -628,8 +660,7 @@  discard block
 block discarded – undo
628 660
 			$time_system = new DateTime('now', $tz_system);
629 661
 			$time_user = new DateTime('now', $tz_user);
630 662
 			$user_info['time_offset'] = ($tz_user->getOffset($time_user) - $tz_system->getOffset($time_system)) / 3600;
631
-		}
632
-		else
663
+		} else
633 664
 		{
634 665
 			// !!! Compatibility.
635 666
 			$user_info['time_offset'] = empty($user_settings['time_offset']) ? 0 : $user_settings['time_offset'];
@@ -643,8 +674,9 @@  discard block
 block discarded – undo
643 674
 		$user_info = array('groups' => array(-1));
644 675
 		$user_settings = array();
645 676
 
646
-		if (isset($_COOKIE[$cookiename]) && empty($context['tfa_member']))
647
-			$_COOKIE[$cookiename] = '';
677
+		if (isset($_COOKIE[$cookiename]) && empty($context['tfa_member'])) {
678
+					$_COOKIE[$cookiename] = '';
679
+		}
648 680
 
649 681
 		// Expire the 2FA cookie
650 682
 		if (isset($_COOKIE[$cookiename . '_tfa']) && empty($context['tfa_member']))
@@ -661,19 +693,20 @@  discard block
 block discarded – undo
661 693
 		}
662 694
 
663 695
 		// Create a login token if it doesn't exist yet.
664
-		if (!isset($_SESSION['token']['post-login']))
665
-			createToken('login');
666
-		else
667
-			list ($context['login_token_var'],,, $context['login_token']) = $_SESSION['token']['post-login'];
696
+		if (!isset($_SESSION['token']['post-login'])) {
697
+					createToken('login');
698
+		} else {
699
+					list ($context['login_token_var'],,, $context['login_token']) = $_SESSION['token']['post-login'];
700
+		}
668 701
 
669 702
 		// Do we perhaps think this is a search robot? Check every five minutes just in case...
670 703
 		if ((!empty($modSettings['spider_mode']) || !empty($modSettings['spider_group'])) && (!isset($_SESSION['robot_check']) || $_SESSION['robot_check'] < time() - 300))
671 704
 		{
672 705
 			require_once($sourcedir . '/ManageSearchEngines.php');
673 706
 			$user_info['possibly_robot'] = SpiderCheck();
707
+		} elseif (!empty($modSettings['spider_mode'])) {
708
+					$user_info['possibly_robot'] = isset($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
674 709
 		}
675
-		elseif (!empty($modSettings['spider_mode']))
676
-			$user_info['possibly_robot'] = isset($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
677 710
 		// If we haven't turned on proper spider hunts then have a guess!
678 711
 		else
679 712
 		{
@@ -721,8 +754,9 @@  discard block
 block discarded – undo
721 754
 	$user_info['groups'] = array_unique($user_info['groups']);
722 755
 
723 756
 	// Make sure that the last item in the ignore boards array is valid. If the list was too long it could have an ending comma that could cause problems.
724
-	if (!empty($user_info['ignoreboards']) && empty($user_info['ignoreboards'][$tmp = count($user_info['ignoreboards']) - 1]))
725
-		unset($user_info['ignoreboards'][$tmp]);
757
+	if (!empty($user_info['ignoreboards']) && empty($user_info['ignoreboards'][$tmp = count($user_info['ignoreboards']) - 1])) {
758
+			unset($user_info['ignoreboards'][$tmp]);
759
+	}
726 760
 
727 761
 	// Allow the user to change their language.
728 762
 	if (!empty($modSettings['userLanguage']))
@@ -735,13 +769,14 @@  discard block
 block discarded – undo
735 769
 			$user_info['language'] = strtr($_GET['language'], './\\:', '____');
736 770
 
737 771
 			// Make it permanent for members.
738
-			if (!empty($user_info['id']))
739
-				updateMemberData($user_info['id'], array('lngfile' => $user_info['language']));
740
-			else
741
-				$_SESSION['language'] = $user_info['language'];
772
+			if (!empty($user_info['id'])) {
773
+							updateMemberData($user_info['id'], array('lngfile' => $user_info['language']));
774
+			} else {
775
+							$_SESSION['language'] = $user_info['language'];
776
+			}
777
+		} elseif (!empty($_SESSION['language']) && isset($languages[strtr($_SESSION['language'], './\\:', '____')])) {
778
+					$user_info['language'] = strtr($_SESSION['language'], './\\:', '____');
742 779
 		}
743
-		elseif (!empty($_SESSION['language']) && isset($languages[strtr($_SESSION['language'], './\\:', '____')]))
744
-			$user_info['language'] = strtr($_SESSION['language'], './\\:', '____');
745 780
 	}
746 781
 
747 782
 	$temp = build_query_board($user_info['id']);
@@ -804,9 +839,9 @@  discard block
 block discarded – undo
804 839
 		}
805 840
 
806 841
 		// Remember redirection is the key to avoiding fallout from your bosses.
807
-		if (!empty($topic))
808
-			redirectexit('topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg']);
809
-		else
842
+		if (!empty($topic)) {
843
+					redirectexit('topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg']);
844
+		} else
810 845
 		{
811 846
 			loadPermissions();
812 847
 			loadTheme();
@@ -824,10 +859,11 @@  discard block
 block discarded – undo
824 859
 	if (!empty($modSettings['cache_enable']) && (empty($topic) || $modSettings['cache_enable'] >= 3))
825 860
 	{
826 861
 		// @todo SLOW?
827
-		if (!empty($topic))
828
-			$temp = cache_get_data('topic_board-' . $topic, 120);
829
-		else
830
-			$temp = cache_get_data('board-' . $board, 120);
862
+		if (!empty($topic)) {
863
+					$temp = cache_get_data('topic_board-' . $topic, 120);
864
+		} else {
865
+					$temp = cache_get_data('board-' . $board, 120);
866
+		}
831 867
 
832 868
 		if (!empty($temp))
833 869
 		{
@@ -865,8 +901,9 @@  discard block
 block discarded – undo
865 901
 			$row = $smcFunc['db_fetch_assoc']($request);
866 902
 
867 903
 			// Set the current board.
868
-			if (!empty($row['id_board']))
869
-				$board = $row['id_board'];
904
+			if (!empty($row['id_board'])) {
905
+							$board = $row['id_board'];
906
+			}
870 907
 
871 908
 			// Basic operating information. (globals... :/)
872 909
 			$board_info = array(
@@ -902,21 +939,23 @@  discard block
 block discarded – undo
902 939
 
903 940
 			do
904 941
 			{
905
-				if (!empty($row['id_moderator']))
906
-					$board_info['moderators'][$row['id_moderator']] = array(
942
+				if (!empty($row['id_moderator'])) {
943
+									$board_info['moderators'][$row['id_moderator']] = array(
907 944
 						'id' => $row['id_moderator'],
908 945
 						'name' => $row['real_name'],
909 946
 						'href' => $scripturl . '?action=profile;u=' . $row['id_moderator'],
910 947
 						'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_moderator'] . '">' . $row['real_name'] . '</a>'
911 948
 					);
949
+				}
912 950
 
913
-				if (!empty($row['id_moderator_group']))
914
-					$board_info['moderator_groups'][$row['id_moderator_group']] = array(
951
+				if (!empty($row['id_moderator_group'])) {
952
+									$board_info['moderator_groups'][$row['id_moderator_group']] = array(
915 953
 						'id' => $row['id_moderator_group'],
916 954
 						'name' => $row['group_name'],
917 955
 						'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'],
918 956
 						'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'] . '">' . $row['group_name'] . '</a>'
919 957
 					);
958
+				}
920 959
 			}
921 960
 			while ($row = $smcFunc['db_fetch_assoc']($request));
922 961
 
@@ -948,12 +987,12 @@  discard block
 block discarded – undo
948 987
 			if (!empty($modSettings['cache_enable']) && (empty($topic) || $modSettings['cache_enable'] >= 3))
949 988
 			{
950 989
 				// @todo SLOW?
951
-				if (!empty($topic))
952
-					cache_put_data('topic_board-' . $topic, $board_info, 120);
990
+				if (!empty($topic)) {
991
+									cache_put_data('topic_board-' . $topic, $board_info, 120);
992
+				}
953 993
 				cache_put_data('board-' . $board, $board_info, 120);
954 994
 			}
955
-		}
956
-		else
995
+		} else
957 996
 		{
958 997
 			// Otherwise the topic is invalid, there are no moderators, etc.
959 998
 			$board_info = array(
@@ -967,8 +1006,9 @@  discard block
 block discarded – undo
967 1006
 		$smcFunc['db_free_result']($request);
968 1007
 	}
969 1008
 
970
-	if (!empty($topic))
971
-		$_GET['board'] = (int) $board;
1009
+	if (!empty($topic)) {
1010
+			$_GET['board'] = (int) $board;
1011
+	}
972 1012
 
973 1013
 	if (!empty($board))
974 1014
 	{
@@ -978,10 +1018,12 @@  discard block
 block discarded – undo
978 1018
 		// Now check if the user is a moderator.
979 1019
 		$user_info['is_mod'] = isset($board_info['moderators'][$user_info['id']]) || count(array_intersect($user_info['groups'], $moderator_groups)) != 0;
980 1020
 
981
-		if (count(array_intersect($user_info['groups'], $board_info['groups'])) == 0 && !$user_info['is_admin'])
982
-			$board_info['error'] = 'access';
983
-		if (!empty($modSettings['deny_boards_access']) && count(array_intersect($user_info['groups'], $board_info['deny_groups'])) != 0 && !$user_info['is_admin'])
984
-			$board_info['error'] = 'access';
1021
+		if (count(array_intersect($user_info['groups'], $board_info['groups'])) == 0 && !$user_info['is_admin']) {
1022
+					$board_info['error'] = 'access';
1023
+		}
1024
+		if (!empty($modSettings['deny_boards_access']) && count(array_intersect($user_info['groups'], $board_info['deny_groups'])) != 0 && !$user_info['is_admin']) {
1025
+					$board_info['error'] = 'access';
1026
+		}
985 1027
 
986 1028
 		// Build up the linktree.
987 1029
 		$context['linktree'] = array_merge(
@@ -1004,8 +1046,9 @@  discard block
 block discarded – undo
1004 1046
 	$context['current_board'] = $board;
1005 1047
 
1006 1048
 	// No posting in redirection boards!
1007
-	if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'post' && !empty($board_info['redirect']))
1008
-		$board_info['error'] == 'post_in_redirect';
1049
+	if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'post' && !empty($board_info['redirect'])) {
1050
+			$board_info['error'] == 'post_in_redirect';
1051
+	}
1009 1052
 
1010 1053
 	// Hacker... you can't see this topic, I'll tell you that. (but moderators can!)
1011 1054
 	if (!empty($board_info['error']) && (!empty($modSettings['deny_boards_access']) || $board_info['error'] != 'access' || !$user_info['is_mod']))
@@ -1031,24 +1074,23 @@  discard block
 block discarded – undo
1031 1074
 			ob_end_clean();
1032 1075
 			header('HTTP/1.1 403 Forbidden');
1033 1076
 			die;
1034
-		}
1035
-		elseif ($board_info['error'] == 'post_in_redirect')
1077
+		} elseif ($board_info['error'] == 'post_in_redirect')
1036 1078
 		{
1037 1079
 			// Slightly different error message here...
1038 1080
 			fatal_lang_error('cannot_post_redirect', false);
1039
-		}
1040
-		elseif ($user_info['is_guest'])
1081
+		} elseif ($user_info['is_guest'])
1041 1082
 		{
1042 1083
 			loadLanguage('Errors');
1043 1084
 			is_not_guest($txt['topic_gone']);
1085
+		} else {
1086
+					fatal_lang_error('topic_gone', false);
1044 1087
 		}
1045
-		else
1046
-			fatal_lang_error('topic_gone', false);
1047 1088
 	}
1048 1089
 
1049
-	if ($user_info['is_mod'])
1050
-		$user_info['groups'][] = 3;
1051
-}
1090
+	if ($user_info['is_mod']) {
1091
+			$user_info['groups'][] = 3;
1092
+	}
1093
+	}
1052 1094
 
1053 1095
 /**
1054 1096
  * Load this user's permissions.
@@ -1069,8 +1111,9 @@  discard block
 block discarded – undo
1069 1111
 		asort($cache_groups);
1070 1112
 		$cache_groups = implode(',', $cache_groups);
1071 1113
 		// If it's a spider then cache it different.
1072
-		if ($user_info['possibly_robot'])
1073
-			$cache_groups .= '-spider';
1114
+		if ($user_info['possibly_robot']) {
1115
+					$cache_groups .= '-spider';
1116
+		}
1074 1117
 
1075 1118
 		if ($modSettings['cache_enable'] >= 2 && !empty($board) && ($temp = cache_get_data('permissions:' . $cache_groups . ':' . $board, 240)) != null && time() - 240 > $modSettings['settings_updated'])
1076 1119
 		{
@@ -1078,9 +1121,9 @@  discard block
 block discarded – undo
1078 1121
 			banPermissions();
1079 1122
 
1080 1123
 			return;
1124
+		} elseif (($temp = cache_get_data('permissions:' . $cache_groups, 240)) != null && time() - 240 > $modSettings['settings_updated']) {
1125
+					list ($user_info['permissions'], $removals) = $temp;
1081 1126
 		}
1082
-		elseif (($temp = cache_get_data('permissions:' . $cache_groups, 240)) != null && time() - 240 > $modSettings['settings_updated'])
1083
-			list ($user_info['permissions'], $removals) = $temp;
1084 1127
 	}
1085 1128
 
1086 1129
 	// If it is detected as a robot, and we are restricting permissions as a special group - then implement this.
@@ -1102,23 +1145,26 @@  discard block
 block discarded – undo
1102 1145
 		$removals = array();
1103 1146
 		while ($row = $smcFunc['db_fetch_assoc']($request))
1104 1147
 		{
1105
-			if (empty($row['add_deny']))
1106
-				$removals[] = $row['permission'];
1107
-			else
1108
-				$user_info['permissions'][] = $row['permission'];
1148
+			if (empty($row['add_deny'])) {
1149
+							$removals[] = $row['permission'];
1150
+			} else {
1151
+							$user_info['permissions'][] = $row['permission'];
1152
+			}
1109 1153
 		}
1110 1154
 		$smcFunc['db_free_result']($request);
1111 1155
 
1112
-		if (isset($cache_groups))
1113
-			cache_put_data('permissions:' . $cache_groups, array($user_info['permissions'], $removals), 240);
1156
+		if (isset($cache_groups)) {
1157
+					cache_put_data('permissions:' . $cache_groups, array($user_info['permissions'], $removals), 240);
1158
+		}
1114 1159
 	}
1115 1160
 
1116 1161
 	// Get the board permissions.
1117 1162
 	if (!empty($board))
1118 1163
 	{
1119 1164
 		// Make sure the board (if any) has been loaded by loadBoard().
1120
-		if (!isset($board_info['profile']))
1121
-			fatal_lang_error('no_board');
1165
+		if (!isset($board_info['profile'])) {
1166
+					fatal_lang_error('no_board');
1167
+		}
1122 1168
 
1123 1169
 		$request = $smcFunc['db_query']('', '
1124 1170
 			SELECT permission, add_deny
@@ -1134,20 +1180,23 @@  discard block
 block discarded – undo
1134 1180
 		);
1135 1181
 		while ($row = $smcFunc['db_fetch_assoc']($request))
1136 1182
 		{
1137
-			if (empty($row['add_deny']))
1138
-				$removals[] = $row['permission'];
1139
-			else
1140
-				$user_info['permissions'][] = $row['permission'];
1183
+			if (empty($row['add_deny'])) {
1184
+							$removals[] = $row['permission'];
1185
+			} else {
1186
+							$user_info['permissions'][] = $row['permission'];
1187
+			}
1141 1188
 		}
1142 1189
 		$smcFunc['db_free_result']($request);
1143 1190
 	}
1144 1191
 
1145 1192
 	// Remove all the permissions they shouldn't have ;).
1146
-	if (!empty($modSettings['permission_enable_deny']))
1147
-		$user_info['permissions'] = array_diff($user_info['permissions'], $removals);
1193
+	if (!empty($modSettings['permission_enable_deny'])) {
1194
+			$user_info['permissions'] = array_diff($user_info['permissions'], $removals);
1195
+	}
1148 1196
 
1149
-	if (isset($cache_groups) && !empty($board) && $modSettings['cache_enable'] >= 2)
1150
-		cache_put_data('permissions:' . $cache_groups . ':' . $board, array($user_info['permissions'], null), 240);
1197
+	if (isset($cache_groups) && !empty($board) && $modSettings['cache_enable'] >= 2) {
1198
+			cache_put_data('permissions:' . $cache_groups . ':' . $board, array($user_info['permissions'], null), 240);
1199
+	}
1151 1200
 
1152 1201
 	// Banned?  Watch, don't touch..
1153 1202
 	banPermissions();
@@ -1159,17 +1208,18 @@  discard block
 block discarded – undo
1159 1208
 		{
1160 1209
 			require_once($sourcedir . '/Subs-Auth.php');
1161 1210
 			rebuildModCache();
1211
+		} else {
1212
+					$user_info['mod_cache'] = $_SESSION['mc'];
1162 1213
 		}
1163
-		else
1164
-			$user_info['mod_cache'] = $_SESSION['mc'];
1165 1214
 
1166 1215
 		// This is a useful phantom permission added to the current user, and only the current user while they are logged in.
1167 1216
 		// For example this drastically simplifies certain changes to the profile area.
1168 1217
 		$user_info['permissions'][] = 'is_not_guest';
1169 1218
 		// And now some backwards compatibility stuff for mods and whatnot that aren't expecting the new permissions.
1170 1219
 		$user_info['permissions'][] = 'profile_view_own';
1171
-		if (in_array('profile_view', $user_info['permissions']))
1172
-			$user_info['permissions'][] = 'profile_view_any';
1220
+		if (in_array('profile_view', $user_info['permissions'])) {
1221
+					$user_info['permissions'][] = 'profile_view_any';
1222
+		}
1173 1223
 	}
1174 1224
 }
1175 1225
 
@@ -1187,8 +1237,9 @@  discard block
 block discarded – undo
1187 1237
 	global $image_proxy_enabled, $boardurl, $user_info;
1188 1238
 
1189 1239
 	// Can't just look for no users :P.
1190
-	if (empty($users))
1191
-		return array();
1240
+	if (empty($users)) {
1241
+			return array();
1242
+	}
1192 1243
 
1193 1244
 	// Pass the set value
1194 1245
 	$context['loadMemberContext_set'] = $set;
@@ -1203,8 +1254,9 @@  discard block
 block discarded – undo
1203 1254
 		for ($i = 0, $n = count($users); $i < $n; $i++)
1204 1255
 		{
1205 1256
 			$data = cache_get_data('member_data-' . $set . '-' . $users[$i], 240);
1206
-			if ($data == null)
1207
-				continue;
1257
+			if ($data == null) {
1258
+							continue;
1259
+			}
1208 1260
 
1209 1261
 			$loaded_ids[] = $data['id_member'];
1210 1262
 			$user_profile[$data['id_member']] = $data;
@@ -1271,16 +1323,19 @@  discard block
 block discarded – undo
1271 1323
 			$row['avatar_original'] = !empty($row['avatar']) ? $row['avatar'] : '';
1272 1324
 
1273 1325
 			// Take care of proxying avatar if required, do this here for maximum reach
1274
-			if ($image_proxy_enabled && !empty($row['avatar']) && stripos($row['avatar'], 'http://') !== false && empty($user_info['possibly_robot']))
1275
-				$row['avatar'] = get_proxied_url($row['avatar']);
1326
+			if ($image_proxy_enabled && !empty($row['avatar']) && stripos($row['avatar'], 'http://') !== false && empty($user_info['possibly_robot'])) {
1327
+							$row['avatar'] = get_proxied_url($row['avatar']);
1328
+			}
1276 1329
 
1277 1330
 			// Keep track of the member's normal member group
1278 1331
 			$row['primary_group'] = $row['member_group'];
1279 1332
 
1280
-			if (isset($row['member_ip']))
1281
-				$row['member_ip'] = inet_dtop($row['member_ip']);
1282
-			if (isset($row['member_ip2']))
1283
-				$row['member_ip2'] = inet_dtop($row['member_ip2']);
1333
+			if (isset($row['member_ip'])) {
1334
+							$row['member_ip'] = inet_dtop($row['member_ip']);
1335
+			}
1336
+			if (isset($row['member_ip2'])) {
1337
+							$row['member_ip2'] = inet_dtop($row['member_ip2']);
1338
+			}
1284 1339
 			$new_loaded_ids[] = $row['id_member'];
1285 1340
 			$loaded_ids[] = $row['id_member'];
1286 1341
 			$row['options'] = array();
@@ -1299,8 +1354,9 @@  discard block
 block discarded – undo
1299 1354
 				'loaded_ids' => $new_loaded_ids,
1300 1355
 			)
1301 1356
 		);
1302
-		while ($row = $smcFunc['db_fetch_assoc']($request))
1303
-			$user_profile[$row['id_member']]['options'][$row['variable']] = $row['value'];
1357
+		while ($row = $smcFunc['db_fetch_assoc']($request)) {
1358
+					$user_profile[$row['id_member']]['options'][$row['variable']] = $row['value'];
1359
+		}
1304 1360
 		$smcFunc['db_free_result']($request);
1305 1361
 	}
1306 1362
 
@@ -1311,10 +1367,11 @@  discard block
 block discarded – undo
1311 1367
 	{
1312 1368
 		foreach ($loaded_ids as $a_member)
1313 1369
 		{
1314
-			if (!empty($user_profile[$a_member]['additional_groups']))
1315
-				$groups = array_merge(array($user_profile[$a_member]['id_group']), explode(',', $user_profile[$a_member]['additional_groups']));
1316
-			else
1317
-				$groups = array($user_profile[$a_member]['id_group']);
1370
+			if (!empty($user_profile[$a_member]['additional_groups'])) {
1371
+							$groups = array_merge(array($user_profile[$a_member]['id_group']), explode(',', $user_profile[$a_member]['additional_groups']));
1372
+			} else {
1373
+							$groups = array($user_profile[$a_member]['id_group']);
1374
+			}
1318 1375
 
1319 1376
 			$temp = array_intersect($groups, array_keys($board_info['moderator_groups']));
1320 1377
 
@@ -1327,8 +1384,9 @@  discard block
 block discarded – undo
1327 1384
 
1328 1385
 	if (!empty($new_loaded_ids) && !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 3)
1329 1386
 	{
1330
-		for ($i = 0, $n = count($new_loaded_ids); $i < $n; $i++)
1331
-			cache_put_data('member_data-' . $set . '-' . $new_loaded_ids[$i], $user_profile[$new_loaded_ids[$i]], 240);
1387
+		for ($i = 0, $n = count($new_loaded_ids); $i < $n; $i++) {
1388
+					cache_put_data('member_data-' . $set . '-' . $new_loaded_ids[$i], $user_profile[$new_loaded_ids[$i]], 240);
1389
+		}
1332 1390
 	}
1333 1391
 
1334 1392
 	// Are we loading any moderators?  If so, fix their group data...
@@ -1354,14 +1412,17 @@  discard block
 block discarded – undo
1354 1412
 		foreach ($temp_mods as $id)
1355 1413
 		{
1356 1414
 			// By popular demand, don't show admins or global moderators as moderators.
1357
-			if ($user_profile[$id]['id_group'] != 1 && $user_profile[$id]['id_group'] != 2)
1358
-				$user_profile[$id]['member_group'] = $row['member_group'];
1415
+			if ($user_profile[$id]['id_group'] != 1 && $user_profile[$id]['id_group'] != 2) {
1416
+							$user_profile[$id]['member_group'] = $row['member_group'];
1417
+			}
1359 1418
 
1360 1419
 			// If the Moderator group has no color or icons, but their group does... don't overwrite.
1361
-			if (!empty($row['icons']))
1362
-				$user_profile[$id]['icons'] = $row['icons'];
1363
-			if (!empty($row['member_group_color']))
1364
-				$user_profile[$id]['member_group_color'] = $row['member_group_color'];
1420
+			if (!empty($row['icons'])) {
1421
+							$user_profile[$id]['icons'] = $row['icons'];
1422
+			}
1423
+			if (!empty($row['member_group_color'])) {
1424
+							$user_profile[$id]['member_group_color'] = $row['member_group_color'];
1425
+			}
1365 1426
 		}
1366 1427
 	}
1367 1428
 
@@ -1383,12 +1444,14 @@  discard block
 block discarded – undo
1383 1444
 	static $loadedLanguages = array();
1384 1445
 
1385 1446
 	// If this person's data is already loaded, skip it.
1386
-	if (isset($dataLoaded[$user]))
1387
-		return true;
1447
+	if (isset($dataLoaded[$user])) {
1448
+			return true;
1449
+	}
1388 1450
 
1389 1451
 	// We can't load guests or members not loaded by loadMemberData()!
1390
-	if ($user == 0)
1391
-		return false;
1452
+	if ($user == 0) {
1453
+			return false;
1454
+	}
1392 1455
 	if (!isset($user_profile[$user]))
1393 1456
 	{
1394 1457
 		trigger_error('loadMemberContext(): member id ' . $user . ' not previously loaded by loadMemberData()', E_USER_WARNING);
@@ -1414,12 +1477,16 @@  discard block
 block discarded – undo
1414 1477
 	$buddy_list = !empty($profile['buddy_list']) ? explode(',', $profile['buddy_list']) : array();
1415 1478
 
1416 1479
 	//We need a little fallback for the membergroup icons. If it doesn't exist in the current theme, fallback to default theme
1417
-	if (isset($profile['icons'][1]) && file_exists($settings['actual_theme_dir'] . '/images/membericons/' . $profile['icons'][1])) //icon is set and exists
1480
+	if (isset($profile['icons'][1]) && file_exists($settings['actual_theme_dir'] . '/images/membericons/' . $profile['icons'][1])) {
1481
+		//icon is set and exists
1418 1482
 		$group_icon_url = $settings['images_url'] . '/membericons/' . $profile['icons'][1];
1419
-	elseif (isset($profile['icons'][1])) //icon is set and doesn't exist, fallback to default
1483
+	} elseif (isset($profile['icons'][1])) {
1484
+		//icon is set and doesn't exist, fallback to default
1420 1485
 		$group_icon_url = $settings['default_images_url'] . '/membericons/' . $profile['icons'][1];
1421
-	else //not set, bye bye
1486
+	} else {
1487
+		//not set, bye bye
1422 1488
 		$group_icon_url = '';
1489
+	}
1423 1490
 
1424 1491
 	// These minimal values are always loaded
1425 1492
 	$memberContext[$user] = array(
@@ -1438,8 +1505,9 @@  discard block
 block discarded – undo
1438 1505
 	if ($context['loadMemberContext_set'] != 'minimal')
1439 1506
 	{
1440 1507
 		// Go the extra mile and load the user's native language name.
1441
-		if (empty($loadedLanguages))
1442
-			$loadedLanguages = getLanguages();
1508
+		if (empty($loadedLanguages)) {
1509
+					$loadedLanguages = getLanguages();
1510
+		}
1443 1511
 
1444 1512
 		$memberContext[$user] += array(
1445 1513
 			'username_color' => '<span ' . (!empty($profile['member_group_color']) ? 'style="color:' . $profile['member_group_color'] . ';"' : '') . '>' . $profile['member_name'] . '</span>',
@@ -1494,31 +1562,33 @@  discard block
 block discarded – undo
1494 1562
 	{
1495 1563
 		if (!empty($modSettings['gravatarOverride']) || (!empty($modSettings['gravatarEnabled']) && stristr($profile['avatar'], 'gravatar://')))
1496 1564
 		{
1497
-			if (!empty($modSettings['gravatarAllowExtraEmail']) && stristr($profile['avatar'], 'gravatar://') && strlen($profile['avatar']) > 11)
1498
-				$image = get_gravatar_url($smcFunc['substr']($profile['avatar'], 11));
1499
-			else
1500
-				$image = get_gravatar_url($profile['email_address']);
1501
-		}
1502
-		else
1565
+			if (!empty($modSettings['gravatarAllowExtraEmail']) && stristr($profile['avatar'], 'gravatar://') && strlen($profile['avatar']) > 11) {
1566
+							$image = get_gravatar_url($smcFunc['substr']($profile['avatar'], 11));
1567
+			} else {
1568
+							$image = get_gravatar_url($profile['email_address']);
1569
+			}
1570
+		} else
1503 1571
 		{
1504 1572
 			// So it's stored in the member table?
1505 1573
 			if (!empty($profile['avatar']))
1506 1574
 			{
1507 1575
 				$image = (stristr($profile['avatar'], 'http://') || stristr($profile['avatar'], 'https://')) ? $profile['avatar'] : $modSettings['avatar_url'] . '/' . $profile['avatar'];
1576
+			} elseif (!empty($profile['filename'])) {
1577
+							$image = $modSettings['custom_avatar_url'] . '/' . $profile['filename'];
1508 1578
 			}
1509
-			elseif (!empty($profile['filename']))
1510
-				$image = $modSettings['custom_avatar_url'] . '/' . $profile['filename'];
1511 1579
 			// Right... no avatar...use the default one
1512
-			else
1513
-				$image = $modSettings['avatar_url'] . '/default.png';
1580
+			else {
1581
+							$image = $modSettings['avatar_url'] . '/default.png';
1582
+			}
1514 1583
 		}
1515
-		if (!empty($image))
1516
-			$memberContext[$user]['avatar'] = array(
1584
+		if (!empty($image)) {
1585
+					$memberContext[$user]['avatar'] = array(
1517 1586
 				'name' => $profile['avatar'],
1518 1587
 				'image' => '<img class="avatar" src="' . $image . '" alt="avatar_' . $profile['member_name'] . '">',
1519 1588
 				'href' => $image,
1520 1589
 				'url' => $image,
1521 1590
 			);
1591
+		}
1522 1592
 	}
1523 1593
 
1524 1594
 	// Are we also loading the members custom fields into context?
@@ -1526,13 +1596,15 @@  discard block
 block discarded – undo
1526 1596
 	{
1527 1597
 		$memberContext[$user]['custom_fields'] = array();
1528 1598
 
1529
-		if (!isset($context['display_fields']))
1530
-			$context['display_fields'] = $smcFunc['json_decode']($modSettings['displayFields'], true);
1599
+		if (!isset($context['display_fields'])) {
1600
+					$context['display_fields'] = $smcFunc['json_decode']($modSettings['displayFields'], true);
1601
+		}
1531 1602
 
1532 1603
 		foreach ($context['display_fields'] as $custom)
1533 1604
 		{
1534
-			if (!isset($custom['col_name']) || trim($custom['col_name']) == '' || empty($profile['options'][$custom['col_name']]))
1535
-				continue;
1605
+			if (!isset($custom['col_name']) || trim($custom['col_name']) == '' || empty($profile['options'][$custom['col_name']])) {
1606
+							continue;
1607
+			}
1536 1608
 
1537 1609
 			$value = $profile['options'][$custom['col_name']];
1538 1610
 
@@ -1540,31 +1612,36 @@  discard block
 block discarded – undo
1540 1612
 			$currentKey = 0;
1541 1613
 
1542 1614
 			// Create a key => value array for multiple options fields
1543
-			if (!empty($custom['options']))
1544
-				foreach ($custom['options'] as $k => $v)
1615
+			if (!empty($custom['options'])) {
1616
+							foreach ($custom['options'] as $k => $v)
1545 1617
 				{
1546 1618
 					$fieldOptions[] = $v;
1547
-					if (empty($currentKey))
1548
-						$currentKey = $v == $value ? $k : 0;
1619
+			}
1620
+					if (empty($currentKey)) {
1621
+											$currentKey = $v == $value ? $k : 0;
1622
+					}
1549 1623
 				}
1550 1624
 
1551 1625
 			// BBC?
1552
-			if ($custom['bbc'])
1553
-				$value = parse_bbc($value);
1626
+			if ($custom['bbc']) {
1627
+							$value = parse_bbc($value);
1628
+			}
1554 1629
 
1555 1630
 			// ... or checkbox?
1556
-			elseif (isset($custom['type']) && $custom['type'] == 'check')
1557
-				$value = $value ? $txt['yes'] : $txt['no'];
1631
+			elseif (isset($custom['type']) && $custom['type'] == 'check') {
1632
+							$value = $value ? $txt['yes'] : $txt['no'];
1633
+			}
1558 1634
 
1559 1635
 			// Enclosing the user input within some other text?
1560
-			if (!empty($custom['enclose']))
1561
-				$value = strtr($custom['enclose'], array(
1636
+			if (!empty($custom['enclose'])) {
1637
+							$value = strtr($custom['enclose'], array(
1562 1638
 					'{SCRIPTURL}' => $scripturl,
1563 1639
 					'{IMAGES_URL}' => $settings['images_url'],
1564 1640
 					'{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
1565 1641
 					'{INPUT}' => $value,
1566 1642
 					'{KEY}' => $currentKey,
1567 1643
 				));
1644
+			}
1568 1645
 
1569 1646
 			$memberContext[$user]['custom_fields'][] = array(
1570 1647
 				'title' => !empty($custom['title']) ? $custom['title'] : $custom['col_name'],
@@ -1591,8 +1668,9 @@  discard block
 block discarded – undo
1591 1668
 	global $smcFunc, $txt, $scripturl, $settings;
1592 1669
 
1593 1670
 	// Do not waste my time...
1594
-	if (empty($users) || empty($params))
1595
-		return false;
1671
+	if (empty($users) || empty($params)) {
1672
+			return false;
1673
+	}
1596 1674
 
1597 1675
 	// Make sure it's an array.
1598 1676
 	$users = !is_array($users) ? array($users) : array_unique($users);
@@ -1619,41 +1697,48 @@  discard block
 block discarded – undo
1619 1697
 		$currentKey = 0;
1620 1698
 
1621 1699
 		// Create a key => value array for multiple options fields
1622
-		if (!empty($row['field_options']))
1623
-			foreach (explode(',', $row['field_options']) as $k => $v)
1700
+		if (!empty($row['field_options'])) {
1701
+					foreach (explode(',', $row['field_options']) as $k => $v)
1624 1702
 			{
1625 1703
 				$fieldOptions[] = $v;
1626
-				if (empty($currentKey))
1627
-					$currentKey = $v == $row['value'] ? $k : 0;
1704
+		}
1705
+				if (empty($currentKey)) {
1706
+									$currentKey = $v == $row['value'] ? $k : 0;
1707
+				}
1628 1708
 			}
1629 1709
 
1630 1710
 		// BBC?
1631
-		if (!empty($row['bbc']))
1632
-			$row['value'] = parse_bbc($row['value']);
1711
+		if (!empty($row['bbc'])) {
1712
+					$row['value'] = parse_bbc($row['value']);
1713
+		}
1633 1714
 
1634 1715
 		// ... or checkbox?
1635
-		elseif (isset($row['type']) && $row['type'] == 'check')
1636
-			$row['value'] = !empty($row['value']) ? $txt['yes'] : $txt['no'];
1716
+		elseif (isset($row['type']) && $row['type'] == 'check') {
1717
+					$row['value'] = !empty($row['value']) ? $txt['yes'] : $txt['no'];
1718
+		}
1637 1719
 
1638 1720
 		// Enclosing the user input within some other text?
1639
-		if (!empty($row['enclose']))
1640
-			$row['value'] = strtr($row['enclose'], array(
1721
+		if (!empty($row['enclose'])) {
1722
+					$row['value'] = strtr($row['enclose'], array(
1641 1723
 				'{SCRIPTURL}' => $scripturl,
1642 1724
 				'{IMAGES_URL}' => $settings['images_url'],
1643 1725
 				'{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
1644 1726
 				'{INPUT}' => un_htmlspecialchars($row['value']),
1645 1727
 				'{KEY}' => $currentKey,
1646 1728
 			));
1729
+		}
1647 1730
 
1648 1731
 		// Send a simple array if there is just 1 param
1649
-		if (count($params) == 1)
1650
-			$return[$row['id_member']] = $row;
1732
+		if (count($params) == 1) {
1733
+					$return[$row['id_member']] = $row;
1734
+		}
1651 1735
 
1652 1736
 		// More than 1? knock yourself out...
1653 1737
 		else
1654 1738
 		{
1655
-			if (!isset($return[$row['id_member']]))
1656
-				$return[$row['id_member']] = array();
1739
+			if (!isset($return[$row['id_member']])) {
1740
+							$return[$row['id_member']] = array();
1741
+			}
1657 1742
 
1658 1743
 			$return[$row['id_member']][$row['variable']] = $row;
1659 1744
 		}
@@ -1687,8 +1772,9 @@  discard block
 block discarded – undo
1687 1772
 	global $context;
1688 1773
 
1689 1774
 	// Don't know any browser!
1690
-	if (empty($context['browser']))
1691
-		detectBrowser();
1775
+	if (empty($context['browser'])) {
1776
+			detectBrowser();
1777
+	}
1692 1778
 
1693 1779
 	return !empty($context['browser'][$browser]) || !empty($context['browser']['is_' . $browser]) ? true : false;
1694 1780
 }
@@ -1706,8 +1792,9 @@  discard block
 block discarded – undo
1706 1792
 	global $context, $settings, $options, $sourcedir, $ssi_theme, $smcFunc, $language, $board, $image_proxy_enabled;
1707 1793
 
1708 1794
 	// The theme was specified by parameter.
1709
-	if (!empty($id_theme))
1710
-		$id_theme = (int) $id_theme;
1795
+	if (!empty($id_theme)) {
1796
+			$id_theme = (int) $id_theme;
1797
+	}
1711 1798
 	// The theme was specified by REQUEST.
1712 1799
 	elseif (!empty($_REQUEST['theme']) && (!empty($modSettings['theme_allow']) || allowedTo('admin_forum')))
1713 1800
 	{
@@ -1715,32 +1802,38 @@  discard block
 block discarded – undo
1715 1802
 		$_SESSION['id_theme'] = $id_theme;
1716 1803
 	}
1717 1804
 	// The theme was specified by REQUEST... previously.
1718
-	elseif (!empty($_SESSION['id_theme']) && (!empty($modSettings['theme_allow']) || allowedTo('admin_forum')))
1719
-		$id_theme = (int) $_SESSION['id_theme'];
1805
+	elseif (!empty($_SESSION['id_theme']) && (!empty($modSettings['theme_allow']) || allowedTo('admin_forum'))) {
1806
+			$id_theme = (int) $_SESSION['id_theme'];
1807
+	}
1720 1808
 	// The theme is just the user's choice. (might use ?board=1;theme=0 to force board theme.)
1721
-	elseif (!empty($user_info['theme']) && !isset($_REQUEST['theme']))
1722
-		$id_theme = $user_info['theme'];
1809
+	elseif (!empty($user_info['theme']) && !isset($_REQUEST['theme'])) {
1810
+			$id_theme = $user_info['theme'];
1811
+	}
1723 1812
 	// The theme was specified by the board.
1724
-	elseif (!empty($board_info['theme']))
1725
-		$id_theme = $board_info['theme'];
1813
+	elseif (!empty($board_info['theme'])) {
1814
+			$id_theme = $board_info['theme'];
1815
+	}
1726 1816
 	// The theme is the forum's default.
1727
-	else
1728
-		$id_theme = $modSettings['theme_guests'];
1817
+	else {
1818
+			$id_theme = $modSettings['theme_guests'];
1819
+	}
1729 1820
 
1730 1821
 	// Verify the id_theme... no foul play.
1731 1822
 	// Always allow the board specific theme, if they are overriding.
1732
-	if (!empty($board_info['theme']) && $board_info['override_theme'])
1733
-		$id_theme = $board_info['theme'];
1823
+	if (!empty($board_info['theme']) && $board_info['override_theme']) {
1824
+			$id_theme = $board_info['theme'];
1825
+	}
1734 1826
 	// If they have specified a particular theme to use with SSI allow it to be used.
1735
-	elseif (!empty($ssi_theme) && $id_theme == $ssi_theme)
1736
-		$id_theme = (int) $id_theme;
1737
-	elseif (!empty($modSettings['enableThemes']) && !allowedTo('admin_forum'))
1827
+	elseif (!empty($ssi_theme) && $id_theme == $ssi_theme) {
1828
+			$id_theme = (int) $id_theme;
1829
+	} elseif (!empty($modSettings['enableThemes']) && !allowedTo('admin_forum'))
1738 1830
 	{
1739 1831
 		$themes = explode(',', $modSettings['enableThemes']);
1740
-		if (!in_array($id_theme, $themes))
1741
-			$id_theme = $modSettings['theme_guests'];
1742
-		else
1743
-			$id_theme = (int) $id_theme;
1832
+		if (!in_array($id_theme, $themes)) {
1833
+					$id_theme = $modSettings['theme_guests'];
1834
+		} else {
1835
+					$id_theme = (int) $id_theme;
1836
+		}
1744 1837
 	}
1745 1838
 		
1746 1839
 	// We already load the basic stuff?
@@ -1749,18 +1842,19 @@  discard block
 block discarded – undo
1749 1842
 		$member = empty($user_info['id']) ? -1 : $user_info['id'];
1750 1843
 
1751 1844
 		// Disable image proxy if we don't have SSL enabled
1752
-		if (empty($modSettings['force_ssl']))
1753
-			$image_proxy_enabled = false;
1845
+		if (empty($modSettings['force_ssl'])) {
1846
+					$image_proxy_enabled = false;
1847
+		}
1754 1848
 
1755 1849
 		if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2 && ($temp = cache_get_data('theme_settings-' . $id_theme . ':' . $member, 60)) != null && time() - 60 > $modSettings['settings_updated'])
1756 1850
 		{
1757 1851
 			$themeData = $temp;
1758 1852
 			$flag = true;
1853
+		} elseif (($temp = cache_get_data('theme_settings-' . $id_theme, 90)) != null && time() - 60 > $modSettings['settings_updated']) {
1854
+					$themeData = $temp + array($member => array());
1855
+		} else {
1856
+					$themeData = array(-1 => array(), 0 => array(), $member => array());
1759 1857
 		}
1760
-		elseif (($temp = cache_get_data('theme_settings-' . $id_theme, 90)) != null && time() - 60 > $modSettings['settings_updated'])
1761
-			$themeData = $temp + array($member => array());
1762
-		else
1763
-			$themeData = array(-1 => array(), 0 => array(), $member => array());
1764 1858
 
1765 1859
 		if (empty($flag))
1766 1860
 		{
@@ -1780,31 +1874,37 @@  discard block
 block discarded – undo
1780 1874
 			while ($row = $smcFunc['db_fetch_assoc']($result))
1781 1875
 			{
1782 1876
 				// There are just things we shouldn't be able to change as members.
1783
-				if ($row['id_member'] != 0 && in_array($row['variable'], array('actual_theme_url', 'actual_images_url', 'base_theme_dir', 'base_theme_url', 'default_images_url', 'default_theme_dir', 'default_theme_url', 'default_template', 'images_url', 'number_recent_posts', 'smiley_sets_default', 'theme_dir', 'theme_id', 'theme_layers', 'theme_templates', 'theme_url')))
1784
-					continue;
1877
+				if ($row['id_member'] != 0 && in_array($row['variable'], array('actual_theme_url', 'actual_images_url', 'base_theme_dir', 'base_theme_url', 'default_images_url', 'default_theme_dir', 'default_theme_url', 'default_template', 'images_url', 'number_recent_posts', 'smiley_sets_default', 'theme_dir', 'theme_id', 'theme_layers', 'theme_templates', 'theme_url'))) {
1878
+									continue;
1879
+				}
1785 1880
 
1786 1881
 				// If this is the theme_dir of the default theme, store it.
1787
-				if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1' && empty($row['id_member']))
1788
-					$themeData[0]['default_' . $row['variable']] = $row['value'];
1882
+				if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1' && empty($row['id_member'])) {
1883
+									$themeData[0]['default_' . $row['variable']] = $row['value'];
1884
+				}
1789 1885
 
1790 1886
 				// If this isn't set yet, is a theme option, or is not the default theme..
1791
-				if (!isset($themeData[$row['id_member']][$row['variable']]) || $row['id_theme'] != '1')
1792
-					$themeData[$row['id_member']][$row['variable']] = substr($row['variable'], 0, 5) == 'show_' ? $row['value'] == '1' : $row['value'];
1887
+				if (!isset($themeData[$row['id_member']][$row['variable']]) || $row['id_theme'] != '1') {
1888
+									$themeData[$row['id_member']][$row['variable']] = substr($row['variable'], 0, 5) == 'show_' ? $row['value'] == '1' : $row['value'];
1889
+				}
1793 1890
 			}
1794 1891
 			$smcFunc['db_free_result']($result);
1795 1892
 
1796
-			if (!empty($themeData[-1]))
1797
-				foreach ($themeData[-1] as $k => $v)
1893
+			if (!empty($themeData[-1])) {
1894
+							foreach ($themeData[-1] as $k => $v)
1798 1895
 				{
1799 1896
 					if (!isset($themeData[$member][$k]))
1800 1897
 						$themeData[$member][$k] = $v;
1898
+			}
1801 1899
 				}
1802 1900
 
1803
-			if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
1804
-				cache_put_data('theme_settings-' . $id_theme . ':' . $member, $themeData, 60);
1901
+			if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) {
1902
+							cache_put_data('theme_settings-' . $id_theme . ':' . $member, $themeData, 60);
1903
+			}
1805 1904
 			// Only if we didn't already load that part of the cache...
1806
-			elseif (!isset($temp))
1807
-				cache_put_data('theme_settings-' . $id_theme, array(-1 => $themeData[-1], 0 => $themeData[0]), 90);
1905
+			elseif (!isset($temp)) {
1906
+							cache_put_data('theme_settings-' . $id_theme, array(-1 => $themeData[-1], 0 => $themeData[0]), 90);
1907
+			}
1808 1908
 		}
1809 1909
 
1810 1910
 		$settings = $themeData[0];
@@ -1821,17 +1921,20 @@  discard block
 block discarded – undo
1821 1921
 		$settings['template_dirs'][] = $settings['theme_dir'];
1822 1922
 
1823 1923
 		// Based on theme (if there is one).
1824
-		if (!empty($settings['base_theme_dir']))
1825
-			$settings['template_dirs'][] = $settings['base_theme_dir'];
1924
+		if (!empty($settings['base_theme_dir'])) {
1925
+					$settings['template_dirs'][] = $settings['base_theme_dir'];
1926
+		}
1826 1927
 
1827 1928
 		// Lastly the default theme.
1828
-		if ($settings['theme_dir'] != $settings['default_theme_dir'])
1829
-			$settings['template_dirs'][] = $settings['default_theme_dir'];
1929
+		if ($settings['theme_dir'] != $settings['default_theme_dir']) {
1930
+					$settings['template_dirs'][] = $settings['default_theme_dir'];
1931
+		}
1830 1932
 	}
1831 1933
 
1832 1934
 
1833
-	if (!$initialize)
1834
-		return;
1935
+	if (!$initialize) {
1936
+			return;
1937
+	}
1835 1938
 
1836 1939
 	// Check to see if we're forcing SSL
1837 1940
 	if (!empty($modSettings['force_ssl']) && empty($maintenance) &&
@@ -1852,8 +1955,9 @@  discard block
 block discarded – undo
1852 1955
 		$detected_url = httpsOn() ? 'https://' : 'http://';
1853 1956
 		$detected_url .= empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
1854 1957
 		$temp = preg_replace('~/' . basename($scripturl) . '(/.+)?$~', '', strtr(dirname($_SERVER['PHP_SELF']), '\\', '/'));
1855
-		if ($temp != '/')
1856
-			$detected_url .= $temp;
1958
+		if ($temp != '/') {
1959
+					$detected_url .= $temp;
1960
+		}
1857 1961
 	}
1858 1962
 	if (isset($detected_url) && $detected_url != $boardurl)
1859 1963
 	{
@@ -1865,8 +1969,9 @@  discard block
 block discarded – undo
1865 1969
 			foreach ($aliases as $alias)
1866 1970
 			{
1867 1971
 				// Rip off all the boring parts, spaces, etc.
1868
-				if ($detected_url == trim($alias) || strtr($detected_url, array('http://' => '', 'https://' => '')) == trim($alias))
1869
-					$do_fix = true;
1972
+				if ($detected_url == trim($alias) || strtr($detected_url, array('http://' => '', 'https://' => '')) == trim($alias)) {
1973
+									$do_fix = true;
1974
+				}
1870 1975
 			}
1871 1976
 		}
1872 1977
 
@@ -1874,21 +1979,23 @@  discard block
 block discarded – undo
1874 1979
 		if (empty($do_fix) && strtr($detected_url, array('://' => '://www.')) == $boardurl && (empty($_GET) || count($_GET) == 1) && SMF != 'SSI')
1875 1980
 		{
1876 1981
 			// Okay, this seems weird, but we don't want an endless loop - this will make $_GET not empty ;).
1877
-			if (empty($_GET))
1878
-				redirectexit('wwwRedirect');
1879
-			else
1982
+			if (empty($_GET)) {
1983
+							redirectexit('wwwRedirect');
1984
+			} else
1880 1985
 			{
1881 1986
 				$k = key($_GET);
1882 1987
 				$v = current($_GET);
1883 1988
 
1884
-				if ($k != 'wwwRedirect')
1885
-					redirectexit('wwwRedirect;' . $k . '=' . $v);
1989
+				if ($k != 'wwwRedirect') {
1990
+									redirectexit('wwwRedirect;' . $k . '=' . $v);
1991
+				}
1886 1992
 			}
1887 1993
 		}
1888 1994
 
1889 1995
 		// #3 is just a check for SSL...
1890
-		if (strtr($detected_url, array('https://' => 'http://')) == $boardurl)
1891
-			$do_fix = true;
1996
+		if (strtr($detected_url, array('https://' => 'http://')) == $boardurl) {
1997
+					$do_fix = true;
1998
+		}
1892 1999
 
1893 2000
 		// Okay, #4 - perhaps it's an IP address?  We're gonna want to use that one, then. (assuming it's the IP or something...)
1894 2001
 		if (!empty($do_fix) || preg_match('~^http[s]?://(?:[\d\.:]+|\[[\d:]+\](?::\d+)?)(?:$|/)~', $detected_url) == 1)
@@ -1923,8 +2030,9 @@  discard block
 block discarded – undo
1923 2030
 					$board_info['moderators'][$k]['link'] = strtr($dummy['link'], array('"' . $oldurl => '"' . $boardurl));
1924 2031
 				}
1925 2032
 			}
1926
-			foreach ($context['linktree'] as $k => $dummy)
1927
-				$context['linktree'][$k]['url'] = strtr($dummy['url'], array($oldurl => $boardurl));
2033
+			foreach ($context['linktree'] as $k => $dummy) {
2034
+							$context['linktree'][$k]['url'] = strtr($dummy['url'], array($oldurl => $boardurl));
2035
+			}
1928 2036
 		}
1929 2037
 	}
1930 2038
 	// Set up the contextual user array.
@@ -1943,16 +2051,16 @@  discard block
 block discarded – undo
1943 2051
 			'email' => $user_info['email'],
1944 2052
 			'ignoreusers' => $user_info['ignoreusers'],
1945 2053
 		);
1946
-		if (!$context['user']['is_guest'])
1947
-			$context['user']['name'] = $user_info['name'];
1948
-		elseif ($context['user']['is_guest'] && !empty($txt['guest_title']))
1949
-			$context['user']['name'] = $txt['guest_title'];
2054
+		if (!$context['user']['is_guest']) {
2055
+					$context['user']['name'] = $user_info['name'];
2056
+		} elseif ($context['user']['is_guest'] && !empty($txt['guest_title'])) {
2057
+					$context['user']['name'] = $txt['guest_title'];
2058
+		}
1950 2059
 
1951 2060
 		// Determine the current smiley set.
1952 2061
 		$user_info['smiley_set'] = (!in_array($user_info['smiley_set'], explode(',', $modSettings['smiley_sets_known'])) && $user_info['smiley_set'] != 'none') || empty($modSettings['smiley_sets_enable']) ? (!empty($settings['smiley_sets_default']) ? $settings['smiley_sets_default'] : $modSettings['smiley_sets_default']) : $user_info['smiley_set'];
1953 2062
 		$context['user']['smiley_set'] = $user_info['smiley_set'];
1954
-	}
1955
-	else
2063
+	} else
1956 2064
 	{
1957 2065
 		// What to do when there is no $user_info (e.g., an error very early in the login process)
1958 2066
 		$context['user'] = array(
@@ -1986,18 +2094,24 @@  discard block
 block discarded – undo
1986 2094
 	}
1987 2095
 
1988 2096
 	// Some basic information...
1989
-	if (!isset($context['html_headers']))
1990
-		$context['html_headers'] = '';
1991
-	if (!isset($context['javascript_files']))
1992
-		$context['javascript_files'] = array();
1993
-	if (!isset($context['css_files']))
1994
-		$context['css_files'] = array();
1995
-	if (!isset($context['css_header']))
1996
-		$context['css_header'] = array();
1997
-	if (!isset($context['javascript_inline']))
1998
-		$context['javascript_inline'] = array('standard' => array(), 'defer' => array());
1999
-	if (!isset($context['javascript_vars']))
2000
-		$context['javascript_vars'] = array();
2097
+	if (!isset($context['html_headers'])) {
2098
+			$context['html_headers'] = '';
2099
+	}
2100
+	if (!isset($context['javascript_files'])) {
2101
+			$context['javascript_files'] = array();
2102
+	}
2103
+	if (!isset($context['css_files'])) {
2104
+			$context['css_files'] = array();
2105
+	}
2106
+	if (!isset($context['css_header'])) {
2107
+			$context['css_header'] = array();
2108
+	}
2109
+	if (!isset($context['javascript_inline'])) {
2110
+			$context['javascript_inline'] = array('standard' => array(), 'defer' => array());
2111
+	}
2112
+	if (!isset($context['javascript_vars'])) {
2113
+			$context['javascript_vars'] = array();
2114
+	}
2001 2115
 
2002 2116
 	$context['login_url'] =  $scripturl . '?action=login2';
2003 2117
 	$context['menu_separator'] = !empty($settings['use_image_buttons']) ? ' ' : ' | ';
@@ -2009,16 +2123,18 @@  discard block
 block discarded – undo
2009 2123
 	$context['current_action'] = isset($_REQUEST['action']) ? $smcFunc['htmlspecialchars']($_REQUEST['action']) : null;
2010 2124
 	$context['current_subaction'] = isset($_REQUEST['sa']) ? $_REQUEST['sa'] : null;
2011 2125
 	$context['can_register'] = empty($modSettings['registration_method']) || $modSettings['registration_method'] != 3;
2012
-	if (isset($modSettings['load_average']))
2013
-		$context['load_average'] = $modSettings['load_average'];
2126
+	if (isset($modSettings['load_average'])) {
2127
+			$context['load_average'] = $modSettings['load_average'];
2128
+	}
2014 2129
 
2015 2130
 	// Detect the browser. This is separated out because it's also used in attachment downloads
2016 2131
 	detectBrowser();
2017 2132
 
2018 2133
 	// Set the top level linktree up.
2019 2134
 	// Note that if we're dealing with certain very early errors (e.g., login) the linktree might not be set yet...
2020
-	if (empty($context['linktree']))
2021
-		$context['linktree'] = array();
2135
+	if (empty($context['linktree'])) {
2136
+			$context['linktree'] = array();
2137
+	}
2022 2138
 	array_unshift($context['linktree'], array(
2023 2139
 		'url' => $scripturl,
2024 2140
 		'name' => $context['forum_name_html_safe']
@@ -2027,8 +2143,9 @@  discard block
 block discarded – undo
2027 2143
 	// This allows sticking some HTML on the page output - useful for controls.
2028 2144
 	$context['insert_after_template'] = '';
2029 2145
 
2030
-	if (!isset($txt))
2031
-		$txt = array();
2146
+	if (!isset($txt)) {
2147
+			$txt = array();
2148
+	}
2032 2149
 
2033 2150
 	$simpleActions = array(
2034 2151
 		'findmember',
@@ -2074,9 +2191,10 @@  discard block
 block discarded – undo
2074 2191
 
2075 2192
 	// See if theres any extra param to check.
2076 2193
 	$requiresXML = false;
2077
-	foreach ($extraParams as $key => $extra)
2078
-		if (isset($_REQUEST[$extra]))
2194
+	foreach ($extraParams as $key => $extra) {
2195
+			if (isset($_REQUEST[$extra]))
2079 2196
 			$requiresXML = true;
2197
+	}
2080 2198
 
2081 2199
 	// Output is fully XML, so no need for the index template.
2082 2200
 	if (isset($_REQUEST['xml']) && (in_array($context['current_action'], $xmlActions) || $requiresXML))
@@ -2091,37 +2209,39 @@  discard block
 block discarded – undo
2091 2209
 	{
2092 2210
 		loadLanguage('index+Modifications');
2093 2211
 		$context['template_layers'] = array();
2094
-	}
2095
-
2096
-	else
2212
+	} else
2097 2213
 	{
2098 2214
 		// Custom templates to load, or just default?
2099
-		if (isset($settings['theme_templates']))
2100
-			$templates = explode(',', $settings['theme_templates']);
2101
-		else
2102
-			$templates = array('index');
2215
+		if (isset($settings['theme_templates'])) {
2216
+					$templates = explode(',', $settings['theme_templates']);
2217
+		} else {
2218
+					$templates = array('index');
2219
+		}
2103 2220
 
2104 2221
 		// Load each template...
2105
-		foreach ($templates as $template)
2106
-			loadTemplate($template);
2222
+		foreach ($templates as $template) {
2223
+					loadTemplate($template);
2224
+		}
2107 2225
 
2108 2226
 		// ...and attempt to load their associated language files.
2109 2227
 		$required_files = implode('+', array_merge($templates, array('Modifications')));
2110 2228
 		loadLanguage($required_files, '', false);
2111 2229
 
2112 2230
 		// Custom template layers?
2113
-		if (isset($settings['theme_layers']))
2114
-			$context['template_layers'] = explode(',', $settings['theme_layers']);
2115
-		else
2116
-			$context['template_layers'] = array('html', 'body');
2231
+		if (isset($settings['theme_layers'])) {
2232
+					$context['template_layers'] = explode(',', $settings['theme_layers']);
2233
+		} else {
2234
+					$context['template_layers'] = array('html', 'body');
2235
+		}
2117 2236
 	}
2118 2237
 
2119 2238
 	// Initialize the theme.
2120 2239
 	loadSubTemplate('init', 'ignore');
2121 2240
 
2122 2241
 	// Allow overriding the board wide time/number formats.
2123
-	if (empty($user_settings['time_format']) && !empty($txt['time_format']))
2124
-		$user_info['time_format'] = $txt['time_format'];
2242
+	if (empty($user_settings['time_format']) && !empty($txt['time_format'])) {
2243
+			$user_info['time_format'] = $txt['time_format'];
2244
+	}
2125 2245
 
2126 2246
 	// Set the character set from the template.
2127 2247
 	$context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
@@ -2129,12 +2249,14 @@  discard block
 block discarded – undo
2129 2249
 	$context['right_to_left'] = !empty($txt['lang_rtl']);
2130 2250
 
2131 2251
 	// Guests may still need a name.
2132
-	if ($context['user']['is_guest'] && empty($context['user']['name']))
2133
-		$context['user']['name'] = $txt['guest_title'];
2252
+	if ($context['user']['is_guest'] && empty($context['user']['name'])) {
2253
+			$context['user']['name'] = $txt['guest_title'];
2254
+	}
2134 2255
 
2135 2256
 	// Any theme-related strings that need to be loaded?
2136
-	if (!empty($settings['require_theme_strings']))
2137
-		loadLanguage('ThemeStrings', '', false);
2257
+	if (!empty($settings['require_theme_strings'])) {
2258
+			loadLanguage('ThemeStrings', '', false);
2259
+	}
2138 2260
 
2139 2261
 	// Make a special URL for the language.
2140 2262
 	$settings['lang_images_url'] = $settings['images_url'] . '/' . (!empty($txt['image_lang']) ? $txt['image_lang'] : $user_info['language']);
@@ -2145,8 +2267,9 @@  discard block
 block discarded – undo
2145 2267
 	// Here is my luvly Responsive CSS
2146 2268
 	loadCSSFile('responsive.css', array('force_current' => false, 'validate' => true, 'minimize' => true, 'order_pos' => 9000), 'smf_responsive');
2147 2269
 
2148
-	if ($context['right_to_left'])
2149
-		loadCSSFile('rtl.css', array('order_pos' => 200), 'smf_rtl');
2270
+	if ($context['right_to_left']) {
2271
+			loadCSSFile('rtl.css', array('order_pos' => 200), 'smf_rtl');
2272
+	}
2150 2273
 
2151 2274
 	// We allow theme variants, because we're cool.
2152 2275
 	$context['theme_variant'] = '';
@@ -2154,14 +2277,17 @@  discard block
 block discarded – undo
2154 2277
 	if (!empty($settings['theme_variants']))
2155 2278
 	{
2156 2279
 		// Overriding - for previews and that ilk.
2157
-		if (!empty($_REQUEST['variant']))
2158
-			$_SESSION['id_variant'] = $_REQUEST['variant'];
2280
+		if (!empty($_REQUEST['variant'])) {
2281
+					$_SESSION['id_variant'] = $_REQUEST['variant'];
2282
+		}
2159 2283
 		// User selection?
2160
-		if (empty($settings['disable_user_variant']) || allowedTo('admin_forum'))
2161
-			$context['theme_variant'] = !empty($_SESSION['id_variant']) ? $_SESSION['id_variant'] : (!empty($options['theme_variant']) ? $options['theme_variant'] : '');
2284
+		if (empty($settings['disable_user_variant']) || allowedTo('admin_forum')) {
2285
+					$context['theme_variant'] = !empty($_SESSION['id_variant']) ? $_SESSION['id_variant'] : (!empty($options['theme_variant']) ? $options['theme_variant'] : '');
2286
+		}
2162 2287
 		// If not a user variant, select the default.
2163
-		if ($context['theme_variant'] == '' || !in_array($context['theme_variant'], $settings['theme_variants']))
2164
-			$context['theme_variant'] = !empty($settings['default_variant']) && in_array($settings['default_variant'], $settings['theme_variants']) ? $settings['default_variant'] : $settings['theme_variants'][0];
2288
+		if ($context['theme_variant'] == '' || !in_array($context['theme_variant'], $settings['theme_variants'])) {
2289
+					$context['theme_variant'] = !empty($settings['default_variant']) && in_array($settings['default_variant'], $settings['theme_variants']) ? $settings['default_variant'] : $settings['theme_variants'][0];
2290
+		}
2165 2291
 
2166 2292
 		// Do this to keep things easier in the templates.
2167 2293
 		$context['theme_variant'] = '_' . $context['theme_variant'];
@@ -2170,20 +2296,23 @@  discard block
 block discarded – undo
2170 2296
 		if (!empty($context['theme_variant']))
2171 2297
 		{
2172 2298
 			loadCSSFile('index' . $context['theme_variant'] . '.css', array('order_pos' => 300), 'smf_index' . $context['theme_variant']);
2173
-			if ($context['right_to_left'])
2174
-				loadCSSFile('rtl' . $context['theme_variant'] . '.css', array('order_pos' => 400), 'smf_rtl' . $context['theme_variant']);
2299
+			if ($context['right_to_left']) {
2300
+							loadCSSFile('rtl' . $context['theme_variant'] . '.css', array('order_pos' => 400), 'smf_rtl' . $context['theme_variant']);
2301
+			}
2175 2302
 		}
2176 2303
 	}
2177 2304
 
2178 2305
 	// Let's be compatible with old themes!
2179
-	if (!function_exists('template_html_above') && in_array('html', $context['template_layers']))
2180
-		$context['template_layers'] = array('main');
2306
+	if (!function_exists('template_html_above') && in_array('html', $context['template_layers'])) {
2307
+			$context['template_layers'] = array('main');
2308
+	}
2181 2309
 
2182 2310
 	$context['tabindex'] = 1;
2183 2311
 
2184 2312
 	// Compatibility.
2185
-	if (!isset($settings['theme_version']))
2186
-		$modSettings['memberCount'] = $modSettings['totalMembers'];
2313
+	if (!isset($settings['theme_version'])) {
2314
+			$modSettings['memberCount'] = $modSettings['totalMembers'];
2315
+	}
2187 2316
 
2188 2317
 	// Default JS variables for use in every theme
2189 2318
 	$context['javascript_vars'] = array(
@@ -2203,18 +2332,18 @@  discard block
 block discarded – undo
2203 2332
 	);
2204 2333
 
2205 2334
 	// Add the JQuery library to the list of files to load.
2206
-	if (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'cdn')
2207
-		loadJavaScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array('external' => true), 'smf_jquery');
2208
-
2209
-	elseif (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'local')
2210
-		loadJavaScriptFile('jquery-3.2.1.min.js', array('seed' => false), 'smf_jquery');
2211
-
2212
-	elseif (isset($modSettings['jquery_source'], $modSettings['jquery_custom']) && $modSettings['jquery_source'] == 'custom')
2213
-		loadJavaScriptFile($modSettings['jquery_custom'], array('external' => true), 'smf_jquery');
2335
+	if (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'cdn') {
2336
+			loadJavaScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array('external' => true), 'smf_jquery');
2337
+	} elseif (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'local') {
2338
+			loadJavaScriptFile('jquery-3.2.1.min.js', array('seed' => false), 'smf_jquery');
2339
+	} elseif (isset($modSettings['jquery_source'], $modSettings['jquery_custom']) && $modSettings['jquery_source'] == 'custom') {
2340
+			loadJavaScriptFile($modSettings['jquery_custom'], array('external' => true), 'smf_jquery');
2341
+	}
2214 2342
 
2215 2343
 	// Auto loading? template_javascript() will take care of the local half of this.
2216
-	else
2217
-		loadJavaScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array('external' => true), 'smf_jquery');
2344
+	else {
2345
+			loadJavaScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array('external' => true), 'smf_jquery');
2346
+	}
2218 2347
 
2219 2348
 	// Queue our JQuery plugins!
2220 2349
 	loadJavaScriptFile('smf_jquery_plugins.js', array('minimize' => true), 'smf_jquery_plugins');
@@ -2237,12 +2366,12 @@  discard block
 block discarded – undo
2237 2366
 			require_once($sourcedir . '/ScheduledTasks.php');
2238 2367
 
2239 2368
 			// What to do, what to do?!
2240
-			if (empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time())
2241
-				AutoTask();
2242
-			else
2243
-				ReduceMailQueue();
2244
-		}
2245
-		else
2369
+			if (empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time()) {
2370
+							AutoTask();
2371
+			} else {
2372
+							ReduceMailQueue();
2373
+			}
2374
+		} else
2246 2375
 		{
2247 2376
 			$type = empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time() ? 'task' : 'mailq';
2248 2377
 			$ts = $type == 'mailq' ? $modSettings['mail_next_send'] : $modSettings['next_task_time'];
@@ -2293,8 +2422,9 @@  discard block
 block discarded – undo
2293 2422
 		foreach ($theme_includes as $include)
2294 2423
 		{
2295 2424
 			$include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir']));
2296
-			if (file_exists($include))
2297
-				require_once($include);
2425
+			if (file_exists($include)) {
2426
+							require_once($include);
2427
+			}
2298 2428
 		}
2299 2429
 	}
2300 2430
 
@@ -2324,16 +2454,19 @@  discard block
 block discarded – undo
2324 2454
 	// Do any style sheets first, cause we're easy with those.
2325 2455
 	if (!empty($style_sheets))
2326 2456
 	{
2327
-		if (!is_array($style_sheets))
2328
-			$style_sheets = array($style_sheets);
2457
+		if (!is_array($style_sheets)) {
2458
+					$style_sheets = array($style_sheets);
2459
+		}
2329 2460
 
2330
-		foreach ($style_sheets as $sheet)
2331
-			loadCSSFile($sheet . '.css', array(), $sheet);
2461
+		foreach ($style_sheets as $sheet) {
2462
+					loadCSSFile($sheet . '.css', array(), $sheet);
2463
+		}
2332 2464
 	}
2333 2465
 
2334 2466
 	// No template to load?
2335
-	if ($template_name === false)
2336
-		return true;
2467
+	if ($template_name === false) {
2468
+			return true;
2469
+	}
2337 2470
 
2338 2471
 	$loaded = false;
2339 2472
 	foreach ($settings['template_dirs'] as $template_dir)
@@ -2348,12 +2481,14 @@  discard block
 block discarded – undo
2348 2481
 
2349 2482
 	if ($loaded)
2350 2483
 	{
2351
-		if ($db_show_debug === true)
2352
-			$context['debug']['templates'][] = $template_name . ' (' . basename($template_dir) . ')';
2484
+		if ($db_show_debug === true) {
2485
+					$context['debug']['templates'][] = $template_name . ' (' . basename($template_dir) . ')';
2486
+		}
2353 2487
 
2354 2488
 		// If they have specified an initialization function for this template, go ahead and call it now.
2355
-		if (function_exists('template_' . $template_name . '_init'))
2356
-			call_user_func('template_' . $template_name . '_init');
2489
+		if (function_exists('template_' . $template_name . '_init')) {
2490
+					call_user_func('template_' . $template_name . '_init');
2491
+		}
2357 2492
 	}
2358 2493
 	// Hmmm... doesn't exist?!  I don't suppose the directory is wrong, is it?
2359 2494
 	elseif (!file_exists($settings['default_theme_dir']) && file_exists($boarddir . '/Themes/default'))
@@ -2373,13 +2508,14 @@  discard block
 block discarded – undo
2373 2508
 		loadTemplate($template_name);
2374 2509
 	}
2375 2510
 	// Cause an error otherwise.
2376
-	elseif ($template_name != 'Errors' && $template_name != 'index' && $fatal)
2377
-		fatal_lang_error('theme_template_error', 'template', array((string) $template_name));
2378
-	elseif ($fatal)
2379
-		die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load Themes/default/%s.template.php!', (string) $template_name), 'template'));
2380
-	else
2381
-		return false;
2382
-}
2511
+	elseif ($template_name != 'Errors' && $template_name != 'index' && $fatal) {
2512
+			fatal_lang_error('theme_template_error', 'template', array((string) $template_name));
2513
+	} elseif ($fatal) {
2514
+			die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load Themes/default/%s.template.php!', (string) $template_name), 'template'));
2515
+	} else {
2516
+			return false;
2517
+	}
2518
+	}
2383 2519
 
2384 2520
 /**
2385 2521
  * Load a sub-template.
@@ -2397,17 +2533,19 @@  discard block
 block discarded – undo
2397 2533
 {
2398 2534
 	global $context, $txt, $db_show_debug;
2399 2535
 
2400
-	if ($db_show_debug === true)
2401
-		$context['debug']['sub_templates'][] = $sub_template_name;
2536
+	if ($db_show_debug === true) {
2537
+			$context['debug']['sub_templates'][] = $sub_template_name;
2538
+	}
2402 2539
 
2403 2540
 	// Figure out what the template function is named.
2404 2541
 	$theme_function = 'template_' . $sub_template_name;
2405
-	if (function_exists($theme_function))
2406
-		$theme_function();
2407
-	elseif ($fatal === false)
2408
-		fatal_lang_error('theme_template_error', 'template', array((string) $sub_template_name));
2409
-	elseif ($fatal !== 'ignore')
2410
-		die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load the %s sub template!', (string) $sub_template_name), 'template'));
2542
+	if (function_exists($theme_function)) {
2543
+			$theme_function();
2544
+	} elseif ($fatal === false) {
2545
+			fatal_lang_error('theme_template_error', 'template', array((string) $sub_template_name));
2546
+	} elseif ($fatal !== 'ignore') {
2547
+			die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load the %s sub template!', (string) $sub_template_name), 'template'));
2548
+	}
2411 2549
 
2412 2550
 	// Are we showing debugging for templates?  Just make sure not to do it before the doctype...
2413 2551
 	if (allowedTo('admin_forum') && isset($_REQUEST['debug']) && !in_array($sub_template_name, array('init', 'main_below')) && ob_get_length() > 0 && !isset($_REQUEST['xml']))
@@ -2437,8 +2575,9 @@  discard block
 block discarded – undo
2437 2575
 {
2438 2576
 	global $settings, $context, $modSettings;
2439 2577
 
2440
-	if (empty($context['css_files_order']))
2441
-		$context['css_files_order'] = array();
2578
+	if (empty($context['css_files_order'])) {
2579
+			$context['css_files_order'] = array();
2580
+	}
2442 2581
 
2443 2582
 	$params['seed'] = (!array_key_exists('seed', $params) || (array_key_exists('seed', $params) && $params['seed'] === true)) ? (array_key_exists('browser_cache', $modSettings) ? $modSettings['browser_cache'] : '') : (is_string($params['seed']) ? ($params['seed'] = $params['seed'][0] === '?' ? $params['seed'] : '?' . $params['seed']) : '');
2444 2583
 	$params['force_current'] = isset($params['force_current']) ? $params['force_current'] : false;
@@ -2449,8 +2588,9 @@  discard block
 block discarded – undo
2449 2588
 	$params['order_pos'] = isset($params['order_pos']) ? (int) $params['order_pos'] : 3000;
2450 2589
 
2451 2590
 	// If this is an external file, automatically set this to false.
2452
-	if (!empty($params['external']))
2453
-		$params['minimize'] = false;
2591
+	if (!empty($params['external'])) {
2592
+			$params['minimize'] = false;
2593
+	}
2454 2594
 
2455 2595
 	// Account for shorthand like admin.css?alp21 filenames
2456 2596
 	$has_seed = strpos($fileName, '.css?');
@@ -2467,16 +2607,12 @@  discard block
 block discarded – undo
2467 2607
 			{
2468 2608
 				$fileUrl = $settings['default_theme_url'] . '/css/' . $fileName . ($has_seed ? '' : $params['seed']);
2469 2609
 				$filePath = $settings['default_theme_dir'] . '/css/' . $fileName . ($has_seed ? '' : $params['seed']);
2470
-			}
2471
-
2472
-			else
2610
+			} else
2473 2611
 			{
2474 2612
 				$fileUrl = false;
2475 2613
 				$filePath = false;
2476 2614
 			}
2477
-		}
2478
-
2479
-		else
2615
+		} else
2480 2616
 		{
2481 2617
 			$fileUrl = $settings[$themeRef . '_url'] . '/css/' . $fileName . ($has_seed ? '' : $params['seed']);
2482 2618
 			$filePath = $settings[$themeRef . '_dir'] . '/css/' . $fileName . ($has_seed ? '' : $params['seed']);
@@ -2494,16 +2630,18 @@  discard block
 block discarded – undo
2494 2630
 	if (!empty($fileName))
2495 2631
 	{
2496 2632
 		// find a free number/position
2497
-		while (isset($context['css_files_order'][$params['order_pos']]))
2498
-			$params['order_pos']++;
2633
+		while (isset($context['css_files_order'][$params['order_pos']])) {
2634
+					$params['order_pos']++;
2635
+		}
2499 2636
 		$context['css_files_order'][$params['order_pos']] = $id;
2500 2637
 
2501 2638
 		$context['css_files'][$id] = array('fileUrl' => $fileUrl, 'filePath' => $filePath, 'fileName' => $fileName, 'options' => $params);
2502 2639
 	}
2503 2640
 
2504
-	if (!empty($context['right_to_left']) && !empty($params['rtl']))
2505
-		loadCSSFile($params['rtl'], array_diff_key($params, array('rtl' => 0)));
2506
-}
2641
+	if (!empty($context['right_to_left']) && !empty($params['rtl'])) {
2642
+			loadCSSFile($params['rtl'], array_diff_key($params, array('rtl' => 0)));
2643
+	}
2644
+	}
2507 2645
 
2508 2646
 /**
2509 2647
  * Add a block of inline css code to be executed later
@@ -2520,8 +2658,9 @@  discard block
 block discarded – undo
2520 2658
 	global $context;
2521 2659
 
2522 2660
 	// Gotta add something...
2523
-	if (empty($css))
2524
-		return false;
2661
+	if (empty($css)) {
2662
+			return false;
2663
+	}
2525 2664
 
2526 2665
 	$context['css_header'][] = $css;
2527 2666
 }
@@ -2557,8 +2696,9 @@  discard block
 block discarded – undo
2557 2696
 	$params['validate'] = isset($params['validate']) ? $params['validate'] : true;
2558 2697
 
2559 2698
 	// If this is an external file, automatically set this to false.
2560
-	if (!empty($params['external']))
2561
-		$params['minimize'] = false;
2699
+	if (!empty($params['external'])) {
2700
+			$params['minimize'] = false;
2701
+	}
2562 2702
 
2563 2703
 	// Account for shorthand like admin.js?alp21 filenames
2564 2704
 	$has_seed = strpos($fileName, '.js?');
@@ -2575,16 +2715,12 @@  discard block
 block discarded – undo
2575 2715
 			{
2576 2716
 				$fileUrl = $settings['default_theme_url'] . '/scripts/' . $fileName . ($has_seed ? '' : $params['seed']);
2577 2717
 				$filePath = $settings['default_theme_dir'] . '/scripts/' . $fileName . ($has_seed ? '' : $params['seed']);
2578
-			}
2579
-
2580
-			else
2718
+			} else
2581 2719
 			{
2582 2720
 				$fileUrl = false;
2583 2721
 				$filePath = false;
2584 2722
 			}
2585
-		}
2586
-
2587
-		else
2723
+		} else
2588 2724
 		{
2589 2725
 			$fileUrl = $settings[$themeRef . '_url'] . '/scripts/' . $fileName . ($has_seed ? '' : $params['seed']);
2590 2726
 			$filePath = $settings[$themeRef . '_dir'] . '/scripts/' . $fileName . ($has_seed ? '' : $params['seed']);
@@ -2599,9 +2735,10 @@  discard block
 block discarded – undo
2599 2735
 	}
2600 2736
 
2601 2737
 	// Add it to the array for use in the template
2602
-	if (!empty($fileName))
2603
-		$context['javascript_files'][$id] = array('fileUrl' => $fileUrl, 'filePath' => $filePath, 'fileName' => $fileName, 'options' => $params);
2604
-}
2738
+	if (!empty($fileName)) {
2739
+			$context['javascript_files'][$id] = array('fileUrl' => $fileUrl, 'filePath' => $filePath, 'fileName' => $fileName, 'options' => $params);
2740
+	}
2741
+	}
2605 2742
 
2606 2743
 /**
2607 2744
  * Add a Javascript variable for output later (for feeding text strings and similar to JS)
@@ -2615,9 +2752,10 @@  discard block
 block discarded – undo
2615 2752
 {
2616 2753
 	global $context;
2617 2754
 
2618
-	if (!empty($key) && (!empty($value) || $value === '0'))
2619
-		$context['javascript_vars'][$key] = !empty($escape) ? JavaScriptEscape($value) : $value;
2620
-}
2755
+	if (!empty($key) && (!empty($value) || $value === '0')) {
2756
+			$context['javascript_vars'][$key] = !empty($escape) ? JavaScriptEscape($value) : $value;
2757
+	}
2758
+	}
2621 2759
 
2622 2760
 /**
2623 2761
  * Add a block of inline Javascript code to be executed later
@@ -2634,8 +2772,9 @@  discard block
 block discarded – undo
2634 2772
 {
2635 2773
 	global $context;
2636 2774
 
2637
-	if (empty($javascript))
2638
-		return false;
2775
+	if (empty($javascript)) {
2776
+			return false;
2777
+	}
2639 2778
 
2640 2779
 	$context['javascript_inline'][($defer === true ? 'defer' : 'standard')][] = $javascript;
2641 2780
 }
@@ -2656,15 +2795,18 @@  discard block
 block discarded – undo
2656 2795
 	static $already_loaded = array();
2657 2796
 
2658 2797
 	// Default to the user's language.
2659
-	if ($lang == '')
2660
-		$lang = isset($user_info['language']) ? $user_info['language'] : $language;
2798
+	if ($lang == '') {
2799
+			$lang = isset($user_info['language']) ? $user_info['language'] : $language;
2800
+	}
2661 2801
 
2662 2802
 	// Do we want the English version of language file as fallback?
2663
-	if (empty($modSettings['disable_language_fallback']) && $lang != 'english')
2664
-		loadLanguage($template_name, 'english', false);
2803
+	if (empty($modSettings['disable_language_fallback']) && $lang != 'english') {
2804
+			loadLanguage($template_name, 'english', false);
2805
+	}
2665 2806
 
2666
-	if (!$force_reload && isset($already_loaded[$template_name]) && $already_loaded[$template_name] == $lang)
2667
-		return $lang;
2807
+	if (!$force_reload && isset($already_loaded[$template_name]) && $already_loaded[$template_name] == $lang) {
2808
+			return $lang;
2809
+	}
2668 2810
 
2669 2811
 	// Make sure we have $settings - if not we're in trouble and need to find it!
2670 2812
 	if (empty($settings['default_theme_dir']))
@@ -2675,8 +2817,9 @@  discard block
 block discarded – undo
2675 2817
 
2676 2818
 	// What theme are we in?
2677 2819
 	$theme_name = basename($settings['theme_url']);
2678
-	if (empty($theme_name))
2679
-		$theme_name = 'unknown';
2820
+	if (empty($theme_name)) {
2821
+			$theme_name = 'unknown';
2822
+	}
2680 2823
 
2681 2824
 	// For each file open it up and write it out!
2682 2825
 	foreach (explode('+', $template_name) as $template)
@@ -2718,8 +2861,9 @@  discard block
 block discarded – undo
2718 2861
 				$found = true;
2719 2862
 
2720 2863
 				// setlocale is required for basename() & pathinfo() to work properly on the selected language
2721
-				if (!empty($txt['lang_locale']) && !empty($modSettings['global_character_set']))
2722
-					setlocale(LC_CTYPE, $txt['lang_locale'] . '.' . $modSettings['global_character_set']);
2864
+				if (!empty($txt['lang_locale']) && !empty($modSettings['global_character_set'])) {
2865
+									setlocale(LC_CTYPE, $txt['lang_locale'] . '.' . $modSettings['global_character_set']);
2866
+				}
2723 2867
 
2724 2868
 				break;
2725 2869
 			}
@@ -2759,8 +2903,9 @@  discard block
 block discarded – undo
2759 2903
 	}
2760 2904
 
2761 2905
 	// Keep track of what we're up to soldier.
2762
-	if ($db_show_debug === true)
2763
-		$context['debug']['language_files'][] = $template_name . '.' . $lang . ' (' . $theme_name . ')';
2906
+	if ($db_show_debug === true) {
2907
+			$context['debug']['language_files'][] = $template_name . '.' . $lang . ' (' . $theme_name . ')';
2908
+	}
2764 2909
 
2765 2910
 	// Remember what we have loaded, and in which language.
2766 2911
 	$already_loaded[$template_name] = $lang;
@@ -2806,8 +2951,9 @@  discard block
 block discarded – undo
2806 2951
 				)
2807 2952
 			);
2808 2953
 			// In the EXTREMELY unlikely event this happens, give an error message.
2809
-			if ($smcFunc['db_num_rows']($result) == 0)
2810
-				fatal_lang_error('parent_not_found', 'critical');
2954
+			if ($smcFunc['db_num_rows']($result) == 0) {
2955
+							fatal_lang_error('parent_not_found', 'critical');
2956
+			}
2811 2957
 			while ($row = $smcFunc['db_fetch_assoc']($result))
2812 2958
 			{
2813 2959
 				if (!isset($boards[$row['id_board']]))
@@ -2824,8 +2970,8 @@  discard block
 block discarded – undo
2824 2970
 					);
2825 2971
 				}
2826 2972
 				// If a moderator exists for this board, add that moderator for all children too.
2827
-				if (!empty($row['id_moderator']))
2828
-					foreach ($boards as $id => $dummy)
2973
+				if (!empty($row['id_moderator'])) {
2974
+									foreach ($boards as $id => $dummy)
2829 2975
 					{
2830 2976
 						$boards[$id]['moderators'][$row['id_moderator']] = array(
2831 2977
 							'id' => $row['id_moderator'],
@@ -2833,11 +2979,12 @@  discard block
 block discarded – undo
2833 2979
 							'href' => $scripturl . '?action=profile;u=' . $row['id_moderator'],
2834 2980
 							'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_moderator'] . '">' . $row['real_name'] . '</a>'
2835 2981
 						);
2982
+				}
2836 2983
 					}
2837 2984
 
2838 2985
 				// If a moderator group exists for this board, add that moderator group for all children too
2839
-				if (!empty($row['id_moderator_group']))
2840
-					foreach ($boards as $id => $dummy)
2986
+				if (!empty($row['id_moderator_group'])) {
2987
+									foreach ($boards as $id => $dummy)
2841 2988
 					{
2842 2989
 						$boards[$id]['moderator_groups'][$row['id_moderator_group']] = array(
2843 2990
 							'id' => $row['id_moderator_group'],
@@ -2845,6 +2992,7 @@  discard block
 block discarded – undo
2845 2992
 							'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'],
2846 2993
 							'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'] . '">' . $row['group_name'] . '</a>'
2847 2994
 						);
2995
+				}
2848 2996
 					}
2849 2997
 			}
2850 2998
 			$smcFunc['db_free_result']($result);
@@ -2871,23 +3019,27 @@  discard block
 block discarded – undo
2871 3019
 	if (!$use_cache || ($context['languages'] = cache_get_data('known_languages', !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600)) == null)
2872 3020
 	{
2873 3021
 		// If we don't have our ucwords function defined yet, let's load the settings data.
2874
-		if (empty($smcFunc['ucwords']))
2875
-			reloadSettings();
3022
+		if (empty($smcFunc['ucwords'])) {
3023
+					reloadSettings();
3024
+		}
2876 3025
 
2877 3026
 		// If we don't have our theme information yet, let's get it.
2878
-		if (empty($settings['default_theme_dir']))
2879
-			loadTheme(0, false);
3027
+		if (empty($settings['default_theme_dir'])) {
3028
+					loadTheme(0, false);
3029
+		}
2880 3030
 
2881 3031
 		// Default language directories to try.
2882 3032
 		$language_directories = array(
2883 3033
 			$settings['default_theme_dir'] . '/languages',
2884 3034
 		);
2885
-		if (!empty($settings['actual_theme_dir']) && $settings['actual_theme_dir'] != $settings['default_theme_dir'])
2886
-			$language_directories[] = $settings['actual_theme_dir'] . '/languages';
3035
+		if (!empty($settings['actual_theme_dir']) && $settings['actual_theme_dir'] != $settings['default_theme_dir']) {
3036
+					$language_directories[] = $settings['actual_theme_dir'] . '/languages';
3037
+		}
2887 3038
 
2888 3039
 		// We possibly have a base theme directory.
2889
-		if (!empty($settings['base_theme_dir']))
2890
-			$language_directories[] = $settings['base_theme_dir'] . '/languages';
3040
+		if (!empty($settings['base_theme_dir'])) {
3041
+					$language_directories[] = $settings['base_theme_dir'] . '/languages';
3042
+		}
2891 3043
 
2892 3044
 		// Remove any duplicates.
2893 3045
 		$language_directories = array_unique($language_directories);
@@ -2901,20 +3053,21 @@  discard block
 block discarded – undo
2901 3053
 		foreach ($language_directories as $language_dir)
2902 3054
 		{
2903 3055
 			// Can't look in here... doesn't exist!
2904
-			if (!file_exists($language_dir))
2905
-				continue;
3056
+			if (!file_exists($language_dir)) {
3057
+							continue;
3058
+			}
2906 3059
 
2907 3060
 			$dir = dir($language_dir);
2908 3061
 			while ($entry = $dir->read())
2909 3062
 			{
2910 3063
 				// Look for the index language file... For good measure skip any "index.language-utf8.php" files
2911
-				if (!preg_match('~^index\.(.+[^-utf8])\.php$~', $entry, $matches))
2912
-					continue;
2913
-
2914
-				if (!empty($langList) && !empty($langList[$matches[1]]))
2915
-					$langName = $langList[$matches[1]];
3064
+				if (!preg_match('~^index\.(.+[^-utf8])\.php$~', $entry, $matches)) {
3065
+									continue;
3066
+				}
2916 3067
 
2917
-				else
3068
+				if (!empty($langList) && !empty($langList[$matches[1]])) {
3069
+									$langName = $langList[$matches[1]];
3070
+				} else
2918 3071
 				{
2919 3072
 					$langName = $smcFunc['ucwords'](strtr($matches[1], array('_' => ' ')));
2920 3073
 
@@ -2955,12 +3108,14 @@  discard block
 block discarded – undo
2955 3108
 		}
2956 3109
 
2957 3110
 		// Do we need to store the lang list?
2958
-		if (empty($langList))
2959
-			updateSettings(array('langList' => $smcFunc['json_encode']($catchLang)));
3111
+		if (empty($langList)) {
3112
+					updateSettings(array('langList' => $smcFunc['json_encode']($catchLang)));
3113
+		}
2960 3114
 
2961 3115
 		// Let's cash in on this deal.
2962
-		if (!empty($modSettings['cache_enable']))
2963
-			cache_put_data('known_languages', $context['languages'], !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
3116
+		if (!empty($modSettings['cache_enable'])) {
3117
+					cache_put_data('known_languages', $context['languages'], !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
3118
+		}
2964 3119
 	}
2965 3120
 
2966 3121
 	return $context['languages'];
@@ -2983,8 +3138,9 @@  discard block
 block discarded – undo
2983 3138
 	global $modSettings, $options, $txt;
2984 3139
 	static $censor_vulgar = null, $censor_proper;
2985 3140
 
2986
-	if ((!empty($options['show_no_censored']) && !empty($modSettings['allow_no_censored']) && !$force) || empty($modSettings['censor_vulgar']) || trim($text) === '')
2987
-		return $text;
3141
+	if ((!empty($options['show_no_censored']) && !empty($modSettings['allow_no_censored']) && !$force) || empty($modSettings['censor_vulgar']) || trim($text) === '') {
3142
+			return $text;
3143
+	}
2988 3144
 
2989 3145
 	// If they haven't yet been loaded, load them.
2990 3146
 	if ($censor_vulgar == null)
@@ -3015,9 +3171,9 @@  discard block
 block discarded – undo
3015 3171
 	{
3016 3172
 		$func = !empty($modSettings['censorIgnoreCase']) ? 'str_ireplace' : 'str_replace';
3017 3173
 		$text = $func($censor_vulgar, $censor_proper, $text);
3174
+	} else {
3175
+			$text = preg_replace($censor_vulgar, $censor_proper, $text);
3018 3176
 	}
3019
-	else
3020
-		$text = preg_replace($censor_vulgar, $censor_proper, $text);
3021 3177
 
3022 3178
 	return $text;
3023 3179
 }
@@ -3043,30 +3199,35 @@  discard block
 block discarded – undo
3043 3199
 	@ini_set('track_errors', '1');
3044 3200
 
3045 3201
 	// Don't include the file more than once, if $once is true.
3046
-	if ($once && in_array($filename, $templates))
3047
-		return;
3202
+	if ($once && in_array($filename, $templates)) {
3203
+			return;
3204
+	}
3048 3205
 	// Add this file to the include list, whether $once is true or not.
3049
-	else
3050
-		$templates[] = $filename;
3206
+	else {
3207
+			$templates[] = $filename;
3208
+	}
3051 3209
 
3052 3210
 
3053 3211
 	$file_found = file_exists($filename);
3054 3212
 
3055
-	if ($once && $file_found)
3056
-		require_once($filename);
3057
-	elseif ($file_found)
3058
-		require($filename);
3213
+	if ($once && $file_found) {
3214
+			require_once($filename);
3215
+	} elseif ($file_found) {
3216
+			require($filename);
3217
+	}
3059 3218
 
3060 3219
 	if ($file_found !== true)
3061 3220
 	{
3062 3221
 		ob_end_clean();
3063
-		if (!empty($modSettings['enableCompressedOutput']))
3064
-			@ob_start('ob_gzhandler');
3065
-		else
3066
-			ob_start();
3222
+		if (!empty($modSettings['enableCompressedOutput'])) {
3223
+					@ob_start('ob_gzhandler');
3224
+		} else {
3225
+					ob_start();
3226
+		}
3067 3227
 
3068
-		if (isset($_GET['debug']))
3069
-			header('content-type: application/xhtml+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
3228
+		if (isset($_GET['debug'])) {
3229
+					header('content-type: application/xhtml+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
3230
+		}
3070 3231
 
3071 3232
 		// Don't cache error pages!!
3072 3233
 		header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
@@ -3085,12 +3246,13 @@  discard block
 block discarded – undo
3085 3246
 		echo '<!DOCTYPE html>
3086 3247
 <html', !empty($context['right_to_left']) ? ' dir="rtl"' : '', '>
3087 3248
 	<head>';
3088
-		if (isset($context['character_set']))
3089
-			echo '
3249
+		if (isset($context['character_set'])) {
3250
+					echo '
3090 3251
 		<meta charset="', $context['character_set'], '">';
3252
+		}
3091 3253
 
3092
-		if (!empty($maintenance) && !allowedTo('admin_forum'))
3093
-			echo '
3254
+		if (!empty($maintenance) && !allowedTo('admin_forum')) {
3255
+					echo '
3094 3256
 		<title>', $mtitle, '</title>
3095 3257
 	</head>
3096 3258
 	<body>
@@ -3098,8 +3260,8 @@  discard block
 block discarded – undo
3098 3260
 		', $mmessage, '
3099 3261
 	</body>
3100 3262
 </html>';
3101
-		elseif (!allowedTo('admin_forum'))
3102
-			echo '
3263
+		} elseif (!allowedTo('admin_forum')) {
3264
+					echo '
3103 3265
 		<title>', $txt['template_parse_error'], '</title>
3104 3266
 	</head>
3105 3267
 	<body>
@@ -3107,14 +3269,16 @@  discard block
 block discarded – undo
3107 3269
 		', $txt['template_parse_error_message'], '
3108 3270
 	</body>
3109 3271
 </html>';
3110
-		else
3272
+		} else
3111 3273
 		{
3112 3274
 			$error = fetch_web_data($boardurl . strtr($filename, array($boarddir => '', strtr($boarddir, '\\', '/') => '')));
3113 3275
 			$error_array = error_get_last();
3114
-			if (empty($error) && ini_get('track_errors') && !empty($error_array))
3115
-				$error = $error_array['message'];
3116
-			if (empty($error))
3117
-				$error = $txt['template_parse_errmsg'];
3276
+			if (empty($error) && ini_get('track_errors') && !empty($error_array)) {
3277
+							$error = $error_array['message'];
3278
+			}
3279
+			if (empty($error)) {
3280
+							$error = $txt['template_parse_errmsg'];
3281
+			}
3118 3282
 
3119 3283
 			$error = strtr($error, array('<b>' => '<strong>', '</b>' => '</strong>'));
3120 3284
 
@@ -3125,11 +3289,12 @@  discard block
 block discarded – undo
3125 3289
 		<h3>', $txt['template_parse_error'], '</h3>
3126 3290
 		', sprintf($txt['template_parse_error_details'], strtr($filename, array($boarddir => '', strtr($boarddir, '\\', '/') => '')));
3127 3291
 
3128
-			if (!empty($error))
3129
-				echo '
3292
+			if (!empty($error)) {
3293
+							echo '
3130 3294
 		<hr>
3131 3295
 
3132 3296
 		<div style="margin: 0 20px;"><pre>', strtr(strtr($error, array('<strong>' . $boarddir => '<strong>...', '<strong>' . strtr($boarddir, '\\', '/') => '<strong>...')), '\\', '/'), '</pre></div>';
3297
+			}
3133 3298
 
3134 3299
 			// I know, I know... this is VERY COMPLICATED.  Still, it's good.
3135 3300
 			if (preg_match('~ <strong>(\d+)</strong><br( /)?' . '>$~i', $error, $match) != 0)
@@ -3139,10 +3304,11 @@  discard block
 block discarded – undo
3139 3304
 				$data2 = preg_split('~\<br( /)?\>~', $data2);
3140 3305
 
3141 3306
 				// Fix the PHP code stuff...
3142
-				if (!isBrowser('gecko'))
3143
-					$data2 = str_replace("\t", '<span style="white-space: pre;">' . "\t" . '</span>', $data2);
3144
-				else
3145
-					$data2 = str_replace('<pre style="display: inline;">' . "\t" . '</pre>', "\t", $data2);
3307
+				if (!isBrowser('gecko')) {
3308
+									$data2 = str_replace("\t", '<span style="white-space: pre;">' . "\t" . '</span>', $data2);
3309
+				} else {
3310
+									$data2 = str_replace('<pre style="display: inline;">' . "\t" . '</pre>', "\t", $data2);
3311
+				}
3146 3312
 
3147 3313
 				// Now we get to work around a bug in PHP where it doesn't escape <br>s!
3148 3314
 				$j = -1;
@@ -3150,8 +3316,9 @@  discard block
 block discarded – undo
3150 3316
 				{
3151 3317
 					$j++;
3152 3318
 
3153
-					if (substr_count($line, '<br>') == 0)
3154
-						continue;
3319
+					if (substr_count($line, '<br>') == 0) {
3320
+											continue;
3321
+					}
3155 3322
 
3156 3323
 					$n = substr_count($line, '<br>');
3157 3324
 					for ($i = 0; $i < $n; $i++)
@@ -3170,38 +3337,42 @@  discard block
 block discarded – undo
3170 3337
 				// Figure out what the color coding was before...
3171 3338
 				$line = max($match[1] - 9, 1);
3172 3339
 				$last_line = '';
3173
-				for ($line2 = $line - 1; $line2 > 1; $line2--)
3174
-					if (strpos($data2[$line2], '<') !== false)
3340
+				for ($line2 = $line - 1; $line2 > 1; $line2--) {
3341
+									if (strpos($data2[$line2], '<') !== false)
3175 3342
 					{
3176 3343
 						if (preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line2], $color_match) != 0)
3177 3344
 							$last_line = $color_match[1];
3345
+				}
3178 3346
 						break;
3179 3347
 					}
3180 3348
 
3181 3349
 				// Show the relevant lines...
3182 3350
 				for ($n = min($match[1] + 4, count($data2) + 1); $line <= $n; $line++)
3183 3351
 				{
3184
-					if ($line == $match[1])
3185
-						echo '</pre><div style="background-color: #ffb0b5;"><pre style="margin: 0;">';
3352
+					if ($line == $match[1]) {
3353
+											echo '</pre><div style="background-color: #ffb0b5;"><pre style="margin: 0;">';
3354
+					}
3186 3355
 
3187 3356
 					echo '<span style="color: black;">', sprintf('%' . strlen($n) . 's', $line), ':</span> ';
3188
-					if (isset($data2[$line]) && $data2[$line] != '')
3189
-						echo substr($data2[$line], 0, 2) == '</' ? preg_replace('~^</[^>]+>~', '', $data2[$line]) : $last_line . $data2[$line];
3357
+					if (isset($data2[$line]) && $data2[$line] != '') {
3358
+											echo substr($data2[$line], 0, 2) == '</' ? preg_replace('~^</[^>]+>~', '', $data2[$line]) : $last_line . $data2[$line];
3359
+					}
3190 3360
 
3191 3361
 					if (isset($data2[$line]) && preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line], $color_match) != 0)
3192 3362
 					{
3193 3363
 						$last_line = $color_match[1];
3194 3364
 						echo '</', substr($last_line, 1, 4), '>';
3365
+					} elseif ($last_line != '' && strpos($data2[$line], '<') !== false) {
3366
+											$last_line = '';
3367
+					} elseif ($last_line != '' && $data2[$line] != '') {
3368
+											echo '</', substr($last_line, 1, 4), '>';
3195 3369
 					}
3196
-					elseif ($last_line != '' && strpos($data2[$line], '<') !== false)
3197
-						$last_line = '';
3198
-					elseif ($last_line != '' && $data2[$line] != '')
3199
-						echo '</', substr($last_line, 1, 4), '>';
3200 3370
 
3201
-					if ($line == $match[1])
3202
-						echo '</pre></div><pre style="margin: 0;">';
3203
-					else
3204
-						echo "\n";
3371
+					if ($line == $match[1]) {
3372
+											echo '</pre></div><pre style="margin: 0;">';
3373
+					} else {
3374
+											echo "\n";
3375
+					}
3205 3376
 				}
3206 3377
 
3207 3378
 				echo '</pre></div>';
@@ -3225,8 +3396,9 @@  discard block
 block discarded – undo
3225 3396
 	global $db_type, $db_name, $ssi_db_user, $ssi_db_passwd, $sourcedir, $db_prefix, $db_port, $db_mb4;
3226 3397
 
3227 3398
 	// Figure out what type of database we are using.
3228
-	if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
3229
-		$db_type = 'mysql';
3399
+	if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php')) {
3400
+			$db_type = 'mysql';
3401
+	}
3230 3402
 
3231 3403
 	// Load the file for the database.
3232 3404
 	require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
@@ -3234,11 +3406,13 @@  discard block
 block discarded – undo
3234 3406
 	$db_options = array();
3235 3407
 
3236 3408
 	// Add in the port if needed
3237
-	if (!empty($db_port))
3238
-		$db_options['port'] = $db_port;
3409
+	if (!empty($db_port)) {
3410
+			$db_options['port'] = $db_port;
3411
+	}
3239 3412
 
3240
-	if (!empty($db_mb4))
3241
-		$db_options['db_mb4'] = $db_mb4;
3413
+	if (!empty($db_mb4)) {
3414
+			$db_options['db_mb4'] = $db_mb4;
3415
+	}
3242 3416
 
3243 3417
 	// If we are in SSI try them first, but don't worry if it doesn't work, we have the normal username and password we can use.
3244 3418
 	if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
@@ -3257,13 +3431,15 @@  discard block
 block discarded – undo
3257 3431
 	}
3258 3432
 
3259 3433
 	// Safe guard here, if there isn't a valid connection lets put a stop to it.
3260
-	if (!$db_connection)
3261
-		display_db_error();
3434
+	if (!$db_connection) {
3435
+			display_db_error();
3436
+	}
3262 3437
 
3263 3438
 	// If in SSI mode fix up the prefix.
3264
-	if (SMF == 'SSI')
3265
-		db_fix_prefix($db_prefix, $db_name);
3266
-}
3439
+	if (SMF == 'SSI') {
3440
+			db_fix_prefix($db_prefix, $db_name);
3441
+	}
3442
+	}
3267 3443
 
3268 3444
 /**
3269 3445
  * Try to load up a supported caching method. This is saved in $cacheAPI if we are not overriding it.
@@ -3277,10 +3453,11 @@  discard block
 block discarded – undo
3277 3453
 	global $sourcedir, $cacheAPI, $cache_accelerator;
3278 3454
 
3279 3455
 	// Not overriding this and we have a cacheAPI, send it back.
3280
-	if (empty($overrideCache) && is_object($cacheAPI))
3281
-		return $cacheAPI;
3282
-	elseif (is_null($cacheAPI))
3283
-		$cacheAPI = false;
3456
+	if (empty($overrideCache) && is_object($cacheAPI)) {
3457
+			return $cacheAPI;
3458
+	} elseif (is_null($cacheAPI)) {
3459
+			$cacheAPI = false;
3460
+	}
3284 3461
 
3285 3462
 	// Make sure our class is in session.
3286 3463
 	require_once($sourcedir . '/Class-CacheAPI.php');
@@ -3301,8 +3478,9 @@  discard block
 block discarded – undo
3301 3478
 		if (!$testAPI->isSupported())
3302 3479
 		{
3303 3480
 			// Can we save ourselves?
3304
-			if (!empty($fallbackSMF) && is_null($overrideCache) && $tryAccelerator != 'smf')
3305
-				return loadCacheAccelerator(null, false);
3481
+			if (!empty($fallbackSMF) && is_null($overrideCache) && $tryAccelerator != 'smf') {
3482
+							return loadCacheAccelerator(null, false);
3483
+			}
3306 3484
 			return false;
3307 3485
 		}
3308 3486
 
@@ -3314,9 +3492,9 @@  discard block
 block discarded – undo
3314 3492
 		{
3315 3493
 			$cacheAPI = $testAPI;
3316 3494
 			return $cacheAPI;
3495
+		} else {
3496
+					return $testAPI;
3317 3497
 		}
3318
-		else
3319
-			return $testAPI;
3320 3498
 	}
3321 3499
 }
3322 3500
 
@@ -3336,8 +3514,9 @@  discard block
 block discarded – undo
3336 3514
 
3337 3515
 	// @todo Why are we doing this if caching is disabled?
3338 3516
 
3339
-	if (function_exists('call_integration_hook'))
3340
-		call_integration_hook('pre_cache_quick_get', array(&$key, &$file, &$function, &$params, &$level));
3517
+	if (function_exists('call_integration_hook')) {
3518
+			call_integration_hook('pre_cache_quick_get', array(&$key, &$file, &$function, &$params, &$level));
3519
+	}
3341 3520
 
3342 3521
 	/* Refresh the cache if either:
3343 3522
 		1. Caching is disabled.
@@ -3351,16 +3530,19 @@  discard block
 block discarded – undo
3351 3530
 		require_once($sourcedir . '/' . $file);
3352 3531
 		$cache_block = call_user_func_array($function, $params);
3353 3532
 
3354
-		if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= $level)
3355
-			cache_put_data($key, $cache_block, $cache_block['expires'] - time());
3533
+		if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= $level) {
3534
+					cache_put_data($key, $cache_block, $cache_block['expires'] - time());
3535
+		}
3356 3536
 	}
3357 3537
 
3358 3538
 	// Some cached data may need a freshening up after retrieval.
3359
-	if (!empty($cache_block['post_retri_eval']))
3360
-		eval($cache_block['post_retri_eval']);
3539
+	if (!empty($cache_block['post_retri_eval'])) {
3540
+			eval($cache_block['post_retri_eval']);
3541
+	}
3361 3542
 
3362
-	if (function_exists('call_integration_hook'))
3363
-		call_integration_hook('post_cache_quick_get', array(&$cache_block));
3543
+	if (function_exists('call_integration_hook')) {
3544
+			call_integration_hook('post_cache_quick_get', array(&$cache_block));
3545
+	}
3364 3546
 
3365 3547
 	return $cache_block['data'];
3366 3548
 }
@@ -3387,8 +3569,9 @@  discard block
 block discarded – undo
3387 3569
 	global $smcFunc, $cache_enable, $cacheAPI;
3388 3570
 	global $cache_hits, $cache_count, $db_show_debug;
3389 3571
 
3390
-	if (empty($cache_enable) || empty($cacheAPI))
3391
-		return;
3572
+	if (empty($cache_enable) || empty($cacheAPI)) {
3573
+			return;
3574
+	}
3392 3575
 
3393 3576
 	$cache_count = isset($cache_count) ? $cache_count + 1 : 1;
3394 3577
 	if (isset($db_show_debug) && $db_show_debug === true)
@@ -3401,12 +3584,14 @@  discard block
 block discarded – undo
3401 3584
 	$value = $value === null ? null : (isset($smcFunc['json_encode']) ? $smcFunc['json_encode']($value) : json_encode($value));
3402 3585
 	$cacheAPI->putData($key, $value, $ttl);
3403 3586
 
3404
-	if (function_exists('call_integration_hook'))
3405
-		call_integration_hook('cache_put_data', array(&$key, &$value, &$ttl));
3587
+	if (function_exists('call_integration_hook')) {
3588
+			call_integration_hook('cache_put_data', array(&$key, &$value, &$ttl));
3589
+	}
3406 3590
 
3407
-	if (isset($db_show_debug) && $db_show_debug === true)
3408
-		$cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
3409
-}
3591
+	if (isset($db_show_debug) && $db_show_debug === true) {
3592
+			$cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
3593
+	}
3594
+	}
3410 3595
 
3411 3596
 /**
3412 3597
  * Gets the value from the cache specified by key, so long as it is not older than ttl seconds.
@@ -3422,8 +3607,9 @@  discard block
 block discarded – undo
3422 3607
 	global $smcFunc, $cache_enable, $cacheAPI;
3423 3608
 	global $cache_hits, $cache_count, $cache_misses, $cache_count_misses, $db_show_debug;
3424 3609
 
3425
-	if (empty($cache_enable) || empty($cacheAPI))
3426
-		return;
3610
+	if (empty($cache_enable) || empty($cacheAPI)) {
3611
+			return;
3612
+	}
3427 3613
 
3428 3614
 	$cache_count = isset($cache_count) ? $cache_count + 1 : 1;
3429 3615
 	if (isset($db_show_debug) && $db_show_debug === true)
@@ -3443,16 +3629,18 @@  discard block
 block discarded – undo
3443 3629
 
3444 3630
 		if (empty($value))
3445 3631
 		{
3446
-			if (!is_array($cache_misses))
3447
-				$cache_misses = array();
3632
+			if (!is_array($cache_misses)) {
3633
+							$cache_misses = array();
3634
+			}
3448 3635
 
3449 3636
 			$cache_count_misses = isset($cache_count_misses) ? $cache_count_misses + 1 : 1;
3450 3637
 			$cache_misses[$cache_count_misses] = array('k' => $original_key, 'd' => 'get');
3451 3638
 		}
3452 3639
 	}
3453 3640
 
3454
-	if (function_exists('call_integration_hook') && isset($value))
3455
-		call_integration_hook('cache_get_data', array(&$key, &$ttl, &$value));
3641
+	if (function_exists('call_integration_hook') && isset($value)) {
3642
+			call_integration_hook('cache_get_data', array(&$key, &$ttl, &$value));
3643
+	}
3456 3644
 
3457 3645
 	return empty($value) ? null : (isset($smcFunc['json_decode']) ? $smcFunc['json_decode']($value, true) : smf_json_decode($value, true));
3458 3646
 }
@@ -3474,8 +3662,9 @@  discard block
 block discarded – undo
3474 3662
 	global $cacheAPI;
3475 3663
 
3476 3664
 	// If we can't get to the API, can't do this.
3477
-	if (empty($cacheAPI))
3478
-		return;
3665
+	if (empty($cacheAPI)) {
3666
+			return;
3667
+	}
3479 3668
 
3480 3669
 	// Ask the API to do the heavy lifting. cleanCache also calls invalidateCache to be sure.
3481 3670
 	$cacheAPI->cleanCache($type);
@@ -3500,8 +3689,9 @@  discard block
 block discarded – undo
3500 3689
 	global $modSettings, $boardurl, $smcFunc, $image_proxy_enabled, $user_info;
3501 3690
 
3502 3691
 	// Come on!
3503
-	if (empty($data))
3504
-		return array();
3692
+	if (empty($data)) {
3693
+			return array();
3694
+	}
3505 3695
 
3506 3696
 	// Set a nice default var.
3507 3697
 	$image = '';
@@ -3509,11 +3699,11 @@  discard block
 block discarded – undo
3509 3699
 	// Gravatar has been set as mandatory!
3510 3700
 	if (!empty($modSettings['gravatarOverride']))
3511 3701
 	{
3512
-		if (!empty($modSettings['gravatarAllowExtraEmail']) && !empty($data['avatar']) && stristr($data['avatar'], 'gravatar://'))
3513
-			$image = get_gravatar_url($smcFunc['substr']($data['avatar'], 11));
3514
-
3515
-		else if (!empty($data['email']))
3516
-			$image = get_gravatar_url($data['email']);
3702
+		if (!empty($modSettings['gravatarAllowExtraEmail']) && !empty($data['avatar']) && stristr($data['avatar'], 'gravatar://')) {
3703
+					$image = get_gravatar_url($smcFunc['substr']($data['avatar'], 11));
3704
+		} else if (!empty($data['email'])) {
3705
+					$image = get_gravatar_url($data['email']);
3706
+		}
3517 3707
 	}
3518 3708
 
3519 3709
 	// Look if the user has a gravatar field or has set an external url as avatar.
@@ -3525,54 +3715,60 @@  discard block
 block discarded – undo
3525 3715
 			// Gravatar.
3526 3716
 			if (stristr($data['avatar'], 'gravatar://'))
3527 3717
 			{
3528
-				if ($data['avatar'] == 'gravatar://')
3529
-					$image = get_gravatar_url($data['email']);
3530
-
3531
-				elseif (!empty($modSettings['gravatarAllowExtraEmail']))
3532
-					$image = get_gravatar_url($smcFunc['substr']($data['avatar'], 11));
3718
+				if ($data['avatar'] == 'gravatar://') {
3719
+									$image = get_gravatar_url($data['email']);
3720
+				} elseif (!empty($modSettings['gravatarAllowExtraEmail'])) {
3721
+									$image = get_gravatar_url($smcFunc['substr']($data['avatar'], 11));
3722
+				}
3533 3723
 			}
3534 3724
 
3535 3725
 			// External url.
3536 3726
 			else
3537 3727
 			{
3538 3728
 				// Using ssl?
3539
-				if (!empty($modSettings['force_ssl']) && $image_proxy_enabled && stripos($data['avatar'], 'http://') !== false && empty($user_info['possibly_robot']))
3540
-					$image = get_proxied_url($data['avatar']);
3729
+				if (!empty($modSettings['force_ssl']) && $image_proxy_enabled && stripos($data['avatar'], 'http://') !== false && empty($user_info['possibly_robot'])) {
3730
+									$image = get_proxied_url($data['avatar']);
3731
+				}
3541 3732
 
3542 3733
 				// Just a plain external url.
3543
-				else
3544
-					$image = (stristr($data['avatar'], 'http://') || stristr($data['avatar'], 'https://')) ? $data['avatar'] : $modSettings['avatar_url'] . '/' . $data['avatar'];
3734
+				else {
3735
+									$image = (stristr($data['avatar'], 'http://') || stristr($data['avatar'], 'https://')) ? $data['avatar'] : $modSettings['avatar_url'] . '/' . $data['avatar'];
3736
+				}
3545 3737
 			}
3546 3738
 		}
3547 3739
 
3548 3740
 		// Perhaps this user has an attachment as avatar...
3549
-		else if (!empty($data['filename']))
3550
-			$image = $modSettings['custom_avatar_url'] . '/' . $data['filename'];
3741
+		else if (!empty($data['filename'])) {
3742
+					$image = $modSettings['custom_avatar_url'] . '/' . $data['filename'];
3743
+		}
3551 3744
 
3552 3745
 		// Right... no avatar... use our default image.
3553
-		else
3554
-			$image = $modSettings['avatar_url'] . '/default.png';
3746
+		else {
3747
+					$image = $modSettings['avatar_url'] . '/default.png';
3748
+		}
3555 3749
 	}
3556 3750
 
3557 3751
 	call_integration_hook('integrate_set_avatar_data', array(&$image, &$data));
3558 3752
 
3559 3753
 	// At this point in time $image has to be filled unless you chose to force gravatar and the user doesn't have the needed data to retrieve it... thus a check for !empty() is still needed.
3560
-	if (!empty($image))
3561
-		return array(
3754
+	if (!empty($image)) {
3755
+			return array(
3562 3756
 			'name' => !empty($data['avatar']) ? $data['avatar'] : '',
3563 3757
 			'image' => '<img class="avatar" src="' . $image . '" />',
3564 3758
 			'href' => $image,
3565 3759
 			'url' => $image,
3566 3760
 		);
3761
+	}
3567 3762
 
3568 3763
 	// Fallback to make life easier for everyone...
3569
-	else
3570
-		return array(
3764
+	else {
3765
+			return array(
3571 3766
 			'name' => '',
3572 3767
 			'image' => '',
3573 3768
 			'href' => '',
3574 3769
 			'url' => '',
3575 3770
 		);
3576
-}
3771
+	}
3772
+	}
3577 3773
 
3578 3774
 ?>
3579 3775
\ No newline at end of file
Please login to merge, or discard this patch.
Sources/Session.php 1 patch
Braces   +40 added lines, -27 removed lines patch added patch discarded remove patch
@@ -17,8 +17,9 @@  discard block
 block discarded – undo
17 17
  * @version 2.1 Beta 4
18 18
  */
19 19
 
20
-if (!defined('SMF'))
20
+if (!defined('SMF')) {
21 21
 	die('No direct access...');
22
+}
22 23
 
23 24
 /**
24 25
  * Attempt to start the session, unless it already has been.
@@ -38,8 +39,9 @@  discard block
 block discarded – undo
38 39
 	{
39 40
 		$parsed_url = parse_url($boardurl);
40 41
 
41
-		if (preg_match('~^\d{1,3}(\.\d{1,3}){3}$~', $parsed_url['host']) == 0 && preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $parsed_url['host'], $parts) == 1)
42
-			@ini_set('session.cookie_domain', '.' . $parts[1]);
42
+		if (preg_match('~^\d{1,3}(\.\d{1,3}){3}$~', $parsed_url['host']) == 0 && preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $parsed_url['host'], $parts) == 1) {
43
+					@ini_set('session.cookie_domain', '.' . $parts[1]);
44
+		}
43 45
 	}
44 46
 	// @todo Set the session cookie path?
45 47
 
@@ -47,8 +49,9 @@  discard block
 block discarded – undo
47 49
 	if ((ini_get('session.auto_start') == 1 && !empty($modSettings['databaseSession_enable'])) || session_id() == '')
48 50
 	{
49 51
 		// Attempt to end the already-started session.
50
-		if (ini_get('session.auto_start') == 1)
51
-			session_write_close();
52
+		if (ini_get('session.auto_start') == 1) {
53
+					session_write_close();
54
+		}
52 55
 
53 56
 		// This is here to stop people from using bad junky PHPSESSIDs.
54 57
 		if (isset($_REQUEST[session_name()]) && preg_match('~^[A-Za-z0-9,-]{16,64}$~', $_REQUEST[session_name()]) == 0 && !isset($_COOKIE[session_name()]))
@@ -63,23 +66,26 @@  discard block
 block discarded – undo
63 66
 		if (!empty($modSettings['databaseSession_enable']))
64 67
 		{
65 68
 			@ini_set('session.serialize_handler', 'php_serialize');
66
-			if (ini_get('session.serialize_handler') != 'php_serialize')
67
-				@ini_set('session.serialize_handler', 'php');
69
+			if (ini_get('session.serialize_handler') != 'php_serialize') {
70
+							@ini_set('session.serialize_handler', 'php');
71
+			}
68 72
 			session_set_save_handler('sessionOpen', 'sessionClose', 'sessionRead', 'sessionWrite', 'sessionDestroy', 'sessionGC');
69 73
 			@ini_set('session.gc_probability', '1');
74
+		} elseif (ini_get('session.gc_maxlifetime') <= 1440 && !empty($modSettings['databaseSession_lifetime'])) {
75
+					@ini_set('session.gc_maxlifetime', max($modSettings['databaseSession_lifetime'], 60));
70 76
 		}
71
-		elseif (ini_get('session.gc_maxlifetime') <= 1440 && !empty($modSettings['databaseSession_lifetime']))
72
-			@ini_set('session.gc_maxlifetime', max($modSettings['databaseSession_lifetime'], 60));
73 77
 
74 78
 		// Use cache setting sessions?
75
-		if (empty($modSettings['databaseSession_enable']) && !empty($modSettings['cache_enable']) && php_sapi_name() != 'cli')
76
-			call_integration_hook('integrate_session_handlers');
79
+		if (empty($modSettings['databaseSession_enable']) && !empty($modSettings['cache_enable']) && php_sapi_name() != 'cli') {
80
+					call_integration_hook('integrate_session_handlers');
81
+		}
77 82
 
78 83
 		session_start();
79 84
 
80 85
 		// Change it so the cache settings are a little looser than default.
81
-		if (!empty($modSettings['databaseSession_loose']))
82
-			header('cache-control: private');
86
+		if (!empty($modSettings['databaseSession_loose'])) {
87
+					header('cache-control: private');
88
+		}
83 89
 	}
84 90
 
85 91
 	// Set the randomly generated code.
@@ -125,8 +131,9 @@  discard block
 block discarded – undo
125 131
 {
126 132
 	global $smcFunc;
127 133
 
128
-	if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0)
129
-		return '';
134
+	if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) {
135
+			return '';
136
+	}
130 137
 
131 138
 	// Look for it in the database.
132 139
 	$result = $smcFunc['db_query']('', '
@@ -156,8 +163,9 @@  discard block
 block discarded – undo
156 163
 	global $smcFunc, $db_connection, $db_server, $db_name, $db_user, $db_passwd;
157 164
 	global $db_prefix, $db_persist, $db_port, $db_mb4;
158 165
 
159
-	if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0)
160
-		return false;
166
+	if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) {
167
+			return false;
168
+	}
161 169
 
162 170
 	// php < 7.0 need this
163 171
 	if (empty($db_connection))
@@ -165,11 +173,13 @@  discard block
 block discarded – undo
165 173
 		$db_options = array();
166 174
 
167 175
 		// Add in the port if needed
168
-		if (!empty($db_port))
169
-			$db_options['port'] = $db_port;
176
+		if (!empty($db_port)) {
177
+					$db_options['port'] = $db_port;
178
+		}
170 179
 
171
-		if (!empty($db_mb4))
172
-		$db_options['db_mb4'] = $db_mb4;
180
+		if (!empty($db_mb4)) {
181
+				$db_options['db_mb4'] = $db_mb4;
182
+		}
173 183
 
174 184
 		$options = array_merge($db_options, array('persist' => $db_persist, 'dont_select_db' => SMF == 'SSI'));
175 185
 
@@ -189,13 +199,14 @@  discard block
 block discarded – undo
189 199
 	);
190 200
 
191 201
 	// If that didn't work, try inserting a new one.
192
-	if ($smcFunc['db_affected_rows']() == 0)
193
-		$smcFunc['db_insert']('ignore',
202
+	if ($smcFunc['db_affected_rows']() == 0) {
203
+			$smcFunc['db_insert']('ignore',
194 204
 			'{db_prefix}sessions',
195 205
 			array('session_id' => 'string', 'data' => 'string', 'last_update' => 'int'),
196 206
 			array($session_id, $data, time()),
197 207
 			array('session_id')
198 208
 		);
209
+	}
199 210
 
200 211
 	return ($smcFunc['db_affected_rows']() == 0 ? false : true);
201 212
 }
@@ -210,8 +221,9 @@  discard block
 block discarded – undo
210 221
 {
211 222
 	global $smcFunc;
212 223
 
213
-	if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0)
214
-		return false;
224
+	if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) {
225
+			return false;
226
+	}
215 227
 
216 228
 	// Just delete the row...
217 229
 	$smcFunc['db_query']('', '
@@ -237,8 +249,9 @@  discard block
 block discarded – undo
237 249
 	global $modSettings, $smcFunc;
238 250
 
239 251
 	// Just set to the default or lower?  Ignore it for a higher value. (hopefully)
240
-	if (!empty($modSettings['databaseSession_lifetime']) && ($max_lifetime <= 1440 || $modSettings['databaseSession_lifetime'] > $max_lifetime))
241
-		$max_lifetime = max($modSettings['databaseSession_lifetime'], 60);
252
+	if (!empty($modSettings['databaseSession_lifetime']) && ($max_lifetime <= 1440 || $modSettings['databaseSession_lifetime'] > $max_lifetime)) {
253
+			$max_lifetime = max($modSettings['databaseSession_lifetime'], 60);
254
+	}
242 255
 
243 256
 	// Clean up after yerself ;).
244 257
 	$smcFunc['db_query']('', '
Please login to merge, or discard this patch.
Sources/Subs-Db-mysql.php 1 patch
Braces   +260 added lines, -191 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 4
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
  *  Maps the implementations in this file (smf_db_function_name)
@@ -33,8 +34,8 @@  discard block
 block discarded – undo
33 34
 	global $smcFunc;
34 35
 
35 36
 	// Map some database specific functions, only do this once.
36
-	if (!isset($smcFunc['db_fetch_assoc']))
37
-		$smcFunc += array(
37
+	if (!isset($smcFunc['db_fetch_assoc'])) {
38
+			$smcFunc += array(
38 39
 			'db_query'                  => 'smf_db_query',
39 40
 			'db_quote'                  => 'smf_db_quote',
40 41
 			'db_fetch_assoc'            => 'mysqli_fetch_assoc',
@@ -64,13 +65,16 @@  discard block
 block discarded – undo
64 65
 			'db_custom_order'			=> 'smf_db_custom_order',
65 66
 			'db_native_replace'			=> 'smf_db_native_replace',
66 67
 		);
68
+	}
67 69
 
68
-	if (!empty($db_options['persist']))
69
-		$db_server = 'p:' . $db_server;
70
+	if (!empty($db_options['persist'])) {
71
+			$db_server = 'p:' . $db_server;
72
+	}
70 73
 
71 74
 	// We are not going to make it very far without these.
72
-	if (!function_exists('mysqli_init') || !function_exists('mysqli_real_connect'))
73
-		display_db_error();
75
+	if (!function_exists('mysqli_init') || !function_exists('mysqli_real_connect')) {
76
+			display_db_error();
77
+	}
74 78
 
75 79
 	$connection = mysqli_init();
76 80
 
@@ -80,29 +84,33 @@  discard block
 block discarded – undo
80 84
 
81 85
 	if ($connection)
82 86
 	{
83
-		if (!empty($db_options['port']))
84
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags);
85
-		else
86
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags);
87
+		if (!empty($db_options['port'])) {
88
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags);
89
+		} else {
90
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags);
91
+		}
87 92
 	}
88 93
 
89 94
 	// Something's wrong, show an error if its fatal (which we assume it is)
90 95
 	if ($success === false)
91 96
 	{
92
-		if (!empty($db_options['non_fatal']))
93
-			return null;
94
-		else
95
-			display_db_error();
97
+		if (!empty($db_options['non_fatal'])) {
98
+					return null;
99
+		} else {
100
+					display_db_error();
101
+		}
96 102
 	}
97 103
 
98 104
 	// Select the database, unless told not to
99
-	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal']))
100
-		display_db_error();
105
+	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal'])) {
106
+			display_db_error();
107
+	}
101 108
 
102 109
 	mysqli_query($connection, 'SET SESSION sql_mode = \'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\'');
103 110
 
104
-	if (!empty($db_options['db_mb4']))
105
-		$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
111
+	if (!empty($db_options['db_mb4'])) {
112
+			$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
113
+	}
106 114
 
107 115
 	return $connection;
108 116
 }
@@ -173,34 +181,42 @@  discard block
 block discarded – undo
173 181
 	global $db_callback, $user_info, $db_prefix, $smcFunc;
174 182
 
175 183
 	list ($values, $connection) = $db_callback;
176
-	if (!is_object($connection))
177
-		display_db_error();
184
+	if (!is_object($connection)) {
185
+			display_db_error();
186
+	}
178 187
 
179
-	if ($matches[1] === 'db_prefix')
180
-		return $db_prefix;
188
+	if ($matches[1] === 'db_prefix') {
189
+			return $db_prefix;
190
+	}
181 191
 
182
-	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false)
183
-		return $user_info[$matches[1]];
192
+	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) {
193
+			return $user_info[$matches[1]];
194
+	}
184 195
 
185
-	if ($matches[1] === 'empty')
186
-		return '\'\'';
196
+	if ($matches[1] === 'empty') {
197
+			return '\'\'';
198
+	}
187 199
 
188
-	if (!isset($matches[2]))
189
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
200
+	if (!isset($matches[2])) {
201
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
202
+	}
190 203
 
191
-	if ($matches[1] === 'literal')
192
-		return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
204
+	if ($matches[1] === 'literal') {
205
+			return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
206
+	}
193 207
 
194
-	if (!isset($values[$matches[2]]))
195
-		smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__);
208
+	if (!isset($values[$matches[2]])) {
209
+			smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__);
210
+	}
196 211
 
197 212
 	$replacement = $values[$matches[2]];
198 213
 
199 214
 	switch ($matches[1])
200 215
 	{
201 216
 		case 'int':
202
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
203
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
217
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
218
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
219
+			}
204 220
 			return (string) (int) $replacement;
205 221
 		break;
206 222
 
@@ -212,65 +228,73 @@  discard block
 block discarded – undo
212 228
 		case 'array_int':
213 229
 			if (is_array($replacement))
214 230
 			{
215
-				if (empty($replacement))
216
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
231
+				if (empty($replacement)) {
232
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
233
+				}
217 234
 
218 235
 				foreach ($replacement as $key => $value)
219 236
 				{
220
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
221
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
237
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
238
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
239
+					}
222 240
 
223 241
 					$replacement[$key] = (string) (int) $value;
224 242
 				}
225 243
 
226 244
 				return implode(', ', $replacement);
245
+			} else {
246
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
227 247
 			}
228
-			else
229
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
230 248
 
231 249
 		break;
232 250
 
233 251
 		case 'array_string':
234 252
 			if (is_array($replacement))
235 253
 			{
236
-				if (empty($replacement))
237
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
254
+				if (empty($replacement)) {
255
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
256
+				}
238 257
 
239
-				foreach ($replacement as $key => $value)
240
-					$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
258
+				foreach ($replacement as $key => $value) {
259
+									$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
260
+				}
241 261
 
242 262
 				return implode(', ', $replacement);
263
+			} else {
264
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
243 265
 			}
244
-			else
245
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
246 266
 		break;
247 267
 
248 268
 		case 'date':
249
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
250
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
251
-			else
252
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
269
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
270
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
271
+			} else {
272
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
273
+			}
253 274
 		break;
254 275
 
255 276
 		case 'time':
256
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
257
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
258
-			else
259
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
277
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
278
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
279
+			} else {
280
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
281
+			}
260 282
 		break;
261 283
 
262 284
 		case 'datetime':
263
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1)
264
-				return 'str_to_date('.
285
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) {
286
+							return 'str_to_date('.
265 287
 					sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5] ,$datetime_matches[6]).
266 288
 					',\'%Y-%m-%d %h:%i:%s\')';
267
-			else
268
-				smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
289
+			} else {
290
+							smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
291
+			}
269 292
 		break;
270 293
 
271 294
 		case 'float':
272
-			if (!is_numeric($replacement))
273
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
295
+			if (!is_numeric($replacement)) {
296
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
297
+			}
274 298
 			return (string) (float) $replacement;
275 299
 		break;
276 300
 
@@ -284,32 +308,37 @@  discard block
 block discarded – undo
284 308
 		break;
285 309
 
286 310
 		case 'inet':
287
-			if ($replacement == 'null' || $replacement == '')
288
-				return 'null';
289
-			if (!isValidIP($replacement))
290
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
311
+			if ($replacement == 'null' || $replacement == '') {
312
+							return 'null';
313
+			}
314
+			if (!isValidIP($replacement)) {
315
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
316
+			}
291 317
 			//we don't use the native support of mysql > 5.6.2
292 318
 			return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement)));
293 319
 
294 320
 		case 'array_inet':
295 321
 			if (is_array($replacement))
296 322
 			{
297
-				if (empty($replacement))
298
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
323
+				if (empty($replacement)) {
324
+									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
325
+				}
299 326
 
300 327
 				foreach ($replacement as $key => $value)
301 328
 				{
302
-					if ($replacement == 'null' || $replacement == '')
303
-						$replacement[$key] = 'null';
304
-					if (!isValidIP($value))
305
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
329
+					if ($replacement == 'null' || $replacement == '') {
330
+											$replacement[$key] = 'null';
331
+					}
332
+					if (!isValidIP($value)) {
333
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
334
+					}
306 335
 					$replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value)));
307 336
 				}
308 337
 
309 338
 				return implode(', ', $replacement);
339
+			} else {
340
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
310 341
 			}
311
-			else
312
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
313 342
 		break;
314 343
 
315 344
 		default:
@@ -380,18 +409,20 @@  discard block
 block discarded – undo
380 409
 	// One more query....
381 410
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
382 411
 
383
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
384
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
412
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
413
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
414
+	}
385 415
 
386 416
 	// Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By
387 417
 	if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && preg_match('~^\s+SELECT~i', $db_string))
388 418
 	{
389 419
 		// Add before LIMIT
390
-		if ($pos = strpos($db_string, 'LIMIT '))
391
-			$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
392
-		else
393
-			// Append it.
420
+		if ($pos = strpos($db_string, 'LIMIT ')) {
421
+					$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
422
+		} else {
423
+					// Append it.
394 424
 			$db_string .= "\n\t\t\tORDER BY null";
425
+		}
395 426
 	}
396 427
 
397 428
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
@@ -417,17 +448,18 @@  discard block
 block discarded – undo
417 448
 		while (true)
418 449
 		{
419 450
 			$pos = strpos($db_string_1, '\'', $pos + 1);
420
-			if ($pos === false)
421
-				break;
451
+			if ($pos === false) {
452
+							break;
453
+			}
422 454
 			$clean .= substr($db_string_1, $old_pos, $pos - $old_pos);
423 455
 
424 456
 			while (true)
425 457
 			{
426 458
 				$pos1 = strpos($db_string_1, '\'', $pos + 1);
427 459
 				$pos2 = strpos($db_string_1, '\\', $pos + 1);
428
-				if ($pos1 === false)
429
-					break;
430
-				elseif ($pos2 === false || $pos2 > $pos1)
460
+				if ($pos1 === false) {
461
+									break;
462
+				} elseif ($pos2 === false || $pos2 > $pos1)
431 463
 				{
432 464
 					$pos = $pos1;
433 465
 					break;
@@ -443,16 +475,19 @@  discard block
 block discarded – undo
443 475
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
444 476
 
445 477
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
446
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
447
-			$fail = true;
478
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
479
+					$fail = true;
480
+		}
448 481
 		// Trying to change passwords, slow us down, or something?
449
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
450
-			$fail = true;
451
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
452
-			$fail = true;
482
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
483
+					$fail = true;
484
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
485
+					$fail = true;
486
+		}
453 487
 
454
-		if (!empty($fail) && function_exists('log_error'))
455
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
488
+		if (!empty($fail) && function_exists('log_error')) {
489
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
490
+		}
456 491
 	}
457 492
 
458 493
 	// Debugging.
@@ -462,8 +497,9 @@  discard block
 block discarded – undo
462 497
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
463 498
 
464 499
 		// Initialize $db_cache if not already initialized.
465
-		if (!isset($db_cache))
466
-			$db_cache = array();
500
+		if (!isset($db_cache)) {
501
+					$db_cache = array();
502
+		}
467 503
 
468 504
 		if (!empty($_SESSION['debug_redirect']))
469 505
 		{
@@ -479,17 +515,20 @@  discard block
 block discarded – undo
479 515
 		$db_cache[$db_count]['s'] = ($st = microtime(true)) - $time_start;
480 516
 	}
481 517
 
482
-	if (empty($db_unbuffered))
483
-		$ret = @mysqli_query($connection, $db_string);
484
-	else
485
-		$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
518
+	if (empty($db_unbuffered)) {
519
+			$ret = @mysqli_query($connection, $db_string);
520
+	} else {
521
+			$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
522
+	}
486 523
 
487
-	if ($ret === false && empty($db_values['db_error_skip']))
488
-		$ret = smf_db_error($db_string, $connection);
524
+	if ($ret === false && empty($db_values['db_error_skip'])) {
525
+			$ret = smf_db_error($db_string, $connection);
526
+	}
489 527
 
490 528
 	// Debugging.
491
-	if (isset($db_show_debug) && $db_show_debug === true)
492
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
529
+	if (isset($db_show_debug) && $db_show_debug === true) {
530
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
531
+	}
493 532
 
494 533
 	return $ret;
495 534
 }
@@ -536,12 +575,13 @@  discard block
 block discarded – undo
536 575
 	// Decide which connection to use
537 576
 	$connection = $connection === null ? $db_connection : $connection;
538 577
 
539
-	if ($type == 'begin')
540
-		return @mysqli_query($connection, 'BEGIN');
541
-	elseif ($type == 'rollback')
542
-		return @mysqli_query($connection, 'ROLLBACK');
543
-	elseif ($type == 'commit')
544
-		return @mysqli_query($connection, 'COMMIT');
578
+	if ($type == 'begin') {
579
+			return @mysqli_query($connection, 'BEGIN');
580
+	} elseif ($type == 'rollback') {
581
+			return @mysqli_query($connection, 'ROLLBACK');
582
+	} elseif ($type == 'commit') {
583
+			return @mysqli_query($connection, 'COMMIT');
584
+	}
545 585
 
546 586
 	return false;
547 587
 }
@@ -579,8 +619,9 @@  discard block
 block discarded – undo
579 619
 	//    1213: Deadlock found.
580 620
 
581 621
 	// Log the error.
582
-	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error'))
583
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
622
+	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) {
623
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
624
+	}
584 625
 
585 626
 	// Database error auto fixing ;).
586 627
 	if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1'))
@@ -589,8 +630,9 @@  discard block
 block discarded – undo
589 630
 		$old_cache = @$modSettings['cache_enable'];
590 631
 		$modSettings['cache_enable'] = '1';
591 632
 
592
-		if (($temp = cache_get_data('db_last_error', 600)) !== null)
593
-			$db_last_error = max(@$db_last_error, $temp);
633
+		if (($temp = cache_get_data('db_last_error', 600)) !== null) {
634
+					$db_last_error = max(@$db_last_error, $temp);
635
+		}
594 636
 
595 637
 		if (@$db_last_error < time() - 3600 * 24 * 3)
596 638
 		{
@@ -606,8 +648,9 @@  discard block
 block discarded – undo
606 648
 					foreach ($tables as $table)
607 649
 					{
608 650
 						// Now, it's still theoretically possible this could be an injection.  So backtick it!
609
-						if (trim($table) != '')
610
-							$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
651
+						if (trim($table) != '') {
652
+													$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
653
+						}
611 654
 					}
612 655
 				}
613 656
 
@@ -616,8 +659,9 @@  discard block
 block discarded – undo
616 659
 			// Table crashed.  Let's try to fix it.
617 660
 			elseif ($query_errno == 1016)
618 661
 			{
619
-				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0)
620
-					$fix_tables = array('`' . $match[1] . '`');
662
+				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) {
663
+									$fix_tables = array('`' . $match[1] . '`');
664
+				}
621 665
 			}
622 666
 			// Indexes crashed.  Should be easy to fix!
623 667
 			elseif ($query_errno == 1034 || $query_errno == 1035)
@@ -636,13 +680,15 @@  discard block
 block discarded – undo
636 680
 
637 681
 			// Make a note of the REPAIR...
638 682
 			cache_put_data('db_last_error', time(), 600);
639
-			if (($temp = cache_get_data('db_last_error', 600)) === null)
640
-				updateSettingsFile(array('db_last_error' => time()));
683
+			if (($temp = cache_get_data('db_last_error', 600)) === null) {
684
+							updateSettingsFile(array('db_last_error' => time()));
685
+			}
641 686
 
642 687
 			// Attempt to find and repair the broken table.
643
-			foreach ($fix_tables as $table)
644
-				$smcFunc['db_query']('', "
688
+			foreach ($fix_tables as $table) {
689
+							$smcFunc['db_query']('', "
645 690
 					REPAIR TABLE $table", false, false);
691
+			}
646 692
 
647 693
 			// And send off an email!
648 694
 			sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair'], null, 'dberror');
@@ -651,11 +697,12 @@  discard block
 block discarded – undo
651 697
 
652 698
 			// Try the query again...?
653 699
 			$ret = $smcFunc['db_query']('', $db_string, false, false);
654
-			if ($ret !== false)
655
-				return $ret;
700
+			if ($ret !== false) {
701
+							return $ret;
702
+			}
703
+		} else {
704
+					$modSettings['cache_enable'] = $old_cache;
656 705
 		}
657
-		else
658
-			$modSettings['cache_enable'] = $old_cache;
659 706
 
660 707
 		// Check for the "lost connection" or "deadlock found" errors - and try it just one more time.
661 708
 		if (in_array($query_errno, array(1205, 1213)))
@@ -668,24 +715,27 @@  discard block
 block discarded – undo
668 715
 					$ret = $smcFunc['db_query']('', $db_string, false, false);
669 716
 
670 717
 					$new_errno = mysqli_errno($db_connection);
671
-					if ($ret !== false || in_array($new_errno, array(1205, 1213)))
672
-						break;
718
+					if ($ret !== false || in_array($new_errno, array(1205, 1213))) {
719
+											break;
720
+					}
673 721
 				}
674 722
 
675 723
 				// If it failed again, shucks to be you... we're not trying it over and over.
676
-				if ($ret !== false)
677
-					return $ret;
724
+				if ($ret !== false) {
725
+									return $ret;
726
+				}
678 727
 			}
679 728
 		}
680 729
 		// Are they out of space, perhaps?
681 730
 		elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false))
682 731
 		{
683
-			if (!isset($txt))
684
-				$query_error .= ' - check database storage space.';
685
-			else
732
+			if (!isset($txt)) {
733
+							$query_error .= ' - check database storage space.';
734
+			} else
686 735
 			{
687
-				if (!isset($txt['mysql_error_space']))
688
-					loadLanguage('Errors');
736
+				if (!isset($txt['mysql_error_space'])) {
737
+									loadLanguage('Errors');
738
+				}
689 739
 
690 740
 				$query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space'];
691 741
 			}
@@ -693,15 +743,17 @@  discard block
 block discarded – undo
693 743
 	}
694 744
 
695 745
 	// Nothing's defined yet... just die with it.
696
-	if (empty($context) || empty($txt))
697
-		die($query_error);
746
+	if (empty($context) || empty($txt)) {
747
+			die($query_error);
748
+	}
698 749
 
699 750
 	// Show an error message, if possible.
700 751
 	$context['error_title'] = $txt['database_error'];
701
-	if (allowedTo('admin_forum'))
702
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
703
-	else
704
-		$context['error_message'] = $txt['try_again'];
752
+	if (allowedTo('admin_forum')) {
753
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
754
+	} else {
755
+			$context['error_message'] = $txt['try_again'];
756
+	}
705 757
 
706 758
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
707 759
 	{
@@ -733,8 +785,9 @@  discard block
 block discarded – undo
733 785
 	$return_var = null;
734 786
 
735 787
 	// With nothing to insert, simply return.
736
-	if (empty($data))
737
-		return;
788
+	if (empty($data)) {
789
+			return;
790
+	}
738 791
 
739 792
 	// Replace the prefix holder with the actual prefix.
740 793
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
@@ -744,23 +797,26 @@  discard block
 block discarded – undo
744 797
 	if (!empty($keys) && (count($keys) > 0) && $returnmode > 0)
745 798
 	{
746 799
 		$with_returning = true;
747
-		if ($returnmode == 2)
748
-			$return_var = array();
800
+		if ($returnmode == 2) {
801
+					$return_var = array();
802
+		}
749 803
 	}
750 804
 
751 805
 	// Inserting data as a single row can be done as a single array.
752
-	if (!is_array($data[array_rand($data)]))
753
-		$data = array($data);
806
+	if (!is_array($data[array_rand($data)])) {
807
+			$data = array($data);
808
+	}
754 809
 
755 810
 	// Create the mold for a single row insert.
756 811
 	$insertData = '(';
757 812
 	foreach ($columns as $columnName => $type)
758 813
 	{
759 814
 		// Are we restricting the length?
760
-		if (strpos($type, 'string-') !== false)
761
-			$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
762
-		else
763
-			$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
815
+		if (strpos($type, 'string-') !== false) {
816
+					$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
817
+		} else {
818
+					$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
819
+		}
764 820
 	}
765 821
 	$insertData = substr($insertData, 0, -2) . ')';
766 822
 
@@ -769,8 +825,9 @@  discard block
 block discarded – undo
769 825
 
770 826
 	// Here's where the variables are injected to the query.
771 827
 	$insertRows = array();
772
-	foreach ($data as $dataRow)
773
-		$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
828
+	foreach ($data as $dataRow) {
829
+			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
830
+	}
774 831
 
775 832
 	// Determine the method of insertion.
776 833
 	$queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT');
@@ -789,8 +846,7 @@  discard block
 block discarded – undo
789 846
 			),
790 847
 			$connection
791 848
 		);
792
-	}
793
-	else //special way for ignore method with returning
849
+	} else //special way for ignore method with returning
794 850
 	{
795 851
 		$count = count($insertRows);
796 852
 		$ai = 0;
@@ -810,19 +866,21 @@  discard block
 block discarded – undo
810 866
 			);
811 867
 			$new_id = $smcFunc['db_insert_id']();
812 868
 
813
-			if ($last_id != $new_id) //the inserted value was new
869
+			if ($last_id != $new_id) {
870
+				//the inserted value was new
814 871
 			{
815 872
 				$ai = $new_id;
816 873
 			}
817
-			else	// the inserted value already exists we need to find the pk
874
+			} else	// the inserted value already exists we need to find the pk
818 875
 			{
819 876
 				$where_string = '';
820 877
 				$count2 = count($indexed_columns);
821 878
 				for ($x = 0; $x < $count2; $x++)
822 879
 				{
823 880
 					$where_string += key($indexed_columns[$x]) . ' = '. $insertRows[$i][$x];
824
-					if (($x + 1) < $count2)
825
-						$where_string += ' AND ';
881
+					if (($x + 1) < $count2) {
882
+											$where_string += ' AND ';
883
+					}
826 884
 				}
827 885
 
828 886
 				$request = $smcFunc['db_query']('','
@@ -838,25 +896,27 @@  discard block
 block discarded – undo
838 896
 				}
839 897
 			}
840 898
 
841
-			if ($returnmode == 1)
842
-				$return_var = $ai;
843
-			else if ($returnmode == 2)
844
-				$return_var[] = $ai;
899
+			if ($returnmode == 1) {
900
+							$return_var = $ai;
901
+			} else if ($returnmode == 2) {
902
+							$return_var[] = $ai;
903
+			}
845 904
 		}
846 905
 	}
847 906
 
848 907
 
849 908
 	if ($with_returning)
850 909
 	{
851
-		if ($returnmode == 1 && empty($return_var))
852
-			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
853
-		else if ($returnmode == 2 && empty($return_var))
910
+		if ($returnmode == 1 && empty($return_var)) {
911
+					$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
912
+		} else if ($returnmode == 2 && empty($return_var))
854 913
 		{
855 914
 			$return_var = array();
856 915
 			$count = count($insertRows);
857 916
 			$start = smf_db_insert_id($table, $keys[0]);
858
-			for ($i = 0; $i < $count; $i++ )
859
-				$return_var[] = $start + $i;
917
+			for ($i = 0; $i < $count; $i++ ) {
918
+							$return_var[] = $start + $i;
919
+			}
860 920
 		}
861 921
 		return $return_var;
862 922
 	}
@@ -874,8 +934,9 @@  discard block
 block discarded – undo
874 934
  */
875 935
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
876 936
 {
877
-	if (empty($log_message))
878
-		$log_message = $error_message;
937
+	if (empty($log_message)) {
938
+			$log_message = $error_message;
939
+	}
879 940
 
880 941
 	foreach (debug_backtrace() as $step)
881 942
 	{
@@ -894,12 +955,14 @@  discard block
 block discarded – undo
894 955
 	}
895 956
 
896 957
 	// A special case - we want the file and line numbers for debugging.
897
-	if ($error_type == 'return')
898
-		return array($file, $line);
958
+	if ($error_type == 'return') {
959
+			return array($file, $line);
960
+	}
899 961
 
900 962
 	// Is always a critical error.
901
-	if (function_exists('log_error'))
902
-		log_error($log_message, 'critical', $file, $line);
963
+	if (function_exists('log_error')) {
964
+			log_error($log_message, 'critical', $file, $line);
965
+	}
903 966
 
904 967
 	if (function_exists('fatal_error'))
905 968
 	{
@@ -907,12 +970,12 @@  discard block
 block discarded – undo
907 970
 
908 971
 		// Cannot continue...
909 972
 		exit;
973
+	} elseif ($error_type) {
974
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
975
+	} else {
976
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
977
+	}
910 978
 	}
911
-	elseif ($error_type)
912
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
913
-	else
914
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
915
-}
916 979
 
917 980
 /**
918 981
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -929,10 +992,11 @@  discard block
 block discarded – undo
929 992
 		'\\' => '\\\\',
930 993
 	);
931 994
 
932
-	if ($translate_human_wildcards)
933
-		$replacements += array(
995
+	if ($translate_human_wildcards) {
996
+			$replacements += array(
934 997
 			'*' => '%',
935 998
 		);
999
+	}
936 1000
 
937 1001
 	return strtr($string, $replacements);
938 1002
 }
@@ -946,8 +1010,9 @@  discard block
 block discarded – undo
946 1010
  */
947 1011
 function smf_is_resource($result)
948 1012
 {
949
-	if ($result instanceof mysqli_result)
950
-		return true;
1013
+	if ($result instanceof mysqli_result) {
1014
+			return true;
1015
+	}
951 1016
 
952 1017
 	return false;
953 1018
 }
@@ -976,19 +1041,22 @@  discard block
 block discarded – undo
976 1041
 	static $mysql_error_data_prep;
977 1042
 
978 1043
 	// without database we can't do anything
979
-	if (empty($db_connection))
980
-		return;
1044
+	if (empty($db_connection)) {
1045
+			return;
1046
+	}
981 1047
 
982
-	if (empty($mysql_error_data_prep))
983
-			$mysql_error_data_prep = mysqli_prepare($db_connection,
1048
+	if (empty($mysql_error_data_prep)) {
1049
+				$mysql_error_data_prep = mysqli_prepare($db_connection,
984 1050
 				'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace)
985 1051
 													VALUES(		?,		?,		unhex(?), ?, 		?,		?,			?,		?,	?, ?)'
986 1052
 			);
1053
+	}
987 1054
 
988
-	if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false)
989
-		$error_array[2] = bin2hex(inet_pton($error_array[2]));
990
-	else
991
-		$error_array[2] = null;
1055
+	if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false) {
1056
+			$error_array[2] = bin2hex(inet_pton($error_array[2]));
1057
+	} else {
1058
+			$error_array[2] = null;
1059
+	}
992 1060
 	mysqli_stmt_bind_param($mysql_error_data_prep, 'iissssssis',
993 1061
 		$error_array[0], $error_array[1], $error_array[2], $error_array[3], $error_array[4], $error_array[5], $error_array[6],
994 1062
 		$error_array[7], $error_array[8], $error_array[9]);
@@ -1010,8 +1078,9 @@  discard block
 block discarded – undo
1010 1078
 	$count = count($array_values);
1011 1079
 	$then = ($desc ? ' THEN -' : ' THEN ');
1012 1080
 
1013
-	for ($i = 0; $i < $count; $i++)
1014
-		$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1081
+	for ($i = 0; $i < $count; $i++) {
1082
+			$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1083
+	}
1015 1084
 
1016 1085
 	$return .= 'END';
1017 1086
 	return $return;
Please login to merge, or discard this patch.
Sources/Subs-Db-postgresql.php 1 patch
Braces   +238 added lines, -175 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 4
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
  * Maps the implementations in this file (smf_db_function_name)
@@ -34,8 +35,8 @@  discard block
 block discarded – undo
34 35
 	global $smcFunc;
35 36
 
36 37
 	// Map some database specific functions, only do this once.
37
-	if (!isset($smcFunc['db_fetch_assoc']))
38
-		$smcFunc += array(
38
+	if (!isset($smcFunc['db_fetch_assoc'])) {
39
+			$smcFunc += array(
39 40
 			'db_query'					=> 'smf_db_query',
40 41
 			'db_quote'					=> 'smf_db_quote',
41 42
 			'db_insert'					=> 'smf_db_insert',
@@ -65,15 +66,18 @@  discard block
 block discarded – undo
65 66
 			'db_custom_order'			=> 'smf_db_custom_order',
66 67
 			'db_native_replace'			=> 'smf_db_native_replace',
67 68
 		);
69
+	}
68 70
 
69 71
 	// We are not going to make it very far without these.
70
-	if (!function_exists('pg_pconnect'))
71
-		display_db_error();
72
+	if (!function_exists('pg_pconnect')) {
73
+			display_db_error();
74
+	}
72 75
 
73
-	if (!empty($db_options['persist']))
74
-		$connection = @pg_pconnect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
75
-	else
76
-		$connection = @pg_connect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
76
+	if (!empty($db_options['persist'])) {
77
+			$connection = @pg_pconnect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
78
+	} else {
79
+			$connection = @pg_connect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
80
+	}
77 81
 
78 82
 	// Something's wrong, show an error if its fatal (which we assume it is)
79 83
 	if (!$connection)
@@ -81,15 +85,15 @@  discard block
 block discarded – undo
81 85
 		if (!empty($db_options['non_fatal']))
82 86
 		{
83 87
 			return null;
84
-		}
85
-		else
88
+		} else
86 89
 		{
87 90
 			display_db_error();
88 91
 		}
89 92
 	}
90 93
 
91
-	if (!empty($db_options['db_mb4']))
92
-		$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
94
+	if (!empty($db_options['db_mb4'])) {
95
+			$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
96
+	}
93 97
 
94 98
 	return $connection;
95 99
 }
@@ -136,31 +140,38 @@  discard block
 block discarded – undo
136 140
 
137 141
 	list ($values, $connection) = $db_callback;
138 142
 
139
-	if ($matches[1] === 'db_prefix')
140
-		return $db_prefix;
143
+	if ($matches[1] === 'db_prefix') {
144
+			return $db_prefix;
145
+	}
141 146
 
142
-	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false)
143
-		return $user_info[$matches[1]];
147
+	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) {
148
+			return $user_info[$matches[1]];
149
+	}
144 150
 
145
-	if ($matches[1] === 'empty')
146
-		return '\'\'';
151
+	if ($matches[1] === 'empty') {
152
+			return '\'\'';
153
+	}
147 154
 
148
-	if (!isset($matches[2]))
149
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
155
+	if (!isset($matches[2])) {
156
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
157
+	}
150 158
 
151
-	if ($matches[1] === 'literal')
152
-		return '\'' . pg_escape_string($matches[2]) . '\'';
159
+	if ($matches[1] === 'literal') {
160
+			return '\'' . pg_escape_string($matches[2]) . '\'';
161
+	}
153 162
 
154
-	if (!isset($values[$matches[2]]))
155
-		smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__);
163
+	if (!isset($values[$matches[2]])) {
164
+			smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__);
165
+	}
156 166
 
157 167
 	$replacement = $values[$matches[2]];
158 168
 
159 169
 	switch ($matches[1])
160 170
 	{
161 171
 		case 'int':
162
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
163
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
172
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
173
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
174
+			}
164 175
 			return (string) (int) $replacement;
165 176
 		break;
166 177
 
@@ -172,65 +183,73 @@  discard block
 block discarded – undo
172 183
 		case 'array_int':
173 184
 			if (is_array($replacement))
174 185
 			{
175
-				if (empty($replacement))
176
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
186
+				if (empty($replacement)) {
187
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
188
+				}
177 189
 
178 190
 				foreach ($replacement as $key => $value)
179 191
 				{
180
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
181
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
192
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
193
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
194
+					}
182 195
 
183 196
 					$replacement[$key] = (string) (int) $value;
184 197
 				}
185 198
 
186 199
 				return implode(', ', $replacement);
200
+			} else {
201
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
187 202
 			}
188
-			else
189
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
190 203
 
191 204
 		break;
192 205
 
193 206
 		case 'array_string':
194 207
 			if (is_array($replacement))
195 208
 			{
196
-				if (empty($replacement))
197
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
209
+				if (empty($replacement)) {
210
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
211
+				}
198 212
 
199
-				foreach ($replacement as $key => $value)
200
-					$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value));
213
+				foreach ($replacement as $key => $value) {
214
+									$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value));
215
+				}
201 216
 
202 217
 				return implode(', ', $replacement);
218
+			} else {
219
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
203 220
 			}
204
-			else
205
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
206 221
 		break;
207 222
 
208 223
 		case 'date':
209
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
210
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date';
211
-			else
212
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
224
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
225
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date';
226
+			} else {
227
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
228
+			}
213 229
 		break;
214 230
 
215 231
 		case 'time':
216
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
217
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time';
218
-			else
219
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
232
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
233
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time';
234
+			} else {
235
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
236
+			}
220 237
 		break;
221 238
 
222 239
 		case 'datetime':
223
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1)
224
-				return 'to_timestamp('.
240
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) {
241
+							return 'to_timestamp('.
225 242
 					sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5] ,$datetime_matches[6]).
226 243
 					',\'YYYY-MM-DD HH24:MI:SS\')';
227
-			else
228
-				smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
244
+			} else {
245
+							smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
246
+			}
229 247
 		break;
230 248
 
231 249
 		case 'float':
232
-			if (!is_numeric($replacement))
233
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
250
+			if (!is_numeric($replacement)) {
251
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
252
+			}
234 253
 			return (string) (float) $replacement;
235 254
 		break;
236 255
 
@@ -243,31 +262,36 @@  discard block
 block discarded – undo
243 262
 		break;
244 263
 
245 264
 		case 'inet':
246
-			if ($replacement == 'null' || $replacement == '')
247
-				return 'null';
248
-			if (inet_pton($replacement) === false)
249
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
265
+			if ($replacement == 'null' || $replacement == '') {
266
+							return 'null';
267
+			}
268
+			if (inet_pton($replacement) === false) {
269
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
270
+			}
250 271
 			return sprintf('\'%1$s\'::inet', pg_escape_string($replacement));
251 272
 
252 273
 		case 'array_inet':
253 274
 			if (is_array($replacement))
254 275
 			{
255
-				if (empty($replacement))
256
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
276
+				if (empty($replacement)) {
277
+									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
278
+				}
257 279
 
258 280
 				foreach ($replacement as $key => $value)
259 281
 				{
260
-					if ($replacement == 'null' || $replacement == '')
261
-						$replacement[$key] = 'null';
262
-					if (!isValidIP($value))
263
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
282
+					if ($replacement == 'null' || $replacement == '') {
283
+											$replacement[$key] = 'null';
284
+					}
285
+					if (!isValidIP($value)) {
286
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
287
+					}
264 288
 					$replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value));
265 289
 				}
266 290
 
267 291
 				return implode(', ', $replacement);
292
+			} else {
293
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
268 294
 			}
269
-			else
270
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
271 295
 		break;
272 296
 
273 297
 		default:
@@ -355,14 +379,16 @@  discard block
 block discarded – undo
355 379
 		),
356 380
 	);
357 381
 
358
-	if (isset($replacements[$identifier]))
359
-		$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
382
+	if (isset($replacements[$identifier])) {
383
+			$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
384
+	}
360 385
 
361 386
 	// Limits need to be a little different.
362 387
 	$db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string);
363 388
 
364
-	if (trim($db_string) == '')
365
-		return false;
389
+	if (trim($db_string) == '') {
390
+			return false;
391
+	}
366 392
 
367 393
 	// Comments that are allowed in a query are preg_removed.
368 394
 	static $allowed_comments_from = array(
@@ -382,8 +408,9 @@  discard block
 block discarded – undo
382 408
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
383 409
 	$db_replace_result = 0;
384 410
 
385
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
386
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
411
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
412
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
413
+	}
387 414
 
388 415
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
389 416
 	{
@@ -408,17 +435,18 @@  discard block
 block discarded – undo
408 435
 		while (true)
409 436
 		{
410 437
 			$pos = strpos($db_string_1, '\'', $pos + 1);
411
-			if ($pos === false)
412
-				break;
438
+			if ($pos === false) {
439
+							break;
440
+			}
413 441
 			$clean .= substr($db_string_1, $old_pos, $pos - $old_pos);
414 442
 
415 443
 			while (true)
416 444
 			{
417 445
 				$pos1 = strpos($db_string_1, '\'', $pos + 1);
418 446
 				$pos2 = strpos($db_string_1, '\\', $pos + 1);
419
-				if ($pos1 === false)
420
-					break;
421
-				elseif ($pos2 === false || $pos2 > $pos1)
447
+				if ($pos1 === false) {
448
+									break;
449
+				} elseif ($pos2 === false || $pos2 > $pos1)
422 450
 				{
423 451
 					$pos = $pos1;
424 452
 					break;
@@ -434,16 +462,19 @@  discard block
 block discarded – undo
434 462
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
435 463
 
436 464
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
437
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
438
-			$fail = true;
465
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
466
+					$fail = true;
467
+		}
439 468
 		// Trying to change passwords, slow us down, or something?
440
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
441
-			$fail = true;
442
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
443
-			$fail = true;
469
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
470
+					$fail = true;
471
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
472
+					$fail = true;
473
+		}
444 474
 
445
-		if (!empty($fail) && function_exists('log_error'))
446
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
475
+		if (!empty($fail) && function_exists('log_error')) {
476
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
477
+		}
447 478
 	}
448 479
 
449 480
 	// Set optimize stuff
@@ -470,8 +501,9 @@  discard block
 block discarded – undo
470 501
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
471 502
 
472 503
 		// Initialize $db_cache if not already initialized.
473
-		if (!isset($db_cache))
474
-			$db_cache = array();
504
+		if (!isset($db_cache)) {
505
+					$db_cache = array();
506
+		}
475 507
 
476 508
 		if (!empty($_SESSION['debug_redirect']))
477 509
 		{
@@ -489,12 +521,14 @@  discard block
 block discarded – undo
489 521
 
490 522
 	$db_last_result = @pg_query($connection, $db_string);
491 523
 
492
-	if ($db_last_result === false && empty($db_values['db_error_skip']))
493
-		$db_last_result = smf_db_error($db_string, $connection);
524
+	if ($db_last_result === false && empty($db_values['db_error_skip'])) {
525
+			$db_last_result = smf_db_error($db_string, $connection);
526
+	}
494 527
 
495 528
 	// Debugging.
496
-	if (isset($db_show_debug) && $db_show_debug === true)
497
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
529
+	if (isset($db_show_debug) && $db_show_debug === true) {
530
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
531
+	}
498 532
 
499 533
 	return $db_last_result;
500 534
 }
@@ -511,10 +545,11 @@  discard block
 block discarded – undo
511 545
 {
512 546
 	global $db_last_result, $db_replace_result;
513 547
 
514
-	if ($db_replace_result)
515
-		return $db_replace_result;
516
-	elseif ($result === null && !$db_last_result)
517
-		return 0;
548
+	if ($db_replace_result) {
549
+			return $db_replace_result;
550
+	} elseif ($result === null && !$db_last_result) {
551
+			return 0;
552
+	}
518 553
 
519 554
 	return pg_affected_rows($result === null ? $db_last_result : $result);
520 555
 }
@@ -538,8 +573,9 @@  discard block
 block discarded – undo
538 573
 		array(
539 574
 		)
540 575
 	);
541
-	if (!$request)
542
-		return false;
576
+	if (!$request) {
577
+			return false;
578
+	}
543 579
 	list ($lastID) = $smcFunc['db_fetch_row']($request);
544 580
 	$smcFunc['db_free_result']($request);
545 581
 
@@ -560,12 +596,13 @@  discard block
 block discarded – undo
560 596
 	// Decide which connection to use
561 597
 	$connection = $connection === null ? $db_connection : $connection;
562 598
 
563
-	if ($type == 'begin')
564
-		return @pg_query($connection, 'BEGIN');
565
-	elseif ($type == 'rollback')
566
-		return @pg_query($connection, 'ROLLBACK');
567
-	elseif ($type == 'commit')
568
-		return @pg_query($connection, 'COMMIT');
599
+	if ($type == 'begin') {
600
+			return @pg_query($connection, 'BEGIN');
601
+	} elseif ($type == 'rollback') {
602
+			return @pg_query($connection, 'ROLLBACK');
603
+	} elseif ($type == 'commit') {
604
+			return @pg_query($connection, 'COMMIT');
605
+	}
569 606
 
570 607
 	return false;
571 608
 }
@@ -593,19 +630,22 @@  discard block
 block discarded – undo
593 630
 	$query_error = @pg_last_error($connection);
594 631
 
595 632
 	// Log the error.
596
-	if (function_exists('log_error'))
597
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line);
633
+	if (function_exists('log_error')) {
634
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line);
635
+	}
598 636
 
599 637
 	// Nothing's defined yet... just die with it.
600
-	if (empty($context) || empty($txt))
601
-		die($query_error);
638
+	if (empty($context) || empty($txt)) {
639
+			die($query_error);
640
+	}
602 641
 
603 642
 	// Show an error message, if possible.
604 643
 	$context['error_title'] = $txt['database_error'];
605
-	if (allowedTo('admin_forum'))
606
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
607
-	else
608
-		$context['error_message'] = $txt['try_again'];
644
+	if (allowedTo('admin_forum')) {
645
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
646
+	} else {
647
+			$context['error_message'] = $txt['try_again'];
648
+	}
609 649
 
610 650
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
611 651
 	{
@@ -627,12 +667,14 @@  discard block
 block discarded – undo
627 667
 {
628 668
 	global $db_row_count;
629 669
 
630
-	if ($counter !== false)
631
-		return pg_fetch_row($request, $counter);
670
+	if ($counter !== false) {
671
+			return pg_fetch_row($request, $counter);
672
+	}
632 673
 
633 674
 	// Reset the row counter...
634
-	if (!isset($db_row_count[(int) $request]))
635
-		$db_row_count[(int) $request] = 0;
675
+	if (!isset($db_row_count[(int) $request])) {
676
+			$db_row_count[(int) $request] = 0;
677
+	}
636 678
 
637 679
 	// Return the right row.
638 680
 	return @pg_fetch_row($request, $db_row_count[(int) $request]++);
@@ -649,12 +691,14 @@  discard block
 block discarded – undo
649 691
 {
650 692
 	global $db_row_count;
651 693
 
652
-	if ($counter !== false)
653
-		return pg_fetch_assoc($request, $counter);
694
+	if ($counter !== false) {
695
+			return pg_fetch_assoc($request, $counter);
696
+	}
654 697
 
655 698
 	// Reset the row counter...
656
-	if (!isset($db_row_count[(int) $request]))
657
-		$db_row_count[(int) $request] = 0;
699
+	if (!isset($db_row_count[(int) $request])) {
700
+			$db_row_count[(int) $request] = 0;
701
+	}
658 702
 
659 703
 	// Return the right row.
660 704
 	return @pg_fetch_assoc($request, $db_row_count[(int) $request]++);
@@ -707,11 +751,13 @@  discard block
 block discarded – undo
707 751
 
708 752
 	$replace = '';
709 753
 
710
-	if (empty($data))
711
-		return;
754
+	if (empty($data)) {
755
+			return;
756
+	}
712 757
 
713
-	if (!is_array($data[array_rand($data)]))
714
-		$data = array($data);
758
+	if (!is_array($data[array_rand($data)])) {
759
+			$data = array($data);
760
+	}
715 761
 
716 762
 	// Replace the prefix holder with the actual prefix.
717 763
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
@@ -737,32 +783,35 @@  discard block
 block discarded – undo
737 783
 					$key_str .= ($count_pk > 0 ? ',' : '');
738 784
 					$key_str .= $columnName;
739 785
 					$count_pk++;
740
-				}
741
-				else if ($method == 'replace') //normal field
786
+				} else if ($method == 'replace') {
787
+					//normal field
742 788
 				{
743 789
 					$col_str .= ($count > 0 ? ',' : '');
790
+				}
744 791
 					$col_str .= $columnName . ' = EXCLUDED.' . $columnName;
745 792
 					$count++;
746 793
 				}
747 794
 			}
748
-			if ($method == 'replace')
749
-				$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str;
750
-			else
751
-				$replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING';
752
-		}
753
-		else if ($method == 'replace')
795
+			if ($method == 'replace') {
796
+							$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str;
797
+			} else {
798
+							$replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING';
799
+			}
800
+		} else if ($method == 'replace')
754 801
 		{
755 802
 			foreach ($columns as $columnName => $type)
756 803
 			{
757 804
 				// Are we restricting the length?
758
-				if (strpos($type, 'string-') !== false)
759
-					$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count);
760
-				else
761
-					$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count);
805
+				if (strpos($type, 'string-') !== false) {
806
+									$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count);
807
+				} else {
808
+									$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count);
809
+				}
762 810
 
763 811
 				// A key? That's what we were looking for.
764
-				if (in_array($columnName, $keys))
765
-					$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2);
812
+				if (in_array($columnName, $keys)) {
813
+									$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2);
814
+				}
766 815
 				$count++;
767 816
 			}
768 817
 
@@ -798,10 +847,11 @@  discard block
 block discarded – undo
798 847
 		foreach ($columns as $columnName => $type)
799 848
 		{
800 849
 			// Are we restricting the length?
801
-			if (strpos($type, 'string-') !== false)
802
-				$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
803
-			else
804
-				$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
850
+			if (strpos($type, 'string-') !== false) {
851
+							$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
852
+			} else {
853
+							$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
854
+			}
805 855
 		}
806 856
 		$insertData = substr($insertData, 0, -2) . ')';
807 857
 
@@ -810,8 +860,9 @@  discard block
 block discarded – undo
810 860
 
811 861
 		// Here's where the variables are injected to the query.
812 862
 		$insertRows = array();
813
-		foreach ($data as $dataRow)
814
-			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
863
+		foreach ($data as $dataRow) {
864
+					$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
865
+		}
815 866
 
816 867
 		// Do the insert.
817 868
 		$request = $smcFunc['db_query']('', '
@@ -828,19 +879,21 @@  discard block
 block discarded – undo
828 879
 
829 880
 		if ($with_returning && $request !== false)
830 881
 		{
831
-			if ($returnmode === 2)
832
-				$return_var = array();
882
+			if ($returnmode === 2) {
883
+							$return_var = array();
884
+			}
833 885
 
834 886
 			while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning)
835 887
 			{
836
-				if (is_numeric($row[0])) // try to emulate mysql limitation
888
+				if (is_numeric($row[0])) {
889
+					// try to emulate mysql limitation
837 890
 				{
838 891
 					if ($returnmode === 1)
839 892
 						$return_var = $row[0];
840
-					elseif ($returnmode === 2)
841
-						$return_var[] = $row[0];
842
-				}
843
-				else
893
+				} elseif ($returnmode === 2) {
894
+											$return_var[] = $row[0];
895
+					}
896
+				} else
844 897
 				{
845 898
 					$with_returning = false;
846 899
 					trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR);
@@ -849,9 +902,10 @@  discard block
 block discarded – undo
849 902
 		}
850 903
 	}
851 904
 
852
-	if ($with_returning && !empty($return_var))
853
-		return $return_var;
854
-}
905
+	if ($with_returning && !empty($return_var)) {
906
+			return $return_var;
907
+	}
908
+	}
855 909
 
856 910
 /**
857 911
  * Dummy function really. Doesn't do anything on PostgreSQL.
@@ -888,8 +942,9 @@  discard block
 block discarded – undo
888 942
  */
889 943
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
890 944
 {
891
-	if (empty($log_message))
892
-		$log_message = $error_message;
945
+	if (empty($log_message)) {
946
+			$log_message = $error_message;
947
+	}
893 948
 
894 949
 	foreach (debug_backtrace() as $step)
895 950
 	{
@@ -908,12 +963,14 @@  discard block
 block discarded – undo
908 963
 	}
909 964
 
910 965
 	// A special case - we want the file and line numbers for debugging.
911
-	if ($error_type == 'return')
912
-		return array($file, $line);
966
+	if ($error_type == 'return') {
967
+			return array($file, $line);
968
+	}
913 969
 
914 970
 	// Is always a critical error.
915
-	if (function_exists('log_error'))
916
-		log_error($log_message, 'critical', $file, $line);
971
+	if (function_exists('log_error')) {
972
+			log_error($log_message, 'critical', $file, $line);
973
+	}
917 974
 
918 975
 	if (function_exists('fatal_error'))
919 976
 	{
@@ -921,12 +978,12 @@  discard block
 block discarded – undo
921 978
 
922 979
 		// Cannot continue...
923 980
 		exit;
981
+	} elseif ($error_type) {
982
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
983
+	} else {
984
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
985
+	}
924 986
 	}
925
-	elseif ($error_type)
926
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
927
-	else
928
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
929
-}
930 987
 
931 988
 /**
932 989
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -943,10 +1000,11 @@  discard block
 block discarded – undo
943 1000
 		'\\' => '\\\\',
944 1001
 	);
945 1002
 
946
-	if ($translate_human_wildcards)
947
-		$replacements += array(
1003
+	if ($translate_human_wildcards) {
1004
+			$replacements += array(
948 1005
 			'*' => '%',
949 1006
 		);
1007
+	}
950 1008
 
951 1009
 	return strtr($string, $replacements);
952 1010
 }
@@ -975,14 +1033,16 @@  discard block
 block discarded – undo
975 1033
 	static $pg_error_data_prep;
976 1034
 
977 1035
 	// without database we can't do anything
978
-	if (empty($db_connection))
979
-		return;
1036
+	if (empty($db_connection)) {
1037
+			return;
1038
+	}
980 1039
 
981
-	if (empty($pg_error_data_prep))
982
-			$pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors',
1040
+	if (empty($pg_error_data_prep)) {
1041
+				$pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors',
983 1042
 				'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace)
984 1043
 													VALUES(		$1,		$2,		$3, $4, 	$5,		$6,			$7,		$8,	$9, $10)'
985 1044
 			);
1045
+	}
986 1046
 
987 1047
 	pg_execute($db_connection, 'smf_log_errors', $error_array);
988 1048
 }
@@ -1002,8 +1062,9 @@  discard block
 block discarded – undo
1002 1062
 	$count = count($array_values);
1003 1063
 	$then = ($desc ? ' THEN -' : ' THEN ');
1004 1064
 
1005
-	for ($i = 0; $i < $count; $i++)
1006
-		$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1065
+	for ($i = 0; $i < $count; $i++) {
1066
+			$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1067
+	}
1007 1068
 
1008 1069
 	$return .= 'END';
1009 1070
 	return $return;
@@ -1026,11 +1087,13 @@  discard block
 block discarded – undo
1026 1087
 		//pg 9.5 got replace support
1027 1088
 		$pg_version = $smcFunc['db_get_version']();
1028 1089
 		// if we got a Beta Version
1029
-		if (stripos($pg_version, 'beta') !== false)
1030
-			$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0';
1090
+		if (stripos($pg_version, 'beta') !== false) {
1091
+					$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0';
1092
+		}
1031 1093
 		// or RC
1032
-		if (stripos($pg_version, 'rc') !== false)
1033
-			$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0';
1094
+		if (stripos($pg_version, 'rc') !== false) {
1095
+					$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0';
1096
+		}
1034 1097
 
1035 1098
 		$replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false);
1036 1099
 	}
Please login to merge, or discard this patch.