Initial Kohana install
[speedfreak] / Server / system / helpers / download.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Download helper class.
4  *
5  * $Id: download.php 3769 2008-12-15 00:48:56Z zombor $
6  *
7  * @package    Core
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class download_Core {
13
14         /**
15          * Force a download of a file to the user's browser. This function is
16          * binary-safe and will work with any MIME type that Kohana is aware of.
17          *
18          * @param   string  a file path or file name
19          * @param   mixed   data to be sent if the filename does not exist
20          * @param   string  suggested filename to display in the download
21          * @return  void
22          */
23         public static function force($filename = NULL, $data = NULL, $nicename = NULL)
24         {
25                 if (empty($filename))
26                         return FALSE;
27
28                 if (is_file($filename))
29                 {
30                         // Get the real path
31                         $filepath = str_replace('\\', '/', realpath($filename));
32
33                         // Set filesize
34                         $filesize = filesize($filepath);
35
36                         // Get filename
37                         $filename = substr(strrchr('/'.$filepath, '/'), 1);
38
39                         // Get extension
40                         $extension = strtolower(substr(strrchr($filepath, '.'), 1));
41                 }
42                 else
43                 {
44                         // Get filesize
45                         $filesize = strlen($data);
46
47                         // Make sure the filename does not have directory info
48                         $filename = substr(strrchr('/'.$filename, '/'), 1);
49
50                         // Get extension
51                         $extension = strtolower(substr(strrchr($filename, '.'), 1));
52                 }
53
54                 // Get the mime type of the file
55                 $mime = Kohana::config('mimes.'.$extension);
56
57                 if (empty($mime))
58                 {
59                         // Set a default mime if none was found
60                         $mime = array('application/octet-stream');
61                 }
62
63                 // Generate the server headers
64                 header('Content-Type: '.$mime[0]);
65                 header('Content-Disposition: attachment; filename="'.(empty($nicename) ? $filename : $nicename).'"');
66                 header('Content-Transfer-Encoding: binary');
67                 header('Content-Length: '.sprintf('%d', $filesize));
68
69                 // More caching prevention
70                 header('Expires: 0');
71
72                 if (Kohana::user_agent('browser') === 'Internet Explorer')
73                 {
74                         // Send IE headers
75                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
76                         header('Pragma: public');
77                 }
78                 else
79                 {
80                         // Send normal headers
81                         header('Pragma: no-cache');
82                 }
83
84                 // Clear the output buffer
85                 Kohana::close_buffers(FALSE);
86
87                 if (isset($filepath))
88                 {
89                         // Open the file
90                         $handle = fopen($filepath, 'rb');
91
92                         // Send the file data
93                         fpassthru($handle);
94
95                         // Close the file
96                         fclose($handle);
97                 }
98                 else
99                 {
100                         // Send the file data
101                         echo $data;
102                 }
103         }
104
105 } // End download