Skip to content

Commit 6ef498b

Browse files
committed
Added get_mimes() function to system/core/Commons.php.The MIMEs array from config/mimes.php is used by multiple core classes, libraries and helpers and each of them has implemented an own way of getting it, which is not needed and is hard to maintain. This also fixes issue bcit-ci#1411
1 parent f4a53ce commit 6ef498b

File tree

8 files changed

+67
-153
lines changed

8 files changed

+67
-153
lines changed

system/core/Common.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function &get_config($replace = array())
233233

234234
$file_path = APPPATH.'config/config.php';
235235
$found = FALSE;
236-
if (file_exists($file_path))
236+
if (file_exists($file_path))
237237
{
238238
$found = TRUE;
239239
require($file_path);
@@ -242,9 +242,9 @@ function &get_config($replace = array())
242242
// Is the config file in the environment folder?
243243
if (defined(ENVIRONMENT) && file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
244244
{
245-
require($file_path);
246-
}
247-
elseif ( ! $found)
245+
require($file_path);
246+
}
247+
elseif ( ! $found)
248248
{
249249
set_status_header(503);
250250
exit('The configuration file does not exist.');
@@ -304,6 +304,32 @@ function config_item($item)
304304

305305
// ------------------------------------------------------------------------
306306

307+
if ( ! function_exists('get_mimes'))
308+
{
309+
/**
310+
* Returns the MIME types array from config/mimes.php
311+
*
312+
* @return array
313+
*/
314+
function &get_mimes()
315+
{
316+
static $_mimes = array();
317+
318+
if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
319+
{
320+
$_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
321+
}
322+
elseif (is_file(APPPATH.'config/mimes.php'))
323+
{
324+
$_mimes = include(APPPATH.'config/mimes.php');
325+
}
326+
327+
return $_mimes;
328+
}
329+
}
330+
331+
// ------------------------------------------------------------------------
332+
307333
if ( ! function_exists('show_error'))
308334
{
309335
/**

system/core/Output.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CI_Output {
6464
*
6565
* @var array
6666
*/
67-
public $mime_types = array();
67+
public $mimes = array();
6868

6969
/**
7070
* Determines wether profiler is enabled
@@ -104,14 +104,7 @@ public function __construct()
104104
$this->_zlib_oc = (bool) @ini_get('zlib.output_compression');
105105

106106
// Get mime types for later
107-
if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
108-
{
109-
$this->mime_types = include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
110-
}
111-
else
112-
{
113-
$this->mime_types = include APPPATH.'config/mimes.php';
114-
}
107+
$this->mimes =& get_mimes();
115108

116109
log_message('debug', 'Output Class Initialized');
117110
}
@@ -214,9 +207,9 @@ public function set_content_type($mime_type)
214207
$extension = ltrim($mime_type, '.');
215208

216209
// Is this extension supported?
217-
if (isset($this->mime_types[$extension]))
210+
if (isset($this->mimes[$extension]))
218211
{
219-
$mime_type =& $this->mime_types[$extension];
212+
$mime_type =& $this->mimes[$extension];
220213

221214
if (is_array($mime_type))
222215
{

system/helpers/download_helper.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,7 @@ function force_download($filename = '', $data = '', $set_mime = FALSE)
7373
}
7474

7575
// Load the mime types
76-
if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
77-
{
78-
$mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
79-
}
80-
elseif (is_file(APPPATH.'config/mimes.php'))
81-
{
82-
$mimes = include(APPPATH.'config/mimes.php');
83-
}
76+
$mimes =& get_mimes();
8477

8578
// Only change the default MIME if we can find one
8679
if (isset($mimes[$extension]))

system/helpers/file_helper.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -353,32 +353,19 @@ function get_mime_by_extension($file)
353353

354354
if ( ! is_array($mimes))
355355
{
356-
if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
357-
{
358-
$mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
359-
}
360-
elseif (is_file(APPPATH.'config/mimes.php'))
361-
{
362-
$mimes = include(APPPATH.'config/mimes.php');
363-
}
356+
$mimes =& get_mimes();
364357

365-
if ( ! is_array($mimes))
358+
if (empty($mimes))
366359
{
367360
return FALSE;
368361
}
369362
}
370363

371-
if (array_key_exists($extension, $mimes))
364+
if (isset($mimes[$extension]))
372365
{
373-
if (is_array($mimes[$extension]))
374-
{
375-
// Multiple mime types, just give the first one
376-
return current($mimes[$extension]);
377-
}
378-
else
379-
{
380-
return $mimes[$extension];
381-
}
366+
return is_array($mimes[$extension])
367+
? current($mimes[$extension]) // Multiple mime types, just give the first one
368+
: $mimes[$extension];
382369
}
383370

384371
return FALSE;

system/libraries/Email.php

Lines changed: 17 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,98 +1816,23 @@ protected function _set_error_message($msg, $val = '')
18161816
*/
18171817
protected function _mime_types($ext = '')
18181818
{
1819-
$mimes = array(
1820-
'hqx' => 'application/mac-binhex40',
1821-
'cpt' => 'application/mac-compactpro',
1822-
'doc' => 'application/msword',
1823-
'bin' => 'application/macbinary',
1824-
'dms' => 'application/octet-stream',
1825-
'lha' => 'application/octet-stream',
1826-
'lzh' => 'application/octet-stream',
1827-
'exe' => 'application/octet-stream',
1828-
'class' => 'application/octet-stream',
1829-
'psd' => 'application/octet-stream',
1830-
'so' => 'application/octet-stream',
1831-
'sea' => 'application/octet-stream',
1832-
'dll' => 'application/octet-stream',
1833-
'oda' => 'application/oda',
1834-
'pdf' => 'application/pdf',
1835-
'ai' => 'application/postscript',
1836-
'eps' => 'application/postscript',
1837-
'ps' => 'application/postscript',
1838-
'smi' => 'application/smil',
1839-
'smil' => 'application/smil',
1840-
'mif' => 'application/vnd.mif',
1841-
'xls' => 'application/vnd.ms-excel',
1842-
'ppt' => 'application/vnd.ms-powerpoint',
1843-
'wbxml' => 'application/vnd.wap.wbxml',
1844-
'wmlc' => 'application/vnd.wap.wmlc',
1845-
'dcr' => 'application/x-director',
1846-
'dir' => 'application/x-director',
1847-
'dxr' => 'application/x-director',
1848-
'dvi' => 'application/x-dvi',
1849-
'gtar' => 'application/x-gtar',
1850-
'php' => 'application/x-httpd-php',
1851-
'php4' => 'application/x-httpd-php',
1852-
'php3' => 'application/x-httpd-php',
1853-
'phtml' => 'application/x-httpd-php',
1854-
'phps' => 'application/x-httpd-php-source',
1855-
'js' => 'application/x-javascript',
1856-
'swf' => 'application/x-shockwave-flash',
1857-
'sit' => 'application/x-stuffit',
1858-
'tar' => 'application/x-tar',
1859-
'tgz' => 'application/x-tar',
1860-
'xhtml' => 'application/xhtml+xml',
1861-
'xht' => 'application/xhtml+xml',
1862-
'zip' => 'application/zip',
1863-
'mid' => 'audio/midi',
1864-
'midi' => 'audio/midi',
1865-
'mpga' => 'audio/mpeg',
1866-
'mp2' => 'audio/mpeg',
1867-
'mp3' => 'audio/mpeg',
1868-
'aif' => 'audio/x-aiff',
1869-
'aiff' => 'audio/x-aiff',
1870-
'aifc' => 'audio/x-aiff',
1871-
'ram' => 'audio/x-pn-realaudio',
1872-
'rm' => 'audio/x-pn-realaudio',
1873-
'rpm' => 'audio/x-pn-realaudio-plugin',
1874-
'ra' => 'audio/x-realaudio',
1875-
'rv' => 'video/vnd.rn-realvideo',
1876-
'wav' => 'audio/x-wav',
1877-
'bmp' => 'image/bmp',
1878-
'gif' => 'image/gif',
1879-
'jpeg' => 'image/jpeg',
1880-
'jpg' => 'image/jpeg',
1881-
'jpe' => 'image/jpeg',
1882-
'png' => 'image/png',
1883-
'tiff' => 'image/tiff',
1884-
'tif' => 'image/tiff',
1885-
'css' => 'text/css',
1886-
'ics' => 'text/calendar',
1887-
'html' => 'text/html',
1888-
'htm' => 'text/html',
1889-
'shtml' => 'text/html',
1890-
'txt' => 'text/plain',
1891-
'text' => 'text/plain',
1892-
'log' => 'text/plain',
1893-
'rtx' => 'text/richtext',
1894-
'rtf' => 'text/rtf',
1895-
'xml' => 'text/xml',
1896-
'xsl' => 'text/xml',
1897-
'mpeg' => 'video/mpeg',
1898-
'mpg' => 'video/mpeg',
1899-
'mpe' => 'video/mpeg',
1900-
'qt' => 'video/quicktime',
1901-
'mov' => 'video/quicktime',
1902-
'avi' => 'video/x-msvideo',
1903-
'movie' => 'video/x-sgi-movie',
1904-
'doc' => 'application/msword',
1905-
'word' => 'application/msword',
1906-
'xl' => 'application/excel',
1907-
'eml' => 'message/rfc822'
1908-
);
1909-
1910-
return isset($mimes[strtolower($ext)]) ? $mimes[strtolower($ext)] : 'application/x-unknown-content-type';
1819+
static $mimes;
1820+
1821+
$ext = strtolower($ext);
1822+
1823+
if ( ! is_array($mimes))
1824+
{
1825+
$mimes =& get_mimes();
1826+
}
1827+
1828+
if (isset($mimes[$ext]))
1829+
{
1830+
return is_array($mimes[$ext])
1831+
? current($mimes[$ext])
1832+
: $mimes[$ext];
1833+
}
1834+
1835+
return 'application/x-unknown-content-type';
19111836
}
19121837

19131838
}

system/libraries/Upload.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function __construct($props = array())
7878
$this->initialize($props);
7979
}
8080

81+
$this->mimes =& get_mimes();
82+
8183
log_message('debug', 'Upload Class Initialized');
8284
}
8385

