Changeset 723

Show
Ignore:
Timestamp:
11/29/06 16:24:29 (2 years ago)
Author:
ben
Message:

Allow files to be downloaded using a different handler.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • devel/_files/download.php

    r690 r723  
    4848                    @apache_setenv('no-gzip', '1'); 
    4949                } 
    50                 spitfile_with_mtime_check($CFG->dataroot . $file->location, $mimetype); 
     50                spitfile_with_mtime_check($CFG->dataroot . $file->location, $mimetype, $file->handler); 
    5151            } 
    5252        } 
  • devel/lib/filelib.php

    r690 r723  
    495495// outputs a file or 304 response to the browser, and exits 
    496496// NB: does not make any security checks, and is meant only as the final output stage 
    497 function spitfile_with_mtime_check ($filepath, $mimetype) { 
    498      
    499     if (is_file($filepath)) { 
    500         $tstamp = filemtime($filepath); 
    501         $filesize = filesize($filepath); 
    502         $lm = gmdate("D, d M Y H:i:s", $tstamp) . " GMT"; 
    503         $etag = md5($filepath . "_" . $tstamp . "_" . $mimetype . "_" . $filesize); 
    504         header('ETag: "' . $etag . '"'); 
     497function spitfile_with_mtime_check ($filepath, $mimetype, $handler = "local") { 
     498     
     499    if (is_file($filepath) || $handler != "local") { 
     500        if ($handler == "local") { 
     501            $tstamp = filemtime($filepath); 
     502            $filesize = filesize($filepath); 
     503            $lm = gmdate("D, d M Y H:i:s", $tstamp) . " GMT"; 
     504            $etag = md5($filepath . "_" . $tstamp . "_" . $mimetype . "_" . $filesize); 
     505            header('ETag: "' . $etag . '"'); 
     506     
     507            $timenow = time(); 
    505508             
    506         $timenow = time(); 
    507          
    508         // Send last-modified header to enable if-modified-since requests 
    509         if ($tstamp < $timenow) { 
    510             header("Last-Modified: " . $lm); 
    511             if ($tstamp < ($timenow - 3600)) { 
    512                 header('Expires: ' . gmdate("D, d M Y H:i:s", ($timenow+3600)) . " GMT"); 
    513             } 
    514         } 
    515          
    516         // Send 304s where possible, rather than spitting out the file each time 
    517         if (array_key_exists('HTTP_IF_NONE_MATCH',$_SERVER)) { 
    518             $if_none_match = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_NONE_MATCH']); 
    519             if ($if_none_match == $etag) { 
    520                 header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); 
    521                 exit; 
    522             } 
    523         } 
    524         if (array_key_exists('HTTP_IF_MODIFIED_SINCE',$_SERVER)) { 
    525             $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']); 
    526             if ($if_modified_since == $lm) { 
    527                 header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); 
    528                 exit; 
    529             } 
    530         } 
    531          
    532         if ($mimetype) { 
    533             header("Content-Type: $mimetype"); 
    534         } 
    535         readfile($filepath); 
     509            // Send last-modified header to enable if-modified-since requests 
     510            if ($tstamp < $timenow) { 
     511                header("Last-Modified: " . $lm); 
     512                if ($tstamp < ($timenow - 3600)) { 
     513                    header('Expires: ' . gmdate("D, d M Y H:i:s", ($timenow+3600)) . " GMT"); 
     514                } 
     515            } 
     516             
     517            // Send 304s where possible, rather than spitting out the file each time 
     518            if (array_key_exists('HTTP_IF_NONE_MATCH',$_SERVER)) { 
     519                $if_none_match = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_NONE_MATCH']); 
     520                if ($if_none_match == $etag) { 
     521                    header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); 
     522                    exit; 
     523                } 
     524            } 
     525            if (array_key_exists('HTTP_IF_MODIFIED_SINCE',$_SERVER)) { 
     526                $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']); 
     527                if ($if_modified_since == $lm) { 
     528                    header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); 
     529                    exit; 
     530                } 
     531            } 
     532             
     533            if ($mimetype) { 
     534                header("Content-Type: $mimetype"); 
     535            } 
     536            readfile($filepath); 
     537        } else { 
     538            $CFG->files->handler[$handler]($filepath); 
     539        } 
    536540    } else { 
    537541        header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found"); 
  • devel/mod/file/lib.php

    r659 r723  
    11<?php 
     2 
     3function file_init() { 
     4    global $CFG; 
     5     
     6    // Has the $CFG->files->default_handler been set? If not, set it to local 
     7        if (empty($CFG->files->default_handler)) { 
     8            $CFG->files->default_handler = "local"; 
     9        } 
     10} 
    211 
    312function file_pagesetup() {