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