summaryrefslogtreecommitdiffstats
path: root/system/helpers/download_helper.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2017-09-09 16:05:22 +0200
committerFlorian Pritz <bluewind@xinu.at>2017-09-09 16:05:22 +0200
commit27639d64d06b62f237bbde253c46cd28fdce8884 (patch)
tree7a2f00cfd44cfcdfe6cb1abc1cfc0675632948c4 /system/helpers/download_helper.php
parent9c5bfbee5b42ea50a5611c537b8dbf01d7a64f79 (diff)
parent6c7a4266410070d30f8f6bcdf9c9e67f3d6478e3 (diff)
Merge tag '3.1.5' into dev-ci3
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'system/helpers/download_helper.php')
-rw-r--r--system/helpers/download_helper.php171
1 files changed, 111 insertions, 60 deletions
diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php
index 34e29447a..b2a1458de 100644
--- a/system/helpers/download_helper.php
+++ b/system/helpers/download_helper.php
@@ -1,19 +1,41 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP
*
- * @package CodeIgniter
- * @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
- * @license http://codeigniter.com/user_guide/license.html
- * @link http://codeigniter.com
- * @since Version 1.0
+ * This content is released under the MIT License (MIT)
+ *
+ * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @package CodeIgniter
+ * @author EllisLab Dev Team
+ * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
+ * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
+ * @license http://opensource.org/licenses/MIT MIT License
+ * @link https://codeigniter.com
+ * @since Version 1.0.0
* @filesource
*/
-
-// ------------------------------------------------------------------------
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Download Helpers
@@ -21,87 +43,116 @@
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
- * @author ExpressionEngine Dev Team
- * @link http://codeigniter.com/user_guide/helpers/download_helper.html
+ * @author EllisLab Dev Team
+ * @link https://codeigniter.com/user_guide/helpers/download_helper.html
*/
// ------------------------------------------------------------------------
-/**
- * Force Download
- *
- * Generates headers that force a download to happen
- *
- * @access public
- * @param string filename
- * @param mixed the data to be downloaded
- * @return void
- */
if ( ! function_exists('force_download'))
{
- function force_download($filename = '', $data = '')
+ /**
+ * Force Download
+ *
+ * Generates headers that force a download to happen
+ *
+ * @param string filename
+ * @param mixed the data to be downloaded
+ * @param bool whether to try and send the actual file MIME type
+ * @return void
+ */
+ function force_download($filename = '', $data = '', $set_mime = FALSE)
{
- if ($filename == '' OR $data == '')
+ if ($filename === '' OR $data === '')
{
- return FALSE;
+ return;
}
+ elseif ($data === NULL)
+ {
+ if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE)
+ {
+ return;
+ }
- // Try to determine if the filename includes a file extension.
- // We need it in order to set the MIME type
- if (FALSE === strpos($filename, '.'))
+ $filepath = $filename;
+ $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
+ $filename = end($filename);
+ }
+ else
{
- return FALSE;
+ $filesize = strlen($data);
}
- // Grab the file extension
+ // Set the default MIME type to send
+ $mime = 'application/octet-stream';
+
$x = explode('.', $filename);
$extension = end($x);
- // Load the mime types
- if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
+ if ($set_mime === TRUE)
{
- include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
+ if (count($x) === 1 OR $extension === '')
+ {
+ /* If we're going to detect the MIME type,
+ * we'll need a file extension.
+ */
+ return;
+ }
+
+ // Load the mime types
+ $mimes =& get_mimes();
+
+ // Only change the default MIME if we can find one
+ if (isset($mimes[$extension]))
+ {
+ $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
+ }
}
- elseif (is_file(APPPATH.'config/mimes.php'))
+
+ /* It was reported that browsers on Android 2.1 (and possibly older as well)
+ * need to have the filename extension upper-cased in order to be able to
+ * download it.
+ *
+ * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
+ */
+ if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT']))
{
- include(APPPATH.'config/mimes.php');
+ $x[count($x) - 1] = strtoupper($extension);
+ $filename = implode('.', $x);
}
- // Set a default mime if we can't find it
- if ( ! isset($mimes[$extension]))
+ if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE)
{
- $mime = 'application/octet-stream';
+ return;
}
- else
+
+ // Clean output buffer
+ if (ob_get_level() !== 0 && @ob_end_clean() === FALSE)
{
- $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
+ @ob_clean();
}
// Generate the server headers
- if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
+ header('Content-Type: '.$mime);
+ header('Content-Disposition: attachment; filename="'.$filename.'"');
+ header('Expires: 0');
+ header('Content-Transfer-Encoding: binary');
+ header('Content-Length: '.$filesize);
+ header('Cache-Control: private, no-transform, no-store, must-revalidate');
+
+ // If we have raw data - just dump it
+ if ($data !== NULL)
{
- header('Content-Type: "'.$mime.'"');
- header('Content-Disposition: attachment; filename="'.$filename.'"');
- header('Expires: 0');
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header("Content-Transfer-Encoding: binary");
- header('Pragma: public');
- header("Content-Length: ".strlen($data));
+ exit($data);
}
- else
+
+ // Flush 1MB chunks of data
+ while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE)
{
- header('Content-Type: "'.$mime.'"');
- header('Content-Disposition: attachment; filename="'.$filename.'"');
- header("Content-Transfer-Encoding: binary");
- header('Expires: 0');
- header('Pragma: no-cache');
- header("Content-Length: ".strlen($data));
+ echo $data;
}
- exit($data);
+ fclose($fp);
+ exit;
}
}
-
-
-/* End of file download_helper.php */
-/* Location: ./system/helpers/download_helper.php */ \ No newline at end of file