Completed
Push — release-2.1 ( 8e08df...630b0e )
by John
11s
created
Sources/Subs-Db-mysqli.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -815,7 +815,7 @@  discard block
 block discarded – undo
815 815
 		$connection
816 816
 	);
817 817
 	
818
-	if(!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
818
+	if (!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
819 819
 	{
820 820
 		if ($returnmode == 1)
821 821
 			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
@@ -824,7 +824,7 @@  discard block
 block discarded – undo
824 824
 			$return_var = array();
825 825
 			$count = count($insertRows);
826 826
 			$start = smf_db_insert_id($table, $keys[0]);
827
-			for ($i = 0; $i < $count; $i++ )
827
+			for ($i = 0; $i < $count; $i++)
828 828
 				$return_var[] = $start + $i;
829 829
 		}
830 830
 		return $return_var;
Please login to merge, or discard this patch.
Braces   +252 added lines, -185 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 3
14 14
  */
15 15
 
16
-if (!defined('SMF'))
16
+if (!defined('SMF')) {
17 17
 	die('No direct access...');
18
+}
18 19
 
19 20
 /**
20 21
  *  Maps the implementations in this file (smf_db_function_name)
@@ -33,8 +34,8 @@  discard block
 block discarded – undo
33 34
 	global $smcFunc, $mysql_set_mode;
34 35
 
35 36
 	// Map some database specific functions, only do this once.
36
-	if (!isset($smcFunc['db_fetch_assoc']) || $smcFunc['db_fetch_assoc'] != 'mysqli_fetch_assoc')
37
-		$smcFunc += array(
37
+	if (!isset($smcFunc['db_fetch_assoc']) || $smcFunc['db_fetch_assoc'] != 'mysqli_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',
@@ -58,9 +59,11 @@  discard block
 block discarded – undo
58 59
 			'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string',
59 60
 			'db_is_resource'            => 'smf_is_resource',
60 61
 		);
62
+	}
61 63
 
62
-	if (!empty($db_options['persist']))
63
-		$db_server = 'p:' . $db_server;
64
+	if (!empty($db_options['persist'])) {
65
+			$db_server = 'p:' . $db_server;
66
+	}
64 67
 
65 68
 	$connection = mysqli_init();
66 69
 	
@@ -69,31 +72,35 @@  discard block
 block discarded – undo
69 72
 	$success = false;
70 73
 	
71 74
 	if ($connection) {
72
-		if (!empty($db_options['port']))
73
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, '', $db_options['port'], null, $flags);
74
-		else
75
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, '', 0, null, $flags);
75
+		if (!empty($db_options['port'])) {
76
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, '', $db_options['port'], null, $flags);
77
+		} else {
78
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, '', 0, null, $flags);
79
+		}
76 80
 	}
77 81
 
78 82
 	// Something's wrong, show an error if its fatal (which we assume it is)
79 83
 	if ($success === false)
80 84
 	{
81
-		if (!empty($db_options['non_fatal']))
82
-			return null;
83
-		else
84
-			display_db_error();
85
+		if (!empty($db_options['non_fatal'])) {
86
+					return null;
87
+		} else {
88
+					display_db_error();
89
+		}
85 90
 	}
86 91
 
87 92
 	// Select the database, unless told not to
88
-	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal']))
89
-		display_db_error();
93
+	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal'])) {
94
+			display_db_error();
95
+	}
90 96
 
91 97
 	// This makes it possible to have SMF automatically change the sql_mode and autocommit if needed.
92
-	if (isset($mysql_set_mode) && $mysql_set_mode === true)
93
-		$smcFunc['db_query']('', 'SET sql_mode = \'\', AUTOCOMMIT = 1',
98
+	if (isset($mysql_set_mode) && $mysql_set_mode === true) {
99
+			$smcFunc['db_query']('', 'SET sql_mode = \'\', AUTOCOMMIT = 1',
94 100
 		array(),
95 101
 		false
96 102
 	);
103
+	}
97 104
 
98 105
 	return $connection;
99 106
 }
@@ -164,37 +171,46 @@  discard block
 block discarded – undo
164 171
 	global $db_callback, $user_info, $db_prefix, $smcFunc;
165 172
 
166 173
 	list ($values, $connection) = $db_callback;
167
-	if (!is_object($connection))
168
-		display_db_error();
174
+	if (!is_object($connection)) {
175
+			display_db_error();
176
+	}
169 177
 
170
-	if ($matches[1] === 'db_prefix')
171
-		return $db_prefix;
178
+	if ($matches[1] === 'db_prefix') {
179
+			return $db_prefix;
180
+	}
172 181
 
173
-	if ($matches[1] === 'query_see_board')
174
-		return $user_info['query_see_board'];
182
+	if ($matches[1] === 'query_see_board') {
183
+			return $user_info['query_see_board'];
184
+	}
175 185
 
176
-	if ($matches[1] === 'query_wanna_see_board')
177
-		return $user_info['query_wanna_see_board'];
186
+	if ($matches[1] === 'query_wanna_see_board') {
187
+			return $user_info['query_wanna_see_board'];
188
+	}
178 189
 
179
-	if ($matches[1] === 'empty')
180
-		return '\'\'';
190
+	if ($matches[1] === 'empty') {
191
+			return '\'\'';
192
+	}
181 193
 
182
-	if (!isset($matches[2]))
183
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
194
+	if (!isset($matches[2])) {
195
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
196
+	}
184 197
 
185
-	if ($matches[1] === 'literal')
186
-		return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
198
+	if ($matches[1] === 'literal') {
199
+			return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
200
+	}
187 201
 
188
-	if (!isset($values[$matches[2]]))
189
-		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__);
202
+	if (!isset($values[$matches[2]])) {
203
+			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__);
204
+	}
190 205
 
191 206
 	$replacement = $values[$matches[2]];
192 207
 
193 208
 	switch ($matches[1])
194 209
 	{
195 210
 		case 'int':
196
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
197
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
211
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
212
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
213
+			}
198 214
 			return (string) (int) $replacement;
199 215
 		break;
200 216
 
@@ -206,56 +222,63 @@  discard block
 block discarded – undo
206 222
 		case 'array_int':
207 223
 			if (is_array($replacement))
208 224
 			{
209
-				if (empty($replacement))
210
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
225
+				if (empty($replacement)) {
226
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
227
+				}
211 228
 
212 229
 				foreach ($replacement as $key => $value)
213 230
 				{
214
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
215
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
231
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
232
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
233
+					}
216 234
 
217 235
 					$replacement[$key] = (string) (int) $value;
218 236
 				}
219 237
 
220 238
 				return implode(', ', $replacement);
239
+			} else {
240
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
221 241
 			}
222
-			else
223
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
224 242
 
225 243
 		break;
226 244
 
227 245
 		case 'array_string':
228 246
 			if (is_array($replacement))
229 247
 			{
230
-				if (empty($replacement))
231
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
248
+				if (empty($replacement)) {
249
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
250
+				}
232 251
 
233
-				foreach ($replacement as $key => $value)
234
-					$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
252
+				foreach ($replacement as $key => $value) {
253
+									$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
254
+				}
235 255
 
236 256
 				return implode(', ', $replacement);
257
+			} else {
258
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
237 259
 			}
238
-			else
239
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
240 260
 		break;
241 261
 
242 262
 		case 'date':
243
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
244
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
245
-			else
246
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
263
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
264
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
265
+			} else {
266
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
267
+			}
247 268
 		break;
248 269
 
249 270
 		case 'time':
250
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
251
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
252
-			else
253
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
271
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
272
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
273
+			} else {
274
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
275
+			}
254 276
 		break;
255 277
 
256 278
 		case 'float':
257
-			if (!is_numeric($replacement))
258
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
279
+			if (!is_numeric($replacement)) {
280
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
281
+			}
259 282
 			return (string) (float) $replacement;
260 283
 		break;
261 284
 
@@ -269,32 +292,37 @@  discard block
 block discarded – undo
269 292
 		break;
270 293
 
271 294
 		case 'inet':
272
-			if ($replacement == 'null' || $replacement == '')
273
-				return 'null';
274
-			if (!isValidIP($replacement))
275
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
295
+			if ($replacement == 'null' || $replacement == '') {
296
+							return 'null';
297
+			}
298
+			if (!isValidIP($replacement)) {
299
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
300
+			}
276 301
 			//we don't use the native support of mysql > 5.6.2
277 302
 			return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement)));
278 303
 
279 304
 		case 'array_inet':
280 305
 			if (is_array($replacement))
281 306
 			{
282
-				if (empty($replacement))
283
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
307
+				if (empty($replacement)) {
308
+									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
309
+				}
284 310
 
285 311
 				foreach ($replacement as $key => $value)
286 312
 				{
287
-					if ($replacement == 'null' || $replacement == '')
288
-						$replacement[$key] = 'null';
289
-					if (!isValidIP($value))
290
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
313
+					if ($replacement == 'null' || $replacement == '') {
314
+											$replacement[$key] = 'null';
315
+					}
316
+					if (!isValidIP($value)) {
317
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
318
+					}
291 319
 					$replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value)));
292 320
 				}
293 321
 
294 322
 				return implode(', ', $replacement);
323
+			} else {
324
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
295 325
 			}
296
-			else
297
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
298 326
 		break;
299 327
 
300 328
 		default:
@@ -370,22 +398,25 @@  discard block
 block discarded – undo
370 398
 		// Are we in SSI mode?  If so try that username and password first
371 399
 		if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
372 400
 		{
373
-			if (empty($db_persist))
374
-				$db_connection = @mysqli_connect($db_server, $ssi_db_user, $ssi_db_passwd);
375
-			else
376
-				$db_connection = @mysqli_connect('p:' . $db_server, $ssi_db_user, $ssi_db_passwd);
401
+			if (empty($db_persist)) {
402
+							$db_connection = @mysqli_connect($db_server, $ssi_db_user, $ssi_db_passwd);
403
+			} else {
404
+							$db_connection = @mysqli_connect('p:' . $db_server, $ssi_db_user, $ssi_db_passwd);
405
+			}
377 406
 		}
378 407
 		// Fall back to the regular username and password if need be
379 408
 		if (!$db_connection)
380 409
 		{
381
-			if (empty($db_persist))
382
-				$db_connection = @mysqli_connect($db_server, $db_user, $db_passwd);
383
-			else
384
-				$db_connection = @mysqli_connect('p:' . $db_server, $db_user, $db_passwd);
410
+			if (empty($db_persist)) {
411
+							$db_connection = @mysqli_connect($db_server, $db_user, $db_passwd);
412
+			} else {
413
+							$db_connection = @mysqli_connect('p:' . $db_server, $db_user, $db_passwd);
414
+			}
385 415
 		}
386 416
 
387
-		if (!$db_connection || !@mysqli_select_db($db_connection, $db_name))
388
-			$db_connection = false;
417
+		if (!$db_connection || !@mysqli_select_db($db_connection, $db_name)) {
418
+					$db_connection = false;
419
+		}
389 420
 
390 421
 		$connection = $db_connection;
391 422
 	}
@@ -393,18 +424,20 @@  discard block
 block discarded – undo
393 424
 	// One more query....
394 425
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
395 426
 
396
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
397
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
427
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
428
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
429
+	}
398 430
 
399 431
 	// Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By
400 432
 	if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && preg_match('~^\s+SELECT~i', $db_string))
401 433
 	{
402 434
 		// Add before LIMIT
403
-		if ($pos = strpos($db_string, 'LIMIT '))
404
-			$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
405
-		else
406
-			// Append it.
435
+		if ($pos = strpos($db_string, 'LIMIT ')) {
436
+					$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
437
+		} else {
438
+					// Append it.
407 439
 			$db_string .= "\n\t\t\tORDER BY null";
440
+		}
408 441
 	}
409 442
 
410 443
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
@@ -426,8 +459,9 @@  discard block
 block discarded – undo
426 459
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
427 460
 
428 461
 		// Initialize $db_cache if not already initialized.
429
-		if (!isset($db_cache))
430
-			$db_cache = array();
462
+		if (!isset($db_cache)) {
463
+					$db_cache = array();
464
+		}
431 465
 
432 466
 		if (!empty($_SESSION['debug_redirect']))
433 467
 		{
@@ -453,17 +487,18 @@  discard block
 block discarded – undo
453 487
 		while (true)
454 488
 		{
455 489
 			$pos = strpos($db_string, '\'', $pos + 1);
456
-			if ($pos === false)
457
-				break;
490
+			if ($pos === false) {
491
+							break;
492
+			}
458 493
 			$clean .= substr($db_string, $old_pos, $pos - $old_pos);
459 494
 
460 495
 			while (true)
461 496
 			{
462 497
 				$pos1 = strpos($db_string, '\'', $pos + 1);
463 498
 				$pos2 = strpos($db_string, '\\', $pos + 1);
464
-				if ($pos1 === false)
465
-					break;
466
-				elseif ($pos2 === false || $pos2 > $pos1)
499
+				if ($pos1 === false) {
500
+									break;
501
+				} elseif ($pos2 === false || $pos2 > $pos1)
467 502
 				{
468 503
 					$pos = $pos1;
469 504
 					break;
@@ -479,29 +514,35 @@  discard block
 block discarded – undo
479 514
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
480 515
 
481 516
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
482
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
483
-			$fail = true;
517
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
518
+					$fail = true;
519
+		}
484 520
 		// Trying to change passwords, slow us down, or something?
485
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
486
-			$fail = true;
487
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
488
-			$fail = true;
521
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
522
+					$fail = true;
523
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
524
+					$fail = true;
525
+		}
489 526
 
490
-		if (!empty($fail) && function_exists('log_error'))
491
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
527
+		if (!empty($fail) && function_exists('log_error')) {
528
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
529
+		}
492 530
 	}
493 531
 
494
-	if (empty($db_unbuffered))
495
-		$ret = @mysqli_query($connection, $db_string);
496
-	else
497
-		$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
532
+	if (empty($db_unbuffered)) {
533
+			$ret = @mysqli_query($connection, $db_string);
534
+	} else {
535
+			$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
536
+	}
498 537
 
499
-	if ($ret === false && empty($db_values['db_error_skip']))
500
-		$ret = smf_db_error($db_string, $connection);
538
+	if ($ret === false && empty($db_values['db_error_skip'])) {
539
+			$ret = smf_db_error($db_string, $connection);
540
+	}
501 541
 
502 542
 	// Debugging.
503
-	if (isset($db_show_debug) && $db_show_debug === true)
504
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
543
+	if (isset($db_show_debug) && $db_show_debug === true) {
544
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
545
+	}
505 546
 
506 547
 	return $ret;
507 548
 }
@@ -548,12 +589,13 @@  discard block
 block discarded – undo
548 589
 	// Decide which connection to use
549 590
 	$connection = $connection === null ? $db_connection : $connection;
550 591
 
551
-	if ($type == 'begin')
552
-		return @mysqli_query($connection, 'BEGIN');
553
-	elseif ($type == 'rollback')
554
-		return @mysqli_query($connection, 'ROLLBACK');
555
-	elseif ($type == 'commit')
556
-		return @mysqli_query($connection, 'COMMIT');
592
+	if ($type == 'begin') {
593
+			return @mysqli_query($connection, 'BEGIN');
594
+	} elseif ($type == 'rollback') {
595
+			return @mysqli_query($connection, 'ROLLBACK');
596
+	} elseif ($type == 'commit') {
597
+			return @mysqli_query($connection, 'COMMIT');
598
+	}
557 599
 
558 600
 	return false;
559 601
 }
@@ -593,8 +635,9 @@  discard block
 block discarded – undo
593 635
 	//    2013: Lost connection to server during query.
594 636
 
595 637
 	// Log the error.
596
-	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error'))
597
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
638
+	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) {
639
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
640
+	}
598 641
 
599 642
 	// Database error auto fixing ;).
600 643
 	if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1'))
@@ -603,8 +646,9 @@  discard block
 block discarded – undo
603 646
 		$old_cache = @$modSettings['cache_enable'];
604 647
 		$modSettings['cache_enable'] = '1';
605 648
 
606
-		if (($temp = cache_get_data('db_last_error', 600)) !== null)
607
-			$db_last_error = max(@$db_last_error, $temp);
649
+		if (($temp = cache_get_data('db_last_error', 600)) !== null) {
650
+					$db_last_error = max(@$db_last_error, $temp);
651
+		}
608 652
 
609 653
 		if (@$db_last_error < time() - 3600 * 24 * 3)
610 654
 		{
@@ -620,8 +664,9 @@  discard block
 block discarded – undo
620 664
 					foreach ($tables as $table)
621 665
 					{
622 666
 						// Now, it's still theoretically possible this could be an injection.  So backtick it!
623
-						if (trim($table) != '')
624
-							$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
667
+						if (trim($table) != '') {
668
+													$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
669
+						}
625 670
 					}
626 671
 				}
627 672
 
@@ -630,8 +675,9 @@  discard block
 block discarded – undo
630 675
 			// Table crashed.  Let's try to fix it.
631 676
 			elseif ($query_errno == 1016)
632 677
 			{
633
-				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0)
634
-					$fix_tables = array('`' . $match[1] . '`');
678
+				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) {
679
+									$fix_tables = array('`' . $match[1] . '`');
680
+				}
635 681
 			}
636 682
 			// Indexes crashed.  Should be easy to fix!
637 683
 			elseif ($query_errno == 1034 || $query_errno == 1035)
@@ -650,13 +696,15 @@  discard block
 block discarded – undo
650 696
 
651 697
 			// Make a note of the REPAIR...
652 698
 			cache_put_data('db_last_error', time(), 600);
653
-			if (($temp = cache_get_data('db_last_error', 600)) === null)
654
-				updateSettingsFile(array('db_last_error' => time()));
699
+			if (($temp = cache_get_data('db_last_error', 600)) === null) {
700
+							updateSettingsFile(array('db_last_error' => time()));
701
+			}
655 702
 
656 703
 			// Attempt to find and repair the broken table.
657
-			foreach ($fix_tables as $table)
658
-				$smcFunc['db_query']('', "
704
+			foreach ($fix_tables as $table) {
705
+							$smcFunc['db_query']('', "
659 706
 					REPAIR TABLE $table", false, false);
707
+			}
660 708
 
661 709
 			// And send off an email!
662 710
 			sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair'], null, 'dberror');
@@ -665,11 +713,12 @@  discard block
 block discarded – undo
665 713
 
666 714
 			// Try the query again...?
667 715
 			$ret = $smcFunc['db_query']('', $db_string, false, false);
668
-			if ($ret !== false)
669
-				return $ret;
716
+			if ($ret !== false) {
717
+							return $ret;
718
+			}
719
+		} else {
720
+					$modSettings['cache_enable'] = $old_cache;
670 721
 		}
671
-		else
672
-			$modSettings['cache_enable'] = $old_cache;
673 722
 
674 723
 		// Check for the "lost connection" or "deadlock found" errors - and try it just one more time.
675 724
 		if (in_array($query_errno, array(1205, 1213, 2006, 2013)))
@@ -679,22 +728,25 @@  discard block
 block discarded – undo
679 728
 				// Are we in SSI mode?  If so try that username and password first
680 729
 				if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
681 730
 				{
682
-					if (empty($db_persist))
683
-						$db_connection = @mysqli_connect($db_server, $ssi_db_user, $ssi_db_passwd);
684
-					else
685
-						$db_connection = @mysqli_connect('p:' . $db_server, $ssi_db_user, $ssi_db_passwd);
731
+					if (empty($db_persist)) {
732
+											$db_connection = @mysqli_connect($db_server, $ssi_db_user, $ssi_db_passwd);
733
+					} else {
734
+											$db_connection = @mysqli_connect('p:' . $db_server, $ssi_db_user, $ssi_db_passwd);
735
+					}
686 736
 				}
687 737
 				// Fall back to the regular username and password if need be
688 738
 				if (!$db_connection)
689 739
 				{
690
-					if (empty($db_persist))
691
-						$db_connection = @mysqli_connect($db_server, $db_user, $db_passwd);
692
-					else
693
-						$db_connection = @mysqli_connect('p:' . $db_server, $db_user, $db_passwd);
740
+					if (empty($db_persist)) {
741
+											$db_connection = @mysqli_connect($db_server, $db_user, $db_passwd);
742
+					} else {
743
+											$db_connection = @mysqli_connect('p:' . $db_server, $db_user, $db_passwd);
744
+					}
694 745
 				}
695 746
 
696
-				if (!$db_connection || !@mysqli_select_db($db_connection, $db_name))
697
-					$db_connection = false;
747
+				if (!$db_connection || !@mysqli_select_db($db_connection, $db_name)) {
748
+									$db_connection = false;
749
+				}
698 750
 			}
699 751
 
700 752
 			if ($db_connection)
@@ -705,24 +757,27 @@  discard block
 block discarded – undo
705 757
 					$ret = $smcFunc['db_query']('', $db_string, false, false);
706 758
 
707 759
 					$new_errno = mysqli_errno($db_connection);
708
-					if ($ret !== false || in_array($new_errno, array(1205, 1213)))
709
-						break;
760
+					if ($ret !== false || in_array($new_errno, array(1205, 1213))) {
761
+											break;
762
+					}
710 763
 				}
711 764
 
712 765
 				// If it failed again, shucks to be you... we're not trying it over and over.
713
-				if ($ret !== false)
714
-					return $ret;
766
+				if ($ret !== false) {
767
+									return $ret;
768
+				}
715 769
 			}
716 770
 		}
717 771
 		// Are they out of space, perhaps?
718 772
 		elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false))
719 773
 		{
720
-			if (!isset($txt))
721
-				$query_error .= ' - check database storage space.';
722
-			else
774
+			if (!isset($txt)) {
775
+							$query_error .= ' - check database storage space.';
776
+			} else
723 777
 			{
724
-				if (!isset($txt['mysql_error_space']))
725
-					loadLanguage('Errors');
778
+				if (!isset($txt['mysql_error_space'])) {
779
+									loadLanguage('Errors');
780
+				}
726 781
 
727 782
 				$query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space'];
728 783
 			}
@@ -730,15 +785,17 @@  discard block
 block discarded – undo
730 785
 	}
731 786
 
732 787
 	// Nothing's defined yet... just die with it.
733
-	if (empty($context) || empty($txt))
734
-		die($query_error);
788
+	if (empty($context) || empty($txt)) {
789
+			die($query_error);
790
+	}
735 791
 
736 792
 	// Show an error message, if possible.
737 793
 	$context['error_title'] = $txt['database_error'];
738
-	if (allowedTo('admin_forum'))
739
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
740
-	else
741
-		$context['error_message'] = $txt['try_again'];
794
+	if (allowedTo('admin_forum')) {
795
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
796
+	} else {
797
+			$context['error_message'] = $txt['try_again'];
798
+	}
742 799
 
743 800
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
744 801
 	{
@@ -769,25 +826,28 @@  discard block
 block discarded – undo
769 826
 	$connection = $connection === null ? $db_connection : $connection;
770 827
 
771 828
 	// With nothing to insert, simply return.
772
-	if (empty($data))
773
-		return;
829
+	if (empty($data)) {
830
+			return;
831
+	}
774 832
 
775 833
 	// Replace the prefix holder with the actual prefix.
776 834
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
777 835
 
778 836
 	// Inserting data as a single row can be done as a single array.
779
-	if (!is_array($data[array_rand($data)]))
780
-		$data = array($data);
837
+	if (!is_array($data[array_rand($data)])) {
838
+			$data = array($data);
839
+	}
781 840
 
782 841
 	// Create the mold for a single row insert.
783 842
 	$insertData = '(';
784 843
 	foreach ($columns as $columnName => $type)
785 844
 	{
786 845
 		// Are we restricting the length?
787
-		if (strpos($type, 'string-') !== false)
788
-			$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
789
-		else
790
-			$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
846
+		if (strpos($type, 'string-') !== false) {
847
+					$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
848
+		} else {
849
+					$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
850
+		}
791 851
 	}
792 852
 	$insertData = substr($insertData, 0, -2) . ')';
793 853
 
@@ -796,8 +856,9 @@  discard block
 block discarded – undo
796 856
 
797 857
 	// Here's where the variables are injected to the query.
798 858
 	$insertRows = array();
799
-	foreach ($data as $dataRow)
800
-		$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
859
+	foreach ($data as $dataRow) {
860
+			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
861
+	}
801 862
 
802 863
 	// Determine the method of insertion.
803 864
 	$queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT');
@@ -817,15 +878,16 @@  discard block
 block discarded – undo
817 878
 	
818 879
 	if(!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
819 880
 	{
820
-		if ($returnmode == 1)
821
-			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
822
-		else if ($returnmode == 2)
881
+		if ($returnmode == 1) {
882
+					$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
883
+		} else if ($returnmode == 2)
823 884
 		{
824 885
 			$return_var = array();
825 886
 			$count = count($insertRows);
826 887
 			$start = smf_db_insert_id($table, $keys[0]);
827
-			for ($i = 0; $i < $count; $i++ )
828
-				$return_var[] = $start + $i;
888
+			for ($i = 0; $i < $count; $i++ ) {
889
+							$return_var[] = $start + $i;
890
+			}
829 891
 		}
830 892
 		return $return_var;
831 893
 	}
@@ -843,8 +905,9 @@  discard block
 block discarded – undo
843 905
  */
844 906
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
845 907
 {
846
-	if (empty($log_message))
847
-		$log_message = $error_message;
908
+	if (empty($log_message)) {
909
+			$log_message = $error_message;
910
+	}
848 911
 
849 912
 	foreach (debug_backtrace() as $step)
850 913
 	{
@@ -863,12 +926,14 @@  discard block
 block discarded – undo
863 926
 	}
864 927
 
865 928
 	// A special case - we want the file and line numbers for debugging.
866
-	if ($error_type == 'return')
867
-		return array($file, $line);
929
+	if ($error_type == 'return') {
930
+			return array($file, $line);
931
+	}
868 932
 
869 933
 	// Is always a critical error.
870
-	if (function_exists('log_error'))
871
-		log_error($log_message, 'critical', $file, $line);
934
+	if (function_exists('log_error')) {
935
+			log_error($log_message, 'critical', $file, $line);
936
+	}
872 937
 
873 938
 	if (function_exists('fatal_error'))
874 939
 	{
@@ -876,12 +941,12 @@  discard block
 block discarded – undo
876 941
 
877 942
 		// Cannot continue...
878 943
 		exit;
944
+	} elseif ($error_type) {
945
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
946
+	} else {
947
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
948
+	}
879 949
 	}
880
-	elseif ($error_type)
881
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
882
-	else
883
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
884
-}
885 950
 
886 951
 /**
887 952
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -898,10 +963,11 @@  discard block
 block discarded – undo
898 963
 		'\\' => '\\\\',
899 964
 	);
900 965
 
901
-	if ($translate_human_wildcards)
902
-		$replacements += array(
966
+	if ($translate_human_wildcards) {
967
+			$replacements += array(
903 968
 			'*' => '%',
904 969
 		);
970
+	}
905 971
 
906 972
 	return strtr($string, $replacements);
907 973
 }
@@ -915,8 +981,9 @@  discard block
 block discarded – undo
915 981
  */
916 982
 function smf_is_resource($result)
917 983
 {
918
-	if ($result instanceof mysqli_result)
919
-		return true;
984
+	if ($result instanceof mysqli_result) {
985
+			return true;
986
+	}
920 987
 
921 988
 	return false;
922 989
 }
Please login to merge, or discard this patch.
Sources/Subs-Db-postgresql.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -763,10 +763,10 @@  discard block
 block discarded – undo
763 763
 	$returning = '';
764 764
 	$with_returning = false;
765 765
 	// lets build the returning string, mysql allow only in normal mode
766
-	if(!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
766
+	if (!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
767 767
 	{
768 768
 		// we only take the first key
769
-		$returning = ' RETURNING '.$keys[0];
769
+		$returning = ' RETURNING ' . $keys[0];
770 770
 		$with_returning = true;
771 771
 	}
772 772
 
@@ -797,7 +797,7 @@  discard block
 block discarded – undo
797 797
 			INSERT INTO ' . $table . '("' . implode('", "', $indexed_columns) . '")
798 798
 			VALUES
799 799
 				' . implode(',
800
-				', $insertRows).$replace.$returning,
800
+				', $insertRows) . $replace . $returning,
801 801
 			array(
802 802
 				'security_override' => true,
803 803
 				'db_error_skip' => $method == 'ignore' || $table === $db_prefix . 'log_errors',
@@ -810,7 +810,7 @@  discard block
 block discarded – undo
810 810
 			if ($returnmode === 2)
811 811
 				$return_var = array();
812 812
 
813
-			while($row = $smcFunc['db_fetch_assoc']($request) && $with_returning)
813
+			while ($row = $smcFunc['db_fetch_assoc']($request) && $with_returning)
814 814
 			{
815 815
 				if (is_int($row[$keys[0]])) // try to emulate mysql limitation
816 816
 				{
Please login to merge, or discard this patch.
Braces   +217 added lines, -161 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 3
14 14
  */
15 15
 
16
-if (!defined('SMF'))
16
+if (!defined('SMF')) {
17 17
 	die('No direct access...');
18
+}
18 19
 
19 20
 /**
20 21
  * 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']) || $smcFunc['db_fetch_assoc'] != 'postg_fetch_assoc')
38
-		$smcFunc += array(
38
+	if (!isset($smcFunc['db_fetch_assoc']) || $smcFunc['db_fetch_assoc'] != 'postg_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',
@@ -59,11 +60,13 @@  discard block
 block discarded – undo
59 60
 			'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string',
60 61
 			'db_is_resource' => 'is_resource',
61 62
 		);
63
+	}
62 64
 
63
-	if (!empty($db_options['persist']))
64
-		$connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
65
-	else
66
-		$connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
65
+	if (!empty($db_options['persist'])) {
66
+			$connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
67
+	} else {
68
+			$connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\''));
69
+	}
67 70
 
68 71
 	// Something's wrong, show an error if its fatal (which we assume it is)
69 72
 	if (!$connection)
@@ -71,8 +74,7 @@  discard block
 block discarded – undo
71 74
 		if (!empty($db_options['non_fatal']))
72 75
 		{
73 76
 			return null;
74
-		}
75
-		else
77
+		} else
76 78
 		{
77 79
 			display_db_error();
78 80
 		}
@@ -123,34 +125,42 @@  discard block
 block discarded – undo
123 125
 
124 126
 	list ($values, $connection) = $db_callback;
125 127
 
126
-	if ($matches[1] === 'db_prefix')
127
-		return $db_prefix;
128
+	if ($matches[1] === 'db_prefix') {
129
+			return $db_prefix;
130
+	}
128 131
 
129
-	if ($matches[1] === 'query_see_board')
130
-		return $user_info['query_see_board'];
132
+	if ($matches[1] === 'query_see_board') {
133
+			return $user_info['query_see_board'];
134
+	}
131 135
 
132
-	if ($matches[1] === 'query_wanna_see_board')
133
-		return $user_info['query_wanna_see_board'];
136
+	if ($matches[1] === 'query_wanna_see_board') {
137
+			return $user_info['query_wanna_see_board'];
138
+	}
134 139
 
135
-	if ($matches[1] === 'empty')
136
-		return '\'\'';
140
+	if ($matches[1] === 'empty') {
141
+			return '\'\'';
142
+	}
137 143
 
138
-	if (!isset($matches[2]))
139
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
144
+	if (!isset($matches[2])) {
145
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
146
+	}
140 147
 
141
-	if ($matches[1] === 'literal')
142
-		return '\'' . pg_escape_string($matches[2]) . '\'';
148
+	if ($matches[1] === 'literal') {
149
+			return '\'' . pg_escape_string($matches[2]) . '\'';
150
+	}
143 151
 
144
-	if (!isset($values[$matches[2]]))
145
-		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__);
152
+	if (!isset($values[$matches[2]])) {
153
+			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__);
154
+	}
146 155
 
147 156
 	$replacement = $values[$matches[2]];
148 157
 
149 158
 	switch ($matches[1])
150 159
 	{
151 160
 		case 'int':
152
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
153
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
161
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
162
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
163
+			}
154 164
 			return (string) (int) $replacement;
155 165
 		break;
156 166
 
@@ -162,56 +172,63 @@  discard block
 block discarded – undo
162 172
 		case 'array_int':
163 173
 			if (is_array($replacement))
164 174
 			{
165
-				if (empty($replacement))
166
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
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__);
177
+				}
167 178
 
168 179
 				foreach ($replacement as $key => $value)
169 180
 				{
170
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
171
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
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__);
183
+					}
172 184
 
173 185
 					$replacement[$key] = (string) (int) $value;
174 186
 				}
175 187
 
176 188
 				return implode(', ', $replacement);
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__);
177 191
 			}
178
-			else
179
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
180 192
 
181 193
 		break;
182 194
 
183 195
 		case 'array_string':
184 196
 			if (is_array($replacement))
185 197
 			{
186
-				if (empty($replacement))
187
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
198
+				if (empty($replacement)) {
199
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
200
+				}
188 201
 
189
-				foreach ($replacement as $key => $value)
190
-					$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value));
202
+				foreach ($replacement as $key => $value) {
203
+									$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value));
204
+				}
191 205
 
192 206
 				return implode(', ', $replacement);
207
+			} else {
208
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
193 209
 			}
194
-			else
195
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
196 210
 		break;
197 211
 
198 212
 		case 'date':
199
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
200
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
201
-			else
202
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
213
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
214
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
215
+			} else {
216
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
217
+			}
203 218
 		break;
204 219
 
205 220
 		case 'time':
206
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
207
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
208
-			else
209
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
221
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
222
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
223
+			} else {
224
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
225
+			}
210 226
 		break;
211 227
 
212 228
 		case 'float':
213
-			if (!is_numeric($replacement))
214
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
229
+			if (!is_numeric($replacement)) {
230
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
231
+			}
215 232
 			return (string) (float) $replacement;
216 233
 		break;
217 234
 
@@ -224,31 +241,36 @@  discard block
 block discarded – undo
224 241
 		break;
225 242
 
226 243
 		case 'inet':
227
-			if ($replacement == 'null' || $replacement == '')
228
-				return 'null';
229
-			if (inet_pton($replacement) === false)
230
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
244
+			if ($replacement == 'null' || $replacement == '') {
245
+							return 'null';
246
+			}
247
+			if (inet_pton($replacement) === false) {
248
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
249
+			}
231 250
 			return sprintf('\'%1$s\'::inet', pg_escape_string($replacement));
232 251
 
233 252
 		case 'array_inet':
234 253
 			if (is_array($replacement))
235 254
 			{
236
-				if (empty($replacement))
237
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
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__);
257
+				}
238 258
 
239 259
 				foreach ($replacement as $key => $value)
240 260
 				{
241
-					if ($replacement == 'null' || $replacement == '')
242
-						$replacement[$key] = 'null';
243
-					if (!isValidIP($value))
244
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
261
+					if ($replacement == 'null' || $replacement == '') {
262
+											$replacement[$key] = 'null';
263
+					}
264
+					if (!isValidIP($value)) {
265
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
266
+					}
245 267
 					$replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value));
246 268
 				}
247 269
 
248 270
 				return implode(', ', $replacement);
271
+			} else {
272
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
249 273
 			}
250
-			else
251
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
252 274
 		break;
253 275
 
254 276
 		default:
@@ -335,14 +357,16 @@  discard block
 block discarded – undo
335 357
 		),
336 358
 	);
337 359
 
338
-	if (isset($replacements[$identifier]))
339
-		$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
360
+	if (isset($replacements[$identifier])) {
361
+			$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
362
+	}
340 363
 
341 364
 	// Limits need to be a little different.
342 365
 	$db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string);
343 366
 
344
-	if (trim($db_string) == '')
345
-		return false;
367
+	if (trim($db_string) == '') {
368
+			return false;
369
+	}
346 370
 
347 371
 	// Comments that are allowed in a query are preg_removed.
348 372
 	static $allowed_comments_from = array(
@@ -362,8 +386,9 @@  discard block
 block discarded – undo
362 386
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
363 387
 	$db_replace_result = 0;
364 388
 
365
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
366
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
389
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
390
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
391
+	}
367 392
 
368 393
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
369 394
 	{
@@ -384,8 +409,9 @@  discard block
 block discarded – undo
384 409
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
385 410
 
386 411
 		// Initialize $db_cache if not already initialized.
387
-		if (!isset($db_cache))
388
-			$db_cache = array();
412
+		if (!isset($db_cache)) {
413
+					$db_cache = array();
414
+		}
389 415
 
390 416
 		if (!empty($_SESSION['debug_redirect']))
391 417
 		{
@@ -411,17 +437,18 @@  discard block
 block discarded – undo
411 437
 		while (true)
412 438
 		{
413 439
 			$pos = strpos($db_string, '\'', $pos + 1);
414
-			if ($pos === false)
415
-				break;
440
+			if ($pos === false) {
441
+							break;
442
+			}
416 443
 			$clean .= substr($db_string, $old_pos, $pos - $old_pos);
417 444
 
418 445
 			while (true)
419 446
 			{
420 447
 				$pos1 = strpos($db_string, '\'', $pos + 1);
421 448
 				$pos2 = strpos($db_string, '\\', $pos + 1);
422
-				if ($pos1 === false)
423
-					break;
424
-				elseif ($pos2 === false || $pos2 > $pos1)
449
+				if ($pos1 === false) {
450
+									break;
451
+				} elseif ($pos2 === false || $pos2 > $pos1)
425 452
 				{
426 453
 					$pos = $pos1;
427 454
 					break;
@@ -437,26 +464,31 @@  discard block
 block discarded – undo
437 464
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
438 465
 
439 466
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
440
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
441
-			$fail = true;
467
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
468
+					$fail = true;
469
+		}
442 470
 		// Trying to change passwords, slow us down, or something?
443
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
444
-			$fail = true;
445
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
446
-			$fail = true;
471
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
472
+					$fail = true;
473
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
474
+					$fail = true;
475
+		}
447 476
 
448
-		if (!empty($fail) && function_exists('log_error'))
449
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
477
+		if (!empty($fail) && function_exists('log_error')) {
478
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
479
+		}
450 480
 	}
451 481
 
452 482
 	$db_last_result = @pg_query($connection, $db_string);
453 483
 
454
-	if ($db_last_result === false && empty($db_values['db_error_skip']))
455
-		$db_last_result = smf_db_error($db_string, $connection);
484
+	if ($db_last_result === false && empty($db_values['db_error_skip'])) {
485
+			$db_last_result = smf_db_error($db_string, $connection);
486
+	}
456 487
 
457 488
 	// Debugging.
458
-	if (isset($db_show_debug) && $db_show_debug === true)
459
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
489
+	if (isset($db_show_debug) && $db_show_debug === true) {
490
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
491
+	}
460 492
 
461 493
 	return $db_last_result;
462 494
 }
@@ -469,10 +501,11 @@  discard block
 block discarded – undo
469 501
 {
470 502
 	global $db_last_result, $db_replace_result;
471 503
 
472
-	if ($db_replace_result)
473
-		return $db_replace_result;
474
-	elseif ($result === null && !$db_last_result)
475
-		return 0;
504
+	if ($db_replace_result) {
505
+			return $db_replace_result;
506
+	} elseif ($result === null && !$db_last_result) {
507
+			return 0;
508
+	}
476 509
 
477 510
 	return pg_affected_rows($result === null ? $db_last_result : $result);
478 511
 }
@@ -496,8 +529,9 @@  discard block
 block discarded – undo
496 529
 		array(
497 530
 		)
498 531
 	);
499
-	if (!$request)
500
-		return false;
532
+	if (!$request) {
533
+			return false;
534
+	}
501 535
 	list ($lastID) = $smcFunc['db_fetch_row']($request);
502 536
 	$smcFunc['db_free_result']($request);
503 537
 
@@ -518,12 +552,13 @@  discard block
 block discarded – undo
518 552
 	// Decide which connection to use
519 553
 	$connection = $connection === null ? $db_connection : $connection;
520 554
 
521
-	if ($type == 'begin')
522
-		return @pg_query($connection, 'BEGIN');
523
-	elseif ($type == 'rollback')
524
-		return @pg_query($connection, 'ROLLBACK');
525
-	elseif ($type == 'commit')
526
-		return @pg_query($connection, 'COMMIT');
555
+	if ($type == 'begin') {
556
+			return @pg_query($connection, 'BEGIN');
557
+	} elseif ($type == 'rollback') {
558
+			return @pg_query($connection, 'ROLLBACK');
559
+	} elseif ($type == 'commit') {
560
+			return @pg_query($connection, 'COMMIT');
561
+	}
527 562
 
528 563
 	return false;
529 564
 }
@@ -551,19 +586,22 @@  discard block
 block discarded – undo
551 586
 	$query_error = @pg_last_error($connection);
552 587
 
553 588
 	// Log the error.
554
-	if (function_exists('log_error'))
555
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line);
589
+	if (function_exists('log_error')) {
590
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line);
591
+	}
556 592
 
557 593
 	// Nothing's defined yet... just die with it.
558
-	if (empty($context) || empty($txt))
559
-		die($query_error);
594
+	if (empty($context) || empty($txt)) {
595
+			die($query_error);
596
+	}
560 597
 
561 598
 	// Show an error message, if possible.
562 599
 	$context['error_title'] = $txt['database_error'];
563
-	if (allowedTo('admin_forum'))
564
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
565
-	else
566
-		$context['error_message'] = $txt['try_again'];
600
+	if (allowedTo('admin_forum')) {
601
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
602
+	} else {
603
+			$context['error_message'] = $txt['try_again'];
604
+	}
567 605
 
568 606
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
569 607
 	{
@@ -585,12 +623,14 @@  discard block
 block discarded – undo
585 623
 {
586 624
 	global $db_row_count;
587 625
 
588
-	if ($counter !== false)
589
-		return pg_fetch_row($request, $counter);
626
+	if ($counter !== false) {
627
+			return pg_fetch_row($request, $counter);
628
+	}
590 629
 
591 630
 	// Reset the row counter...
592
-	if (!isset($db_row_count[(int) $request]))
593
-		$db_row_count[(int) $request] = 0;
631
+	if (!isset($db_row_count[(int) $request])) {
632
+			$db_row_count[(int) $request] = 0;
633
+	}
594 634
 
595 635
 	// Return the right row.
596 636
 	return @pg_fetch_row($request, $db_row_count[(int) $request]++);
@@ -607,12 +647,14 @@  discard block
 block discarded – undo
607 647
 {
608 648
 	global $db_row_count;
609 649
 
610
-	if ($counter !== false)
611
-		return pg_fetch_assoc($request, $counter);
650
+	if ($counter !== false) {
651
+			return pg_fetch_assoc($request, $counter);
652
+	}
612 653
 
613 654
 	// Reset the row counter...
614
-	if (!isset($db_row_count[(int) $request]))
615
-		$db_row_count[(int) $request] = 0;
655
+	if (!isset($db_row_count[(int) $request])) {
656
+			$db_row_count[(int) $request] = 0;
657
+	}
616 658
 
617 659
 	// Return the right row.
618 660
 	return @pg_fetch_assoc($request, $db_row_count[(int) $request]++);
@@ -666,11 +708,13 @@  discard block
 block discarded – undo
666 708
 
667 709
 	$replace = '';
668 710
 
669
-	if (empty($data))
670
-		return;
711
+	if (empty($data)) {
712
+			return;
713
+	}
671 714
 
672
-	if (!is_array($data[array_rand($data)]))
673
-		$data = array($data);
715
+	if (!is_array($data[array_rand($data)])) {
716
+			$data = array($data);
717
+	}
674 718
 
675 719
 	// Replace the prefix holder with the actual prefix.
676 720
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
@@ -696,11 +740,13 @@  discard block
 block discarded – undo
696 740
 			//pg 9.5 got replace support
697 741
 			$pg_version = $smcFunc['db_get_version']();
698 742
 			// if we got a Beta Version
699
-			if (stripos($pg_version, 'beta') !== false)
700
-				$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0';
743
+			if (stripos($pg_version, 'beta') !== false) {
744
+							$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0';
745
+			}
701 746
 			// or RC
702
-			if (stripos($pg_version, 'rc') !== false)
703
-				$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0';
747
+			if (stripos($pg_version, 'rc') !== false) {
748
+							$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0';
749
+			}
704 750
 
705 751
 			$replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false);
706 752
 		}
@@ -719,8 +765,7 @@  discard block
 block discarded – undo
719 765
 					$key_str .= ($count_pk > 0 ? ',' : '');
720 766
 					$key_str .= $columnName;
721 767
 					$count_pk++;
722
-				}
723
-				else //normal field
768
+				} else //normal field
724 769
 				{
725 770
 					$col_str .= ($count > 0 ? ',' : '');
726 771
 					$col_str .= $columnName . ' = EXCLUDED.' . $columnName;
@@ -728,20 +773,21 @@  discard block
 block discarded – undo
728 773
 				}
729 774
 			}
730 775
 			$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str;
731
-		}
732
-		else
776
+		} else
733 777
 		{
734 778
 			foreach ($columns as $columnName => $type)
735 779
 			{
736 780
 				// Are we restricting the length?
737
-				if (strpos($type, 'string-') !== false)
738
-					$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count);
739
-				else
740
-					$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count);
781
+				if (strpos($type, 'string-') !== false) {
782
+									$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count);
783
+				} else {
784
+									$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count);
785
+				}
741 786
 
742 787
 				// A key? That's what we were looking for.
743
-				if (in_array($columnName, $keys))
744
-					$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2);
788
+				if (in_array($columnName, $keys)) {
789
+									$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2);
790
+				}
745 791
 				$count++;
746 792
 			}
747 793
 
@@ -777,10 +823,11 @@  discard block
 block discarded – undo
777 823
 		foreach ($columns as $columnName => $type)
778 824
 		{
779 825
 			// Are we restricting the length?
780
-			if (strpos($type, 'string-') !== false)
781
-				$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
782
-			else
783
-				$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
826
+			if (strpos($type, 'string-') !== false) {
827
+							$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
828
+			} else {
829
+							$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
830
+			}
784 831
 		}
785 832
 		$insertData = substr($insertData, 0, -2) . ')';
786 833
 
@@ -789,8 +836,9 @@  discard block
 block discarded – undo
789 836
 
790 837
 		// Here's where the variables are injected to the query.
791 838
 		$insertRows = array();
792
-		foreach ($data as $dataRow)
793
-			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
839
+		foreach ($data as $dataRow) {
840
+					$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
841
+		}
794 842
 
795 843
 		// Do the insert.
796 844
 		$request = $smcFunc['db_query']('', '
@@ -807,19 +855,21 @@  discard block
 block discarded – undo
807 855
 
808 856
 		if ($with_returning && $request !== false)
809 857
 		{
810
-			if ($returnmode === 2)
811
-				$return_var = array();
858
+			if ($returnmode === 2) {
859
+							$return_var = array();
860
+			}
812 861
 
813 862
 			while($row = $smcFunc['db_fetch_assoc']($request) && $with_returning)
814 863
 			{
815
-				if (is_int($row[$keys[0]])) // try to emulate mysql limitation
864
+				if (is_int($row[$keys[0]])) {
865
+					// try to emulate mysql limitation
816 866
 				{
817 867
 					if ($returnmode === 1)
818 868
 						$return_var = $row[$keys[0]];
819
-					elseif ($returnmode === 2)
820
-						$return_var[] = $row[$keys[0]];
821
-				}
822
-				else
869
+				} elseif ($returnmode === 2) {
870
+											$return_var[] = $row[$keys[0]];
871
+					}
872
+				} else
823 873
 				{
824 874
 					$with_returning = false;
825 875
 					trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR);
@@ -828,12 +878,14 @@  discard block
 block discarded – undo
828 878
 		}
829 879
 	}
830 880
 
831
-	if ($priv_trans)
832
-		$smcFunc['db_transaction']('commit', $connection);
881
+	if ($priv_trans) {
882
+			$smcFunc['db_transaction']('commit', $connection);
883
+	}
833 884
 	
834
-	if ($with_returning && !empty($return_var))
835
-		return $return_var; 
836
-}
885
+	if ($with_returning && !empty($return_var)) {
886
+			return $return_var;
887
+	}
888
+	}
837 889
 
838 890
 /**
839 891
  * Dummy function really. Doesn't do anything on PostgreSQL.
@@ -870,8 +922,9 @@  discard block
 block discarded – undo
870 922
  */
871 923
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
872 924
 {
873
-	if (empty($log_message))
874
-		$log_message = $error_message;
925
+	if (empty($log_message)) {
926
+			$log_message = $error_message;
927
+	}
875 928
 
876 929
 	foreach (debug_backtrace() as $step)
877 930
 	{
@@ -890,12 +943,14 @@  discard block
 block discarded – undo
890 943
 	}
891 944
 
892 945
 	// A special case - we want the file and line numbers for debugging.
893
-	if ($error_type == 'return')
894
-		return array($file, $line);
946
+	if ($error_type == 'return') {
947
+			return array($file, $line);
948
+	}
895 949
 
896 950
 	// Is always a critical error.
897
-	if (function_exists('log_error'))
898
-		log_error($log_message, 'critical', $file, $line);
951
+	if (function_exists('log_error')) {
952
+			log_error($log_message, 'critical', $file, $line);
953
+	}
899 954
 
900 955
 	if (function_exists('fatal_error'))
901 956
 	{
@@ -903,12 +958,12 @@  discard block
 block discarded – undo
903 958
 
904 959
 		// Cannot continue...
905 960
 		exit;
961
+	} elseif ($error_type) {
962
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
963
+	} else {
964
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
965
+	}
906 966
 	}
907
-	elseif ($error_type)
908
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
909
-	else
910
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
911
-}
912 967
 
913 968
 /**
914 969
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -925,10 +980,11 @@  discard block
 block discarded – undo
925 980
 		'\\' => '\\\\',
926 981
 	);
927 982
 
928
-	if ($translate_human_wildcards)
929
-		$replacements += array(
983
+	if ($translate_human_wildcards) {
984
+			$replacements += array(
930 985
 			'*' => '%',
931 986
 		);
987
+	}
932 988
 
933 989
 	return strtr($string, $replacements);
934 990
 }
Please login to merge, or discard this patch.
Sources/Subs-Db-mysql.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -757,7 +757,7 @@  discard block
 block discarded – undo
757 757
 		$connection
758 758
 	);
759 759
 	
760
-	if(!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
760
+	if (!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
761 761
 	{
762 762
 		if ($returnmode == 1)
763 763
 			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
@@ -766,7 +766,7 @@  discard block
 block discarded – undo
766 766
 			$return_var = array();
767 767
 			$count = count($insertRows);
768 768
 			$start = smf_db_insert_id($table, $keys[0]);
769
-			for ($i = 0; $i < $count; $i++ )
769
+			for ($i = 0; $i < $count; $i++)
770 770
 				$return_var[] = $start + $i;
771 771
 		}
772 772
 		return $return_var;
Please login to merge, or discard this patch.
Braces   +236 added lines, -173 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 3
14 14
  */
15 15
 
16
-if (!defined('SMF'))
16
+if (!defined('SMF')) {
17 17
 	die('No direct access...');
18
+}
18 19
 
19 20
 /**
20 21
  *  Maps the implementations in this file (smf_db_function_name)
@@ -33,8 +34,8 @@  discard block
 block discarded – undo
33 34
 	global $smcFunc, $mysql_set_mode;
34 35
 
35 36
 	// Map some database specific functions, only do this once.
36
-	if (!isset($smcFunc['db_fetch_assoc']) || $smcFunc['db_fetch_assoc'] != 'mysql_fetch_assoc')
37
-		$smcFunc += array(
37
+	if (!isset($smcFunc['db_fetch_assoc']) || $smcFunc['db_fetch_assoc'] != 'mysql_fetch_assoc') {
38
+			$smcFunc += array(
38 39
 			'db_query' => 'smf_db_query',
39 40
 			'db_quote' => 'smf_db_quote',
40 41
 			'db_fetch_assoc' => 'mysql_fetch_assoc',
@@ -58,36 +59,42 @@  discard block
 block discarded – undo
58 59
 			'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string',
59 60
 			'db_is_resource' => 'is_resource',
60 61
 		);
62
+	}
61 63
 
62
-	if (!empty($db_options['port']))
63
-		$db_server .= ':' . $db_options['port'];
64
+	if (!empty($db_options['port'])) {
65
+			$db_server .= ':' . $db_options['port'];
66
+	}
64 67
 	
65 68
 	$flags = 2; //#define CLIENT_FOUND_ROWS 2 /* Found instead of affected rows */
66 69
 
67
-	if (!empty($db_options['persist']))
68
-		$connection = @mysql_pconnect($db_server, $db_user, $db_passwd, $flags);
69
-	else
70
-		$connection = @mysql_connect($db_server, $db_user, $db_passwd, false, $flags);
70
+	if (!empty($db_options['persist'])) {
71
+			$connection = @mysql_pconnect($db_server, $db_user, $db_passwd, $flags);
72
+	} else {
73
+			$connection = @mysql_connect($db_server, $db_user, $db_passwd, false, $flags);
74
+	}
71 75
 
72 76
 	// Something's wrong, show an error if its fatal (which we assume it is)
73 77
 	if (!$connection)
74 78
 	{
75
-		if (!empty($db_options['non_fatal']))
76
-			return null;
77
-		else
78
-			display_db_error();
79
+		if (!empty($db_options['non_fatal'])) {
80
+					return null;
81
+		} else {
82
+					display_db_error();
83
+		}
79 84
 	}
80 85
 
81 86
 	// Select the database, unless told not to
82
-	if (empty($db_options['dont_select_db']) && !@mysql_select_db($db_name, $connection) && empty($db_options['non_fatal']))
83
-		display_db_error();
87
+	if (empty($db_options['dont_select_db']) && !@mysql_select_db($db_name, $connection) && empty($db_options['non_fatal'])) {
88
+			display_db_error();
89
+	}
84 90
 
85 91
 	// This makes it possible to have SMF automatically change the sql_mode and autocommit if needed.
86
-	if (isset($mysql_set_mode) && $mysql_set_mode === true)
87
-		$smcFunc['db_query']('', 'SET sql_mode = \'\', AUTOCOMMIT = 1',
92
+	if (isset($mysql_set_mode) && $mysql_set_mode === true) {
93
+			$smcFunc['db_query']('', 'SET sql_mode = \'\', AUTOCOMMIT = 1',
88 94
 		array(),
89 95
 		false
90 96
 	);
97
+	}
91 98
 
92 99
 	return $connection;
93 100
 }
@@ -134,37 +141,46 @@  discard block
 block discarded – undo
134 141
 	list ($values, $connection) = $db_callback;
135 142
 
136 143
 	// Connection gone???  This should *never* happen at this point, yet it does :'(
137
-	if (!is_resource($connection))
138
-		display_db_error();
144
+	if (!is_resource($connection)) {
145
+			display_db_error();
146
+	}
139 147
 
140
-	if ($matches[1] === 'db_prefix')
141
-		return $db_prefix;
148
+	if ($matches[1] === 'db_prefix') {
149
+			return $db_prefix;
150
+	}
142 151
 
143
-	if ($matches[1] === 'query_see_board')
144
-		return $user_info['query_see_board'];
152
+	if ($matches[1] === 'query_see_board') {
153
+			return $user_info['query_see_board'];
154
+	}
145 155
 
146
-	if ($matches[1] === 'query_wanna_see_board')
147
-		return $user_info['query_wanna_see_board'];
156
+	if ($matches[1] === 'query_wanna_see_board') {
157
+			return $user_info['query_wanna_see_board'];
158
+	}
148 159
 
149
-	if ($matches[1] === 'empty')
150
-		return '\'\'';
160
+	if ($matches[1] === 'empty') {
161
+			return '\'\'';
162
+	}
151 163
 
152
-	if (!isset($matches[2]))
153
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
164
+	if (!isset($matches[2])) {
165
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
166
+	}
154 167
 
155
-	if ($matches[1] === 'literal')
156
-		return '\'' . mysql_real_escape_string($matches[2], $connection) . '\'';
168
+	if ($matches[1] === 'literal') {
169
+			return '\'' . mysql_real_escape_string($matches[2], $connection) . '\'';
170
+	}
157 171
 
158
-	if (!isset($values[$matches[2]]))
159
-		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__);
172
+	if (!isset($values[$matches[2]])) {
173
+			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__);
174
+	}
160 175
 
161 176
 	$replacement = $values[$matches[2]];
162 177
 
163 178
 	switch ($matches[1])
164 179
 	{
165 180
 		case 'int':
166
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
167
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
181
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
182
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
183
+			}
168 184
 			return (string) (int) $replacement;
169 185
 		break;
170 186
 
@@ -176,56 +192,63 @@  discard block
 block discarded – undo
176 192
 		case 'array_int':
177 193
 			if (is_array($replacement))
178 194
 			{
179
-				if (empty($replacement))
180
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
195
+				if (empty($replacement)) {
196
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
197
+				}
181 198
 
182 199
 				foreach ($replacement as $key => $value)
183 200
 				{
184
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
185
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
201
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
202
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
203
+					}
186 204
 
187 205
 					$replacement[$key] = (string) (int) $value;
188 206
 				}
189 207
 
190 208
 				return implode(', ', $replacement);
209
+			} else {
210
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
191 211
 			}
192
-			else
193
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
194 212
 
195 213
 		break;
196 214
 
197 215
 		case 'array_string':
198 216
 			if (is_array($replacement))
199 217
 			{
200
-				if (empty($replacement))
201
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
218
+				if (empty($replacement)) {
219
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
220
+				}
202 221
 
203
-				foreach ($replacement as $key => $value)
204
-					$replacement[$key] = sprintf('\'%1$s\'', mysql_real_escape_string($value, $connection));
222
+				foreach ($replacement as $key => $value) {
223
+									$replacement[$key] = sprintf('\'%1$s\'', mysql_real_escape_string($value, $connection));
224
+				}
205 225
 
206 226
 				return implode(', ', $replacement);
227
+			} else {
228
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
207 229
 			}
208
-			else
209
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
210 230
 		break;
211 231
 
212 232
 		case 'date':
213
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
214
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
215
-			else
216
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
233
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
234
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
235
+			} else {
236
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
237
+			}
217 238
 		break;
218 239
 
219 240
 		case 'time':
220
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
221
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
222
-			else
223
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
241
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
242
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
243
+			} else {
244
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
245
+			}
224 246
 		break;
