@@ -13,8 +13,9 @@ discard block |
||
| 13 | 13 | * @version 2.1 Beta 4 |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -if (!defined('SMF')) |
|
| 16 | +if (!defined('SMF')) { |
|
| 17 | 17 | die('No direct access...'); |
| 18 | +} |
|
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * Maps the implementations in this file (smf_db_function_name) |
@@ -34,8 +35,8 @@ discard block |
||
| 34 | 35 | global $smcFunc; |
| 35 | 36 | |
| 36 | 37 | // Map some database specific functions, only do this once. |
| 37 | - if (!isset($smcFunc['db_fetch_assoc'])) |
|
| 38 | - $smcFunc += array( |
|
| 38 | + if (!isset($smcFunc['db_fetch_assoc'])) { |
|
| 39 | + $smcFunc += array( |
|
| 39 | 40 | 'db_query' => 'smf_db_query', |
| 40 | 41 | 'db_quote' => 'smf_db_quote', |
| 41 | 42 | 'db_insert' => 'smf_db_insert', |
@@ -65,15 +66,18 @@ discard block |
||
| 65 | 66 | 'db_custom_order' => 'smf_db_custom_order', |
| 66 | 67 | 'db_native_replace' => 'smf_db_native_replace', |
| 67 | 68 | ); |
| 69 | + } |
|
| 68 | 70 | |
| 69 | 71 | // We are not going to make it very far without these. |
| 70 | - if (!function_exists('pg_pconnect')) |
|
| 71 | - display_db_error(); |
|
| 72 | + if (!function_exists('pg_pconnect')) { |
|
| 73 | + display_db_error(); |
|
| 74 | + } |
|
| 72 | 75 | |
| 73 | - if (!empty($db_options['persist'])) |
|
| 74 | - $connection = @pg_pconnect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 75 | - else |
|
| 76 | - $connection = @pg_connect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 76 | + if (!empty($db_options['persist'])) { |
|
| 77 | + $connection = @pg_pconnect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 78 | + } else { |
|
| 79 | + $connection = @pg_connect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 80 | + } |
|
| 77 | 81 | |
| 78 | 82 | // Something's wrong, show an error if its fatal (which we assume it is) |
| 79 | 83 | if (!$connection) |
@@ -81,8 +85,7 @@ discard block |
||
| 81 | 85 | if (!empty($db_options['non_fatal'])) |
| 82 | 86 | { |
| 83 | 87 | return null; |
| 84 | - } |
|
| 85 | - else |
|
| 88 | + } else |
|
| 86 | 89 | { |
| 87 | 90 | display_db_error(); |
| 88 | 91 | } |
@@ -133,31 +136,38 @@ discard block |
||
| 133 | 136 | |
| 134 | 137 | list ($values, $connection) = $db_callback; |
| 135 | 138 | |
| 136 | - if ($matches[1] === 'db_prefix') |
|
| 137 | - return $db_prefix; |
|
| 139 | + if ($matches[1] === 'db_prefix') { |
|
| 140 | + return $db_prefix; |
|
| 141 | + } |
|
| 138 | 142 | |
| 139 | - if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) |
|
| 140 | - return $user_info[$matches[1]]; |
|
| 143 | + if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) { |
|
| 144 | + return $user_info[$matches[1]]; |
|
| 145 | + } |
|
| 141 | 146 | |
| 142 | - if ($matches[1] === 'empty') |
|
| 143 | - return '\'\''; |
|
| 147 | + if ($matches[1] === 'empty') { |
|
| 148 | + return '\'\''; |
|
| 149 | + } |
|
| 144 | 150 | |
| 145 | - if (!isset($matches[2])) |
|
| 146 | - smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 151 | + if (!isset($matches[2])) { |
|
| 152 | + smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 153 | + } |
|
| 147 | 154 | |
| 148 | - if ($matches[1] === 'literal') |
|
| 149 | - return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 155 | + if ($matches[1] === 'literal') { |
|
| 156 | + return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 157 | + } |
|
| 150 | 158 | |
| 151 | - if (!isset($values[$matches[2]])) |
|
| 152 | - smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 159 | + if (!isset($values[$matches[2]])) { |
|
| 160 | + smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 161 | + } |
|
| 153 | 162 | |
| 154 | 163 | $replacement = $values[$matches[2]]; |
| 155 | 164 | |
| 156 | 165 | switch ($matches[1]) |
| 157 | 166 | { |
| 158 | 167 | case 'int': |
| 159 | - if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 160 | - smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 168 | + if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) { |
|
| 169 | + smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 170 | + } |
|
| 161 | 171 | return (string) (int) $replacement; |
| 162 | 172 | break; |
| 163 | 173 | |
@@ -169,65 +179,73 @@ discard block |
||
| 169 | 179 | case 'array_int': |
| 170 | 180 | if (is_array($replacement)) |
| 171 | 181 | { |
| 172 | - if (empty($replacement)) |
|
| 173 | - smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 182 | + if (empty($replacement)) { |
|
| 183 | + smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 184 | + } |
|
| 174 | 185 | |
| 175 | 186 | foreach ($replacement as $key => $value) |
| 176 | 187 | { |
| 177 | - if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 178 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 188 | + if (!is_numeric($value) || (string) $value !== (string) (int) $value) { |
|
| 189 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 190 | + } |
|
| 179 | 191 | |
| 180 | 192 | $replacement[$key] = (string) (int) $value; |
| 181 | 193 | } |
| 182 | 194 | |
| 183 | 195 | return implode(', ', $replacement); |
| 196 | + } else { |
|
| 197 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 184 | 198 | } |
| 185 | - else |
|
| 186 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 187 | 199 | |
| 188 | 200 | break; |
| 189 | 201 | |
| 190 | 202 | case 'array_string': |
| 191 | 203 | if (is_array($replacement)) |
| 192 | 204 | { |
| 193 | - if (empty($replacement)) |
|
| 194 | - smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 205 | + if (empty($replacement)) { |
|
| 206 | + smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 207 | + } |
|
| 195 | 208 | |
| 196 | - foreach ($replacement as $key => $value) |
|
| 197 | - $replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
|
| 209 | + foreach ($replacement as $key => $value) { |
|
| 210 | + $replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
|
| 211 | + } |
|
| 198 | 212 | |
| 199 | 213 | return implode(', ', $replacement); |
| 214 | + } else { |
|
| 215 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 200 | 216 | } |
| 201 | - else |
|
| 202 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 203 | 217 | break; |
| 204 | 218 | |
| 205 | 219 | case 'date': |
| 206 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
|
| 207 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 208 | - else |
|
| 209 | - smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 220 | + if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) { |
|
| 221 | + return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 222 | + } else { |
|
| 223 | + smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 224 | + } |
|
| 210 | 225 | break; |
| 211 | 226 | |
| 212 | 227 | case 'time': |
| 213 | - if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 214 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 215 | - else |
|
| 216 | - smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 228 | + if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) { |
|
| 229 | + return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 230 | + } else { |
|
| 231 | + smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 232 | + } |
|
| 217 | 233 | break; |
| 218 | 234 | |
| 219 | 235 | case 'datetime': |
| 220 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) |
|
| 221 | - return 'to_timestamp('. |
|
| 236 | + if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) { |
|
| 237 | + return 'to_timestamp('. |
|
| 222 | 238 | sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5] ,$datetime_matches[6]). |
| 223 | 239 | ',\'YYYY-MM-DD HH24:MI:SS\')'; |
| 224 | - else |
|
| 225 | - smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 240 | + } else { |
|
| 241 | + smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 242 | + } |
|
| 226 | 243 | break; |
| 227 | 244 | |
| 228 | 245 | case 'float': |
| 229 | - if (!is_numeric($replacement)) |
|
| 230 | - smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 246 | + if (!is_numeric($replacement)) { |
|
| 247 | + smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 248 | + } |
|
| 231 | 249 | return (string) (float) $replacement; |
| 232 | 250 | break; |
| 233 | 251 | |
@@ -240,31 +258,36 @@ discard block |
||
| 240 | 258 | break; |
| 241 | 259 | |
| 242 | 260 | case 'inet': |
| 243 | - if ($replacement == 'null' || $replacement == '') |
|
| 244 | - return 'null'; |
|
| 245 | - if (inet_pton($replacement) === false) |
|
| 246 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 261 | + if ($replacement == 'null' || $replacement == '') { |
|
| 262 | + return 'null'; |
|
| 263 | + } |
|
| 264 | + if (inet_pton($replacement) === false) { |
|
| 265 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 266 | + } |
|
| 247 | 267 | return sprintf('\'%1$s\'::inet', pg_escape_string($replacement)); |
| 248 | 268 | |
| 249 | 269 | case 'array_inet': |
| 250 | 270 | if (is_array($replacement)) |
| 251 | 271 | { |
| 252 | - if (empty($replacement)) |
|
| 253 | - smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 272 | + if (empty($replacement)) { |
|
| 273 | + smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 274 | + } |
|
| 254 | 275 | |
| 255 | 276 | foreach ($replacement as $key => $value) |
| 256 | 277 | { |
| 257 | - if ($replacement == 'null' || $replacement == '') |
|
| 258 | - $replacement[$key] = 'null'; |
|
| 259 | - if (!isValidIP($value)) |
|
| 260 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 278 | + if ($replacement == 'null' || $replacement == '') { |
|
| 279 | + $replacement[$key] = 'null'; |
|
| 280 | + } |
|
| 281 | + if (!isValidIP($value)) { |
|
| 282 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 283 | + } |
|
| 261 | 284 | $replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value)); |
| 262 | 285 | } |
| 263 | 286 | |
| 264 | 287 | return implode(', ', $replacement); |
| 288 | + } else { |
|
| 289 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 265 | 290 | } |
| 266 | - else |
|
| 267 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 268 | 291 | break; |
| 269 | 292 | |
| 270 | 293 | default: |
@@ -352,14 +375,16 @@ discard block |
||
| 352 | 375 | ), |
| 353 | 376 | ); |
| 354 | 377 | |
| 355 | - if (isset($replacements[$identifier])) |
|
| 356 | - $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 378 | + if (isset($replacements[$identifier])) { |
|
| 379 | + $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 380 | + } |
|
| 357 | 381 | |
| 358 | 382 | // Limits need to be a little different. |
| 359 | 383 | $db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string); |
| 360 | 384 | |
| 361 | - if (trim($db_string) == '') |
|
| 362 | - return false; |
|
| 385 | + if (trim($db_string) == '') { |
|
| 386 | + return false; |
|
| 387 | + } |
|
| 363 | 388 | |
| 364 | 389 | // Comments that are allowed in a query are preg_removed. |
| 365 | 390 | static $allowed_comments_from = array( |
@@ -379,8 +404,9 @@ discard block |
||
| 379 | 404 | $db_count = !isset($db_count) ? 1 : $db_count + 1; |
| 380 | 405 | $db_replace_result = 0; |
| 381 | 406 | |
| 382 | - if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) |
|
| 383 | - smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 407 | + if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) { |
|
| 408 | + smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 409 | + } |
|
| 384 | 410 | |
| 385 | 411 | if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
| 386 | 412 | { |
@@ -405,17 +431,18 @@ discard block |
||
| 405 | 431 | while (true) |
| 406 | 432 | { |
| 407 | 433 | $pos = strpos($db_string_1, '\'', $pos + 1); |
| 408 | - if ($pos === false) |
|
| 409 | - break; |
|
| 434 | + if ($pos === false) { |
|
| 435 | + break; |
|
| 436 | + } |
|
| 410 | 437 | $clean .= substr($db_string_1, $old_pos, $pos - $old_pos); |
| 411 | 438 | |
| 412 | 439 | while (true) |
| 413 | 440 | { |
| 414 | 441 | $pos1 = strpos($db_string_1, '\'', $pos + 1); |
| 415 | 442 | $pos2 = strpos($db_string_1, '\\', $pos + 1); |
| 416 | - if ($pos1 === false) |
|
| 417 | - break; |
|
| 418 | - elseif ($pos2 === false || $pos2 > $pos1) |
|
| 443 | + if ($pos1 === false) { |
|
| 444 | + break; |
|
| 445 | + } elseif ($pos2 === false || $pos2 > $pos1) |
|
| 419 | 446 | { |
| 420 | 447 | $pos = $pos1; |
| 421 | 448 | break; |
@@ -431,16 +458,19 @@ discard block |
||
| 431 | 458 | $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); |
| 432 | 459 | |
| 433 | 460 | // Comments? We don't use comments in our queries, we leave 'em outside! |
| 434 | - if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
|
| 435 | - $fail = true; |
|
| 461 | + if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) { |
|
| 462 | + $fail = true; |
|
| 463 | + } |
|
| 436 | 464 | // Trying to change passwords, slow us down, or something? |
| 437 | - elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) |
|
| 438 | - $fail = true; |
|
| 439 | - elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) |
|
| 440 | - $fail = true; |
|
| 465 | + elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) { |
|
| 466 | + $fail = true; |
|
| 467 | + } elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { |
|
| 468 | + $fail = true; |
|
| 469 | + } |
|
| 441 | 470 | |
| 442 | - if (!empty($fail) && function_exists('log_error')) |
|
| 443 | - smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 471 | + if (!empty($fail) && function_exists('log_error')) { |
|
| 472 | + smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 473 | + } |
|
| 444 | 474 | } |
| 445 | 475 | |
| 446 | 476 | // Set optimize stuff |
@@ -467,8 +497,9 @@ discard block |
||
| 467 | 497 | list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
| 468 | 498 | |
| 469 | 499 | // Initialize $db_cache if not already initialized. |
| 470 | - if (!isset($db_cache)) |
|
| 471 | - $db_cache = array(); |
|
| 500 | + if (!isset($db_cache)) { |
|
| 501 | + $db_cache = array(); |
|
| 502 | + } |
|
| 472 | 503 | |
| 473 | 504 | if (!empty($_SESSION['debug_redirect'])) |
| 474 | 505 | { |
@@ -486,12 +517,14 @@ discard block |
||
| 486 | 517 | |
| 487 | 518 | $db_last_result = @pg_query($connection, $db_string); |
| 488 | 519 | |
| 489 | - if ($db_last_result === false && empty($db_values['db_error_skip'])) |
|
| 490 | - $db_last_result = smf_db_error($db_string, $connection); |
|
| 520 | + if ($db_last_result === false && empty($db_values['db_error_skip'])) { |
|
| 521 | + $db_last_result = smf_db_error($db_string, $connection); |
|
| 522 | + } |
|
| 491 | 523 | |
| 492 | 524 | // Debugging. |
| 493 | - if (isset($db_show_debug) && $db_show_debug === true) |
|
| 494 | - $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 525 | + if (isset($db_show_debug) && $db_show_debug === true) { |
|
| 526 | + $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 527 | + } |
|
| 495 | 528 | |
| 496 | 529 | return $db_last_result; |
| 497 | 530 | } |
@@ -508,10 +541,11 @@ discard block |
||
| 508 | 541 | { |
| 509 | 542 | global $db_last_result, $db_replace_result; |
| 510 | 543 | |
| 511 | - if ($db_replace_result) |
|
| 512 | - return $db_replace_result; |
|
| 513 | - elseif ($result === null && !$db_last_result) |
|
| 514 | - return 0; |
|
| 544 | + if ($db_replace_result) { |
|
| 545 | + return $db_replace_result; |
|
| 546 | + } elseif ($result === null && !$db_last_result) { |
|
| 547 | + return 0; |
|
| 548 | + } |
|
| 515 | 549 | |
| 516 | 550 | return pg_affected_rows($result === null ? $db_last_result : $result); |
| 517 | 551 | } |
@@ -535,8 +569,9 @@ discard block |
||
| 535 | 569 | array( |
| 536 | 570 | ) |
| 537 | 571 | ); |
| 538 | - if (!$request) |
|
| 539 | - return false; |
|
| 572 | + if (!$request) { |
|
| 573 | + return false; |
|
| 574 | + } |
|
| 540 | 575 | list ($lastID) = $smcFunc['db_fetch_row']($request); |
| 541 | 576 | $smcFunc['db_free_result']($request); |
| 542 | 577 | |
@@ -557,12 +592,13 @@ discard block |
||
| 557 | 592 | // Decide which connection to use |
| 558 | 593 | $connection = $connection === null ? $db_connection : $connection; |
| 559 | 594 | |
| 560 | - if ($type == 'begin') |
|
| 561 | - return @pg_query($connection, 'BEGIN'); |
|
| 562 | - elseif ($type == 'rollback') |
|
| 563 | - return @pg_query($connection, 'ROLLBACK'); |
|
| 564 | - elseif ($type == 'commit') |
|
| 565 | - return @pg_query($connection, 'COMMIT'); |
|
| 595 | + if ($type == 'begin') { |
|
| 596 | + return @pg_query($connection, 'BEGIN'); |
|
| 597 | + } elseif ($type == 'rollback') { |
|
| 598 | + return @pg_query($connection, 'ROLLBACK'); |
|
| 599 | + } elseif ($type == 'commit') { |
|
| 600 | + return @pg_query($connection, 'COMMIT'); |
|
| 601 | + } |
|
| 566 | 602 | |
| 567 | 603 | return false; |
| 568 | 604 | } |
@@ -590,19 +626,22 @@ discard block |
||
| 590 | 626 | $query_error = @pg_last_error($connection); |
| 591 | 627 | |
| 592 | 628 | // Log the error. |
| 593 | - if (function_exists('log_error')) |
|
| 594 | - log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 629 | + if (function_exists('log_error')) { |
|
| 630 | + log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 631 | + } |
|
| 595 | 632 | |
| 596 | 633 | // Nothing's defined yet... just die with it. |
| 597 | - if (empty($context) || empty($txt)) |
|
| 598 | - die($query_error); |
|
| 634 | + if (empty($context) || empty($txt)) { |
|
| 635 | + die($query_error); |
|
| 636 | + } |
|
| 599 | 637 | |
| 600 | 638 | // Show an error message, if possible. |
| 601 | 639 | $context['error_title'] = $txt['database_error']; |
| 602 | - if (allowedTo('admin_forum')) |
|
| 603 | - $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 604 | - else |
|
| 605 | - $context['error_message'] = $txt['try_again']; |
|
| 640 | + if (allowedTo('admin_forum')) { |
|
| 641 | + $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 642 | + } else { |
|
| 643 | + $context['error_message'] = $txt['try_again']; |
|
| 644 | + } |
|
| 606 | 645 | |
| 607 | 646 | if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) |
| 608 | 647 | { |
@@ -624,12 +663,14 @@ discard block |
||
| 624 | 663 | { |
| 625 | 664 | global $db_row_count; |
| 626 | 665 | |
| 627 | - if ($counter !== false) |
|
| 628 | - return pg_fetch_row($request, $counter); |
|
| 666 | + if ($counter !== false) { |
|
| 667 | + return pg_fetch_row($request, $counter); |
|
| 668 | + } |
|
| 629 | 669 | |
| 630 | 670 | // Reset the row counter... |
| 631 | - if (!isset($db_row_count[(int) $request])) |
|
| 632 | - $db_row_count[(int) $request] = 0; |
|
| 671 | + if (!isset($db_row_count[(int) $request])) { |
|
| 672 | + $db_row_count[(int) $request] = 0; |
|
| 673 | + } |
|
| 633 | 674 | |
| 634 | 675 | // Return the right row. |
| 635 | 676 | return @pg_fetch_row($request, $db_row_count[(int) $request]++); |
@@ -646,12 +687,14 @@ discard block |
||
| 646 | 687 | { |
| 647 | 688 | global $db_row_count; |
| 648 | 689 | |
| 649 | - if ($counter !== false) |
|
| 650 | - return pg_fetch_assoc($request, $counter); |
|
| 690 | + if ($counter !== false) { |
|
| 691 | + return pg_fetch_assoc($request, $counter); |
|
| 692 | + } |
|
| 651 | 693 | |
| 652 | 694 | // Reset the row counter... |
| 653 | - if (!isset($db_row_count[(int) $request])) |
|
| 654 | - $db_row_count[(int) $request] = 0; |
|
| 695 | + if (!isset($db_row_count[(int) $request])) { |
|
| 696 | + $db_row_count[(int) $request] = 0; |
|
| 697 | + } |
|
| 655 | 698 | |
| 656 | 699 | // Return the right row. |
| 657 | 700 | return @pg_fetch_assoc($request, $db_row_count[(int) $request]++); |
@@ -704,11 +747,13 @@ discard block |
||
| 704 | 747 | |
| 705 | 748 | $replace = ''; |
| 706 | 749 | |
| 707 | - if (empty($data)) |
|
| 708 | - return; |
|
| 750 | + if (empty($data)) { |
|
| 751 | + return; |
|
| 752 | + } |
|
| 709 | 753 | |
| 710 | - if (!is_array($data[array_rand($data)])) |
|
| 711 | - $data = array($data); |
|
| 754 | + if (!is_array($data[array_rand($data)])) { |
|
| 755 | + $data = array($data); |
|
| 756 | + } |
|
| 712 | 757 | |
| 713 | 758 | // Replace the prefix holder with the actual prefix. |
| 714 | 759 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
@@ -734,32 +779,35 @@ discard block |
||
| 734 | 779 | $key_str .= ($count_pk > 0 ? ',' : ''); |
| 735 | 780 | $key_str .= $columnName; |
| 736 | 781 | $count_pk++; |
| 737 | - } |
|
| 738 | - else if ($method == 'replace') //normal field |
|
| 782 | + } else if ($method == 'replace') { |
|
| 783 | + //normal field |
|
| 739 | 784 | { |
| 740 | 785 | $col_str .= ($count > 0 ? ',' : ''); |
| 786 | + } |
|
| 741 | 787 | $col_str .= $columnName . ' = EXCLUDED.' . $columnName; |
| 742 | 788 | $count++; |
| 743 | 789 | } |
| 744 | 790 | } |
| 745 | - if ($method == 'replace') |
|
| 746 | - $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
|
| 747 | - else |
|
| 748 | - $replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
|
| 749 | - } |
|
| 750 | - else if ($method == 'replace') |
|
| 791 | + if ($method == 'replace') { |
|
| 792 | + $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
|
| 793 | + } else { |
|
| 794 | + $replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
|
| 795 | + } |
|
| 796 | + } else if ($method == 'replace') |
|
| 751 | 797 | { |
| 752 | 798 | foreach ($columns as $columnName => $type) |
| 753 | 799 | { |
| 754 | 800 | // Are we restricting the length? |
| 755 | - if (strpos($type, 'string-') !== false) |
|
| 756 | - $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 757 | - else |
|
| 758 | - $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 801 | + if (strpos($type, 'string-') !== false) { |
|
| 802 | + $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 803 | + } else { |
|
| 804 | + $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 805 | + } |
|
| 759 | 806 | |
| 760 | 807 | // A key? That's what we were looking for. |
| 761 | - if (in_array($columnName, $keys)) |
|
| 762 | - $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 808 | + if (in_array($columnName, $keys)) { |
|
| 809 | + $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 810 | + } |
|
| 763 | 811 | $count++; |
| 764 | 812 | } |
| 765 | 813 | |
@@ -795,10 +843,11 @@ discard block |
||
| 795 | 843 | foreach ($columns as $columnName => $type) |
| 796 | 844 | { |
| 797 | 845 | // Are we restricting the length? |
| 798 | - if (strpos($type, 'string-') !== false) |
|
| 799 | - $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 800 | - else |
|
| 801 | - $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 846 | + if (strpos($type, 'string-') !== false) { |
|
| 847 | + $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 848 | + } else { |
|
| 849 | + $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 850 | + } |
|
| 802 | 851 | } |
| 803 | 852 | $insertData = substr($insertData, 0, -2) . ')'; |
| 804 | 853 | |
@@ -807,8 +856,9 @@ discard block |
||
| 807 | 856 | |
| 808 | 857 | // Here's where the variables are injected to the query. |
| 809 | 858 | $insertRows = array(); |
| 810 | - foreach ($data as $dataRow) |
|
| 811 | - $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 859 | + foreach ($data as $dataRow) { |
|
| 860 | + $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 861 | + } |
|
| 812 | 862 | |
| 813 | 863 | // Do the insert. |
| 814 | 864 | $request = $smcFunc['db_query']('', ' |
@@ -825,19 +875,21 @@ discard block |
||
| 825 | 875 | |
| 826 | 876 | if ($with_returning && $request !== false) |
| 827 | 877 | { |
| 828 | - if ($returnmode === 2) |
|
| 829 | - $return_var = array(); |
|
| 878 | + if ($returnmode === 2) { |
|
| 879 | + $return_var = array(); |
|
| 880 | + } |
|
| 830 | 881 | |
| 831 | 882 | while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
| 832 | 883 | { |
| 833 | - if (is_numeric($row[0])) // try to emulate mysql limitation |
|
| 884 | + if (is_numeric($row[0])) { |
|
| 885 | + // try to emulate mysql limitation |
|
| 834 | 886 | { |
| 835 | 887 | if ($returnmode === 1) |
| 836 | 888 | $return_var = $row[0]; |
| 837 | - elseif ($returnmode === 2) |
|
| 838 | - $return_var[] = $row[0]; |
|
| 839 | - } |
|
| 840 | - else |
|
| 889 | + } elseif ($returnmode === 2) { |
|
| 890 | + $return_var[] = $row[0]; |
|
| 891 | + } |
|
| 892 | + } else |
|
| 841 | 893 | { |
| 842 | 894 | $with_returning = false; |
| 843 | 895 | trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); |
@@ -846,9 +898,10 @@ discard block |
||
| 846 | 898 | } |
| 847 | 899 | } |
| 848 | 900 | |
| 849 | - if ($with_returning && !empty($return_var)) |
|
| 850 | - return $return_var; |
|
| 851 | -} |
|
| 901 | + if ($with_returning && !empty($return_var)) { |
|
| 902 | + return $return_var; |
|
| 903 | + } |
|
| 904 | + } |
|
| 852 | 905 | |
| 853 | 906 | /** |
| 854 | 907 | * Dummy function really. Doesn't do anything on PostgreSQL. |
@@ -885,8 +938,9 @@ discard block |
||
| 885 | 938 | */ |
| 886 | 939 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
| 887 | 940 | { |
| 888 | - if (empty($log_message)) |
|
| 889 | - $log_message = $error_message; |
|
| 941 | + if (empty($log_message)) { |
|
| 942 | + $log_message = $error_message; |
|
| 943 | + } |
|
| 890 | 944 | |
| 891 | 945 | foreach (debug_backtrace() as $step) |
| 892 | 946 | { |
@@ -905,12 +959,14 @@ discard block |
||
| 905 | 959 | } |
| 906 | 960 | |
| 907 | 961 | // A special case - we want the file and line numbers for debugging. |
| 908 | - if ($error_type == 'return') |
|
| 909 | - return array($file, $line); |
|
| 962 | + if ($error_type == 'return') { |
|
| 963 | + return array($file, $line); |
|
| 964 | + } |
|
| 910 | 965 | |
| 911 | 966 | // Is always a critical error. |
| 912 | - if (function_exists('log_error')) |
|
| 913 | - log_error($log_message, 'critical', $file, $line); |
|
| 967 | + if (function_exists('log_error')) { |
|
| 968 | + log_error($log_message, 'critical', $file, $line); |
|
| 969 | + } |
|
| 914 | 970 | |
| 915 | 971 | if (function_exists('fatal_error')) |
| 916 | 972 | { |
@@ -918,12 +974,12 @@ discard block |
||
| 918 | 974 | |
| 919 | 975 | // Cannot continue... |
| 920 | 976 | exit; |
| 977 | + } elseif ($error_type) { |
|
| 978 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 979 | + } else { |
|
| 980 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 981 | + } |
|
| 921 | 982 | } |
| 922 | - elseif ($error_type) |
|
| 923 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 924 | - else |
|
| 925 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 926 | -} |
|
| 927 | 983 | |
| 928 | 984 | /** |
| 929 | 985 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
@@ -940,10 +996,11 @@ discard block |
||
| 940 | 996 | '\\' => '\\\\', |
| 941 | 997 | ); |
| 942 | 998 | |
| 943 | - if ($translate_human_wildcards) |
|
| 944 | - $replacements += array( |
|
| 999 | + if ($translate_human_wildcards) { |
|
| 1000 | + $replacements += array( |
|
| 945 | 1001 | '*' => '%', |
| 946 | 1002 | ); |
| 1003 | + } |
|
| 947 | 1004 | |
| 948 | 1005 | return strtr($string, $replacements); |
| 949 | 1006 | } |
@@ -972,14 +1029,16 @@ discard block |
||
| 972 | 1029 | static $pg_error_data_prep; |
| 973 | 1030 | |
| 974 | 1031 | // without database we can't do anything |
| 975 | - if (empty($db_connection)) |
|
| 976 | - return; |
|
| 1032 | + if (empty($db_connection)) { |
|
| 1033 | + return; |
|
| 1034 | + } |
|
| 977 | 1035 | |
| 978 | - if (empty($pg_error_data_prep)) |
|
| 979 | - $pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
|
| 1036 | + if (empty($pg_error_data_prep)) { |
|
| 1037 | + $pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
|
| 980 | 1038 | 'INSERT INTO ' . $db_prefix . 'log_errors(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace) |
| 981 | 1039 | VALUES( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' |
| 982 | 1040 | ); |
| 1041 | + } |
|
| 983 | 1042 | |
| 984 | 1043 | pg_execute($db_connection, 'smf_log_errors', $error_array); |
| 985 | 1044 | } |
@@ -999,8 +1058,9 @@ discard block |
||
| 999 | 1058 | $count = count($array_values); |
| 1000 | 1059 | $then = ($desc ? ' THEN -' : ' THEN '); |
| 1001 | 1060 | |
| 1002 | - for ($i = 0; $i < $count; $i++) |
|
| 1003 | - $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1061 | + for ($i = 0; $i < $count; $i++) { |
|
| 1062 | + $return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
|
| 1063 | + } |
|
| 1004 | 1064 | |
| 1005 | 1065 | $return .= 'END'; |
| 1006 | 1066 | return $return; |
@@ -1023,11 +1083,13 @@ discard block |
||
| 1023 | 1083 | //pg 9.5 got replace support |
| 1024 | 1084 | $pg_version = $smcFunc['db_get_version'](); |
| 1025 | 1085 | // if we got a Beta Version |
| 1026 | - if (stripos($pg_version, 'beta') !== false) |
|
| 1027 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 1086 | + if (stripos($pg_version, 'beta') !== false) { |
|
| 1087 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 1088 | + } |
|
| 1028 | 1089 | // or RC |
| 1029 | - if (stripos($pg_version, 'rc') !== false) |
|
| 1030 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 1090 | + if (stripos($pg_version, 'rc') !== false) { |
|
| 1091 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 1092 | + } |
|
| 1031 | 1093 | |
| 1032 | 1094 | $replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); |
| 1033 | 1095 | } |