@@ -197,22 +197,22 @@ discard block |
||
| 197 | 197 | |
| 198 | 198 | case 'date': |
| 199 | 199 | if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
| 200 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 200 | + return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]) . '::date'; |
|
| 201 | 201 | else |
| 202 | 202 | smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
| 203 | 203 | break; |
| 204 | 204 | |
| 205 | 205 | case 'time': |
| 206 | 206 | if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
| 207 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 207 | + return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]) . '::time'; |
|
| 208 | 208 | else |
| 209 | 209 | smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
| 210 | 210 | break; |
| 211 | 211 | |
| 212 | 212 | case 'datetime': |
| 213 | 213 | 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) |
| 214 | - return 'to_timestamp('. |
|
| 215 | - 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]). |
|
| 214 | + return 'to_timestamp(' . |
|
| 215 | + 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]) . |
|
| 216 | 216 | ',\'YYYY-MM-DD HH24:MI:SS\')'; |
| 217 | 217 | else |
| 218 | 218 | smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
@@ -764,10 +764,10 @@ discard block |
||
| 764 | 764 | $returning = ''; |
| 765 | 765 | $with_returning = false; |
| 766 | 766 | // lets build the returning string, mysql allow only in normal mode |
| 767 | - if(!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0) |
|
| 767 | + if (!empty($keys) && (count($keys) > 0) && $method == '' && $returnmode > 0) |
|
| 768 | 768 | { |
| 769 | 769 | // we only take the first key |
| 770 | - $returning = ' RETURNING '.$keys[0]; |
|
| 770 | + $returning = ' RETURNING ' . $keys[0]; |
|
| 771 | 771 | $with_returning = true; |
| 772 | 772 | } |
| 773 | 773 | |
@@ -798,7 +798,7 @@ discard block |
||
| 798 | 798 | INSERT INTO ' . $table . '("' . implode('", "', $indexed_columns) . '") |
| 799 | 799 | VALUES |
| 800 | 800 | ' . implode(', |
| 801 | - ', $insertRows).$replace.$returning, |
|
| 801 | + ', $insertRows) . $replace . $returning, |
|
| 802 | 802 | array( |
| 803 | 803 | 'security_override' => true, |
| 804 | 804 | 'db_error_skip' => $method == 'ignore' || $table === $db_prefix . 'log_errors', |
@@ -811,7 +811,7 @@ discard block |
||
| 811 | 811 | if ($returnmode === 2) |
| 812 | 812 | $return_var = array(); |
| 813 | 813 | |
| 814 | - while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
|
| 814 | + while (($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
|
| 815 | 815 | { |
| 816 | 816 | if (is_numeric($row[0])) // try to emulate mysql limitation |
| 817 | 817 | { |
@@ -13,8 +13,9 @@ discard block |
||
| 13 | 13 | * @version 2.1 Beta 3 |
| 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', |
@@ -59,11 +60,13 @@ discard block |
||
| 59 | 60 | 'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string', |
| 60 | 61 | 'db_is_resource' => 'is_resource', |
| 61 | 62 | ); |
| 63 | + } |
|
| 62 | 64 | |
| 63 | - if (!empty($db_options['persist'])) |
|
| 64 | - $connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 65 | - else |
|
| 66 | - $connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 65 | + if (!empty($db_options['persist'])) { |
|
| 66 | + $connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 67 | + } else { |
|
| 68 | + $connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 69 | + } |
|
| 67 | 70 | |
| 68 | 71 | // Something's wrong, show an error if its fatal (which we assume it is) |
| 69 | 72 | if (!$connection) |
@@ -71,8 +74,7 @@ discard block |
||
| 71 | 74 | if (!empty($db_options['non_fatal'])) |
| 72 | 75 | { |
| 73 | 76 | return null; |
| 74 | - } |
|
| 75 | - else |
|
| 77 | + } else |
|
| 76 | 78 | { |
| 77 | 79 | display_db_error(); |
| 78 | 80 | } |
@@ -123,34 +125,42 @@ discard block |
||
| 123 | 125 | |
| 124 | 126 | list ($values, $connection) = $db_callback; |
| 125 | 127 | |
| 126 | - if ($matches[1] === 'db_prefix') |
|
| 127 | - return $db_prefix; |
|
| 128 | + if ($matches[1] === 'db_prefix') { |
|
| 129 | + return $db_prefix; |
|
| 130 | + } |
|
| 128 | 131 | |
| 129 | - if ($matches[1] === 'query_see_board') |
|
| 130 | - return $user_info['query_see_board']; |
|
| 132 | + if ($matches[1] === 'query_see_board') { |
|
| 133 | + return $user_info['query_see_board']; |
|
| 134 | + } |
|
| 131 | 135 | |
| 132 | - if ($matches[1] === 'query_wanna_see_board') |
|
| 133 | - return $user_info['query_wanna_see_board']; |
|
| 136 | + if ($matches[1] === 'query_wanna_see_board') { |
|
| 137 | + return $user_info['query_wanna_see_board']; |
|
| 138 | + } |
|
| 134 | 139 | |
| 135 | - if ($matches[1] === 'empty') |
|
| 136 | - return '\'\''; |
|
| 140 | + if ($matches[1] === 'empty') { |
|
| 141 | + return '\'\''; |
|
| 142 | + } |
|
| 137 | 143 | |
| 138 | - if (!isset($matches[2])) |
|
| 139 | - 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 | + } |
|
| 140 | 147 | |
| 141 | - if ($matches[1] === 'literal') |
|
| 142 | - return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 148 | + if ($matches[1] === 'literal') { |
|
| 149 | + return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 150 | + } |
|
| 143 | 151 | |
| 144 | - if (!isset($values[$matches[2]])) |
|
| 145 | - 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 | + } |
|
| 146 | 155 | |
| 147 | 156 | $replacement = $values[$matches[2]]; |
| 148 | 157 | |
| 149 | 158 | switch ($matches[1]) |
| 150 | 159 | { |
| 151 | 160 | case 'int': |
| 152 | - if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 153 | - 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 | + } |
|
| 154 | 164 | return (string) (int) $replacement; |
| 155 | 165 | break; |
| 156 | 166 | |
@@ -162,65 +172,73 @@ discard block |
||
| 162 | 172 | case 'array_int': |
| 163 | 173 | if (is_array($replacement)) |
| 164 | 174 | { |
| 165 | - if (empty($replacement)) |
|
| 166 | - 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 | + } |
|
| 167 | 178 | |
| 168 | 179 | foreach ($replacement as $key => $value) |
| 169 | 180 | { |
| 170 | - if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 171 | - 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 | + } |
|
| 172 | 184 | |
| 173 | 185 | $replacement[$key] = (string) (int) $value; |
| 174 | 186 | } |
| 175 | 187 | |
| 176 | 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__); |
|
| 177 | 191 | } |
| 178 | - else |
|
| 179 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 180 | 192 | |
| 181 | 193 | break; |
| 182 | 194 | |
| 183 | 195 | case 'array_string': |
| 184 | 196 | if (is_array($replacement)) |
| 185 | 197 | { |
| 186 | - if (empty($replacement)) |
|
| 187 | - 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 | + } |
|
| 188 | 201 | |
| 189 | - foreach ($replacement as $key => $value) |
|
| 190 | - $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 | + } |
|
| 191 | 205 | |
| 192 | 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__); |
|
| 193 | 209 | } |
| 194 | - else |
|
| 195 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 196 | 210 | break; |
| 197 | 211 | |
| 198 | 212 | case 'date': |
| 199 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
|
| 200 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 201 | - else |
|
| 202 | - 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 | + } |
|
| 203 | 218 | break; |
| 204 | 219 | |
| 205 | 220 | case 'time': |
| 206 | - if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 207 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 208 | - else |
|
| 209 | - 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 | + } |
|
| 210 | 226 | break; |
| 211 | 227 | |
| 212 | 228 | case 'datetime': |
| 213 | - 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) |
|
| 214 | - 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('. |
|
| 215 | 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]). |
| 216 | 232 | ',\'YYYY-MM-DD HH24:MI:SS\')'; |
| 217 | - else |
|
| 218 | - 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 | + } |
|
| 219 | 236 | break; |
| 220 | 237 | |
| 221 | 238 | case 'float': |
| 222 | - if (!is_numeric($replacement)) |
|
| 223 | - 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 | + } |
|
| 224 | 242 | return (string) (float) $replacement; |
| 225 | 243 | break; |
| 226 | 244 | |
@@ -233,31 +251,36 @@ discard block |
||
| 233 | 251 | break; |
| 234 | 252 | |
| 235 | 253 | case 'inet': |
| 236 | - if ($replacement == 'null' || $replacement == '') |
|
| 237 | - return 'null'; |
|
| 238 | - if (inet_pton($replacement) === false) |
|
| 239 | - 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 | + } |
|
| 240 | 260 | return sprintf('\'%1$s\'::inet', pg_escape_string($replacement)); |
| 241 | 261 | |
| 242 | 262 | case 'array_inet': |
| 243 | 263 | if (is_array($replacement)) |
| 244 | 264 | { |
| 245 | - if (empty($replacement)) |
|
| 246 | - 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 | + } |
|
| 247 | 268 | |
| 248 | 269 | foreach ($replacement as $key => $value) |
| 249 | 270 | { |
| 250 | - if ($replacement == 'null' || $replacement == '') |
|
| 251 | - $replacement[$key] = 'null'; |
|
| 252 | - if (!isValidIP($value)) |
|
| 253 | - 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 | + } |
|
| 254 | 277 | $replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value)); |
| 255 | 278 | } |
| 256 | 279 | |
| 257 | 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__); |
|
| 258 | 283 | } |
| 259 | - else |
|
| 260 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 261 | 284 | break; |
| 262 | 285 | |
| 263 | 286 | default: |
@@ -344,14 +367,16 @@ discard block |
||
| 344 | 367 | ), |
| 345 | 368 | ); |
| 346 | 369 | |
| 347 | - if (isset($replacements[$identifier])) |
|
| 348 | - $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 370 | + if (isset($replacements[$identifier])) { |
|
| 371 | + $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 372 | + } |
|
| 349 | 373 | |
| 350 | 374 | // Limits need to be a little different. |
| 351 | 375 | $db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string); |
| 352 | 376 | |
| 353 | - if (trim($db_string) == '') |
|
| 354 | - return false; |
|
| 377 | + if (trim($db_string) == '') { |
|
| 378 | + return false; |
|
| 379 | + } |
|
| 355 | 380 | |
| 356 | 381 | // Comments that are allowed in a query are preg_removed. |
| 357 | 382 | static $allowed_comments_from = array( |
@@ -371,8 +396,9 @@ discard block |
||
| 371 | 396 | $db_count = !isset($db_count) ? 1 : $db_count + 1; |
| 372 | 397 | $db_replace_result = 0; |
| 373 | 398 | |
| 374 | - if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) |
|
| 375 | - smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 399 | + if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) { |
|
| 400 | + smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 401 | + } |
|
| 376 | 402 | |
| 377 | 403 | if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
| 378 | 404 | { |
@@ -393,8 +419,9 @@ discard block |
||
| 393 | 419 | list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
| 394 | 420 | |
| 395 | 421 | // Initialize $db_cache if not already initialized. |
| 396 | - if (!isset($db_cache)) |
|
| 397 | - $db_cache = array(); |
|
| 422 | + if (!isset($db_cache)) { |
|
| 423 | + $db_cache = array(); |
|
| 424 | + } |
|
| 398 | 425 | |
| 399 | 426 | if (!empty($_SESSION['debug_redirect'])) |
| 400 | 427 | { |
@@ -420,17 +447,18 @@ discard block |
||
| 420 | 447 | while (true) |
| 421 | 448 | { |
| 422 | 449 | $pos = strpos($db_string, '\'', $pos + 1); |
| 423 | - if ($pos === false) |
|
| 424 | - break; |
|
| 450 | + if ($pos === false) { |
|
| 451 | + break; |
|
| 452 | + } |
|
| 425 | 453 | $clean .= substr($db_string, $old_pos, $pos - $old_pos); |
| 426 | 454 | |
| 427 | 455 | while (true) |
| 428 | 456 | { |
| 429 | 457 | $pos1 = strpos($db_string, '\'', $pos + 1); |
| 430 | 458 | $pos2 = strpos($db_string, '\\', $pos + 1); |
| 431 | - if ($pos1 === false) |
|
| 432 | - break; |
|
| 433 | - elseif ($pos2 === false || $pos2 > $pos1) |
|
| 459 | + if ($pos1 === false) { |
|
| 460 | + break; |
|
| 461 | + } elseif ($pos2 === false || $pos2 > $pos1) |
|
| 434 | 462 | { |
| 435 | 463 | $pos = $pos1; |
| 436 | 464 | break; |
@@ -446,26 +474,31 @@ discard block |
||
| 446 | 474 | $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); |
| 447 | 475 | |
| 448 | 476 | // Comments? We don't use comments in our queries, we leave 'em outside! |
| 449 | - if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
|
| 450 | - $fail = true; |
|
| 477 | + if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) { |
|
| 478 | + $fail = true; |
|
| 479 | + } |
|
| 451 | 480 | // Trying to change passwords, slow us down, or something? |
| 452 | - elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) |
|
| 453 | - $fail = true; |
|
| 454 | - elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) |
|
| 455 | - $fail = true; |
|
| 481 | + elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) { |
|
| 482 | + $fail = true; |
|
| 483 | + } elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { |
|
| 484 | + $fail = true; |
|
| 485 | + } |
|
| 456 | 486 | |
| 457 | - if (!empty($fail) && function_exists('log_error')) |
|
| 458 | - smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 487 | + if (!empty($fail) && function_exists('log_error')) { |
|
| 488 | + smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 489 | + } |
|
| 459 | 490 | } |
| 460 | 491 | |
| 461 | 492 | $db_last_result = @pg_query($connection, $db_string); |
| 462 | 493 | |
| 463 | - if ($db_last_result === false && empty($db_values['db_error_skip'])) |
|
| 464 | - $db_last_result = smf_db_error($db_string, $connection); |
|
| 494 | + if ($db_last_result === false && empty($db_values['db_error_skip'])) { |
|
| 495 | + $db_last_result = smf_db_error($db_string, $connection); |
|
| 496 | + } |
|
| 465 | 497 | |
| 466 | 498 | // Debugging. |
| 467 | - if (isset($db_show_debug) && $db_show_debug === true) |
|
| 468 | - $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 499 | + if (isset($db_show_debug) && $db_show_debug === true) { |
|
| 500 | + $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 501 | + } |
|
| 469 | 502 | |
| 470 | 503 | return $db_last_result; |
| 471 | 504 | } |
@@ -478,10 +511,11 @@ discard block |
||
| 478 | 511 | { |
| 479 | 512 | global $db_last_result, $db_replace_result; |
| 480 | 513 | |
| 481 | - if ($db_replace_result) |
|
| 482 | - return $db_replace_result; |
|
| 483 | - elseif ($result === null && !$db_last_result) |
|
| 484 | - return 0; |
|
| 514 | + if ($db_replace_result) { |
|
| 515 | + return $db_replace_result; |
|
| 516 | + } elseif ($result === null && !$db_last_result) { |
|
| 517 | + return 0; |
|
| 518 | + } |
|
| 485 | 519 | |
| 486 | 520 | return pg_affected_rows($result === null ? $db_last_result : $result); |
| 487 | 521 | } |
@@ -505,8 +539,9 @@ discard block |
||
| 505 | 539 | array( |
| 506 | 540 | ) |
| 507 | 541 | ); |
| 508 | - if (!$request) |
|
| 509 | - return false; |
|
| 542 | + if (!$request) { |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 510 | 545 | list ($lastID) = $smcFunc['db_fetch_row']($request); |
| 511 | 546 | $smcFunc['db_free_result']($request); |
| 512 | 547 | |
@@ -527,12 +562,13 @@ discard block |
||
| 527 | 562 | // Decide which connection to use |
| 528 | 563 | $connection = $connection === null ? $db_connection : $connection; |
| 529 | 564 | |
| 530 | - if ($type == 'begin') |
|
| 531 | - return @pg_query($connection, 'BEGIN'); |
|
| 532 | - elseif ($type == 'rollback') |
|
| 533 | - return @pg_query($connection, 'ROLLBACK'); |
|
| 534 | - elseif ($type == 'commit') |
|
| 535 | - return @pg_query($connection, 'COMMIT'); |
|
| 565 | + if ($type == 'begin') { |
|
| 566 | + return @pg_query($connection, 'BEGIN'); |
|
| 567 | + } elseif ($type == 'rollback') { |
|
| 568 | + return @pg_query($connection, 'ROLLBACK'); |
|
| 569 | + } elseif ($type == 'commit') { |
|
| 570 | + return @pg_query($connection, 'COMMIT'); |
|
| 571 | + } |
|
| 536 | 572 | |
| 537 | 573 | return false; |
| 538 | 574 | } |
@@ -560,19 +596,22 @@ discard block |
||
| 560 | 596 | $query_error = @pg_last_error($connection); |
| 561 | 597 | |
| 562 | 598 | // Log the error. |
| 563 | - if (function_exists('log_error')) |
|
| 564 | - log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 599 | + if (function_exists('log_error')) { |
|
| 600 | + log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 601 | + } |
|
| 565 | 602 | |
| 566 | 603 | // Nothing's defined yet... just die with it. |
| 567 | - if (empty($context) || empty($txt)) |
|
| 568 | - die($query_error); |
|
| 604 | + if (empty($context) || empty($txt)) { |
|
| 605 | + die($query_error); |
|
| 606 | + } |
|
| 569 | 607 | |
| 570 | 608 | // Show an error message, if possible. |
| 571 | 609 | $context['error_title'] = $txt['database_error']; |
| 572 | - if (allowedTo('admin_forum')) |
|
| 573 | - $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 574 | - else |
|
| 575 | - $context['error_message'] = $txt['try_again']; |
|
| 610 | + if (allowedTo('admin_forum')) { |
|
| 611 | + $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 612 | + } else { |
|
| 613 | + $context['error_message'] = $txt['try_again']; |
|
| 614 | + } |
|
| 576 | 615 | |
| 577 | 616 | if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) |
| 578 | 617 | { |
@@ -594,12 +633,14 @@ discard block |
||
| 594 | 633 | { |
| 595 | 634 | global $db_row_count; |
| 596 | 635 | |
| 597 | - if ($counter !== false) |
|
| 598 | - return pg_fetch_row($request, $counter); |
|
| 636 | + if ($counter !== false) { |
|
| 637 | + return pg_fetch_row($request, $counter); |
|
| 638 | + } |
|
| 599 | 639 | |
| 600 | 640 | // Reset the row counter... |
| 601 | - if (!isset($db_row_count[(int) $request])) |
|
| 602 | - $db_row_count[(int) $request] = 0; |
|
| 641 | + if (!isset($db_row_count[(int) $request])) { |
|
| 642 | + $db_row_count[(int) $request] = 0; |
|
| 643 | + } |
|
| 603 | 644 | |
| 604 | 645 | // Return the right row. |
| 605 | 646 | return @pg_fetch_row($request, $db_row_count[(int) $request]++); |
@@ -616,12 +657,14 @@ discard block |
||
| 616 | 657 | { |
| 617 | 658 | global $db_row_count; |
| 618 | 659 | |
| 619 | - if ($counter !== false) |
|
| 620 | - return pg_fetch_assoc($request, $counter); |
|
| 660 | + if ($counter !== false) { |
|
| 661 | + return pg_fetch_assoc($request, $counter); |
|
| 662 | + } |
|
| 621 | 663 | |
| 622 | 664 | // Reset the row counter... |
| 623 | - if (!isset($db_row_count[(int) $request])) |
|
| 624 | - $db_row_count[(int) $request] = 0; |
|
| 665 | + if (!isset($db_row_count[(int) $request])) { |
|
| 666 | + $db_row_count[(int) $request] = 0; |
|
| 667 | + } |
|
| 625 | 668 | |
| 626 | 669 | // Return the right row. |
| 627 | 670 | return @pg_fetch_assoc($request, $db_row_count[(int) $request]++); |
@@ -674,11 +717,13 @@ discard block |
||
| 674 | 717 | |
| 675 | 718 | $replace = ''; |
| 676 | 719 | |
| 677 | - if (empty($data)) |
|
| 678 | - return; |
|
| 720 | + if (empty($data)) { |
|
| 721 | + return; |
|
| 722 | + } |
|
| 679 | 723 | |
| 680 | - if (!is_array($data[array_rand($data)])) |
|
| 681 | - $data = array($data); |
|
| 724 | + if (!is_array($data[array_rand($data)])) { |
|
| 725 | + $data = array($data); |
|
| 726 | + } |
|
| 682 | 727 | |
| 683 | 728 | // Replace the prefix holder with the actual prefix. |
| 684 | 729 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
@@ -697,11 +742,13 @@ discard block |
||
| 697 | 742 | //pg 9.5 got replace support |
| 698 | 743 | $pg_version = $smcFunc['db_get_version'](); |
| 699 | 744 | // if we got a Beta Version |
| 700 | - if (stripos($pg_version, 'beta') !== false) |
|
| 701 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 745 | + if (stripos($pg_version, 'beta') !== false) { |
|
| 746 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 747 | + } |
|
| 702 | 748 | // or RC |
| 703 | - if (stripos($pg_version, 'rc') !== false) |
|
| 704 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 749 | + if (stripos($pg_version, 'rc') !== false) { |
|
| 750 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 751 | + } |
|
| 705 | 752 | |
| 706 | 753 | $replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); |
| 707 | 754 | } |
@@ -720,8 +767,7 @@ discard block |
||
| 720 | 767 | $key_str .= ($count_pk > 0 ? ',' : ''); |
| 721 | 768 | $key_str .= $columnName; |
| 722 | 769 | $count_pk++; |
| 723 | - } |
|
| 724 | - else //normal field |
|
| 770 | + } else //normal field |
|
| 725 | 771 | { |
| 726 | 772 | $col_str .= ($count > 0 ? ',' : ''); |
| 727 | 773 | $col_str .= $columnName . ' = EXCLUDED.' . $columnName; |
@@ -729,20 +775,21 @@ discard block |
||
| 729 | 775 | } |
| 730 | 776 | } |
| 731 | 777 | $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
| 732 | - } |
|
| 733 | - else |
|
| 778 | + } else |
|
| 734 | 779 | { |
| 735 | 780 | foreach ($columns as $columnName => $type) |
| 736 | 781 | { |
| 737 | 782 | // Are we restricting the length? |
| 738 | - if (strpos($type, 'string-') !== false) |
|
| 739 | - $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 740 | - else |
|
| 741 | - $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 783 | + if (strpos($type, 'string-') !== false) { |
|
| 784 | + $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 785 | + } else { |
|
| 786 | + $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 787 | + } |
|
| 742 | 788 | |
| 743 | 789 | // A key? That's what we were looking for. |
| 744 | - if (in_array($columnName, $keys)) |
|
| 745 | - $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 790 | + if (in_array($columnName, $keys)) { |
|
| 791 | + $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 792 | + } |
|
| 746 | 793 | $count++; |
| 747 | 794 | } |
| 748 | 795 | |
@@ -778,10 +825,11 @@ discard block |
||
| 778 | 825 | foreach ($columns as $columnName => $type) |
| 779 | 826 | { |
| 780 | 827 | // Are we restricting the length? |
| 781 | - if (strpos($type, 'string-') !== false) |
|
| 782 | - $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 783 | - else |
|
| 784 | - $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 828 | + if (strpos($type, 'string-') !== false) { |
|
| 829 | + $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 830 | + } else { |
|
| 831 | + $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 832 | + } |
|
| 785 | 833 | } |
| 786 | 834 | $insertData = substr($insertData, 0, -2) . ')'; |
| 787 | 835 | |
@@ -790,8 +838,9 @@ discard block |
||
| 790 | 838 | |
| 791 | 839 | // Here's where the variables are injected to the query. |
| 792 | 840 | $insertRows = array(); |
| 793 | - foreach ($data as $dataRow) |
|
| 794 | - $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 841 | + foreach ($data as $dataRow) { |
|
| 842 | + $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 843 | + } |
|
| 795 | 844 | |
| 796 | 845 | // Do the insert. |
| 797 | 846 | $request = $smcFunc['db_query']('', ' |
@@ -808,19 +857,21 @@ discard block |
||
| 808 | 857 | |
| 809 | 858 | if ($with_returning && $request !== false) |
| 810 | 859 | { |
| 811 | - if ($returnmode === 2) |
|
| 812 | - $return_var = array(); |
|
| 860 | + if ($returnmode === 2) { |
|
| 861 | + $return_var = array(); |
|
| 862 | + } |
|
| 813 | 863 | |
| 814 | 864 | while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
| 815 | 865 | { |
| 816 | - if (is_numeric($row[0])) // try to emulate mysql limitation |
|
| 866 | + if (is_numeric($row[0])) { |
|
| 867 | + // try to emulate mysql limitation |
|
| 817 | 868 | { |
| 818 | 869 | if ($returnmode === 1) |
| 819 | 870 | $return_var = $row[0]; |
| 820 | - elseif ($returnmode === 2) |
|
| 821 | - $return_var[] = $row[0]; |
|
| 822 | - } |
|
| 823 | - else |
|
| 871 | + } elseif ($returnmode === 2) { |
|
| 872 | + $return_var[] = $row[0]; |
|
| 873 | + } |
|
| 874 | + } else |
|
| 824 | 875 | { |
| 825 | 876 | $with_returning = false; |
| 826 | 877 | trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); |
@@ -829,9 +880,10 @@ discard block |
||
| 829 | 880 | } |
| 830 | 881 | } |
| 831 | 882 | |
| 832 | - if ($with_returning && !empty($return_var)) |
|
| 833 | - return $return_var; |
|
| 834 | -} |
|
| 883 | + if ($with_returning && !empty($return_var)) { |
|
| 884 | + return $return_var; |
|
| 885 | + } |
|
| 886 | + } |
|
| 835 | 887 | |
| 836 | 888 | /** |
| 837 | 889 | * Dummy function really. Doesn't do anything on PostgreSQL. |
@@ -868,8 +920,9 @@ discard block |
||
| 868 | 920 | */ |
| 869 | 921 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
| 870 | 922 | { |
| 871 | - if (empty($log_message)) |
|
| 872 | - $log_message = $error_message; |
|
| 923 | + if (empty($log_message)) { |
|
| 924 | + $log_message = $error_message; |
|
| 925 | + } |
|
| 873 | 926 | |
| 874 | 927 | foreach (debug_backtrace() as $step) |
| 875 | 928 | { |
@@ -888,12 +941,14 @@ discard block |
||
| 888 | 941 | } |
| 889 | 942 | |
| 890 | 943 | // A special case - we want the file and line numbers for debugging. |
| 891 | - if ($error_type == 'return') |
|
| 892 | - return array($file, $line); |
|
| 944 | + if ($error_type == 'return') { |
|
| 945 | + return array($file, $line); |
|
| 946 | + } |
|
| 893 | 947 | |
| 894 | 948 | // Is always a critical error. |
| 895 | - if (function_exists('log_error')) |
|
| 896 | - log_error($log_message, 'critical', $file, $line); |
|
| 949 | + if (function_exists('log_error')) { |
|
| 950 | + log_error($log_message, 'critical', $file, $line); |
|
| 951 | + } |
|
| 897 | 952 | |
| 898 | 953 | if (function_exists('fatal_error')) |
| 899 | 954 | { |
@@ -901,12 +956,12 @@ discard block |
||
| 901 | 956 | |
| 902 | 957 | // Cannot continue... |
| 903 | 958 | exit; |
| 959 | + } elseif ($error_type) { |
|
| 960 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 961 | + } else { |
|
| 962 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 963 | + } |
|
| 904 | 964 | } |
| 905 | - elseif ($error_type) |
|
| 906 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 907 | - else |
|
| 908 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 909 | -} |
|
| 910 | 965 | |
| 911 | 966 | /** |
| 912 | 967 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
@@ -923,10 +978,11 @@ discard block |
||
| 923 | 978 | '\\' => '\\\\', |
| 924 | 979 | ); |
| 925 | 980 | |
| 926 | - if ($translate_human_wildcards) |
|
| 927 | - $replacements += array( |
|
| 981 | + if ($translate_human_wildcards) { |
|
| 982 | + $replacements += array( |
|
| 928 | 983 | '*' => '%', |
| 929 | 984 | ); |
| 985 | + } |
|
| 930 | 986 | |
| 931 | 987 | return strtr($string, $replacements); |
| 932 | 988 | } |