225 247
 
226 248
 		case 'float':
227
-			if (!is_numeric($replacement))
228
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
249
+			if (!is_numeric($replacement)) {
250
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
251
+			}
229 252
 			return (string) (float) $replacement;
230 253
 		break;
231 254
 
@@ -239,32 +262,37 @@  discard block
 block discarded – undo
239 262
 		break;
240 263
 
241 264
 		case 'inet':
242
-			if ($replacement == 'null' || $replacement == '')
243
-				return 'null';
244
-			if (!isValidIP($replacement))
245
-				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 (!isValidIP($replacement)) {
269
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
270
+			}
246 271
 			//we don't use the native support of mysql > 5.6.2
247 272
 			return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement)));
248 273
 
249 274
 		case 'array_inet':
250 275
 			if (is_array($replacement))
251 276
 			{
252
-				if (empty($replacement))
253
-					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
+				}
254 280
 
255 281
 				foreach ($replacement as $key => $value)
256 282
 				{
257
-					if ($replacement == 'null' || $replacement == '')
258
-						$replacement[$key] = 'null';
259
-					if (!isValidIP($value))
260
-						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
+					}
261 289
 					$replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value)));
262 290
 				}
263 291
 
264 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__);
265 295
 			}
266
-			else
267
-				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 296
 		break;
