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