AbstractController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers;
  4. use PhpMyAdmin\Core;
  5. use PhpMyAdmin\Message;
  6. use PhpMyAdmin\Response;
  7. use PhpMyAdmin\Template;
  8. use PhpMyAdmin\Url;
  9. use function strlen;
  10. abstract class AbstractController
  11. {
  12. /** @var Response */
  13. protected $response;
  14. /** @var Template */
  15. protected $template;
  16. /**
  17. * @param Response $response
  18. */
  19. public function __construct($response, Template $template)
  20. {
  21. $this->response = $response;
  22. $this->template = $template;
  23. }
  24. /**
  25. * @param array<string, mixed> $templateData
  26. */
  27. protected function render(string $templatePath, array $templateData = []): void
  28. {
  29. $this->response->addHTML($this->template->render($templatePath, $templateData));
  30. }
  31. /**
  32. * @param string[] $files
  33. */
  34. protected function addScriptFiles(array $files): void
  35. {
  36. $header = $this->response->getHeader();
  37. $scripts = $header->getScripts();
  38. $scripts->addFiles($files);
  39. }
  40. protected function hasDatabase(): bool
  41. {
  42. global $db, $is_db, $errno, $dbi, $message;
  43. if (isset($is_db) && $is_db) {
  44. return true;
  45. }
  46. $is_db = false;
  47. if (strlen($db) > 0) {
  48. $is_db = $dbi->selectDb($db);
  49. // This "Command out of sync" 2014 error may happen, for example
  50. // after calling a MySQL procedure; at this point we can't select
  51. // the db but it's not necessarily wrong
  52. if ($dbi->getError() && $errno == 2014) {
  53. $is_db = true;
  54. unset($errno);
  55. }
  56. }
  57. if (strlen($db) === 0 || ! $is_db) {
  58. if ($this->response->isAjax()) {
  59. $this->response->setRequestStatus(false);
  60. $this->response->addJSON(
  61. 'message',
  62. Message::error(__('No databases selected.'))
  63. );
  64. return false;
  65. }
  66. // Not a valid db name -> back to the welcome page
  67. $params = ['reload' => '1'];
  68. if (isset($message)) {
  69. $params['message'] = $message;
  70. }
  71. $uri = './index.php?route=/' . Url::getCommonRaw($params, '&');
  72. Core::sendHeaderLocation($uri);
  73. return false;
  74. }
  75. return $is_db;
  76. }
  77. }