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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Download Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/helpers/download_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | if ( ! function_exists('force_download')) |
| 53 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Force Download |
| 56 | * |
| 57 | * Generates headers that force a download to happen |
| 58 | * |
| 59 | * @param string filename |
| 60 | * @param mixed the data to be downloaded |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 61 | * @param bool whether to try and send the actual file MIME type |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 62 | * @return void |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 63 | */ |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 64 | function force_download($filename = '', $data = '', $set_mime = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 66 | if ($filename === '' OR $data === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | { |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 68 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | } |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 70 | elseif ($data === NULL) |
| 71 | { |
Andrey Andreev | 15e2df7 | 2015-07-17 13:56:49 +0300 | [diff] [blame] | 72 | if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE) |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 73 | { |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 74 | return; |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 75 | } |
Andrey Andreev | 15e2df7 | 2015-07-17 13:56:49 +0300 | [diff] [blame] | 76 | |
| 77 | $filepath = $filename; |
| 78 | $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); |
| 79 | $filename = end($filename); |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 80 | } |
| 81 | else |
| 82 | { |
| 83 | $filesize = strlen($data); |
| 84 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 86 | // Set the default MIME type to send |
| 87 | $mime = 'application/octet-stream'; |
| 88 | |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 89 | $x = explode('.', $filename); |
| 90 | $extension = end($x); |
| 91 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 92 | if ($set_mime === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | { |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 94 | if (count($x) === 1 OR $extension === '') |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 95 | { |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 96 | /* If we're going to detect the MIME type, |
| 97 | * we'll need a file extension. |
| 98 | */ |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 99 | return; |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 102 | // Load the mime types |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 103 | $mimes =& get_mimes(); |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 104 | |
| 105 | // Only change the default MIME if we can find one |
| 106 | if (isset($mimes[$extension])) |
| 107 | { |
| 108 | $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; |
| 109 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 112 | /* It was reported that browsers on Android 2.1 (and possibly older as well) |
| 113 | * need to have the filename extension upper-cased in order to be able to |
| 114 | * download it. |
| 115 | * |
| 116 | * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ |
| 117 | */ |
Andrey Andreev | 3d933b6 | 2012-03-11 22:08:57 +0200 | [diff] [blame] | 118 | 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] | 119 | { |
| 120 | $x[count($x) - 1] = strtoupper($extension); |
| 121 | $filename = implode('.', $x); |
| 122 | } |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 123 | |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 124 | if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) |
| 125 | { |
Gabriel Potkány | cea5fb7 | 2015-02-04 08:22:06 +0100 | [diff] [blame] | 126 | return; |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Sam Li | bbd9dd3 | 2012-04-23 23:13:46 +0800 | [diff] [blame] | 129 | // Clean output buffer |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 130 | if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) |
Michiel Vugteveen | 28ff292 | 2012-08-14 10:55:46 +0200 | [diff] [blame] | 131 | { |
Andrey Andreev | 88d16d7 | 2014-01-28 12:28:42 +0200 | [diff] [blame] | 132 | @ob_clean(); |
Michiel Vugteveen | 28ff292 | 2012-08-14 10:55:46 +0200 | [diff] [blame] | 133 | } |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | // Generate the server headers |
dododedodonl | 4da9478 | 2012-03-10 13:56:17 +0100 | [diff] [blame] | 136 | header('Content-Type: '.$mime); |
Eric Roberts | 0e4d2b6 | 2012-01-23 18:19:20 -0600 | [diff] [blame] | 137 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 138 | header('Expires: 0'); |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 139 | header('Content-Transfer-Encoding: binary'); |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 140 | header('Content-Length: '.$filesize); |
Andrey Andreev | 15e2df7 | 2015-07-17 13:56:49 +0300 | [diff] [blame] | 141 | header('Cache-Control: private, no-transform, no-store, must-revalidate'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 143 | // If we have raw data - just dump it |
| 144 | if ($data !== NULL) |
| 145 | { |
| 146 | exit($data); |
| 147 | } |
| 148 | |
| 149 | // Flush 1MB chunks of data |
| 150 | while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) |
| 151 | { |
| 152 | echo $data; |
| 153 | } |
| 154 | |
| 155 | fclose($fp); |
| 156 | exit; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
| 158 | } |