Completed
Push — release-2.1 ( e3ceef...dd179b )
by Colin
07:50
created
Sources/Subs-Db-mysql.php 1 patch
Braces   +254 added lines, -187 removed lines patch added patch discarded remove patch
@@ -13,8 +13,9 @@  discard block
 block discarded – undo
13 13
  * @version 2.1 Beta 4
14 14
  */
15 15
 
16
-if (!defined('SMF'))
16
+if (!defined('SMF')) {
17 17
 	die('No direct access...');
18
+}
18 19
 
19 20
 /**
20 21
  *  Maps the implementations in this file (smf_db_function_name)
@@ -33,8 +34,8 @@  discard block
 block discarded – undo
33 34
 	global $smcFunc;
34 35
 
35 36
 	// Map some database specific functions, only do this once.
36
-	if (!isset($smcFunc['db_fetch_assoc']))
37
-		$smcFunc += array(
37
+	if (!isset($smcFunc['db_fetch_assoc'])) {
38
+			$smcFunc += array(
38 39
 			'db_query'                  => 'smf_db_query',
39 40
 			'db_quote'                  => 'smf_db_quote',
40 41
 			'db_fetch_assoc'            => 'mysqli_fetch_assoc',
@@ -64,9 +65,11 @@  discard block
 block discarded – undo
64 65
 			'db_custom_order'			=> 'smf_db_custom_order',
65 66
 			'db_native_replace'			=> 'smf_db_native_replace',
66 67
 		);
68
+	}
67 69
 
68
-	if (!empty($db_options['persist']))
69
-		$db_server = 'p:' . $db_server;
70
+	if (!empty($db_options['persist'])) {
71
+			$db_server = 'p:' . $db_server;
72
+	}
70 73
 
71 74
 	$connection = mysqli_init();
72 75
 
@@ -76,24 +79,27 @@  discard block
 block discarded – undo
76 79
 
77 80
 	if ($connection)
78 81
 	{
79
-		if (!empty($db_options['port']))
80
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags);
81
-		else
82
-			$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags);
82
+		if (!empty($db_options['port'])) {
83
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags);
84
+		} else {
85
+					$success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags);
86
+		}
83 87
 	}
84 88
 
85 89
 	// Something's wrong, show an error if its fatal (which we assume it is)
86 90
 	if ($success === false)
87 91
 	{
88
-		if (!empty($db_options['non_fatal']))
89
-			return null;
90
-		else
91
-			display_db_error();
92
+		if (!empty($db_options['non_fatal'])) {
93
+					return null;
94
+		} else {
95
+					display_db_error();
96
+		}
92 97
 	}
93 98
 
94 99
 	// Select the database, unless told not to
95
-	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal']))
96
-		display_db_error();
100
+	if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal'])) {
101
+			display_db_error();
102
+	}
97 103
 
98 104
 	mysqli_query($connection, 'SET SESSION sql_mode = \'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\'');
99 105
 
@@ -166,34 +172,42 @@  discard block
 block discarded – undo
166 172
 	global $db_callback, $user_info, $db_prefix, $smcFunc;
167 173
 
168 174
 	list ($values, $connection) = $db_callback;
169
-	if (!is_object($connection))
170
-		display_db_error();
175
+	if (!is_object($connection)) {
176
+			display_db_error();
177
+	}
171 178
 
172
-	if ($matches[1] === 'db_prefix')
173
-		return $db_prefix;
179
+	if ($matches[1] === 'db_prefix') {
180
+			return $db_prefix;
181
+	}
174 182
 
175
-	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false)
176
-		return $user_info[$matches[1]];
183
+	if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) {
184
+			return $user_info[$matches[1]];
185
+	}
177 186
 
178
-	if ($matches[1] === 'empty')
179
-		return '\'\'';
187
+	if ($matches[1] === 'empty') {
188
+			return '\'\'';
189
+	}
180 190
 
181
-	if (!isset($matches[2]))
182
-		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
191
+	if (!isset($matches[2])) {
192
+			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__);
193
+	}
183 194
 
184
-	if ($matches[1] === 'literal')
185
-		return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
195
+	if ($matches[1] === 'literal') {
196
+			return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\'';
197
+	}
186 198
 
187
-	if (!isset($values[$matches[2]]))
188
-		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__);
199
+	if (!isset($values[$matches[2]])) {
200
+			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__);
201
+	}
189 202
 
190 203
 	$replacement = $values[$matches[2]];
191 204
 
192 205
 	switch ($matches[1])
193 206
 	{
194 207
 		case 'int':
195
-			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement)
196
-				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
208
+			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) {
209
+							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
210
+			}
197 211
 			return (string) (int) $replacement;
198 212
 		break;
199 213
 
@@ -205,65 +219,73 @@  discard block
 block discarded – undo
205 219
 		case 'array_int':
206 220
 			if (is_array($replacement))
207 221
 			{
208
-				if (empty($replacement))
209
-					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
222
+				if (empty($replacement)) {
223
+									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
224
+				}
210 225
 
211 226
 				foreach ($replacement as $key => $value)
212 227
 				{
213
-					if (!is_numeric($value) || (string) $value !== (string) (int) $value)
214
-						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
228
+					if (!is_numeric($value) || (string) $value !== (string) (int) $value) {
229
+											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
230
+					}
215 231
 
216 232
 					$replacement[$key] = (string) (int) $value;
217 233
 				}
218 234
 
219 235
 				return implode(', ', $replacement);
236
+			} else {
237
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
220 238
 			}
221
-			else
222
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
223 239
 
224 240
 		break;
225 241
 
226 242
 		case 'array_string':
227 243
 			if (is_array($replacement))
228 244
 			{
229
-				if (empty($replacement))
230
-					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
245
+				if (empty($replacement)) {
246
+									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
247
+				}
231 248
 
232
-				foreach ($replacement as $key => $value)
233
-					$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
249
+				foreach ($replacement as $key => $value) {
250
+									$replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value));
251
+				}
234 252
 
235 253
 				return implode(', ', $replacement);
254
+			} else {
255
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
236 256
 			}
237
-			else
238
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
239 257
 		break;
240 258
 
241 259
 		case 'date':
242
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1)
243
-				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
244
-			else
245
-				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
260
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) {
261
+							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]);
262
+			} else {
263
+							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
264
+			}
246 265
 		break;
247 266
 
248 267
 		case 'time':
249
-			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1)
250
-				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
251
-			else
252
-				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
268
+			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) {
269
+							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]);
270
+			} else {
271
+							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
272
+			}
253 273
 		break;
254 274
 
255 275
 		case 'datetime':
256
-			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1)
257
-				return 'str_to_date('.
276
+			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) {
277
+							return 'str_to_date('.
258 278
 					sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5] ,$datetime_matches[6]).
259 279
 					',\'%Y-%m-%d %h:%i:%s\')';
260
-			else
261
-				smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
280
+			} else {
281
+							smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
282
+			}
262 283
 		break;
263 284
 
264 285
 		case 'float':
265
-			if (!is_numeric($replacement))
266
-				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
286
+			if (!is_numeric($replacement)) {
287
+							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
288
+			}
267 289
 			return (string) (float) $replacement;
268 290
 		break;
269 291
 
@@ -277,32 +299,37 @@  discard block
 block discarded – undo
277 299
 		break;
278 300
 
279 301
 		case 'inet':
280
-			if ($replacement == 'null' || $replacement == '')
281
-				return 'null';
282
-			if (!isValidIP($replacement))
283
-				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
302
+			if ($replacement == 'null' || $replacement == '') {
303
+							return 'null';
304
+			}
305
+			if (!isValidIP($replacement)) {
306
+							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
307
+			}
284 308
 			//we don't use the native support of mysql > 5.6.2
285 309
 			return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement)));
286 310
 
287 311
 		case 'array_inet':
288 312
 			if (is_array($replacement))
289 313
 			{
290
-				if (empty($replacement))
291
-					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
314
+				if (empty($replacement)) {
315
+									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
316
+				}
292 317
 
293 318
 				foreach ($replacement as $key => $value)
294 319
 				{
295
-					if ($replacement == 'null' || $replacement == '')
296
-						$replacement[$key] = 'null';
297
-					if (!isValidIP($value))
298
-						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
320
+					if ($replacement == 'null' || $replacement == '') {
321
+											$replacement[$key] = 'null';
322
+					}
323
+					if (!isValidIP($value)) {
324
+											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
325
+					}
299 326
 					$replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value)));
300 327
 				}
301 328
 
302 329
 				return implode(', ', $replacement);
330
+			} else {
331
+							smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
303 332
 			}
304
-			else
305
-				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
306 333
 		break;
307 334
 
308 335
 		default:
@@ -373,18 +400,20 @@  discard block
 block discarded – undo
373 400
 	// One more query....
374 401
 	$db_count = !isset($db_count) ? 1 : $db_count + 1;
375 402
 
376
-	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override']))
377
-		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
403
+	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) {
404
+			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__);
405
+	}
378 406
 
379 407
 	// Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By
380 408
 	if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && preg_match('~^\s+SELECT~i', $db_string))
381 409
 	{
382 410
 		// Add before LIMIT
383
-		if ($pos = strpos($db_string, 'LIMIT '))
384
-			$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
385
-		else
386
-			// Append it.
411
+		if ($pos = strpos($db_string, 'LIMIT ')) {
412
+					$db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string));
413
+		} else {
414
+					// Append it.
387 415
 			$db_string .= "\n\t\t\tORDER BY null";
416
+		}
388 417
 	}
389 418
 
390 419
 	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false))
@@ -410,17 +439,18 @@  discard block
 block discarded – undo
410 439
 		while (true)
411 440
 		{
412 441
 			$pos = strpos($db_string_1, '\'', $pos + 1);
413
-			if ($pos === false)
414
-				break;
442
+			if ($pos === false) {
443
+							break;
444
+			}
415 445
 			$clean .= substr($db_string_1, $old_pos, $pos - $old_pos);
416 446
 
417 447
 			while (true)
418 448
 			{
419 449
 				$pos1 = strpos($db_string_1, '\'', $pos + 1);
420 450
 				$pos2 = strpos($db_string_1, '\\', $pos + 1);
421
-				if ($pos1 === false)
422
-					break;
423
-				elseif ($pos2 === false || $pos2 > $pos1)
451
+				if ($pos1 === false) {
452
+									break;
453
+				} elseif ($pos2 === false || $pos2 > $pos1)
424 454
 				{
425 455
 					$pos = $pos1;
426 456
 					break;
@@ -436,16 +466,19 @@  discard block
 block discarded – undo
436 466
 		$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
437 467
 
438 468
 		// Comments?  We don't use comments in our queries, we leave 'em outside!
439
-		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)
440
-			$fail = true;
469
+		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) {
470
+					$fail = true;
471
+		}
441 472
 		// Trying to change passwords, slow us down, or something?
442
-		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0)
443
-			$fail = true;
444
-		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0)
445
-			$fail = true;
473
+		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) {
474
+					$fail = true;
475
+		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
476
+					$fail = true;
477
+		}
446 478
 
447
-		if (!empty($fail) && function_exists('log_error'))
448
-			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
479
+		if (!empty($fail) && function_exists('log_error')) {
480
+					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__);
481
+		}
449 482
 	}
450 483
 
451 484
 	// Debugging.
@@ -455,8 +488,9 @@  discard block
 block discarded – undo
455 488
 		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__);
456 489
 
457 490
 		// Initialize $db_cache if not already initialized.
458
-		if (!isset($db_cache))
459
-			$db_cache = array();
491
+		if (!isset($db_cache)) {
492
+					$db_cache = array();
493
+		}
460 494
 
461 495
 		if (!empty($_SESSION['debug_redirect']))
462 496
 		{
@@ -472,17 +506,20 @@  discard block
 block discarded – undo
472 506
 		$db_cache[$db_count]['s'] = ($st = microtime(true)) - $time_start;
473 507
 	}
474 508
 
475
-	if (empty($db_unbuffered))
476
-		$ret = @mysqli_query($connection, $db_string);
477
-	else
478
-		$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
509
+	if (empty($db_unbuffered)) {
510
+			$ret = @mysqli_query($connection, $db_string);
511
+	} else {
512
+			$ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT);
513
+	}
479 514
 
480
-	if ($ret === false && empty($db_values['db_error_skip']))
481
-		$ret = smf_db_error($db_string, $connection);
515
+	if ($ret === false && empty($db_values['db_error_skip'])) {
516
+			$ret = smf_db_error($db_string, $connection);
517
+	}
482 518
 
483 519
 	// Debugging.
484
-	if (isset($db_show_debug) && $db_show_debug === true)
485
-		$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
520
+	if (isset($db_show_debug) && $db_show_debug === true) {
521
+			$db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
522
+	}
486 523
 
487 524
 	return $ret;
488 525
 }
@@ -529,12 +566,13 @@  discard block
 block discarded – undo
529 566
 	// Decide which connection to use
530 567
 	$connection = $connection === null ? $db_connection : $connection;
531 568
 
532
-	if ($type == 'begin')
533
-		return @mysqli_query($connection, 'BEGIN');
534
-	elseif ($type == 'rollback')
535
-		return @mysqli_query($connection, 'ROLLBACK');
536
-	elseif ($type == 'commit')
537
-		return @mysqli_query($connection, 'COMMIT');
569
+	if ($type == 'begin') {
570
+			return @mysqli_query($connection, 'BEGIN');
571
+	} elseif ($type == 'rollback') {
572
+			return @mysqli_query($connection, 'ROLLBACK');
573
+	} elseif ($type == 'commit') {
574
+			return @mysqli_query($connection, 'COMMIT');
575
+	}
538 576
 
539 577
 	return false;
540 578
 }
@@ -572,8 +610,9 @@  discard block
 block discarded – undo
572 610
 	//    1213: Deadlock found.
573 611
 
574 612
 	// Log the error.
575
-	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error'))
576
-		log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
613
+	if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) {
614
+			log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line);
615
+	}
577 616
 
578 617
 	// Database error auto fixing ;).
579 618
 	if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1'))
@@ -582,8 +621,9 @@  discard block
 block discarded – undo
582 621
 		$old_cache = @$modSettings['cache_enable'];
583 622
 		$modSettings['cache_enable'] = '1';
584 623
 
585
-		if (($temp = cache_get_data('db_last_error', 600)) !== null)
586
-			$db_last_error = max(@$db_last_error, $temp);
624
+		if (($temp = cache_get_data('db_last_error', 600)) !== null) {
625
+					$db_last_error = max(@$db_last_error, $temp);
626
+		}
587 627
 
588 628
 		if (@$db_last_error < time() - 3600 * 24 * 3)
589 629
 		{
@@ -599,8 +639,9 @@  discard block
 block discarded – undo
599 639
 					foreach ($tables as $table)
600 640
 					{
601 641
 						// Now, it's still theoretically possible this could be an injection.  So backtick it!
602
-						if (trim($table) != '')
603
-							$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
642
+						if (trim($table) != '') {
643
+													$fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`';
644
+						}
604 645
 					}
605 646
 				}
606 647
 
@@ -609,8 +650,9 @@  discard block
 block discarded – undo
609 650
 			// Table crashed.  Let's try to fix it.
610 651
 			elseif ($query_errno == 1016)
611 652
 			{
612
-				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0)
613
-					$fix_tables = array('`' . $match[1] . '`');
653
+				if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) {
654
+									$fix_tables = array('`' . $match[1] . '`');
655
+				}
614 656
 			}
615 657
 			// Indexes crashed.  Should be easy to fix!
616 658
 			elseif ($query_errno == 1034 || $query_errno == 1035)
@@ -629,13 +671,15 @@  discard block
 block discarded – undo
629 671
 
630 672
 			// Make a note of the REPAIR...
631 673
 			cache_put_data('db_last_error', time(), 600);
632
-			if (($temp = cache_get_data('db_last_error', 600)) === null)
633
-				updateSettingsFile(array('db_last_error' => time()));
674
+			if (($temp = cache_get_data('db_last_error', 600)) === null) {
675
+							updateSettingsFile(array('db_last_error' => time()));
676
+			}
634 677
 
635 678
 			// Attempt to find and repair the broken table.
636
-			foreach ($fix_tables as $table)
637
-				$smcFunc['db_query']('', "
679
+			foreach ($fix_tables as $table) {
680
+							$smcFunc['db_query']('', "
638 681
 					REPAIR TABLE $table", false, false);
682
+			}
639 683
 
640 684
 			// And send off an email!
641 685
 			sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair'], null, 'dberror');
@@ -644,11 +688,12 @@  discard block
 block discarded – undo
644 688
 
645 689
 			// Try the query again...?
646 690
 			$ret = $smcFunc['db_query']('', $db_string, false, false);
647
-			if ($ret !== false)
648
-				return $ret;
691
+			if ($ret !== false) {
692
+							return $ret;
693
+			}
694
+		} else {
695
+					$modSettings['cache_enable'] = $old_cache;
649 696
 		}
650
-		else
651
-			$modSettings['cache_enable'] = $old_cache;
652 697
 
653 698
 		// Check for the "lost connection" or "deadlock found" errors - and try it just one more time.
654 699
 		if (in_array($query_errno, array(1205, 1213)))
@@ -661,24 +706,27 @@  discard block
 block discarded – undo
661 706
 					$ret = $smcFunc['db_query']('', $db_string, false, false);
662 707
 
663 708
 					$new_errno = mysqli_errno($db_connection);
664
-					if ($ret !== false || in_array($new_errno, array(1205, 1213)))
665
-						break;
709
+					if ($ret !== false || in_array($new_errno, array(1205, 1213))) {
710
+											break;
711
+					}
666 712
 				}
667 713
 
668 714
 				// If it failed again, shucks to be you... we're not trying it over and over.
669
-				if ($ret !== false)
670
-					return $ret;
715
+				if ($ret !== false) {
716
+									return $ret;
717
+				}
671 718
 			}
672 719
 		}
673 720
 		// Are they out of space, perhaps?
674 721
 		elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false))
675 722
 		{
676
-			if (!isset($txt))
677
-				$query_error .= ' - check database storage space.';
678
-			else
723
+			if (!isset($txt)) {
724
+							$query_error .= ' - check database storage space.';
725
+			} else
679 726
 			{
680
-				if (!isset($txt['mysql_error_space']))
681
-					loadLanguage('Errors');
727
+				if (!isset($txt['mysql_error_space'])) {
728
+									loadLanguage('Errors');
729
+				}
682 730
 
683 731
 				$query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space'];
684 732
 			}
@@ -686,15 +734,17 @@  discard block
 block discarded – undo
686 734
 	}
687 735
 
688 736
 	// Nothing's defined yet... just die with it.
689
-	if (empty($context) || empty($txt))
690
-		die($query_error);
737
+	if (empty($context) || empty($txt)) {
738
+			die($query_error);
739
+	}
691 740
 
692 741
 	// Show an error message, if possible.
693 742
 	$context['error_title'] = $txt['database_error'];
694
-	if (allowedTo('admin_forum'))
695
-		$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
696
-	else
697
-		$context['error_message'] = $txt['try_again'];
743
+	if (allowedTo('admin_forum')) {
744
+			$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line;
745
+	} else {
746
+			$context['error_message'] = $txt['try_again'];
747
+	}
698 748
 
699 749
 	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true)
700 750
 	{
@@ -726,8 +776,9 @@  discard block
 block discarded – undo
726 776
 	$return_var = null;
727 777
 
728 778
 	// With nothing to insert, simply return.
729
-	if (empty($data))
730
-		return;
779
+	if (empty($data)) {
780
+			return;
781
+	}
731 782
 
732 783
 	// Replace the prefix holder with the actual prefix.
733 784
 	$table = str_replace('{db_prefix}', $db_prefix, $table);
@@ -737,23 +788,26 @@  discard block
 block discarded – undo
737 788
 	if (!empty($keys) && (count($keys) > 0) && $returnmode > 0)
738 789
 	{
739 790
 		$with_returning = true;
740
-		if ($returnmode == 2)
741
-			$return_var = array();
791
+		if ($returnmode == 2) {
792
+					$return_var = array();
793
+		}
742 794
 	}
743 795
 
744 796
 	// Inserting data as a single row can be done as a single array.
745
-	if (!is_array($data[array_rand($data)]))
746
-		$data = array($data);
797
+	if (!is_array($data[array_rand($data)])) {
798
+			$data = array($data);
799
+	}
747 800
 
748 801
 	// Create the mold for a single row insert.
749 802
 	$insertData = '(';
750 803
 	foreach ($columns as $columnName => $type)
751 804
 	{
752 805
 		// Are we restricting the length?
753
-		if (strpos($type, 'string-') !== false)
754
-			$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
755
-		else
756
-			$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
806
+		if (strpos($type, 'string-') !== false) {
807
+					$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName);
808
+		} else {
809
+					$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName);
810
+		}
757 811
 	}
758 812
 	$insertData = substr($insertData, 0, -2) . ')';
759 813
 
@@ -762,8 +816,9 @@  discard block
 block discarded – undo
762 816
 
763 817
 	// Here's where the variables are injected to the query.
764 818
 	$insertRows = array();
765
-	foreach ($data as $dataRow)
766
-		$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
819
+	foreach ($data as $dataRow) {
820
+			$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection);
821
+	}
767 822
 
768 823
 	// Determine the method of insertion.
769 824
 	$queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT');
@@ -782,8 +837,7 @@  discard block
 block discarded – undo
782 837
 			),
783 838
 			$connection
784 839
 		);
785
-	}
786
-	else //special way for ignore method with returning
840
+	} else //special way for ignore method with returning
787 841
 	{
788 842
 		$count = count($insertRows);
789 843
 		$ai = 0;
@@ -803,19 +857,21 @@  discard block
 block discarded – undo
803 857
 			);
804 858
 			$new_id = $smcFunc['db_insert_id']();
805 859
 
806
-			if ($last_id != $new_id) //the inserted value was new
860
+			if ($last_id != $new_id) {
861
+				//the inserted value was new
807 862
 			{
808 863
 				$ai = $new_id;
809 864
 			}
810
-			else	// the inserted value already exists we need to find the pk
865
+			} else	// the inserted value already exists we need to find the pk
811 866
 			{
812 867
 				$where_string = '';
813 868
 				$count2 = count($indexed_columns);
814 869
 				for ($x = 0; $x < $count2; $x++)
815 870
 				{
816 871
 					$where_string += key($indexed_columns[$x]) . ' = '. $insertRows[$i][$x];
817
-					if (($x + 1) < $count2)
818
-						$where_string += ' AND ';
872
+					if (($x + 1) < $count2) {
873
+											$where_string += ' AND ';
874
+					}
819 875
 				}
820 876
 
821 877
 				$request = $smcFunc['db_query']('','
@@ -831,25 +887,27 @@  discard block
 block discarded – undo
831 887
 				}
832 888
 			}
833 889
 
834
-			if ($returnmode == 1)
835
-				$return_var = $ai;
836
-			else if ($returnmode == 2)
837
-				$return_var[] = $ai;
890
+			if ($returnmode == 1) {
891
+							$return_var = $ai;
892
+			} else if ($returnmode == 2) {
893
+							$return_var[] = $ai;
894
+			}
838 895
 		}
839 896
 	}
840 897
 
841 898
 
842 899
 	if ($with_returning)
843 900
 	{
844
-		if ($returnmode == 1 && empty($return_var))
845
-			$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
846
-		else if ($returnmode == 2 && empty($return_var))
901
+		if ($returnmode == 1 && empty($return_var)) {
902
+					$return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1;
903
+		} else if ($returnmode == 2 && empty($return_var))
847 904
 		{
848 905
 			$return_var = array();
849 906
 			$count = count($insertRows);
850 907
 			$start = smf_db_insert_id($table, $keys[0]);
851
-			for ($i = 0; $i < $count; $i++ )
852
-				$return_var[] = $start + $i;
908
+			for ($i = 0; $i < $count; $i++ ) {
909
+							$return_var[] = $start + $i;
910
+			}
853 911
 		}
854 912
 		return $return_var;
855 913
 	}
@@ -867,8 +925,9 @@  discard block
 block discarded – undo
867 925
  */
