blob: 258cd49678d811108c2d9140c93739b1d7a68bbd [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 Andreev188abaf2012-01-07 19:09:42 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev188abaf2012-01-07 19:09:42 +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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060030 * Common Functions
31 *
32 * Loads the base classes and executes the request.
33 *
34 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030035 * @subpackage CodeIgniter
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060036 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060038 * @link http://codeigniter.com/user_guide/
39 */
40
41// ------------------------------------------------------------------------
42
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040043if ( ! function_exists('is_php'))
44{
Timothy Warrenad475052012-04-19 13:21:06 -040045 /**
46 * Determines if the current version of PHP is greater then the supplied value
47 *
Andrey Andreev24bd2302012-06-05 22:29:12 +030048 * Since there are a few places where we conditionally test for PHP > 5.3
Timothy Warrenad475052012-04-19 13:21:06 -040049 * we'll set a static variable.
50 *
51 * @param string
52 * @return bool TRUE if the current version is $version or higher
53 */
Andrey Andreev24bd2302012-06-05 22:29:12 +030054 function is_php($version = '5.3.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000055 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060056 static $_is_php;
Andrey Andreevb7b43962012-02-27 22:45:48 +020057 $version = (string) $version;
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060059 if ( ! isset($_is_php[$version]))
60 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061 $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060062 }
Derek Jones086ee5a2009-07-28 14:42:12 +000063
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060064 return $_is_php[$version];
65 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040066}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000067
Derek Jones086ee5a2009-07-28 14:42:12 +000068// ------------------------------------------------------------------------
69
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040070if ( ! function_exists('is_really_writable'))
71{
Timothy Warrenad475052012-04-19 13:21:06 -040072 /**
73 * Tests for file writability
74 *
75 * is_writable() returns TRUE on Windows servers when you really can't write to
76 * the file, based on the read-only attribute. is_writable() is also unreliable
77 * on Unix servers if safe_mode is on.
78 *
79 * @param string
80 * @return void
81 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060082 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060084 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreevb7b43962012-02-27 22:45:48 +020085 if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060086 {
87 return is_writable($file);
88 }
Derek Allard2067d1a2008-11-13 22:59:24 +000089
Andrey Andreev188abaf2012-01-07 19:09:42 +020090 /* For Windows servers and safe_mode "on" installations we'll actually
91 * write a file then read it. Bah...
92 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060093 if (is_dir($file))
94 {
Andrey Andreev536b7712012-01-07 21:31:25 +020095 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060096 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
97 {
98 return FALSE;
99 }
100
101 fclose($fp);
102 @chmod($file, DIR_WRITE_MODE);
103 @unlink($file);
104 return TRUE;
105 }
Eric Barnes15083012011-03-21 22:13:12 -0400106 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 return FALSE;
109 }
110
111 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 return TRUE;
113 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400114}
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
116// ------------------------------------------------------------------------
117
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400118if ( ! function_exists('load_class'))
119{
Timothy Warrenad475052012-04-19 13:21:06 -0400120 /**
121 * Class registry
122 *
123 * This function acts as a singleton. If the requested class does not
124 * exist it is instantiated and set to a static variable. If it has
125 * previously been instantiated the variable is returned.
126 *
127 * @param string the class name being requested
128 * @param string the directory where the class should be found
129 * @param string the class name prefix
130 * @return object
131 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600132 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600134 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200135
Andrey Andreev188abaf2012-01-07 19:09:42 +0200136 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600137 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600139 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600141
142 $name = FALSE;
143
Shane Pearsonab57a352011-08-22 16:11:20 -0500144 // Look for the class first in the local application/libraries folder
145 // then in the native system/libraries folder
146 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200147 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200148 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600149 {
150 $name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600152 if (class_exists($name) === FALSE)
153 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300154 require_once($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600157 break;
158 }
159 }
160
Andrey Andreev188abaf2012-01-07 19:09:42 +0200161 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200162 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200163 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600164 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600166 if (class_exists($name) === FALSE)
167 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300168 require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600169 }
170 }
171
172 // Did we find the class?
173 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Barry Mienydd671972010-10-04 16:33:58 +0200175 // Note: We use exit() rather then show_error() in order to avoid a
vlakofffe8fe452012-07-11 19:56:50 +0200176 // self-referencing loop with the Exceptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500177 set_status_header(503);
Greg Aker3a746652011-04-19 10:59:47 -0500178 exit('Unable to locate the specified class: '.$class.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600180
181 // Keep track of what we just loaded
182 is_loaded($class);
183
Pascal Kriete58560022010-11-10 16:01:20 -0500184 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600185 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400187}
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600189// --------------------------------------------------------------------
190
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400191if ( ! function_exists('is_loaded'))
192{
Timothy Warrenad475052012-04-19 13:21:06 -0400193 /**
194 * Keeps track of which libraries have been loaded. This function is
195 * called by the load_class() function above
196 *
197 * @param string
198 * @return array
199 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200200 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600201 {
202 static $_is_loaded = array();
203
Alex Bilbieed944a32012-06-02 11:07:47 +0100204 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600205 {
206 $_is_loaded[strtolower($class)] = $class;
207 }
208
209 return $_is_loaded;
210 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400211}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600212
213// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000214
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400215if ( ! function_exists('get_config'))
216{
Timothy Warrenad475052012-04-19 13:21:06 -0400217 /**
218 * Loads the main config.php file
219 *
220 * This function lets us grab the config file even if the Config class
221 * hasn't been instantiated yet
222 *
223 * @param array
224 * @return array
225 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600226 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600228 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600230 if (isset($_config))
231 {
232 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200233 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600234
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300235 $file_path = APPPATH.'config/config.php';
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300236 $found = FALSE;
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300237 if (file_exists($file_path))
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300238 {
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300239 $found = TRUE;
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300240 require($file_path);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100241 }
joelcox2035fd82011-01-16 16:50:36 +0100242
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300243 // Is the config file in the environment folder?
Andrey Andreevdb529ca2013-01-28 11:00:02 +0200244 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300246 require($file_path);
247 }
248 elseif ( ! $found)
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300249 {
250 set_status_header(503);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100251 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600252 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100253
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600254 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 if ( ! isset($config) OR ! is_array($config))
256 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500257 set_status_header(503);
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 exit('Your config file does not appear to be formatted correctly.');
259 }
260
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600261 // Are any values being dynamically replaced?
262 if (count($replace) > 0)
263 {
264 foreach ($replace as $key => $val)
265 {
266 if (isset($config[$key]))
267 {
268 $config[$key] = $val;
269 }
270 }
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600273 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400275}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600276
277// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400279if ( ! function_exists('config_item'))
280{
Timothy Warrenad475052012-04-19 13:21:06 -0400281 /**
282 * Returns the specified config item
283 *
284 * @param string
285 * @return mixed
286 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600287 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600289 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600291 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600293 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600295 if ( ! isset($config[$item]))
296 {
297 return FALSE;
298 }
299 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600302 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400304}
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600306// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000307
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300308if ( ! function_exists('get_mimes'))
309{
310 /**
311 * Returns the MIME types array from config/mimes.php
312 *
313 * @return array
314 */
315 function &get_mimes()
316 {
317 static $_mimes = array();
318
Andrey Andreev06879112013-01-29 15:05:02 +0200319 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300320 {
321 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
322 }
Andrey Andreev06879112013-01-29 15:05:02 +0200323 elseif (file_exists(APPPATH.'config/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300324 {
325 $_mimes = include(APPPATH.'config/mimes.php');
326 }
327
328 return $_mimes;
329 }
330}
331
332// ------------------------------------------------------------------------
333
Andrey Andreev3fb02672012-10-22 16:48:01 +0300334if ( ! function_exists('is_https'))
335{
336 /**
337 * Is HTTPS?
338 *
339 * Determines if the application is accessed via an encrypted
340 * (HTTPS) connection.
341 *
342 * @return bool
343 */
344 function is_https()
345 {
346 return ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
347 }
348}
349
350// ------------------------------------------------------------------------
351
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400352if ( ! function_exists('show_error'))
353{
Timothy Warrenad475052012-04-19 13:21:06 -0400354 /**
355 * Error Handler
356 *
357 * This function lets us invoke the exception class and
358 * display errors using the standard error template located
359 * in application/errors/errors.php
360 * This function will send the error page directly to the
361 * browser and exit.
362 *
363 * @param string
364 * @param int
365 * @param string
366 * @return void
367 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600368 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
369 {
370 $_error =& load_class('Exceptions', 'core');
371 echo $_error->show_error($heading, $message, 'error_general', $status_code);
372 exit;
373 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400374}
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600376// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000377
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400378if ( ! function_exists('show_404'))
379{
Timothy Warrenad475052012-04-19 13:21:06 -0400380 /**
381 * 404 Page Handler
382 *
383 * This function is similar to the show_error() function above
384 * However, instead of the standard error template it displays
385 * 404 errors.
386 *
387 * @param string
388 * @param bool
389 * @return void
390 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400391 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600392 {
393 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400394 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600395 exit;
396 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400397}
Derek Allard2067d1a2008-11-13 22:59:24 +0000398
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600399// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000400
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400401if ( ! function_exists('log_message'))
402{
Timothy Warrenad475052012-04-19 13:21:06 -0400403 /**
404 * Error Logging Interface
405 *
406 * We use this as a simple mechanism to access the logging
407 * class and send messages to be logged.
408 *
409 * @param string
410 * @param string
411 * @param bool
412 * @return void
413 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600414 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Ted Woodb19a2032013-01-05 16:02:43 -0800416 static $_log, $_log_threshold;
417
418 if ($_log_threshold === NULL)
419 {
420 $_log_threshold = config_item('log_threshold');
421 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600422
Ted Woodb19a2032013-01-05 16:02:43 -0800423 if ($_log_threshold === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600424 {
425 return;
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Ted Woodb19a2032013-01-05 16:02:43 -0800428 if ($_log === NULL)
429 {
430 $_log =& load_class('Log', 'core');
431 }
432
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600433 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400435}
Derek Allard2067d1a2008-11-13 22:59:24 +0000436
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600437// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000438
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400439if ( ! function_exists('set_status_header'))
440{
Timothy Warrenad475052012-04-19 13:21:06 -0400441 /**
442 * Set HTTP Status Header
443 *
444 * @param int the status code
445 * @param string
446 * @return void
447 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600448 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000449 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600450 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400451 200 => 'OK',
452 201 => 'Created',
453 202 => 'Accepted',
454 203 => 'Non-Authoritative Information',
455 204 => 'No Content',
456 205 => 'Reset Content',
457 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000458
Timothy Warrenad475052012-04-19 13:21:06 -0400459 300 => 'Multiple Choices',
460 301 => 'Moved Permanently',
461 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300462 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400463 304 => 'Not Modified',
464 305 => 'Use Proxy',
465 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600466
Timothy Warrenad475052012-04-19 13:21:06 -0400467 400 => 'Bad Request',
468 401 => 'Unauthorized',
469 403 => 'Forbidden',
470 404 => 'Not Found',
471 405 => 'Method Not Allowed',
472 406 => 'Not Acceptable',
473 407 => 'Proxy Authentication Required',
474 408 => 'Request Timeout',
475 409 => 'Conflict',
476 410 => 'Gone',
477 411 => 'Length Required',
478 412 => 'Precondition Failed',
479 413 => 'Request Entity Too Large',
480 414 => 'Request-URI Too Long',
481 415 => 'Unsupported Media Type',
482 416 => 'Requested Range Not Satisfiable',
483 417 => 'Expectation Failed',
484 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600485
Timothy Warrenad475052012-04-19 13:21:06 -0400486 500 => 'Internal Server Error',
487 501 => 'Not Implemented',
488 502 => 'Bad Gateway',
489 503 => 'Service Unavailable',
490 504 => 'Gateway Timeout',
491 505 => 'HTTP Version Not Supported'
492 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600493
Andrey Andreev51d6d842012-06-15 16:41:09 +0300494 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600495 {
496 show_error('Status codes must be numeric', 500);
497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Andrey Andreev51d6d842012-06-15 16:41:09 +0300499 is_int($code) OR $code = (int) $code;
500
501 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600502 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300503 if (isset($stati[$code]))
504 {
505 $text = $stati[$code];
506 }
507 else
508 {
509 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
510 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Andrey Andreevb7b43962012-02-27 22:45:48 +0200513 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600514
Andrey Andreev188abaf2012-01-07 19:09:42 +0200515 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600516 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200517 header('Status: '.$code.' '.$text, TRUE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600518 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600519 else
520 {
Daniel Morris7885c5c2012-10-04 21:44:09 +0100521 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600522 }
Derek Jones817163a2009-07-11 17:05:58 +0000523 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400524}
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600526// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000527
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400528if ( ! function_exists('_exception_handler'))
529{
Timothy Warrenad475052012-04-19 13:21:06 -0400530 /**
531 * Exception Handler
532 *
533 * This is the custom exception handler that is declaired at the top
534 * of Codeigniter.php. The main reason we use this is to permit
535 * PHP errors to be logged in our own log files since the user may
536 * not have access to server logs. Since this function
537 * effectively intercepts PHP errors, however, we also need
538 * to display errors based on the current error_reporting level.
539 * We do that with the use of a PHP error template.
540 *
541 * @param int
542 * @param string
543 * @param string
544 * @param int
545 * @return void
546 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600547 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200548 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600549 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200550
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300551 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600552 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300553 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600554 {
555 return;
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Francesco Negri312bdc52012-08-04 07:32:19 -0400558 // Should we display the error?
559 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 $_error->show_php_error($severity, $message, $filepath, $line);
562 }
563
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600564 $_error->log_exception($severity, $message, $filepath, $line);
565 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400566}
Derek Allard2067d1a2008-11-13 22:59:24 +0000567
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400568// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200569
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400570if ( ! function_exists('remove_invisible_characters'))
571{
Timothy Warrenad475052012-04-19 13:21:06 -0400572 /**
573 * Remove Invisible Characters
574 *
575 * This prevents sandwiching null characters
576 * between ascii characters, like Java\0script.
577 *
578 * @param string
579 * @param bool
580 * @return string
581 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400582 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500583 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400584 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200585
586 // every control character except newline (dec 10),
587 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400588 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500589 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400590 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
591 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500592 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200593
Pascal Kriete0ff50262011-04-05 14:52:03 -0400594 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500595
596 do
597 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400598 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500599 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400600 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500601
602 return $str;
603 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400604}
Derek Allard2067d1a2008-11-13 22:59:24 +0000605
kenjisfbac8b42011-08-25 10:51:44 +0900606// ------------------------------------------------------------------------
607
kenjisfbac8b42011-08-25 10:51:44 +0900608if ( ! function_exists('html_escape'))
609{
Timothy Warrenad475052012-04-19 13:21:06 -0400610 /**
611 * Returns HTML escaped variable
612 *
613 * @param mixed
614 * @return mixed
615 */
kenjisfbac8b42011-08-25 10:51:44 +0900616 function html_escape($var)
617 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200618 return is_array($var)
619 ? array_map('html_escape', $var)
620 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600621 }
Greg Akerd96f8822011-12-27 16:23:47 -0600622}
Greg Aker5c1aa632011-12-25 01:24:29 -0600623
Chad Furmana1abada2012-07-29 01:03:50 -0400624// ------------------------------------------------------------------------
625
626if ( ! function_exists('_stringify_attributes'))
627{
628 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300629 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400630 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300631 * Helper function used to convert a string, array, or object
632 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400633 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300634 * @param mixed string, array, object
635 * @param bool
636 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400637 */
638 function _stringify_attributes($attributes, $js = FALSE)
639 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300640 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400641
642 if (empty($attributes))
643 {
644 return $atts;
645 }
646
647 if (is_string($attributes))
648 {
649 return ' '.$attributes;
650 }
651
652 $attributes = (array) $attributes;
653
654 foreach ($attributes as $key => $val)
655 {
656 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
657 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300658
Chad Furmana1abada2012-07-29 01:03:50 -0400659 return rtrim($atts, ',');
660 }
661}
662
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200663// ------------------------------------------------------------------------
664
665if ( ! function_exists('function_usable'))
666{
667 /**
668 * Function usable
669 *
670 * Executes a function_exists() check, and if the Suhosin PHP
671 * extension is loaded - checks whether the function that is
672 * checked might be disabled in there as well.
673 *
674 * This is useful as function_exists() will return FALSE for
675 * functions disabled via the *disable_functions* php.ini
676 * setting, but not for *suhosin.executor.func.blacklist* and
677 * *suhosin.executor.disable_eval*. These settings will just
678 * terminate script execution if a disabled function is executed.
679 *
680 * @link http://www.hardened-php.net/suhosin/
681 * @param string $function_name Function to check for
682 * @return bool TRUE if the function exists and is safe to call,
683 * FALSE otherwise.
684 */
685 function function_usable($function_name)
686 {
687 static $_suhosin_func_blacklist;
688
689 if (function_exists($function_name))
690 {
691 if ( ! isset($_suhosin_func_blacklist))
692 {
Andrey Andreevae634622012-12-17 10:30:18 +0200693 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200694 {
Andrey Andreevae634622012-12-17 10:30:18 +0200695 $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));
696
697 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
698 {
699 $_suhosin_func_blacklist[] = 'eval';
700 }
701 }
702 else
703 {
704 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200705 }
706 }
707
Andrey Andreevae634622012-12-17 10:30:18 +0200708 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200709 }
710
711 return FALSE;
712 }
713}
714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715/* End of file Common.php */
Andrey Andreevbdb99992012-07-30 17:38:05 +0300716/* Location: ./system/core/Common.php */