@@ -113,7 +115,6 @@ public function initialize($config = array())
113115
'image_type' => '',
114116
'image_size_str' => '',
115117
'error_msg' => array(),
116-
'mimes' => array(),
117118
'remove_spaces' => TRUE,
118119
'xss_clean' => FALSE,
119120
'temp_prefix' => 'temp_file_',
@@ -924,24 +925,6 @@ public function display_errors($open = '<p>', $close = '</p>')
924925
*/
925926
public function mimes_types($mime)
926927
{
927-
global $mimes;
928-
929-
if (count($this->mimes) === 0)
930-
{
931-
if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
932-
{
933-
$this->mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
934-
}
935-
elseif (is_file(APPPATH.'config/mimes.php'))
936-
{
937-
$this->mimes = include(APPPATH.'config/mimes.php');
938-
}
939-
else
940-
{
941-
return FALSE;
942-
}
943-
}
944-
945928
return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
946929
}
947930

user_guide_src/source/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Release Date: Not Released
147147
- Added support for HTTP-Only cookies with new config option ``cookie_httponly`` (default FALSE).
148148
- Renamed method _call_hook() to call_hook() in the :doc:`Hooks Library <general/hooks>`.
149149
- Added get_content_type() method to the :doc:`Output Library <libraries/output>`.
150+
- Added get_mimes() function to system/core/Commons.php to return the config/mimes.php array.
150151

151152
Bug fixes for 3.0
152153
------------------
@@ -225,6 +226,7 @@ Bug fixes for 3.0
225226
- Fixed a bug (#356) - PostgreSQL driver didn't have an _update_batch() method, which resulted in fatal error being triggered when update_batch() is used with it.
226227
- Fixed a bug (#862) - create_table() failed on SQLSRV/MSSQL when used with 'IF NOT EXISTS'.
227228
- Fixed a bug (#1419) - libraries/Driver.php had a static variable that was causing an error.
229+
- Fixed a bug (#1411) - the :doc:`Email library <libraries/email>` used its own short list of MIMEs instead the one from config/mimes.php.
228230

229231
Version 2.1.1
230232
=============

user_guide_src/source/general/common_functions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ html_escape($mixed)
7979
This function provides short cut for htmlspecialchars() function. It
8080
accepts string and array. To prevent Cross Site Scripting (XSS), it is
8181
very useful.
82+
83+
get_mimes()
84+
=============
85+
86+
This function returns the MIMEs array from config/mimes.php.

0 commit comments

Comments
 (0)