@@ -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', |
@@ -63,9 +64,11 @@ discard block |
||
| 63 | 64 | 'db_error_insert' => 'smf_db_error_insert', |
| 64 | 65 | 'db_custom_order' => 'smf_db_custom_order', |
| 65 | 66 | ); |
| 67 | + } |
|
| 66 | 68 | |
| 67 | - if (!empty($db_options['persist'])) |
|
| 68 | - $db_server = 'p:' . $db_server; |
|
| 69 | + if (!empty($db_options['persist'])) { |
|
| 70 | + $db_server = 'p:' . $db_server; |
|
| 71 | + } |
|
| 69 | 72 | |
| 70 | 73 | $connection = mysqli_init(); |
| 71 | 74 | |
@@ -74,24 +77,27 @@ discard block |
||
| 74 | 77 | $success = false; |
| 75 | 78 | |
| 76 | 79 | if ($connection) { |
| 77 | - if (!empty($db_options['port'])) |
|
| 78 | - $success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags); |
|
| 79 | - else |
|
| 80 | - $success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags); |
|
| 80 | + if (!empty($db_options['port'])) { |
|
| 81 | + $success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, $db_options['port'], null, $flags); |
|
| 82 | + } else { |
|
| 83 | + $success = mysqli_real_connect($connection, $db_server, $db_user, $db_passwd, null, 0, null, $flags); |
|
| 84 | + } |
|
| 81 | 85 | } |
| 82 | 86 | |
| 83 | 87 | // Something's wrong, show an error if its fatal (which we assume it is) |
| 84 | 88 | if ($success === false) |
| 85 | 89 | { |
| 86 | - if (!empty($db_options['non_fatal'])) |
|
| 87 | - return null; |
|
| 88 | - else |
|
| 89 | - display_db_error(); |
|
| 90 | + if (!empty($db_options['non_fatal'])) { |
|
| 91 | + return null; |
|
| 92 | + } else { |
|
| 93 | + display_db_error(); |
|
| 94 | + } |
|
| 90 | 95 | } |
| 91 | 96 | |
| 92 | 97 | // Select the database, unless told not to |
| 93 | - if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal'])) |
|
| 94 | - display_db_error(); |
|
| 98 | + if (empty($db_options['dont_select_db']) && !@mysqli_select_db($connection, $db_name) && empty($db_options['non_fatal'])) { |
|
| 99 | + display_db_error(); |
|
| 100 | + } |
|
| 95 | 101 | |
| 96 | 102 | mysqli_query($connection, 'SET SESSION sql_mode = \'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\''); |
| 97 | 103 | |
@@ -164,34 +170,42 @@ discard block |
||
| 164 | 170 | global $db_callback, $user_info, $db_prefix, $smcFunc; |
| 165 | 171 | |
| 166 | 172 | list ($values, $connection) = $db_callback; |
| 167 | - if (!is_object($connection)) |
|
| 168 | - display_db_error(); |
|
| 173 | + if (!is_object($connection)) { |
|
| 174 | + display_db_error(); |
|
| 175 | + } |
|
| 169 | 176 | |
| 170 | - if ($matches[1] === 'db_prefix') |
|
| 171 | - return $db_prefix; |
|
| 177 | + if ($matches[1] === 'db_prefix') { |
|
| 178 | + return $db_prefix; |
|
| 179 | + } |
|
| 172 | 180 | |
| 173 | - if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) |
|
| 174 | - return $user_info[$matches[1]]; |
|
| 181 | + if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) { |
|
| 182 | + return $user_info[$matches[1]]; |
|
| 183 | + } |
|
| 175 | 184 | |
| 176 | - if ($matches[1] === 'empty') |
|
| 177 | - return '\'\''; |
|
| 185 | + if ($matches[1] === 'empty') { |
|
| 186 | + return '\'\''; |
|
| 187 | + } |
|
| 178 | 188 | |
| 179 | - if (!isset($matches[2])) |
|
| 180 | - smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 189 | + if (!isset($matches[2])) { |
|
| 190 | + smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 191 | + } |
|
| 181 | 192 | |
| 182 | - if ($matches[1] === 'literal') |
|
| 183 | - return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\''; |
|
| 193 | + if ($matches[1] === 'literal') { |
|
| 194 | + return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\''; |
|
| 195 | + } |
|
| 184 | 196 | |
| 185 | - if (!isset($values[$matches[2]])) |
|
| 186 | - smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 197 | + if (!isset($values[$matches[2]])) { |
|
| 198 | + smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 199 | + } |
|
| 187 | 200 | |
| 188 | 201 | $replacement = $values[$matches[2]]; |
| 189 | 202 | |
| 190 | 203 | switch ($matches[1]) |
| 191 | 204 | { |
| 192 | 205 | case 'int': |
| 193 | - if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 194 | - smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 206 | + if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) { |
|
| 207 | + smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 208 | + } |
|
| 195 | 209 | return (string) (int) $replacement; |
| 196 | 210 | break; |
| 197 | 211 | |
@@ -203,65 +217,73 @@ discard block |
||
| 203 | 217 | case 'array_int': |
| 204 | 218 | if (is_array($replacement)) |
| 205 | 219 | { |
| 206 | - if (empty($replacement)) |
|
| 207 | - smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 220 | + if (empty($replacement)) { |
|
| 221 | + smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 222 | + } |
|
| 208 | 223 | |
| 209 | 224 | foreach ($replacement as $key => $value) |
| 210 | 225 | { |
| 211 | - if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 212 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 226 | + if (!is_numeric($value) || (string) $value !== (string) (int) $value) { |
|
| 227 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 228 | + } |
|
| 213 | 229 | |
| 214 | 230 | $replacement[$key] = (string) (int) $value; |
| 215 | 231 | } |
| 216 | 232 | |
| 217 | 233 | return implode(', ', $replacement); |
| 234 | + } else { |
|
| 235 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 218 | 236 | } |
| 219 | - else |
|
| 220 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 221 | 237 | |
| 222 | 238 | break; |
| 223 | 239 | |
| 224 | 240 | case 'array_string': |
| 225 | 241 | if (is_array($replacement)) |
| 226 | 242 | { |
| 227 | - if (empty($replacement)) |
|
| 228 | - smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 243 | + if (empty($replacement)) { |
|
| 244 | + smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 245 | + } |
|
| 229 | 246 | |
| 230 | - foreach ($replacement as $key => $value) |
|
| 231 | - $replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value)); |
|
| 247 | + foreach ($replacement as $key => $value) { |
|
| 248 | + $replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value)); |
|
| 249 | + } |
|
| 232 | 250 | |
| 233 | 251 | return implode(', ', $replacement); |
| 252 | + } else { |
|
| 253 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 234 | 254 | } |
| 235 | - else |
|
| 236 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 237 | 255 | break; |
| 238 | 256 | |
| 239 | 257 | case 'date': |
| 240 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
|
| 241 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]); |
|
| 242 | - else |
|
| 243 | - smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 258 | + if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) { |
|
| 259 | + return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]); |
|
| 260 | + } else { |
|
| 261 | + smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 262 | + } |
|
| 244 | 263 | break; |
| 245 | 264 | |
| 246 | 265 | case 'time': |
| 247 | - if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 248 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]); |
|
| 249 | - else |
|
| 250 | - smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 266 | + if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) { |
|
| 267 | + return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]); |
|
| 268 | + } else { |
|
| 269 | + smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 270 | + } |
|
| 251 | 271 | break; |
| 252 | 272 | |
| 253 | 273 | case 'datetime': |
| 254 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) |
|
| 255 | - return 'str_to_date('. |
|
| 274 | + if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) { |
|
| 275 | + return 'str_to_date('. |
|
| 256 | 276 | sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5] ,$datetime_matches[6]). |
| 257 | 277 | ',\'%Y-%m-%d %h:%i:%s\')'; |
| 258 | - else |
|
| 259 | - smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 278 | + } else { |
|
| 279 | + smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 280 | + } |
|
| 260 | 281 | break; |
| 261 | 282 | |
| 262 | 283 | case 'float': |
| 263 | - if (!is_numeric($replacement)) |
|
| 264 | - smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 284 | + if (!is_numeric($replacement)) { |
|
| 285 | + smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 286 | + } |
|
| 265 | 287 | return (string) (float) $replacement; |
| 266 | 288 | break; |
| 267 | 289 | |
@@ -275,32 +297,37 @@ discard block |
||
| 275 | 297 | break; |
| 276 | 298 | |
| 277 | 299 | case 'inet': |
| 278 | - if ($replacement == 'null' || $replacement == '') |
|
| 279 | - return 'null'; |
|
| 280 | - if (!isValidIP($replacement)) |
|
| 281 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 300 | + if ($replacement == 'null' || $replacement == '') { |
|
| 301 | + return 'null'; |
|
| 302 | + } |
|
| 303 | + if (!isValidIP($replacement)) { |
|
| 304 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 305 | + } |
|
| 282 | 306 | //we don't use the native support of mysql > 5.6.2 |
| 283 | 307 | return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement))); |
| 284 | 308 | |
| 285 | 309 | case 'array_inet': |
| 286 | 310 | if (is_array($replacement)) |
| 287 | 311 | { |
| 288 | - if (empty($replacement)) |
|
| 289 | - smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 312 | + if (empty($replacement)) { |
|
| 313 | + smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 314 | + } |
|
| 290 | 315 | |
| 291 | 316 | foreach ($replacement as $key => $value) |
| 292 | 317 | { |
| 293 | - if ($replacement == 'null' || $replacement == '') |
|
| 294 | - $replacement[$key] = 'null'; |
|
| 295 | - if (!isValidIP($value)) |
|
| 296 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 318 | + if ($replacement == 'null' || $replacement == '') { |
|
| 319 | + $replacement[$key] = 'null'; |
|
| 320 | + } |
|
| 321 | + if (!isValidIP($value)) { |
|
| 322 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 323 | + } |
|
| 297 | 324 | $replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value))); |
| 298 | 325 | } |
| 299 | 326 | |
| 300 | 327 | return implode(', ', $replacement); |
| 328 | + } else { |
|
| 329 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 301 | 330 | } |
| 302 | - else |
|
| 303 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 304 | 331 | break; |
| 305 | 332 | |
| 306 | 333 | default: |
@@ -371,18 +398,20 @@ discard block |
||
| 371 | 398 | // One more query.... |
| 372 | 399 | $db_count = !isset($db_count) ? 1 : $db_count + 1; |
| 373 | 400 | |
| 374 | - if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) |
|
| 375 | - smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 401 | + if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) { |
|
| 402 | + smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 403 | + } |
|
| 376 | 404 | |
| 377 | 405 | // Use "ORDER BY null" to prevent Mysql doing filesorts for Group By clauses without an Order By |
| 378 | 406 | if (strpos($db_string, 'GROUP BY') !== false && strpos($db_string, 'ORDER BY') === false && preg_match('~^\s+SELECT~i', $db_string)) |
| 379 | 407 | { |
| 380 | 408 | // Add before LIMIT |
| 381 | - if ($pos = strpos($db_string, 'LIMIT ')) |
|
| 382 | - $db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string)); |
|
| 383 | - else |
|
| 384 | - // Append it. |
|
| 409 | + if ($pos = strpos($db_string, 'LIMIT ')) { |
|
| 410 | + $db_string = substr($db_string, 0, $pos) . "\t\t\tORDER BY null\n" . substr($db_string, $pos, strlen($db_string)); |
|
| 411 | + } else { |
|
| 412 | + // Append it. |
|
| 385 | 413 | $db_string .= "\n\t\t\tORDER BY null"; |
| 414 | + } |
|
| 386 | 415 | } |
| 387 | 416 | |
| 388 | 417 | if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
@@ -408,17 +437,18 @@ discard block |
||
| 408 | 437 | while (true) |
| 409 | 438 | { |
| 410 | 439 | $pos = strpos($db_string_1, '\'', $pos + 1); |
| 411 | - if ($pos === false) |
|
| 412 | - break; |
|
| 440 | + if ($pos === false) { |
|
| 441 | + break; |
|
| 442 | + } |
|
| 413 | 443 | $clean .= substr($db_string_1, $old_pos, $pos - $old_pos); |
| 414 | 444 | |
| 415 | 445 | while (true) |
| 416 | 446 | { |
| 417 | 447 | $pos1 = strpos($db_string_1, '\'', $pos + 1); |
| 418 | 448 | $pos2 = strpos($db_string_1, '\\', $pos + 1); |
| 419 | - if ($pos1 === false) |
|
| 420 | - break; |
|
| 421 | - elseif ($pos2 === false || $pos2 > $pos1) |
|
| 449 | + if ($pos1 === false) { |
|
| 450 | + break; |
|
| 451 | + } elseif ($pos2 === false || $pos2 > $pos1) |
|
| 422 | 452 | { |
| 423 | 453 | $pos = $pos1; |
| 424 | 454 | break; |
@@ -434,16 +464,19 @@ discard block |
||
| 434 | 464 | $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); |
| 435 | 465 | |
| 436 | 466 | // Comments? We don't use comments in our queries, we leave 'em outside! |
| 437 | - if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
|
| 438 | - $fail = true; |
|
| 467 | + if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) { |
|
| 468 | + $fail = true; |
|
| 469 | + } |
|
| 439 | 470 | // Trying to change passwords, slow us down, or something? |
| 440 | - elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) |
|
| 441 | - $fail = true; |
|
| 442 | - elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) |
|
| 443 | - $fail = true; |
|
| 471 | + elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) { |
|
| 472 | + $fail = true; |
|
| 473 | + } elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { |
|
| 474 | + $fail = true; |
|
| 475 | + } |
|
| 444 | 476 | |
| 445 | - if (!empty($fail) && function_exists('log_error')) |
|
| 446 | - smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 477 | + if (!empty($fail) && function_exists('log_error')) { |
|
| 478 | + smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 479 | + } |
|
| 447 | 480 | } |
| 448 | 481 | |
| 449 | 482 | // Debugging. |
@@ -453,8 +486,9 @@ discard block |
||
| 453 | 486 | list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
| 454 | 487 | |
| 455 | 488 | // Initialize $db_cache if not already initialized. |
| 456 | - if (!isset($db_cache)) |
|
| 457 | - $db_cache = array(); |
|
| 489 | + if (!isset($db_cache)) { |
|
| 490 | + $db_cache = array(); |
|
| 491 | + } |
|
| 458 | 492 | |
| 459 | 493 | if (!empty($_SESSION['debug_redirect'])) |
| 460 | 494 | { |
@@ -470,17 +504,20 @@ discard block |
||
| 470 | 504 | $db_cache[$db_count]['s'] = ($st = microtime(true)) - $time_start; |
| 471 | 505 | } |
| 472 | 506 | |
| 473 | - if (empty($db_unbuffered)) |
|
| 474 | - $ret = @mysqli_query($connection, $db_string); |
|
| 475 | - else |
|
| 476 | - $ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT); |
|
| 507 | + if (empty($db_unbuffered)) { |
|
| 508 | + $ret = @mysqli_query($connection, $db_string); |
|
| 509 | + } else { |
|
| 510 | + $ret = @mysqli_query($connection, $db_string, MYSQLI_USE_RESULT); |
|
| 511 | + } |
|
| 477 | 512 | |
| 478 | - if ($ret === false && empty($db_values['db_error_skip'])) |
|
| 479 | - $ret = smf_db_error($db_string, $connection); |
|
| 513 | + if ($ret === false && empty($db_values['db_error_skip'])) { |
|
| 514 | + $ret = smf_db_error($db_string, $connection); |
|
| 515 | + } |
|
| 480 | 516 | |
| 481 | 517 | // Debugging. |
| 482 | - if (isset($db_show_debug) && $db_show_debug === true) |
|
| 483 | - $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 518 | + if (isset($db_show_debug) && $db_show_debug === true) { |
|
| 519 | + $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 520 | + } |
|
| 484 | 521 | |
| 485 | 522 | return $ret; |
| 486 | 523 | } |
@@ -527,12 +564,13 @@ discard block |
||
| 527 | 564 | // Decide which connection to use |
| 528 | 565 | $connection = $connection === null ? $db_connection : $connection; |
| 529 | 566 | |
| 530 | - if ($type == 'begin') |
|
| 531 | - return @mysqli_query($connection, 'BEGIN'); |
|
| 532 | - elseif ($type == 'rollback') |
|
| 533 | - return @mysqli_query($connection, 'ROLLBACK'); |
|
| 534 | - elseif ($type == 'commit') |
|
| 535 | - return @mysqli_query($connection, 'COMMIT'); |
|
| 567 | + if ($type == 'begin') { |
|
| 568 | + return @mysqli_query($connection, 'BEGIN'); |
|
| 569 | + } elseif ($type == 'rollback') { |
|
| 570 | + return @mysqli_query($connection, 'ROLLBACK'); |
|
| 571 | + } elseif ($type == 'commit') { |
|
| 572 | + return @mysqli_query($connection, 'COMMIT'); |
|
| 573 | + } |
|
| 536 | 574 | |
| 537 | 575 | return false; |
| 538 | 576 | } |
@@ -570,8 +608,9 @@ discard block |
||
| 570 | 608 | // 1213: Deadlock found. |
| 571 | 609 | |
| 572 | 610 | // Log the error. |
| 573 | - if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) |
|
| 574 | - log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line); |
|
| 611 | + if ($query_errno != 1213 && $query_errno != 1205 && function_exists('log_error')) { |
|
| 612 | + log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n$db_string" : ''), 'database', $file, $line); |
|
| 613 | + } |
|
| 575 | 614 | |
| 576 | 615 | // Database error auto fixing ;). |
| 577 | 616 | if (function_exists('cache_get_data') && (!isset($modSettings['autoFixDatabase']) || $modSettings['autoFixDatabase'] == '1')) |
@@ -580,8 +619,9 @@ discard block |
||
| 580 | 619 | $old_cache = @$modSettings['cache_enable']; |
| 581 | 620 | $modSettings['cache_enable'] = '1'; |
| 582 | 621 | |
| 583 | - if (($temp = cache_get_data('db_last_error', 600)) !== null) |
|
| 584 | - $db_last_error = max(@$db_last_error, $temp); |
|
| 622 | + if (($temp = cache_get_data('db_last_error', 600)) !== null) { |
|
| 623 | + $db_last_error = max(@$db_last_error, $temp); |
|
| 624 | + } |
|
| 585 | 625 | |
| 586 | 626 | if (@$db_last_error < time() - 3600 * 24 * 3) |
| 587 | 627 | { |
@@ -597,8 +637,9 @@ discard block |
||
| 597 | 637 | foreach ($tables as $table) |
| 598 | 638 | { |
| 599 | 639 | // Now, it's still theoretically possible this could be an injection. So backtick it! |
| 600 | - if (trim($table) != '') |
|
| 601 | - $fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`'; |
|
| 640 | + if (trim($table) != '') { |
|
| 641 | + $fix_tables[] = '`' . strtr(trim($table), array('`' => '')) . '`'; |
|
| 642 | + } |
|
| 602 | 643 | } |
| 603 | 644 | } |
| 604 | 645 | |
@@ -607,8 +648,9 @@ discard block |
||
| 607 | 648 | // Table crashed. Let's try to fix it. |
| 608 | 649 | elseif ($query_errno == 1016) |
| 609 | 650 | { |
| 610 | - if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) |
|
| 611 | - $fix_tables = array('`' . $match[1] . '`'); |
|
| 651 | + if (preg_match('~\'([^\.\']+)~', $query_error, $match) != 0) { |
|
| 652 | + $fix_tables = array('`' . $match[1] . '`'); |
|
| 653 | + } |
|
| 612 | 654 | } |
| 613 | 655 | // Indexes crashed. Should be easy to fix! |
| 614 | 656 | elseif ($query_errno == 1034 || $query_errno == 1035) |
@@ -627,13 +669,15 @@ discard block |
||
| 627 | 669 | |
| 628 | 670 | // Make a note of the REPAIR... |
| 629 | 671 | cache_put_data('db_last_error', time(), 600); |
| 630 | - if (($temp = cache_get_data('db_last_error', 600)) === null) |
|
| 631 | - updateSettingsFile(array('db_last_error' => time())); |
|
| 672 | + if (($temp = cache_get_data('db_last_error', 600)) === null) { |
|
| 673 | + updateSettingsFile(array('db_last_error' => time())); |
|
| 674 | + } |
|
| 632 | 675 | |
| 633 | 676 | // Attempt to find and repair the broken table. |
| 634 | - foreach ($fix_tables as $table) |
|
| 635 | - $smcFunc['db_query']('', " |
|
| 677 | + foreach ($fix_tables as $table) { |
|
| 678 | + $smcFunc['db_query']('', " |
|
| 636 | 679 | REPAIR TABLE $table", false, false); |
| 680 | + } |
|
| 637 | 681 | |
| 638 | 682 | // And send off an email! |
| 639 | 683 | sendmail($webmaster_email, $txt['database_error'], $txt['tried_to_repair'], null, 'dberror'); |
@@ -642,11 +686,12 @@ discard block |
||
| 642 | 686 | |
| 643 | 687 | // Try the query again...? |
| 644 | 688 | $ret = $smcFunc['db_query']('', $db_string, false, false); |
| 645 | - if ($ret !== false) |
|
| 646 | - return $ret; |
|
| 689 | + if ($ret !== false) { |
|
| 690 | + return $ret; |
|
| 691 | + } |
|
| 692 | + } else { |
|
| 693 | + $modSettings['cache_enable'] = $old_cache; |
|
| 647 | 694 | } |
| 648 | - else |
|
| 649 | - $modSettings['cache_enable'] = $old_cache; |
|
| 650 | 695 | |
| 651 | 696 | // Check for the "lost connection" or "deadlock found" errors - and try it just one more time. |
| 652 | 697 | if (in_array($query_errno, array(1205, 1213))) |
@@ -659,24 +704,27 @@ discard block |
||
| 659 | 704 | $ret = $smcFunc['db_query']('', $db_string, false, false); |
| 660 | 705 | |
| 661 | 706 | $new_errno = mysqli_errno($db_connection); |
| 662 | - if ($ret !== false || in_array($new_errno, array(1205, 1213))) |
|
| 663 | - break; |
|
| 707 | + if ($ret !== false || in_array($new_errno, array(1205, 1213))) { |
|
| 708 | + break; |
|
| 709 | + } |
|
| 664 | 710 | } |
| 665 | 711 | |
| 666 | 712 | // If it failed again, shucks to be you... we're not trying it over and over. |
| 667 | - if ($ret !== false) |
|
| 668 | - return $ret; |
|
| 713 | + if ($ret !== false) { |
|
| 714 | + return $ret; |
|
| 715 | + } |
|
| 669 | 716 | } |
| 670 | 717 | } |
| 671 | 718 | // Are they out of space, perhaps? |
| 672 | 719 | elseif ($query_errno == 1030 && (strpos($query_error, ' -1 ') !== false || strpos($query_error, ' 28 ') !== false || strpos($query_error, ' 12 ') !== false)) |
| 673 | 720 | { |
| 674 | - if (!isset($txt)) |
|
| 675 | - $query_error .= ' - check database storage space.'; |
|
| 676 | - else |
|
| 721 | + if (!isset($txt)) { |
|
| 722 | + $query_error .= ' - check database storage space.'; |
|
| 723 | + } else |
|
| 677 | 724 | { |
| 678 | - if (!isset($txt['mysql_error_space'])) |
|
| 679 | - loadLanguage('Errors'); |
|
| 725 | + if (!isset($txt['mysql_error_space'])) { |
|
| 726 | + loadLanguage('Errors'); |
|
| 727 | + } |
|
| 680 | 728 | |
| 681 | 729 | $query_error .= !isset($txt['mysql_error_space']) ? ' - check database storage space.' : $txt['mysql_error_space']; |
| 682 | 730 | } |
@@ -684,15 +732,17 @@ discard block |
||
| 684 | 732 | } |
| 685 | 733 | |
| 686 | 734 | // Nothing's defined yet... just die with it. |
| 687 | - if (empty($context) || empty($txt)) |
|
| 688 | - die($query_error); |
|
| 735 | + if (empty($context) || empty($txt)) { |
|
| 736 | + die($query_error); |
|
| 737 | + } |
|
| 689 | 738 | |
| 690 | 739 | // Show an error message, if possible. |
| 691 | 740 | $context['error_title'] = $txt['database_error']; |
| 692 | - if (allowedTo('admin_forum')) |
|
| 693 | - $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 694 | - else |
|
| 695 | - $context['error_message'] = $txt['try_again']; |
|
| 741 | + if (allowedTo('admin_forum')) { |
|
| 742 | + $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 743 | + } else { |
|
| 744 | + $context['error_message'] = $txt['try_again']; |
|
| 745 | + } |
|
| 696 | 746 | |
| 697 | 747 | if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) |
| 698 | 748 | { |
@@ -724,8 +774,9 @@ discard block |
||
| 724 | 774 | $return_var = null; |
| 725 | 775 | |
| 726 | 776 | // With nothing to insert, simply return. |
| 727 | - if (empty($data)) |
|
| 728 | - return; |
|
| 777 | + if (empty($data)) { |
|
| 778 | + return; |
|
| 779 | + } |
|
| 729 | 780 | |
| 730 | 781 | // Replace the prefix holder with the actual prefix. |
| 731 | 782 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
@@ -735,23 +786,26 @@ discard block |
||
| 735 | 786 | if (!empty($keys) && (count($keys) > 0) && $returnmode > 0) |
| 736 | 787 | { |
| 737 | 788 | $with_returning = true; |
| 738 | - if ($returnmode == 2) |
|
| 739 | - $return_var = array(); |
|
| 789 | + if ($returnmode == 2) { |
|
| 790 | + $return_var = array(); |
|
| 791 | + } |
|
| 740 | 792 | } |
| 741 | 793 | |
| 742 | 794 | // Inserting data as a single row can be done as a single array. |
| 743 | - if (!is_array($data[array_rand($data)])) |
|
| 744 | - $data = array($data); |
|
| 795 | + if (!is_array($data[array_rand($data)])) { |
|
| 796 | + $data = array($data); |
|
| 797 | + } |
|
| 745 | 798 | |
| 746 | 799 | // Create the mold for a single row insert. |
| 747 | 800 | $insertData = '('; |
| 748 | 801 | foreach ($columns as $columnName => $type) |
| 749 | 802 | { |
| 750 | 803 | // Are we restricting the length? |
| 751 | - if (strpos($type, 'string-') !== false) |
|
| 752 | - $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 753 | - else |
|
| 754 | - $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 804 | + if (strpos($type, 'string-') !== false) { |
|
| 805 | + $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 806 | + } else { |
|
| 807 | + $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 808 | + } |
|
| 755 | 809 | } |
| 756 | 810 | $insertData = substr($insertData, 0, -2) . ')'; |
| 757 | 811 | |
@@ -760,8 +814,9 @@ discard block |
||
| 760 | 814 | |
| 761 | 815 | // Here's where the variables are injected to the query. |
| 762 | 816 | $insertRows = array(); |
| 763 | - foreach ($data as $dataRow) |
|
| 764 | - $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 817 | + foreach ($data as $dataRow) { |
|
| 818 | + $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 819 | + } |
|
| 765 | 820 | |
| 766 | 821 | // Determine the method of insertion. |
| 767 | 822 | $queryTitle = $method == 'replace' ? 'REPLACE' : ($method == 'ignore' ? 'INSERT IGNORE' : 'INSERT'); |
@@ -780,8 +835,7 @@ discard block |
||
| 780 | 835 | ), |
| 781 | 836 | $connection |
| 782 | 837 | ); |
| 783 | - } |
|
| 784 | - else //special way for ignore method with returning |
|
| 838 | + } else //special way for ignore method with returning |
|
| 785 | 839 | { |
| 786 | 840 | $count = count($insertRows); |
| 787 | 841 | $ai = 0; |
@@ -801,19 +855,21 @@ discard block |
||
| 801 | 855 | ); |
| 802 | 856 | $new_id = $smcFunc['db_insert_id'](); |
| 803 | 857 | |
| 804 | - if ($last_id != $new_id) //the inserted value was new |
|
| 858 | + if ($last_id != $new_id) { |
|
| 859 | + //the inserted value was new |
|
| 805 | 860 | { |
| 806 | 861 | $ai = $new_id; |
| 807 | 862 | } |
| 808 | - else // the inserted value already exists we need to find the pk |
|
| 863 | + } else // the inserted value already exists we need to find the pk |
|
| 809 | 864 | { |
| 810 | 865 | $where_string = ''; |
| 811 | 866 | $count2 = count($indexed_columns); |
| 812 | 867 | for ($x = 0; $x < $count2; $x++) |
| 813 | 868 | { |
| 814 | 869 | $where_string += key($indexed_columns[$x]) . ' = '. $insertRows[$i][$x]; |
| 815 | - if (($x + 1) < $count2) |
|
| 816 | - $where_string += ' AND '; |
|
| 870 | + if (($x + 1) < $count2) { |
|
| 871 | + $where_string += ' AND '; |
|
| 872 | + } |
|
| 817 | 873 | } |
| 818 | 874 | |
| 819 | 875 | $request = $smcFunc['db_query']('',' |
@@ -829,25 +885,27 @@ discard block |
||
| 829 | 885 | } |
| 830 | 886 | } |
| 831 | 887 | |
| 832 | - if ($returnmode == 1) |
|
| 833 | - $return_var = $ai; |
|
| 834 | - else if ($returnmode == 2) |
|
| 835 | - $return_var[] = $ai; |
|
| 888 | + if ($returnmode == 1) { |
|
| 889 | + $return_var = $ai; |
|
| 890 | + } else if ($returnmode == 2) { |
|
| 891 | + $return_var[] = $ai; |
|
| 892 | + } |
|
| 836 | 893 | } |
| 837 | 894 | } |
| 838 | 895 | |
| 839 | 896 | |
| 840 | 897 | if ($with_returning) |
| 841 | 898 | { |
| 842 | - if ($returnmode == 1 && empty($return_var)) |
|
| 843 | - $return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1; |
|
| 844 | - else if ($returnmode == 2 && empty($return_var)) |
|
| 899 | + if ($returnmode == 1 && empty($return_var)) { |
|
| 900 | + $return_var = smf_db_insert_id($table, $keys[0]) + count($insertRows) - 1; |
|
| 901 | + } else if ($returnmode == 2 && empty($return_var)) |
|
| 845 | 902 | { |
| 846 | 903 | $return_var = array(); |
| 847 | 904 | $count = count($insertRows); |
| 848 | 905 | $start = smf_db_insert_id($table, $keys[0]); |
| 849 | - for ($i = 0; $i < $count; $i++ ) |
|
| 850 | - $return_var[] = $start + $i; |
|
| 906 | + for ($i = 0; $i < $count; $i++ ) { |
|
| 907 | + $return_var[] = $start + $i; |
|
| 908 | + } |
|
| 851 | 909 | } |
| 852 | 910 | return $return_var; |
| 853 | 911 | } |
@@ -865,8 +923,9 @@ discard block |
||
| 865 | 923 | */ |
| 866 | 924 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
| 867 | 925 | { |
| 868 | - if (empty($log_message)) |
|
| 869 | - $log_message = $error_message; |
|
| 926 | + if (empty($log_message)) { |
|
| 927 | + $log_message = $error_message; |
|
| 928 | + } |
|
| 870 | 929 | |
| 871 | 930 | foreach (debug_backtrace() as $step) |
| 872 | 931 | { |
@@ -885,12 +944,14 @@ discard block |
||
| 885 | 944 | } |
| 886 | 945 | |
| 887 | 946 | // A special case - we want the file and line numbers for debugging. |
| 888 | - if ($error_type == 'return') |
|
| 889 | - return array($file, $line); |
|
| 947 | + if ($error_type == 'return') { |
|
| 948 | + return array($file, $line); |
|
| 949 | + } |
|
| 890 | 950 | |
| 891 | 951 | // Is always a critical error. |
| 892 | - if (function_exists('log_error')) |
|
| 893 | - log_error($log_message, 'critical', $file, $line); |
|
| 952 | + if (function_exists('log_error')) { |
|
| 953 | + log_error($log_message, 'critical', $file, $line); |
|
| 954 | + } |
|
| 894 | 955 | |
| 895 | 956 | if (function_exists('fatal_error')) |
| 896 | 957 | { |
@@ -898,12 +959,12 @@ discard block |
||
| 898 | 959 | |
| 899 | 960 | // Cannot continue... |
| 900 | 961 | exit; |
| 962 | + } elseif ($error_type) { |
|
| 963 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 964 | + } else { |
|
| 965 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 966 | + } |
|
| 901 | 967 | } |
| 902 | - elseif ($error_type) |
|
| 903 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 904 | - else |
|
| 905 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 906 | -} |
|
| 907 | 968 | |
| 908 | 969 | /** |
| 909 | 970 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
@@ -920,10 +981,11 @@ discard block |
||
| 920 | 981 | '\\' => '\\\\', |
| 921 | 982 | ); |
| 922 | 983 | |
| 923 | - if ($translate_human_wildcards) |
|
| 924 | - $replacements += array( |
|
| 984 | + if ($translate_human_wildcards) { |
|
| 985 | + $replacements += array( |
|
| 925 | 986 | '*' => '%', |
| 926 | 987 | ); |
| 988 | + } |
|
| 927 | 989 | |
| 928 | 990 | return strtr($string, $replacements); |
| 929 | 991 | } |
@@ -937,8 +999,9 @@ discard block |
||
| 937 | 999 | */ |
| 938 | 1000 | function smf_is_resource($result) |
| 939 | 1001 | { |
| 940 | - if ($result instanceof mysqli_result) |
|
| 941 | - return true; |
|
| 1002 | + if ($result instanceof mysqli_result) { |
|
| 1003 | + return true; |
|
| 1004 | + } |
|
| 942 | 1005 | |
| 943 | 1006 | return false; |
| 944 | 1007 | } |
@@ -967,19 +1030,22 @@ discard block |
||
| 967 | 1030 | static $mysql_error_data_prep; |
| 968 | 1031 | |
| 969 | 1032 | // without database we can't do anything |
| 970 | - if (empty($db_connection)) |
|
| 971 | - return; |
|
| 1033 | + if (empty($db_connection)) { |
|
| 1034 | + return; |
|
| 1035 | + } |
|
| 972 | 1036 | |
| 973 | - if (empty($mysql_error_data_prep)) |
|
| 974 | - $mysql_error_data_prep = mysqli_prepare($db_connection, |
|
| 1037 | + if (empty($mysql_error_data_prep)) { |
|
| 1038 | + $mysql_error_data_prep = mysqli_prepare($db_connection, |
|
| 975 | 1039 | 'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line) |
| 976 | 1040 | VALUES( ?, ?, unhex(?), ?, ?, ?, ?, ?, ?)' |
| 977 | 1041 | ); |
| 1042 | + } |
|
| 978 | 1043 | |
| 979 | - if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false) |
|
| 980 | - $error_array[2] = bin2hex(inet_pton($error_array[2])); |
|
| 981 | - else |
|
| 982 | - $error_array[2] = null; |
|
| 1044 | + if (filter_var($error_array[2], FILTER_VALIDATE_IP) !== false) { |
|
| 1045 | + $error_array[2] = bin2hex(inet_pton($error_array[2])); |
|
| 1046 | + } else { |
|
| 1047 | + $error_array[2] = null; |
|
| 1048 | + } |
|
| 983 | 1049 | mysqli_stmt_bind_param($mysql_error_data_prep, 'iissssssi', |
| 984 | 1050 | $error_array[0], $error_array[1], $error_array[2], $error_array[3], $error_array[4], $error_array[5], $error_array[6], |
| 985 | 1051 | $error_array[7], $error_array[8]); |
@@ -1001,8 +1067,9 @@ discard block |
||
| 1001 | 1067 | $count = count($array_values); |
| 1002 | 1068 | $then = ($desc ? ' THEN -' : ' THEN '); |
| 1003 | 1069 | |
| 1004 | - for ($i = 0; $i < $count; $i++) |
|
| 1005 | - $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1070 | + for ($i = 0; $i < $count; $i++) { |
|
| 1071 | + $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1072 | + } |
|
| 1006 | 1073 | |
| 1007 | 1074 | $return .= 'END'; |
| 1008 | 1075 | return $return; |
@@ -15,8 +15,9 @@ discard block |
||
| 15 | 15 | * @version 2.1 Beta 4 |
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | -if (!defined('SMF')) |
|
| 18 | +if (!defined('SMF')) { |
|
| 19 | 19 | die('No direct access...'); |
| 20 | +} |
|
| 20 | 21 | |
| 21 | 22 | /** |
| 22 | 23 | * Log an error, if the error logging is enabled. |
@@ -41,17 +42,19 @@ discard block |
||
| 41 | 42 | // are we in a loop? |
| 42 | 43 | if($error_call > 2) |
| 43 | 44 | { |
| 44 | - if (!isset($db_show_debug) || $db_show_debug === false) |
|
| 45 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 46 | - else |
|
| 47 | - $backtrace = debug_backtrace(); |
|
| 45 | + if (!isset($db_show_debug) || $db_show_debug === false) { |
|
| 46 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 47 | + } else { |
|
| 48 | + $backtrace = debug_backtrace(); |
|
| 49 | + } |
|
| 48 | 50 | var_dump($backtrace); |
| 49 | 51 | die('Error loop.'); |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | 54 | // Check if error logging is actually on. |
| 53 | - if (empty($modSettings['enableErrorLogging'])) |
|
| 54 | - return $error_message; |
|
| 55 | + if (empty($modSettings['enableErrorLogging'])) { |
|
| 56 | + return $error_message; |
|
| 57 | + } |
|
| 55 | 58 | |
| 56 | 59 | // Basically, htmlspecialchars it minus &. (for entities!) |
| 57 | 60 | $error_message = strtr($error_message, array('<' => '<', '>' => '>', '"' => '"')); |
@@ -59,33 +62,39 @@ discard block |
||
| 59 | 62 | |
| 60 | 63 | // Add a file and line to the error message? |
| 61 | 64 | // Don't use the actual txt entries for file and line but instead use %1$s for file and %2$s for line |
| 62 | - if ($file == null) |
|
| 63 | - $file = ''; |
|
| 64 | - else |
|
| 65 | - // Window style slashes don't play well, lets convert them to the unix style. |
|
| 65 | + if ($file == null) { |
|
| 66 | + $file = ''; |
|
| 67 | + } else { |
|
| 68 | + // Window style slashes don't play well, lets convert them to the unix style. |
|
| 66 | 69 | $file = str_replace('\\', '/', $file); |
| 70 | + } |
|
| 67 | 71 | |
| 68 | - if ($line == null) |
|
| 69 | - $line = 0; |
|
| 70 | - else |
|
| 71 | - $line = (int) $line; |
|
| 72 | + if ($line == null) { |
|
| 73 | + $line = 0; |
|
| 74 | + } else { |
|
| 75 | + $line = (int) $line; |
|
| 76 | + } |
|
| 72 | 77 | |
| 73 | 78 | // Just in case there's no id_member or IP set yet. |
| 74 | - if (empty($user_info['id'])) |
|
| 75 | - $user_info['id'] = 0; |
|
| 76 | - if (empty($user_info['ip'])) |
|
| 77 | - $user_info['ip'] = ''; |
|
| 79 | + if (empty($user_info['id'])) { |
|
| 80 | + $user_info['id'] = 0; |
|
| 81 | + } |
|
| 82 | + if (empty($user_info['ip'])) { |
|
| 83 | + $user_info['ip'] = ''; |
|
| 84 | + } |
|
| 78 | 85 | |
| 79 | 86 | // Find the best query string we can... |
| 80 | 87 | $query_string = empty($_SERVER['QUERY_STRING']) ? (empty($_SERVER['REQUEST_URL']) ? '' : str_replace($scripturl, '', $_SERVER['REQUEST_URL'])) : $_SERVER['QUERY_STRING']; |
| 81 | 88 | |
| 82 | 89 | // Don't log the session hash in the url twice, it's a waste. |
| 83 | - if (!empty($smcFunc['htmlspecialchars'])) |
|
| 84 | - $query_string = $smcFunc['htmlspecialchars']((SMF == 'SSI' || SMF == 'BACKGROUND' ? '' : '?') . preg_replace(array('~;sesc=[^&;]+~', '~' . session_name() . '=' . session_id() . '[&;]~'), array(';sesc', ''), $query_string)); |
|
| 90 | + if (!empty($smcFunc['htmlspecialchars'])) { |
|
| 91 | + $query_string = $smcFunc['htmlspecialchars']((SMF == 'SSI' || SMF == 'BACKGROUND' ? '' : '?') . preg_replace(array('~;sesc=[^&;]+~', '~' . session_name() . '=' . session_id() . '[&;]~'), array(';sesc', ''), $query_string)); |
|
| 92 | + } |
|
| 85 | 93 | |
| 86 | 94 | // Just so we know what board error messages are from. |
| 87 | - if (isset($_POST['board']) && !isset($_GET['board'])) |
|
| 88 | - $query_string .= ($query_string == '' ? 'board=' : ';board=') . $_POST['board']; |
|
| 95 | + if (isset($_POST['board']) && !isset($_GET['board'])) { |
|
| 96 | + $query_string .= ($query_string == '' ? 'board=' : ';board=') . $_POST['board']; |
|
| 97 | + } |
|
| 89 | 98 | |
| 90 | 99 | // What types of categories do we have? |
| 91 | 100 | $known_error_types = array( |
@@ -134,9 +143,9 @@ discard block |
||
| 134 | 143 | |
| 135 | 144 | list($context['num_errors']) = $smcFunc['db_fetch_row']($query); |
| 136 | 145 | $smcFunc['db_free_result']($query); |
| 146 | + } else { |
|
| 147 | + $context['num_errors']++; |
|
| 137 | 148 | } |
| 138 | - else |
|
| 139 | - $context['num_errors']++; |
|
| 140 | 149 | } |
| 141 | 150 | |
| 142 | 151 | // reset error call |
@@ -158,12 +167,14 @@ discard block |
||
| 158 | 167 | global $txt; |
| 159 | 168 | |
| 160 | 169 | // Send the appropriate HTTP status header - set this to 0 or false if you don't want to send one at all |
| 161 | - if (!empty($status)) |
|
| 162 | - send_http_status($status); |
|
| 170 | + if (!empty($status)) { |
|
| 171 | + send_http_status($status); |
|
| 172 | + } |
|
| 163 | 173 | |
| 164 | 174 | // We don't have $txt yet, but that's okay... |
| 165 | - if (empty($txt)) |
|
| 166 | - die($error); |
|
| 175 | + if (empty($txt)) { |
|
| 176 | + die($error); |
|
| 177 | + } |
|
| 167 | 178 | |
| 168 | 179 | log_error_online($error, false); |
| 169 | 180 | setup_fatal_error_context($log ? log_error($error, $log) : $error); |
@@ -190,8 +201,9 @@ discard block |
||
| 190 | 201 | static $fatal_error_called = false; |
| 191 | 202 | |
| 192 | 203 | // Send the status header - set this to 0 or false if you don't want to send one at all |
| 193 | - if (!empty($status)) |
|
| 194 | - send_http_status($status); |
|
| 204 | + if (!empty($status)) { |
|
| 205 | + send_http_status($status); |
|
| 206 | + } |
|
| 195 | 207 | |
| 196 | 208 | // Try to load a theme if we don't have one. |
| 197 | 209 | if (empty($context['theme_loaded']) && empty($fatal_error_called)) |
@@ -201,8 +213,9 @@ discard block |
||
| 201 | 213 | } |
| 202 | 214 | |
| 203 | 215 | // If we have no theme stuff we can't have the language file... |
| 204 | - if (empty($context['theme_loaded'])) |
|
| 205 | - die($error); |
|
| 216 | + if (empty($context['theme_loaded'])) { |
|
| 217 | + die($error); |
|
| 218 | + } |
|
| 206 | 219 | |
| 207 | 220 | $reload_lang_file = true; |
| 208 | 221 | // Log the error in the forum's language, but don't waste the time if we aren't logging |
@@ -238,8 +251,9 @@ discard block |
||
| 238 | 251 | global $settings, $modSettings, $db_show_debug; |
| 239 | 252 | |
| 240 | 253 | // Ignore errors if we're ignoring them or they are strict notices from PHP 5 |
| 241 | - if (error_reporting() == 0) |
|
| 242 | - return; |
|
| 254 | + if (error_reporting() == 0) { |
|
| 255 | + return; |
|
| 256 | + } |
|
| 243 | 257 | |
| 244 | 258 | if (strpos($file, 'eval()') !== false && !empty($settings['current_include_filename'])) |
| 245 | 259 | { |
@@ -247,19 +261,22 @@ discard block |
||
| 247 | 261 | $count = count($array); |
| 248 | 262 | for ($i = 0; $i < $count; $i++) |
| 249 | 263 | { |
| 250 | - if ($array[$i]['function'] != 'loadSubTemplate') |
|
| 251 | - continue; |
|
| 264 | + if ($array[$i]['function'] != 'loadSubTemplate') { |
|
| 265 | + continue; |
|
| 266 | + } |
|
| 252 | 267 | |
| 253 | 268 | // This is a bug in PHP, with eval, it seems! |
| 254 | - if (empty($array[$i]['args'])) |
|
| 255 | - $i++; |
|
| 269 | + if (empty($array[$i]['args'])) { |
|
| 270 | + $i++; |
|
| 271 | + } |
|
| 256 | 272 | break; |
| 257 | 273 | } |
| 258 | 274 | |
| 259 | - if (isset($array[$i]) && !empty($array[$i]['args'])) |
|
| 260 | - $file = realpath($settings['current_include_filename']) . ' (' . $array[$i]['args'][0] . ' sub template - eval?)'; |
|
| 261 | - else |
|
| 262 | - $file = realpath($settings['current_include_filename']) . ' (eval?)'; |
|
| 275 | + if (isset($array[$i]) && !empty($array[$i]['args'])) { |
|
| 276 | + $file = realpath($settings['current_include_filename']) . ' (' . $array[$i]['args'][0] . ' sub template - eval?)'; |
|
| 277 | + } else { |
|
| 278 | + $file = realpath($settings['current_include_filename']) . ' (eval?)'; |
|
| 279 | + } |
|
| 263 | 280 | } |
| 264 | 281 | |
| 265 | 282 | if (isset($db_show_debug) && $db_show_debug === true) |
@@ -268,8 +285,9 @@ discard block |
||
| 268 | 285 | if ($error_level % 255 != E_ERROR) |
| 269 | 286 | { |
| 270 | 287 | $temporary = ob_get_contents(); |
| 271 | - if (substr($temporary, -2) == '="') |
|
| 272 | - echo '"'; |
|
| 288 | + if (substr($temporary, -2) == '="') { |
|
| 289 | + echo '"'; |
|
| 290 | + } |
|
| 273 | 291 | } |
| 274 | 292 | |
| 275 | 293 | // Debugging! This should look like a PHP error message. |
@@ -285,23 +303,27 @@ discard block |
||
| 285 | 303 | call_integration_hook('integrate_output_error', array($message, $error_type, $error_level, $file, $line)); |
| 286 | 304 | |
| 287 | 305 | // Dying on these errors only causes MORE problems (blank pages!) |
| 288 | - if ($file == 'Unknown') |
|
| 289 | - return; |
|
| 306 | + if ($file == 'Unknown') { |
|
| 307 | + return; |
|
| 308 | + } |
|
| 290 | 309 | |
| 291 | 310 | // If this is an E_ERROR or E_USER_ERROR.... die. Violently so. |
| 292 | - if ($error_level % 255 == E_ERROR) |
|
| 293 | - obExit(false); |
|
| 294 | - else |
|
| 295 | - return; |
|
| 311 | + if ($error_level % 255 == E_ERROR) { |
|
| 312 | + obExit(false); |
|
| 313 | + } else { |
|
| 314 | + return; |
|
| 315 | + } |
|
| 296 | 316 | |
| 297 | 317 | // If this is an E_ERROR, E_USER_ERROR, E_WARNING, or E_USER_WARNING.... die. Violently so. |
| 298 | - if ($error_level % 255 == E_ERROR || $error_level % 255 == E_WARNING) |
|
| 299 | - fatal_error(allowedTo('admin_forum') ? $message : $error_string, false); |
|
| 318 | + if ($error_level % 255 == E_ERROR || $error_level % 255 == E_WARNING) { |
|
| 319 | + fatal_error(allowedTo('admin_forum') ? $message : $error_string, false); |
|
| 320 | + } |
|
| 300 | 321 | |
| 301 | 322 | // We should NEVER get to this point. Any fatal error MUST quit, or very bad things can happen. |
| 302 | - if ($error_level % 255 == E_ERROR) |
|
| 303 | - die('No direct access...'); |
|
| 304 | -} |
|
| 323 | + if ($error_level % 255 == E_ERROR) { |
|
| 324 | + die('No direct access...'); |
|
| 325 | + } |
|
| 326 | + } |
|
| 305 | 327 | |
| 306 | 328 | /** |
| 307 | 329 | * It is called by {@link fatal_error()} and {@link fatal_lang_error()}. |
@@ -317,24 +339,28 @@ discard block |
||
| 317 | 339 | |
| 318 | 340 | // Attempt to prevent a recursive loop. |
| 319 | 341 | ++$level; |
| 320 | - if ($level > 1) |
|
| 321 | - return false; |
|
| 342 | + if ($level > 1) { |
|
| 343 | + return false; |
|
| 344 | + } |
|
| 322 | 345 | |
| 323 | 346 | // Maybe they came from dlattach or similar? |
| 324 | - if (SMF != 'SSI' && SMF != 'BACKGROUND' && empty($context['theme_loaded'])) |
|
| 325 | - loadTheme(); |
|
| 347 | + if (SMF != 'SSI' && SMF != 'BACKGROUND' && empty($context['theme_loaded'])) { |
|
| 348 | + loadTheme(); |
|
| 349 | + } |
|
| 326 | 350 | |
| 327 | 351 | // Don't bother indexing errors mate... |
| 328 | 352 | $context['robot_no_index'] = true; |
| 329 | 353 | |
| 330 | - if (!isset($context['error_title'])) |
|
| 331 | - $context['error_title'] = $txt['error_occured']; |
|
| 354 | + if (!isset($context['error_title'])) { |
|
| 355 | + $context['error_title'] = $txt['error_occured']; |
|
| 356 | + } |
|
| 332 | 357 | $context['error_message'] = isset($context['error_message']) ? $context['error_message'] : $error_message; |
| 333 | 358 | |
| 334 | 359 | $context['error_code'] = isset($error_code) ? 'id="' . $error_code . '" ' : ''; |
| 335 | 360 | |
| 336 | - if (empty($context['page_title'])) |
|
| 337 | - $context['page_title'] = $context['error_title']; |
|
| 361 | + if (empty($context['page_title'])) { |
|
| 362 | + $context['page_title'] = $context['error_title']; |
|
| 363 | + } |
|
| 338 | 364 | |
| 339 | 365 | loadTemplate('Errors'); |
| 340 | 366 | $context['sub_template'] = 'fatal_error'; |
@@ -342,23 +368,26 @@ discard block |
||
| 342 | 368 | // If this is SSI, what do they want us to do? |
| 343 | 369 | if (SMF == 'SSI') |
| 344 | 370 | { |
| 345 | - if (!empty($ssi_on_error_method) && $ssi_on_error_method !== true && is_callable($ssi_on_error_method)) |
|
| 346 | - $ssi_on_error_method(); |
|
| 347 | - elseif (empty($ssi_on_error_method) || $ssi_on_error_method !== true) |
|
| 348 | - loadSubTemplate('fatal_error'); |
|
| 371 | + if (!empty($ssi_on_error_method) && $ssi_on_error_method !== true && is_callable($ssi_on_error_method)) { |
|
| 372 | + $ssi_on_error_method(); |
|
| 373 | + } elseif (empty($ssi_on_error_method) || $ssi_on_error_method !== true) { |
|
| 374 | + loadSubTemplate('fatal_error'); |
|
| 375 | + } |
|
| 349 | 376 | |
| 350 | 377 | // No layers? |
| 351 | - if (empty($ssi_on_error_method) || $ssi_on_error_method !== true) |
|
| 352 | - exit; |
|
| 378 | + if (empty($ssi_on_error_method) || $ssi_on_error_method !== true) { |
|
| 379 | + exit; |
|
| 380 | + } |
|
| 353 | 381 | } |
| 354 | 382 | // Alternatively from the cron call? |
| 355 | 383 | elseif (SMF == 'BACKGROUND') |
| 356 | 384 | { |
| 357 | 385 | // We can't rely on even having language files available. |
| 358 | - if (defined('FROM_CLI') && FROM_CLI) |
|
| 359 | - echo 'cron error: ', $context['error_message']; |
|
| 360 | - else |
|
| 361 | - echo 'An error occurred. More information may be available in your logs.'; |
|
| 386 | + if (defined('FROM_CLI') && FROM_CLI) { |
|
| 387 | + echo 'cron error: ', $context['error_message']; |
|
| 388 | + } else { |
|
| 389 | + echo 'An error occurred. More information may be available in your logs.'; |
|
| 390 | + } |
|
| 362 | 391 | exit; |
| 363 | 392 | } |
| 364 | 393 | |
@@ -386,8 +415,8 @@ discard block |
||
| 386 | 415 | |
| 387 | 416 | set_fatal_error_headers(); |
| 388 | 417 | |
| 389 | - if (!empty($maintenance)) |
|
| 390 | - echo '<!DOCTYPE html> |
|
| 418 | + if (!empty($maintenance)) { |
|
| 419 | + echo '<!DOCTYPE html> |
|
| 391 | 420 | <html> |
| 392 | 421 | <head> |
| 393 | 422 | <meta name="robots" content="noindex"> |
@@ -398,6 +427,7 @@ discard block |
||
| 398 | 427 | ', $mmessage, ' |
| 399 | 428 | </body> |
| 400 | 429 | </html>'; |
| 430 | + } |
|
| 401 | 431 | |
| 402 | 432 | die(); |
| 403 | 433 | } |
@@ -419,15 +449,17 @@ discard block |
||
| 419 | 449 | // For our purposes, we're gonna want this on if at all possible. |
| 420 | 450 | $modSettings['cache_enable'] = '1'; |
| 421 | 451 | |
| 422 | - if (($temp = cache_get_data('db_last_error', 600)) !== null) |
|
| 423 | - $db_last_error = max($db_last_error, $temp); |
|
| 452 | + if (($temp = cache_get_data('db_last_error', 600)) !== null) { |
|
| 453 | + $db_last_error = max($db_last_error, $temp); |
|
| 454 | + } |
|
| 424 | 455 | |
| 425 | 456 | if ($db_last_error < time() - 3600 * 24 * 3 && empty($maintenance) && !empty($db_error_send)) |
| 426 | 457 | { |
| 427 | 458 | // Avoid writing to the Settings.php file if at all possible; use shared memory instead. |
| 428 | 459 | cache_put_data('db_last_error', time(), 600); |
| 429 | - if (($temp = cache_get_data('db_last_error', 600)) === null) |
|
| 430 | - logLastDatabaseError(); |
|
| 460 | + if (($temp = cache_get_data('db_last_error', 600)) === null) { |
|
| 461 | + logLastDatabaseError(); |
|
| 462 | + } |
|
| 431 | 463 | |
| 432 | 464 | // Language files aren't loaded yet :(. |
| 433 | 465 | $db_error = @$smcFunc['db_error']($db_connection); |
@@ -484,8 +516,9 @@ discard block |
||
| 484 | 516 | */ |
| 485 | 517 | function set_fatal_error_headers() |
| 486 | 518 | { |
| 487 | - if (headers_sent()) |
|
| 488 | - return; |
|
| 519 | + if (headers_sent()) { |
|
| 520 | + return; |
|
| 521 | + } |
|
| 489 | 522 | |
| 490 | 523 | // Don't cache this page! |
| 491 | 524 | header('expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
@@ -511,12 +544,14 @@ discard block |
||
| 511 | 544 | global $smcFunc, $user_info, $modSettings; |
| 512 | 545 | |
| 513 | 546 | // Don't bother if Who's Online is disabled. |
| 514 | - if (empty($modSettings['who_enabled'])) |
|
| 515 | - return; |
|
| 547 | + if (empty($modSettings['who_enabled'])) { |
|
| 548 | + return; |
|
| 549 | + } |
|
| 516 | 550 | |
| 517 | 551 | // Maybe they came from SSI or similar where sessions are not recorded? |
| 518 | - if (SMF == 'SSI' || SMF == 'BACKGROUND') |
|
| 519 | - return; |
|
| 552 | + if (SMF == 'SSI' || SMF == 'BACKGROUND') { |
|
| 553 | + return; |
|
| 554 | + } |
|
| 520 | 555 | |
| 521 | 556 | $session_id = !empty($user_info['is_guest']) ? 'ip' . $user_info['ip'] : session_id(); |
| 522 | 557 | |
@@ -542,8 +577,9 @@ discard block |
||
| 542 | 577 | $url = $smcFunc['json_decode']($url, true); |
| 543 | 578 | $url['error'] = $error; |
| 544 | 579 | |
| 545 | - if (!empty($sprintf)) |
|
| 546 | - $url['error_params'] = $sprintf; |
|
| 580 | + if (!empty($sprintf)) { |
|
| 581 | + $url['error_params'] = $sprintf; |
|
| 582 | + } |
|
| 547 | 583 | |
| 548 | 584 | $smcFunc['db_query']('', ' |
| 549 | 585 | UPDATE {db_prefix}log_online |
@@ -574,10 +610,11 @@ discard block |
||
| 574 | 610 | |
| 575 | 611 | $protocol = preg_match('~HTTP/1\.[01]~i', $_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'; |
| 576 | 612 | |
| 577 | - if (!isset($statuses[$code])) |
|
| 578 | - header($protocol . ' 500 Internal Server Error'); |
|
| 579 | - else |
|
| 580 | - header($protocol . ' ' . $code . ' ' . $statuses[$code]); |
|
| 581 | -} |
|
| 613 | + if (!isset($statuses[$code])) { |
|
| 614 | + header($protocol . ' 500 Internal Server Error'); |
|
| 615 | + } else { |
|
| 616 | + header($protocol . ' ' . $code . ' ' . $statuses[$code]); |
|
| 617 | + } |
|
| 618 | + } |
|
| 582 | 619 | |
| 583 | 620 | ?> |
| 584 | 621 | \ No newline at end of file |
@@ -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', |
@@ -64,11 +65,13 @@ discard block |
||
| 64 | 65 | 'db_error_insert' => 'smf_db_error_insert', |
| 65 | 66 | 'db_custom_order' => 'smf_db_custom_order', |
| 66 | 67 | ); |
| 68 | + } |
|
| 67 | 69 | |
| 68 | - if (!empty($db_options['persist'])) |
|
| 69 | - $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'] . '\'')); |
|
| 70 | - else |
|
| 71 | - $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'] . '\'')); |
|
| 70 | + if (!empty($db_options['persist'])) { |
|
| 71 | + $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'] . '\'')); |
|
| 72 | + } else { |
|
| 73 | + $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'] . '\'')); |
|
| 74 | + } |
|
| 72 | 75 | |
| 73 | 76 | // Something's wrong, show an error if its fatal (which we assume it is) |
| 74 | 77 | if (!$connection) |
@@ -76,8 +79,7 @@ discard block |
||
| 76 | 79 | if (!empty($db_options['non_fatal'])) |
| 77 | 80 | { |
| 78 | 81 | return null; |
| 79 | - } |
|
| 80 | - else |
|
| 82 | + } else |
|
| 81 | 83 | { |
| 82 | 84 | display_db_error(); |
| 83 | 85 | } |
@@ -128,31 +130,38 @@ discard block |
||
| 128 | 130 | |
| 129 | 131 | list ($values, $connection) = $db_callback; |
| 130 | 132 | |
| 131 | - if ($matches[1] === 'db_prefix') |
|
| 132 | - return $db_prefix; |
|
| 133 | + if ($matches[1] === 'db_prefix') { |
|
| 134 | + return $db_prefix; |
|
| 135 | + } |
|
| 133 | 136 | |
| 134 | - if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) |
|
| 135 | - return $user_info[$matches[1]]; |
|
| 137 | + if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) { |
|
| 138 | + return $user_info[$matches[1]]; |
|
| 139 | + } |
|
| 136 | 140 | |
| 137 | - if ($matches[1] === 'empty') |
|
| 138 | - return '\'\''; |
|
| 141 | + if ($matches[1] === 'empty') { |
|
| 142 | + return '\'\''; |
|
| 143 | + } |
|
| 139 | 144 | |
| 140 | - if (!isset($matches[2])) |
|
| 141 | - smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 145 | + if (!isset($matches[2])) { |
|
| 146 | + smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 147 | + } |
|
| 142 | 148 | |
| 143 | - if ($matches[1] === 'literal') |
|
| 144 | - return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 149 | + if ($matches[1] === 'literal') { |
|
| 150 | + return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 151 | + } |
|
| 145 | 152 | |
| 146 | - if (!isset($values[$matches[2]])) |
|
| 147 | - smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 153 | + if (!isset($values[$matches[2]])) { |
|
| 154 | + 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__); |
|
| 155 | + } |
|
| 148 | 156 | |
| 149 | 157 | $replacement = $values[$matches[2]]; |
| 150 | 158 | |
| 151 | 159 | switch ($matches[1]) |
| 152 | 160 | { |
| 153 | 161 | case 'int': |
| 154 | - if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 155 | - smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 162 | + if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) { |
|
| 163 | + smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 164 | + } |
|
| 156 | 165 | return (string) (int) $replacement; |
| 157 | 166 | break; |
| 158 | 167 | |
@@ -164,65 +173,73 @@ discard block |
||
| 164 | 173 | case 'array_int': |
| 165 | 174 | if (is_array($replacement)) |
| 166 | 175 | { |
| 167 | - if (empty($replacement)) |
|
| 168 | - smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 176 | + if (empty($replacement)) { |
|
| 177 | + smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 178 | + } |
|
| 169 | 179 | |
| 170 | 180 | foreach ($replacement as $key => $value) |
| 171 | 181 | { |
| 172 | - if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 173 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 182 | + if (!is_numeric($value) || (string) $value !== (string) (int) $value) { |
|
| 183 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 184 | + } |
|
| 174 | 185 | |
| 175 | 186 | $replacement[$key] = (string) (int) $value; |
| 176 | 187 | } |
| 177 | 188 | |
| 178 | 189 | return implode(', ', $replacement); |
| 190 | + } else { |
|
| 191 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 179 | 192 | } |
| 180 | - else |
|
| 181 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 182 | 193 | |
| 183 | 194 | break; |
| 184 | 195 | |
| 185 | 196 | case 'array_string': |
| 186 | 197 | if (is_array($replacement)) |
| 187 | 198 | { |
| 188 | - if (empty($replacement)) |
|
| 189 | - smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 199 | + if (empty($replacement)) { |
|
| 200 | + smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 201 | + } |
|
| 190 | 202 | |
| 191 | - foreach ($replacement as $key => $value) |
|
| 192 | - $replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
|
| 203 | + foreach ($replacement as $key => $value) { |
|
| 204 | + $replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
|
| 205 | + } |
|
| 193 | 206 | |
| 194 | 207 | return implode(', ', $replacement); |
| 208 | + } else { |
|
| 209 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 195 | 210 | } |
| 196 | - else |
|
| 197 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 198 | 211 | break; |
| 199 | 212 | |
| 200 | 213 | case 'date': |
| 201 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
|
| 202 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 203 | - else |
|
| 204 | - smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 214 | + if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) { |
|
| 215 | + return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 216 | + } else { |
|
| 217 | + smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 218 | + } |
|
| 205 | 219 | break; |
| 206 | 220 | |
| 207 | 221 | case 'time': |
| 208 | - if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 209 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 210 | - else |
|
| 211 | - smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 222 | + if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) { |
|
| 223 | + return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 224 | + } else { |
|
| 225 | + smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 226 | + } |
|
| 212 | 227 | break; |
| 213 | 228 | |
| 214 | 229 | case 'datetime': |
| 215 | - 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) |
|
| 216 | - return 'to_timestamp('. |
|
| 230 | + 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) { |
|
| 231 | + return 'to_timestamp('. |
|
| 217 | 232 | 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]). |
| 218 | 233 | ',\'YYYY-MM-DD HH24:MI:SS\')'; |
| 219 | - else |
|
| 220 | - smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 234 | + } else { |
|
| 235 | + smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 236 | + } |
|
| 221 | 237 | break; |
| 222 | 238 | |
| 223 | 239 | case 'float': |
| 224 | - if (!is_numeric($replacement)) |
|
| 225 | - smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 240 | + if (!is_numeric($replacement)) { |
|
| 241 | + smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 242 | + } |
|
| 226 | 243 | return (string) (float) $replacement; |
| 227 | 244 | break; |
| 228 | 245 | |
@@ -235,31 +252,36 @@ discard block |
||
| 235 | 252 | break; |
| 236 | 253 | |
| 237 | 254 | case 'inet': |
| 238 | - if ($replacement == 'null' || $replacement == '') |
|
| 239 | - return 'null'; |
|
| 240 | - if (inet_pton($replacement) === false) |
|
| 241 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 255 | + if ($replacement == 'null' || $replacement == '') { |
|
| 256 | + return 'null'; |
|
| 257 | + } |
|
| 258 | + if (inet_pton($replacement) === false) { |
|
| 259 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 260 | + } |
|
| 242 | 261 | return sprintf('\'%1$s\'::inet', pg_escape_string($replacement)); |
| 243 | 262 | |
| 244 | 263 | case 'array_inet': |
| 245 | 264 | if (is_array($replacement)) |
| 246 | 265 | { |
| 247 | - if (empty($replacement)) |
|
| 248 | - smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 266 | + if (empty($replacement)) { |
|
| 267 | + smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 268 | + } |
|
| 249 | 269 | |
| 250 | 270 | foreach ($replacement as $key => $value) |
| 251 | 271 | { |
| 252 | - if ($replacement == 'null' || $replacement == '') |
|
| 253 | - $replacement[$key] = 'null'; |
|
| 254 | - if (!isValidIP($value)) |
|
| 255 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 272 | + if ($replacement == 'null' || $replacement == '') { |
|
| 273 | + $replacement[$key] = 'null'; |
|
| 274 | + } |
|
| 275 | + if (!isValidIP($value)) { |
|
| 276 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 277 | + } |
|
| 256 | 278 | $replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value)); |
| 257 | 279 | } |
| 258 | 280 | |
| 259 | 281 | return implode(', ', $replacement); |
| 282 | + } else { |
|
| 283 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 260 | 284 | } |
| 261 | - else |
|
| 262 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 263 | 285 | break; |
| 264 | 286 | |
| 265 | 287 | default: |
@@ -350,14 +372,16 @@ discard block |
||
| 350 | 372 | ), |
| 351 | 373 | ); |
| 352 | 374 | |
| 353 | - if (isset($replacements[$identifier])) |
|
| 354 | - $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 375 | + if (isset($replacements[$identifier])) { |
|
| 376 | + $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 377 | + } |
|
| 355 | 378 | |
| 356 | 379 | // Limits need to be a little different. |
| 357 | 380 | $db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string); |
| 358 | 381 | |
| 359 | - if (trim($db_string) == '') |
|
| 360 | - return false; |
|
| 382 | + if (trim($db_string) == '') { |
|
| 383 | + return false; |
|
| 384 | + } |
|
| 361 | 385 | |
| 362 | 386 | // Comments that are allowed in a query are preg_removed. |
| 363 | 387 | static $allowed_comments_from = array( |
@@ -377,8 +401,9 @@ discard block |
||
| 377 | 401 | $db_count = !isset($db_count) ? 1 : $db_count + 1; |
| 378 | 402 | $db_replace_result = 0; |
| 379 | 403 | |
| 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__); |
|
| 404 | + if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) { |
|
| 405 | + smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 406 | + } |
|
| 382 | 407 | |
| 383 | 408 | if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
| 384 | 409 | { |
@@ -403,17 +428,18 @@ discard block |
||
| 403 | 428 | while (true) |
| 404 | 429 | { |
| 405 | 430 | $pos = strpos($db_string_1, '\'', $pos + 1); |
| 406 | - if ($pos === false) |
|
| 407 | - break; |
|
| 431 | + if ($pos === false) { |
|
| 432 | + break; |
|
| 433 | + } |
|
| 408 | 434 | $clean .= substr($db_string_1, $old_pos, $pos - $old_pos); |
| 409 | 435 | |
| 410 | 436 | while (true) |
| 411 | 437 | { |
| 412 | 438 | $pos1 = strpos($db_string_1, '\'', $pos + 1); |
| 413 | 439 | $pos2 = strpos($db_string_1, '\\', $pos + 1); |
| 414 | - if ($pos1 === false) |
|
| 415 | - break; |
|
| 416 | - elseif ($pos2 === false || $pos2 > $pos1) |
|
| 440 | + if ($pos1 === false) { |
|
| 441 | + break; |
|
| 442 | + } elseif ($pos2 === false || $pos2 > $pos1) |
|
| 417 | 443 | { |
| 418 | 444 | $pos = $pos1; |
| 419 | 445 | break; |
@@ -429,16 +455,19 @@ discard block |
||
| 429 | 455 | $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); |
| 430 | 456 | |
| 431 | 457 | // Comments? We don't use comments in our queries, we leave 'em outside! |
| 432 | - if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
|
| 433 | - $fail = true; |
|
| 458 | + if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) { |
|
| 459 | + $fail = true; |
|
| 460 | + } |
|
| 434 | 461 | // Trying to change passwords, slow us down, or something? |
| 435 | - elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) |
|
| 436 | - $fail = true; |
|
| 437 | - elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) |
|
| 438 | - $fail = true; |
|
| 462 | + elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) { |
|
| 463 | + $fail = true; |
|
| 464 | + } elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { |
|
| 465 | + $fail = true; |
|
| 466 | + } |
|
| 439 | 467 | |
| 440 | - if (!empty($fail) && function_exists('log_error')) |
|
| 441 | - smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 468 | + if (!empty($fail) && function_exists('log_error')) { |
|
| 469 | + smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 470 | + } |
|
| 442 | 471 | } |
| 443 | 472 | |
| 444 | 473 | // Set optimize stuff |
@@ -465,8 +494,9 @@ discard block |
||
| 465 | 494 | list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
| 466 | 495 | |
| 467 | 496 | // Initialize $db_cache if not already initialized. |
| 468 | - if (!isset($db_cache)) |
|
| 469 | - $db_cache = array(); |
|
| 497 | + if (!isset($db_cache)) { |
|
| 498 | + $db_cache = array(); |
|
| 499 | + } |
|
| 470 | 500 | |
| 471 | 501 | if (!empty($_SESSION['debug_redirect'])) |
| 472 | 502 | { |
@@ -484,12 +514,14 @@ discard block |
||
| 484 | 514 | |
| 485 | 515 | $db_last_result = @pg_query($connection, $db_string); |
| 486 | 516 | |
| 487 | - if ($db_last_result === false && empty($db_values['db_error_skip'])) |
|
| 488 | - $db_last_result = smf_db_error($db_string, $connection); |
|
| 517 | + if ($db_last_result === false && empty($db_values['db_error_skip'])) { |
|
| 518 | + $db_last_result = smf_db_error($db_string, $connection); |
|
| 519 | + } |
|
| 489 | 520 | |
| 490 | 521 | // Debugging. |
| 491 | - if (isset($db_show_debug) && $db_show_debug === true) |
|
| 492 | - $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 522 | + if (isset($db_show_debug) && $db_show_debug === true) { |
|
| 523 | + $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 524 | + } |
|
| 493 | 525 | |
| 494 | 526 | return $db_last_result; |
| 495 | 527 | } |
@@ -506,10 +538,11 @@ discard block |
||
| 506 | 538 | { |
| 507 | 539 | global $db_last_result, $db_replace_result; |
| 508 | 540 | |
| 509 | - if ($db_replace_result) |
|
| 510 | - return $db_replace_result; |
|
| 511 | - elseif ($result === null && !$db_last_result) |
|
| 512 | - return 0; |
|
| 541 | + if ($db_replace_result) { |
|
| 542 | + return $db_replace_result; |
|
| 543 | + } elseif ($result === null && !$db_last_result) { |
|
| 544 | + return 0; |
|
| 545 | + } |
|
| 513 | 546 | |
| 514 | 547 | return pg_affected_rows($result === null ? $db_last_result : $result); |
| 515 | 548 | } |
@@ -533,8 +566,9 @@ discard block |
||
| 533 | 566 | array( |
| 534 | 567 | ) |
| 535 | 568 | ); |
| 536 | - if (!$request) |
|
| 537 | - return false; |
|
| 569 | + if (!$request) { |
|
| 570 | + return false; |
|
| 571 | + } |
|
| 538 | 572 | list ($lastID) = $smcFunc['db_fetch_row']($request); |
| 539 | 573 | $smcFunc['db_free_result']($request); |
| 540 | 574 | |
@@ -555,12 +589,13 @@ discard block |
||
| 555 | 589 | // Decide which connection to use |
| 556 | 590 | $connection = $connection === null ? $db_connection : $connection; |
| 557 | 591 | |
| 558 | - if ($type == 'begin') |
|
| 559 | - return @pg_query($connection, 'BEGIN'); |
|
| 560 | - elseif ($type == 'rollback') |
|
| 561 | - return @pg_query($connection, 'ROLLBACK'); |
|
| 562 | - elseif ($type == 'commit') |
|
| 563 | - return @pg_query($connection, 'COMMIT'); |
|
| 592 | + if ($type == 'begin') { |
|
| 593 | + return @pg_query($connection, 'BEGIN'); |
|
| 594 | + } elseif ($type == 'rollback') { |
|
| 595 | + return @pg_query($connection, 'ROLLBACK'); |
|
| 596 | + } elseif ($type == 'commit') { |
|
| 597 | + return @pg_query($connection, 'COMMIT'); |
|
| 598 | + } |
|
| 564 | 599 | |
| 565 | 600 | return false; |
| 566 | 601 | } |
@@ -588,19 +623,22 @@ discard block |
||
| 588 | 623 | $query_error = @pg_last_error($connection); |
| 589 | 624 | |
| 590 | 625 | // Log the error. |
| 591 | - if (function_exists('log_error')) |
|
| 592 | - log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 626 | + if (function_exists('log_error')) { |
|
| 627 | + log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 628 | + } |
|
| 593 | 629 | |
| 594 | 630 | // Nothing's defined yet... just die with it. |
| 595 | - if (empty($context) || empty($txt)) |
|
| 596 | - die($query_error); |
|
| 631 | + if (empty($context) || empty($txt)) { |
|
| 632 | + die($query_error); |
|
| 633 | + } |
|
| 597 | 634 | |
| 598 | 635 | // Show an error message, if possible. |
| 599 | 636 | $context['error_title'] = $txt['database_error']; |
| 600 | - if (allowedTo('admin_forum')) |
|
| 601 | - $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 602 | - else |
|
| 603 | - $context['error_message'] = $txt['try_again']; |
|
| 637 | + if (allowedTo('admin_forum')) { |
|
| 638 | + $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 639 | + } else { |
|
| 640 | + $context['error_message'] = $txt['try_again']; |
|
| 641 | + } |
|
| 604 | 642 | |
| 605 | 643 | if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) |
| 606 | 644 | { |
@@ -622,12 +660,14 @@ discard block |
||
| 622 | 660 | { |
| 623 | 661 | global $db_row_count; |
| 624 | 662 | |
| 625 | - if ($counter !== false) |
|
| 626 | - return pg_fetch_row($request, $counter); |
|
| 663 | + if ($counter !== false) { |
|
| 664 | + return pg_fetch_row($request, $counter); |
|
| 665 | + } |
|
| 627 | 666 | |
| 628 | 667 | // Reset the row counter... |
| 629 | - if (!isset($db_row_count[(int) $request])) |
|
| 630 | - $db_row_count[(int) $request] = 0; |
|
| 668 | + if (!isset($db_row_count[(int) $request])) { |
|
| 669 | + $db_row_count[(int) $request] = 0; |
|
| 670 | + } |
|
| 631 | 671 | |
| 632 | 672 | // Return the right row. |
| 633 | 673 | return @pg_fetch_row($request, $db_row_count[(int) $request]++); |
@@ -644,12 +684,14 @@ discard block |
||
| 644 | 684 | { |
| 645 | 685 | global $db_row_count; |
| 646 | 686 | |
| 647 | - if ($counter !== false) |
|
| 648 | - return pg_fetch_assoc($request, $counter); |
|
| 687 | + if ($counter !== false) { |
|
| 688 | + return pg_fetch_assoc($request, $counter); |
|
| 689 | + } |
|
| 649 | 690 | |
| 650 | 691 | // Reset the row counter... |
| 651 | - if (!isset($db_row_count[(int) $request])) |
|
| 652 | - $db_row_count[(int) $request] = 0; |
|
| 692 | + if (!isset($db_row_count[(int) $request])) { |
|
| 693 | + $db_row_count[(int) $request] = 0; |
|
| 694 | + } |
|
| 653 | 695 | |
| 654 | 696 | // Return the right row. |
| 655 | 697 | return @pg_fetch_assoc($request, $db_row_count[(int) $request]++); |
@@ -702,11 +744,13 @@ discard block |
||
| 702 | 744 | |
| 703 | 745 | $replace = ''; |
| 704 | 746 | |
| 705 | - if (empty($data)) |
|
| 706 | - return; |
|
| 747 | + if (empty($data)) { |
|
| 748 | + return; |
|
| 749 | + } |
|
| 707 | 750 | |
| 708 | - if (!is_array($data[array_rand($data)])) |
|
| 709 | - $data = array($data); |
|
| 751 | + if (!is_array($data[array_rand($data)])) { |
|
| 752 | + $data = array($data); |
|
| 753 | + } |
|
| 710 | 754 | |
| 711 | 755 | // Replace the prefix holder with the actual prefix. |
| 712 | 756 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
@@ -725,11 +769,13 @@ discard block |
||
| 725 | 769 | //pg 9.5 got replace support |
| 726 | 770 | $pg_version = $smcFunc['db_get_version'](); |
| 727 | 771 | // if we got a Beta Version |
| 728 | - if (stripos($pg_version, 'beta') !== false) |
|
| 729 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 772 | + if (stripos($pg_version, 'beta') !== false) { |
|
| 773 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 774 | + } |
|
| 730 | 775 | // or RC |
| 731 | - if (stripos($pg_version, 'rc') !== false) |
|
| 732 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 776 | + if (stripos($pg_version, 'rc') !== false) { |
|
| 777 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 778 | + } |
|
| 733 | 779 | |
| 734 | 780 | $replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); |
| 735 | 781 | } |
@@ -748,32 +794,35 @@ discard block |
||
| 748 | 794 | $key_str .= ($count_pk > 0 ? ',' : ''); |
| 749 | 795 | $key_str .= $columnName; |
| 750 | 796 | $count_pk++; |
| 751 | - } |
|
| 752 | - else if ($method == 'replace') //normal field |
|
| 797 | + } else if ($method == 'replace') { |
|
| 798 | + //normal field |
|
| 753 | 799 | { |
| 754 | 800 | $col_str .= ($count > 0 ? ',' : ''); |
| 801 | + } |
|
| 755 | 802 | $col_str .= $columnName . ' = EXCLUDED.' . $columnName; |
| 756 | 803 | $count++; |
| 757 | 804 | } |
| 758 | 805 | } |
| 759 | - if ($method == 'replace') |
|
| 760 | - $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
|
| 761 | - else |
|
| 762 | - $replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
|
| 763 | - } |
|
| 764 | - else if ($method == 'replace') |
|
| 806 | + if ($method == 'replace') { |
|
| 807 | + $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
|
| 808 | + } else { |
|
| 809 | + $replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
|
| 810 | + } |
|
| 811 | + } else if ($method == 'replace') |
|
| 765 | 812 | { |
| 766 | 813 | foreach ($columns as $columnName => $type) |
| 767 | 814 | { |
| 768 | 815 | // Are we restricting the length? |
| 769 | - if (strpos($type, 'string-') !== false) |
|
| 770 | - $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 771 | - else |
|
| 772 | - $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 816 | + if (strpos($type, 'string-') !== false) { |
|
| 817 | + $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 818 | + } else { |
|
| 819 | + $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 820 | + } |
|
| 773 | 821 | |
| 774 | 822 | // A key? That's what we were looking for. |
| 775 | - if (in_array($columnName, $keys)) |
|
| 776 | - $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 823 | + if (in_array($columnName, $keys)) { |
|
| 824 | + $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 825 | + } |
|
| 777 | 826 | $count++; |
| 778 | 827 | } |
| 779 | 828 | |
@@ -809,10 +858,11 @@ discard block |
||
| 809 | 858 | foreach ($columns as $columnName => $type) |
| 810 | 859 | { |
| 811 | 860 | // Are we restricting the length? |
| 812 | - if (strpos($type, 'string-') !== false) |
|
| 813 | - $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 814 | - else |
|
| 815 | - $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 861 | + if (strpos($type, 'string-') !== false) { |
|
| 862 | + $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 863 | + } else { |
|
| 864 | + $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 865 | + } |
|
| 816 | 866 | } |
| 817 | 867 | $insertData = substr($insertData, 0, -2) . ')'; |
| 818 | 868 | |
@@ -821,8 +871,9 @@ discard block |
||
| 821 | 871 | |
| 822 | 872 | // Here's where the variables are injected to the query. |
| 823 | 873 | $insertRows = array(); |
| 824 | - foreach ($data as $dataRow) |
|
| 825 | - $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 874 | + foreach ($data as $dataRow) { |
|
| 875 | + $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 876 | + } |
|
| 826 | 877 | |
| 827 | 878 | // Do the insert. |
| 828 | 879 | $request = $smcFunc['db_query']('', ' |
@@ -839,19 +890,21 @@ discard block |
||
| 839 | 890 | |
| 840 | 891 | if ($with_returning && $request !== false) |
| 841 | 892 | { |
| 842 | - if ($returnmode === 2) |
|
| 843 | - $return_var = array(); |
|
| 893 | + if ($returnmode === 2) { |
|
| 894 | + $return_var = array(); |
|
| 895 | + } |
|
| 844 | 896 | |
| 845 | 897 | while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
| 846 | 898 | { |
| 847 | - if (is_numeric($row[0])) // try to emulate mysql limitation |
|
| 899 | + if (is_numeric($row[0])) { |
|
| 900 | + // try to emulate mysql limitation |
|
| 848 | 901 | { |
| 849 | 902 | if ($returnmode === 1) |
| 850 | 903 | $return_var = $row[0]; |
| 851 | - elseif ($returnmode === 2) |
|
| 852 | - $return_var[] = $row[0]; |
|
| 853 | - } |
|
| 854 | - else |
|
| 904 | + } elseif ($returnmode === 2) { |
|
| 905 | + $return_var[] = $row[0]; |
|
| 906 | + } |
|
| 907 | + } else |
|
| 855 | 908 | { |
| 856 | 909 | $with_returning = false; |
| 857 | 910 | trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); |
@@ -860,9 +913,10 @@ discard block |
||
| 860 | 913 | } |
| 861 | 914 | } |
| 862 | 915 | |
| 863 | - if ($with_returning && !empty($return_var)) |
|
| 864 | - return $return_var; |
|
| 865 | -} |
|
| 916 | + if ($with_returning && !empty($return_var)) { |
|
| 917 | + return $return_var; |
|
| 918 | + } |
|
| 919 | + } |
|
| 866 | 920 | |
| 867 | 921 | /** |
| 868 | 922 | * Dummy function really. Doesn't do anything on PostgreSQL. |
@@ -899,8 +953,9 @@ discard block |
||
| 899 | 953 | */ |
| 900 | 954 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
| 901 | 955 | { |
| 902 | - if (empty($log_message)) |
|
| 903 | - $log_message = $error_message; |
|
| 956 | + if (empty($log_message)) { |
|
| 957 | + $log_message = $error_message; |
|
| 958 | + } |
|
| 904 | 959 | |
| 905 | 960 | foreach (debug_backtrace() as $step) |
| 906 | 961 | { |
@@ -919,12 +974,14 @@ discard block |
||
| 919 | 974 | } |
| 920 | 975 | |
| 921 | 976 | // A special case - we want the file and line numbers for debugging. |
| 922 | - if ($error_type == 'return') |
|
| 923 | - return array($file, $line); |
|
| 977 | + if ($error_type == 'return') { |
|
| 978 | + return array($file, $line); |
|
| 979 | + } |
|
| 924 | 980 | |
| 925 | 981 | // Is always a critical error. |
| 926 | - if (function_exists('log_error')) |
|
| 927 | - log_error($log_message, 'critical', $file, $line); |
|
| 982 | + if (function_exists('log_error')) { |
|
| 983 | + log_error($log_message, 'critical', $file, $line); |
|
| 984 | + } |
|
| 928 | 985 | |
| 929 | 986 | if (function_exists('fatal_error')) |
| 930 | 987 | { |
@@ -932,12 +989,12 @@ discard block |
||
| 932 | 989 | |
| 933 | 990 | // Cannot continue... |
| 934 | 991 | exit; |
| 992 | + } elseif ($error_type) { |
|
| 993 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 994 | + } else { |
|
| 995 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 996 | + } |
|
| 935 | 997 | } |
| 936 | - elseif ($error_type) |
|
| 937 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 938 | - else |
|
| 939 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 940 | -} |
|
| 941 | 998 | |
| 942 | 999 | /** |
| 943 | 1000 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
@@ -954,10 +1011,11 @@ discard block |
||
| 954 | 1011 | '\\' => '\\\\', |
| 955 | 1012 | ); |
| 956 | 1013 | |
| 957 | - if ($translate_human_wildcards) |
|
| 958 | - $replacements += array( |
|
| 1014 | + if ($translate_human_wildcards) { |
|
| 1015 | + $replacements += array( |
|
| 959 | 1016 | '*' => '%', |
| 960 | 1017 | ); |
| 1018 | + } |
|
| 961 | 1019 | |
| 962 | 1020 | return strtr($string, $replacements); |
| 963 | 1021 | } |
@@ -986,14 +1044,16 @@ discard block |
||
| 986 | 1044 | static $pg_error_data_prep; |
| 987 | 1045 | |
| 988 | 1046 | // without database we can't do anything |
| 989 | - if (empty($db_connection)) |
|
| 990 | - return; |
|
| 1047 | + if (empty($db_connection)) { |
|
| 1048 | + return; |
|
| 1049 | + } |
|
| 991 | 1050 | |
| 992 | - if (empty($pg_error_data_prep)) |
|
| 993 | - $pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
|
| 1051 | + if (empty($pg_error_data_prep)) { |
|
| 1052 | + $pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
|
| 994 | 1053 | 'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line) |
| 995 | 1054 | VALUES( $1, $2, $3, $4, $5, $6, $7, $8, $9)' |
| 996 | 1055 | ); |
| 1056 | + } |
|
| 997 | 1057 | |
| 998 | 1058 | pg_execute($db_connection, 'smf_log_errors', $error_array); |
| 999 | 1059 | } |
@@ -1013,8 +1073,9 @@ discard block |
||
| 1013 | 1073 | $count = count($array_values); |
| 1014 | 1074 | $then = ($desc ? ' THEN -' : ' THEN '); |
| 1015 | 1075 | |
| 1016 | - for ($i = 0; $i < $count; $i++) |
|
| 1017 | - $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1076 | + for ($i = 0; $i < $count; $i++) { |
|
| 1077 | + $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1078 | + } |
|
| 1018 | 1079 | |
| 1019 | 1080 | $return .= 'END'; |
| 1020 | 1081 | return $return; |
@@ -17,8 +17,9 @@ discard block |
||
| 17 | 17 | * @version 2.1 Beta 4 |
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | -if (!defined('SMF')) |
|
| 20 | +if (!defined('SMF')) { |
|
| 21 | 21 | die('No direct access...'); |
| 22 | +} |
|
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | 25 | * Attempt to start the session, unless it already has been. |
@@ -38,8 +39,9 @@ discard block |
||
| 38 | 39 | { |
| 39 | 40 | $parsed_url = parse_url($boardurl); |
| 40 | 41 | |
| 41 | - if (preg_match('~^\d{1,3}(\.\d{1,3}){3}$~', $parsed_url['host']) == 0 && preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $parsed_url['host'], $parts) == 1) |
|
| 42 | - @ini_set('session.cookie_domain', '.' . $parts[1]); |
|
| 42 | + if (preg_match('~^\d{1,3}(\.\d{1,3}){3}$~', $parsed_url['host']) == 0 && preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $parsed_url['host'], $parts) == 1) { |
|
| 43 | + @ini_set('session.cookie_domain', '.' . $parts[1]); |
|
| 44 | + } |
|
| 43 | 45 | } |
| 44 | 46 | // @todo Set the session cookie path? |
| 45 | 47 | |
@@ -47,8 +49,9 @@ discard block |
||
| 47 | 49 | if ((ini_get('session.auto_start') == 1 && !empty($modSettings['databaseSession_enable'])) || session_id() == '') |
| 48 | 50 | { |
| 49 | 51 | // Attempt to end the already-started session. |
| 50 | - if (ini_get('session.auto_start') == 1) |
|
| 51 | - session_write_close(); |
|
| 52 | + if (ini_get('session.auto_start') == 1) { |
|
| 53 | + session_write_close(); |
|
| 54 | + } |
|
| 52 | 55 | |
| 53 | 56 | // This is here to stop people from using bad junky PHPSESSIDs. |
| 54 | 57 | if (isset($_REQUEST[session_name()]) && preg_match('~^[A-Za-z0-9,-]{16,64}$~', $_REQUEST[session_name()]) == 0 && !isset($_COOKIE[session_name()])) |
@@ -63,23 +66,26 @@ discard block |
||
| 63 | 66 | if (!empty($modSettings['databaseSession_enable'])) |
| 64 | 67 | { |
| 65 | 68 | @ini_set('session.serialize_handler', 'php_serialize'); |
| 66 | - if (ini_get('session.serialize_handler') != 'php_serialize') |
|
| 67 | - @ini_set('session.serialize_handler', 'php'); |
|
| 69 | + if (ini_get('session.serialize_handler') != 'php_serialize') { |
|
| 70 | + @ini_set('session.serialize_handler', 'php'); |
|
| 71 | + } |
|
| 68 | 72 | session_set_save_handler('sessionOpen', 'sessionClose', 'sessionRead', 'sessionWrite', 'sessionDestroy', 'sessionGC'); |
| 69 | 73 | @ini_set('session.gc_probability', '1'); |
| 74 | + } elseif (ini_get('session.gc_maxlifetime') <= 1440 && !empty($modSettings['databaseSession_lifetime'])) { |
|
| 75 | + @ini_set('session.gc_maxlifetime', max($modSettings['databaseSession_lifetime'], 60)); |
|
| 70 | 76 | } |
| 71 | - elseif (ini_get('session.gc_maxlifetime') <= 1440 && !empty($modSettings['databaseSession_lifetime'])) |
|
| 72 | - @ini_set('session.gc_maxlifetime', max($modSettings['databaseSession_lifetime'], 60)); |
|
| 73 | 77 | |
| 74 | 78 | // Use cache setting sessions? |
| 75 | - if (empty($modSettings['databaseSession_enable']) && !empty($modSettings['cache_enable']) && php_sapi_name() != 'cli') |
|
| 76 | - call_integration_hook('integrate_session_handlers'); |
|
| 79 | + if (empty($modSettings['databaseSession_enable']) && !empty($modSettings['cache_enable']) && php_sapi_name() != 'cli') { |
|
| 80 | + call_integration_hook('integrate_session_handlers'); |
|
| 81 | + } |
|
| 77 | 82 | |
| 78 | 83 | session_start(); |
| 79 | 84 | |
| 80 | 85 | // Change it so the cache settings are a little looser than default. |
| 81 | - if (!empty($modSettings['databaseSession_loose'])) |
|
| 82 | - header('cache-control: private'); |
|
| 86 | + if (!empty($modSettings['databaseSession_loose'])) { |
|
| 87 | + header('cache-control: private'); |
|
| 88 | + } |
|
| 83 | 89 | } |
| 84 | 90 | |
| 85 | 91 | // Set the randomly generated code. |
@@ -125,8 +131,9 @@ discard block |
||
| 125 | 131 | { |
| 126 | 132 | global $smcFunc; |
| 127 | 133 | |
| 128 | - if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
|
| 129 | - return ''; |
|
| 134 | + if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) { |
|
| 135 | + return ''; |
|
| 136 | + } |
|
| 130 | 137 | |
| 131 | 138 | // Look for it in the database. |
| 132 | 139 | $result = $smcFunc['db_query']('', ' |
@@ -155,8 +162,9 @@ discard block |
||
| 155 | 162 | { |
| 156 | 163 | global $smcFunc, $db_connection, $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_persist, $db_port; |
| 157 | 164 | |
| 158 | - if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
|
| 159 | - return false; |
|
| 165 | + if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) { |
|
| 166 | + return false; |
|
| 167 | + } |
|
| 160 | 168 | |
| 161 | 169 | // php < 7.0 need this |
| 162 | 170 | if (empty($db_connection)) |
@@ -164,8 +172,9 @@ discard block |
||
| 164 | 172 | $db_options = array(); |
| 165 | 173 | |
| 166 | 174 | // Add in the port if needed |
| 167 | - if (!empty($db_port)) |
|
| 168 | - $db_options['port'] = $db_port; |
|
| 175 | + if (!empty($db_port)) { |
|
| 176 | + $db_options['port'] = $db_port; |
|
| 177 | + } |
|
| 169 | 178 | $options = array_merge($db_options, array('persist' => $db_persist, 'dont_select_db' => SMF == 'SSI')); |
| 170 | 179 | |
| 171 | 180 | $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options); |
@@ -184,13 +193,14 @@ discard block |
||
| 184 | 193 | ); |
| 185 | 194 | |
| 186 | 195 | // If that didn't work, try inserting a new one. |
| 187 | - if ($smcFunc['db_affected_rows']() == 0) |
|
| 188 | - $smcFunc['db_insert']('ignore', |
|
| 196 | + if ($smcFunc['db_affected_rows']() == 0) { |
|
| 197 | + $smcFunc['db_insert']('ignore', |
|
| 189 | 198 | '{db_prefix}sessions', |
| 190 | 199 | array('session_id' => 'string', 'data' => 'string', 'last_update' => 'int'), |
| 191 | 200 | array($session_id, $data, time()), |
| 192 | 201 | array('session_id') |
| 193 | 202 | ); |
| 203 | + } |
|
| 194 | 204 | |
| 195 | 205 | return ($smcFunc['db_affected_rows']() == 0 ? false : true); |
| 196 | 206 | } |
@@ -205,8 +215,9 @@ discard block |
||
| 205 | 215 | { |
| 206 | 216 | global $smcFunc; |
| 207 | 217 | |
| 208 | - if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
|
| 209 | - return false; |
|
| 218 | + if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) { |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 210 | 221 | |
| 211 | 222 | // Just delete the row... |
| 212 | 223 | $smcFunc['db_query']('', ' |
@@ -232,8 +243,9 @@ discard block |
||
| 232 | 243 | global $modSettings, $smcFunc; |
| 233 | 244 | |
| 234 | 245 | // Just set to the default or lower? Ignore it for a higher value. (hopefully) |
| 235 | - if (!empty($modSettings['databaseSession_lifetime']) && ($max_lifetime <= 1440 || $modSettings['databaseSession_lifetime'] > $max_lifetime)) |
|
| 236 | - $max_lifetime = max($modSettings['databaseSession_lifetime'], 60); |
|
| 246 | + if (!empty($modSettings['databaseSession_lifetime']) && ($max_lifetime <= 1440 || $modSettings['databaseSession_lifetime'] > $max_lifetime)) { |
|
| 247 | + $max_lifetime = max($modSettings['databaseSession_lifetime'], 60); |
|
| 248 | + } |
|
| 237 | 249 | |
| 238 | 250 | // Clean up after yerself ;). |
| 239 | 251 | $smcFunc['db_query']('', ' |