PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 3ms app.codeStats 1ms

/manage/codeigniter/system/core/Common.php

https://bitbucket.org/myockey/clearcreek-chapel-website
PHP | 503 lines | 250 code | 72 blank | 181 comment | 42 complexity | 144232415391802b23293f985e1ab328 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Common Functions
  18. *
  19. * Loads the base classes and executes the request.
  20. *
  21. * @package CodeIgniter
  22. * @subpackage codeigniter
  23. * @category Common Functions
  24. * @author EllisLab Dev Team
  25. * @link http://codeigniter.com/user_guide/
  26. */
  27. // ------------------------------------------------------------------------
  28. /**
  29. * Determines if the current version of PHP is greater then the supplied value
  30. *
  31. * Since there are a few places where we conditionally test for PHP > 5
  32. * we'll set a static variable.
  33. *
  34. * @access public
  35. * @param string
  36. * @return bool TRUE if the current version is $version or higher
  37. */
  38. function is_php($version = '5.0.0')
  39. {
  40. static $_is_php;
  41. $version = (string)$version;
  42. if ( ! isset($_is_php[$version]))
  43. {
  44. $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
  45. }
  46. return $_is_php[$version];
  47. }
  48. // ------------------------------------------------------------------------
  49. /**
  50. * Tests for file writability
  51. *
  52. * is_writable() returns TRUE on Windows servers when you really can't write to
  53. * the file, based on the read-only attribute. is_writable() is also unreliable
  54. * on Unix servers if safe_mode is on.
  55. *
  56. * @access private
  57. * @return void
  58. */
  59. function is_really_writable($file)
  60. {
  61. // If we're on a Unix server with safe_mode off we call is_writable
  62. if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
  63. {
  64. return is_writable($file);
  65. }
  66. // For windows servers and safe_mode "on" installations we'll actually
  67. // write a file then read it. Bah...
  68. if (is_dir($file))
  69. {
  70. $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
  71. if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  72. {
  73. return FALSE;
  74. }
  75. fclose($fp);
  76. @chmod($file, DIR_WRITE_MODE);
  77. @unlink($file);
  78. return TRUE;
  79. }
  80. elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  81. {
  82. return FALSE;
  83. }
  84. fclose($fp);
  85. return TRUE;
  86. }
  87. // ------------------------------------------------------------------------
  88. /**
  89. * Class registry
  90. *
  91. * This function acts as a singleton. If the requested class does not
  92. * exist it is instantiated and set to a static variable. If it has
  93. * previously been instantiated the variable is returned.
  94. *
  95. * @access public
  96. * @param string the class name being requested
  97. * @param string the directory where the class should be found
  98. * @param string the class name prefix
  99. * @return object
  100. */
  101. function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
  102. {
  103. static $_classes = array();
  104. // Does the class exist? If so, we're done...
  105. if (isset($_classes[$class]))
  106. {
  107. return $_classes[$class];
  108. }
  109. $name = FALSE;
  110. // Look for the class first in the native system/libraries folder
  111. // thenin the local application/libraries folder
  112. foreach (array(BASEPATH, APPPATH) as $path)
  113. {
  114. if (file_exists($path.$directory.'/'.$class.'.php'))
  115. {
  116. $name = $prefix.$class;
  117. if (class_exists($name) === FALSE)
  118. {
  119. require($path.$directory.'/'.$class.'.php');
  120. }
  121. break;
  122. }
  123. }
  124. // Is the request a class extension? If so we load it too
  125. if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
  126. {
  127. $name = config_item('subclass_prefix').$class;
  128. if (class_exists($name) === FALSE)
  129. {
  130. require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
  131. }
  132. }
  133. // Did we find the class?
  134. if ($name === FALSE)
  135. {
  136. // Note: We use exit() rather then show_error() in order to avoid a
  137. // self-referencing loop with the Excptions class
  138. set_status_header(503);
  139. exit('Unable to locate the specified class: '.$class.'.php');
  140. }
  141. // Keep track of what we just loaded
  142. is_loaded($class);
  143. $_classes[$class] = new $name();
  144. return $_classes[$class];
  145. }
  146. // --------------------------------------------------------------------
  147. /**
  148. * Keeps track of which libraries have been loaded. This function is
  149. * called by the load_class() function above
  150. *
  151. * @access public
  152. * @return array
  153. */
  154. function is_loaded($class = '')
  155. {
  156. static $_is_loaded = array();
  157. if ($class != '')
  158. {
  159. $_is_loaded[strtolower($class)] = $class;
  160. }
  161. return $_is_loaded;
  162. }
  163. // ------------------------------------------------------------------------
  164. /**
  165. * Loads the main config.php file
  166. *
  167. * This function lets us grab the config file even if the Config class
  168. * hasn't been instantiated yet
  169. *
  170. * @access private
  171. * @return array
  172. */
  173. function &get_config($replace = array())
  174. {
  175. static $_config;
  176. if (isset($_config))
  177. {
  178. return $_config[0];
  179. }
  180. // Fetch the config file
  181. if ( ! file_exists(APPPATH.'config/config.php'))
  182. {
  183. set_status_header(503);
  184. exit('The configuration file does not exist.');
  185. }
  186. else
  187. {
  188. require(APPPATH.'config/config.php');
  189. }
  190. // Does the $config array exist in the file?
  191. if ( ! isset($config) OR ! is_array($config))
  192. {
  193. set_status_header(503);
  194. exit('Your config file does not appear to be formatted correctly.');
  195. }
  196. // Are any values being dynamically replaced?
  197. if (count($replace) > 0)
  198. {
  199. foreach ($replace as $key => $val)
  200. {
  201. if (isset($config[$key]))
  202. {
  203. $config[$key] = $val;
  204. }
  205. }
  206. }
  207. return $_config[0] =& $config;
  208. }
  209. // ------------------------------------------------------------------------
  210. /**
  211. * Returns the specified config item
  212. *
  213. * @access public
  214. * @return mixed
  215. */
  216. function config_item($item)
  217. {
  218. static $_config_item = array();
  219. if ( ! isset($_config_item[$item]))
  220. {
  221. $config =& get_config();
  222. if ( ! isset($config[$item]))
  223. {
  224. return FALSE;
  225. }
  226. $_config_item[$item] = $config[$item];
  227. }
  228. return $_config_item[$item];
  229. }
  230. // ------------------------------------------------------------------------
  231. /**
  232. * Error Handler
  233. *
  234. * This function lets us invoke the exception class and
  235. * display errors using the standard error template located
  236. * in application/errors/errors.php
  237. * This function will send the error page directly to the
  238. * browser and exit.
  239. *
  240. * @access public
  241. * @return void
  242. */
  243. function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
  244. {
  245. $_error =& load_class('Exceptions', 'core');
  246. echo $_error->show_error($heading, $message, 'error_general', $status_code);
  247. exit;
  248. }
  249. // ------------------------------------------------------------------------
  250. /**
  251. * 404 Page Handler
  252. *
  253. * This function is similar to the show_error() function above
  254. * However, instead of the standard error template it displays
  255. * 404 errors.
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. function show_404($page = '', $log_error = TRUE)
  261. {
  262. $_error =& load_class('Exceptions', 'core');
  263. $_error->show_404($page, $log_error);
  264. exit;
  265. }
  266. // ------------------------------------------------------------------------
  267. /**
  268. * Error Logging Interface
  269. *
  270. * We use this as a simple mechanism to access the logging
  271. * class and send messages to be logged.
  272. *
  273. * @access public
  274. * @return void
  275. */
  276. function log_message($level = 'error', $message, $php_error = FALSE)
  277. {
  278. static $_log;
  279. if (config_item('log_threshold') == 0)
  280. {
  281. return;
  282. }
  283. $_log =& load_class('Log');
  284. $_log->write_log($level, $message, $php_error);
  285. }
  286. // ------------------------------------------------------------------------
  287. /**
  288. * Set HTTP Status Header
  289. *
  290. * @access public
  291. * @param int the status code
  292. * @param string
  293. * @return void
  294. */
  295. function set_status_header($code = 200, $text = '')
  296. {
  297. $stati = array(
  298. 200 => 'OK',
  299. 201 => 'Created',
  300. 202 => 'Accepted',
  301. 203 => 'Non-Authoritative Information',
  302. 204 => 'No Content',
  303. 205 => 'Reset Content',
  304. 206 => 'Partial Content',
  305. 300 => 'Multiple Choices',
  306. 301 => 'Moved Permanently',
  307. 302 => 'Found',
  308. 304 => 'Not Modified',
  309. 305 => 'Use Proxy',
  310. 307 => 'Temporary Redirect',
  311. 400 => 'Bad Request',
  312. 401 => 'Unauthorized',
  313. 403 => 'Forbidden',
  314. 404 => 'Not Found',
  315. 405 => 'Method Not Allowed',
  316. 406 => 'Not Acceptable',
  317. 407 => 'Proxy Authentication Required',
  318. 408 => 'Request Timeout',
  319. 409 => 'Conflict',
  320. 410 => 'Gone',
  321. 411 => 'Length Required',
  322. 412 => 'Precondition Failed',
  323. 413 => 'Request Entity Too Large',
  324. 414 => 'Request-URI Too Long',
  325. 415 => 'Unsupported Media Type',
  326. 416 => 'Requested Range Not Satisfiable',
  327. 417 => 'Expectation Failed',
  328. 500 => 'Internal Server Error',
  329. 501 => 'Not Implemented',
  330. 502 => 'Bad Gateway',
  331. 503 => 'Service Unavailable',
  332. 504 => 'Gateway Timeout',
  333. 505 => 'HTTP Version Not Supported'
  334. );
  335. if ($code == '' OR ! is_numeric($code))
  336. {
  337. show_error('Status codes must be numeric', 500);
  338. }
  339. if (isset($stati[$code]) AND $text == '')
  340. {
  341. $text = $stati[$code];
  342. }
  343. if ($text == '')
  344. {
  345. show_error('No status text available. Please check your status code number or supply your own message text.', 500);
  346. }
  347. $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
  348. if (substr(php_sapi_name(), 0, 3) == 'cgi')
  349. {
  350. header("Status: {$code} {$text}", TRUE);
  351. }
  352. elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
  353. {
  354. header($server_protocol." {$code} {$text}", TRUE, $code);
  355. }
  356. else
  357. {
  358. header("HTTP/1.1 {$code} {$text}", TRUE, $code);
  359. }
  360. }
  361. // --------------------------------------------------------------------
  362. /**
  363. * Exception Handler
  364. *
  365. * This is the custom exception handler that is declared at the top
  366. * of Codeigniter.php. The main reason we use this is to permit
  367. * PHP errors to be logged in our own log files since the user may
  368. * not have access to server logs. Since this function
  369. * effectively intercepts PHP errors, however, we also need
  370. * to display errors based on the current error_reporting level.
  371. * We do that with the use of a PHP error template.
  372. *
  373. * @access private
  374. * @return void
  375. */
  376. function _exception_handler($severity, $message, $filepath, $line)
  377. {
  378. // We don't bother with "strict" notices since they tend to fill up
  379. // the log file with excess information that isn't normally very helpful.
  380. // For example, if you are running PHP 5 and you use version 4 style
  381. // class functions (without prefixes like "public", "private", etc.)
  382. // you'll get notices telling you that these have been deprecated.
  383. if ($severity == E_STRICT)
  384. {
  385. return;
  386. }
  387. $_error =& load_class('Exceptions', 'core');
  388. // Should we display the error? We'll get the current error_reporting
  389. // level and add its bits with the severity bits to find out.
  390. if (($severity & error_reporting()) == $severity)
  391. {
  392. $_error->show_php_error($severity, $message, $filepath, $line);
  393. }
  394. // Should we log the error? No? We're done...
  395. if (config_item('log_threshold') == 0)
  396. {
  397. return;
  398. }
  399. $_error->log_exception($severity, $message, $filepath, $line);
  400. }
  401. // --------------------------------------------------------------------
  402. /**
  403. * Remove Invisible Characters
  404. *
  405. * This prevents sandwiching null characters
  406. * between ascii characters, like Java\0script.
  407. *
  408. * @access public
  409. * @param string
  410. * @return string
  411. */
  412. function remove_invisible_characters($str, $url_encoded = TRUE)
  413. {
  414. $non_displayables = array();
  415. // every control character except newline (dec 10)
  416. // carriage return (dec 13), and horizontal tab (dec 09)
  417. if ($url_encoded)
  418. {
  419. $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
  420. $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
  421. }
  422. $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
  423. do
  424. {
  425. $str = preg_replace($non_displayables, '', $str, -1, $count);
  426. }
  427. while ($count);
  428. return $str;
  429. }
  430. /* End of file Common.php */
  431. /* Location: ./system/core/Common.php */