// Store the query as a bookmark before executing it if bookmarklabel was given

if (!empty($bkm_label) &&!empty($import_text)) {

require_once'./libraries/bookmark.lib.php';

$bfields=array(

'dbase'=>$db,

'user'=>$cfg['Bookmark']['user'],

'query'=>urlencode($import_text),

'label'=>$bkm_label

);

// Should we replace bookmark?

if (isset($bkm_replace)) {

$bookmarks= PMA_Bookmark_getList($db);

foreach ($bookmarksas$key=>$val) {

if ($val==$bkm_label) {

PMA_Bookmark_delete($db, $key);

}

}

}

PMA_Bookmark_save($bfields, isset($bkm_all_users));

$bookmark_created=TRUE;

} // end store bookmarks

// We can not read all at once, otherwise we can run out of memory

$memory_limit=trim(@ini_get('memory_limit'));

// 2 MB as default

if (empty($memory_limit)) {

$memory_limit=2*1024*1024;

}

// In case no memory limit we work on 10MB chunks

if ($memory_limit==-1) {

$memory_limit=10*1024*1024;

}

// Calculate value of the limit

if (strtolower(substr($memory_limit, -1)) =='m') {

$memory_limit= (int)substr($memory_limit, 0, -1) *1024*1024;

} elseif (strtolower(substr($memory_limit, -1)) =='k') {

$memory_limit= (int)substr($memory_limit, 0, -1) *1024;

} elseif (strtolower(substr($memory_limit, -1)) =='g') {

$memory_limit= (int)substr($memory_limit, 0, -1) *1024*1024*1024;

} else {

$memory_limit= (int)$memory_limit;

}

$read_limit=$memory_limit/8; // Just to be sure, there might be lot of memory needed for uncompression

// handle filenames

if (!empty($local_import_file) &&!empty($cfg['UploadDir'])) {

// sanitize $local_import_file as it comes from a POST

$local_import_file= PMA_securePath($local_import_file);

$import_file= PMA_userDir($cfg['UploadDir']) .$local_import_file;

} elseif (empty($import_file) ||!is_uploaded_file($import_file)) {

$import_file='none';

}

// Do we have file to import?

if ($import_file!='none'&&!$error) {

// work around open_basedir and other limitations

$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

if (!empty($open_basedir)) {

$tmp_subdir= (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');

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;

$file_to_unlink=$import_file_new;

}

$size=filesize($import_file);

}

}

/**

* Handle file compression

* @todo duplicate code exists in File.class.php

*/

$compression= PMA_detectCompression($import_file);

if ($compression===FALSE) {

$message=PMA_Message::error(__('File could not be read'));

$error=TRUE;

} else {

switch ($compression) {

case'application/bzip2':

if ($cfg['BZipDump'] &&@function_exists('bzopen')) {

$import_handle=@bzopen($import_file, 'r');

} else {

$message=PMA_Message::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;

}

break;

case'application/gzip':

if ($cfg['GZipDump'] &&@function_exists('gzopen')) {

$import_handle=@gzopen($import_file, 'r');

} else {

$message=PMA_Message::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;

}

break;

case'application/zip':

if ($cfg['ZipDump'] &&@function_exists('zip_open')) {

/**

* Load interface for zip extension.

*/

include_once'./libraries/zip_extension.lib.php';

$result= PMA_getZipContents($import_file);

if (!empty($result['error'])) {

$message=PMA_Message::rawError($result['error']);

$error=TRUE;

} else {

$import_text=$result['data'];

}

} else {

$message=PMA_Message::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;

}

break;

case'none':

$import_handle=@fopen($import_file, 'r');

break;

default:

$message=PMA_Message::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;

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;

}

} 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 [a@./Documentation.html#faq1_16@Documentation]FAQ 1.16[/a].'));