Completed
Pull Request — release-2.1 (#5031)
by Mathias
07:04
created
Sources/Subs-Db-postgresql.php 1 patch
Braces   +241 added lines, -177 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',
@@ -66,15 +67,18 @@  discard block
 block discarded – undo
66 67
 			'db_native_replace'			=> 'smf_db_native_replace',
67 68
 			'db_cte_support'			=> 'smf_db_cte_support',
68 69
 		);
70
+	}
69 71
 
70 72
 	// We are not going to make it very far without these.
71
-	if (!function_exists('pg_pconnect'))
72
-		display_db_error();
73
+	if (!function_exists('pg_pconnect')) {
74
+			display_db_error();
75
+	}
73 76
 
74
-	if (!empty($db_options['persist']))
75
-		$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'] . '\''));
76
-	else
77
-		$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'] . '\''));
77
+	if (!empty($db_options['persist'])) {
78
+			$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'] . '\''));
79
+	} else {
80
+			$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'] . '\''));
81
+	}
78 82
 
79 83
 	// Something's wrong, show an error if its fatal (which we assume it is)
80 84
 	if (!$connection)
@@ -82,15 +86,15 @@  discard block
 block discarded – undo
82 86
 		if (!empty($db_options['non_fatal']))
83 87
 		{
84 88
 			return null;
85
-		}
86
-		else
89
+		} else
87 90
 		{
88 91
 			display_db_error();
89 92
 		}
90 93
 	}
91 94
 
92
-	if (!empty($db_options['db_mb4']))
93
-		$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
95
+	if (!empty($db_options['db_mb4'])) {
96
+			$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
97
+	}
94 98
 
95 99
 	return $connection;
96 100
 }
@@ -137,31 +141,38 @@  discard block
 block discarded – undo
137 141
 
138 142
 	list ($values, $connection) = $db_callback;
139 143
 
140
-	if ($matches[1] === 'db_prefix')
141
-		return $db_prefix;
144
+	if ($matches[1] === 'db_prefix') {
145
+			return $db_prefix;
146
+	}
142 147
 
143
-	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false)
144
-		return $user_info[$matches[1]];
148
+	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) {
149
+			return $user_info[$matches[1]];
150
+	}
145 151
 
146
-	if ($matches[1] === 'empty')
147
-		return '\'\'';
152
+	if ($matches[1] === 'empty') {
153
+			return '\'\'';
154
+	}
148 155
 
149
-	if (!isset($matches[2]))
150
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
156
+	if (!isset($matches[2])) {
157
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
158
+	}
151 159
 
152
-	if ($matches[1] === 'literal')
153
-		return '\'' . pg_escape_string($matches[2]) . '\'';
160
+	if ($matches[1] === 'literal') {
161
+			return '\'' . pg_escape_string($matches[2]) . '\'';
162
+	}
154 163
 
155
-	if (!isset($values[$matches[2]]))
156
-		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__);
164
+	if (!isset($values[$matches[2]])) {
165
+			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__);
166
+	}
157 167
 
158 168
 	$replacement = $values[$matches[2]];
159 169
 
160 170
 	switch ($matches[1])
161 171
 	{
162 172
 		case 'int':
163
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
164
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
173
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
174
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
175
+			}
165 176
 			return (string) (int) $replacement;
166 177
 		break;
167 178
 
@@ -173,65 +184,73 @@  discard block
 block discarded – undo
173 184
 		case 'array_int':
174 185
 			if (is_array($replacement))
175 186
 			{
176
-				if (empty($replacement))
177
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
187
+				if (empty($replacement)) {
188
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
189
+				}
178 190
 
179 191
 				foreach ($replacement as $key => $value)
180 192
 				{
181
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
182
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
193
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
194
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
195
+					}
183 196
 
184 197
 					$replacement[$key] = (string) (int) $value;
185 198
 				}
186 199
 
187 200
 				return implode(', ', $replacement);
201
+			} else {
202
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
188 203
 			}
189
-			else
190
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
191 204
 
192 205
 		break;
193 206
 
194 207
 		case 'array_string':
195 208
 			if (is_array($replacement))
196 209
 			{
197
-				if (empty($replacement))
198
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
210
+				if (empty($replacement)) {
211
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
212
+				}
199 213
 
200
-				foreach ($replacement as $key => $value)
201
-					$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value));
214
+				foreach ($replacement as $key => $value) {
215
+									$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value));
216
+				}
202 217
 
203 218
 				return implode(', ', $replacement);
219
+			} else {
220
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
204 221
 			}
205
-			else
206
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
207 222
 		break;
208 223
 
209 224
 		case 'date':
210
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
211
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date';
212
-			else
213
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
225
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
226
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date';
227
+			} else {
228
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
229
+			}
214 230
 		break;
215 231
 
216 232
 		case 'time':
217
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
218
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time';
219
-			else
220
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
233
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
234
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time';
235
+			} else {
236
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
237
+			}
221 238
 		break;
222 239
 
223 240
 		case 'datetime':
224
-			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)
225
-				return 'to_timestamp('.
241
+			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) {
242
+							return 'to_timestamp('.
226 243
 					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]).
227 244
 					',\'YYYY-MM-DD HH24:MI:SS\')';
228
-			else
229
-				smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
245
+			} else {
246
+							smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
247
+			}
230 248
 		break;
231 249
 
232 250
 		case 'float':
233
-			if (!is_numeric($replacement))
234
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
251
+			if (!is_numeric($replacement)) {
252
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
253
+			}
235 254
 			return (string) (float) $replacement;
236 255
 		break;
237 256
 
@@ -244,31 +263,36 @@  discard block
 block discarded – undo
244 263
 		break;
245 264
 
246 265
 		case 'inet':
247
-			if ($replacement == 'null' || $replacement == '')
248
-				return 'null';
249
-			if (inet_pton($replacement) === false)
250
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
266
+			if ($replacement == 'null' || $replacement == '') {
267
+							return 'null';
268
+			}
269
+			if (inet_pton($replacement) === false) {
270
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
271
+			}
251 272
 			return sprintf('\'%1$s\'::inet', pg_escape_string($replacement));
252 273
 
253 274
 		case 'array_inet':
254 275
 			if (is_array($replacement))
255 276
 			{
256
-				if (empty($replacement))
257
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
277
+				if (empty($replacement)) {
278
+									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
279
+				}
258 280
 
259 281
 				foreach ($replacement as $key => $value)
260 282
 				{
261
-					if ($replacement == 'null' || $replacement == '')
262
-						$replacement[$key] = 'null';
263
-					if (!isValidIP($value))
264
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
283
+					if ($replacement == 'null' || $replacement == '') {
284
+											$replacement[$key] = 'null';
285
+					}
286
+					if (!isValidIP($value)) {
287
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
288
+					}
265 289
 					$replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value));
