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