269 297
 
270 298
 		default:
@@ -335,18 +363,20 @@  discard block
 block discarded – undo
335 363
 	// One more query....
336 364
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
337 365
 
338
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
339
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
366
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
367
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
368
+	}
340 369
 
341 370
 	// Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By
342 371
 	if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && preg_match('~^\s+SELECT~i', $db_string))
343 372
 	{
344 373
 		// Add before LIMIT
345
-		if ($pos = strpos($db_string, 'LIMIT '))
346
-			$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
347
-		else
348
-			// Append it.
374
+		if ($pos = strpos($db_string, 'LIMIT ')) {
375
+					$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
376
+		} else {
377
+					// Append it.
349 378
 			$db_string .= "\n\t\t\tORDER BY null";
379
+		}
350 380
 	}
351 381
 
352 382
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
@@ -368,8 +398,9 @@  discard block
 block discarded – undo
368 398
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
369 399
 
370 400
 		// Initialize $db_cache if not already initialized.
371
-		if (!isset($db_cache))
372
-			$db_cache = array();
401
+		if (!isset($db_cache)) {
402
+					$db_cache = array();
403
+		}
373 404
 
374 405
 		if (!empty($_SESSION['debug_redirect']))
375 406
 		{
@@ -395,17 +426,18 @@  discard block
 block discarded – undo
395 426
 		while (true)
396 427
 		{
397 428
 			$pos = strpos($db_string, '\'', $pos + 1);
398
-			if ($pos === false)
399
-				break;
429
+			if ($pos === false) {
430
+							break;
431
+			}
400 432
 			$clean .= substr($db_string, $old_pos, $pos - $old_pos);
401 433
 
402 434
 			while (true)
403 435
 			{
404 436
 				$pos1 = strpos($db_string, '\'', $pos + 1);
405 437
 				$pos2 = strpos($db_string, '\\', $pos + 1);
406
-				if ($pos1 === false)
407
-					break;
408
-				elseif ($pos2 === false || $pos2 > $pos1)
438
+				if ($pos1 === false) {
439
+									break;
440
+				} elseif ($pos2 === false || $pos2 > $pos1)
409 441
 				{
410 442
 					$pos = $pos1;
411 443
 					break;
@@ -421,29 +453,35 @@  discard block
 block discarded – undo
421 453
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
422 454
 
423 455
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
424
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
425
-			$fail = true;
456
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
457
+					$fail = true;
458
+		}
426 459
 		// Trying to change passwords, slow us down, or something?
427
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
428
-			$fail = true;
429
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
430
-			$fail = true;
460
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
461
+					$fail = true;
462
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
463
+					$fail = true;
464
+		}
431 465
 
432
-		if (!empty($fail) && function_exists('log_error'))
433
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
466
+		if (!empty($fail) && function_exists('log_error')) {
467
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
468
+		}
434 469
 	}
435 470
 
436
-	if (empty($db_unbuffered))
437
-		$ret = @mysql_query($db_string, $connection);
438
-	else
439
-		$ret = @mysql_unbuffered_query($db_string, $connection);
471
+	if (empty($db_unbuffered)) {
472
+			$ret = @mysql_query($db_string, $connection);
473
+	} else {
474
+			$ret = @mysql_unbuffered_query($db_string, $connection);
475
+	}
440 476
 
441
-	if ($ret === false && empty($db_values['db_error_skip']))
442
-		$ret = smf_db_error($db_string, $connection);
477
+	if ($ret === false && empty($db_values['db_error_skip'])) {
478
+			$ret = smf_db_error($db_string, $connection);
479
+	}
443 480
 
444 481
 	// Debugging.
445
-	if (isset($db_show_debug) && $db_show_debug === true)
446
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
482
+	if (isset($db_show_debug) && $db_show_debug === true) {
483
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
484
+	}
447 485
 
448 486
 	return $ret;
449 487
 }