266 290
 				}
267 291
 
268 292
 				return implode(', ', $replacement);
293
+			} else {
294
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
269 295
 			}
270
-			else
271
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
272 296
 		break;
273 297
 
274 298
 		default:
@@ -356,14 +380,16 @@  discard block
 block discarded – undo
356 380
 		),
357 381
 	);
358 382
 
359
-	if (isset($replacements[$identifier]))
360
-		$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
383
+	if (isset($replacements[$identifier])) {
384
+			$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
385
+	}
361 386
 
362 387
 	// Limits need to be a little different.
363 388
 	$db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string);
364 389
 
365
-	if (trim($db_string) == '')
366
-		return false;
390
+	if (trim($db_string) == '') {
391
+			return false;
392
+	}
367 393
 
368 394
 	// Comments that are allowed in a query are preg_removed.
369 395
 	static $allowed_comments_from = array(
@@ -383,8 +409,9 @@  discard block
 block discarded – undo
383 409
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
384 410
 	$db_replace_result = 0;
385 411
 
386
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
387
-		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
+	}
388 415
 
389 416
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
390 417
 	{
@@ -409,17 +436,18 @@  discard block
 block discarded – undo
409 436
 		while (true)
410 437
 		{
411 438
 			$pos = strpos($db_string_1, '\'', $pos + 1);
412
-			if ($pos === false)
413
-				break;
439
+			if ($pos === false) {
440
+							break;
441
+			}
414 442
 			$clean .= substr($db_string_1, $old_pos, $pos - $old_pos);
415 443
 
416 444
 			while (true)
417 445
 			{
418 446
 				$pos1 = strpos($db_string_1, '\'', $pos + 1);
419 447
 				$pos2 = strpos($db_string_1, '\\', $pos + 1);
420
-				if ($pos1 === false)
421
-					break;
422
-				elseif ($pos2 === false || $pos2 > $pos1)
448
+				if ($pos1 === false) {
449
+									break;
450
+				} elseif ($pos2 === false || $pos2 > $pos1)
423 451
 				{
424 452
 					$pos = $pos1;
425 453
 					break;
@@ -435,16 +463,19 @@  discard block
 block discarded – undo
435 463
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
436 464
 
437 465
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
438
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
439
-			$fail = true;
466
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
467
+					$fail = true;
468
+		}
440 469
 		// Trying to change passwords, slow us down, or something?
441
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
442
-			$fail = true;
443
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
444
-			$fail = true;
470
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
471
+					$fail = true;
472
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
473
+					$fail = true;
474
+		}
445 475
 
446
-		if (!empty($fail) && function_exists('log_error'))
447
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
476
+		if (!empty($fail) && function_exists('log_error')) {
477
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
478
+		}
448 479
 	}
449 480
 
450 481
 	// Set optimize stuff
@@ -471,8 +502,9 @@  discard block
 block discarded – undo
471 502
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
472 503
 
473 504
 		// Initialize $db_cache if not already initialized.
474
-		if (!isset($db_cache))
475
-			$db_cache = array();
505
+		if (!isset($db_cache)) {
506
+					$db_cache = array();
507
+		}
476 508
 
477 509
 		if (!empty($_SESSION['debug_redirect']))
478 510
 		{
@@ -490,12 +522,14 @@  discard block
 block discarded – undo
490 522
 
491 523
 	$db_last_result = @pg_query($connection, $db_string);
492 524
 
493
-	if ($db_last_result === false && empty($db_values['db_error_skip']))
494
-		$db_last_result = smf_db_error($db_string, $connection);
525
+	if ($db_last_result === false && empty($db_values['db_error_skip'])) {
526
+			$db_last_result = smf_db_error($db_string, $connection);
527
+	}
495 528
 
496 529
 	// Debugging.
497
-	if (isset($db_show_debug) && $db_show_debug === true)
498
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
530
+	if (isset($db_show_debug) && $db_show_debug === true) {
531
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
532
+	}
499 533
 
500 534
 	return $db_last_result;
501 535
 }
@@ -512,10 +546,11 @@  discard block
 block discarded – undo
512 546
 {
513 547
 	global $db_last_result, $db_replace_result;
514 548
 
515
-	if ($db_replace_result)
516
-		return $db_replace_result;
517
-	elseif ($result === null && !$db_last_result)
518
-		return 0;
549
+	if ($db_replace_result) {
550
+			return $db_replace_result;
551
+	} elseif ($result === null && !$db_last_result) {
552
+			return 0;
553
+	}
519 554
 
520 555
 	return pg_affected_rows($result === null ? $db_last_result : $result);
521 556
 }
@@ -539,8 +574,9 @@  discard block
 block discarded – undo
539 574
 		array(
540 575
 		)
541 576
 	);
542
-	if (!$request)
543
-		return false;
577
+	if (!$request) {
578
+			return false;
579
+	}
544 580
 	list ($lastID) = $smcFunc['db_fetch_row']($request);
545 581
 	$smcFunc['db_free_result']($request);
546 582
 
@@ -561,12 +597,13 @@  discard block
 block discarded – undo
561 597
 	// Decide which connection to use
562 598
 	$connection = $connection === null ? $db_connection : $connection;
563 599
 
564
-	if ($type == 'begin')
565
-		return @pg_query($connection, 'BEGIN');
566
-	elseif ($type == 'rollback')
567
-		return @pg_query($connection, 'ROLLBACK');
568
-	elseif ($type == 'commit')
569
-		return @pg_query($connection, 'COMMIT');
600
+	if ($type == 'begin') {
601
+			return @pg_query($connection, 'BEGIN');
602
+	} elseif ($type == 'rollback') {
603
+			return @pg_query($connection, 'ROLLBACK');
604
+	} elseif ($type == 'commit') {
605
+			return @pg_query($connection, 'COMMIT');
606
+	}
570 607
 
571 608
 	return false;
572 609
 }
@@ -594,19 +631,22 @@  discard block
 block discarded – undo
594 631
 	$query_error = @pg_last_error($connection);
595 632
 
596 633
 	// Log the error.
597
-	if (function_exists('log_error'))
598
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line);
634
+	if (function_exists('log_error')) {
635
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line);
636
+	}
599 637
 
600 638
 	// Nothing's defined yet... just die with it.
601
-	if (empty($context) || empty($txt))
602
-		die($query_error);
639
+	if (empty($context) || empty($txt)) {
640
+			die($query_error);
641
+	}
603 642
 
604 643
 	// Show an error message, if possible.
605 644
 	$context['error_title'] = $txt['database_error'];