868 926
 function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null)
869 927
 {
870
-	if (empty($log_message))
871
-		$log_message = $error_message;
928
+	if (empty($log_message)) {
929
+			$log_message = $error_message;
930
+	}
872 931
 
873 932
 	foreach (debug_backtrace() as $step)
874 933
 	{
@@ -887,12 +946,14 @@  discard block
 block discarded – undo
887 946
 	}
888 947
 
889 948
 	// A special case - we want the file and line numbers for debugging.
890
-	if ($error_type == 'return')
891
-		return array($file, $line);
949
+	if ($error_type == 'return') {
950
+			return array($file, $line);
951
+	}
892 952
 
893 953
 	// Is always a critical error.
894
-	if (function_exists('log_error'))
895
-		log_error($log_message, 'critical', $file, $line);
954
+	if (function_exists('log_error')) {
955
+			log_error($log_message, 'critical', $file, $line);
956
+	}
896 957
 
897 958
 	if (function_exists('fatal_error'))
898 959
 	{
@@ -900,12 +961,12 @@  discard block
 block discarded – undo
900 961
 
901 962
 		// Cannot continue...
902 963
 		exit;
964
+	} elseif ($error_type) {
965
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
966
+	} else {
967
+			trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
968
+	}
903 969
 	}
904
-	elseif ($error_type)
905
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type);
906
-	else
907
-		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''));
908
-}
909 970
 