@@ -490,12 +528,13 @@  discard block
 block discarded – undo
490 528
 	// Decide which connection to use
491 529
 	$connection = $connection === null ? $db_connection : $connection;
492 530
 
493
-	if ($type == 'begin')
494
-		return @mysql_query('BEGIN', $connection);
495
-	elseif ($type == 'rollback')
496
-		return @mysql_query('ROLLBACK', $connection);
497
-	elseif ($type == 'commit')
498
-		return @mysql_query('COMMIT', $connection);
531
+	if ($type == 'begin') {
532
+			return @mysql_query('BEGIN', $connection);
533
+	} elseif ($type == 'rollback') {
534
+			return @mysql_query('ROLLBACK', $connection);
535
+	} elseif ($type == 'commit') {
536
+			return @mysql_query('COMMIT', $connection);
537
+	}
499 538
 
500 539
 	return false;
501 540
 }
@@ -535,8 +574,9 @@  discard block
 block discarded – undo
535 574
 	//    2013: Lost connection to server during query.
536 575
 
537 576
 	// Log the error.
538
-	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error'))
539
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
577
+	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) {
578
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
579
+	}
540 580
 
541 581
 	// Database error auto fixing ;).
542 582
 	if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1'))
@@ -545,8 +585,9 @@  discard block
 block discarded – undo
