diff --git a/import.php b/import.php index 121d279442..3f0a00595f 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) { @@ -365,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; @@ -382,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); } } @@ -392,7 +430,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 +441,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 +452,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 +464,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 +473,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 +484,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 +514,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,17 +552,17 @@ 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); } } -if (! $error && false !== $import_handle && null !== $import_handle) { +if (false !== $import_handle && null !== $import_handle) { fclose($import_handle); }