function add_files_to_archive_root(&$archive, $base_upload_dir, $user_template_dir, $base_preview_dir, $user_theme_name)
{
    save_config($base_preview_dir, $base_upload_dir, $user_theme_name);
    $paths = array('Readme.txt' => FilesHelper::normalizePath($user_template_dir . '/Readme.txt'), 'Config.xml' => FilesHelper::normalizePath($base_upload_dir . '/designer/Export/Config.xml'));
    foreach ($paths as $name => $path) {
        if (!file_exists($path)) {
            continue;
        }
        $dir = dirname($path);
        addToArchive($archive, $path, $dir);
    }
}
function makeArchive($pluginName, $generationBaseDir, $templateBaseDir)
{
    $zip = new ZipArchive();
    $filename = $pluginName . '.zip';
    $filepath = $generationBaseDir . '/' . $filename;
    if (file_exists($filepath)) {
        $flags = ZipArchive::OVERWRITE;
    } else {
        $flags = ZipArchive::CREATE;
    }
    if ($zip->open($filepath, $flags) !== TRUE) {
        exit("Impossible d'ouvrir le fichier <{$filepath}>\n");
    }
    //add generated files
    addToArchive($zip, $generationBaseDir . '/' . $pluginName, '.');
    //add static files
    addToArchive($zip, $templateBaseDir . '/static', '.');
    $zip->close();
    downloadFile($filename, $filepath);
    //delete file
    unlink($filepath);
}
function uploadTheme($themeName, $filename, $tmp_path)
{
    $uniqid = uniqid();
    $sandbox = FilesHelper::normalizePath(_PS_CACHE_DIR_ . 'sandbox' . DIRECTORY_SEPARATOR . $uniqid) . '/';
    $uploadDir = $sandbox . ThemeInstallHelper::UPLOADED_THEME_DIR_NAME;
    // used in AdminThemesController
    $base_path = $uploadDir . '.zip';
    FilesHelper::createDir($sandbox);
    FilesHelper::renameFile($tmp_path, $base_path);
    $archive = new PclZip($base_path);
    deleteFromArchive($archive, 'modules', true);
    // We upload new theme, not modules to save module functionality
    $helper = new ThemeInstallHelper();
    if ($helper->install($base_path, $sandbox)) {
        $result = array('status' => 'done', 'path' => $base_path);
    } else {
        $themesDir = $uploadDir . '/themes';
        $files = FilesHelper::enumerate($themesDir, false);
        // not enumerateFiles method with directory checking
        if (count($files) === 1 && is_dir($files[0]['path'])) {
            $themeName = str_replace("{$themesDir}/", '', $files[0]['path']);
        }
        if ($themeName && ($theme = Theme::getByDirectory($themeName)) && is_file($uploadDir . '/Config.xml')) {
            $themeDir = $themesDir . '/' . $themeName;
            $previewThemeDir = $themeDir . '/' . $themeName . _PREVIEW_SUFFIX_;
            $newThemeName = checkThemeName($themeName);
            $newThemeDir = $themesDir . '/' . $newThemeName;
            $newThemePreviewDir = $themeDir . '/' . $newThemeName . _PREVIEW_SUFFIX_;
            // 1. rename inner preview folder
            processRename($themeDir, $previewThemeDir, $newThemeDir, $newThemePreviewDir, $newThemeName);
            $newThemePreviewDir = $newThemeDir . '/' . $newThemeName . _PREVIEW_SUFFIX_;
            // 2. now preview folder is correct
            FilesHelper::deleteFile($uploadDir . '/Config.xml');
            FilesHelper::copyFile($newThemePreviewDir . '/designer/Export/Config.xml', $uploadDir . '/Config.xml');
            unset($archive);
            FilesHelper::deleteFile($base_path);
            $archive = new PclZip($base_path);
            addToArchive($archive, $uploadDir, $uploadDir);
            $helper = new ThemeInstallHelper();
            // new object with no errors
            if ($helper->install($base_path, $sandbox)) {
                $result = array('status' => 'done', 'path' => $base_path);
            } else {
                $result = array('status' => 'error', 'errors' => Tools::jsonEncode($helper->getErrors()));
            }
        } else {
            $result = array('status' => 'error', 'errors' => 'Please make sure, that you have selected a valid PrestaShop theme.');
        }
    }
    return $result;
}