545 585
 		$old_cache = @$modSettings['cache_enable'];
546 586
 		$modSettings['cache_enable'] = '1';
547 587
 
548
-		if (($temp = cache_get_data('db_last_error', 600)) !== null)
549
-			$db_last_error = max(@$db_last_error, $temp);
588
+		if (($temp = cache_get_data('db_last_error', 600)) !== null) {
589
+					$db_last_error = max(@$db_last_error, $temp);
590
+		}
550 591
 
551 592
 		if (@$db_last_error < time() - 3600 * 24 * 3)
552 593
 		{
@@ -562,8 +603,9 @@  discard block
 block discarded – undo
562 603
 					foreach ($tables as $table)
563 604
 					{
564 605
 						// Now, it's still theoretically possible this could be an injection.  So backtick it!
565
-						if (trim($table) != '')
566
-							$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
606
+						if (trim($table) != '') {
607
+													$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
608
+						}
567 609
 					}
568 610
 				}
569 611
 
@@ -572,8 +614,9 @@  discard block
 block discarded – undo
572 614
 			// Table crashed.  Let's try to fix it.
573 615
 			elseif ($query_errno == 1016)
574 616
 			{
575
-				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0)
576
-					$fix_tables = array('`' . $match[1] . '`');
617
+				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) {
618
+									$fix_tables = array('`' . $match[1] . '`');
619
+				}
577 620
 			}
