<?php

function theme_get_normalized_path($path)
{
    if (substr($path, 1, 1) == ':') {
        $path = substr($path, 2);
        $path = str_replace('\\', '/', $path);
    }
    return $path;
}

function theme_add_to_archive(&$archive, $args = array())
{
    $archive_file = $args['archive_file'];
    $from         = $args['from'];
    $remove_path  = isset($args['remove_path']) ? $args['remove_path'] : '';
    $add_path     = isset($args['add_path'])    ? $args['add_path']    : '';
    $except       = isset($args['except'])      ? $args['except']      : array();

    if ($dh = opendir($from)) {
        while(($file = readdir($dh)) !== false) {
            if ($file === '.' || $file === '..')
                continue;
            $path = theme_get_normalized_path($from . '/' . $file);
            if (in_array($path, $except))
                continue;
            if ($archive === null) {
                $archive = new PclZip($archive_file);
                $v_list = $archive->create($path, PCLZIP_OPT_ADD_PATH, $add_path, PCLZIP_OPT_REMOVE_PATH, $remove_path);
            } else {
                $v_list = $archive->add($path, PCLZIP_OPT_ADD_PATH, $add_path, PCLZIP_OPT_REMOVE_PATH, $remove_path);
            }
            if ($v_list == 0)
                die("Error : " . $archive->errorInfo(true));
        }
        closedir($dh);
    }
}

function theme_get_theme_archive(){
    $template = get_template();
    $preview_template = $template . '_preview';
    $theme_root = get_theme_root( $template );
    $base_template_dir = get_template_directory();
    $preview_template_dir = FilesHelper::normalize_path($base_template_dir . '_preview');

    if (!file_exists($base_template_dir) || !file_exists($preview_template_dir)){
        die('Error : No Theme Folders');
    }

    require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');

    $base_upload_dir = wp_upload_dir();
    if (false !== $base_upload_dir['error']) {
        die('Upload folder error!');
    }
    $archive = null;

    $base_template_dir = theme_get_normalized_path($base_template_dir);
    $preview_template_dir = theme_get_normalized_path($preview_template_dir);
    $remove_path = theme_get_normalized_path($theme_root);

    $archive_name = 'theme_' . uniqid(time()) .  '.zip';
    $archive_file = $base_upload_dir['basedir'] . '/' . $archive_name;

    $new_template = $_POST['themeName'];
    if (!$new_template)
        die('Error: theme name is empty');
    $preview_new_template = $new_template . '_preview';
    theme_add_to_archive($archive, array(
        'archive_file' => $archive_file,
        'from'         => $base_template_dir,
        'remove_path'  => $remove_path . '/' . $template,
        'add_path'     => $new_template,
        'except'       => array($base_template_dir . '/preview')
    ));
    theme_add_to_archive($archive, array(
        'archive_file' => $archive_file,
        'from'         => $preview_template_dir,
        'remove_path'  => $remove_path . '/' . $preview_template,
        'add_path'     => $new_template . '/preview/' . $preview_new_template,
        'except'       => array($preview_template_dir . '/preview')
    ));
    return $archive_file;
}