606
-	if (allowedTo('admin_forum'))
607
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
608
-	else
609
-		$context['error_message'] = $txt['try_again'];
645
+	if (allowedTo('admin_forum')) {
646
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
647
+	} else {
648
+			$context['error_message'] = $txt['try_again'];
649
+	}
610 650
 
611 651
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
612 652
 	{
@@ -628,12 +668,14 @@  discard block
 block discarded – undo
628 668
 {
629 669
 	global $db_row_count;
630 670
 
631
-	if ($counter !== false)
632
-		return pg_fetch_row($request, $counter);
671
+	if ($counter !== false) {
672
+			return pg_fetch_row($request, $counter);
673
+	}
633 674
 
634 675
 	// Reset the row counter...
635
-	if (!isset($db_row_count[(int) $request]))
636
-		$db_row_count[(int) $request] = 0;
676
+	if (!isset($db_row_count[(int) $request])) {
677
+			$db_row_count[(int) $request] = 0;
678
+	}
637 679
 
638 680
 	// Return the right row.
639 681
 	return @pg_fetch_row($request, $db_row_count[(int) $request]++);
@@ -650,12 +692,14 @@  discard block
 block discarded – undo
650 692
 {
651 693
 	global $db_row_count;
652 694
 
653
-	if ($counter !== false)
654
-		return pg_fetch_assoc($request, $counter);
695
+	if ($counter !== false) {
696
+			return pg_fetch_assoc($request, $counter);
697
+	}
655 698
 
656 699
 	// Reset the row counter...
657
-	if (!isset($db_row_count[(int) $request]))
658
-		$db_row_count[(int) $request] = 0;
700
+	if (!isset($db_row_count[(int) $request])) {
701
+			$db_row_count[(int) $request] = 0;
702
+	}
659 703
 
660 704
 	// Return the right row.
661 705
 	return @pg_fetch_assoc($request, $db_row_count[(int) $request]++);
@@ -708,19 +752,22 @@  discard block
 block discarded – undo
708 752
 
709 753
 	$replace = '';
710 754
 
711
-	if (empty($data))
712
-		return;
755
+	if (empty($data)) {
756
+			return;
757
+	}
713 758
 
714
-	if (!is_array($data[array_rand($data)]))
715
-		$data = array($data);
759
+	if (!is_array($data[array_rand($data)])) {
760
+			$data = array($data);
761
+	}
716 762
 
717 763
 	// Replace the prefix holder with the actual prefix.
718 764
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
719 765
 
720 766
 	// Sanity check for replace is key part of the columns array
721
-	if ($method == 'replace' && count(array_intersect_key($columns, array_flip($keys))) !== count($keys))
722
-		smf_db_error_backtrace('Primary Key field missing in insert call',
767
+	if ($method == 'replace' && count(array_intersect_key($columns, array_flip($keys))) !== count($keys)) {
768
+			smf_db_error_backtrace('Primary Key field missing in insert call',
723 769
 				'Change the methode of db insert to insert or add the pk field to the columns array', E_USER_ERROR, __FILE__, __LINE__);
770
+	}
724 771
 
725 772
 	// PostgreSQL doesn't support replace: we implement a MySQL-compatible behavior instead
726 773
 	if ($method == 'replace' || $method == 'ignore')
@@ -743,32 +790,35 @@  discard block
 block discarded – undo
743 790
 					$key_str .= ($count_pk > 0 ? ',' : '');
744 791
 					$key_str .= $columnName;
745 792
 					$count_pk++;
746
-				}
747
-				else if ($method == 'replace') //normal field
793
+				} else if ($method == 'replace') {
794
+					//normal field
748 795
 				{
749 796
 					$col_str .= ($count > 0 ? ',' : '');
797
+				}
750 798
 					$col_str .= $columnName . ' = EXCLUDED.' . $columnName;
751 799
 					$count++;
752 800
 				}
753 801
 			}
754
-			if ($method == 'replace')
755
-				$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str;
756
-			else
757
-				$replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING';
758
-		}
759
-		else if ($method == 'replace')
802
+			if ($method == 'replace') {
803
+							$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str;
804
+			} else {
805
+							$replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING';
806
+			}
807
+		} else if ($method == 'replace')
760 808
 		{
761 809
 			foreach ($columns as $columnName => $type)
762 810
 			{
763 811
 				// Are we restricting the length?
764
-				if (strpos($type, 'string-') !== false)
765
-					$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count);
766
-				else
767
-					$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count);
812
+				if (strpos($type, 'string-') !== false) {
813
+									$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count);
814
+				} else {
815
+									$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count);
816
+				}
768 817
 
769 818
 				// A key? That's what we were looking for.
770
-				if (in_array($columnName, $keys))
771
-					$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2);
819
+				if (in_array($columnName, $keys)) {
820
+									$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2);
821
+				}
772 822
 				$count++;
773 823
 			}
774 824
 
@@ -804,10 +854,11 @@  discard block
 block discarded – undo
804 854
 		foreach ($columns as $columnName => $type)
805 855
 		{
806 856
 			// Are we restricting the length?
807
-			if (strpos($type, 'string-') !== false)
808
-				$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
809
-			else
810
-				$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
857
+			if (strpos($type, 'string-') !== false) {
858
+							$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
859
+			} else {
860
+							$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
861
+			}
811 862
 		}
812 863
 		$insertData = substr($insertData, 0, -2) . ')';
813 864
 
@@ -816,8 +867,9 @@  discard block
 block discarded – undo
816 867
 
817 868
 		// Here's where the variables are injected to the query.
818 869
 		$insertRows = array();
819
-		foreach ($data as $dataRow)
820
-			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
870
+		foreach ($data as $dataRow) {
871
+					$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
872
+		}
821 873
 
822 874
 		// Do the insert.
823 875
 		$request = $smcFunc['db_query']('', '
@@ -834,19 +886,21 @@  discard block
 block discarded – undo
834 886
 
835 887
 		if ($with_returning && $request !== false)
836 888
 		{
837
-			if ($returnmode === 2)
838
-				$return_var = array();
889
+			if ($returnmode === 2) {
890
+							$return_var = array();
891
+			}
839 892
 
840 893
 			while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning)
841 894
 			{
842
-				if (is_numeric($row[0])) // try to emulate mysql limitation
895
+				if (is_numeric($row[0])) {
896
+					// try to emulate mysql limitation
843 897
 				{
844 898
 					if ($returnmode === 1)
845 899
 						$return_var = $row[0];
846
-					elseif ($returnmode === 2)
847
-						$return_var[] = $row[0];
848
-				}
849
-				else
900
+				} elseif ($returnmode === 2) {
901
+											$return_var[] = $row[0];
902
+					}
903
+				} else
850 904
 				{
851 905
 					$with_returning = false;
852 906
 					trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR);
@@ -855,9 +909,10 @@  discard block
 block discarded – undo
855 909
 		}
