1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* CodeIgniter |
4
|
|
|
* |
5
|
|
|
* An open source application development framework for PHP |
6
|
|
|
* |
7
|
|
|
* This content is released under the MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
10
|
|
|
* |
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: |
17
|
|
|
* |
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 |
31
|
|
|
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
32
|
|
|
* @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
33
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
34
|
|
|
* @link http://codeigniter.com |
35
|
|
|
* @since Version 1.0.0 |
36
|
|
|
* @filesource |
37
|
|
|
*/ |
38
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed'); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* CodeIgniter Download Helpers |
42
|
|
|
* |
43
|
|
|
* @package CodeIgniter |
44
|
|
|
* @subpackage Helpers |
45
|
|
|
* @category Helpers |
46
|
|
|
* @author EllisLab Dev Team |
47
|
|
|
* @link http://codeigniter.com/user_guide/helpers/download_helper.html |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
// ------------------------------------------------------------------------ |
51
|
|
|
|
52
|
|
|
if ( ! function_exists('force_download')) |
53
|
|
|
{ |
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 |
61
|
|
|
* @param bool whether to try and send the actual file MIME type |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
function force_download($filename = '', $data = '', $set_mime = FALSE) |
65
|
|
|
{ |
66
|
|
|
if ($filename === '' OR $data === '') |
67
|
|
|
{ |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
elseif ($data === NULL) |
71
|
|
|
{ |
72
|
|
|
if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE) |
73
|
|
|
{ |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$filepath = $filename; |
78
|
|
|
$filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); |
79
|
|
|
$filename = end($filename); |
80
|
|
|
} |
81
|
|
|
else |
82
|
|
|
{ |
83
|
|
|
$filesize = strlen($data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Set the default MIME type to send |
87
|
|
|
$mime = 'application/octet-stream'; |
88
|
|
|
|
89
|
|
|
$x = explode('.', $filename); |
90
|
|
|
$extension = end($x); |
91
|
|
|
|
92
|
|
|
if ($set_mime === TRUE) |
93
|
|
|
{ |
94
|
|
|
if (count($x) === 1 OR $extension === '') |
95
|
|
|
{ |
96
|
|
|
/* If we're going to detect the MIME type, |
97
|
|
|
* we'll need a file extension. |
98
|
|
|
*/ |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Load the mime types |
103
|
|
|
$mimes =& get_mimes(); |
104
|
|
|
|
105
|
|
|
// Only change the default MIME if we can find one |
106
|
|
View Code Duplication |
if (isset($mimes[$extension])) |
107
|
|
|
{ |
108
|
|
|
$mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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
|
|
|
*/ |
118
|
|
|
if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) |
119
|
|
|
{ |
120
|
|
|
$x[count($x) - 1] = strtoupper($extension); |
121
|
|
|
$filename = implode('.', $x); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) |
125
|
|
|
{ |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Clean output buffer |
130
|
|
|
if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) |
131
|
|
|
{ |
132
|
|
|
@ob_clean(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Generate the server headers |
136
|
|
|
header('Content-Type: '.$mime); |
137
|
|
|
header('Content-Disposition: attachment; filename="'.$filename.'"'); |
138
|
|
|
header('Expires: 0'); |
139
|
|
|
header('Content-Transfer-Encoding: binary'); |
140
|
|
|
header('Content-Length: '.$filesize); |
141
|
|
|
header('Cache-Control: private, no-transform, no-store, must-revalidate'); |
142
|
|
|
|
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; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.