578 621
 			// Indexes crashed.  Should be easy to fix!
579 622
 			elseif ($query_errno == 1034 || $query_errno == 1035)
@@ -592,13 +635,15 @@  discard block
 block discarded – undo
592 635
 
593 636
 			// Make a note of the REPAIR...
594 637
 			cache_put_data('db_last_error', time(), 600);
595
-			if (($temp = cache_get_data('db_last_error', 600)) === null)
596
-				updateSettingsFile(array('db_last_error' => time()));
638
+			if (($temp = cache_get_data('db_last_error', 600)) === null) {
639
+							updateSettingsFile(array('db_last_error' => time()));
640
+			}
597 641
 
598 642
 			// Attempt to find and repair the broken table.
599
-			foreach ($fix_tables as $table)
600
-				$smcFunc['db_query']('', "
643
+			foreach ($fix_tables as $table) {
644
+							$smcFunc['db_query']('', "
601 645
 					REPAIR TABLE $table", false, false);
646
+			}
602 647
 
603 648
 			// And send off an email!
604 649
 			sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair'], null, 'dberror');
@@ -607,11 +652,12 @@  discard block
 block discarded – undo
607 652
 
608 653
 			// Try the query again...?
609 654
 			$ret = $smcFunc['db_query']('', $db_string, false, false);
610
-			if ($ret !== false)
611
-				return $ret;
655
+			if ($ret !== false) {
656
+							return $ret;
657
+			}
658
+		} else {
659
+					$modSettings['cache_enable'] = $old_cache;
612 660
 		}
613
-		else
614
-			$modSettings['cache_enable'] = $old_cache;
615 661
 
616 662
 		// Check for the "lost connection" or "deadlock found" errors - and try it just one more time.
617 663
 		if (in_array($query_errno, array(1205, 1213, 2006, 2013)))
@@ -621,22 +667,25 @@  discard block
 block discarded – undo
621 667
 				// Are we in SSI mode?  If so try that username and password first
622 668
 				if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
623 669
 				{
624
-					if (empty($db_persist))
625
-						$db_connection = @mysql_connect($db_server, $ssi_db_user, $ssi_db_passwd);
626
-					else
627
-						$db_connection = @mysql_pconnect($db_server, $ssi_db_user, $ssi_db_passwd);
670
+					if (empty($db_persist)) {
671
+											$db_connection = @mysql_connect($db_server, $ssi_db_user, $ssi_db_passwd);
672
+					} else {
673
+											$db_connection = @mysql_pconnect($db_server, $ssi_db_user, $ssi_db_passwd);
674
+					}
628 675
 				}
629 676
 				// Fall back to the regular username and password if need be
630 677
 				if (!$db_connection)
631 678
 				{
632
-					if (empty($db_persist))
633
-						$db_connection = @mysql_connect($db_server, $db_user, $db_passwd);
634
-					else
635
-						$db_connection = @mysql_pconnect($db_server, $db_user, $db_passwd);
679
+					if (empty($db_persist)) {
680
+											$db_connection = @mysql_connect($db_server, $db_user, $db_passwd);
681
+					} else {
682
+											$db_connection = @mysql_pconnect($db_server, $db_user, $db_passwd);
683
+					}
636 684
 				}
637 685
 
638
-				if (!$db_connection || !@mysql_select_db($db_name, $db_connection))
639
-					$db_connection = false;
686
+				if (!$db_connection || !@mysql_select_db($db_name, $db_connection)) {
687
+									$db_connection = false;
688
+				}
640 689
 			}
