From 8ed117ca50aa743f9b029594c3817d3dd454357e Mon Sep 17 00:00:00 2001 From: Roy-Orbison Date: Mon, 7 Aug 2023 17:22:55 +0930 Subject: [PATCH] Allow selection of SQL files to import instead of only accepting the very non-descriptive adminer.sql[.gz], and make it possible to use a source directory other than Adminer's own. --- plugins/import-from-dir.php | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 plugins/import-from-dir.php diff --git a/plugins/import-from-dir.php b/plugins/import-from-dir.php new file mode 100644 index 00000000..7d65d301 --- /dev/null +++ b/plugins/import-from-dir.php @@ -0,0 +1,85 @@ +dir = $dir; + } + + protected function _readFiles($gz = false) { + $mapped = array(); + $glob = "$this->dir*.[Ss][Qq][Ll]"; + if ($gz) { + $suffix = '.gz'; # lowercase only because of core + $glob .= $suffix; + $suffix_cut = -3; + } + if ($files = glob($glob)) { + $from = strlen($this->dir); + foreach ($files as $file) { + if ($from) { + $file = substr($file, $from); # do not expose server paths in output + } + if ($gz) { + $mapped[substr($file, 0, $suffix_cut)] = $file; + } + else { + $mapped[$file] = $file; + } + } + } + return $mapped; + } + + function importServerPath() { + static $posted = null; + + $files = $this->_readFiles(); + if (extension_loaded('zlib')) { + $files += $this->_readFiles(true); # core prioritises files without .gz + } + if (count($files) > 1) { + ksort($files); + } + + if ($posted !== null || !isset($_POST['webfile'])) { + # use existing translation strings + echo "
" . lang('From server') . "
"; + echo lang('Webserver file %s', ''); + echo ' '; + echo "
\n"; + $posted = null; + return false; # skip core UI + } + + if ( + empty($_POST['webfilename']) + || !is_string($_POST['webfilename']) + || !array_key_exists($_POST['webfilename'], $files) + ) { + $posted = ''; + return 'SELECTED_FILE_DOES_NOT_EXIST'; # can't return empty string because of core file_exists() check + } + + $posted = $_POST['webfilename']; + return $this->dir . $posted; + } +}