// ....
<?php
public function init()
{
$this->_check_auth();
if ( ($post_max_size = app\string2bytes(ini_get("post_max_size")) - 1024) < 0)
{
$post_max_size = 0;
}
(new app_1c_views_exchange_base())->init ( class_exists("ZipArchive"), min ($post_max_size, 1024*1024*5) );
}
public function file()
{
$this->_check_auth();
if (! ($filename = app_request::request("filename")) )
{
throw new app_1c_exceptions_import("Переменная \$_GET['filename'] не передана");
}
$this->_model->save_input_file($filename);
if (preg_match("/\\.zip$/i", $filename))
{
app_sessions::add("1c_zip_files", $filename);
}
(new app_1c_views_exchange_base())->file();
}
....
/* Распаковка архива */
if (is_array($zip_files))
{
app_sessions::start();
app_sessions::set("1c_import_catalog_state", "progress");
app_sessions::set("1c_import_catalog_message", "Распаковка ZIP архивов");
app_sessions::write_close();
$this->unzip_files($zip_files, $base);
app_sessions::start();
app_sessions::delete("1c_zip_files");
}
....
/**
* Распаковка ZIP архивов
* @param unknown $zip_files
* @param unknown $base
* @throws app_1c_exceptions_import
* @return app_1c_models_catalog
*/
private function unzip_files($zip_files, $base)
{
foreach (array_unique($zip_files) as $_file)
{
$_src = $base.$_file;
$fs = new app_fs();
if (!$fs->is_file_in_secure_dirrectory($_src, $base))
{
throw new app_1c_exceptions_import("Ошибка. Файл {$_src} находится вне безопасной директории {$base}: {$fs->getErrors(";")}");
}
$zip_arhive = new ZipArchive();
if (! $zip_arhive->open($_src))
{
throw new app_1c_exceptions_import("Ошибка открытия {$_src}");
}
if (! $zip_arhive->extractTo($base))
{
throw new app_1c_exceptions_import("Ошибка распаковки ZIP архива {$_src} в {$base}");
}
$zip_arhive->close();
}
return $this;
}
// ...
public function save_input_file($filename)
{
$fs = new app_fs();
$base = "tmp/1c/".app_sessions::id()."/";
$path = $base.$filename;
if (! $fs->check_path_secure_symbols($filename) )
{
throw new app_1c_exceptions_import("Filename has unsecure symbols {$filename}");
}
if (!file_exists($base))
{
if (! $fs->mkdir($base) )
{
throw new app_1c_exceptions_import("Can't create base dir {$base}");
}
}
// Проверем перед сохранением
if (file_exists($path) || file_exists(dirname($path)))
{
if (! $fs->is_file_in_secure_dirrectory(file_exists($path) ? $path : dirname($path), $base) )
{
throw new app_1c_exceptions_import("Base dir failed for filename: {$fs->getErrors(";")}");
}
}
if (!$fs->mkdir($dir = dirname($path), true))
{
throw new app_1c_exceptions_import("Can't create directory {$dir}");
}
if (! $fs->copy("php://input", $path))
{
throw new app_1c_exceptions_import("Ошибка копирования php://input в {$path}: {$fs->getErrors(";")}");
}
// Повторно проверим
if (file_exists($path) || file_exists(dirname($path)))
{
if (! $fs->is_file_in_secure_dirrectory(file_exists($path) ? $path : dirname($path), $base) )
{
$fs->unlink($path);
throw new app_1c_exceptions_import("Base dir failed for filename: {$fs->getErrors(";")}");
}
}
return true;
}