| 1 | <?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class download_Core { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | public static function force($filename = '', $data = '', $blocksize = 1) |
|---|
| 24 | { |
|---|
| 25 | static $user_agent; |
|---|
| 26 | |
|---|
| 27 | if ($filename == '') |
|---|
| 28 | return FALSE; |
|---|
| 29 | |
|---|
| 30 | if (valid::numeric($blocksize)) |
|---|
| 31 | $blocksize = abs($blocksize); |
|---|
| 32 | else |
|---|
| 33 | return FALSE; |
|---|
| 34 | |
|---|
| 35 | if (is_file($filename)) |
|---|
| 36 | { |
|---|
| 37 | |
|---|
| 38 | $filepath = str_replace('\\', '/', realpath($filename)); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | $extension = pathinfo($filepath, PATHINFO_EXTENSION); |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | $filename = end(explode('/', $filepath)); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | $filesize = filesize($filepath); |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | $extension = end(explode('.', $filename)); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | if (empty($data) OR $extension === $filename) |
|---|
| 57 | return FALSE; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | $filesize = strlen($data); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | if (($mime = Config::item('mimes.'.$extension)) === NULL) |
|---|
| 65 | { |
|---|
| 66 | $mime = 'application/octet-stream'; |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | $mime = current((array) $mime); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | header('Content-Type: '.$mime); |
|---|
| 75 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
|---|
| 76 | header('Content-Transfer-Encoding: binary'); |
|---|
| 77 | header('Expires: 0'); |
|---|
| 78 | header('Content-Length: '.$filesize); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | if (Kohana::user_agent('browser') === 'Internet Explorer') |
|---|
| 82 | { |
|---|
| 83 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|---|
| 84 | header('Pragma: public'); |
|---|
| 85 | } |
|---|
| 86 | else |
|---|
| 87 | { |
|---|
| 88 | header('Pragma: no-cache'); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | if (isset($filepath)) |
|---|
| 92 | { |
|---|
| 93 | |
|---|
| 94 | $blocksize *= 1000000; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | $handle = fopen($filepath, 'rb'); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | while ( ! feof($handle)) |
|---|
| 101 | { |
|---|
| 102 | |
|---|
| 103 | echo fread($handle, $blocksize); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | ob_flush(); |
|---|
| 107 | flush(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | fclose($handle); |
|---|
| 112 | } |
|---|
| 113 | else |
|---|
| 114 | { |
|---|
| 115 | |
|---|
| 116 | echo $data; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | } |
|---|