856 910
 	}
857 911
 
858
-	if ($with_returning && !empty($return_var))
859
-		return $return_var;
860
-}
912
+	if ($with_returning && !empty($return_var)) {
913
+			return $return_var;
914
+	}
915
+	}
861 916
 
862 917
 /**
863 918
  * Dummy function really. Doesn't do anything on PostgreSQL.
@@ -894,8 +949,9 @@  discard block
 block discarded – undo
894 949
  */
895 950
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
896 951
 {
897
-	if (empty($log_message))
898
-		$log_message = $error_message;
952
+	if (empty($log_message)) {
953
+			$log_message = $error_message;
954
+	}
899 955
 
900 956
 	foreach (debug_backtrace() as $step)
901 957
 	{
@@ -914,12 +970,14 @@  discard block
 block discarded – undo
914 970
 	}
915 971
 
916 972
 	// A special case - we want the file and line numbers for debugging.
917
-	if ($error_type == 'return')
918
-		return array($file, $line);
973
+	if ($error_type == 'return') {
974
+			return array($file, $line);
975
+	}
919 976
 
920 977
 	// Is always a critical error.
921
-	if (function_exists('log_error'))
922
-		log_error($log_message, 'critical', $file, $line);
978
+	if (function_exists('log_error')) {
979
+			log_error($log_message, 'critical', $file, $line);
980
+	}
923 981
 
924 982
 	if (function_exists('fatal_error'))
925 983
 	{
@@ -927,12 +985,12 @@  discard block
 block discarded – undo
927 985
 
928 986
 		// Cannot continue...
929 987
 		exit;
988
+	} elseif ($error_type) {
989
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
990
+	} else {
991
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
992
+	}
930 993
 	}
931
-	elseif ($error_type)
932
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
933
-	else
934
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
935
-}
936 994
 
937 995
 /**
938 996
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -949,10 +1007,11 @@  discard block
 block discarded – undo
949 1007
 		'\\' => '\\\\',
950 1008
 	);
951 1009
 
952
-	if ($translate_human_wildcards)
953
-		$replacements += array(
1010
+	if ($translate_human_wildcards) {
1011
+			$replacements += array(
954 1012
 			'*' => '%',
955 1013
 		);
1014
+	}
956 1015
 
957 1016
 	return strtr($string, $replacements);
958 1017
 }
@@ -981,14 +1040,16 @@  discard block
 block discarded – undo
981 1040
 	static $pg_error_data_prep;
982 1041
 
983 1042
 	// without database we can't do anything
984
-	if (empty($db_connection))
985
-		return;
1043
+	if (empty($db_connection)) {
1044
+			return;
1045
+	}
986 1046
 
987
-	if (empty($pg_error_data_prep))
988
-			$pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors',
1047
+	if (empty($pg_error_data_prep)) {
1048
+				$pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors',
989 1049
 				'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace)
990 1050
 													VALUES(		$1,		$2,		$3, $4, 	$5,		$6,			$7,		$8,	$9, $10)'
991 1051
 			);
1052
+	}
992 1053
 
993 1054
 	pg_execute($db_connection, 'smf_log_errors', $error_array);
994 1055
 }
@@ -1008,8 +1069,9 @@  discard block
 block discarded – undo
1008 1069
 	$count = count($array_values);
1009 1070
 	$then = ($desc ? ' THEN -' : ' THEN ');
1010 1071
 
1011
-	for ($i = 0; $i < $count; $i++)
1012
-		$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1072
+	for ($i = 0; $i < $count; $i++) {
1073
+			$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1074
+	}
1013 1075
 
1014 1076
 	$return .= 'END';
1015 1077
 	return $return;
@@ -1032,11 +1094,13 @@  discard block
 block discarded – undo
1032 1094
 		//pg 9.5 got replace support
1033 1095
 		$pg_version = $smcFunc['db_get_version']();
1034 1096
 		// if we got a Beta Version
1035
-		if (stripos($pg_version, 'beta') !== false)
1036
-			$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0';
1097
+		if (stripos($pg_version, 'beta') !== false) {
1098
+					$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0';
1099
+		}
1037 1100
 		// or RC
1038
-		if (stripos($pg_version, 'rc') !== false)
1039
-			$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0';
1101
+		if (stripos($pg_version, 'rc') !== false) {
1102
+					$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0';
1103
+		}
1040 1104
 
1041 1105
 		$replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false);
1042 1106
 	}
Please login to merge, or discard this patch.
Sources/Subs-Db-mysql.php 1 patch
Braces   +271 added lines, -198 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',
@@ -65,13 +66,16 @@  discard block
 block discarded – undo
65 66
 			'db_native_replace'			=> 'smf_db_native_replace',
66 67
 			'db_cte_support'			=> 'smf_db_cte_support',
67 68
 		);
69
+	}
68 70
 
69
-	if (!empty($db_options['persist']))
70
-		$db_server = 'p:' . $db_server;
71
+	if (!empty($db_options['persist'])) {
72
+			$db_server = 'p:' . $db_server;
73
+	}
71 74
 
72 75
 	// We are not going to make it very far without these.
73
-	if (!function_exists('mysqli_init') || !function_exists('mysqli_real_connect'))
74
-		display_db_error();
76
+	if (!function_exists('mysqli_init') || !function_exists('mysqli_real_connect')) {
77
+			display_db_error();
78
+	}
75 79
 
76 80
 	$connection = mysqli_init();
77 81
 
@@ -81,29 +85,33 @@  discard block
 block discarded – undo
81 85
 
82 86
 	if ($connection)
83 87
 	{
84
-		if (!empty($db_options['port']))
85
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags);
86
-		else
87
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags);
88
+		if (!empty($db_options['port'])) {
89
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags);
90
+		} else {
91
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags);
92
+		}
88 93
 	}
89 94
 
90 95
 	// Something's wrong, show an error if its fatal (which we assume it is)
91 96
 	if ($success === false)
92 97
 	{
93
-		if (!empty($db_options['non_fatal']))
94
-			return null;
95
-		else
96
-			display_db_error();
98
+		if (!empty($db_options['non_fatal'])) {
99
+					return null;
100
+		} else {
101
+					display_db_error();
102
+		}
97 103
 	}
98 104
 
99 105
 	// Select the database, unless told not to
100
-	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal']))
101
-		display_db_error();
106
+	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal'])) {
107
+			display_db_error();
108
+	}
102 109
 
103 110
 	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\'');
104 111
 
105
-	if (!empty($db_options['db_mb4']))
106
-		$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
112
+	if (!empty($db_options['db_mb4'])) {
113
+			$smcFunc['db_mb4'] = (bool) $db_options['db_mb4'];
114
+	}
107 115
 
108 116
 	return $connection;
109 117
 }
@@ -174,34 +182,42 @@  discard block
 block discarded – undo
174 182
 	global $db_callback, $user_info, $db_prefix, $smcFunc;
175 183
 
176 184
 	list ($values, $connection) = $db_callback;
177
-	if (!is_object($connection))
178
-		display_db_error();
185
+	if (!is_object($connection)) {
186
+			display_db_error();
187
+	}
179 188
 
180
-	if ($matches[1] === 'db_prefix')
181
-		return $db_prefix;
189
+	if ($matches[1] === 'db_prefix') {
190
+			return $db_prefix;
191
+	}
182 192
 
183
-	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false)
184
-		return $user_info[$matches[1]];
193
+	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) {
194
+			return $user_info[$matches[1]];
195
+	}
185 196
 
186
-	if ($matches[1] === 'empty')
187
-		return '\'\'';
197
+	if ($matches[1] === 'empty') {
198
+			return '\'\'';
199
+	}
188 200
 
189
-	if (!isset($matches[2]))
190
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
201
+	if (!isset($matches[2])) {
202
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
203
+	}
191 204
 
192
-	if ($matches[1] === 'literal')
193
-		return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
205
+	if ($matches[1] === 'literal') {
206
+			return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
207
+	}
194 208
 
195
-	if (!isset($values[$matches[2]]))
196
-		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__);
209
+	if (!isset($values[$matches[2]])) {
210
+			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__);
211
+	}
197 212
 
198 213
 	$replacement = $values[$matches[2]];
199 214
 
200 215
 	switch ($matches[1])
201 216
 	{
202 217
 		case 'int':
203
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
204
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
218
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
219
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
220
+			}
205 221
 			return (string) (int) $replacement;
206 222
 		break;
207 223
 
@@ -213,65 +229,73 @@  discard block
 block discarded – undo
213 229
 		case 'array_int':
214 230
 			if (is_array($replacement))
215 231
 			{
216
-				if (empty($replacement))
217
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
232
+				if (empty($replacement)) {
233
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
234
+				}
218 235
 
219 236
 				foreach ($replacement as $key => $value)
220 237
 				{
221
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
222
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
238
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
239
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
240
+					}
223 241
 
224 242
 					$replacement[$key] = (string) (int) $value;
225 243
 				}
226 244
 
227 245
 				return implode(', ', $replacement);
246
+			} else {
247
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
228 248
 			}
229
-			else
230
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
231 249
 
232 250
 		break;
233 251
 
234 252
 		case 'array_string':
235 253
 			if (is_array($replacement))
236 254
 			{
237
-				if (empty($replacement))
238
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
255
+				if (empty($replacement)) {
256
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
257
+				}
239 258
 
240
-				foreach ($replacement as $key => $value)
241
-					$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
259
+				foreach ($replacement as $key => $value) {
260
+									$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
261
+				}
242 262
 
243 263
 				return implode(', ', $replacement);
264
+			} else {
265
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
244 266
 			}
245
-			else
246
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
247 267
 		break;
248 268
 
249 269
 		case 'date':
250
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
251
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
252
-			else
253
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
270
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
271
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
272
+			} else {
273
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
274
+			}
254 275
 		break;
255 276
 
256 277
 		case 'time':
257
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
258
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
259
-			else
260
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
278
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
279
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
280
+			} else {
281
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
282
+			}
261 283
 		break;
262 284
 
263 285
 		case 'datetime':
264
-			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)
265
-				return 'str_to_date('.
286
+			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) {
287
+							return 'str_to_date('.
266 288
 					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]).
267 289
 					',\'%Y-%m-%d %h:%i:%s\')';
268
-			else
269
-				smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
290
+			} else {
291
+							smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
292
+			}
270 293
 		break;
271 294
 
272 295
 		case 'float':
273
-			if (!is_numeric($replacement))
274
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
296
+			if (!is_numeric($replacement)) {
297
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
298
+			}
275 299
 			return (string) (float) $replacement;
276 300
 		break;
277 301
 
@@ -285,32 +309,37 @@  discard block
 block discarded – undo
285 309
 		break;
286 310
 
287 311
 		case 'inet':
288
-			if ($replacement == 'null' || $replacement == '')
289
-				return 'null';
290
-			if (!isValidIP($replacement))
291
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
312
+			if ($replacement == 'null' || $replacement == '') {
313
+							return 'null';
314
+			}
315
+			if (!isValidIP($replacement)) {
316
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
317
+			}
292 318
 			//we don't use the native support of mysql > 5.6.2
293 319
 			return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement)));
294 320
 
295 321
 		case 'array_inet':
296 322
 			if (is_array($replacement))
297 323
 			{
298
-				if (empty($replacement))
299
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
324
+				if (empty($replacement)) {
325
+									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
326
+				}
300 327
 
301 328
 				foreach ($replacement as $key => $value)
302 329
 				{
303
-					if ($replacement == 'null' || $replacement == '')
304
-						$replacement[$key] = 'null';
305
-					if (!isValidIP($value))
306
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
330
+					if ($replacement == 'null' || $replacement == '') {
331
+											$replacement[$key] = 'null';
332
+					}
333
+					if (!isValidIP($value)) {
334
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
335
+					}
307 336
 					$replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value)));
308 337
 				}
309 338
 
310 339
 				return implode(', ', $replacement);
340
+			} else {
341
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
311 342
 			}
312
-			else
313
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
314 343
 		break;
315 344
 
316 345
 		default:
@@ -381,18 +410,20 @@  discard block
 block discarded – undo
381 410
 	// One more query....
382 411
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
383 412
 
384
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
385
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
413
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
414
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
415
+	}
386 416
 
387 417
 	// Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By
388 418
 	if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && preg_match('~^\s+SELECT~i', $db_string))
389 419
 	{
390 420
 		// Add before LIMIT
391
-		if ($pos = strpos($db_string, 'LIMIT '))
392
-			$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
393
-		else
394
-			// Append it.
421
+		if ($pos = strpos($db_string, 'LIMIT ')) {
422
+					$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
423
+		} else {
424
+					// Append it.
395 425
 			$db_string .= "\n\t\t\tORDER BY null";
426
+		}
396 427
 	}
397 428
 
398 429
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
@@ -418,17 +449,18 @@  discard block
 block discarded – undo
418 449
 		while (true)
419 450
 		{
420 451
 			$pos = strpos($db_string_1, '\'', $pos + 1);
421
-			if ($pos === false)
422
-				break;
452
+			if ($pos === false) {
453
+							break;
454
+			}
423 455
 			$clean .= substr($db_string_1, $old_pos, $pos - $old_pos);
424 456
 
425 457
 			while (true)
426 458
 			{
427 459
 				$pos1 = strpos($db_string_1, '\'', $pos + 1);
428 460
 				$pos2 = strpos($db_string_1, '\\', $pos + 1);
429
-				if ($pos1 === false)
430
-					break;
431
-				elseif ($pos2 === false || $pos2 > $pos1)
461
+				if ($pos1 === false) {
462
+									break;
463
+				} elseif ($pos2 === false || $pos2 > $pos1)
432 464
 				{
433 465
 					$pos = $pos1;
434 466
 					break;
@@ -444,16 +476,19 @@  discard block
 block discarded – undo
444 476
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
445 477
 
446 478
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
447
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
448
-			$fail = true;
479
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
480
+					$fail = true;
481
+		}
449 482
 		// Trying to change passwords, slow us down, or something?
450
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
451
-			$fail = true;
452
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
453
-			$fail = true;
483
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
484
+					$fail = true;
485
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
486
+					$fail = true;
487
+		}
454 488
 
455
-		if (!empty($fail) && function_exists('log_error'))
456
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
489
+		if (!empty($fail) && function_exists('log_error')) {
490
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
491
+		}
457 492
 	}
458 493
 
459 494
 	// Debugging.
@@ -463,8 +498,9 @@  discard block
 block discarded – undo
463 498
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
464 499
 
465 500
 		// Initialize $db_cache if not already initialized.
466
-		if (!isset($db_cache))
467
-			$db_cache = array();
501
+		if (!isset($db_cache)) {
502
+					$db_cache = array();
503
+		}
468 504
 
469 505
 		if (!empty($_SESSION['debug_redirect']))
470 506
 		{
@@ -480,17 +516,20 @@  discard block
 block discarded – undo
480 516
 		$db_cache[$db_count]['s'] = ($st = microtime(true)) - $time_start;
481 517
 	}
482 518
 
483
-	if (empty($db_unbuffered))
484
-		$ret = @mysqli_query($connection, $db_string);
485
-	else
486
-		$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
519
+	if (empty($db_unbuffered)) {
520
+			$ret = @mysqli_query($connection, $db_string);
521
+	} else {
522
+			$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
523
+	}
487 524
 
488
-	if ($ret === false && empty($db_values['db_error_skip']))
489
-		$ret = smf_db_error($db_string, $connection);
525
+	if ($ret === false && empty($db_values['db_error_skip'])) {
526
+			$ret = smf_db_error($db_string, $connection);
527
+	}
490 528
 
491 529
 	// Debugging.
492
-	if (isset($db_show_debug) && $db_show_debug === true)
493
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
530
+	if (isset($db_show_debug) && $db_show_debug === true) {
531
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
532
+	}
494 533
 
495 534
 	return $ret;
496 535
 }
@@ -537,12 +576,13 @@  discard block
 block discarded – undo
537 576
 	// Decide which connection to use
538 577
 	$connection = $connection === null ? $db_connection : $connection;
539 578
 
540
-	if ($type == 'begin')
541
-		return @mysqli_query($connection, 'BEGIN');
542
-	elseif ($type == 'rollback')
543
-		return @mysqli_query($connection, 'ROLLBACK');
544
-	elseif ($type == 'commit')
545
-		return @mysqli_query($connection, 'COMMIT');
579
+	if ($type == 'begin') {
580
+			return @mysqli_query($connection, 'BEGIN');
581
+	} elseif ($type == 'rollback') {
582
+			return @mysqli_query($connection, 'ROLLBACK');
583
+	} elseif ($type == 'commit') {
584
+			return @mysqli_query($connection, 'COMMIT');
585
+	}
546 586
 
547 587
 	return false;
548 588
 }
@@ -580,8 +620,9 @@  discard block
 block discarded – undo
580 620
 	//    1213: Deadlock found.
581 621
 
582 622
 	// Log the error.
583
-	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error'))
584
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
623
+	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) {
624
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
625
+	}
585 626
 
586 627
 	// Database error auto fixing ;).
587 628
 	if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1'))
@@ -590,8 +631,9 @@  discard block
 block discarded – undo
590 631
 		$old_cache = @$modSettings['cache_enable'];
591 632
 		$modSettings['cache_enable'] = '1';
592 633
 
593
-		if (($temp = cache_get_data('db_last_error', 600)) !== null)
594
-			$db_last_error = max(@$db_last_error, $temp);
634
+		if (($temp = cache_get_data('db_last_error', 600)) !== null) {
635
+					$db_last_error = max(@$db_last_error, $temp);
636
+		}
595 637
 
596 638
 		if (@$db_last_error < time() - 3600 * 24 * 3)
597 639
 		{
@@ -607,8 +649,9 @@  discard block
 block discarded – undo
607 649
 					foreach ($tables as $table)
608 650
 					{
609 651
 						// Now, it's still theoretically possible this could be an injection.  So backtick it!
610
-						if (trim($table) != '')
611
-							$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
652
+						if (trim($table) != '') {
653
+													$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
654
+						}
612 655
 					}
613 656
 				}
614 657
 
@@ -617,8 +660,9 @@  discard block
 block discarded – undo
617 660
 			// Table crashed.  Let's try to fix it.
618 661
 			elseif ($query_errno == 1016)
619 662
 			{
620
-				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0)
621
-					$fix_tables = array('`' . $match[1] . '`');
663
+				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) {
664
+									$fix_tables = array('`' . $match[1] . '`');
665
+				}
622 666
 			}
623 667
 			// Indexes crashed.  Should be easy to fix!
624 668
 			elseif ($query_errno == 1034 || $query_errno == 1035)
@@ -637,13 +681,15 @@  discard block
 block discarded – undo
637 681
 
638 682
 			// Make a note of the REPAIR...
639 683
 			cache_put_data('db_last_error', time(), 600);
640
-			if (($temp = cache_get_data('db_last_error', 600)) === null)
641
-				updateSettingsFile(array('db_last_error' => time()));
684
+			if (($temp = cache_get_data('db_last_error', 600)) === null) {
685
+							updateSettingsFile(array('db_last_error' => time()));
686
+			}
642 687
 
643 688
 			// Attempt to find and repair the broken table.
644
-			foreach ($fix_tables as $table)
645
-				$smcFunc['db_query']('', "
689
+			foreach ($fix_tables as $table) {
690
+							$smcFunc['db_query']('', "
646 691
 					REPAIR TABLE $table", false, false);
692
+			}
647 693
 
648 694
 			// And send off an email!
649 695
 			sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair'], null, 'dberror');
@@ -652,11 +698,12 @@  discard block
 block discarded – undo
652 698
 
653 699
 			// Try the query again...?
654 700
 			$ret = $smcFunc['db_query']('', $db_string, false, false);
655
-			if ($ret !== false)
656
-				return $ret;
701
+			if ($ret !== false) {
702
+							return $ret;
703
+			}
704
+		} else {
705
+					$modSettings['cache_enable'] = $old_cache;
657 706
 		}
658
-		else
659
-			$modSettings['cache_enable'] = $old_cache;
660 707
 
661 708
 		// Check for the "lost connection" or "deadlock found" errors - and try it just one more time.
662 709
 		if (in_array($query_errno, array(1205, 1213)))
@@ -669,24 +716,27 @@  discard block
 block discarded – undo
669 716
 					$ret = $smcFunc['db_query']('', $db_string, false, false);
670 717
 
671 718
 					$new_errno = mysqli_errno($db_connection);
672
-					if ($ret !== false || in_array($new_errno, array(1205, 1213)))
673
-						break;
719
+					if ($ret !== false || in_array($new_errno, array(1205, 1213))) {
720
+											break;
721
+					}
674 722
 				}
675 723
 
676 724
 				// If it failed again, shucks to be you... we're not trying it over and over.
677
-				if ($ret !== false)
678
-					return $ret;
725
+				if ($ret !== false) {
726
+									return $ret;
727
+				}
679 728
 			}
680 729
 		}
681 730
 		// Are they out of space, perhaps?
682 731
 		elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false))
683 732
 		{
684
-			if (!isset($txt))
685
-				$query_error .= ' - check database storage space.';
686
-			else
733
+			if (!isset($txt)) {
734
+							$query_error .= ' - check database storage space.';
735
+			} else
687 736
 			{
688
-				if (!isset($txt['mysql_error_space']))
689
-					loadLanguage('Errors');
737
+				if (!isset($txt['mysql_error_space'])) {
738
+									loadLanguage('Errors');
739
+				}
690 740
 
691 741
 				$query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space'];
692 742
 			}
@@ -694,15 +744,17 @@  discard block
 block discarded – undo
694 744
 	}
695 745
 
696 746
 	// Nothing's defined yet... just die with it.
697
-	if (empty($context) || empty($txt))
698
-		die($query_error);
747
+	if (empty($context) || empty($txt)) {
748
+			die($query_error);
749
+	}
699 750
 
700 751
 	// Show an error message, if possible.
701 752
 	$context['error_title'] = $txt['database_error'];
702
-	if (allowedTo('admin_forum'))
703
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
704
-	else
705
-		$context['error_message'] = $txt['try_again'];
753
+	if (allowedTo('admin_forum')) {
754
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
755
+	} else {
756
+			$context['error_message'] = $txt['try_again'];
757
+	}
706 758
 
707 759
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
708 760
 	{
@@ -734,8 +786,9 @@  discard block
 block discarded – undo
734 786
 	$return_var = null;
735 787
 
736 788
 	// With nothing to insert, simply return.
737
-	if (empty($data))
738
-		return;
789
+	if (empty($data)) {
790
+			return;
791
+	}
739 792
 
740 793
 	// Replace the prefix holder with the actual prefix.
741 794
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
@@ -745,23 +798,26 @@  discard block
 block discarded – undo
745 798
 	if (!empty($keys) && (count($keys) > 0) && $returnmode > 0)
746 799
 	{
747 800
 		$with_returning = true;
748
-		if ($returnmode == 2)
749
-			$return_var = array();
801
+		if ($returnmode == 2) {
802
+					$return_var = array();
803
+		}
750 804
 	}
751 805
 
752 806
 	// Inserting data as a single row can be done as a single array.
753
-	if (!is_array($data[array_rand($data)]))
754
-		$data = array($data);
807
+	if (!is_array($data[array_rand($data)])) {
808
+			$data = array($data);
809
+	}
755 810
 
756 811
 	// Create the mold for a single row insert.
757 812
 	$insertData = '(';
758 813
 	foreach ($columns as $columnName => $type)
759 814
 	{
760 815
 		// Are we restricting the length?
761
-		if (strpos($type, 'string-') !== false)
762
-			$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
763
-		else
764
-			$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
816
+		if (strpos($type, 'string-') !== false) {
817
+					$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
818
+		} else {
819
+					$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
820
+		}
765 821
 	}
766 822
 	$insertData = substr($insertData, 0, -2) . ')';
767 823
 
@@ -770,16 +826,18 @@  discard block
 block discarded – undo
770 826
 
771 827
 	// Here's where the variables are injected to the query.
772 828
 	$insertRows = array();
773
-	foreach ($data as $dataRow)
774
-		$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
829
+	foreach ($data as $dataRow) {
830
+			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
831
+	}
775 832
 
776 833
 	// Determine the method of insertion.
777 834
 	$queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT');
778 835
 
779 836
 	// Sanity check for replace is key part of the columns array
780
-	if ($method == 'replace' && count(array_intersect_key($columns, array_flip($keys))) !== count($keys))
781
-		smf_db_error_backtrace('Primary Key field missing in insert call',
837
+	if ($method == 'replace' && count(array_intersect_key($columns, array_flip($keys))) !== count($keys)) {
838
+			smf_db_error_backtrace('Primary Key field missing in insert call',
782 839
 				'Change the methode of db insert to insert or add the pk field to the columns array', E_USER_ERROR, __FILE__, __LINE__);
840
+	}
783 841
 
784 842
 	if (!$with_returning || $method != 'ingore')
785 843
 	{
@@ -795,8 +853,7 @@  discard block
 block discarded – undo
795 853
 			),
796 854
 			$connection
797 855
 		);
798
-	}
799
-	else //special way for ignore method with returning
856
+	} else //special way for ignore method with returning
800 857
 	{
801 858
 		$count = count($insertRows);
802 859
 		$ai = 0;
@@ -816,19 +873,21 @@  discard block
 block discarded – undo
816 873
 			);
817 874
 			$new_id = $smcFunc['db_insert_id']();
818 875
 
819
-			if ($last_id != $new_id) //the inserted value was new
876
+			if ($last_id != $new_id) {
877
+				//the inserted value was new
820 878
 			{
821 879
 				$ai = $new_id;
822 880
 			}
823
-			else	// the inserted value already exists we need to find the pk
881
+			} else	// the inserted value already exists we need to find the pk
824 882
 			{
825 883
 				$where_string = '';
826 884
 				$count2 = count($indexed_columns);
827 885
 				for ($x = 0; $x < $count2; $x++)
828 886
 				{
829 887
 					$where_string += key($indexed_columns[$x]) . ' = '. $insertRows[$i][$x];
830
-					if (($x + 1) < $count2)
831
-						$where_string += ' AND ';
888
+					if (($x + 1) < $count2) {
889
+											$where_string += ' AND ';
890
+					}
832 891
 				}
833 892
 
834 893
 				$request = $smcFunc['db_query']('','
@@ -844,25 +903,27 @@  discard block
 block discarded – undo
844 903
 				}
845 904
 			}
846 905
 
847
-			if ($returnmode == 1)
848
-				$return_var = $ai;
849
-			else if ($returnmode == 2)
850
-				$return_var[] = $ai;
906
+			if ($returnmode == 1) {
907
+							$return_var = $ai;
908
+			} else if ($returnmode == 2) {
909
+							$return_var[] = $ai;
910
+			}
851 911
 		}
852 912
 	}
853 913
 
854 914
 
855 915
 	if ($with_returning)
856 916
 	{
857
-		if ($returnmode == 1 && empty($return_var))
858
-			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
859
-		else if ($returnmode == 2 && empty($return_var))
917
+		if ($returnmode == 1 && empty($return_var)) {
918
+					$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
919
+		} else if ($returnmode == 2 && empty($return_var))
860 920
 		{
861 921
 			$return_var = array();
862 922
 			$count = count($insertRows);
863 923
 			$start = smf_db_insert_id($table, $keys[0]);
864
-			for ($i = 0; $i < $count; $i++ )
865
-				$return_var[] = $start + $i;
924
+			for ($i = 0; $i < $count; $i++ ) {
925
+							$return_var[] = $start + $i;
926
+			}
866 927
 		}
867 928
 		return $return_var;
868 929
 	}
@@ -880,8 +941,9 @@  discard block
 block discarded – undo
880 941
  */
