Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter Download Helpers |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Helpers |
| 34 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/helpers/download_helper.html |
| 37 | */ |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | if ( ! function_exists('force_download')) |
| 42 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 43 | /** |
| 44 | * Force Download |
| 45 | * |
| 46 | * Generates headers that force a download to happen |
| 47 | * |
| 48 | * @param string filename |
| 49 | * @param mixed the data to be downloaded |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 50 | * @param bool whether to try and send the actual file MIME type |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 51 | * @return void |
| 52 | */ |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 53 | function force_download($filename = '', $data = '', $set_mime = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 55 | if ($filename === '' OR $data === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | { |
| 57 | return FALSE; |
| 58 | } |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame^] | 59 | elseif ($data === NULL) |
| 60 | { |
| 61 | if (@is_file($filename) && @file_exists($filename) && ($filesize = @filesize($filename)) !== FALSE) |
| 62 | { |
| 63 | $filepath = $filename; |
| 64 | $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); |
| 65 | $filename = end($filename); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | return FALSE; |
| 70 | } |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | $filesize = strlen($data); |
| 75 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 77 | // Set the default MIME type to send |
| 78 | $mime = 'application/octet-stream'; |
| 79 | |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 80 | $x = explode('.', $filename); |
| 81 | $extension = end($x); |
| 82 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 83 | if ($set_mime === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 85 | if (count($x) === 1 OR $extension === '') |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 86 | { |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 87 | /* If we're going to detect the MIME type, |
| 88 | * we'll need a file extension. |
| 89 | */ |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 90 | return FALSE; |
| 91 | } |
| 92 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 93 | // Load the mime types |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 94 | $mimes =& get_mimes(); |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 95 | |
| 96 | // Only change the default MIME if we can find one |
| 97 | if (isset($mimes[$extension])) |
| 98 | { |
| 99 | $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; |
| 100 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 103 | /* It was reported that browsers on Android 2.1 (and possibly older as well) |
| 104 | * need to have the filename extension upper-cased in order to be able to |
| 105 | * download it. |
| 106 | * |
| 107 | * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ |
| 108 | */ |
Andrey Andreev | 3d933b6 | 2012-03-11 22:08:57 +0200 | [diff] [blame] | 109 | if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 110 | { |
| 111 | $x[count($x) - 1] = strtoupper($extension); |
| 112 | $filename = implode('.', $x); |
| 113 | } |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 114 | |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame^] | 115 | if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) |
| 116 | { |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
Sam Li | bbd9dd3 | 2012-04-23 23:13:46 +0800 | [diff] [blame] | 120 | // Clean output buffer |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame^] | 121 | if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) |
Michiel Vugteveen | 28ff292 | 2012-08-14 10:55:46 +0200 | [diff] [blame] | 122 | { |
| 123 | ob_clean(); |
| 124 | } |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 125 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | // Generate the server headers |
dododedodonl | 4da9478 | 2012-03-10 13:56:17 +0100 | [diff] [blame] | 127 | header('Content-Type: '.$mime); |
Eric Roberts | 0e4d2b6 | 2012-01-23 18:19:20 -0600 | [diff] [blame] | 128 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 129 | header('Expires: 0'); |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 130 | header('Content-Transfer-Encoding: binary'); |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame^] | 131 | header('Content-Length: '.$filesize); |
Eric Roberts | 0e4d2b6 | 2012-01-23 18:19:20 -0600 | [diff] [blame] | 132 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 133 | // Internet Explorer-specific headers |
| 134 | if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | { |
Andrey Andreev | d87f6bf | 2012-10-24 22:12:53 +0300 | [diff] [blame] | 136 | header('Cache-Control: no-cache, no-store, must-revalidate'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | } |
Andrey Andreev | d87f6bf | 2012-10-24 22:12:53 +0300 | [diff] [blame] | 138 | |
| 139 | header('Pragma: no-cache'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame^] | 141 | // If we have raw data - just dump it |
| 142 | if ($data !== NULL) |
| 143 | { |
| 144 | exit($data); |
| 145 | } |
| 146 | |
| 147 | // Flush 1MB chunks of data |
| 148 | while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) |
| 149 | { |
| 150 | echo $data; |
| 151 | } |
| 152 | |
| 153 | fclose($fp); |
| 154 | exit; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | /* End of file download_helper.php */ |
Andrey Andreev | 627e172 | 2012-03-26 20:53:47 +0300 | [diff] [blame] | 159 | /* Location: ./system/helpers/download_helper.php */ |