SearchController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Database;
  4. use PhpMyAdmin\Database\Search;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\Html\Generator;
  7. use PhpMyAdmin\Response;
  8. use PhpMyAdmin\Template;
  9. use PhpMyAdmin\Url;
  10. use PhpMyAdmin\Util;
  11. class SearchController extends AbstractController
  12. {
  13. /** @var DatabaseInterface */
  14. private $dbi;
  15. /**
  16. * @param Response $response
  17. * @param string $db Database name.
  18. * @param DatabaseInterface $dbi
  19. */
  20. public function __construct($response, Template $template, $db, $dbi)
  21. {
  22. parent::__construct($response, $template, $db);
  23. $this->dbi = $dbi;
  24. }
  25. public function index(): void
  26. {
  27. global $cfg, $db, $err_url, $url_params, $tables, $num_tables, $total_num_tables, $sub_part;
  28. global $tooltip_truename, $tooltip_aliasname, $pos;
  29. $this->addScriptFiles([
  30. 'database/search.js',
  31. 'vendor/stickyfill.min.js',
  32. 'sql.js',
  33. 'makegrid.js',
  34. ]);
  35. Util::checkParameters(['db']);
  36. $err_url = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  37. $err_url .= Url::getCommon(['db' => $db], '&');
  38. if (! $this->hasDatabase()) {
  39. return;
  40. }
  41. // If config variable $cfg['UseDbSearch'] is on false : exit.
  42. if (! $cfg['UseDbSearch']) {
  43. Generator::mysqlDie(
  44. __('Access denied!'),
  45. '',
  46. false,
  47. $err_url
  48. );
  49. }
  50. $url_params['goto'] = Url::getFromRoute('/database/search');
  51. // Create a database search instance
  52. $databaseSearch = new Search($this->dbi, $db, $this->template);
  53. // Display top links if we are not in an Ajax request
  54. if (! $this->response->isAjax()) {
  55. [
  56. $tables,
  57. $num_tables,
  58. $total_num_tables,
  59. $sub_part,,,
  60. $tooltip_truename,
  61. $tooltip_aliasname,
  62. $pos,
  63. ] = Util::getDbInfo($db, $sub_part ?? '');
  64. }
  65. // Main search form has been submitted, get results
  66. if (isset($_POST['submit_search'])) {
  67. $this->response->addHTML($databaseSearch->getSearchResults());
  68. }
  69. // If we are in an Ajax request, we need to exit after displaying all the HTML
  70. if ($this->response->isAjax() && empty($_REQUEST['ajax_page_request'])) {
  71. return;
  72. }
  73. // Display the search form
  74. $this->response->addHTML($databaseSearch->getMainHtml());
  75. }
  76. }