910 971
 /**
911 972
  * Escape the LIKE wildcards so that they match the character and not the wildcard.
@@ -922,10 +983,11 @@  discard block
 block discarded – undo
922 983
 		'\\' => '\\\\',
923 984
 	);
924 985
 
925
-	if ($translate_human_wildcards)
926
-		$replacements += array(
986
+	if ($translate_human_wildcards) {
987
+			$replacements += array(
927 988
 			'*' => '%',
928 989
 		);
990
+	}
929 991
 
930 992
 	return strtr($string, $replacements);
931 993
 }
@@ -939,8 +1001,9 @@  discard block
 block discarded – undo
939 1001
  */
940 1002
 function smf_is_resource($result)
941 1003
 {
942
-	if ($result instanceof mysqli_result)
943
-		return true;
1004
+	if ($result instanceof mysqli_result) {
1005
+			return true;
1006
+	}
944 1007
 
945 1008
 	return false;
946 1009
 }
@@ -969,19 +1032,22 @@  discard block
 block discarded – undo
969 1032
 	static $mysql_error_data_prep;
970 1033
 
971 1034
 	// without database we can't do anything
972
-	if (empty($db_connection))
973
-		return;
1035
+	if (empty($db_connection)) {
1036
+			return;
1037
+	}
974 1038
 
