@@ -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]++); |
@@ -697,11 +741,13 @@ discard block |
||
| 697 | 741 | |
| 698 | 742 | $replace = ''; |
| 699 | 743 | |
| 700 | - if (empty($data)) |
|
| 701 | - return; |
|
| 744 | + if (empty($data)) { |
|
| 745 | + return; |
|
| 746 | + } |
|
| 702 | 747 | |
| 703 | - if (!is_array($data[array_rand($data)])) |
|
| 704 | - $data = array($data); |
|
| 748 | + if (!is_array($data[array_rand($data)])) { |
|
| 749 | + $data = array($data); |
|
| 750 | + } |
|
| 705 | 751 | |
| 706 | 752 | // Replace the prefix holder with the actual prefix. |
| 707 | 753 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
@@ -727,32 +773,35 @@ discard block |
||
| 727 | 773 | $key_str .= ($count_pk > 0 ? ',' : ''); |
| 728 | 774 | $key_str .= $columnName; |
| 729 | 775 | $count_pk++; |
| 730 | - } |
|
| 731 | - else if ($method == 'replace') //normal field |
|
| 776 | + } else if ($method == 'replace') { |
|
| 777 | + //normal field |
|
| 732 | 778 | { |
| 733 | 779 | $col_str .= ($count > 0 ? ',' : ''); |
| 780 | + } |
|
| 734 | 781 | $col_str .= $columnName . ' = EXCLUDED.' . $columnName; |
| 735 | 782 | $count++; |
| 736 | 783 | } |
| 737 | 784 | } |
| 738 | - if ($method == 'replace') |
|
| 739 | - $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
|
| 740 | - else |
|
| 741 | - $replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
|
| 742 | - } |
|
| 743 | - else if ($method == 'replace') |
|
| 785 | + if ($method == 'replace') { |
|
| 786 | + $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
|
| 787 | + } else { |
|
| 788 | + $replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
|
| 789 | + } |
|
| 790 | + } else if ($method == 'replace') |
|
| 744 | 791 | { |
| 745 | 792 | foreach ($columns as $columnName => $type) |
| 746 | 793 | { |
| 747 | 794 | // Are we restricting the length? |
| 748 | - if (strpos($type, 'string-') !== false) |
|
| 749 | - $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 750 | - else |
|
| 751 | - $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 795 | + if (strpos($type, 'string-') !== false) { |
|
| 796 | + $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 797 | + } else { |
|
| 798 | + $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 799 | + } |
|
| 752 | 800 | |
| 753 | 801 | // A key? That's what we were looking for. |
| 754 | - if (in_array($columnName, $keys)) |
|
| 755 | - $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 802 | + if (in_array($columnName, $keys)) { |
|
| 803 | + $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 804 | + } |
|
| 756 | 805 | $count++; |
| 757 | 806 | } |
| 758 | 807 | |
@@ -788,10 +837,11 @@ discard block |
||
| 788 | 837 | foreach ($columns as $columnName => $type) |
| 789 | 838 | { |
| 790 | 839 | // Are we restricting the length? |
| 791 | - if (strpos($type, 'string-') !== false) |
|
| 792 | - $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 793 | - else |
|
| 794 | - $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 840 | + if (strpos($type, 'string-') !== false) { |
|
| 841 | + $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 842 | + } else { |
|
| 843 | + $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 844 | + } |
|
| 795 | 845 | } |
| 796 | 846 | $insertData = substr($insertData, 0, -2) . ')'; |
| 797 | 847 | |
@@ -800,8 +850,9 @@ discard block |
||
| 800 | 850 | |
| 801 | 851 | // Here's where the variables are injected to the query. |
| 802 | 852 | $insertRows = array(); |
| 803 | - foreach ($data as $dataRow) |
|
| 804 | - $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 853 | + foreach ($data as $dataRow) { |
|
| 854 | + $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 855 | + } |
|
| 805 | 856 | |
| 806 | 857 | // Do the insert. |
| 807 | 858 | $request = $smcFunc['db_query']('', ' |
@@ -818,19 +869,21 @@ discard block |
||
| 818 | 869 | |
| 819 | 870 | if ($with_returning && $request !== false) |
| 820 | 871 | { |
| 821 | - if ($returnmode === 2) |
|
| 822 | - $return_var = array(); |
|
| 872 | + if ($returnmode === 2) { |
|
| 873 | + $return_var = array(); |
|
| 874 | + } |
|
| 823 | 875 | |
| 824 | 876 | while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
| 825 | 877 | { |
| 826 | - if (is_numeric($row[0])) // try to emulate mysql limitation |
|
| 878 | + if (is_numeric($row[0])) { |
|
| 879 | + // try to emulate mysql limitation |
|
| 827 | 880 | { |
| 828 | 881 | if ($returnmode === 1) |
| 829 | 882 | $return_var = $row[0]; |
| 830 | - elseif ($returnmode === 2) |
|
| 831 | - $return_var[] = $row[0]; |
|
| 832 | - } |
|
| 833 | - else |
|
| 883 | + } elseif ($returnmode === 2) { |
|
| 884 | + $return_var[] = $row[0]; |
|
| 885 | + } |
|
| 886 | + } else |
|
| 834 | 887 | { |
| 835 | 888 | $with_returning = false; |
| 836 | 889 | trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); |
@@ -839,9 +892,10 @@ discard block |
||
| 839 | 892 | } |
| 840 | 893 | } |
| 841 | 894 | |
| 842 | - if ($with_returning && !empty($return_var)) |
|
| 843 | - return $return_var; |
|
| 844 | -} |
|
| 895 | + if ($with_returning && !empty($return_var)) { |
|
| 896 | + return $return_var; |
|
| 897 | + } |
|
| 898 | + } |
|
| 845 | 899 | |
| 846 | 900 | /** |
| 847 | 901 | * Dummy function really. Doesn't do anything on PostgreSQL. |
@@ -878,8 +932,9 @@ discard block |
||
| 878 | 932 | */ |
| 879 | 933 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
| 880 | 934 | { |
| 881 | - if (empty($log_message)) |
|
| 882 | - $log_message = $error_message; |
|
| 935 | + if (empty($log_message)) { |
|
| 936 | + $log_message = $error_message; |
|
| 937 | + } |
|
| 883 | 938 | |
| 884 | 939 | foreach (debug_backtrace() as $step) |
| 885 | 940 | { |
@@ -898,12 +953,14 @@ discard block |
||
| 898 | 953 | } |
| 899 | 954 | |
| 900 | 955 | // A special case - we want the file and line numbers for debugging. |
| 901 | - if ($error_type == 'return') |
|
| 902 | - return array($file, $line); |
|
| 956 | + if ($error_type == 'return') { |
|
| 957 | + return array($file, $line); |
|
| 958 | + } |
|
| 903 | 959 | |
| 904 | 960 | // Is always a critical error. |
| 905 | - if (function_exists('log_error')) |
|
| 906 | - log_error($log_message, 'critical', $file, $line); |
|
| 961 | + if (function_exists('log_error')) { |
|
| 962 | + log_error($log_message, 'critical', $file, $line); |
|
| 963 | + } |
|
| 907 | 964 | |
| 908 | 965 | if (function_exists('fatal_error')) |
| 909 | 966 | { |
@@ -911,12 +968,12 @@ discard block |
||
| 911 | 968 | |
| 912 | 969 | // Cannot continue... |
| 913 | 970 | exit; |
| 971 | + } elseif ($error_type) { |
|
| 972 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 973 | + } else { |
|
| 974 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 975 | + } |
|
| 914 | 976 | } |
| 915 | - elseif ($error_type) |
|
| 916 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 917 | - else |
|
| 918 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 919 | -} |
|
| 920 | 977 | |
| 921 | 978 | /** |
| 922 | 979 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
@@ -933,10 +990,11 @@ discard block |
||
| 933 | 990 | '\\' => '\\\\', |
| 934 | 991 | ); |
| 935 | 992 | |
| 936 | - if ($translate_human_wildcards) |
|
| 937 | - $replacements += array( |
|
| 993 | + if ($translate_human_wildcards) { |
|
| 994 | + $replacements += array( |
|
| 938 | 995 | '*' => '%', |
| 939 | 996 | ); |
| 997 | + } |
|
| 940 | 998 | |
| 941 | 999 | return strtr($string, $replacements); |
| 942 | 1000 | } |
@@ -965,14 +1023,16 @@ discard block |
||
| 965 | 1023 | static $pg_error_data_prep; |
| 966 | 1024 | |
| 967 | 1025 | // without database we can't do anything |
| 968 | - if (empty($db_connection)) |
|
| 969 | - return; |
|
| 1026 | + if (empty($db_connection)) { |
|
| 1027 | + return; |
|
| 1028 | + } |
|
| 970 | 1029 | |
| 971 | - if (empty($pg_error_data_prep)) |
|
| 972 | - $pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
|
| 1030 | + if (empty($pg_error_data_prep)) { |
|
| 1031 | + $pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
|
| 973 | 1032 | 'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace) |
| 974 | 1033 | VALUES( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' |
| 975 | 1034 | ); |
| 1035 | + } |
|
| 976 | 1036 | |
| 977 | 1037 | pg_execute($db_connection, 'smf_log_errors', $error_array); |
| 978 | 1038 | } |
@@ -992,8 +1052,9 @@ discard block |
||
| 992 | 1052 | $count = count($array_values); |
| 993 | 1053 | $then = ($desc ? ' THEN -' : ' THEN '); |
| 994 | 1054 | |
| 995 | - for ($i = 0; $i < $count; $i++) |
|
| 996 | - $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1055 | + for ($i = 0; $i < $count; $i++) { |
|
| 1056 | + $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1057 | + } |
|
| 997 | 1058 | |
| 998 | 1059 | $return .= 'END'; |
| 999 | 1060 | return $return; |
@@ -1016,11 +1077,13 @@ discard block |
||
| 1016 | 1077 | //pg 9.5 got replace support |
| 1017 | 1078 | $pg_version = $smcFunc['db_get_version'](); |
| 1018 | 1079 | // if we got a Beta Version |
| 1019 | - if (stripos($pg_version, 'beta') !== false) |
|
| 1020 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 1080 | + if (stripos($pg_version, 'beta') !== false) { |
|
| 1081 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 1082 | + } |
|
| 1021 | 1083 | // or RC |
| 1022 | - if (stripos($pg_version, 'rc') !== false) |
|
| 1023 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 1084 | + if (stripos($pg_version, 'rc') !== false) { |
|
| 1085 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 1086 | + } |
|
| 1024 | 1087 | |
| 1025 | 1088 | $replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); |
| 1026 | 1089 | } |