Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 1 | |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 2 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 3 | /** |
| 4 | * CodeIgniter |
| 5 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 6 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 7 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 8 | * NOTICE OF LICENSE |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 9 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 10 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 11 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 12 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 13 | * bundled with this package in the files license.txt / license.rst. It is |
| 14 | * also available through the world wide web at this URL: |
| 15 | * http://opensource.org/licenses/OSL-3.0 |
| 16 | * If you did not receive a copy of the license and are unable to obtain it |
| 17 | * through the world wide web, please send an email to |
| 18 | * licensing@ellislab.com so we can send you a copy immediately. |
| 19 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 20 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 21 | * @author EllisLab Dev Team |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 22 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 23 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 24 | * @link http://codeigniter.com |
| 25 | * @since Version 1.0 |
| 26 | * @filesource |
| 27 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 28 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 30 | /** |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 31 | * Common Functions |
| 32 | * |
| 33 | * Loads the base classes and executes the request. |
| 34 | * |
| 35 | * @package CodeIgniter |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 36 | * @subpackage CodeIgniter |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 37 | * @category Common Functions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/ |
| 40 | */ |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 44 | if ( ! function_exists('is_php')) |
| 45 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 46 | /** |
| 47 | * Determines if the current version of PHP is greater then the supplied value |
| 48 | * |
Andrey Andreev | 24bd230 | 2012-06-05 22:29:12 +0300 | [diff] [blame] | 49 | * Since there are a few places where we conditionally test for PHP > 5.3 |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 50 | * we'll set a static variable. |
| 51 | * |
| 52 | * @param string |
| 53 | * @return bool TRUE if the current version is $version or higher |
| 54 | */ |
Andrey Andreev | 24bd230 | 2012-06-05 22:29:12 +0300 | [diff] [blame] | 55 | function is_php($version = '5.3.0') |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 56 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 57 | static $_is_php; |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 58 | $version = (string) $version; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 60 | if ( ! isset($_is_php[$version])) |
| 61 | { |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 62 | $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 63 | } |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 64 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 65 | return $_is_php[$version]; |
| 66 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 67 | } |
Derek Jones | 5bcfd2e | 2009-07-28 14:42:42 +0000 | [diff] [blame] | 68 | |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 69 | // ------------------------------------------------------------------------ |
| 70 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 71 | if ( ! function_exists('is_really_writable')) |
| 72 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 73 | /** |
| 74 | * Tests for file writability |
| 75 | * |
| 76 | * is_writable() returns TRUE on Windows servers when you really can't write to |
| 77 | * the file, based on the read-only attribute. is_writable() is also unreliable |
| 78 | * on Unix servers if safe_mode is on. |
| 79 | * |
| 80 | * @param string |
| 81 | * @return void |
| 82 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 83 | function is_really_writable($file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 85 | // If we're on a Unix server with safe_mode off we call is_writable |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 86 | if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 87 | { |
| 88 | return is_writable($file); |
| 89 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 91 | /* For Windows servers and safe_mode "on" installations we'll actually |
| 92 | * write a file then read it. Bah... |
| 93 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 94 | if (is_dir($file)) |
| 95 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 96 | $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100)); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 97 | if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) |
| 98 | { |
| 99 | return FALSE; |
| 100 | } |
| 101 | |
| 102 | fclose($fp); |
| 103 | @chmod($file, DIR_WRITE_MODE); |
| 104 | @unlink($file); |
| 105 | return TRUE; |
| 106 | } |
Eric Barnes | 1508301 | 2011-03-21 22:13:12 -0400 | [diff] [blame] | 107 | elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
| 109 | return FALSE; |
| 110 | } |
| 111 | |
| 112 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | return TRUE; |
| 114 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 115 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | |
| 117 | // ------------------------------------------------------------------------ |
| 118 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 119 | if ( ! function_exists('load_class')) |
| 120 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 121 | /** |
| 122 | * Class registry |
| 123 | * |
| 124 | * This function acts as a singleton. If the requested class does not |
| 125 | * exist it is instantiated and set to a static variable. If it has |
| 126 | * previously been instantiated the variable is returned. |
| 127 | * |
| 128 | * @param string the class name being requested |
| 129 | * @param string the directory where the class should be found |
| 130 | * @param string the class name prefix |
| 131 | * @return object |
| 132 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 133 | function &load_class($class, $directory = 'libraries', $prefix = 'CI_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 135 | static $_classes = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 137 | // Does the class exist? If so, we're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 138 | if (isset($_classes[$class])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 140 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 142 | |
| 143 | $name = FALSE; |
| 144 | |
Shane Pearson | ab57a35 | 2011-08-22 16:11:20 -0500 | [diff] [blame] | 145 | // Look for the class first in the local application/libraries folder |
| 146 | // then in the native system/libraries folder |
| 147 | foreach (array(APPPATH, BASEPATH) as $path) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 149 | if (file_exists($path.$directory.'/'.$class.'.php')) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 150 | { |
| 151 | $name = $prefix.$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 153 | if (class_exists($name) === FALSE) |
| 154 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 155 | require_once($path.$directory.'/'.$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 156 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 162 | // Is the request a class extension? If so we load it too |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 163 | if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 165 | $name = config_item('subclass_prefix').$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 167 | if (class_exists($name) === FALSE) |
| 168 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 169 | require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // Did we find the class? |
| 174 | if ($name === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | // Note: We use exit() rather then show_error() in order to avoid a |
vlakoff | fe8fe45 | 2012-07-11 19:56:50 +0200 | [diff] [blame] | 177 | // self-referencing loop with the Exceptions class |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 178 | set_status_header(503); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 179 | echo 'Unable to locate the specified class: '.$class.'.php'; |
| 180 | exit(2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 182 | |
| 183 | // Keep track of what we just loaded |
| 184 | is_loaded($class); |
| 185 | |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 186 | $_classes[$class] = new $name(); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 187 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 189 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 191 | // -------------------------------------------------------------------- |
| 192 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 193 | if ( ! function_exists('is_loaded')) |
| 194 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 195 | /** |
| 196 | * Keeps track of which libraries have been loaded. This function is |
| 197 | * called by the load_class() function above |
| 198 | * |
| 199 | * @param string |
| 200 | * @return array |
| 201 | */ |
Andrey Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 202 | function &is_loaded($class = '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 203 | { |
| 204 | static $_is_loaded = array(); |
| 205 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 206 | if ($class !== '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 207 | { |
| 208 | $_is_loaded[strtolower($class)] = $class; |
| 209 | } |
| 210 | |
| 211 | return $_is_loaded; |
| 212 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 213 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 214 | |
| 215 | // ------------------------------------------------------------------------ |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 216 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 217 | if ( ! function_exists('get_config')) |
| 218 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 219 | /** |
| 220 | * Loads the main config.php file |
| 221 | * |
| 222 | * This function lets us grab the config file even if the Config class |
| 223 | * hasn't been instantiated yet |
| 224 | * |
| 225 | * @param array |
| 226 | * @return array |
| 227 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 228 | function &get_config($replace = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 230 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 232 | if (isset($_config)) |
| 233 | { |
| 234 | return $_config[0]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 236 | |
Thanasis Polychronakis | b6e0b58 | 2012-05-14 21:31:04 +0300 | [diff] [blame] | 237 | $file_path = APPPATH.'config/config.php'; |
Thanasis Polychronakis | 142eef9 | 2012-05-21 14:38:22 +0300 | [diff] [blame] | 238 | $found = FALSE; |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 239 | if (file_exists($file_path)) |
Thanasis Polychronakis | 8991cb8 | 2012-05-20 18:44:21 +0300 | [diff] [blame] | 240 | { |
Thanasis Polychronakis | 142eef9 | 2012-05-21 14:38:22 +0300 | [diff] [blame] | 241 | $found = TRUE; |
Thanasis Polychronakis | b6e0b58 | 2012-05-14 21:31:04 +0300 | [diff] [blame] | 242 | require($file_path); |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 243 | } |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 244 | |
Thanasis Polychronakis | b6e0b58 | 2012-05-14 21:31:04 +0300 | [diff] [blame] | 245 | // Is the config file in the environment folder? |
Andrey Andreev | 92aa67c | 2012-06-09 23:37:17 +0300 | [diff] [blame] | 246 | if (defined('ENVIRONMENT') && file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | { |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 248 | require($file_path); |
| 249 | } |
| 250 | elseif ( ! $found) |
Thanasis Polychronakis | 8991cb8 | 2012-05-20 18:44:21 +0300 | [diff] [blame] | 251 | { |
| 252 | set_status_header(503); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 253 | echo 'The configuration file does not exist.'; |
| 254 | exit(1); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 255 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 256 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 257 | // Does the $config array exist in the file? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | if ( ! isset($config) OR ! is_array($config)) |
| 259 | { |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 260 | set_status_header(503); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 261 | echo 'Your config file does not appear to be formatted correctly.'; |
| 262 | exit(1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 265 | // Are any values being dynamically replaced? |
| 266 | if (count($replace) > 0) |
| 267 | { |
| 268 | foreach ($replace as $key => $val) |
| 269 | { |
| 270 | if (isset($config[$key])) |
| 271 | { |
| 272 | $config[$key] = $val; |
| 273 | } |
| 274 | } |
| 275 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 277 | return $_config[0] =& $config; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 279 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 280 | |
| 281 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 283 | if ( ! function_exists('config_item')) |
| 284 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 285 | /** |
| 286 | * Returns the specified config item |
| 287 | * |
| 288 | * @param string |
| 289 | * @return mixed |
| 290 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 291 | function config_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 293 | static $_config_item = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 295 | if ( ! isset($_config_item[$item])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 297 | $config =& get_config(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 298 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 299 | if ( ! isset($config[$item])) |
| 300 | { |
| 301 | return FALSE; |
| 302 | } |
| 303 | $_config_item[$item] = $config[$item]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 306 | return $_config_item[$item]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 308 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 310 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 312 | if ( ! function_exists('get_mimes')) |
| 313 | { |
| 314 | /** |
| 315 | * Returns the MIME types array from config/mimes.php |
| 316 | * |
| 317 | * @return array |
| 318 | */ |
| 319 | function &get_mimes() |
| 320 | { |
| 321 | static $_mimes = array(); |
| 322 | |
| 323 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
| 324 | { |
| 325 | $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 326 | } |
| 327 | elseif (is_file(APPPATH.'config/mimes.php')) |
| 328 | { |
| 329 | $_mimes = include(APPPATH.'config/mimes.php'); |
| 330 | } |
| 331 | |
| 332 | return $_mimes; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // ------------------------------------------------------------------------ |
| 337 | |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 338 | if ( ! function_exists('is_https')) |
| 339 | { |
| 340 | /** |
| 341 | * Is HTTPS? |
| 342 | * |
| 343 | * Determines if the application is accessed via an encrypted |
| 344 | * (HTTPS) connection. |
| 345 | * |
| 346 | * @return bool |
| 347 | */ |
| 348 | function is_https() |
| 349 | { |
| 350 | return ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // ------------------------------------------------------------------------ |
| 355 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 356 | if ( ! function_exists('show_error')) |
| 357 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 358 | /** |
| 359 | * Error Handler |
| 360 | * |
| 361 | * This function lets us invoke the exception class and |
| 362 | * display errors using the standard error template located |
| 363 | * in application/errors/errors.php |
| 364 | * This function will send the error page directly to the |
| 365 | * browser and exit. |
| 366 | * |
| 367 | * @param string |
| 368 | * @param int |
| 369 | * @param string |
| 370 | * @return void |
| 371 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 372 | function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') |
| 373 | { |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 374 | $status_code = abs($status_code); |
| 375 | if ($status_code < 100) |
| 376 | { |
| 377 | $exit_status = $status_code + 28; |
| 378 | $status_code = 500; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | $exit_status = 27; |
| 383 | } |
| 384 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 385 | $_error =& load_class('Exceptions', 'core'); |
| 386 | echo $_error->show_error($heading, $message, 'error_general', $status_code); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 387 | exit($exit_status); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 388 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 389 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 391 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 393 | if ( ! function_exists('show_404')) |
| 394 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 395 | /** |
| 396 | * 404 Page Handler |
| 397 | * |
| 398 | * This function is similar to the show_error() function above |
| 399 | * However, instead of the standard error template it displays |
| 400 | * 404 errors. |
| 401 | * |
| 402 | * @param string |
| 403 | * @param bool |
| 404 | * @return void |
| 405 | */ |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 406 | function show_404($page = '', $log_error = TRUE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 407 | { |
| 408 | $_error =& load_class('Exceptions', 'core'); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 409 | $_error->show_404($page, $log_error); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 410 | exit(4); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 411 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 412 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 414 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 416 | if ( ! function_exists('log_message')) |
| 417 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 418 | /** |
| 419 | * Error Logging Interface |
| 420 | * |
| 421 | * We use this as a simple mechanism to access the logging |
| 422 | * class and send messages to be logged. |
| 423 | * |
| 424 | * @param string |
| 425 | * @param string |
| 426 | * @param bool |
| 427 | * @return void |
| 428 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 429 | function log_message($level = 'error', $message, $php_error = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | { |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 431 | static $_log, $_log_threshold; |
| 432 | |
| 433 | if ($_log_threshold === NULL) |
| 434 | { |
| 435 | $_log_threshold = config_item('log_threshold'); |
| 436 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 437 | |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 438 | if ($_log_threshold === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 439 | { |
| 440 | return; |
| 441 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 443 | if ($_log === NULL) |
| 444 | { |
| 445 | $_log =& load_class('Log', 'core'); |
| 446 | } |
| 447 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 448 | $_log->write_log($level, $message, $php_error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 450 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 452 | // ------------------------------------------------------------------------ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 453 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 454 | if ( ! function_exists('set_status_header')) |
| 455 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 456 | /** |
| 457 | * Set HTTP Status Header |
| 458 | * |
| 459 | * @param int the status code |
| 460 | * @param string |
| 461 | * @return void |
| 462 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 463 | function set_status_header($code = 200, $text = '') |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 464 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 465 | $stati = array( |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 466 | 200 => 'OK', |
| 467 | 201 => 'Created', |
| 468 | 202 => 'Accepted', |
| 469 | 203 => 'Non-Authoritative Information', |
| 470 | 204 => 'No Content', |
| 471 | 205 => 'Reset Content', |
| 472 | 206 => 'Partial Content', |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 473 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 474 | 300 => 'Multiple Choices', |
| 475 | 301 => 'Moved Permanently', |
| 476 | 302 => 'Found', |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 477 | 303 => 'See Other', |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 478 | 304 => 'Not Modified', |
| 479 | 305 => 'Use Proxy', |
| 480 | 307 => 'Temporary Redirect', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 481 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 482 | 400 => 'Bad Request', |
| 483 | 401 => 'Unauthorized', |
| 484 | 403 => 'Forbidden', |
| 485 | 404 => 'Not Found', |
| 486 | 405 => 'Method Not Allowed', |
| 487 | 406 => 'Not Acceptable', |
| 488 | 407 => 'Proxy Authentication Required', |
| 489 | 408 => 'Request Timeout', |
| 490 | 409 => 'Conflict', |
| 491 | 410 => 'Gone', |
| 492 | 411 => 'Length Required', |
| 493 | 412 => 'Precondition Failed', |
| 494 | 413 => 'Request Entity Too Large', |
| 495 | 414 => 'Request-URI Too Long', |
| 496 | 415 => 'Unsupported Media Type', |
| 497 | 416 => 'Requested Range Not Satisfiable', |
| 498 | 417 => 'Expectation Failed', |
| 499 | 422 => 'Unprocessable Entity', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 500 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 501 | 500 => 'Internal Server Error', |
| 502 | 501 => 'Not Implemented', |
| 503 | 502 => 'Bad Gateway', |
| 504 | 503 => 'Service Unavailable', |
| 505 | 504 => 'Gateway Timeout', |
| 506 | 505 => 'HTTP Version Not Supported' |
| 507 | ); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 508 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 509 | if (empty($code) OR ! is_numeric($code)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 510 | { |
| 511 | show_error('Status codes must be numeric', 500); |
| 512 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 513 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 514 | is_int($code) OR $code = (int) $code; |
| 515 | |
| 516 | if (empty($text)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 517 | { |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 518 | if (isset($stati[$code])) |
| 519 | { |
| 520 | $text = $stati[$code]; |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | show_error('No status text available. Please check your status code number or supply your own message text.', 500); |
| 525 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 526 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 527 | |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 528 | $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 529 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 530 | if (strpos(php_sapi_name(), 'cgi') === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 531 | { |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 532 | if (!headers_sent()) header('Status: '.$code.' '.$text, TRUE); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 533 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 534 | else |
| 535 | { |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame^] | 536 | if (!headers_sent()) header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 537 | } |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 538 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 539 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 540 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 541 | // -------------------------------------------------------------------- |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 542 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 543 | if ( ! function_exists('_exception_handler')) |
| 544 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 545 | /** |
| 546 | * Exception Handler |
| 547 | * |
| 548 | * This is the custom exception handler that is declaired at the top |
| 549 | * of Codeigniter.php. The main reason we use this is to permit |
| 550 | * PHP errors to be logged in our own log files since the user may |
| 551 | * not have access to server logs. Since this function |
| 552 | * effectively intercepts PHP errors, however, we also need |
| 553 | * to display errors based on the current error_reporting level. |
| 554 | * We do that with the use of a PHP error template. |
| 555 | * |
| 556 | * @param int |
| 557 | * @param string |
| 558 | * @param string |
| 559 | * @param int |
| 560 | * @return void |
| 561 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 562 | function _exception_handler($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 563 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 564 | $_error =& load_class('Exceptions', 'core'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 566 | // Should we ignore the error? We'll get the current error_reporting |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 567 | // level and add its bits with the severity bits to find out. |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 568 | if (($severity & error_reporting()) !== $severity) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 569 | { |
| 570 | return; |
| 571 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 572 | |
Francesco Negri | 312bdc5 | 2012-08-04 07:32:19 -0400 | [diff] [blame] | 573 | // Should we display the error? |
| 574 | if ((bool) ini_get('display_errors') === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | { |
| 576 | $_error->show_php_error($severity, $message, $filepath, $line); |
| 577 | } |
| 578 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 579 | $_error->log_exception($severity, $message, $filepath, $line); |
| 580 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 581 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 583 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 584 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 585 | if ( ! function_exists('remove_invisible_characters')) |
| 586 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 587 | /** |
| 588 | * Remove Invisible Characters |
| 589 | * |
| 590 | * This prevents sandwiching null characters |
| 591 | * between ascii characters, like Java\0script. |
| 592 | * |
| 593 | * @param string |
| 594 | * @param bool |
| 595 | * @return string |
| 596 | */ |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 597 | function remove_invisible_characters($str, $url_encoded = TRUE) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 598 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 599 | $non_displayables = array(); |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 600 | |
| 601 | // every control character except newline (dec 10), |
| 602 | // carriage return (dec 13) and horizontal tab (dec 09) |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 603 | if ($url_encoded) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 604 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 605 | $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
| 606 | $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 607 | } |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 608 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 609 | $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 610 | |
| 611 | do |
| 612 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 613 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 614 | } |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 615 | while ($count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 616 | |
| 617 | return $str; |
| 618 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 619 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 621 | // ------------------------------------------------------------------------ |
| 622 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 623 | if ( ! function_exists('html_escape')) |
| 624 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 625 | /** |
| 626 | * Returns HTML escaped variable |
| 627 | * |
| 628 | * @param mixed |
| 629 | * @return mixed |
| 630 | */ |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 631 | function html_escape($var) |
| 632 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 633 | return is_array($var) |
| 634 | ? array_map('html_escape', $var) |
| 635 | : htmlspecialchars($var, ENT_QUOTES, config_item('charset')); |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 636 | } |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 637 | } |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 638 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 639 | // ------------------------------------------------------------------------ |
| 640 | |
| 641 | if ( ! function_exists('_stringify_attributes')) |
| 642 | { |
| 643 | /** |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 644 | * Stringify attributes for use in HTML tags. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 645 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 646 | * Helper function used to convert a string, array, or object |
| 647 | * of attributes to a string. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 648 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 649 | * @param mixed string, array, object |
| 650 | * @param bool |
| 651 | * @return string |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 652 | */ |
| 653 | function _stringify_attributes($attributes, $js = FALSE) |
| 654 | { |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 655 | $atts = NULL; |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 656 | |
| 657 | if (empty($attributes)) |
| 658 | { |
| 659 | return $atts; |
| 660 | } |
| 661 | |
| 662 | if (is_string($attributes)) |
| 663 | { |
| 664 | return ' '.$attributes; |
| 665 | } |
| 666 | |
| 667 | $attributes = (array) $attributes; |
| 668 | |
| 669 | foreach ($attributes as $key => $val) |
| 670 | { |
| 671 | $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"'; |
| 672 | } |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 673 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 674 | return rtrim($atts, ','); |
| 675 | } |
| 676 | } |
| 677 | |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 678 | // ------------------------------------------------------------------------ |
| 679 | |
| 680 | if ( ! function_exists('function_usable')) |
| 681 | { |
| 682 | /** |
| 683 | * Function usable |
| 684 | * |
| 685 | * Executes a function_exists() check, and if the Suhosin PHP |
| 686 | * extension is loaded - checks whether the function that is |
| 687 | * checked might be disabled in there as well. |
| 688 | * |
| 689 | * This is useful as function_exists() will return FALSE for |
| 690 | * functions disabled via the *disable_functions* php.ini |
| 691 | * setting, but not for *suhosin.executor.func.blacklist* and |
| 692 | * *suhosin.executor.disable_eval*. These settings will just |
| 693 | * terminate script execution if a disabled function is executed. |
| 694 | * |
| 695 | * @link http://www.hardened-php.net/suhosin/ |
| 696 | * @param string $function_name Function to check for |
| 697 | * @return bool TRUE if the function exists and is safe to call, |
| 698 | * FALSE otherwise. |
| 699 | */ |
| 700 | function function_usable($function_name) |
| 701 | { |
| 702 | static $_suhosin_func_blacklist; |
| 703 | |
| 704 | if (function_exists($function_name)) |
| 705 | { |
| 706 | if ( ! isset($_suhosin_func_blacklist)) |
| 707 | { |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 708 | if (extension_loaded('suhosin')) |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 709 | { |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 710 | $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist'))); |
| 711 | |
| 712 | if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval')) |
| 713 | { |
| 714 | $_suhosin_func_blacklist[] = 'eval'; |
| 715 | } |
| 716 | } |
| 717 | else |
| 718 | { |
| 719 | $_suhosin_func_blacklist = array(); |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 723 | return ! in_array($function_name, $_suhosin_func_blacklist, TRUE); |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | return FALSE; |
| 727 | } |
| 728 | } |
| 729 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | /* End of file Common.php */ |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 731 | /* Location: ./system/core/Common.php */ |