blob: a9bea94e2b3728a63d51ec220c11cd57b285b1f7 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevb38c5dd2012-02-29 14:07:45 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Download Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/download_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('force_download'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
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 Bilbief512b732012-06-16 11:15:19 +010061 * @param bool whether to try and send the actual file MIME type
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010062 * @return void
Timothy Warren01b129a2012-04-27 11:36:50 -040063 */
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020064 function force_download($filename = '', $data = '', $set_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010066 if ($filename === '' OR $data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010068 return;
Derek Allard2067d1a2008-11-13 22:59:24 +000069 }
Andrey Andreev53fff912012-11-22 16:57:23 +020070 elseif ($data === NULL)
71 {
Andrey Andreev15e2df72015-07-17 13:56:49 +030072 if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE)
Andrey Andreev53fff912012-11-22 16:57:23 +020073 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010074 return;
Andrey Andreev53fff912012-11-22 16:57:23 +020075 }
Andrey Andreev15e2df72015-07-17 13:56:49 +030076
77 $filepath = $filename;
78 $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
79 $filename = end($filename);
Andrey Andreev53fff912012-11-22 16:57:23 +020080 }
81 else
82 {
83 $filesize = strlen($data);
84 }
Derek Allard2067d1a2008-11-13 22:59:24 +000085
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020086 // Set the default MIME type to send
87 $mime = 'application/octet-stream';
88
Andrey Andreevfce2ed62012-03-11 22:04:48 +020089 $x = explode('.', $filename);
90 $extension = end($x);
91
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020092 if ($set_mime === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Andrey Andreevfce2ed62012-03-11 22:04:48 +020094 if (count($x) === 1 OR $extension === '')
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020095 {
Andrey Andreevfce2ed62012-03-11 22:04:48 +020096 /* If we're going to detect the MIME type,
97 * we'll need a file extension.
98 */
Gabriel Potkánycea5fb72015-02-04 08:22:06 +010099 return;
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200100 }
101
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200102 // Load the mime types
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300103 $mimes =& get_mimes();
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200104
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 Allard2067d1a2008-11-13 22:59:24 +0000110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200112 /* 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 Andreev3d933b62012-03-11 22:08:57 +0200118 if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT']))
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200119 {
120 $x[count($x) - 1] = strtoupper($extension);
121 $filename = implode('.', $x);
122 }
Andrey Andreevae31eb52012-05-17 14:54:15 +0300123
Andrey Andreev53fff912012-11-22 16:57:23 +0200124 if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE)
125 {
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100126 return;
Andrey Andreev53fff912012-11-22 16:57:23 +0200127 }
128
Sam Libbd9dd32012-04-23 23:13:46 +0800129 // Clean output buffer
Andrey Andreev53fff912012-11-22 16:57:23 +0200130 if (ob_get_level() !== 0 && @ob_end_clean() === FALSE)
Michiel Vugteveen28ff2922012-08-14 10:55:46 +0200131 {
Andrey Andreev88d16d72014-01-28 12:28:42 +0200132 @ob_clean();
Michiel Vugteveen28ff2922012-08-14 10:55:46 +0200133 }
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Generate the server headers
dododedodonl4da94782012-03-10 13:56:17 +0100136 header('Content-Type: '.$mime);
Eric Roberts0e4d2b62012-01-23 18:19:20 -0600137 header('Content-Disposition: attachment; filename="'.$filename.'"');
138 header('Expires: 0');
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200139 header('Content-Transfer-Encoding: binary');
Andrey Andreev53fff912012-11-22 16:57:23 +0200140 header('Content-Length: '.$filesize);
Andrey Andreev15e2df72015-07-17 13:56:49 +0300141 header('Cache-Control: private, no-transform, no-store, must-revalidate');
Barry Mienydd671972010-10-04 16:33:58 +0200142
Andrey Andreev53fff912012-11-22 16:57:23 +0200143 // 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 Allard2067d1a2008-11-13 22:59:24 +0000157 }
158}