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