@@ -200,22 +200,22 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | case 'date': |
| 202 | 202 | if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
| 203 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 203 | + return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]) . '::date'; |
|
| 204 | 204 | else |
| 205 | 205 | smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
| 206 | 206 | break; |
| 207 | 207 | |
| 208 | 208 | case 'time': |
| 209 | 209 | if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
| 210 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 210 | + return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]) . '::time'; |
|
| 211 | 211 | else |
| 212 | 212 | smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
| 213 | 213 | break; |
| 214 | 214 | |
| 215 | 215 | case 'datetime': |
| 216 | 216 | 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) |
| 217 | - return 'to_timestamp('. |
|
| 218 | - sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5] ,$datetime_matches[6]). |
|
| 217 | + return 'to_timestamp(' . |
|
| 218 | + 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]) . |
|
| 219 | 219 | ',\'YYYY-MM-DD HH24:MI:SS\')'; |
| 220 | 220 | else |
| 221 | 221 | smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
@@ -475,7 +475,7 @@ discard block |
||
| 475 | 475 | $query_hints_set .= 'SET LOCAL join_collapse_limit = 1;'; |
| 476 | 476 | } |
| 477 | 477 | |
| 478 | - $db_string = $query_hints_set .' |
|
| 478 | + $db_string = $query_hints_set . ' |
|
| 479 | 479 | ' . $db_string; |
| 480 | 480 | } |
| 481 | 481 | |
@@ -785,10 +785,10 @@ discard block |
||
| 785 | 785 | $returning = ''; |
| 786 | 786 | $with_returning = false; |
| 787 | 787 | // lets build the returning string, mysql allow only in normal mode |
| 788 | - if(!empty($keys) && (count($keys) > 0) && ($method === '' || $method === 'insert') && $returnmode > 0) |
|
| 788 | + if (!empty($keys) && (count($keys) > 0) && ($method === '' || $method === 'insert') && $returnmode > 0) |
|
| 789 | 789 | { |
| 790 | 790 | // we only take the first key |
| 791 | - $returning = ' RETURNING '.$keys[0]; |
|
| 791 | + $returning = ' RETURNING ' . $keys[0]; |
|
| 792 | 792 | $with_returning = true; |
| 793 | 793 | } |
| 794 | 794 | |
@@ -819,7 +819,7 @@ discard block |
||
| 819 | 819 | INSERT INTO ' . $table . '("' . implode('", "', $indexed_columns) . '") |
| 820 | 820 | VALUES |
| 821 | 821 | ' . implode(', |
| 822 | - ', $insertRows).$replace.$returning, |
|
| 822 | + ', $insertRows) . $replace . $returning, |
|
| 823 | 823 | array( |
| 824 | 824 | 'security_override' => true, |
| 825 | 825 | 'db_error_skip' => $method == 'ignore' || $table === $db_prefix . 'log_errors', |
@@ -832,7 +832,7 @@ discard block |
||
| 832 | 832 | if ($returnmode === 2) |
| 833 | 833 | $return_var = array(); |
| 834 | 834 | |
| 835 | - while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
|
| 835 | + while (($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
|
| 836 | 836 | { |
| 837 | 837 | if (is_numeric($row[0])) // try to emulate mysql limitation |
| 838 | 838 | { |
@@ -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', |
@@ -61,11 +62,13 @@ discard block |
||
| 61 | 62 | 'db_mb4' => true, |
| 62 | 63 | 'db_ping' => 'pg_ping', |
| 63 | 64 | ); |
| 65 | + } |
|
| 64 | 66 | |
| 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'] . '\'')); |
|
| 67 | + if (!empty($db_options['persist'])) { |
|
| 68 | + $connection = @pg_pconnect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 69 | + } else { |
|
| 70 | + $connection = @pg_connect('host=' . $db_server . ' dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
|
| 71 | + } |
|
| 69 | 72 | |
| 70 | 73 | // Something's wrong, show an error if its fatal (which we assume it is) |
| 71 | 74 | if (!$connection) |
@@ -73,8 +76,7 @@ discard block |
||
| 73 | 76 | if (!empty($db_options['non_fatal'])) |
| 74 | 77 | { |
| 75 | 78 | return null; |
| 76 | - } |
|
| 77 | - else |
|
| 79 | + } else |
|
| 78 | 80 | { |
| 79 | 81 | display_db_error(); |
| 80 | 82 | } |
@@ -125,35 +127,42 @@ discard block |
||
| 125 | 127 | |
| 126 | 128 | list ($values, $connection) = $db_callback; |
| 127 | 129 | |
| 128 | - if ($matches[1] === 'db_prefix') |
|
| 129 | - return $db_prefix; |
|
| 130 | + if ($matches[1] === 'db_prefix') { |
|
| 131 | + return $db_prefix; |
|
| 132 | + } |
|
| 130 | 133 | |
| 131 | 134 | if (!empty($user_info)) |
| 132 | 135 | { |
| 133 | - foreach (array_keys($user_info) as $key) |
|
| 134 | - if (strpos($key, 'query_') !== false && $key === $matches[1]) |
|
| 136 | + foreach (array_keys($user_info) as $key) { |
|
| 137 | + if (strpos($key, 'query_') !== false && $key === $matches[1]) |
|
| 135 | 138 | return $user_info[$matches[1]]; |
| 139 | + } |
|
| 136 | 140 | } |
| 137 | 141 | |
| 138 | - if ($matches[1] === 'empty') |
|
| 139 | - return '\'\''; |
|
| 142 | + if ($matches[1] === 'empty') { |
|
| 143 | + return '\'\''; |
|
| 144 | + } |
|
| 140 | 145 | |
| 141 | - if (!isset($matches[2])) |
|
| 142 | - smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 146 | + if (!isset($matches[2])) { |
|
| 147 | + smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 148 | + } |
|
| 143 | 149 | |
| 144 | - if ($matches[1] === 'literal') |
|
| 145 | - return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 150 | + if ($matches[1] === 'literal') { |
|
| 151 | + return '\'' . pg_escape_string($matches[2]) . '\''; |
|
| 152 | + } |
|
| 146 | 153 | |
| 147 | - if (!isset($values[$matches[2]])) |
|
| 148 | - 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 | + if (!isset($values[$matches[2]])) { |
|
| 155 | + 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__); |
|
| 156 | + } |
|
| 149 | 157 | |
| 150 | 158 | $replacement = $values[$matches[2]]; |
| 151 | 159 | |
| 152 | 160 | switch ($matches[1]) |
| 153 | 161 | { |
| 154 | 162 | case 'int': |
| 155 | - if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 156 | - smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 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__); |
|
| 165 | + } |
|
| 157 | 166 | return (string) (int) $replacement; |
| 158 | 167 | break; |
| 159 | 168 | |
@@ -165,65 +174,73 @@ discard block |
||
| 165 | 174 | case 'array_int': |
| 166 | 175 | if (is_array($replacement)) |
| 167 | 176 | { |
| 168 | - if (empty($replacement)) |
|
| 169 | - smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 177 | + if (empty($replacement)) { |
|
| 178 | + smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 179 | + } |
|
| 170 | 180 | |
| 171 | 181 | foreach ($replacement as $key => $value) |
| 172 | 182 | { |
| 173 | - if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 174 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 183 | + if (!is_numeric($value) || (string) $value !== (string) (int) $value) { |
|
| 184 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 185 | + } |
|
| 175 | 186 | |
| 176 | 187 | $replacement[$key] = (string) (int) $value; |
| 177 | 188 | } |
| 178 | 189 | |
| 179 | 190 | return implode(', ', $replacement); |
| 191 | + } else { |
|
| 192 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 180 | 193 | } |
| 181 | - else |
|
| 182 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 183 | 194 | |
| 184 | 195 | break; |
| 185 | 196 | |
| 186 | 197 | case 'array_string': |
| 187 | 198 | if (is_array($replacement)) |
| 188 | 199 | { |
| 189 | - if (empty($replacement)) |
|
| 190 | - smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 200 | + if (empty($replacement)) { |
|
| 201 | + smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 202 | + } |
|
| 191 | 203 | |
| 192 | - foreach ($replacement as $key => $value) |
|
| 193 | - $replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
|
| 204 | + foreach ($replacement as $key => $value) { |
|
| 205 | + $replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
|
| 206 | + } |
|
| 194 | 207 | |
| 195 | 208 | return implode(', ', $replacement); |
| 209 | + } else { |
|
| 210 | + smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 196 | 211 | } |
| 197 | - else |
|
| 198 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 199 | 212 | break; |
| 200 | 213 | |
| 201 | 214 | case 'date': |
| 202 | - if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
|
| 203 | - return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 204 | - else |
|
| 205 | - smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 215 | + if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) { |
|
| 216 | + return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]).'::date'; |
|
| 217 | + } else { |
|
| 218 | + smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 219 | + } |
|
| 206 | 220 | break; |
| 207 | 221 | |
| 208 | 222 | case 'time': |
| 209 | - if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 210 | - return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 211 | - else |
|
| 212 | - smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 223 | + if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) { |
|
| 224 | + return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]).'::time'; |
|
| 225 | + } else { |
|
| 226 | + smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 227 | + } |
|
| 213 | 228 | break; |
| 214 | 229 | |
| 215 | 230 | case 'datetime': |
| 216 | - 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) |
|
| 217 | - return 'to_timestamp('. |
|
| 231 | + 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) { |
|
| 232 | + return 'to_timestamp('. |
|
| 218 | 233 | 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]). |
| 219 | 234 | ',\'YYYY-MM-DD HH24:MI:SS\')'; |
| 220 | - else |
|
| 221 | - smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 235 | + } else { |
|
| 236 | + smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 237 | + } |
|
| 222 | 238 | break; |
| 223 | 239 | |
| 224 | 240 | case 'float': |
| 225 | - if (!is_numeric($replacement)) |
|
| 226 | - smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 241 | + if (!is_numeric($replacement)) { |
|
| 242 | + smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 243 | + } |
|
| 227 | 244 | return (string) (float) $replacement; |
| 228 | 245 | break; |
| 229 | 246 | |
@@ -236,31 +253,36 @@ discard block |
||
| 236 | 253 | break; |
| 237 | 254 | |
| 238 | 255 | case 'inet': |
| 239 | - if ($replacement == 'null' || $replacement == '') |
|
| 240 | - return 'null'; |
|
| 241 | - if (inet_pton($replacement) === false) |
|
| 242 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 256 | + if ($replacement == 'null' || $replacement == '') { |
|
| 257 | + return 'null'; |
|
| 258 | + } |
|
| 259 | + if (inet_pton($replacement) === false) { |
|
| 260 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 261 | + } |
|
| 243 | 262 | return sprintf('\'%1$s\'::inet', pg_escape_string($replacement)); |
| 244 | 263 | |
| 245 | 264 | case 'array_inet': |
| 246 | 265 | if (is_array($replacement)) |
| 247 | 266 | { |
| 248 | - if (empty($replacement)) |
|
| 249 | - smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 267 | + if (empty($replacement)) { |
|
| 268 | + smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 269 | + } |
|
| 250 | 270 | |
| 251 | 271 | foreach ($replacement as $key => $value) |
| 252 | 272 | { |
| 253 | - if ($replacement == 'null' || $replacement == '') |
|
| 254 | - $replacement[$key] = 'null'; |
|
| 255 | - if (!isValidIP($value)) |
|
| 256 | - smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 273 | + if ($replacement == 'null' || $replacement == '') { |
|
| 274 | + $replacement[$key] = 'null'; |
|
| 275 | + } |
|
| 276 | + if (!isValidIP($value)) { |
|
| 277 | + smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 278 | + } |
|
| 257 | 279 | $replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value)); |
| 258 | 280 | } |
| 259 | 281 | |
| 260 | 282 | return implode(', ', $replacement); |
| 283 | + } else { |
|
| 284 | + 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 | 285 | } |
| 262 | - else |
|
| 263 | - smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 264 | 286 | break; |
| 265 | 287 | |
| 266 | 288 | default: |
@@ -351,14 +373,16 @@ discard block |
||
| 351 | 373 | ) |
| 352 | 374 | ); |
| 353 | 375 | |
| 354 | - if (isset($replacements[$identifier])) |
|
| 355 | - $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 376 | + if (isset($replacements[$identifier])) { |
|
| 377 | + $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
| 378 | + } |
|
| 356 | 379 | |
| 357 | 380 | // Limits need to be a little different. |
| 358 | 381 | $db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string); |
| 359 | 382 | |
| 360 | - if (trim($db_string) == '') |
|
| 361 | - return false; |
|
| 383 | + if (trim($db_string) == '') { |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 362 | 386 | |
| 363 | 387 | // Comments that are allowed in a query are preg_removed. |
| 364 | 388 | static $allowed_comments_from = array( |
@@ -378,8 +402,9 @@ discard block |
||
| 378 | 402 | $db_count = !isset($db_count) ? 1 : $db_count + 1; |
| 379 | 403 | $db_replace_result = 0; |
| 380 | 404 | |
| 381 | - if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) |
|
| 382 | - smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 405 | + if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) { |
|
| 406 | + smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
|
| 407 | + } |
|
| 383 | 408 | |
| 384 | 409 | if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
| 385 | 410 | { |
@@ -400,8 +425,9 @@ discard block |
||
| 400 | 425 | list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
| 401 | 426 | |
| 402 | 427 | // Initialize $db_cache if not already initialized. |
| 403 | - if (!isset($db_cache)) |
|
| 404 | - $db_cache = array(); |
|
| 428 | + if (!isset($db_cache)) { |
|
| 429 | + $db_cache = array(); |
|
| 430 | + } |
|
| 405 | 431 | |
| 406 | 432 | if (!empty($_SESSION['debug_redirect'])) |
| 407 | 433 | { |
@@ -427,17 +453,18 @@ discard block |
||
| 427 | 453 | while (true) |
| 428 | 454 | { |
| 429 | 455 | $pos = strpos($db_string, '\'', $pos + 1); |
| 430 | - if ($pos === false) |
|
| 431 | - break; |
|
| 456 | + if ($pos === false) { |
|
| 457 | + break; |
|
| 458 | + } |
|
| 432 | 459 | $clean .= substr($db_string, $old_pos, $pos - $old_pos); |
| 433 | 460 | |
| 434 | 461 | while (true) |
| 435 | 462 | { |
| 436 | 463 | $pos1 = strpos($db_string, '\'', $pos + 1); |
| 437 | 464 | $pos2 = strpos($db_string, '\\', $pos + 1); |
| 438 | - if ($pos1 === false) |
|
| 439 | - break; |
|
| 440 | - elseif ($pos2 === false || $pos2 > $pos1) |
|
| 465 | + if ($pos1 === false) { |
|
| 466 | + break; |
|
| 467 | + } elseif ($pos2 === false || $pos2 > $pos1) |
|
| 441 | 468 | { |
| 442 | 469 | $pos = $pos1; |
| 443 | 470 | break; |
@@ -453,16 +480,19 @@ discard block |
||
| 453 | 480 | $clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); |
| 454 | 481 | |
| 455 | 482 | // Comments? We don't use comments in our queries, we leave 'em outside! |
| 456 | - if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
|
| 457 | - $fail = true; |
|
| 483 | + if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) { |
|
| 484 | + $fail = true; |
|
| 485 | + } |
|
| 458 | 486 | // Trying to change passwords, slow us down, or something? |
| 459 | - elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) |
|
| 460 | - $fail = true; |
|
| 461 | - elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) |
|
| 462 | - $fail = true; |
|
| 487 | + elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) { |
|
| 488 | + $fail = true; |
|
| 489 | + } elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) { |
|
| 490 | + $fail = true; |
|
| 491 | + } |
|
| 463 | 492 | |
| 464 | - if (!empty($fail) && function_exists('log_error')) |
|
| 465 | - smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 493 | + if (!empty($fail) && function_exists('log_error')) { |
|
| 494 | + smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
|
| 495 | + } |
|
| 466 | 496 | } |
| 467 | 497 | |
| 468 | 498 | // Set optimize stuff |
@@ -481,12 +511,14 @@ discard block |
||
| 481 | 511 | |
| 482 | 512 | $db_last_result = @pg_query($connection, $db_string); |
| 483 | 513 | |
| 484 | - if ($db_last_result === false && empty($db_values['db_error_skip'])) |
|
| 485 | - $db_last_result = smf_db_error($db_string, $connection); |
|
| 514 | + if ($db_last_result === false && empty($db_values['db_error_skip'])) { |
|
| 515 | + $db_last_result = smf_db_error($db_string, $connection); |
|
| 516 | + } |
|
| 486 | 517 | |
| 487 | 518 | // Debugging. |
| 488 | - if (isset($db_show_debug) && $db_show_debug === true) |
|
| 489 | - $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 519 | + if (isset($db_show_debug) && $db_show_debug === true) { |
|
| 520 | + $db_cache[$db_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); |
|
| 521 | + } |
|
| 490 | 522 | |
| 491 | 523 | return $db_last_result; |
| 492 | 524 | } |
@@ -499,10 +531,11 @@ discard block |
||
| 499 | 531 | { |
| 500 | 532 | global $db_last_result, $db_replace_result; |
| 501 | 533 | |
| 502 | - if ($db_replace_result) |
|
| 503 | - return $db_replace_result; |
|
| 504 | - elseif ($result === null && !$db_last_result) |
|
| 505 | - return 0; |
|
| 534 | + if ($db_replace_result) { |
|
| 535 | + return $db_replace_result; |
|
| 536 | + } elseif ($result === null && !$db_last_result) { |
|
| 537 | + return 0; |
|
| 538 | + } |
|
| 506 | 539 | |
| 507 | 540 | return pg_affected_rows($result === null ? $db_last_result : $result); |
| 508 | 541 | } |
@@ -526,8 +559,9 @@ discard block |
||
| 526 | 559 | array( |
| 527 | 560 | ) |
| 528 | 561 | ); |
| 529 | - if (!$request) |
|
| 530 | - return false; |
|
| 562 | + if (!$request) { |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 531 | 565 | list ($lastID) = $smcFunc['db_fetch_row']($request); |
| 532 | 566 | $smcFunc['db_free_result']($request); |
| 533 | 567 | |
@@ -548,12 +582,13 @@ discard block |
||
| 548 | 582 | // Decide which connection to use |
| 549 | 583 | $connection = $connection === null ? $db_connection : $connection; |
| 550 | 584 | |
| 551 | - if ($type == 'begin') |
|
| 552 | - return @pg_query($connection, 'BEGIN'); |
|
| 553 | - elseif ($type == 'rollback') |
|
| 554 | - return @pg_query($connection, 'ROLLBACK'); |
|
| 555 | - elseif ($type == 'commit') |
|
| 556 | - return @pg_query($connection, 'COMMIT'); |
|
| 585 | + if ($type == 'begin') { |
|
| 586 | + return @pg_query($connection, 'BEGIN'); |
|
| 587 | + } elseif ($type == 'rollback') { |
|
| 588 | + return @pg_query($connection, 'ROLLBACK'); |
|
| 589 | + } elseif ($type == 'commit') { |
|
| 590 | + return @pg_query($connection, 'COMMIT'); |
|
| 591 | + } |
|
| 557 | 592 | |
| 558 | 593 | return false; |
| 559 | 594 | } |
@@ -581,19 +616,22 @@ discard block |
||
| 581 | 616 | $query_error = @pg_last_error($connection); |
| 582 | 617 | |
| 583 | 618 | // Log the error. |
| 584 | - if (function_exists('log_error')) |
|
| 585 | - log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 619 | + if (function_exists('log_error')) { |
|
| 620 | + log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
|
| 621 | + } |
|
| 586 | 622 | |
| 587 | 623 | // Nothing's defined yet... just die with it. |
| 588 | - if (empty($context) || empty($txt)) |
|
| 589 | - die($query_error); |
|
| 624 | + if (empty($context) || empty($txt)) { |
|
| 625 | + die($query_error); |
|
| 626 | + } |
|
| 590 | 627 | |
| 591 | 628 | // Show an error message, if possible. |
| 592 | 629 | $context['error_title'] = $txt['database_error']; |
| 593 | - if (allowedTo('admin_forum')) |
|
| 594 | - $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 595 | - else |
|
| 596 | - $context['error_message'] = $txt['try_again']; |
|
| 630 | + if (allowedTo('admin_forum')) { |
|
| 631 | + $context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
|
| 632 | + } else { |
|
| 633 | + $context['error_message'] = $txt['try_again']; |
|
| 634 | + } |
|
| 597 | 635 | |
| 598 | 636 | if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) |
| 599 | 637 | { |
@@ -615,12 +653,14 @@ discard block |
||
| 615 | 653 | { |
| 616 | 654 | global $db_row_count; |
| 617 | 655 | |
| 618 | - if ($counter !== false) |
|
| 619 | - return pg_fetch_row($request, $counter); |
|
| 656 | + if ($counter !== false) { |
|
| 657 | + return pg_fetch_row($request, $counter); |
|
| 658 | + } |
|
| 620 | 659 | |
| 621 | 660 | // Reset the row counter... |
| 622 | - if (!isset($db_row_count[(int) $request])) |
|
| 623 | - $db_row_count[(int) $request] = 0; |
|
| 661 | + if (!isset($db_row_count[(int) $request])) { |
|
| 662 | + $db_row_count[(int) $request] = 0; |
|
| 663 | + } |
|
| 624 | 664 | |
| 625 | 665 | // Return the right row. |
| 626 | 666 | return @pg_fetch_row($request, $db_row_count[(int) $request]++); |
@@ -637,12 +677,14 @@ discard block |
||
| 637 | 677 | { |
| 638 | 678 | global $db_row_count; |
| 639 | 679 | |
| 640 | - if ($counter !== false) |
|
| 641 | - return pg_fetch_assoc($request, $counter); |
|
| 680 | + if ($counter !== false) { |
|
| 681 | + return pg_fetch_assoc($request, $counter); |
|
| 682 | + } |
|
| 642 | 683 | |
| 643 | 684 | // Reset the row counter... |
| 644 | - if (!isset($db_row_count[(int) $request])) |
|
| 645 | - $db_row_count[(int) $request] = 0; |
|
| 685 | + if (!isset($db_row_count[(int) $request])) { |
|
| 686 | + $db_row_count[(int) $request] = 0; |
|
| 687 | + } |
|
| 646 | 688 | |
| 647 | 689 | // Return the right row. |
| 648 | 690 | return @pg_fetch_assoc($request, $db_row_count[(int) $request]++); |
@@ -695,11 +737,13 @@ discard block |
||
| 695 | 737 | |
| 696 | 738 | $replace = ''; |
| 697 | 739 | |
| 698 | - if (empty($data)) |
|
| 699 | - return; |
|
| 740 | + if (empty($data)) { |
|
| 741 | + return; |
|
| 742 | + } |
|
| 700 | 743 | |
| 701 | - if (!is_array($data[array_rand($data)])) |
|
| 702 | - $data = array($data); |
|
| 744 | + if (!is_array($data[array_rand($data)])) { |
|
| 745 | + $data = array($data); |
|
| 746 | + } |
|
| 703 | 747 | |
| 704 | 748 | // Replace the prefix holder with the actual prefix. |
| 705 | 749 | $table = str_replace('{db_prefix}', $db_prefix, $table); |
@@ -718,11 +762,13 @@ discard block |
||
| 718 | 762 | //pg 9.5 got replace support |
| 719 | 763 | $pg_version = $smcFunc['db_get_version'](); |
| 720 | 764 | // if we got a Beta Version |
| 721 | - if (stripos($pg_version, 'beta') !== false) |
|
| 722 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 765 | + if (stripos($pg_version, 'beta') !== false) { |
|
| 766 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
|
| 767 | + } |
|
| 723 | 768 | // or RC |
| 724 | - if (stripos($pg_version, 'rc') !== false) |
|
| 725 | - $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 769 | + if (stripos($pg_version, 'rc') !== false) { |
|
| 770 | + $pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
|
| 771 | + } |
|
| 726 | 772 | |
| 727 | 773 | $replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); |
| 728 | 774 | } |
@@ -741,8 +787,7 @@ discard block |
||
| 741 | 787 | $key_str .= ($count_pk > 0 ? ',' : ''); |
| 742 | 788 | $key_str .= $columnName; |
| 743 | 789 | $count_pk++; |
| 744 | - } |
|
| 745 | - else //normal field |
|
| 790 | + } else //normal field |
|
| 746 | 791 | { |
| 747 | 792 | $col_str .= ($count > 0 ? ',' : ''); |
| 748 | 793 | $col_str .= $columnName . ' = EXCLUDED.' . $columnName; |
@@ -750,20 +795,21 @@ discard block |
||
| 750 | 795 | } |
| 751 | 796 | } |
| 752 | 797 | $replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
| 753 | - } |
|
| 754 | - else |
|
| 798 | + } else |
|
| 755 | 799 | { |
| 756 | 800 | foreach ($columns as $columnName => $type) |
| 757 | 801 | { |
| 758 | 802 | // Are we restricting the length? |
| 759 | - if (strpos($type, 'string-') !== false) |
|
| 760 | - $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 761 | - else |
|
| 762 | - $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 803 | + if (strpos($type, 'string-') !== false) { |
|
| 804 | + $actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
|
| 805 | + } else { |
|
| 806 | + $actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
|
| 807 | + } |
|
| 763 | 808 | |
| 764 | 809 | // A key? That's what we were looking for. |
| 765 | - if (in_array($columnName, $keys)) |
|
| 766 | - $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 810 | + if (in_array($columnName, $keys)) { |
|
| 811 | + $where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
|
| 812 | + } |
|
| 767 | 813 | $count++; |
| 768 | 814 | } |
| 769 | 815 | |
@@ -799,10 +845,11 @@ discard block |
||
| 799 | 845 | foreach ($columns as $columnName => $type) |
| 800 | 846 | { |
| 801 | 847 | // Are we restricting the length? |
| 802 | - if (strpos($type, 'string-') !== false) |
|
| 803 | - $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 804 | - else |
|
| 805 | - $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 848 | + if (strpos($type, 'string-') !== false) { |
|
| 849 | + $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
|
| 850 | + } else { |
|
| 851 | + $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
|
| 852 | + } |
|
| 806 | 853 | } |
| 807 | 854 | $insertData = substr($insertData, 0, -2) . ')'; |
| 808 | 855 | |
@@ -811,8 +858,9 @@ discard block |
||
| 811 | 858 | |
| 812 | 859 | // Here's where the variables are injected to the query. |
| 813 | 860 | $insertRows = array(); |
| 814 | - foreach ($data as $dataRow) |
|
| 815 | - $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 861 | + foreach ($data as $dataRow) { |
|
| 862 | + $insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
| 863 | + } |
|
| 816 | 864 | |
| 817 | 865 | // Do the insert. |
| 818 | 866 | $request = $smcFunc['db_query']('', ' |
@@ -829,19 +877,21 @@ discard block |
||
| 829 | 877 | |
| 830 | 878 | if ($with_returning && $request !== false) |
| 831 | 879 | { |
| 832 | - if ($returnmode === 2) |
|
| 833 | - $return_var = array(); |
|
| 880 | + if ($returnmode === 2) { |
|
| 881 | + $return_var = array(); |
|
| 882 | + } |
|
| 834 | 883 | |
| 835 | 884 | while(($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
| 836 | 885 | { |
| 837 | - if (is_numeric($row[0])) // try to emulate mysql limitation |
|
| 886 | + if (is_numeric($row[0])) { |
|
| 887 | + // try to emulate mysql limitation |
|
| 838 | 888 | { |
| 839 | 889 | if ($returnmode === 1) |
| 840 | 890 | $return_var = $row[0]; |
| 841 | - elseif ($returnmode === 2) |
|
| 842 | - $return_var[] = $row[0]; |
|
| 843 | - } |
|
| 844 | - else |
|
| 891 | + } elseif ($returnmode === 2) { |
|
| 892 | + $return_var[] = $row[0]; |
|
| 893 | + } |
|
| 894 | + } else |
|
| 845 | 895 | { |
| 846 | 896 | $with_returning = false; |
| 847 | 897 | trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); |
@@ -850,9 +900,10 @@ discard block |
||
| 850 | 900 | } |
| 851 | 901 | } |
| 852 | 902 | |
| 853 | - if ($with_returning && !empty($return_var)) |
|
| 854 | - return $return_var; |
|
| 855 | -} |
|
| 903 | + if ($with_returning && !empty($return_var)) { |
|
| 904 | + return $return_var; |
|
| 905 | + } |
|
| 906 | + } |
|
| 856 | 907 | |
| 857 | 908 | /** |
| 858 | 909 | * Dummy function really. Doesn't do anything on PostgreSQL. |
@@ -889,8 +940,9 @@ discard block |
||
| 889 | 940 | */ |
| 890 | 941 | function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
| 891 | 942 | { |
| 892 | - if (empty($log_message)) |
|
| 893 | - $log_message = $error_message; |
|
| 943 | + if (empty($log_message)) { |
|
| 944 | + $log_message = $error_message; |
|
| 945 | + } |
|
| 894 | 946 | |
| 895 | 947 | foreach (debug_backtrace() as $step) |
| 896 | 948 | { |
@@ -909,12 +961,14 @@ discard block |
||
| 909 | 961 | } |
| 910 | 962 | |
| 911 | 963 | // A special case - we want the file and line numbers for debugging. |
| 912 | - if ($error_type == 'return') |
|
| 913 | - return array($file, $line); |
|
| 964 | + if ($error_type == 'return') { |
|
| 965 | + return array($file, $line); |
|
| 966 | + } |
|
| 914 | 967 | |
| 915 | 968 | // Is always a critical error. |
| 916 | - if (function_exists('log_error')) |
|
| 917 | - log_error($log_message, 'critical', $file, $line); |
|
| 969 | + if (function_exists('log_error')) { |
|
| 970 | + log_error($log_message, 'critical', $file, $line); |
|
| 971 | + } |
|
| 918 | 972 | |
| 919 | 973 | if (function_exists('fatal_error')) |
| 920 | 974 | { |
@@ -922,12 +976,12 @@ discard block |
||
| 922 | 976 | |
| 923 | 977 | // Cannot continue... |
| 924 | 978 | exit; |
| 979 | + } elseif ($error_type) { |
|
| 980 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 981 | + } else { |
|
| 982 | + trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 983 | + } |
|
| 925 | 984 | } |
| 926 | - elseif ($error_type) |
|
| 927 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
| 928 | - else |
|
| 929 | - trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
|
| 930 | -} |
|
| 931 | 985 | |
| 932 | 986 | /** |
| 933 | 987 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
@@ -944,10 +998,11 @@ discard block |
||
| 944 | 998 | '\\' => '\\\\', |
| 945 | 999 | ); |
| 946 | 1000 | |
| 947 | - if ($translate_human_wildcards) |
|
| 948 | - $replacements += array( |
|
| 1001 | + if ($translate_human_wildcards) { |
|
| 1002 | + $replacements += array( |
|
| 949 | 1003 | '*' => '%', |
| 950 | 1004 | ); |
| 1005 | + } |
|
| 951 | 1006 | |
| 952 | 1007 | return strtr($string, $replacements); |
| 953 | 1008 | } |