PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Common.php

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