Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
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 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Download Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/download_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
| 41 | * Force Download |
| 42 | * |
| 43 | * Generates headers that force a download to happen |
| 44 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | * @param string filename |
| 46 | * @param mixed the data to be downloaded |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 47 | * @param bool wether to try and send the actual file MIME type |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 49 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | if ( ! function_exists('force_download')) |
| 51 | { |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 52 | function force_download($filename = '', $data = '', $set_mime = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | { |
| 54 | if ($filename == '' OR $data == '') |
| 55 | { |
| 56 | return FALSE; |
| 57 | } |
| 58 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 59 | // Set the default MIME type to send |
| 60 | $mime = 'application/octet-stream'; |
| 61 | |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 62 | $x = explode('.', $filename); |
| 63 | $extension = end($x); |
| 64 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 65 | if ($set_mime === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | { |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 67 | if (count($x) === 1 OR $extension === '') |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 68 | { |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 69 | /* If we're going to detect the MIME type, |
| 70 | * we'll need a file extension. |
| 71 | */ |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 72 | return FALSE; |
| 73 | } |
| 74 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 75 | // Load the mime types |
| 76 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
| 77 | { |
| 78 | include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 79 | } |
| 80 | elseif (is_file(APPPATH.'config/mimes.php')) |
| 81 | { |
| 82 | include(APPPATH.'config/mimes.php'); |
| 83 | } |
| 84 | |
| 85 | // Only change the default MIME if we can find one |
| 86 | if (isset($mimes[$extension])) |
| 87 | { |
| 88 | $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; |
| 89 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 92 | /* It was reported that browsers on Android 2.1 (and possibly older as well) |
| 93 | * need to have the filename extension upper-cased in order to be able to |
| 94 | * download it. |
| 95 | * |
| 96 | * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ |
| 97 | */ |
Andrey Andreev | 3d933b6 | 2012-03-11 22:08:57 +0200 | [diff] [blame] | 98 | 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] | 99 | { |
| 100 | $x[count($x) - 1] = strtoupper($extension); |
| 101 | $filename = implode('.', $x); |
| 102 | } |
Sam Li | d52e3b1 | 2012-04-23 23:04:35 +0800 | [diff] [blame] | 103 | |
Sam Li | bbd9dd3 | 2012-04-23 23:13:46 +0800 | [diff] [blame^] | 104 | // Clean output buffer |
Sam Li | d52e3b1 | 2012-04-23 23:04:35 +0800 | [diff] [blame] | 105 | ob_clean(); |
Andrey Andreev | fce2ed6 | 2012-03-11 22:04:48 +0200 | [diff] [blame] | 106 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | // Generate the server headers |
dododedodonl | 4da9478 | 2012-03-10 13:56:17 +0100 | [diff] [blame] | 108 | header('Content-Type: '.$mime); |
Eric Roberts | 0e4d2b6 | 2012-01-23 18:19:20 -0600 | [diff] [blame] | 109 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 110 | header('Expires: 0'); |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 111 | header('Content-Transfer-Encoding: binary'); |
| 112 | header('Content-Length: '.strlen($data)); |
Eric Roberts | 0e4d2b6 | 2012-01-23 18:19:20 -0600 | [diff] [blame] | 113 | |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 114 | // Internet Explorer-specific headers |
| 115 | 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] | 116 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | header('Pragma: public'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | } |
Andrey Andreev | b38c5dd | 2012-02-29 14:07:45 +0200 | [diff] [blame] | 120 | else |
| 121 | { |
| 122 | header('Pragma: no-cache'); |
| 123 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | exit($data); |
| 126 | } |
| 127 | } |
| 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | /* End of file download_helper.php */ |
Andrey Andreev | 627e172 | 2012-03-26 20:53:47 +0300 | [diff] [blame] | 130 | /* Location: ./system/helpers/download_helper.php */ |