1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 
<?php

/* --------------------------------------------------------------
    PackedDownloadController.inc.php 2016-02-05
    Gambio GmbH
    http://www.gambio.de
    Copyright (c) 2015 Gambio GmbH
    Released under the GNU General Public License (Version 2)
    [http://www.gnu.org/licenses/gpl-2.0.html]
    --------------------------------------------------------------
*/

/**
 * Class PackedDownloadController
 * 
 * This class implements a mass downloader. It can be used to have the shop retrieve a list of resources (denoted by their URLs)
 * and pack them in a ZIP file. The ZIP file is stored in the cache folder and can then be downloaded.
 * 
 * @extends    AdminHttpViewController
 * @category   System
 * @package    AdminHttpViewControllers
 */
class PackedDownloadController extends AdminHttpViewController
{
    /**
     * initializes the controller
     * @param HttpContextReaderInterface
     * @param HttpResponseProcessorInterface
     * @param ContentViewInterface
     */
    public function __construct(HttpContextReaderInterface $httpContextReader,
                                HttpResponseProcessorInterface $httpResponseProcessor,
                                ContentViewInterface $contentView)
    {
        parent::__construct($httpContextReader, $httpResponseProcessor, $contentView);
    }
    
    
    /**
     * Returns an error (JSON-encoded) as this controller does not have a default action
     * @return JsonHttpControllerResponse
     */
    public function defaultAction()
    {
        return MainFactory::create('JsonHttpControllerResponse', array('error' => 'invalid action'));
    }
    
    
    /**
     * Retrieves a number of resources and puts them in a ZIP for later retrieval by the user.
     *
     * Input is a JSON-encoded list (array) of URLs. Response is a JSON object with a downloadKey property (cf. actionDownloadPackage())
     * or an error message.
     *
     * @return JsonHttpControllerResponse
     */
    public function actionDownloadByJson()
    {
        $response = array();
        $rawInput = file_get_contents('php://input');
        $json     = json_decode($rawInput);
        
        try
        {
            $tmpDir = $this->makeTmpDir();
            $files  = array();
            foreach($json->urls as $url)
            {
                $files[] = $this->downloadFile($url, $tmpDir);
            }
            $downloadKey             = $this->packForDownload($tmpDir, $files);
            $response['input']       = $json;
            $response['result']      = 'OK';
            $response['downloadKey'] = $downloadKey;
        }
        catch(Exception $e)
        {
            $response['result']        = 'ERROR';
            $response['error_message'] = $e->getMessage();
        }
        if(!empty($tmpDir)
           && is_dir($tmpDir)
        ) // to be refactored into a finally block when system requirements are raised to PHP 5.5
        {
            $this->cleanUp($tmpDir);
        }
        
        return MainFactory::create('JsonHttpControllerResponse', $response);
    }
    
    
    /**
     * Download a pack of resources retrieved with actionDownloadByJson() identified by a downloadKey
     * @return HttpControllerResponse (empty; download is initiated prior to standard response handling)
     */
    public function actionDownloadPackage()
    {
        $downloadKey  = preg_replace('_[^[:alnum:]]_', '', (string)$this->_getQueryParameter('key'));
        $downloadFile = DIR_FS_CATALOG . '/cache/PDC_' . $downloadKey . '.zip';
        if(!(is_file($downloadFile) && is_readable($downloadFile)))
        {
            return MainFactory::create('HttpControllerResponse', 'invalid key');
        }
        ob_clean();
        header('Content-Description: Bundled files');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="PackedDownload_' . date('YmdHis') . '.zip"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($downloadFile));
        readfile($downloadFile);
        unlink($downloadFile);
        
        return MainFactory::create('HttpControllerResponse', '');
    }
    
    
    /**
     * Retrieve a file/resource and store in $tmpDir
     * File name is determined from URL.
     * @param  string $url
     * @param  string $tmpDir
     * @return string name of retrieved file
     */
    protected function downloadFile($url, $tmpDir)
    {
        $targetFile = basename($url);
        if(empty($targetFile))
        {
            throw new Exception('ERROR - could not derive file name from URL');
        }
        $out_fp      = fopen(realpath($tmpDir) . '/' . $targetFile, 'w');
        $followLocation = ini_get('open_basedir') == '' && (ini_get('safe_mode') == 'Off' || ini_get('safe_mode') == false);
        $curlOptions = array(
            CURLOPT_URL            => $url,
            CURLOPT_FOLLOWLOCATION => $followLocation,
            CURLOPT_USERAGENT      => 'Gambio GX2 PackedDownload',
            CURLOPT_FILE           => $out_fp,
        );
        $ch          = curl_init();
        curl_setopt_array($ch, $curlOptions);
        $curl_success = curl_exec($ch);
        $curl_errno   = curl_errno($ch);
        $curl_error   = curl_error($ch);
        $curl_info    = curl_getinfo($ch);
        curl_close($ch);
        fclose($out_fp);
        if($curl_success === false)
        {
            throw new Exception(sprintf("%s - %s", $curl_errno, $curl_error));
        }
        
        return $targetFile;
    }
    
    
    /**
     * Packs up files in $tmpDir into a ZIP file
     * @param  string
     * @param  array
     * @return downloadKey
     */
    protected function packForDownload($tmpDir, $files)
    {
        $downloadKey = sha1(uniqid() . (string)microtime(true));
        $zip         = new ZipArchive();
        $zipFileName = DIR_FS_CATALOG . '/cache/PDC_' . $downloadKey . '.zip';
        if($zip->open($zipFileName, ZipArchive::CREATE) !== true)
        {
            throw new Exception('ERROR cannot create archive file');
        }
        $filesOK = true;
        foreach($files as $filename)
        {
            $filesOK = $filesOK && $zip->addFile($tmpDir . '/' . $filename, $filename);
            if($filesOK !== true)
            {
                break;
            }
        }
        $zip->close();
        if($filesOK !== true)
        {
            unlink($zipFileName);
            throw new Exception('ERROR cannot add file to archive');
        }
        
        return $downloadKey;
    }
    
    
    /**
     * Clean up $tmpDir
     * @param  $tmpDir
     * @return bool
     */
    protected function cleanUp($tmpDir)
    {
        $tmpDirIterator = new DirectoryIterator('glob://' . $tmpDir . '/*');
        foreach($tmpDirIterator as $directoryEntry)
        {
            if($directoryEntry->isFile())
            {
                unlink($directoryEntry->getPathname());
            }
        }
        rmdir($tmpDir);
        
        return true;
    }
    
    
    /**
     * Makes a new tmpDir location
     * @return string full path of tmpDir
     */
    protected function makeTmpDir()
    {
        $baseDir    = DIR_FS_CATALOG . '/cache/';
        $tmpDirName = sha1(uniqid() . (string)microtime(true));
        $tmpDir     = $baseDir . $tmpDirName;
        $success    = mkdir($tmpDir, 0700);
        if($success === false)
        {
            throw new Exception('ERROR creating temporary directory');
        }
        
        return $tmpDir;
    }
}