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