elkarte /
Elkarte
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * This file provides an implementation of the most common functions needed |
||||
| 5 | * for the database drivers to work. |
||||
| 6 | * |
||||
| 7 | * @package ElkArte Forum |
||||
| 8 | * @copyright ElkArte Forum contributors |
||||
| 9 | * @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
||||
| 10 | * |
||||
| 11 | * This file contains code covered by: |
||||
| 12 | * copyright: 2011 Simple Machines (http://www.simplemachines.org) |
||||
| 13 | * |
||||
| 14 | * @version 2.0 dev |
||||
| 15 | * |
||||
| 16 | */ |
||||
| 17 | |||||
| 18 | namespace ElkArte\Database; |
||||
| 19 | |||||
| 20 | use ElkArte\Debug; |
||||
| 21 | use ElkArte\Errors\Errors; |
||||
|
0 ignored issues
–
show
|
|||||
| 22 | use ElkArte\Exceptions\Exception; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * Abstract database class, implements database to control functions |
||||
| 26 | */ |
||||
| 27 | abstract class AbstractQuery implements QueryInterface |
||||
| 28 | { |
||||
| 29 | /** @var string Of course the character used to escape characters that have to be escaped */ |
||||
| 30 | public const ESCAPE_CHAR = '\\'; |
||||
| 31 | |||||
| 32 | /** @var int Number of queries run (may include queries from $_SESSION if is a redirect) */ |
||||
| 33 | protected $_query_count = 0; |
||||
| 34 | |||||
| 35 | /** @var bool The way to skip a database error */ |
||||
| 36 | protected $_skip_error = false; |
||||
| 37 | |||||
| 38 | /** @var string String to match visible boards. Default is false, so that unless it is set, nothing is returned. */ |
||||
| 39 | protected $query_see_board = '1!=1'; |
||||
| 40 | |||||
| 41 | /** @var String to match boards the user want to see. Default is false, so that unless it is set, nothing is returned. */ |
||||
| 42 | protected $query_wanna_see_board = '1!=1'; |
||||
| 43 | |||||
| 44 | /** @var string String to match boards the user want to see. Default is false, so that unless it is set, nothing is returned. */ |
||||
| 45 | protected $ilike = ''; |
||||
| 46 | |||||
| 47 | /** @var string String that defines case insensitive not-like query operator */ |
||||
| 48 | protected $not_ilike = ''; |
||||
| 49 | |||||
| 50 | /** @var string String that defines regular-expression-like query operator */ |
||||
| 51 | protected $rlike = ''; |
||||
| 52 | |||||
| 53 | /** @var string String that defines regular-expression-not-like query operator */ |
||||
| 54 | protected $not_rlike = ''; |
||||
| 55 | |||||
| 56 | /** @var bool MySQL supports unbuffered queries, this remembers if we are running an unbuffered or not */ |
||||
| 57 | protected $_unbuffered = false; |
||||
| 58 | |||||
| 59 | /** @var array This holds the "values" used in the replacement__callback method */ |
||||
| 60 | protected $_db_callback_values = []; |
||||
| 61 | |||||
| 62 | /** @var bool|resource Holds the resource from the dBMS of the last query run */ |
||||
| 63 | protected $_db_last_result; |
||||
| 64 | |||||
| 65 | /** @var array Holds some values (time, file, line, delta) to debug performance of the queries. */ |
||||
| 66 | protected $db_cache = []; |
||||
| 67 | |||||
| 68 | /** @var Debug The debug object. */ |
||||
| 69 | protected $_debug; |
||||
| 70 | |||||
| 71 | /** @var AbstractResult Temporary variable to support the migration to the new db-layer. |
||||
| 72 | * Ideally to be removed before 2.0 shipment */ |
||||
| 73 | protected $result; |
||||
| 74 | |||||
| 75 | /** @var string[] Comments that are allowed in a query are preg_removed. These replacements happen in the query checks. */ |
||||
| 76 | protected $allowed_comments = [ |
||||
| 77 | 'from' => [ |
||||
| 78 | '~\s+~s', |
||||
| 79 | '~/\*!40001 SQL_NO_CACHE \*/~', |
||||
| 80 | '~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~', |
||||
| 81 | '~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~', |
||||
| 82 | ], |
||||
| 83 | 'to' => [ |
||||
| 84 | ' ', |
||||
| 85 | '', |
||||
| 86 | '', |
||||
| 87 | '', |
||||
| 88 | ] |
||||
| 89 | ]; |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * Constructor. |
||||
| 93 | * |
||||
| 94 | * @param string $_db_prefix Guess what? The tables prefix |
||||
| 95 | * @param \MySqli|resource|object $connection Obviously the database connection |
||||
| 96 | */ |
||||
| 97 | public function __construct(protected $_db_prefix, protected $connection) |
||||
| 98 | { |
||||
| 99 | global $db_show_debug; |
||||
| 100 | |||||
| 101 | // Debugging. |
||||
| 102 | if ($db_show_debug === true) |
||||
| 103 | { |
||||
| 104 | $this->_debug = Debug::instance(); |
||||
| 105 | } |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * {@inheritDoc} |
||||
| 110 | */ |
||||
| 111 | abstract public function transaction($type = 'commit'); |
||||
| 112 | |||||
| 113 | /** |
||||
| 114 | * {@inheritDoc} |
||||
| 115 | */ |
||||
| 116 | abstract public function last_error(); |
||||
| 117 | |||||
| 118 | /** |
||||
| 119 | * Public setter for the string that defines which boards the user can see. |
||||
| 120 | * |
||||
| 121 | * @param string $string |
||||
| 122 | */ |
||||
| 123 | public function setSeeBoard($string): void |
||||
| 124 | { |
||||
| 125 | $this->query_see_board = $string; |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Public setter for the string that defines which boards the user want to see. |
||||
| 130 | * |
||||
| 131 | * @param string $string |
||||
| 132 | */ |
||||
| 133 | public function setWannaSeeBoard($string): void |
||||
| 134 | { |
||||
| 135 | $this->query_wanna_see_board = $string; |
||||
| 136 | } |
||||
| 137 | 1 | ||||
| 138 | /** |
||||
| 139 | 1 | * {@inheritDoc} |
|||
| 140 | */ |
||||
| 141 | 1 | public function quote($db_string, $db_values) |
|||
| 142 | 1 | { |
|||
| 143 | // Only bother if there's something to replace. |
||||
| 144 | if (strpos($db_string, '{') !== false) |
||||
| 145 | 1 | { |
|||
| 146 | // This is needed by the callback function. |
||||
| 147 | $this->_db_callback_values = $db_values; |
||||
| 148 | |||||
| 149 | 1 | // Do the quoting and escaping |
|||
| 150 | $db_string = preg_replace_callback('~{([a-z_]+)(?::([.a-zA-Z0-9_-]+))?}~', |
||||
| 151 | fn($matches) => $this->replacement__callback($matches), $db_string); |
||||
| 152 | |||||
| 153 | // Clear this variables. |
||||
| 154 | $this->_db_callback_values = []; |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | return $db_string; |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | /** |
||||
| 161 | * Callback for preg_replace_callback on the query. |
||||
| 162 | * |
||||
| 163 | * It allows replacing on the fly a few pre-defined strings, for convenience |
||||
| 164 | * ('query_see_board', 'query_wanna_see_board'), with their current values from User::$info. |
||||
| 165 | * |
||||
| 166 | 5 | * In addition, it performs checks and sanitation on the values sent to the database. |
|||
| 167 | * |
||||
| 168 | 5 | * @param array $matches |
|||
| 169 | 5 | * |
|||
| 170 | * @return mixed|string |
||||
| 171 | */ |
||||
| 172 | public function replacement__callback($matches) |
||||
| 173 | { |
||||
| 174 | // Connection gone??? This should *never* happen at this point, yet it does :'( |
||||
| 175 | if (!$this->validConnection()) |
||||
| 176 | 5 | { |
|||
| 177 | Errors::instance()->display_db_error('ElkArte\\Database\\AbstractQuery::replacement__callback'); |
||||
| 178 | 5 | } |
|||
| 179 | 5 | ||||
| 180 | switch ($matches[1]) |
||||
| 181 | { |
||||
| 182 | case 'db_prefix': |
||||
| 183 | return $this->_db_prefix; |
||||
| 184 | 81 | case 'query_see_board': |
|||
| 185 | return $this->query_see_board; |
||||
| 186 | case 'query_wanna_see_board': |
||||
| 187 | 81 | return $this->query_wanna_see_board; |
|||
| 188 | case 'ilike': |
||||
| 189 | return $this->ilike; |
||||
| 190 | 81 | case 'not_ilike': |
|||
| 191 | return $this->not_ilike; |
||||
| 192 | case 'rlike': |
||||
| 193 | 81 | return $this->rlike; |
|||
| 194 | case 'not_rlike': |
||||
| 195 | 81 | return $this->not_rlike; |
|||
| 196 | 81 | case 'column_case_insensitive': |
|||
| 197 | return $this->_replaceColumnCaseInsensitive($matches[2]); |
||||
| 198 | } |
||||
| 199 | 81 | ||||
| 200 | if (!isset($matches[2])) |
||||
| 201 | { |
||||
| 202 | 81 | $this->error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|||
| 203 | } |
||||
| 204 | |||||
| 205 | if (!isset($this->_db_callback_values[$matches[2]])) |
||||
| 206 | { |
||||
| 207 | $this->error_backtrace("The database value you're trying to insert does not exist: " . htmlspecialchars($matches[2], ENT_COMPAT, 'UTF-8'), '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | $replacement = $this->_db_callback_values[$matches[2]]; |
||||
| 211 | |||||
| 212 | return match ($matches[1]) |
||||
| 213 | { |
||||
| 214 | 'int' => $this->_replaceInt($matches[2], $replacement), |
||||
| 215 | 'string', 'text' => $this->_replaceString($replacement), |
||||
| 216 | 'string_case_sensitive' => $this->_replaceStringCaseSensitive($replacement), |
||||
| 217 | 'string_case_insensitive' => $this->_replaceStringCaseInsensitive($replacement), |
||||
| 218 | 303 | 'array_int' => $this->_replaceArrayInt($matches[2], $replacement), |
|||
| 219 | 'array_string' => $this->_replaceArrayString($matches[2], $replacement), |
||||
| 220 | 'array_string_case_insensitive' => $this->_replaceArrayStringCaseInsensitive($matches[2], $replacement), |
||||
| 221 | 303 | 'date' => $this->_replaceDate($matches[2], $replacement), |
|||
| 222 | 'float' => $this->_replaceFloat($matches[2], $replacement), |
||||
| 223 | 'identifier' => $this->_replaceIdentifier($replacement), |
||||
| 224 | 'raw' => $replacement, |
||||
| 225 | default => $this->error_backtrace('Undefined type used in the database query. (' . $matches[1] . ':' . $matches[2] . ')', '', false, __FILE__, __LINE__), |
||||
| 226 | 303 | }; |
|||
| 227 | } |
||||
| 228 | 303 | ||||
| 229 | 297 | /** |
|||
| 230 | 301 | * Finds out if the connection is still valid. |
|||
| 231 | 23 | * |
|||
| 232 | 301 | * @return bool |
|||
| 233 | 2 | */ |
|||
| 234 | 301 | public function validConnection() |
|||
| 235 | 12 | { |
|||
| 236 | return (bool) $this->connection; |
||||
| 237 | } |
||||
| 238 | 301 | ||||
| 239 | /** |
||||
| 240 | * Casts the column to LOWER(column_name) for replacement__callback. |
||||
| 241 | * |
||||
| 242 | * @param mixed $replacement |
||||
| 243 | 301 | * @return string |
|||
| 244 | */ |
||||
| 245 | protected function _replaceColumnCaseInsensitive($replacement) |
||||
| 246 | { |
||||
| 247 | return 'LOWER(' . $replacement . ')'; |
||||
| 248 | 301 | } |
|||
| 249 | |||||
| 250 | 301 | /** |
|||
| 251 | * Scans the debug_backtrace output looking for the place where the |
||||
| 252 | 301 | * actual error happened |
|||
| 253 | 275 | * |
|||
| 254 | 137 | * @return array |
|||
| 255 | 106 | */ |
|||
| 256 | 110 | protected function backtrace_message(): array |
|||
| 257 | 106 | { |
|||
| 258 | 2 | $log_message = ''; |
|||
| 259 | 106 | $file = null; |
|||
| 260 | 10 | $line = null; |
|||
| 261 | 102 | foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $step) |
|||
| 262 | 69 | { |
|||
| 263 | 86 | // Found it? |
|||
| 264 | 43 | if (!method_exists($this, $step['function']) && !in_array(substr($step['function'], 0, 7), ['elk_db_', 'preg_re', 'db_erro', 'call_us'])) |
|||
| 265 | 65 | { |
|||
| 266 | 2 | $log_message .= '<br />Function: ' . $step['function']; |
|||
| 267 | 63 | break; |
|||
| 268 | 3 | } |
|||
| 269 | 60 | ||||
| 270 | 1 | if (isset($step['line'])) |
|||
| 271 | 60 | { |
|||
| 272 | $file = $step['file']; |
||||
| 273 | 60 | $line = $step['line']; |
|||
| 274 | 60 | } |
|||
| 275 | } |
||||
| 276 | |||||
| 277 | return [$file, $line, $log_message]; |
||||
| 278 | } |
||||
| 279 | |||||
| 280 | /** |
||||
| 281 | * This function tries to work out additional error information from a back trace. |
||||
| 282 | * |
||||
| 283 | * @param string $error_message |
||||
| 284 | * @param string $log_message |
||||
| 285 | * @param string|bool $error_type |
||||
| 286 | * @param string|null $file_fallback |
||||
| 287 | * @param int|null $line_fallback |
||||
| 288 | * |
||||
| 289 | * @throws Exception |
||||
| 290 | */ |
||||
| 291 | protected function error_backtrace($error_message, $log_message = '', $error_type = false, $file_fallback = null, $line_fallback = null): void |
||||
|
0 ignored issues
–
show
The parameter
$error_type is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 292 | { |
||||
| 293 | if (empty($log_message)) |
||||
| 294 | { |
||||
| 295 | $log_message = $error_message; |
||||
| 296 | } |
||||
| 297 | |||||
| 298 | // We'll try recovering the file and line number the original db query was called from. |
||||
| 299 | 6 | [$file, $line, $backtrace_message] = $this->backtrace_message(); |
|||
| 300 | |||||
| 301 | 6 | // Just in case nothing can be found from debug_backtrace |
|||
| 302 | $file = $file ?? $file_fallback; |
||||
| 303 | $line = $line ?? $line_fallback; |
||||
| 304 | $log_message .= $backtrace_message; |
||||
| 305 | |||||
| 306 | // Is always a critical error. |
||||
| 307 | 37 | Errors::instance()->log_error($log_message, 'critical', $file, $line); |
|||
| 308 | |||||
| 309 | 37 | throw new Exception([false, $error_message], false); |
|||
|
0 ignored issues
–
show
array(false, $error_message) of type array<integer,false|string> is incompatible with the type string|string[] expected by parameter $message of ElkArte\Exceptions\Exception::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 310 | } |
||||
| 311 | |||||
| 312 | /** |
||||
| 313 | * Tests and casts integers for replacement__callback. |
||||
| 314 | 37 | * |
|||
| 315 | * @param mixed $identifier |
||||
| 316 | * @param mixed $replacement |
||||
| 317 | 37 | * @return string |
|||
| 318 | * @throws Exception |
||||
| 319 | 37 | */ |
|||
| 320 | 37 | protected function _replaceInt($identifier, $replacement): string |
|||
| 321 | { |
||||
| 322 | if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
||||
| 323 | 37 | { |
|||
| 324 | $this->error_backtrace('Wrong value type sent to the database. Integer expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 325 | 37 | } |
|||
| 326 | 37 | ||||
| 327 | return (string) (int) $replacement; |
||||
| 328 | } |
||||
| 329 | |||||
| 330 | /** |
||||
| 331 | 37 | * Casts values to string for replacement__callback. |
|||
| 332 | * |
||||
| 333 | 37 | * @param mixed $replacement |
|||
| 334 | * @return string |
||||
| 335 | */ |
||||
| 336 | protected function _replaceString($replacement): string |
||||
| 337 | { |
||||
| 338 | return sprintf('\'%1$s\'', $this->escape_string($replacement)); |
||||
| 339 | } |
||||
| 340 | |||||
| 341 | /** |
||||
| 342 | * Escape string for the database input |
||||
| 343 | * |
||||
| 344 | * @param string $string |
||||
| 345 | * |
||||
| 346 | * @return string |
||||
| 347 | */ |
||||
| 348 | abstract public function escape_string($string); |
||||
| 349 | |||||
| 350 | /** |
||||
| 351 | * Casts values to string for replacement__callback and in the DBMS that |
||||
| 352 | * require this solution makes it so that the comparison will be case sensitive. |
||||
| 353 | * |
||||
| 354 | * @param mixed $replacement |
||||
| 355 | * @return string |
||||
| 356 | */ |
||||
| 357 | protected function _replaceStringCaseSensitive($replacement) |
||||
| 358 | { |
||||
| 359 | return $this->_replaceString($replacement); |
||||
| 360 | } |
||||
| 361 | |||||
| 362 | /** |
||||
| 363 | * Casts values to LOWER(string) for replacement__callback. |
||||
| 364 | * |
||||
| 365 | * @param mixed $replacement |
||||
| 366 | 275 | * @return string |
|||
| 367 | */ |
||||
| 368 | 275 | protected function _replaceStringCaseInsensitive($replacement) |
|||
| 369 | { |
||||
| 370 | return 'LOWER(' . $this->_replaceString($replacement) . ')'; |
||||
| 371 | } |
||||
| 372 | |||||
| 373 | 275 | /** |
|||
| 374 | * Tests and casts arrays of integers for replacement__callback. |
||||
| 375 | * |
||||
| 376 | * @param string $identifier |
||||
| 377 | * @param array $replacement |
||||
| 378 | * @return string|null |
||||
| 379 | */ |
||||
| 380 | protected function _replaceArrayInt($identifier, $replacement): ?string |
||||
| 381 | { |
||||
| 382 | 114 | if (is_array($replacement)) |
|||
|
0 ignored issues
–
show
|
|||||
| 383 | { |
||||
| 384 | 114 | if (empty($replacement)) |
|||
| 385 | { |
||||
| 386 | $this->error_backtrace('Database error, given array of integer values is empty. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 387 | } |
||||
| 388 | |||||
| 389 | foreach ($replacement as $key => $value) |
||||
| 390 | { |
||||
| 391 | if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
||||
| 392 | { |
||||
| 393 | $this->error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 394 | } |
||||
| 395 | |||||
| 396 | $replacement[$key] = (string) (int) $value; |
||||
| 397 | } |
||||
| 398 | |||||
| 399 | return implode(', ', $replacement); |
||||
| 400 | } |
||||
| 401 | |||||
| 402 | $this->error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 403 | 1 | } |
|||
| 404 | |||||
| 405 | 1 | /** |
|||
| 406 | * Tests and casts arrays of strings for replacement__callback. |
||||
| 407 | * |
||||
| 408 | * @param string $identifier |
||||
| 409 | * @param array $replacement |
||||
| 410 | * @return string|null |
||||
| 411 | */ |
||||
| 412 | protected function _replaceArrayString($identifier, $replacement): ?string |
||||
| 413 | { |
||||
| 414 | 5 | if (is_array($replacement)) |
|||
|
0 ignored issues
–
show
|
|||||
| 415 | { |
||||
| 416 | 5 | if (empty($replacement)) |
|||
| 417 | { |
||||
| 418 | $this->error_backtrace('Database error, given array of string values is empty. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 419 | } |
||||
| 420 | |||||
| 421 | foreach ($replacement as $key => $value) |
||||
| 422 | { |
||||
| 423 | $replacement[$key] = sprintf('\'%1$s\'', $this->escape_string($value)); |
||||
| 424 | } |
||||
| 425 | |||||
| 426 | return implode(', ', $replacement); |
||||
| 427 | 69 | } |
|||
| 428 | |||||
| 429 | 69 | $this->error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|||
| 430 | } |
||||
| 431 | 69 | ||||
| 432 | /** |
||||
| 433 | * Tests and casts to LOWER(column_name) (if needed) arrays of strings |
||||
| 434 | * for replacement__callback. |
||||
| 435 | * |
||||
| 436 | 69 | * @param string $identifier |
|||
| 437 | * @param array $replacement |
||||
| 438 | 69 | * @return string|null |
|||
| 439 | */ |
||||
| 440 | protected function _replaceArrayStringCaseInsensitive($identifier, $replacement): ?string |
||||
| 441 | { |
||||
| 442 | if (is_array($replacement)) |
||||
|
0 ignored issues
–
show
|
|||||
| 443 | 69 | { |
|||
| 444 | if (empty($replacement)) |
||||
| 445 | { |
||||
| 446 | 69 | $this->error_backtrace('Database error, given array of string values is empty. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|||
| 447 | } |
||||
| 448 | |||||
| 449 | foreach ($replacement as $key => $value) |
||||
| 450 | { |
||||
| 451 | $replacement[$key] = $this->_replaceStringCaseInsensitive($value); |
||||
| 452 | } |
||||
| 453 | |||||
| 454 | return implode(', ', $replacement); |
||||
| 455 | } |
||||
| 456 | |||||
| 457 | $this->error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 458 | } |
||||
| 459 | |||||
| 460 | /** |
||||
| 461 | * Tests and casts date for replacement__callback. |
||||
| 462 | 43 | * |
|||
| 463 | * @param mixed $identifier |
||||
| 464 | 43 | * @param mixed $replacement |
|||
| 465 | * @return string|null |
||||
| 466 | 43 | */ |
|||
| 467 | protected function _replaceDate($identifier, $replacement): ?string |
||||
| 468 | { |
||||
| 469 | if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
||||
| 470 | { |
||||
| 471 | 43 | return sprintf("'%04d-%02d-%02d'", $date_matches[1], $date_matches[2], $date_matches[3]); |
|||
| 472 | } |
||||
| 473 | 43 | ||||
| 474 | $this->error_backtrace('Wrong value type sent to the database. Date expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 475 | } |
||||
| 476 | 43 | ||||
| 477 | /** |
||||
| 478 | * Tests and casts floating numbers for replacement__callback. |
||||
| 479 | * |
||||
| 480 | * @param mixed $identifier |
||||
| 481 | * @param mixed $replacement |
||||
| 482 | * @return string |
||||
| 483 | */ |
||||
| 484 | protected function _replaceFloat($identifier, $replacement): string |
||||
| 485 | { |
||||
| 486 | if (!is_numeric($replacement)) |
||||
| 487 | { |
||||
| 488 | $this->error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $identifier . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||||
| 489 | } |
||||
| 490 | |||||
| 491 | return (string) (float) $replacement; |
||||
| 492 | } |
||||
| 493 | 2 | ||||
| 494 | /** |
||||
| 495 | 2 | * Quotes identifiers for replacement__callback. |
|||
| 496 | * |
||||
| 497 | 2 | * @param mixed $replacement |
|||
| 498 | * @return string |
||||
| 499 | */ |
||||
| 500 | protected function _replaceIdentifier($replacement) |
||||
| 501 | { |
||||
| 502 | 2 | if (preg_match('~[a-z_][0-9a-zA-Z$,_]{0,60}~', $replacement) !== 1) |
|||
| 503 | { |
||||
| 504 | 2 | $this->error_backtrace('Wrong value type sent to the database. Invalid identifier used. (' . $replacement . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|||
| 505 | } |
||||
| 506 | |||||
| 507 | 2 | return '`' . $replacement . '`'; |
|||
| 508 | } |
||||
| 509 | |||||
| 510 | /** |
||||
| 511 | * {@inheritDoc} |
||||
| 512 | */ |
||||
| 513 | public function fetchQuery($db_string, $db_values = []) |
||||
| 514 | { |
||||
| 515 | return $this->query('', $db_string, $db_values); |
||||
| 516 | } |
||||
| 517 | |||||
| 518 | /** |
||||
| 519 | * {@inheritDoc} |
||||
| 520 | */ |
||||
| 521 | public function query($identifier, $db_string, $db_values = []) |
||||
| 522 | { |
||||
| 523 | 3 | // One more query.... |
|||
| 524 | $this->_query_count++; |
||||
| 525 | 3 | ||||
| 526 | $db_string = $this->initialChecks($db_string, $db_values, $identifier); |
||||
| 527 | 3 | ||||
| 528 | if (trim($db_string) === '') |
||||
| 529 | { |
||||
| 530 | throw new \Exception('Query string empty'); |
||||
| 531 | } |
||||
| 532 | |||||
| 533 | $db_string = $this->_prepareQuery($db_string, $db_values); |
||||
| 534 | |||||
| 535 | $this->_preQueryDebug($db_string); |
||||
| 536 | |||||
| 537 | $this->_doSanityCheck($db_string); |
||||
| 538 | |||||
| 539 | $this->executeQuery($db_string); |
||||
| 540 | |||||
| 541 | if ($this->_db_last_result === false && !$this->_skip_error) |
||||
| 542 | { |
||||
| 543 | 1 | $this->_db_last_result = $this->error($db_string); |
|||
| 544 | } |
||||
| 545 | 1 | ||||
| 546 | // Revert not to skip errors |
||||
| 547 | if ($this->_skip_error) |
||||
| 548 | { |
||||
| 549 | $this->_skip_error = false; |
||||
| 550 | 1 | } |
|||
| 551 | |||||
| 552 | // Debugging. |
||||
| 553 | $this->_postQueryDebug(); |
||||
| 554 | |||||
| 555 | return $this->result; |
||||
| 556 | } |
||||
| 557 | |||||
| 558 | /** |
||||
| 559 | * Actually execute the DBMS-specific code to run the query |
||||
| 560 | * |
||||
| 561 | * @param string $db_string |
||||
| 562 | */ |
||||
| 563 | abstract protected function executeQuery($db_string); |
||||
| 564 | |||||
| 565 | /** |
||||
| 566 | * {@inheritDoc} |
||||
| 567 | */ |
||||
| 568 | abstract public function error($db_string); |
||||
| 569 | |||||
| 570 | /** |
||||
| 571 | * Prepares the strings to show the error to the user/admin and stop the code execution |
||||
| 572 | * |
||||
| 573 | 277 | * @param string $db_string |
|||
| 574 | * @param string $query_error |
||||
| 575 | 277 | * @param string $file |
|||
| 576 | * @param int $line |
||||
| 577 | * @throws Exception |
||||
| 578 | */ |
||||
| 579 | protected function throwError($db_string, $query_error, $file, $line): void |
||||
| 580 | { |
||||
| 581 | global $context, $txt, $modSettings, $db_show_debug; |
||||
| 582 | |||||
| 583 | // Nothing's defined yet... just die with it. |
||||
| 584 | if (empty($context) || empty($txt)) |
||||
| 585 | { |
||||
| 586 | die($query_error); |
||||
|
0 ignored issues
–
show
|
|||||
| 587 | } |
||||
| 588 | |||||
| 589 | // Show an error message, if possible. |
||||
| 590 | $context['error_title'] = $txt['database_error']; |
||||
| 591 | $message = $txt['try_again']; |
||||
| 592 | |||||
| 593 | // Add database version that we know of, for the admin to know. (and ask for support) |
||||
| 594 | if (allowedTo('admin_forum')) |
||||
| 595 | { |
||||
| 596 | $message = nl2br($query_error) . '<br />' . $txt['file'] . ': ' . $file . '<br />' . $txt['line'] . ': ' . $line . |
||||
| 597 | '<br /><br />' . sprintf($txt['database_error_versions'], $modSettings['elkVersion']); |
||||
| 598 | |||||
| 599 | if ($db_show_debug === true) |
||||
| 600 | { |
||||
| 601 | $message .= '<br /><br />' . nl2br($db_string); |
||||
| 602 | } |
||||
| 603 | } |
||||
| 604 | |||||
| 605 | // It's already been logged... don't log it again. |
||||
| 606 | throw new Exception($message, false); |
||||
| 607 | } |
||||
| 608 | |||||
| 609 | /** |
||||
| 610 | * {@inheritDoc} |
||||
| 611 | */ |
||||
| 612 | abstract public function insert($method, $table, $columns, $data, $keys, $disable_trans = false); |
||||
| 613 | |||||
| 614 | /** |
||||
| 615 | * Prepares the data that will be later implode'd into the actual query string |
||||
| 616 | * |
||||
| 617 | * @param string $table |
||||
| 618 | * @param array $columns |
||||
| 619 | * @param array $data |
||||
| 620 | * @return array |
||||
| 621 | * @throws \Exception |
||||
| 622 | */ |
||||
| 623 | protected function prepareInsert($table, $columns, $data): array |
||||
| 624 | { |
||||
| 625 | // With nothing to insert, simply return. |
||||
| 626 | if (empty($data)) |
||||
| 627 | { |
||||
| 628 | throw new \Exception('No data to insert'); |
||||
| 629 | } |
||||
| 630 | |||||
| 631 | // Inserting data as a single row can be done as a single array. |
||||
| 632 | if (!is_array($data[array_rand($data)])) |
||||
| 633 | { |
||||
| 634 | 38 | $data = [$data]; |
|||
| 635 | } |
||||
| 636 | 38 | ||||
| 637 | 38 | // Replace the prefix holder with the actual prefix. |
|||
| 638 | $table = str_replace('{db_prefix}', $this->_db_prefix, $table); |
||||
| 639 | $this->_skip_error = $table === $this->_db_prefix . 'log_errors'; |
||||
| 640 | |||||
| 641 | // Create the mold for a single row insert. |
||||
| 642 | $insertData = '('; |
||||
| 643 | foreach ($columns as $columnName => $type) |
||||
| 644 | { |
||||
| 645 | // Are we restricting the length? |
||||
| 646 | if (strpos($type, 'string-') !== false) |
||||
| 647 | { |
||||
| 648 | $insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
||||
| 649 | } |
||||
| 650 | else |
||||
| 651 | { |
||||
| 652 | $insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
||||
| 653 | } |
||||
| 654 | } |
||||
| 655 | |||||
| 656 | $insertData = substr($insertData, 0, -2) . ')'; |
||||
| 657 | |||||
| 658 | // Create an array consisting of only the columns. |
||||
| 659 | $indexed_columns = array_keys($columns); |
||||
| 660 | |||||
| 661 | // Here's where the variables are injected to the query. |
||||
| 662 | $insertRows = []; |
||||
| 663 | foreach ($data as $dataRow) |
||||
| 664 | { |
||||
| 665 | $insertRows[] = $this->quote($insertData, $this->_array_combine($indexed_columns, $dataRow)); |
||||
| 666 | } |
||||
| 667 | |||||
| 668 | return [$table, $indexed_columns, $insertRows]; |
||||
| 669 | } |
||||
| 670 | |||||
| 671 | /** |
||||
| 672 | * {@inheritDoc} |
||||
| 673 | */ |
||||
| 674 | abstract public function replace($table, $columns, $data, $keys, $disable_trans = false); |
||||
| 675 | |||||
| 676 | /** |
||||
| 677 | * {@inheritDoc} |
||||
| 678 | */ |
||||
| 679 | public function escape_wildcard_string($string, $translate_human_wildcards = false) |
||||
| 680 | { |
||||
| 681 | $replacements = [ |
||||
| 682 | '%' => '\%', |
||||
| 683 | '_' => '\_', |
||||
| 684 | '\\' => '\\\\', |
||||
| 685 | ]; |
||||
| 686 | |||||
| 687 | if ($translate_human_wildcards) |
||||
| 688 | { |
||||
| 689 | $replacements += [ |
||||
| 690 | '*' => '%', |
||||
| 691 | ]; |
||||
| 692 | } |
||||
| 693 | |||||
| 694 | return strtr($string, $replacements); |
||||
| 695 | } |
||||
| 696 | |||||
| 697 | /** |
||||
| 698 | 6 | * {@inheritDoc} |
|||
| 699 | */ |
||||
| 700 | public function connection() |
||||
| 701 | 6 | { |
|||
| 702 | // find it, find it |
||||
| 703 | return $this->connection; |
||||
|
0 ignored issues
–
show
|
|||||
| 704 | } |
||||
| 705 | |||||
| 706 | /** |
||||
| 707 | 6 | * {@inheritDoc} |
|||
| 708 | */ |
||||
| 709 | public function num_queries() |
||||
| 710 | { |
||||
| 711 | return $this->_query_count; |
||||
| 712 | } |
||||
| 713 | |||||
| 714 | /** |
||||
| 715 | * {@inheritDoc} |
||||
| 716 | */ |
||||
| 717 | public function skip_next_error() |
||||
| 718 | { |
||||
| 719 | $this->_skip_error = true; |
||||
| 720 | } |
||||
| 721 | |||||
| 722 | /** |
||||
| 723 | * {@inheritDoc} |
||||
| 724 | */ |
||||
| 725 | public function truncate($table) |
||||
| 726 | { |
||||
| 727 | return $this->fetchQuery(' |
||||
| 728 | TRUNCATE ' . $table, |
||||
| 729 | [] |
||||
| 730 | ); |
||||
| 731 | } |
||||
| 732 | |||||
| 733 | /** |
||||
| 734 | 6 | * Set the unbuffered state for the connection |
|||
| 735 | * |
||||
| 736 | * @param bool $state |
||||
| 737 | 6 | */ |
|||
| 738 | public function setUnbuffered($state): void |
||||
| 739 | { |
||||
| 740 | $this->_unbuffered = (bool) $state; |
||||
| 741 | } |
||||
| 742 | |||||
| 743 | 6 | /** |
|||
| 744 | * Get the version number. |
||||
| 745 | * |
||||
| 746 | * @return string - the version |
||||
| 747 | * @throws Exception |
||||
| 748 | */ |
||||
| 749 | abstract public function client_version(); |
||||
| 750 | |||||
| 751 | /** |
||||
| 752 | * Return server info. |
||||
| 753 | * |
||||
| 754 | * @return string |
||||
| 755 | */ |
||||
| 756 | abstract public function server_info(); |
||||
| 757 | |||||
| 758 | /** |
||||
| 759 | * Whether the database system is case sensitive. |
||||
| 760 | * |
||||
| 761 | * @return bool |
||||
| 762 | */ |
||||
| 763 | abstract public function case_sensitive(); |
||||
| 764 | |||||
| 765 | /** |
||||
| 766 | * Get the name (title) of the database system. |
||||
| 767 | * |
||||
| 768 | * @return string |
||||
| 769 | */ |
||||
| 770 | abstract public function title(); |
||||
| 771 | |||||
| 772 | /** |
||||
| 773 | * Returns whether the database system supports ignore. |
||||
| 774 | * |
||||
| 775 | * @return false |
||||
| 776 | */ |
||||
| 777 | abstract public function support_ignore(); |
||||
| 778 | |||||
| 779 | /** |
||||
| 780 | * Get the version number. |
||||
| 781 | * |
||||
| 782 | * @return string - the version |
||||
| 783 | * @throws Exception |
||||
| 784 | */ |
||||
| 785 | abstract public function server_version(); |
||||
| 786 | |||||
| 787 | /** |
||||
| 788 | * Temporary function to support migration to the new schema of the db layer |
||||
| 789 | * |
||||
| 790 | * @deprecated since 2.0 |
||||
| 791 | */ |
||||
| 792 | public function fetch_row($result): bool|array |
||||
| 793 | { |
||||
| 794 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::fetch_row()', 'Result::fetch_row()'); |
||||
| 795 | if ($result === false) |
||||
| 796 | { |
||||
| 797 | return false; |
||||
| 798 | } |
||||
| 799 | 19 | ||||
| 800 | return $result->fetch_row(); |
||||
| 801 | } |
||||
| 802 | 19 | ||||
| 803 | /** |
||||
| 804 | * Temporary function to support migration to the new schema of the db layer |
||||
| 805 | * |
||||
| 806 | * @deprecated since 2.0 |
||||
| 807 | */ |
||||
| 808 | public function fetch_assoc($result): array|bool |
||||
| 809 | { |
||||
| 810 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::fetch_assoc()', 'Result::fetch_assoc()'); |
||||
| 811 | if ($result === false) |
||||
| 812 | { |
||||
| 813 | return false; |
||||
| 814 | } |
||||
| 815 | |||||
| 816 | return $result->fetch_assoc(); |
||||
| 817 | } |
||||
| 818 | |||||
| 819 | /** |
||||
| 820 | * Temporary function to support migration to the new schema of the db layer |
||||
| 821 | * |
||||
| 822 | * @deprecated since 2.0 |
||||
| 823 | */ |
||||
| 824 | public function free_result($result) |
||||
| 825 | { |
||||
| 826 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::free_result()', 'Result::free_result()'); |
||||
| 827 | if ($result === false) |
||||
| 828 | { |
||||
| 829 | return; |
||||
| 830 | } |
||||
| 831 | |||||
| 832 | return $result->free_result(); |
||||
| 833 | } |
||||
| 834 | |||||
| 835 | /** |
||||
| 836 | * Temporary function to support migration to the new schema of the db layer |
||||
| 837 | * |
||||
| 838 | * @deprecated since 2.0 |
||||
| 839 | */ |
||||
| 840 | public function affected_rows(): int |
||||
| 841 | 79 | { |
|||
| 842 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::affected_rows()', 'Result::affected_rows()'); |
||||
| 843 | 79 | return $this->result->affected_rows(); |
|||
| 844 | } |
||||
| 845 | 79 | ||||
| 846 | /** |
||||
| 847 | 79 | * Temporary function to support migration to the new schema of the db layer |
|||
| 848 | * |
||||
| 849 | * @deprecated since 2.0 |
||||
| 850 | */ |
||||
| 851 | 30 | public function num_rows($result): int |
|||
| 852 | 30 | { |
|||
| 853 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::num_rows()', 'Result::num_rows()'); |
||||
| 854 | 30 | if ($result === false) |
|||
| 855 | { |
||||
| 856 | 30 | return 0; |
|||
| 857 | } |
||||
| 858 | |||||
| 859 | return (int) $result->num_rows(); |
||||
| 860 | } |
||||
| 861 | 30 | ||||
| 862 | /** |
||||
| 863 | * Temporary function to support migration to the new schema of the db layer |
||||
| 864 | * |
||||
| 865 | * @deprecated since 2.0 |
||||
| 866 | */ |
||||
| 867 | public function num_fields($result): int |
||||
| 868 | { |
||||
| 869 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::num_fields()', 'Result::num_fields()'); |
||||
| 870 | if ($result === false) |
||||
| 871 | { |
||||
| 872 | return 0; |
||||
| 873 | } |
||||
| 874 | |||||
| 875 | 301 | return $result->num_fields(); |
|||
| 876 | } |
||||
| 877 | 301 | ||||
| 878 | /** |
||||
| 879 | 301 | * Temporary function to support migration to the new schema of the db layer |
|||
| 880 | * |
||||
| 881 | 37 | * @deprecated since 2.0 |
|||
| 882 | */ |
||||
| 883 | public function insert_id($table) |
||||
| 884 | 301 | { |
|||
| 885 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::insert_id()', 'Result::insert_id()'); |
||||
| 886 | return $this->result->insert_id(); |
||||
|
0 ignored issues
–
show
|
|||||
| 887 | 299 | } |
|||
| 888 | |||||
| 889 | /** |
||||
| 890 | 299 | * Temporary function to support migration to the new schema of the db layer |
|||
| 891 | 299 | * |
|||
| 892 | * @deprecated since 2.0 |
||||
| 893 | 299 | */ |
|||
| 894 | public function data_seek($result, $counter) |
||||
| 895 | 299 | { |
|||
| 896 | 299 | // \ElkArte\Errors\Errors::instance()->log_deprecated('Query::data_seek()', 'Result::data_seek()'); |
|||
| 897 | return $result->data_seek($counter); |
||||
| 898 | } |
||||
| 899 | |||||
| 900 | 299 | /** |
|||
| 901 | * Temporary function: I'm not sure if this is the best place to have it, though it was |
||||
| 902 | * convenient while fixing other issues. |
||||
| 903 | 301 | * |
|||
| 904 | * @deprecated since 2.0 |
||||
| 905 | */ |
||||
| 906 | public function supportMediumtext() |
||||
| 907 | { |
||||
| 908 | return false; |
||||
| 909 | } |
||||
| 910 | |||||
| 911 | /** |
||||
| 912 | 301 | * Temporary function to support migration to the new schema of the db layer |
|||
| 913 | * |
||||
| 914 | 301 | * @deprecated since 2.0 |
|||
| 915 | */ |
||||
| 916 | abstract public function list_tables($db_name_str = false, $filter = false); |
||||
| 917 | 301 | ||||
| 918 | /** |
||||
| 919 | * This function combines the keys and values of the data passed to db::insert. |
||||
| 920 | * |
||||
| 921 | * @param int[] $keys |
||||
| 922 | * @param array $values |
||||
| 923 | * @return array |
||||
| 924 | */ |
||||
| 925 | protected function _array_combine($keys, $values): array |
||||
| 926 | { |
||||
| 927 | $is_numeric = array_filter(array_keys($values), 'is_numeric'); |
||||
| 928 | |||||
| 929 | if (!empty($is_numeric)) |
||||
| 930 | { |
||||
| 931 | return array_combine($keys, $values); |
||||
| 932 | } |
||||
| 933 | |||||
| 934 | $combined = []; |
||||
| 935 | foreach ($keys as $key) |
||||
| 936 | { |
||||
| 937 | if (isset($values[$key])) |
||||
| 938 | { |
||||
| 939 | 301 | $combined[$key] = $values[$key]; |
|||
| 940 | } |
||||
| 941 | } |
||||
| 942 | |||||
| 943 | // @todo should throw an E_WARNING if count($combined) != count($keys) |
||||
| 944 | 301 | return $combined; |
|||
| 945 | } |
||||
| 946 | 301 | ||||
| 947 | /** |
||||
| 948 | 301 | * Checks for "illegal characters" and runs replacement__callback if not |
|||
| 949 | * overridden. |
||||
| 950 | * In case of problems, the method can ends up dying. |
||||
| 951 | * |
||||
| 952 | * @param string $db_string |
||||
| 953 | * @param mixed $db_values |
||||
| 954 | 301 | * @return string |
|||
| 955 | */ |
||||
| 956 | protected function _prepareQuery($db_string, $db_values): string |
||||
| 957 | { |
||||
| 958 | global $modSettings; |
||||
| 959 | |||||
| 960 | if (empty($modSettings['disableQueryCheck']) && empty($db_values['security_override']) && strpos($db_string, "'") !== false) |
||||
| 961 | { |
||||
| 962 | $this->error_backtrace('Hacking attempt...', "Illegal character (') used in query...", true, __FILE__, __LINE__); |
||||
| 963 | } |
||||
| 964 | 301 | ||||
| 965 | if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
||||
| 966 | 301 | { |
|||
| 967 | // Store these values for use in the callback function. |
||||
| 968 | $this->_db_callback_values = $db_values; |
||||
| 969 | 301 | ||||
| 970 | 301 | // Inject the values passed to this function. |
|||
| 971 | $count = -1; |
||||
| 972 | 301 | while (($count > 0 && isset($db_values['recursive'])) || $count === -1) |
|||
| 973 | 301 | { |
|||
| 974 | 301 | $db_string = preg_replace_callback('~{([a-z_]+)(?::([.a-zA-Z0-9_-]+))?}~', |
|||
| 975 | fn($matches) => $this->replacement__callback($matches), $db_string, -1, $count); |
||||
| 976 | 301 | } |
|||
| 977 | 301 | ||||
| 978 | // No need for them any longer. |
||||
| 979 | 301 | $this->_db_callback_values = []; |
|||
| 980 | } |
||||
| 981 | 122 | ||||
| 982 | return $db_string; |
||||
| 983 | 122 | } |
|||
| 984 | |||||
| 985 | 122 | /** |
|||
| 986 | 122 | * Some initial checks and replacement of text insside the query string |
|||
| 987 | * |
||||
| 988 | 122 | * @param string $db_string |
|||
| 989 | * @param mixed $db_values |
||||
| 990 | * @param string $identifier The old (now mostly unused) query identifier |
||||
| 991 | * @return string |
||||
| 992 | 122 | */ |
|||
| 993 | abstract protected function initialChecks($db_string, $db_values, $identifier = ''); |
||||
| 994 | 122 | ||||
| 995 | 122 | /** |
|||
| 996 | * Tracks the initial status (time, file/line, query) for performance evaluation. |
||||
| 997 | * |
||||
| 998 | 10 | * @param string $db_string |
|||
| 999 | */ |
||||
| 1000 | protected function _preQueryDebug($db_string): void |
||||
| 1001 | 122 | { |
|||
| 1002 | 122 | global $db_show_debug, $time_start; |
|||
| 1003 | |||||
| 1004 | // Debugging. |
||||
| 1005 | 301 | if ($db_show_debug === true) |
|||
| 1006 | 301 | { |
|||
| 1007 | // We'll try recovering the file and line number the original db query was called from. |
||||
| 1008 | [$file, $line] = $this->backtrace_message(); |
||||
| 1009 | 301 | ||||
| 1010 | // Just in case nothing can be found from debug_backtrace |
||||
| 1011 | $file = $file ?? __FILE__; |
||||
| 1012 | $line = $line ?? __LINE__; |
||||
| 1013 | |||||
| 1014 | 301 | if (!empty($_SESSION['debug_redirect'])) |
|||
| 1015 | { |
||||
| 1016 | $this->_debug->merge_db($_SESSION['debug_redirect']); |
||||
| 1017 | // @todo this may be off by 1 |
||||
| 1018 | 301 | $this->_query_count += count($_SESSION['debug_redirect']); |
|||
| 1019 | $_SESSION['debug_redirect'] = []; |
||||
| 1020 | } |
||||
| 1021 | |||||
| 1022 | // Don't overload it. |
||||
| 1023 | 301 | $st = microtime(true); |
|||
| 1024 | $this->db_cache = []; |
||||
| 1025 | $this->db_cache['q'] = $this->_query_count < 50 ? $db_string : '...'; |
||||
| 1026 | $this->db_cache['f'] = $file; |
||||
| 1027 | $this->db_cache['l'] = $line; |
||||
| 1028 | 301 | $this->db_cache['s'] = $st - $time_start; |
|||
| 1029 | $this->db_cache['st'] = $st; |
||||
| 1030 | } |
||||
| 1031 | } |
||||
| 1032 | |||||
| 1033 | /** |
||||
| 1034 | * Closes up the tracking and stores everything in the debug class. |
||||
| 1035 | */ |
||||
| 1036 | protected function _postQueryDebug(): void |
||||
| 1037 | { |
||||
| 1038 | global $db_show_debug; |
||||
| 1039 | |||||
| 1040 | if ($db_show_debug === true) |
||||
| 1041 | { |
||||
| 1042 | $this->db_cache['t'] = microtime(true) - $this->db_cache['st']; |
||||
| 1043 | $this->_debug->db_query($this->db_cache); |
||||
| 1044 | $this->db_cache = []; |
||||
| 1045 | } |
||||
| 1046 | } |
||||
| 1047 | |||||
| 1048 | /** |
||||
| 1049 | * Checks the query doesn't have nasty stuff in it. |
||||
| 1050 | * In case of problems, the method can ends up dying. |
||||
| 1051 | * |
||||
| 1052 | * @param string $db_string |
||||
| 1053 | */ |
||||
| 1054 | protected function _doSanityCheck($db_string): void |
||||
| 1055 | { |
||||
| 1056 | global $modSettings; |
||||
| 1057 | |||||
| 1058 | // First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over. |
||||
| 1059 | $clean = ''; |
||||
| 1060 | if (empty($modSettings['disableQueryCheck'])) |
||||
| 1061 | { |
||||
| 1062 | $old_pos = 0; |
||||
| 1063 | $pos = -1; |
||||
| 1064 | while (true) |
||||
| 1065 | { |
||||
| 1066 | $pos = strpos($db_string, "'", $pos + 1); |
||||
| 1067 | if ($pos === false) |
||||
| 1068 | { |
||||
| 1069 | break; |
||||
| 1070 | } |
||||
| 1071 | |||||
| 1072 | $clean .= substr($db_string, $old_pos, $pos - $old_pos); |
||||
| 1073 | |||||
| 1074 | while (true) |
||||
| 1075 | { |
||||
| 1076 | $pos1 = strpos($db_string, "'", $pos + 1); |
||||
| 1077 | $pos2 = strpos($db_string, static::ESCAPE_CHAR, $pos + 1); |
||||
| 1078 | |||||
| 1079 | if ($pos1 === false) |
||||
| 1080 | { |
||||
| 1081 | break; |
||||
| 1082 | } |
||||
| 1083 | |||||
| 1084 | if ($pos2 === false || $pos2 > $pos1) |
||||
| 1085 | { |
||||
| 1086 | $pos = $pos1; |
||||
| 1087 | break; |
||||
| 1088 | } |
||||
| 1089 | |||||
| 1090 | $pos = $pos2 + 1; |
||||
| 1091 | } |
||||
| 1092 | |||||
| 1093 | $clean .= ' %s '; |
||||
| 1094 | $old_pos = $pos + 1; |
||||
| 1095 | } |
||||
| 1096 | |||||
| 1097 | $clean .= substr($db_string, $old_pos); |
||||
| 1098 | $clean = strtolower(trim(preg_replace($this->allowed_comments['from'], $this->allowed_comments['to'], $clean))); |
||||
| 1099 | |||||
| 1100 | // Comments? We don't use comments in our queries, we leave 'em outside! |
||||
| 1101 | if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
||||
| 1102 | { |
||||
| 1103 | $fail = true; |
||||
| 1104 | } |
||||
| 1105 | // Trying to change passwords, slow us down, or something? |
||||
| 1106 | elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~', $clean) === 1) |
||||
| 1107 | { |
||||
| 1108 | $fail = true; |
||||
| 1109 | } |
||||
| 1110 | elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~', $clean) === 1) |
||||
| 1111 | { |
||||
| 1112 | $fail = true; |
||||
| 1113 | } |
||||
| 1114 | |||||
| 1115 | if (!empty($fail) && class_exists(Errors::class)) |
||||
| 1116 | { |
||||
| 1117 | $this->error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
||||
| 1118 | } |
||||
| 1119 | } |
||||
| 1120 | } |
||||
| 1121 | } |
||||
| 1122 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths