| @@ 130-274 (lines=145) @@ | ||
| 127 | * @param array $matches The matches from preg_replace_callback |
|
| 128 | * @return string The appropriate string depending on $matches[1] |
|
| 129 | */ |
|
| 130 | function smf_db_replacement__callback($matches) |
|
| 131 | { |
|
| 132 | global $db_callback, $user_info, $db_prefix, $smcFunc; |
|
| 133 | ||
| 134 | list ($values, $connection) = $db_callback; |
|
| 135 | ||
| 136 | // Connection gone??? This should *never* happen at this point, yet it does :'( |
|
| 137 | if (!is_resource($connection)) |
|
| 138 | display_db_error(); |
|
| 139 | ||
| 140 | if ($matches[1] === 'db_prefix') |
|
| 141 | return $db_prefix; |
|
| 142 | ||
| 143 | if ($matches[1] === 'query_see_board') |
|
| 144 | return $user_info['query_see_board']; |
|
| 145 | ||
| 146 | if ($matches[1] === 'query_wanna_see_board') |
|
| 147 | return $user_info['query_wanna_see_board']; |
|
| 148 | ||
| 149 | if ($matches[1] === 'empty') |
|
| 150 | return '\'\''; |
|
| 151 | ||
| 152 | if (!isset($matches[2])) |
|
| 153 | smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 154 | ||
| 155 | if ($matches[1] === 'literal') |
|
| 156 | return '\'' . mysql_real_escape_string($matches[2], $connection) . '\''; |
|
| 157 | ||
| 158 | if (!isset($values[$matches[2]])) |
|
| 159 | 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__); |
|
| 160 | ||
| 161 | $replacement = $values[$matches[2]]; |
|
| 162 | ||
| 163 | switch ($matches[1]) |
|
| 164 | { |
|
| 165 | case 'int': |
|
| 166 | if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 167 | smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 168 | return (string) (int) $replacement; |
|
| 169 | break; |
|
| 170 | ||
| 171 | case 'string': |
|
| 172 | case 'text': |
|
| 173 | return sprintf('\'%1$s\'', mysql_real_escape_string($replacement, $connection)); |
|
| 174 | break; |
|
| 175 | ||
| 176 | case 'array_int': |
|
| 177 | if (is_array($replacement)) |
|
| 178 | { |
|
| 179 | if (empty($replacement)) |
|
| 180 | smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 181 | ||
| 182 | foreach ($replacement as $key => $value) |
|
| 183 | { |
|
| 184 | if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 185 | smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 186 | ||
| 187 | $replacement[$key] = (string) (int) $value; |
|
| 188 | } |
|
| 189 | ||
| 190 | return implode(', ', $replacement); |
|
| 191 | } |
|
| 192 | else |
|
| 193 | smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 194 | ||
| 195 | break; |
|
| 196 | ||
| 197 | case 'array_string': |
|
| 198 | if (is_array($replacement)) |
|
| 199 | { |
|
| 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 | ||
| 203 | foreach ($replacement as $key => $value) |
|
| 204 | $replacement[$key] = sprintf('\'%1$s\'', mysql_real_escape_string($value, $connection)); |
|
| 205 | ||
| 206 | return implode(', ', $replacement); |
|
| 207 | } |
|
| 208 | else |
|
| 209 | smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 210 | break; |
|
| 211 | ||
| 212 | case 'date': |
|
| 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]); |
|
| 215 | else |
|
| 216 | smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 217 | break; |
|
| 218 | ||
| 219 | case 'time': |
|
| 220 | if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 221 | return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]); |
|
| 222 | else |
|
| 223 | smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 224 | break; |
|
| 225 | ||
| 226 | case 'float': |
|
| 227 | if (!is_numeric($replacement)) |
|
| 228 | smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 229 | return (string) (float) $replacement; |
|
| 230 | break; |
|
| 231 | ||
| 232 | case 'identifier': |
|
| 233 | // Backticks inside identifiers are supported as of MySQL 4.1. We don't need them for SMF. |
|
| 234 | return '`' . strtr($replacement, array('`' => '', '.' => '')) . '`'; |
|
| 235 | break; |
|
| 236 | ||
| 237 | case 'raw': |
|
| 238 | return $replacement; |
|
| 239 | break; |
|
| 240 | ||
| 241 | case 'inet': |
|
| 242 | if ($replacement == 'null' || $replacement == '') |
|
| 243 | return 'null'; |
|
| 244 | if (!isValidIP($replacement)) |
|
| 245 | smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 246 | //we don't use the native support of mysql > 5.6.2 |
|
| 247 | return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement))); |
|
| 248 | ||
| 249 | case 'array_inet': |
|
| 250 | if (is_array($replacement)) |
|
| 251 | { |
|
| 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__); |
|
| 254 | ||
| 255 | foreach ($replacement as $key => $value) |
|
| 256 | { |
|
| 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__); |
|
| 261 | $replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value))); |
|
| 262 | } |
|
| 263 | ||
| 264 | return implode(', ', $replacement); |
|
| 265 | } |
|
| 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 | break; |
|
| 269 | ||
| 270 | default: |
|
| 271 | smf_db_error_backtrace('Undefined type used in the database query. (' . $matches[1] . ':' . $matches[2] . ')', '', false, __FILE__, __LINE__); |
|
| 272 | break; |
|
| 273 | } |
|
| 274 | } |
|
| 275 | ||
| 276 | /** |
|
| 277 | * Just like the db_query, escape and quote a string, but not executing the query. |
|
| @@ 162-304 (lines=143) @@ | ||
| 159 | * @param array $matches The matches from preg_replace_callback |
|
| 160 | * @return string The appropriate string depending on $matches[1] |
|
| 161 | */ |
|
| 162 | function smf_db_replacement__callback($matches) |
|
| 163 | { |
|
| 164 | global $db_callback, $user_info, $db_prefix, $smcFunc; |
|
| 165 | ||
| 166 | list ($values, $connection) = $db_callback; |
|
| 167 | if (!is_object($connection)) |
|
| 168 | display_db_error(); |
|
| 169 | ||
| 170 | if ($matches[1] === 'db_prefix') |
|
| 171 | return $db_prefix; |
|
| 172 | ||
| 173 | if ($matches[1] === 'query_see_board') |
|
| 174 | return $user_info['query_see_board']; |
|
| 175 | ||
| 176 | if ($matches[1] === 'query_wanna_see_board') |
|
| 177 | return $user_info['query_wanna_see_board']; |
|
| 178 | ||
| 179 | if ($matches[1] === 'empty') |
|
| 180 | return '\'\''; |
|
| 181 | ||
| 182 | if (!isset($matches[2])) |
|
| 183 | smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 184 | ||
| 185 | if ($matches[1] === 'literal') |
|
| 186 | return '\'' . mysqli_real_escape_string($connection, $matches[2]) . '\''; |
|
| 187 | ||
| 188 | if (!isset($values[$matches[2]])) |
|
| 189 | 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__); |
|
| 190 | ||
| 191 | $replacement = $values[$matches[2]]; |
|
| 192 | ||
| 193 | switch ($matches[1]) |
|
| 194 | { |
|
| 195 | case 'int': |
|
| 196 | if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 197 | smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 198 | return (string) (int) $replacement; |
|
| 199 | break; |
|
| 200 | ||
| 201 | case 'string': |
|
| 202 | case 'text': |
|
| 203 | return sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $replacement)); |
|
| 204 | break; |
|
| 205 | ||
| 206 | case 'array_int': |
|
| 207 | if (is_array($replacement)) |
|
| 208 | { |
|
| 209 | if (empty($replacement)) |
|
| 210 | smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 211 | ||
| 212 | foreach ($replacement as $key => $value) |
|
| 213 | { |
|
| 214 | if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 215 | smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 216 | ||
| 217 | $replacement[$key] = (string) (int) $value; |
|
| 218 | } |
|
| 219 | ||
| 220 | return implode(', ', $replacement); |
|
| 221 | } |
|
| 222 | else |
|
| 223 | smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 224 | ||
| 225 | break; |
|
| 226 | ||
| 227 | case 'array_string': |
|
| 228 | if (is_array($replacement)) |
|
| 229 | { |
|
| 230 | if (empty($replacement)) |
|
| 231 | smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 232 | ||
| 233 | foreach ($replacement as $key => $value) |
|
| 234 | $replacement[$key] = sprintf('\'%1$s\'', mysqli_real_escape_string($connection, $value)); |
|
| 235 | ||
| 236 | return implode(', ', $replacement); |
|
| 237 | } |
|
| 238 | else |
|
| 239 | smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 240 | break; |
|
| 241 | ||
| 242 | case 'date': |
|
| 243 | if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
|
| 244 | return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]); |
|
| 245 | else |
|
| 246 | smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 247 | break; |
|
| 248 | ||
| 249 | case 'time': |
|
| 250 | if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
|
| 251 | return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]); |
|
| 252 | else |
|
| 253 | smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 254 | break; |
|
| 255 | ||
| 256 | case 'float': |
|
| 257 | if (!is_numeric($replacement)) |
|
| 258 | smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 259 | return (string) (float) $replacement; |
|
| 260 | break; |
|
| 261 | ||
| 262 | case 'identifier': |
|
| 263 | // Backticks inside identifiers are supported as of MySQL 4.1. We don't need them for SMF. |
|
| 264 | return '`' . strtr($replacement, array('`' => '', '.' => '')) . '`'; |
|
| 265 | break; |
|
| 266 | ||
| 267 | case 'raw': |
|
| 268 | return $replacement; |
|
| 269 | break; |
|
| 270 | ||
| 271 | case 'inet': |
|
| 272 | if ($replacement == 'null' || $replacement == '') |
|
| 273 | return 'null'; |
|
| 274 | if (!isValidIP($replacement)) |
|
| 275 | smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 276 | //we don't use the native support of mysql > 5.6.2 |
|
| 277 | return sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($replacement))); |
|
| 278 | ||
| 279 | case 'array_inet': |
|
| 280 | if (is_array($replacement)) |
|
| 281 | { |
|
| 282 | if (empty($replacement)) |
|
| 283 | smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 284 | ||
| 285 | foreach ($replacement as $key => $value) |
|
| 286 | { |
|
| 287 | if ($replacement == 'null' || $replacement == '') |
|
| 288 | $replacement[$key] = 'null'; |
|
| 289 | if (!isValidIP($value)) |
|
| 290 | smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 291 | $replacement[$key] = sprintf('unhex(\'%1$s\')', bin2hex(inet_pton($value))); |
|
| 292 | } |
|
| 293 | ||
| 294 | return implode(', ', $replacement); |
|
| 295 | } |
|
| 296 | else |
|
| 297 | smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 298 | break; |
|
| 299 | ||
| 300 | default: |
|
| 301 | smf_db_error_backtrace('Undefined type used in the database query. (' . $matches[1] . ':' . $matches[2] . ')', '', false, __FILE__, __LINE__); |
|
| 302 | break; |
|
| 303 | } |
|
| 304 | } |
|
| 305 | ||
| 306 | /** |
|
| 307 | * Just like the db_query, escape and quote a string, but not executing the query. |
|