641 690
 
642 691
 			if ($db_connection)
@@ -647,24 +696,27 @@  discard block
 block discarded – undo
647 696
 					$ret = $smcFunc['db_query']('', $db_string, false, false);
648 697
 
649 698
 					$new_errno = mysql_errno($db_connection);
650
-					if ($ret !== false || in_array($new_errno, array(1205, 1213)))
651
-						break;
699
+					if ($ret !== false || in_array($new_errno, array(1205, 1213))) {
700
+											break;
701
+					}
652 702
 				}
653 703
 
654 704
 				// If it failed again, shucks to be you... we're not trying it over and over.
655
-				if ($ret !== false)
656
-					return $ret;
705
+				if ($ret !== false) {
706
+									return $ret;
707
+				}
657 708
 			}
658 709
 		}
659 710
 		// Are they out of space, perhaps?
660 711
 		elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false))
661 712
 		{
662
-			if (!isset($txt))
663
-				$query_error .= ' - check database storage space.';
664
-			else
713
+			if (!isset($txt)) {
714
+							$query_error .= ' - check database storage space.';
715
+			} else
665 716
 			{
666
-				if (!isset($txt['mysql_error_space']))
667
-					loadLanguage('Errors');
717
+				if (!isset($txt['mysql_error_space'])) {
718
+									loadLanguage('Errors');
719
+				}
668 720
 
669 721
 				$query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space'];
670 722
 			}
@@ -672,15 +724,17 @@  discard block
 block discarded – undo
672 724
 	}
