From 315c307b89954743877bd41ed66f8a6d70898315 Mon Sep 17 00:00:00 2001 From: yzsolt Date: Mon, 17 Feb 2014 12:43:07 +0100 Subject: [PATCH 1/4] Exit immediately on import error. Signed-off-by: yzsolt --- import.php | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/import.php b/import.php index 121d279442..0839f40c72 100644 --- a/import.php +++ b/import.php @@ -358,6 +358,30 @@ if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) { $import_file = 'none'; } +// This helper function "stops" the import on (mostly upload/file related) error +// It's more a hack than a solution, the whole import code needs serious revision +function _pma_stop_import( PMA_Message $error_message ) { + global $import_handle, $file_to_unlink; + + // Close open handles + if ($import_handle !== false && $import_handle !== null) { + fclose($import_handle); + } + + // Delete temporary file + if ($file_to_unlink != '') { + unlink($file_to_unlink); + } + + $_SESSION['Import_message']['message'] = $error_message->getDisplay(); + + $response = PMA_Response::getInstance(); + $response->isSuccess(false); + $response->addJSON('message', PMA_Message::error($msg)); + + exit; +} + // Do we have file to import? if ($import_file != 'none' && ! $error) { @@ -392,7 +416,7 @@ if ($import_file != 'none' && ! $error) { $compression = PMA_detectCompression($import_file); if ($compression === false) { $message = PMA_Message::error(__('File could not be read!')); - $error = true; + _pma_stop_import($message); } else { switch ($compression) { case 'application/bzip2': @@ -403,7 +427,7 @@ if ($import_file != 'none' && ! $error) { __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.') ); $message->addParam($compression); - $error = true; + _pma_stop_import($message); } break; case 'application/gzip': @@ -414,7 +438,7 @@ if ($import_file != 'none' && ! $error) { __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.') ); $message->addParam($compression); - $error = true; + _pma_stop_import($message); } break; case 'application/zip': @@ -426,7 +450,7 @@ if ($import_file != 'none' && ! $error) { $zipResult = PMA_getZipContents($import_file); if (! empty($zipResult['error'])) { $message = PMA_Message::rawError($zipResult['error']); - $error = true; + _pma_stop_import($message); } else { $import_text = $zipResult['data']; } @@ -435,7 +459,7 @@ if ($import_file != 'none' && ! $error) { __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.') ); $message->addParam($compression); - $error = true; + _pma_stop_import($message); } break; case 'none': @@ -446,21 +470,21 @@ if ($import_file != 'none' && ! $error) { __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.') ); $message->addParam($compression); - $error = true; + _pma_stop_import($message); break; } } // use isset() because zip compression type does not use a handle if (! $error && isset($import_handle) && $import_handle === false) { $message = PMA_Message::error(__('File could not be read!')); - $error = true; + _pma_stop_import($message); } } elseif (! $error) { if (! isset($import_text) || empty($import_text)) { $message = PMA_Message::error( __('No data was received to import. Either no file name was submitted, or the file size exceeded the maximum size permitted by your PHP configuration. See [doc@faq1-16]FAQ 1.16[/doc].') ); - $error = true; + _pma_stop_import($message); } } @@ -476,10 +500,10 @@ if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_fil if (PMA_DRIZZLE) { // Drizzle doesn't support other character sets, // so we can't fallback to SET NAMES - throw an error - $error = true; $message = PMA_Message::error( __('Cannot convert file\'s character set without character set conversion library!') ); + _pma_stop_import($message); } else { $GLOBALS['dbi']->query('SET NAMES \'' . $charset_of_file . '\''); // We can not show query in this case, it is in different charset @@ -514,10 +538,10 @@ if (! $error) { $import_type ); if ($import_plugin == null) { - $error = true; $message = PMA_Message::error( __('Could not load import plugins, please check your installation!') ); + _pma_stop_import($message); } else { // Do the real import $import_plugin->doImport($sql_data); From 7022f5a1d5767661be2b9159f4f40c63be72369d Mon Sep 17 00:00:00 2001 From: yzsolt Date: Mon, 17 Feb 2014 12:43:41 +0100 Subject: [PATCH 2/4] This check is unnecessary. Signed-off-by: yzsolt --- import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/import.php b/import.php index 0839f40c72..2a3c7db4eb 100644 --- a/import.php +++ b/import.php @@ -548,7 +548,7 @@ if (! $error) { } } -if (! $error && false !== $import_handle && null !== $import_handle) { +if (false !== $import_handle && null !== $import_handle) { fclose($import_handle); } From e27a5ccbacc85d808428a9b377db8b5bd8e23df6 Mon Sep 17 00:00:00 2001 From: yzsolt Date: Mon, 17 Feb 2014 12:45:01 +0100 Subject: [PATCH 3/4] Use the tmp directory provided by the system instead of a hard-coded one. Signed-off-by: yzsolt --- import.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/import.php b/import.php index 2a3c7db4eb..907067eca5 100644 --- a/import.php +++ b/import.php @@ -389,16 +389,17 @@ if ($import_file != 'none' && ! $error) { $open_basedir = @ini_get('open_basedir'); // If we are on a server with open_basedir, we must move the file - // before opening it. The doc explains how to create the "./tmp" - // directory + // before opening it. if (! empty($open_basedir)) { - $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/'); + /** + * @todo make use of the config's temp dir with fallback to the system's tmp dir + */ + $tmp_subdir = sys_get_temp_dir(); if (is_writable($tmp_subdir)) { - $import_file_new = $tmp_subdir . basename($import_file) . uniqid(); if (move_uploaded_file($import_file, $import_file_new)) { $import_file = $import_file_new; From 1c1d95e8bb816103ed7a485f38a30b3ac62b6574 Mon Sep 17 00:00:00 2001 From: yzsolt Date: Mon, 17 Feb 2014 12:47:26 +0100 Subject: [PATCH 4/4] Show a meaningful error if open_basedir is misconfigured (the tmp directoy is unavailable). Signed-off-by: yzsolt --- import.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/import.php b/import.php index 907067eca5..3f0a00595f 100644 --- a/import.php +++ b/import.php @@ -407,6 +407,19 @@ if ($import_file != 'none' && ! $error) { } $size = filesize($import_file); + } else { + + // If the php.ini is misconfigured (eg. there is no /tmp access defined + // with open_basedir), $tmp_subdir won't be writable and the user gets + // a 'File could not be read!' error (at PMA_detectCompression), which + // is not too meaningful. Show a meaningful error message to the user + // instead. + + $message = PMA_Message::error( + __('Uploaded file cannot be moved, because the server has open_basedir enabled without access to the %s directory (for temporary files).') + ); + $message->addParam($tmp_subdir); + _pma_stop_import($message); } }