LintController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Represents the interface between the linter and the query editor.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Controllers;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Linter;
  9. use function is_array;
  10. use function is_string;
  11. use function json_encode;
  12. /**
  13. * Represents the interface between the linter and the query editor.
  14. */
  15. class LintController extends AbstractController
  16. {
  17. public const EDITOR_SQL_PREFIX = [
  18. 'event' => "DELIMITER $$ CREATE EVENT `a` ON SCHEDULE EVERY MINUTE DO\n",
  19. 'routine' => "DELIMITER $$ CREATE PROCEDURE `a`()\n",
  20. 'trigger' => "DELIMITER $$ CREATE TRIGGER `a` AFTER INSERT ON `b` FOR EACH ROW\n",
  21. ];
  22. public function __invoke(): void
  23. {
  24. $sqlQueryParam = $_POST['sql_query'] ?? null;
  25. $options = $_POST['options'] ?? null;
  26. /**
  27. * The SQL query to be analyzed.
  28. *
  29. * This does not need to be checked against XSS or MySQL injections because it is
  30. * never executed, just parsed.
  31. *
  32. * The client, which will receive the JSON response will decode the message and
  33. * and any HTML fragments that are displayed to the user will be encoded anyway.
  34. */
  35. $sqlQuery = is_string($sqlQueryParam) ? $sqlQueryParam : '';
  36. $editorType = is_array($options) ? ($options['editorType'] ?? null) : null;
  37. $prefix = is_string($editorType) ? self::EDITOR_SQL_PREFIX[$editorType] ?? '' : '';
  38. $lints = Linter::lint($prefix . $sqlQuery);
  39. if ($prefix !== '') {
  40. // Adjust positions to account for prefix
  41. foreach ($lints as $i => $lint) {
  42. if ($lint['fromLine'] === 0) {
  43. continue;
  44. }
  45. $lints[$i]['fromLine'] -= 1;
  46. $lints[$i]['toLine'] -= 1;
  47. }
  48. }
  49. $this->response->setAjax(true);
  50. // Disabling standard response.
  51. $this->response->disable();
  52. Core::headerJSON();
  53. echo json_encode($lints);
  54. }
  55. }