881 942
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
882 943
 {
883
-	if (empty($log_message))
884
-		$log_message = $error_message;
944
+	if (empty($log_message)) {
945
+			$log_message = $error_message;
946
+	}
885 947
 
886 948
 	foreach (debug_backtrace() as $step)
887 949
 	{
@@ -900,12 +962,14 @@  discard block
 block discarded – undo
900 962
 	}
901 963
 
902 964
 	// A special case - we want the file and line numbers for debugging.
903
-	if ($error_type == 'return')
904
-		return array($file, $line);
965
+	if ($error_type == 'return') {
966
+			return array($file, $line);
967
+	}
905 968
 
906 969
 	// Is always a critical error.
907
-	if (function_exists('log_error'))
908
-		log_error($log_message, 'critical', $file, $line);
970
+	if (function_exists('log_error')) {
971
+			log_error($log_message, 'critical', $file, $line);
972
+	}
909 973
 
910 974
 	if (function_exists('fatal_error'))
911 975
 	{
@@ -913,12 +977,12 @@  discard block
 block discarded – undo
913 977
 
914 978
 		// Cannot continue...
915 979
 		exit;
980
+	} elseif ($error_type) {
981
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
982
+	} else {
983
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
984
+	}
916 985
 	}
917
-	elseif ($error_type)
918
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
919
-	else
920
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
921
-}
922 986
 
