<?php
/**
 * Manage zip archives
 *
 * @category PHP
 * @package  VenoFileManager
 * @author   Nicola Franchini <info@veno.it>
 * @license  https://veno.es/shop/help Veno Commercial License
 * @link     http://filemanager.veno.it/
 */
if (!class_exists('Zipper', false)) {
    /**
     * Zipper class
     *
     * @category PHP
     * @package  VenoFileManager
     * @author   Nicola Franchini <info@veno.it>
     * @license  https://veno.es/shop/help Veno Commercial License
     * @link     http://filemanager.veno.it/
     */
    class Zipper
    {
        /**
         * Prepare ZIP archive
         *
         * @param array  $files  files array to download
         * @param string $folder folder path or false
         *
         * @return file served
         */
        public function prepareZip(
            $files = false,
            $folder = false
        ) {
            $response = array('error' => false);

            // create /tmp/ folder if needed
            $temp_path = dirname(__DIR__).'/tmp';
            if (!is_dir($temp_path)) {
                if (!mkdir($temp_path, 0755, true)) {
                    $response['error'] = 'Cannot create a tmp dir for .zip files';
                    return $response;
                }
            }

            global $setUp;

            $time = time();
            $json_name = md5($folder.$time);
            $hash = md5($setUp->getConfig('salt').$time);

            $starting_dir = $setUp->getConfig('starting_dir');
            $cleanpath = dirname(dirname(__DIR__)).'/'.ltrim($starting_dir, './');

            $filesarray = array();

            if ($files && is_array($files)) {
                $response['name'] = 'zip-'.$time.'.zip';
                foreach ($files as $pezzo) {
                    // Keep files inside main dir and avoid tricky injections.
                    $cleanfile = $setUp->sanitizePath(urldecode(base64_decode($pezzo)));
                    if (realpath($cleanpath.$cleanfile)) {
                        $filesarray[] = $cleanfile;
                    }
                }
            }

            if ($folder) {
                $cleanfolder = $setUp->sanitizePath($folder);
                if (realpath($cleanpath.$cleanfolder)) {
                    $folder = $cleanfolder;
                } else {
                    $folder = false;
                }
                $folderpathinfo = Utils::mbPathinfo($cleanfolder);
                $archivename = Utils::checkMagicQuotes($folderpathinfo['filename']);
                $response['name'] = $archivename.'.zip';
            }

            $saveData = array(
                'time' => $time,
                'hash' => $hash,
                'dir' => $folder,
                'files' => $filesarray,
            );

            // save dowloadable link if it does not already exists
            if (!file_exists($temp_path.'/'.$json_name.'.json')) {
                $fp = fopen($temp_path.'/'.$json_name.'.json', 'w');
                fwrite($fp, json_encode($saveData));
                fclose($fp);
            }

            // Delete tmp file if is older than 48 hours
            $oldtmp = glob($temp_path.'/*');
            foreach ($oldtmp as $oldfile) {
                if (is_file($oldfile)) {
                    if (filemtime($oldfile) < time() - 60*60*48) {
                        unlink($oldfile);
                    }
                }
            }

            $response['json'] = $json_name;
            $response['supah'] = md5($time.$hash);

            return $response;
        }
    }
}