975
-	if (empty($mysql_error_data_prep))
976
-			$mysql_error_data_prep = mysqli_prepare($db_connection,
1039
+	if (empty($mysql_error_data_prep)) {
1040
+				$mysql_error_data_prep = mysqli_prepare($db_connection,
977 1041
 				'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line)
978 1042
 													VALUES(		?,		?,		unhex(?), ?, 		?,		?,			?,		?,	?)'
979 1043
 			);
1044
+	}
980 1045
 
981
-	if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false)
982
-		$error_array[2] = bin2hex(inet_pton($error_array[2]));
983
-	else
984
-		$error_array[2] = null;
1046
+	if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false) {
1047
+			$error_array[2] = bin2hex(inet_pton($error_array[2]));
1048
+	} else {
1049
+			$error_array[2] = null;
1050
+	}
985 1051
 	mysqli_stmt_bind_param($mysql_error_data_prep, 'iissssssi',
986 1052
 		$error_array[0], $error_array[1], $error_array[2], $error_array[3], $error_array[4], $error_array[5], $error_array[6],
987 1053
 		$error_array[7], $error_array[8]);
@@ -1003,8 +1069,9 @@  discard block
 block discarded – undo
1003 1069
 	$count = count($array_values);
1004 1070
 	$then = ($desc ? ' THEN -' : ' THEN ');
1005 1071
 
1006
-	for ($i = 0; $i < $count; $i++)
1007
-		$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1072
+	for ($i = 0; $i < $count; $i++) {
1073
+			$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' ';
1074
+	}
1008 1075
 
1009 1076
 	$return .= 'END';
1010 1077
 	return $return;
Please login to merge, or discard this patch.