923 987
 /**
924 988
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -935,10 +999,11 @@  discard block
 block discarded – undo
935 999
 		'\\' => '\\\\',
936 1000
 	);
937 1001
 
938
-	if ($translate_human_wildcards)
939
-		$replacements += array(
1002
+	if ($translate_human_wildcards) {
1003
+			$replacements += array(
940 1004
 			'*' => '%',
941 1005
 		);
1006
+	}
942 1007
 
943 1008
 	return strtr($string, $replacements);
944 1009
 }
@@ -952,8 +1017,9 @@  discard block
 block discarded – undo
952 1017
  */
953 1018
 function smf_is_resource($result)
954 1019
 {
955
-	if ($result instanceof mysqli_result)
956
-		return true;
1020
+	if ($result instanceof mysqli_result) {
1021
+			return true;
1022
+	}
957 1023
 
958 1024
 	return false;
959 1025
 }
@@ -982,19 +1048,22 @@  discard block
 block discarded – undo
982 1048
 	static $mysql_error_data_prep;
983 1049
 
984 1050
 	// without database we can't do anything
985
-	if (empty($db_connection))
986
-		return;
1051
+	if (empty($db_connection)) {
1052
+			return;
1053
+	}
987 1054
 
988
-	if (empty($mysql_error_data_prep))
989
-			$mysql_error_data_prep = mysqli_prepare($db_connection,
1055
+	if (empty($mysql_error_data_prep)) {
1056
+				$mysql_error_data_prep = mysqli_prepare($db_connection,
990 1057
 				'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace)
991 1058
 													VALUES(		?,		?,		unhex(?), ?, 		?,		?,			?,		?,	?, ?)'
992 1059
 			);
1060
+	}
993 1061
 
994
-	if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false)
995
-		$error_array[2] = bin2hex(inet_pton($error_array[2]));
996
-	else
997
-		$error_array[2] = null;
1062
+	if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false) {
1063
+			$error_array[2] = bin2hex(inet_pton($error_array[2]));
1064
+	} else {
1065
+			$error_array[2] = null;
1066
+	}
998 1067
 	mysqli_stmt_bind_param($mysql_error_data_prep, 'iissssssis',
999 1068
 		$error_array[0], $error_array[1], $error_array[2], $error_array[3], $error_array[4], $error_array[5], $error_array[6],
1000 1069
 		$error_array[7], $error_array[8], $error_array[9]);
