Changeset 2982

Show
Ignore:
Timestamp:
07/07/08 12:59:03 (8 weeks ago)
Author:
Shadowhand
Message:

Fixing #610, thanks SuicidalWeasel?!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/download.php

    r2373 r2982  
    2222    public static function force($filename = '', $data = '') 
    2323    { 
    24         static $user_agent; 
    25  
    26         if ($filename == '') 
     24        if (empty($filename)) 
    2725            return FALSE; 
    2826 
     
    3230            $filepath = str_replace('\\', '/', realpath($filename)); 
    3331 
     32            // Set filesize 
     33            $filesize = filesize($filepath); 
     34 
     35            // Get filename 
     36            // Note: Do not use pathinfo for this, it may not be utf8 compatible 
     37            $filename = end(explode('/', $filepath)); 
     38 
    3439            // Get extension 
    3540            $extension = pathinfo($filepath, PATHINFO_EXTENSION); 
    36  
    37             // Remove directory path from the filename 
    38             $filename = end(explode('/', $filepath)); 
    39  
    40             // Set filesize 
    41             $filesize = filesize($filepath); 
    4241        } 
    4342        else 
    4443        { 
    45             // Grab the file extension 
     44            // Get filesize 
     45            $filesize = strlen($data); 
     46 
     47            // Make sure the filename does not have directory info 
     48            $filename = end(explode('/', $filename)); 
     49 
     50            // Get extension 
    4651            $extension = end(explode('.', $filename)); 
    47  
    48             // Try to determine if the filename includes a file extension. 
    49             // We need it in order to set the MIME type 
    50             if (empty($data) OR $extension === $filename) 
    51                 return FALSE; 
    52  
    53             // Set filesize 
    54             $filesize = strlen($data); 
    5552        } 
    5653 
    57         // Set a default mime if we can't find it 
    58         if (($mime = Config::item('mimes.'.$extension)) === NULL) 
     54        // Get the mime type of the file 
     55        $mime = Config::item('mimes.'.$extension); 
     56 
     57        if (empty($mime)) 
    5958        { 
    60             $mime = 'application/octet-stream'; 
    61         } 
    62         else 
    63         { 
    64             $mime = current((array) $mime); 
     59            // Set a default mime if none was found 
     60            $mime = array('application/octet-stream'); 
    6561        } 
    6662 
    6763        // Generate the server headers 
    68         header('Content-Type: '.$mime); 
     64        header('Content-Type: '.$mime[0]); 
    6965        header('Content-Disposition: attachment; filename="'.$filename.'"'); 
    7066        header('Content-Transfer-Encoding: binary'); 
     67        header('Content-Length: '.sprintf('%d', $filesize)); 
     68 
     69        // More caching prevention 
    7170        header('Expires: 0'); 
    72         header('Content-Length: '.$filesize); 
    7371 
    74         // IE headers 
    7572        if (Kohana::user_agent('browser') === 'Internet Explorer') 
    7673        { 
     74            // Send IE headers 
    7775            header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    7876            header('Pragma: public'); 
     
    8078        else 
    8179        { 
     80            // Send normal headers 
    8281            header('Pragma: no-cache'); 
    8382        } 
     83 
     84        // Flush the output buffer 
     85        Kohana::close_buffers(FALSE); 
    8486 
    8587        if (isset($filepath))