blob: 8fe66e222a05164dc5c2e3a0f41bd197d07b8e4f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevb38c5dd2012-02-29 14:07:45 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Download Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/download_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('force_download'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Force Download
45 *
46 * Generates headers that force a download to happen
47 *
48 * @param string filename
49 * @param mixed the data to be downloaded
Alex Bilbief512b732012-06-16 11:15:19 +010050 * @param bool whether to try and send the actual file MIME type
Timothy Warren01b129a2012-04-27 11:36:50 -040051 * @return void
52 */
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020053 function force_download($filename = '', $data = '', $set_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010055 if ($filename === '' OR $data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
57 return FALSE;
58 }
59
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020060 // Set the default MIME type to send
61 $mime = 'application/octet-stream';
62
Andrey Andreevfce2ed62012-03-11 22:04:48 +020063 $x = explode('.', $filename);
64 $extension = end($x);
65
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020066 if ($set_mime === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Andrey Andreevfce2ed62012-03-11 22:04:48 +020068 if (count($x) === 1 OR $extension === '')
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020069 {
Andrey Andreevfce2ed62012-03-11 22:04:48 +020070 /* If we're going to detect the MIME type,
71 * we'll need a file extension.
72 */
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020073 return FALSE;
74 }
75
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020076 // Load the mime types
Andrey Andreev6ef498b2012-06-05 22:01:58 +030077 $mimes =& get_mimes();
Andrey Andreevb38c5dd2012-02-29 14:07:45 +020078
79 // Only change the default MIME if we can find one
80 if (isset($mimes[$extension]))
81 {
82 $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
83 }
Derek Allard2067d1a2008-11-13 22:59:24 +000084 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Andrey Andreevfce2ed62012-03-11 22:04:48 +020086 /* It was reported that browsers on Android 2.1 (and possibly older as well)
87 * need to have the filename extension upper-cased in order to be able to
88 * download it.
89 *
90 * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
91 */
Andrey Andreev3d933b62012-03-11 22:08:57 +020092 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 +020093 {
94 $x[count($x) - 1] = strtoupper($extension);
95 $filename = implode('.', $x);
96 }
Andrey Andreevae31eb52012-05-17 14:54:15 +030097
Sam Libbd9dd32012-04-23 23:13:46 +080098 // Clean output buffer
Michiel Vugteveen28ff2922012-08-14 10:55:46 +020099 if (ob_get_level() !== 0)
100 {
101 ob_clean();
102 }
Andrey Andreevfce2ed62012-03-11 22:04:48 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // Generate the server headers
dododedodonl4da94782012-03-10 13:56:17 +0100105 header('Content-Type: '.$mime);
Eric Roberts0e4d2b62012-01-23 18:19:20 -0600106 header('Content-Disposition: attachment; filename="'.$filename.'"');
107 header('Expires: 0');
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200108 header('Content-Transfer-Encoding: binary');
109 header('Content-Length: '.strlen($data));
Eric Roberts0e4d2b62012-01-23 18:19:20 -0600110
Andrey Andreevb38c5dd2012-02-29 14:07:45 +0200111 // Internet Explorer-specific headers
112 if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Andrey Andreevd87f6bf2012-10-24 22:12:53 +0300114 header('Cache-Control: no-cache, no-store, must-revalidate');
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
Andrey Andreevd87f6bf2012-10-24 22:12:53 +0300116
117 header('Pragma: no-cache');
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 exit($data);
120 }
121}
122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123/* End of file download_helper.php */
Andrey Andreev627e1722012-03-26 20:53:47 +0300124/* Location: ./system/helpers/download_helper.php */