@@ -1016,8 +1085,9 @@  discard block
 block discarded – undo
1016 1085
 	$count = count($array_values);
1017 1086
 	$then = ($desc ? ' THEN -' : ' THEN ');
1018 1087
 
1019
-	for ($i = 0; $i < $count; $i++)
1020
-		$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1088
+	for ($i = 0; $i < $count; $i++) {
1089
+			$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1090
+	}
1021 1091
 
1022 1092
 	$return .= 'END';
1023 1093
 	return $return;
@@ -1043,17 +1113,20 @@  discard block
 block discarded – undo
1043 1113
 	global $smcFunc;
1044 1114
 	static $return;
1045 1115
 
1046
-	if (isset($return))
1047
-		return $return;
1116
+	if (isset($return)) {
1117
+			return $return;
1118
+	}
1048 1119
 
1049 1120
 	db_extend('extra');
1050 1121
 
1051 1122
 	$version = $smcFunc['db_get_version']();
1052 1123
 
1053
-	if (strpos(strtolower($version), 'mariadb') !== false)
1054
-		$return = version_compare($version, '10.2.2', '>=');
1055
-	else //mysql
1124
+	if (strpos(strtolower($version), 'mariadb') !== false) {
1125
+			$return = version_compare($version, '10.2.2', '>=');
1126
+	} else {
1127
+		//mysql
1056 1128
 		$return = version_compare($version, '8.0.1', '>=');
1129
+	}
1057 1130
 
1058 1131
 	return $return;
1059 1132
 }
Please login to merge, or discard this patch.