Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * 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] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @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] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 30 | * Common Functions |
| 31 | * |
| 32 | * Loads the base classes and executes the request. |
| 33 | * |
| 34 | * @package CodeIgniter |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 35 | * @subpackage CodeIgniter |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 36 | * @category Common Functions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/ |
| 39 | */ |
| 40 | |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 43 | if ( ! function_exists('is_php')) |
| 44 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 45 | /** |
| 46 | * Determines if the current version of PHP is greater then the supplied value |
| 47 | * |
Andrey Andreev | 24bd230 | 2012-06-05 22:29:12 +0300 | [diff] [blame] | 48 | * 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] | 49 | * we'll set a static variable. |
| 50 | * |
| 51 | * @param string |
| 52 | * @return bool TRUE if the current version is $version or higher |
| 53 | */ |
Andrey Andreev | 24bd230 | 2012-06-05 22:29:12 +0300 | [diff] [blame] | 54 | function is_php($version = '5.3.0') |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 55 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 56 | static $_is_php; |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 57 | $version = (string) $version; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 58 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 59 | if ( ! isset($_is_php[$version])) |
| 60 | { |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 61 | $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 62 | } |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 63 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 64 | return $_is_php[$version]; |
| 65 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 66 | } |
Derek Jones | 5bcfd2e | 2009-07-28 14:42:42 +0000 | [diff] [blame] | 67 | |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 68 | // ------------------------------------------------------------------------ |
| 69 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 70 | if ( ! function_exists('is_really_writable')) |
| 71 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 72 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 82 | function is_really_writable($file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 84 | // 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] | 85 | if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 86 | { |
| 87 | return is_writable($file); |
| 88 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 90 | /* For Windows servers and safe_mode "on" installations we'll actually |
| 91 | * write a file then read it. Bah... |
| 92 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 93 | if (is_dir($file)) |
| 94 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 95 | $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100)); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 96 | 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 Barnes | 1508301 | 2011-03-21 22:13:12 -0400 | [diff] [blame] | 106 | elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | { |
| 108 | return FALSE; |
| 109 | } |
| 110 | |
| 111 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | return TRUE; |
| 113 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 114 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 118 | if ( ! function_exists('load_class')) |
| 119 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 120 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 132 | function &load_class($class, $directory = 'libraries', $prefix = 'CI_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 134 | static $_classes = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 136 | // Does the class exist? If so, we're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 137 | if (isset($_classes[$class])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 139 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 141 | |
| 142 | $name = FALSE; |
| 143 | |
Shane Pearson | ab57a35 | 2011-08-22 16:11:20 -0500 | [diff] [blame] | 144 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 148 | if (file_exists($path.$directory.'/'.$class.'.php')) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 149 | { |
| 150 | $name = $prefix.$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 152 | if (class_exists($name) === FALSE) |
| 153 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 154 | require_once($path.$directory.'/'.$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 161 | // Is the request a class extension? If so we load it too |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 162 | if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 164 | $name = config_item('subclass_prefix').$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 166 | if (class_exists($name) === FALSE) |
| 167 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 168 | require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // Did we find the class? |
| 173 | if ($name === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | // Note: We use exit() rather then show_error() in order to avoid a |
vlakoff | fe8fe45 | 2012-07-11 19:56:50 +0200 | [diff] [blame] | 176 | // self-referencing loop with the Exceptions class |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 177 | set_status_header(503); |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 178 | exit('Unable to locate the specified class: '.$class.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 180 | |
| 181 | // Keep track of what we just loaded |
| 182 | is_loaded($class); |
| 183 | |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 184 | $_classes[$class] = new $name(); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 185 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 187 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 189 | // -------------------------------------------------------------------- |
| 190 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 191 | if ( ! function_exists('is_loaded')) |
| 192 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 193 | /** |
| 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 Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 200 | function &is_loaded($class = '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 201 | { |
| 202 | static $_is_loaded = array(); |
| 203 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 204 | if ($class !== '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 205 | { |
| 206 | $_is_loaded[strtolower($class)] = $class; |
| 207 | } |
| 208 | |
| 209 | return $_is_loaded; |
| 210 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 211 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 212 | |
| 213 | // ------------------------------------------------------------------------ |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 214 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 215 | if ( ! function_exists('get_config')) |
| 216 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 217 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 226 | function &get_config($replace = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 228 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 230 | if (isset($_config)) |
| 231 | { |
| 232 | return $_config[0]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 234 | |
Thanasis Polychronakis | b6e0b58 | 2012-05-14 21:31:04 +0300 | [diff] [blame] | 235 | $file_path = APPPATH.'config/config.php'; |
Thanasis Polychronakis | 142eef9 | 2012-05-21 14:38:22 +0300 | [diff] [blame] | 236 | $found = FALSE; |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 237 | if (file_exists($file_path)) |
Thanasis Polychronakis | 8991cb8 | 2012-05-20 18:44:21 +0300 | [diff] [blame] | 238 | { |
Thanasis Polychronakis | 142eef9 | 2012-05-21 14:38:22 +0300 | [diff] [blame] | 239 | $found = TRUE; |
Thanasis Polychronakis | b6e0b58 | 2012-05-14 21:31:04 +0300 | [diff] [blame] | 240 | require($file_path); |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 241 | } |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 242 | |
Thanasis Polychronakis | b6e0b58 | 2012-05-14 21:31:04 +0300 | [diff] [blame] | 243 | // Is the config file in the environment folder? |
Andrey Andreev | 92aa67c | 2012-06-09 23:37:17 +0300 | [diff] [blame] | 244 | if (defined('ENVIRONMENT') && file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | { |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 246 | require($file_path); |
| 247 | } |
| 248 | elseif ( ! $found) |
Thanasis Polychronakis | 8991cb8 | 2012-05-20 18:44:21 +0300 | [diff] [blame] | 249 | { |
| 250 | set_status_header(503); |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 251 | exit('The configuration file does not exist.'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 252 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 253 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 254 | // Does the $config array exist in the file? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | if ( ! isset($config) OR ! is_array($config)) |
| 256 | { |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 257 | set_status_header(503); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | exit('Your config file does not appear to be formatted correctly.'); |
| 259 | } |
| 260 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 261 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 273 | return $_config[0] =& $config; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 275 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 276 | |
| 277 | // ------------------------------------------------------------------------ |
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 | if ( ! function_exists('config_item')) |
| 280 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 281 | /** |
| 282 | * Returns the specified config item |
| 283 | * |
| 284 | * @param string |
| 285 | * @return mixed |
| 286 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 287 | function config_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 289 | static $_config_item = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 291 | if ( ! isset($_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 | $config =& get_config(); |
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])) |
| 296 | { |
| 297 | return FALSE; |
| 298 | } |
| 299 | $_config_item[$item] = $config[$item]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 302 | return $_config_item[$item]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 304 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 306 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 308 | if ( ! 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 | |
| 319 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
| 320 | { |
| 321 | $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 322 | } |
| 323 | elseif (is_file(APPPATH.'config/mimes.php')) |
| 324 | { |
| 325 | $_mimes = include(APPPATH.'config/mimes.php'); |
| 326 | } |
| 327 | |
| 328 | return $_mimes; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // ------------------------------------------------------------------------ |
| 333 | |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 334 | if ( ! 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 Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 352 | if ( ! function_exists('show_error')) |
| 353 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 354 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 368 | 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 Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 374 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 376 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 378 | if ( ! function_exists('show_404')) |
| 379 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 380 | /** |
| 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 Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 391 | function show_404($page = '', $log_error = TRUE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 392 | { |
| 393 | $_error =& load_class('Exceptions', 'core'); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 394 | $_error->show_404($page, $log_error); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 395 | exit; |
| 396 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 397 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 399 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 401 | if ( ! function_exists('log_message')) |
| 402 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 403 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 414 | function log_message($level = 'error', $message, $php_error = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 416 | static $_log; |
| 417 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 418 | if (config_item('log_threshold') === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 419 | { |
| 420 | return; |
| 421 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 422 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 423 | $_log =& load_class('Log'); |
| 424 | $_log->write_log($level, $message, $php_error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 426 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 428 | // ------------------------------------------------------------------------ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 429 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 430 | if ( ! function_exists('set_status_header')) |
| 431 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 432 | /** |
| 433 | * Set HTTP Status Header |
| 434 | * |
| 435 | * @param int the status code |
| 436 | * @param string |
| 437 | * @return void |
| 438 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 439 | function set_status_header($code = 200, $text = '') |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 440 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 441 | $stati = array( |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 442 | 200 => 'OK', |
| 443 | 201 => 'Created', |
| 444 | 202 => 'Accepted', |
| 445 | 203 => 'Non-Authoritative Information', |
| 446 | 204 => 'No Content', |
| 447 | 205 => 'Reset Content', |
| 448 | 206 => 'Partial Content', |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 449 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 450 | 300 => 'Multiple Choices', |
| 451 | 301 => 'Moved Permanently', |
| 452 | 302 => 'Found', |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 453 | 303 => 'See Other', |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 454 | 304 => 'Not Modified', |
| 455 | 305 => 'Use Proxy', |
| 456 | 307 => 'Temporary Redirect', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 457 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 458 | 400 => 'Bad Request', |
| 459 | 401 => 'Unauthorized', |
| 460 | 403 => 'Forbidden', |
| 461 | 404 => 'Not Found', |
| 462 | 405 => 'Method Not Allowed', |
| 463 | 406 => 'Not Acceptable', |
| 464 | 407 => 'Proxy Authentication Required', |
| 465 | 408 => 'Request Timeout', |
| 466 | 409 => 'Conflict', |
| 467 | 410 => 'Gone', |
| 468 | 411 => 'Length Required', |
| 469 | 412 => 'Precondition Failed', |
| 470 | 413 => 'Request Entity Too Large', |
| 471 | 414 => 'Request-URI Too Long', |
| 472 | 415 => 'Unsupported Media Type', |
| 473 | 416 => 'Requested Range Not Satisfiable', |
| 474 | 417 => 'Expectation Failed', |
| 475 | 422 => 'Unprocessable Entity', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 476 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 477 | 500 => 'Internal Server Error', |
| 478 | 501 => 'Not Implemented', |
| 479 | 502 => 'Bad Gateway', |
| 480 | 503 => 'Service Unavailable', |
| 481 | 504 => 'Gateway Timeout', |
| 482 | 505 => 'HTTP Version Not Supported' |
| 483 | ); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 484 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 485 | if (empty($code) OR ! is_numeric($code)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 486 | { |
| 487 | show_error('Status codes must be numeric', 500); |
| 488 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 490 | is_int($code) OR $code = (int) $code; |
| 491 | |
| 492 | if (empty($text)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 493 | { |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 494 | if (isset($stati[$code])) |
| 495 | { |
| 496 | $text = $stati[$code]; |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | show_error('No status text available. Please check your status code number or supply your own message text.', 500); |
| 501 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 504 | $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 505 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 506 | if (strpos(php_sapi_name(), 'cgi') === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 507 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 508 | header('Status: '.$code.' '.$text, TRUE); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 509 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 510 | else |
| 511 | { |
Daniel Morris | 7885c5c | 2012-10-04 21:44:09 +0100 | [diff] [blame] | 512 | header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 513 | } |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 514 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 515 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 517 | // -------------------------------------------------------------------- |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 518 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 519 | if ( ! function_exists('_exception_handler')) |
| 520 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 521 | /** |
| 522 | * Exception Handler |
| 523 | * |
| 524 | * This is the custom exception handler that is declaired at the top |
| 525 | * of Codeigniter.php. The main reason we use this is to permit |
| 526 | * PHP errors to be logged in our own log files since the user may |
| 527 | * not have access to server logs. Since this function |
| 528 | * effectively intercepts PHP errors, however, we also need |
| 529 | * to display errors based on the current error_reporting level. |
| 530 | * We do that with the use of a PHP error template. |
| 531 | * |
| 532 | * @param int |
| 533 | * @param string |
| 534 | * @param string |
| 535 | * @param int |
| 536 | * @return void |
| 537 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 538 | function _exception_handler($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 539 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 540 | $_error =& load_class('Exceptions', 'core'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 542 | // Should we ignore the error? We'll get the current error_reporting |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 543 | // level and add its bits with the severity bits to find out. |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 544 | if (($severity & error_reporting()) !== $severity) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 545 | { |
| 546 | return; |
| 547 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 548 | |
Francesco Negri | 312bdc5 | 2012-08-04 07:32:19 -0400 | [diff] [blame] | 549 | // Should we display the error? |
| 550 | if ((bool) ini_get('display_errors') === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | { |
| 552 | $_error->show_php_error($severity, $message, $filepath, $line); |
| 553 | } |
| 554 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 555 | $_error->log_exception($severity, $message, $filepath, $line); |
| 556 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 557 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 559 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 560 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 561 | if ( ! function_exists('remove_invisible_characters')) |
| 562 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 563 | /** |
| 564 | * Remove Invisible Characters |
| 565 | * |
| 566 | * This prevents sandwiching null characters |
| 567 | * between ascii characters, like Java\0script. |
| 568 | * |
| 569 | * @param string |
| 570 | * @param bool |
| 571 | * @return string |
| 572 | */ |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 573 | function remove_invisible_characters($str, $url_encoded = TRUE) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 574 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 575 | $non_displayables = array(); |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 576 | |
| 577 | // every control character except newline (dec 10), |
| 578 | // carriage return (dec 13) and horizontal tab (dec 09) |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 579 | if ($url_encoded) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 580 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 581 | $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
| 582 | $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 583 | } |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 584 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 585 | $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] | 586 | |
| 587 | do |
| 588 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 589 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 590 | } |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 591 | while ($count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 592 | |
| 593 | return $str; |
| 594 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 595 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 597 | // ------------------------------------------------------------------------ |
| 598 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 599 | if ( ! function_exists('html_escape')) |
| 600 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 601 | /** |
| 602 | * Returns HTML escaped variable |
| 603 | * |
| 604 | * @param mixed |
| 605 | * @return mixed |
| 606 | */ |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 607 | function html_escape($var) |
| 608 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 609 | return is_array($var) |
| 610 | ? array_map('html_escape', $var) |
| 611 | : htmlspecialchars($var, ENT_QUOTES, config_item('charset')); |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 612 | } |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 613 | } |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 614 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 615 | // ------------------------------------------------------------------------ |
| 616 | |
| 617 | if ( ! function_exists('_stringify_attributes')) |
| 618 | { |
| 619 | /** |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 620 | * Stringify attributes for use in HTML tags. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 621 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 622 | * Helper function used to convert a string, array, or object |
| 623 | * of attributes to a string. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 624 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 625 | * @param mixed string, array, object |
| 626 | * @param bool |
| 627 | * @return string |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 628 | */ |
| 629 | function _stringify_attributes($attributes, $js = FALSE) |
| 630 | { |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 631 | $atts = NULL; |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 632 | |
| 633 | if (empty($attributes)) |
| 634 | { |
| 635 | return $atts; |
| 636 | } |
| 637 | |
| 638 | if (is_string($attributes)) |
| 639 | { |
| 640 | return ' '.$attributes; |
| 641 | } |
| 642 | |
| 643 | $attributes = (array) $attributes; |
| 644 | |
| 645 | foreach ($attributes as $key => $val) |
| 646 | { |
| 647 | $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"'; |
| 648 | } |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 649 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 650 | return rtrim($atts, ','); |
| 651 | } |
| 652 | } |
| 653 | |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame^] | 654 | // ------------------------------------------------------------------------ |
| 655 | |
| 656 | if ( ! function_exists('function_usable')) |
| 657 | { |
| 658 | /** |
| 659 | * Function usable |
| 660 | * |
| 661 | * Executes a function_exists() check, and if the Suhosin PHP |
| 662 | * extension is loaded - checks whether the function that is |
| 663 | * checked might be disabled in there as well. |
| 664 | * |
| 665 | * This is useful as function_exists() will return FALSE for |
| 666 | * functions disabled via the *disable_functions* php.ini |
| 667 | * setting, but not for *suhosin.executor.func.blacklist* and |
| 668 | * *suhosin.executor.disable_eval*. These settings will just |
| 669 | * terminate script execution if a disabled function is executed. |
| 670 | * |
| 671 | * @link http://www.hardened-php.net/suhosin/ |
| 672 | * @param string $function_name Function to check for |
| 673 | * @return bool TRUE if the function exists and is safe to call, |
| 674 | * FALSE otherwise. |
| 675 | */ |
| 676 | function function_usable($function_name) |
| 677 | { |
| 678 | static $_suhosin_func_blacklist; |
| 679 | |
| 680 | if (function_exists($function_name)) |
| 681 | { |
| 682 | if ( ! isset($_suhosin_func_blacklist)) |
| 683 | { |
| 684 | $_suhosin_func_blacklist = extension_loaded('suhosin') |
| 685 | ? array() |
| 686 | : explode(',', trim(@ini_get('suhosin.executor.func.blacklist'))); |
| 687 | |
| 688 | if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval')) |
| 689 | { |
| 690 | $_suhosin_func_blacklist[] = 'eval'; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return in_array($function_name, $_suhosin_func_blacklist, TRUE); |
| 695 | } |
| 696 | |
| 697 | return FALSE; |
| 698 | } |
| 699 | } |
| 700 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 701 | /* End of file Common.php */ |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 702 | /* Location: ./system/core/Common.php */ |