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