| @@ -13,8 +13,9 @@ discard block | ||
| 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 | ||
| 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', | 
| @@ -61,11 +62,13 @@ discard block | ||
| 61 | 62 | 'db_mb4' => true, | 
| 62 | 63 | 'db_ping' => 'pg_ping', | 
| 63 | 64 | ); | 
| 65 | + } | |
| 64 | 66 | |
| 65 | - if (!empty($db_options['persist'])) | |
| 66 | -		$connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); | |
| 67 | - else | |
| 68 | -		$connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); | |
| 67 | +	if (!empty($db_options['persist'])) { | |
| 68 | +			$connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); | |
| 69 | +	} else { | |
| 70 | +			$connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); | |
| 71 | + } | |
| 69 | 72 | |
| 70 | 73 | // Something's wrong, show an error if its fatal (which we assume it is) | 
| 71 | 74 | if (!$connection) | 
| @@ -73,8 +76,7 @@ discard block | ||
| 73 | 76 | if (!empty($db_options['non_fatal'])) | 
| 74 | 77 |  		{ | 
| 75 | 78 | return null; | 
| 76 | - } | |
| 77 | - else | |
| 79 | + } else | |
| 78 | 80 |  		{ | 
| 79 | 81 | display_db_error(); | 
| 80 | 82 | } | 
| @@ -125,35 +127,42 @@ discard block | ||
| 125 | 127 | |
| 126 | 128 | list ($values, $connection) = $db_callback; | 
| 127 | 129 | |
| 128 | - if ($matches[1] === 'db_prefix') | |
| 129 | - return $db_prefix; | |
| 130 | +	if ($matches[1] === 'db_prefix') { | |
| 131 | + return $db_prefix; | |
| 132 | + } | |
| 130 | 133 | |
| 131 | 134 | if (!empty($user_info)) | 
| 132 | 135 |  	{ | 
| 133 | - foreach (array_keys($user_info) as $key) | |
| 134 | - if (strpos($key, 'query_') !== false && $key === $matches[1]) | |
| 136 | +		foreach (array_keys($user_info) as $key) { | |
| 137 | + if (strpos($key, 'query_') !== false && $key === $matches[1]) | |
| 135 | 138 | return $user_info[$matches[1]]; | 
| 139 | + } | |
| 136 | 140 | } | 
| 137 | 141 | |
| 138 | - if ($matches[1] === 'empty') | |
| 139 | - return '\'\''; | |
| 142 | +	if ($matches[1] === 'empty') { | |
| 143 | + return '\'\''; | |
| 144 | + } | |
| 140 | 145 | |
| 141 | - if (!isset($matches[2])) | |
| 142 | -		smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 146 | +	if (!isset($matches[2])) { | |
| 147 | +			smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 148 | + } | |
| 143 | 149 | |
| 144 | - if ($matches[1] === 'literal') | |
| 145 | - return '\'' . pg_escape_string($matches[2]) . '\''; | |
| 150 | +	if ($matches[1] === 'literal') { | |
| 151 | + return '\'' . pg_escape_string($matches[2]) . '\''; | |
| 152 | + } | |
| 146 | 153 | |
| 147 | - if (!isset($values[$matches[2]])) | |
| 148 | -		smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); | |
| 154 | +	if (!isset($values[$matches[2]])) { | |
| 155 | +			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__); | |
| 156 | + } | |
| 149 | 157 | |
| 150 | 158 | $replacement = $values[$matches[2]]; | 
| 151 | 159 | |
| 152 | 160 | switch ($matches[1]) | 
| 153 | 161 |  	{ | 
| 154 | 162 | case 'int': | 
| 155 | - if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) | |
| 156 | -				smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 163 | +			if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) { | |
| 164 | +							smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 165 | + } | |
| 157 | 166 | return (string) (int) $replacement; | 
| 158 | 167 | break; | 
| 159 | 168 | |
| @@ -165,65 +174,73 @@ discard block | ||
| 165 | 174 | case 'array_int': | 
| 166 | 175 | if (is_array($replacement)) | 
| 167 | 176 |  			{ | 
| 168 | - if (empty($replacement)) | |
| 169 | -					smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 177 | +				if (empty($replacement)) { | |
| 178 | +									smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 179 | + } | |
| 170 | 180 | |
| 171 | 181 | foreach ($replacement as $key => $value) | 
| 172 | 182 |  				{ | 
| 173 | - if (!is_numeric($value) || (string) $value !== (string) (int) $value) | |
| 174 | -						smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 183 | +					if (!is_numeric($value) || (string) $value !== (string) (int) $value) { | |
| 184 | +											smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 185 | + } | |
| 175 | 186 | |
| 176 | 187 | $replacement[$key] = (string) (int) $value; | 
| 177 | 188 | } | 
| 178 | 189 | |
| 179 | 190 |  				return implode(', ', $replacement); | 
| 191 | +			} else { | |
| 192 | +							smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 180 | 193 | } | 
| 181 | - else | |
| 182 | -				smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 183 | 194 | |
| 184 | 195 | break; | 
| 185 | 196 | |
| 186 | 197 | case 'array_string': | 
| 187 | 198 | if (is_array($replacement)) | 
| 188 | 199 |  			{ | 
| 189 | - if (empty($replacement)) | |
| 190 | -					smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 200 | +				if (empty($replacement)) { | |
| 201 | +									smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 202 | + } | |
| 191 | 203 | |
| 192 | - foreach ($replacement as $key => $value) | |
| 193 | -					$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); | |
| 204 | +				foreach ($replacement as $key => $value) { | |
| 205 | +									$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); | |
| 206 | + } | |
| 194 | 207 | |
| 195 | 208 |  				return implode(', ', $replacement); | 
| 209 | +			} else { | |
| 210 | +							smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 196 | 211 | } | 
| 197 | - else | |
| 198 | -				smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 199 | 212 | break; | 
| 200 | 213 | |
| 201 | 214 | case 'date': | 
| 202 | -			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) | |
| 203 | -				return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; | |
| 204 | - else | |
| 205 | -				smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 215 | +			if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) { | |
| 216 | +							return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; | |
| 217 | +			} else { | |
| 218 | +							smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 219 | + } | |
| 206 | 220 | break; | 
| 207 | 221 | |
| 208 | 222 | case 'time': | 
| 209 | -			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) | |
| 210 | -				return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; | |
| 211 | - else | |
| 212 | -				smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 223 | +			if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) { | |
| 224 | +							return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; | |
| 225 | +			} else { | |
| 226 | +							smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 227 | + } | |
| 213 | 228 | break; | 
| 214 | 229 | |
| 215 | 230 | case 'datetime': | 
| 216 | -			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) | |
| 217 | -				return 'to_timestamp('. | |
| 231 | +			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) { | |
| 232 | +							return 'to_timestamp('. | |
| 218 | 233 |  					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]). | 
| 219 | 234 | ',\'YYYY-MM-DD HH24:MI:SS\')'; | 
| 220 | - else | |
| 221 | -				smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 235 | +			} else { | |
| 236 | +							smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 237 | + } | |
| 222 | 238 | break; | 
| 223 | 239 | |
| 224 | 240 | case 'float': | 
| 225 | - if (!is_numeric($replacement)) | |
| 226 | -				smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 241 | +			if (!is_numeric($replacement)) { | |
| 242 | +							smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 243 | + } | |
| 227 | 244 | return (string) (float) $replacement; | 
| 228 | 245 | break; | 
| 229 | 246 | |
| @@ -236,31 +253,36 @@ discard block | ||
| 236 | 253 | break; | 
| 237 | 254 | |
| 238 | 255 | case 'inet': | 
| 239 | - if ($replacement == 'null' || $replacement == '') | |
| 240 | - return 'null'; | |
| 241 | - if (inet_pton($replacement) === false) | |
| 242 | -				smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 256 | +			if ($replacement == 'null' || $replacement == '') { | |
| 257 | + return 'null'; | |
| 258 | + } | |
| 259 | +			if (inet_pton($replacement) === false) { | |
| 260 | +							smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 261 | + } | |
| 243 | 262 |  			return sprintf('\'%1$s\'::inet', pg_escape_string($replacement)); | 
| 244 | 263 | |
| 245 | 264 | case 'array_inet': | 
| 246 | 265 | if (is_array($replacement)) | 
| 247 | 266 |  			{ | 
| 248 | - if (empty($replacement)) | |
| 249 | -					smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 267 | +				if (empty($replacement)) { | |
| 268 | +									smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 269 | + } | |
| 250 | 270 | |
| 251 | 271 | foreach ($replacement as $key => $value) | 
| 252 | 272 |  				{ | 
| 253 | - if ($replacement == 'null' || $replacement == '') | |
| 254 | - $replacement[$key] = 'null'; | |
| 255 | - if (!isValidIP($value)) | |
| 256 | -						smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 273 | +					if ($replacement == 'null' || $replacement == '') { | |
| 274 | + $replacement[$key] = 'null'; | |
| 275 | + } | |
| 276 | +					if (!isValidIP($value)) { | |
| 277 | +											smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 278 | + } | |
| 257 | 279 |  					$replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value)); | 
| 258 | 280 | } | 
| 259 | 281 | |
| 260 | 282 |  				return implode(', ', $replacement); | 
| 283 | +			} else { | |
| 284 | +							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 | 285 | } | 
| 262 | - else | |
| 263 | -				smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); | |
| 264 | 286 | break; | 
| 265 | 287 | |
| 266 | 288 | default: | 
| @@ -354,14 +376,16 @@ discard block | ||
| 354 | 376 | ), | 
| 355 | 377 | ); | 
| 356 | 378 | |
| 357 | - if (isset($replacements[$identifier])) | |
| 358 | - $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); | |
| 379 | +	if (isset($replacements[$identifier])) { | |
| 380 | + $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); | |
| 381 | + } | |
| 359 | 382 | |
| 360 | 383 | // Limits need to be a little different. | 
| 361 | 384 |  	$db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string); | 
| 362 | 385 | |
| 363 | - if (trim($db_string) == '') | |
| 364 | - return false; | |
| 386 | +	if (trim($db_string) == '') { | |
| 387 | + return false; | |
| 388 | + } | |
| 365 | 389 | |
| 366 | 390 | // Comments that are allowed in a query are preg_removed. | 
| 367 | 391 | static $allowed_comments_from = array( | 
| @@ -381,8 +405,9 @@ discard block | ||
| 381 | 405 | $db_count = !isset($db_count) ? 1 : $db_count + 1; | 
| 382 | 406 | $db_replace_result = 0; | 
| 383 | 407 | |
| 384 | - if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) | |
| 385 | -		smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); | |
| 408 | +	if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) { | |
| 409 | +			smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); | |
| 410 | + } | |
| 386 | 411 | |
| 387 | 412 |  	if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) | 
| 388 | 413 |  	{ | 
| @@ -403,8 +428,9 @@ discard block | ||
| 403 | 428 |  		list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); | 
| 404 | 429 | |
| 405 | 430 | // Initialize $db_cache if not already initialized. | 
| 406 | - if (!isset($db_cache)) | |
| 407 | - $db_cache = array(); | |
| 431 | +		if (!isset($db_cache)) { | |
| 432 | + $db_cache = array(); | |
| 433 | + } | |
| 408 | 434 | |
| 409 | 435 | if (!empty($_SESSION['debug_redirect'])) | 
| 410 | 436 |  		{ | 
| @@ -430,17 +456,18 @@ discard block | ||
| 430 | 456 | while (true) | 
| 431 | 457 |  		{ | 
| 432 | 458 | $pos = strpos($db_string, '\'', $pos + 1); | 
| 433 | - if ($pos === false) | |
| 434 | - break; | |
| 459 | +			if ($pos === false) { | |
| 460 | + break; | |
| 461 | + } | |
| 435 | 462 | $clean .= substr($db_string, $old_pos, $pos - $old_pos); | 
| 436 | 463 | |
| 437 | 464 | while (true) | 
| 438 | 465 |  			{ | 
| 439 | 466 | $pos1 = strpos($db_string, '\'', $pos + 1); | 
| 440 | 467 | $pos2 = strpos($db_string, '\\', $pos + 1); | 
| 441 | - if ($pos1 === false) | |
| 442 | - break; | |
| 443 | - elseif ($pos2 === false || $pos2 > $pos1) | |
| 468 | +				if ($pos1 === false) { | |
| 469 | + break; | |
| 470 | + } elseif ($pos2 === false || $pos2 > $pos1) | |
| 444 | 471 |  				{ | 
| 445 | 472 | $pos = $pos1; | 
| 446 | 473 | break; | 
| @@ -456,16 +483,19 @@ discard block | ||
| 456 | 483 | $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); | 
| 457 | 484 | |
| 458 | 485 | // Comments? We don't use comments in our queries, we leave 'em outside! | 
| 459 | - if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) | |
| 460 | - $fail = true; | |
| 486 | +		if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) { | |
| 487 | + $fail = true; | |
| 488 | + } | |
| 461 | 489 | // Trying to change passwords, slow us down, or something? | 
| 462 | -		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) | |
| 463 | - $fail = true; | |
| 464 | -		elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) | |
| 465 | - $fail = true; | |
| 490 | +		elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) { | |
| 491 | + $fail = true; | |
| 492 | +		} elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { | |
| 493 | + $fail = true; | |
| 494 | + } | |
| 466 | 495 | |
| 467 | -		if (!empty($fail) && function_exists('log_error')) | |
| 468 | -			smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); | |
| 496 | +		if (!empty($fail) && function_exists('log_error')) { | |
| 497 | +					smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); | |
| 498 | + } | |
| 469 | 499 | } | 
| 470 | 500 | |
| 471 | 501 | // Set optimize stuff | 
| @@ -488,12 +518,14 @@ discard block | ||
| 488 | 518 | |
| 489 | 519 | $db_last_result = @pg_query($connection, $db_string); | 
| 490 | 520 | |
| 491 | - if ($db_last_result === false && empty($db_values['db_error_skip'])) | |
| 492 | - $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 | + } | |
| 493 | 524 | |
| 494 | 525 | // Debugging. | 
| 495 | - if (isset($db_show_debug) && $db_show_debug === true) | |
| 496 | -		$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 | + } | |
| 497 | 529 | |
| 498 | 530 | return $db_last_result; | 
| 499 | 531 | } | 
| @@ -506,10 +538,11 @@ discard block | ||
| 506 | 538 |  { | 
| 507 | 539 | global $db_last_result, $db_replace_result; | 
| 508 | 540 | |
| 509 | - if ($db_replace_result) | |
| 510 | - return $db_replace_result; | |
| 511 | - elseif ($result === null && !$db_last_result) | |
| 512 | - return 0; | |
| 541 | +	if ($db_replace_result) { | |
| 542 | + return $db_replace_result; | |
| 543 | +	} elseif ($result === null && !$db_last_result) { | |
| 544 | + return 0; | |
| 545 | + } | |
| 513 | 546 | |
| 514 | 547 | return pg_affected_rows($result === null ? $db_last_result : $result); | 
| 515 | 548 | } | 
| @@ -533,8 +566,9 @@ discard block | ||
| 533 | 566 | array( | 
| 534 | 567 | ) | 
| 535 | 568 | ); | 
| 536 | - if (!$request) | |
| 537 | - return false; | |
| 569 | +	if (!$request) { | |
| 570 | + return false; | |
| 571 | + } | |
| 538 | 572 | list ($lastID) = $smcFunc['db_fetch_row']($request); | 
| 539 | 573 | $smcFunc['db_free_result']($request); | 
| 540 | 574 | |
| @@ -555,12 +589,13 @@ discard block | ||
| 555 | 589 | // Decide which connection to use | 
| 556 | 590 | $connection = $connection === null ? $db_connection : $connection; | 
| 557 | 591 | |
| 558 | - if ($type == 'begin') | |
| 559 | - return @pg_query($connection, 'BEGIN'); | |
| 560 | - elseif ($type == 'rollback') | |
| 561 | - return @pg_query($connection, 'ROLLBACK'); | |
| 562 | - elseif ($type == 'commit') | |
| 563 | - 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 | + } | |
| 564 | 599 | |
| 565 | 600 | return false; | 
| 566 | 601 | } | 
| @@ -588,19 +623,22 @@ discard block | ||
| 588 | 623 | $query_error = @pg_last_error($connection); | 
| 589 | 624 | |
| 590 | 625 | // Log the error. | 
| 591 | -	if (function_exists('log_error')) | |
| 592 | - 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 | + } | |
| 593 | 629 | |
| 594 | 630 | // Nothing's defined yet... just die with it. | 
| 595 | - if (empty($context) || empty($txt)) | |
| 596 | - die($query_error); | |
| 631 | +	if (empty($context) || empty($txt)) { | |
| 632 | + die($query_error); | |
| 633 | + } | |
| 597 | 634 | |
| 598 | 635 | // Show an error message, if possible. | 
| 599 | 636 | $context['error_title'] = $txt['database_error']; | 
| 600 | -	if (allowedTo('admin_forum')) | |
| 601 | - $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; | |
| 602 | - else | |
| 603 | - $context['error_message'] = $txt['try_again']; | |
| 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 | + } | |
| 604 | 642 | |
| 605 | 643 |  	if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) | 
| 606 | 644 |  	{ | 
| @@ -622,12 +660,14 @@ discard block | ||
| 622 | 660 |  { | 
| 623 | 661 | global $db_row_count; | 
| 624 | 662 | |
| 625 | - if ($counter !== false) | |
| 626 | - return pg_fetch_row($request, $counter); | |
| 663 | +	if ($counter !== false) { | |
| 664 | + return pg_fetch_row($request, $counter); | |
| 665 | + } | |
| 627 | 666 | |
| 628 | 667 | // Reset the row counter... | 
| 629 | - if (!isset($db_row_count[(int) $request])) | |
| 630 | - $db_row_count[(int) $request] = 0; | |
| 668 | +	if (!isset($db_row_count[(int) $request])) { | |
| 669 | + $db_row_count[(int) $request] = 0; | |
| 670 | + } | |
| 631 | 671 | |
| 632 | 672 | // Return the right row. | 
| 633 | 673 | return @pg_fetch_row($request, $db_row_count[(int) $request]++); | 
| @@ -644,12 +684,14 @@ discard block | ||
| 644 | 684 |  { | 
| 645 | 685 | global $db_row_count; | 
| 646 | 686 | |
| 647 | - if ($counter !== false) | |
| 648 | - return pg_fetch_assoc($request, $counter); | |
| 687 | +	if ($counter !== false) { | |
| 688 | + return pg_fetch_assoc($request, $counter); | |
| 689 | + } | |
| 649 | 690 | |
| 650 | 691 | // Reset the row counter... | 
| 651 | - if (!isset($db_row_count[(int) $request])) | |
| 652 | - $db_row_count[(int) $request] = 0; | |
| 692 | +	if (!isset($db_row_count[(int) $request])) { | |
| 693 | + $db_row_count[(int) $request] = 0; | |
| 694 | + } | |
| 653 | 695 | |
| 654 | 696 | // Return the right row. | 
| 655 | 697 | return @pg_fetch_assoc($request, $db_row_count[(int) $request]++); | 
| @@ -702,11 +744,13 @@ discard block | ||
| 702 | 744 | |
| 703 | 745 | $replace = ''; | 
| 704 | 746 | |
| 705 | - if (empty($data)) | |
| 706 | - return; | |
| 747 | +	if (empty($data)) { | |
| 748 | + return; | |
| 749 | + } | |
| 707 | 750 | |
| 708 | - if (!is_array($data[array_rand($data)])) | |
| 709 | - $data = array($data); | |
| 751 | +	if (!is_array($data[array_rand($data)])) { | |
| 752 | + $data = array($data); | |
| 753 | + } | |
| 710 | 754 | |
| 711 | 755 | // Replace the prefix holder with the actual prefix. | 
| 712 | 756 |  	$table = str_replace('{db_prefix}', $db_prefix, $table); | 
| @@ -725,11 +769,13 @@ discard block | ||
| 725 | 769 | //pg 9.5 got replace support | 
| 726 | 770 | $pg_version = $smcFunc['db_get_version'](); | 
| 727 | 771 | // if we got a Beta Version | 
| 728 | - if (stripos($pg_version, 'beta') !== false) | |
| 729 | - $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 | + } | |
| 730 | 775 | // or RC | 
| 731 | - if (stripos($pg_version, 'rc') !== false) | |
| 732 | - $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 | + } | |
| 733 | 779 | |
| 734 | 780 | $replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); | 
| 735 | 781 | } | 
| @@ -748,8 +794,7 @@ discard block | ||
| 748 | 794 | $key_str .= ($count_pk > 0 ? ',' : ''); | 
| 749 | 795 | $key_str .= $columnName; | 
| 750 | 796 | $count_pk++; | 
| 751 | - } | |
| 752 | - else //normal field | |
| 797 | + } else //normal field | |
| 753 | 798 |  				{ | 
| 754 | 799 | $col_str .= ($count > 0 ? ',' : ''); | 
| 755 | 800 | $col_str .= $columnName . ' = EXCLUDED.' . $columnName; | 
| @@ -757,20 +802,21 @@ discard block | ||
| 757 | 802 | } | 
| 758 | 803 | } | 
| 759 | 804 |  			$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; | 
| 760 | - } | |
| 761 | - else | |
| 805 | + } else | |
| 762 | 806 |  		{ | 
| 763 | 807 | foreach ($columns as $columnName => $type) | 
| 764 | 808 |  			{ | 
| 765 | 809 | // Are we restricting the length? | 
| 766 | - if (strpos($type, 'string-') !== false) | |
| 767 | -					$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); | |
| 768 | - else | |
| 769 | -					$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 | + } | |
| 770 | 815 | |
| 771 | 816 | // A key? That's what we were looking for. | 
| 772 | - if (in_array($columnName, $keys)) | |
| 773 | - $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); | |
| 817 | +				if (in_array($columnName, $keys)) { | |
| 818 | + $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); | |
| 819 | + } | |
| 774 | 820 | $count++; | 
| 775 | 821 | } | 
| 776 | 822 | |
| @@ -806,10 +852,11 @@ discard block | ||
| 806 | 852 | foreach ($columns as $columnName => $type) | 
| 807 | 853 |  		{ | 
| 808 | 854 | // Are we restricting the length? | 
| 809 | - if (strpos($type, 'string-') !== false) | |
| 810 | -				$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); | |
| 811 | - else | |
| 812 | -				$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 | + } | |
| 813 | 860 | } | 
| 814 | 861 | $insertData = substr($insertData, 0, -2) . ')'; | 
| 815 | 862 | |
| @@ -818,8 +865,9 @@ discard block | ||
| 818 | 865 | |
| 819 | 866 | // Here's where the variables are injected to the query. | 
| 820 | 867 | $insertRows = array(); | 
| 821 | - foreach ($data as $dataRow) | |
| 822 | - $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 | + } | |
| 823 | 871 | |
| 824 | 872 | // Do the insert. | 
| 825 | 873 |  		$request = $smcFunc['db_query']('', ' | 
| @@ -836,19 +884,21 @@ discard block | ||
| 836 | 884 | |
| 837 | 885 | if ($with_returning && $request !== false) | 
| 838 | 886 |  		{ | 
| 839 | - if ($returnmode === 2) | |
| 840 | - $return_var = array(); | |
| 887 | +			if ($returnmode === 2) { | |
| 888 | + $return_var = array(); | |
| 889 | + } | |
| 841 | 890 | |
| 842 | 891 | while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) | 
| 843 | 892 |  			{ | 
| 844 | - if (is_numeric($row[0])) // try to emulate mysql limitation | |
| 893 | +				if (is_numeric($row[0])) { | |
| 894 | + // try to emulate mysql limitation | |
| 845 | 895 |  				{ | 
| 846 | 896 | if ($returnmode === 1) | 
| 847 | 897 | $return_var = $row[0]; | 
| 848 | - elseif ($returnmode === 2) | |
| 849 | - $return_var[] = $row[0]; | |
| 850 | - } | |
| 851 | - else | |
| 898 | +				} elseif ($returnmode === 2) { | |
| 899 | + $return_var[] = $row[0]; | |
| 900 | + } | |
| 901 | + } else | |
| 852 | 902 |  				{ | 
| 853 | 903 | $with_returning = false; | 
| 854 | 904 |  					trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); | 
| @@ -857,9 +907,10 @@ discard block | ||
| 857 | 907 | } | 
| 858 | 908 | } | 
| 859 | 909 | |
| 860 | - if ($with_returning && !empty($return_var)) | |
| 861 | - return $return_var; | |
| 862 | -} | |
| 910 | +	if ($with_returning && !empty($return_var)) { | |
| 911 | + return $return_var; | |
| 912 | + } | |
| 913 | + } | |
| 863 | 914 | |
| 864 | 915 | /** | 
| 865 | 916 | * Dummy function really. Doesn't do anything on PostgreSQL. | 
| @@ -896,8 +947,9 @@ discard block | ||
| 896 | 947 | */ | 
| 897 | 948 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) | 
| 898 | 949 |  { | 
| 899 | - if (empty($log_message)) | |
| 900 | - $log_message = $error_message; | |
| 950 | +	if (empty($log_message)) { | |
| 951 | + $log_message = $error_message; | |
| 952 | + } | |
| 901 | 953 | |
| 902 | 954 | foreach (debug_backtrace() as $step) | 
| 903 | 955 |  	{ | 
| @@ -916,12 +968,14 @@ discard block | ||
| 916 | 968 | } | 
| 917 | 969 | |
| 918 | 970 | // A special case - we want the file and line numbers for debugging. | 
| 919 | - if ($error_type == 'return') | |
| 920 | - return array($file, $line); | |
| 971 | +	if ($error_type == 'return') { | |
| 972 | + return array($file, $line); | |
| 973 | + } | |
| 921 | 974 | |
| 922 | 975 | // Is always a critical error. | 
| 923 | -	if (function_exists('log_error')) | |
| 924 | - log_error($log_message, 'critical', $file, $line); | |
| 976 | +	if (function_exists('log_error')) { | |
| 977 | + log_error($log_message, 'critical', $file, $line); | |
| 978 | + } | |
| 925 | 979 | |
| 926 | 980 |  	if (function_exists('fatal_error')) | 
| 927 | 981 |  	{ | 
| @@ -929,12 +983,12 @@ discard block | ||
| 929 | 983 | |
| 930 | 984 | // Cannot continue... | 
| 931 | 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 | + } | |
| 932 | 991 | } | 
| 933 | - elseif ($error_type) | |
| 934 | -		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); | |
| 935 | - else | |
| 936 | -		trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); | |
| 937 | -} | |
| 938 | 992 | |
| 939 | 993 | /** | 
| 940 | 994 | * Escape the LIKE wildcards so that they match the character and not the wildcard. | 
| @@ -951,10 +1005,11 @@ discard block | ||
| 951 | 1005 | '\\' => '\\\\', | 
| 952 | 1006 | ); | 
| 953 | 1007 | |
| 954 | - if ($translate_human_wildcards) | |
| 955 | - $replacements += array( | |
| 1008 | +	if ($translate_human_wildcards) { | |
| 1009 | + $replacements += array( | |
| 956 | 1010 | '*' => '%', | 
| 957 | 1011 | ); | 
| 1012 | + } | |
| 958 | 1013 | |
| 959 | 1014 | return strtr($string, $replacements); | 
| 960 | 1015 | } |