PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Common.php

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