673 725
 
674 726
 	// Nothing's defined yet... just die with it.
675
-	if (empty($context) || empty($txt))
676
-		die($query_error);
727
+	if (empty($context) || empty($txt)) {
728
+			die($query_error);
729
+	}
677 730
 
678 731
 	// Show an error message, if possible.
679 732
 	$context['error_title'] = $txt['database_error'];
680
-	if (allowedTo('admin_forum'))
681
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
682
-	else
683
-		$context['error_message'] = $txt['try_again'];
733
+	if (allowedTo('admin_forum')) {
734
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
735
+	} else {
736
+			$context['error_message'] = $txt['try_again'];
737
+	}
684 738
 
685 739
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
686 740
 	{
@@ -711,25 +765,28 @@  discard block
 block discarded – undo
711 765
 	$connection = $connection === null ? $db_connection : $connection;
712 766
 
713 767
 	// With nothing to insert, simply return.
714
-	if (empty($data))
715
-		return;
768
+	if (empty($data)) {
769
+			return;
770
+	}
716 771
 
717 772
 	// Replace the prefix holder with the actual prefix.
718 773
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
719 774
 
720 775
 	// Inserting data as a single row can be done as a single array.
721
-	if (!is_array($data[array_rand($data)]))
722
-		$data = array($data);
776
+	if (!is_array($data[array_rand($data)])) {
777
+			$data = array($data);
778
+	}
723 779
 
724 780
 	// Create the mold for a single row insert.
725 781
 	$insertData = '(';
726 782
 	foreach ($columns as $columnName => $type)
727 783
 	{
728 784
 		// Are we restricting the length?
729
-		if (strpos($type, 'string-') !== false)
730
-			$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
731
-		else
732
-			$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
785
+		if (strpos($type, 'string-') !== false) {
786
+					$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
787
+		} else {
788
+					$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
789
+		}
733 790
 	}
734 791
 	$insertData = substr($insertData, 0, -2) . ')';
735 792
 
@@ -738,8 +795,9 @@  discard block
 block discarded – undo
738 795
 
739 796
 	// Here's where the variables are injected to the query.
740 797
 	$insertRows = array();
741
-	foreach ($data as $dataRow)
742
-		$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
798
+	foreach ($data as $dataRow) {
799
+			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
800
+	}
743 801
 
744 802
 	// Determine the method of insertion.
745 803
 	$queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT');
@@ -759,15 +817,16 @@  discard block
 block discarded – undo
759 817
 	
760 818
 	if(!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0)
761 819
 	{
762
-		if ($returnmode == 1)
763
-			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
764
-		else if ($returnmode == 2)
820
+		if ($returnmode == 1) {
821
+					$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
822
+		} else if ($returnmode == 2)
765 823
 		{
766 824
 			$return_var = array();
767 825
 			$count = count($insertRows);
768 826
 			$start = smf_db_insert_id($table, $keys[0]);
769
-			for ($i = 0; $i < $count; $i++ )
770
-				$return_var[] = $start + $i;
827
+			for ($i = 0; $i < $count; $i++ ) {
828
+							$return_var[] = $start + $i;
829
+			}
771 830
 		}
772 831
 		return $return_var;
773 832
 	}
@@ -785,8 +844,9 @@  discard block
 block discarded – undo
785 844
  */
786 845
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
787 846
 {
788
-	if (empty($log_message))
789
-		$log_message = $error_message;
847
+	if (empty($log_message)) {
848
+			$log_message = $error_message;
849
+	}
790 850
 
791 851
 	foreach (debug_backtrace() as $step)
792 852
 	{
@@ -805,12 +865,14 @@  discard block
 block discarded – undo
805 865
 	}
806 866
 
807 867
 	// A special case - we want the file and line numbers for debugging.
808
-	if ($error_type == 'return')
809
-		return array($file, $line);
868
+	if ($error_type == 'return') {
869
+			return array($file, $line);
870
+	}
810 871
 
811 872
 	// Is always a critical error.
812
-	if (function_exists('log_error'))
813
-		log_error($log_message, 'critical', $file, $line);
873
+	if (function_exists('log_error')) {
874
+			log_error($log_message, 'critical', $file, $line);
875
+	}
814 876
 
815 877
 	if (function_exists('fatal_error'))
816 878
 	{
@@ -818,12 +880,12 @@  discard block
 block discarded – undo
818 880
 
819 881
 		// Cannot continue...
820 882
 		exit;
883
+	} elseif ($error_type) {
884
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
885
+	} else {
886
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
887
+	}
821 888
 	}
822
-	elseif ($error_type)
823
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
824
-	else
825
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
826
-}
827 889
 
828 890
 /**
829 891
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -840,10 +902,11 @@  discard block
 block discarded – undo
840 902
 		'\\' => '\\\\',
841 903
 	);
842 904
 
843
-	if ($translate_human_wildcards)
844
-		$replacements += array(
905
+	if ($translate_human_wildcards) {
906
+			$replacements += array(
845 907
 			'*' => '%',
846 908
 		);
909
+	}
847 910
 
848 911
 	return strtr($string, $replacements);
849 912
 }
Please login to merge, or discard this patch.