Back to PhocacartDownload class

Method download

public static
download
(mixed $id)

Method download - Source code

public static function download($id)
{
    $file = self::getDownloadFile((int) $id);
    $user = PhocacartUser::getUser();
    $app = Factory::getApplication();
    $tokenDownload = $app->input->post->get('d', '', 'string');
    $tokenOrder = $app->input->post->get('o', '', 'string');
    $pC = PhocacartUtils::getComponentParameters();
    $download_days = $pC->get('download_days', 0);
    $download_count = $pC->get('download_count', 0);
    $download_guest_access = $pC->get('download_guest_access', 0);
    if ($download_guest_access == 0) {
        $token = '';
    }
    // CHECK USER AND TOKEN
    if ((int) $user->id < 1 && ($tokenDownload == '' || $tokenOrder == '')) {
        return false;
    }
    if (!isset($file->userid) && ($tokenDownload == '' || $tokenOrder == '')) {
        return false;
    }
    if ($user->id != $file->userid && ($tokenDownload == '' || $tokenOrder == '')) {
        return false;
    }
    if ((int) $user->id < 1 && ($tokenDownload == '' || $tokenOrder == '') && $token != $file->download_token) {
        return false;
    }
    // CHECK COUNT
    if ($download_count > 0 && ((int) $download_count == (int) $file->download_hits || (int) $download_count < (int) $file->download_hits)) {
        return false;
    }
    // CHECK DAYS (download days from ordered file can override the download days set in phoca cart parameters)
    $downloadDays = (int) $download_days;
    if (isset($file->download_days) && (int) $file->download_days > -1) {
        $downloadDays = (int) $file->download_days;
    }
    if ($downloadDays != 0 && !PhocacartDownload::isActive($file->date, $downloadDays)) {
        return false;
    }
    // Clears file status cache
    clearstatcache();
    $pathFile = PhocacartPath::getPath('productfile');
    $absOrRelFile = $pathFile['orig_abs_ds'] . $file->download_file;
    if (!File::exists($absOrRelFile)) {
        return false;
    }
    $fileWithoutPath = basename($absOrRelFile);
    $fileSize = filesize($absOrRelFile);
    if (function_exists('finfo_open') && function_exists('finfo_open') && function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $f = finfo_file($finfo, $absOrRelFile);
        finfo_close($finfo);
        $mimeType = $f;
    } else {
        if (function_exists('mime_content_type')) {
            // we have mime magic
            $mimeType = mime_content_type($absOrRelFile);
        } else {
            $mimeType = '';
        }
    }
    // HIT Statistics
    self::hit($id);
    /*if ((int)$params->get('send_mail_download', 0) > 0) {
    			PhocacartMail::sendMail((int)$params->get('send_mail_download', 0), $fileWithoutPath, 1);
    		}*/
    if ($fileSize == 0) {
        die(Text::_('COM_PHOCACART_FILE_SIZE_EMPTY'));
        exit;
    }
    // Clean the output buffer
    ob_end_clean();
    // test for protocol and set the appropriate headers
    jimport('joomla.environment.uri');
    $_tmp_uri = Uri::getInstance(Uri::current());
    $_tmp_protocol = $_tmp_uri->getScheme();
    if ($_tmp_protocol == "https") {
        // SSL Support
        header('Cache-Control: private, max-age=0, must-revalidate, no-store');
    } else {
        header("Cache-Control: public, must-revalidate");
        header('Cache-Control: pre-check=0, post-check=0, max-age=0');
        header("Pragma: no-cache");
        header("Expires: 0");
    }
    /* end if protocol https */
    header("Content-Description: File Transfer");
    header("Expires: Sat, 30 Dec 1990 07:07:07 GMT");
    header("Accept-Ranges: bytes");
    // Modified by Rene
    // HTTP Range - see RFC2616 for more informations (http://www.ietf.org/rfc/rfc2616.txt)
    $httpRange = 0;
    $newFileSize = $fileSize - 1;
    // Default values! Will be overridden if a valid range header field was detected!
    $resultLength = (string) $fileSize;
    $resultRange = "0-" . $newFileSize;
    // We support requests for a single range only.
    // So we check if we have a range field. If yes ensure that it is a valid one.
    // If it is not valid we ignore it and sending the whole file.
    if (isset($_SERVER['HTTP_RANGE']) && preg_match('%^bytes=\\d*\\-\\d*$%', $_SERVER['HTTP_RANGE'])) {
        // Let's take the right side
        list($a, $httpRange) = explode('=', $_SERVER['HTTP_RANGE']);
        // and get the two values (as strings!)
        $httpRange = explode('-', $httpRange);
        // Check if we have values! If not we have nothing to do!
        if (!empty($httpRange[0]) || !empty($httpRange[1])) {
            // We need the new content length ...
            $resultLength = $fileSize - $httpRange[0] - $httpRange[1];
            // ... and we can add the 206 Status.
            header("HTTP/1.1 206 Partial Content");
            // Now we need the content-range, so we have to build it depending on the given range!
            // ex.: -500 -> the last 500 bytes
            if (empty($httpRange[0])) {
                $resultRange = $resultLength . '-' . $newFileSize;
            } elseif (empty($httpRange[1])) {
                $resultRange = $httpRange[0] . '-' . $newFileSize;
            } else {
                $resultRange = $httpRange[0] . '-' . $httpRange[1];
            }
            //header("Content-Range: bytes ".$httpRange . $newFileSize .'/'. $fileSize);
        }
    }
    header("Content-Length: " . $resultLength);
    header("Content-Range: bytes " . $resultRange . '/' . $fileSize);
    header("Content-Type: " . (string) $mimeType);
    header('Content-Disposition: attachment; filename="' . $fileWithoutPath . '"');
    header("Content-Transfer-Encoding: binary\n");
    //@readfile($absOrRelFile);
    // Try to deliver in chunks
    @set_time_limit(0);
    $fp = @fopen($absOrRelFile, 'rb');
    if ($fp !== false) {
        while (!feof($fp)) {
            echo fread($fp, 8192);
        }
        fclose($fp);
    } else {
        @readfile($absOrRelFile);
    }
    flush();
    exit;
}