ImportController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Table;
  4. use PhpMyAdmin\Charsets;
  5. use PhpMyAdmin\Charsets\Charset;
  6. use PhpMyAdmin\Config\PageSettings;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\DatabaseInterface;
  9. use PhpMyAdmin\DbTableExists;
  10. use PhpMyAdmin\Encoding;
  11. use PhpMyAdmin\Import;
  12. use PhpMyAdmin\Import\Ajax;
  13. use PhpMyAdmin\Message;
  14. use PhpMyAdmin\Plugins;
  15. use PhpMyAdmin\Response;
  16. use PhpMyAdmin\Template;
  17. use PhpMyAdmin\Url;
  18. use PhpMyAdmin\Util;
  19. use function intval;
  20. final class ImportController extends AbstractController
  21. {
  22. /** @var DatabaseInterface */
  23. private $dbi;
  24. /**
  25. * @param Response $response
  26. * @param string $db Database name.
  27. * @param string $table Table name.
  28. * @param DatabaseInterface $dbi
  29. */
  30. public function __construct($response, Template $template, $db, $table, $dbi)
  31. {
  32. parent::__construct($response, $template, $db, $table);
  33. $this->dbi = $dbi;
  34. }
  35. public function index(): void
  36. {
  37. global $db, $max_upload_size, $table, $url_params, $SESSION_KEY, $cfg, $PMA_Theme, $err_url;
  38. $pageSettings = new PageSettings('Import');
  39. $pageSettingsErrorHtml = $pageSettings->getErrorHTML();
  40. $pageSettingsHtml = $pageSettings->getHTML();
  41. $this->addScriptFiles(['import.js']);
  42. Util::checkParameters(['db', 'table']);
  43. $url_params = ['db' => $db, 'table' => $table];
  44. $err_url = Util::getScriptNameForOption($cfg['DefaultTabTable'], 'table');
  45. $err_url .= Url::getCommon($url_params, '&');
  46. DbTableExists::check();
  47. $url_params['goto'] = Url::getFromRoute('/table/import');
  48. $url_params['back'] = Url::getFromRoute('/table/import');
  49. [$SESSION_KEY, $uploadId] = Ajax::uploadProgressSetup();
  50. $importList = Plugins::getImport('table');
  51. if (empty($importList)) {
  52. $this->response->addHTML(Message::error(__(
  53. 'Could not load import plugins, please check your installation!'
  54. ))->getDisplay());
  55. return;
  56. }
  57. $offset = null;
  58. if (Core::isValid($_REQUEST['offset'], 'numeric')) {
  59. $offset = intval($_REQUEST['offset']);
  60. }
  61. $timeoutPassed = $_REQUEST['timeout_passed'] ?? null;
  62. $localImportFile = $_REQUEST['local_import_file'] ?? null;
  63. $compressions = Import::getCompressions();
  64. $allCharsets = Charsets::getCharsets($this->dbi, $cfg['Server']['DisableIS']);
  65. $charsets = [];
  66. /** @var Charset $charset */
  67. foreach ($allCharsets as $charset) {
  68. $charsets[] = [
  69. 'name' => $charset->getName(),
  70. 'description' => $charset->getDescription(),
  71. ];
  72. }
  73. $idKey = $_SESSION[$SESSION_KEY]['handler']::getIdKey();
  74. $hiddenInputs = [
  75. $idKey => $uploadId,
  76. 'import_type' => 'table',
  77. 'db' => $db,
  78. 'table' => $table,
  79. ];
  80. $this->render('table/import/index', [
  81. 'page_settings_error_html' => $pageSettingsErrorHtml,
  82. 'page_settings_html' => $pageSettingsHtml,
  83. 'upload_id' => $uploadId,
  84. 'handler' => $_SESSION[$SESSION_KEY]['handler'],
  85. 'theme_image_path' => $PMA_Theme->getImgPath(),
  86. 'hidden_inputs' => $hiddenInputs,
  87. 'db' => $db,
  88. 'table' => $table,
  89. 'max_upload_size' => $max_upload_size,
  90. 'import_list' => $importList,
  91. 'local_import_file' => $localImportFile,
  92. 'is_upload' => $GLOBALS['is_upload'],
  93. 'upload_dir' => $cfg['UploadDir'] ?? null,
  94. 'timeout_passed_global' => $GLOBALS['timeout_passed'] ?? null,
  95. 'compressions' => $compressions,
  96. 'is_encoding_supported' => Encoding::isSupported(),
  97. 'encodings' => Encoding::listEncodings(),
  98. 'import_charset' => $cfg['Import']['charset'] ?? null,
  99. 'timeout_passed' => $timeoutPassed,
  100. 'offset' => $offset,
  101. 'can_convert_kanji' => Encoding::canConvertKanji(),
  102. 'charsets' => $charsets,
  103. 'is_foreign_key_check' => Util::isForeignKeyCheck(),
  104. 'user_upload_dir' => Util::userDir($cfg['UploadDir'] ?? ''),
  105. 'local_files' => Import::getLocalFiles($importList),
  106. ]);
  107. }
  108. }