PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/system/core/Common.php

https://gitlab.com/sylver.gocloud/gocloudasia-college-system-framework
PHP | 565 lines | 299 code | 76 blank | 190 comment | 57 complexity | 6e3e228b5a42524cb228a804704ab495 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 ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, 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. $_config[0] =& $config;
  221. return $_config[0];
  222. }
  223. }
  224. // ------------------------------------------------------------------------
  225. /**
  226. * Returns the specified config item
  227. *
  228. * @access public
  229. * @return mixed
  230. */
  231. if ( ! function_exists('config_item'))
  232. {
  233. function config_item($item)
  234. {
  235. static $_config_item = array();
  236. if ( ! isset($_config_item[$item]))
  237. {
  238. $config =& get_config();
  239. if ( ! isset($config[$item]))
  240. {
  241. return FALSE;
  242. }
  243. $_config_item[$item] = $config[$item];
  244. }
  245. return $_config_item[$item];
  246. }
  247. }
  248. // ------------------------------------------------------------------------
  249. /**
  250. * Error Handler
  251. *
  252. * This function lets us invoke the exception class and
  253. * display errors using the standard error template located
  254. * in application/errors/errors.php
  255. * This function will send the error page directly to the
  256. * browser and exit.
  257. *
  258. * @access public
  259. * @return void
  260. */
  261. if ( ! function_exists('show_error'))
  262. {
  263. function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
  264. {
  265. $_error =& load_class('Exceptions', 'core');
  266. echo $_error->show_error($heading, $message, 'error_general', $status_code);
  267. exit;
  268. }
  269. }
  270. // ------------------------------------------------------------------------
  271. /**
  272. * 404 Page Handler
  273. *
  274. * This function is similar to the show_error() function above
  275. * However, instead of the standard error template it displays
  276. * 404 errors.
  277. *
  278. * @access public
  279. * @return void
  280. */
  281. if ( ! function_exists('show_404'))
  282. {
  283. function show_404($page = '', $log_error = TRUE)
  284. {
  285. $_error =& load_class('Exceptions', 'core');
  286. $_error->show_404($page, $log_error);
  287. exit;
  288. }
  289. }
  290. // ------------------------------------------------------------------------
  291. /**
  292. * Error Logging Interface
  293. *
  294. * We use this as a simple mechanism to access the logging
  295. * class and send messages to be logged.
  296. *
  297. * @access public
  298. * @return void
  299. */
  300. if ( ! function_exists('log_message'))
  301. {
  302. function log_message($level = 'error', $message, $php_error = FALSE)
  303. {
  304. static $_log;
  305. if (config_item('log_threshold') == 0)
  306. {
  307. return;
  308. }
  309. $_log =& load_class('Log');
  310. $_log->write_log($level, $message, $php_error);
  311. }
  312. }
  313. // ------------------------------------------------------------------------
  314. /**
  315. * Set HTTP Status Header
  316. *
  317. * @access public
  318. * @param int the status code
  319. * @param string
  320. * @return void
  321. */
  322. if ( ! function_exists('set_status_header'))
  323. {
  324. function set_status_header($code = 200, $text = '')
  325. {
  326. $stati = array(
  327. 200 => 'OK',
  328. 201 => 'Created',
  329. 202 => 'Accepted',
  330. 203 => 'Non-Authoritative Information',
  331. 204 => 'No Content',
  332. 205 => 'Reset Content',
  333. 206 => 'Partial Content',
  334. 300 => 'Multiple Choices',
  335. 301 => 'Moved Permanently',
  336. 302 => 'Found',
  337. 304 => 'Not Modified',
  338. 305 => 'Use Proxy',
  339. 307 => 'Temporary Redirect',
  340. 400 => 'Bad Request',
  341. 401 => 'Unauthorized',
  342. 403 => 'Forbidden',
  343. 404 => 'Not Found',
  344. 405 => 'Method Not Allowed',
  345. 406 => 'Not Acceptable',
  346. 407 => 'Proxy Authentication Required',
  347. 408 => 'Request Timeout',
  348. 409 => 'Conflict',
  349. 410 => 'Gone',
  350. 411 => 'Length Required',
  351. 412 => 'Precondition Failed',
  352. 413 => 'Request Entity Too Large',
  353. 414 => 'Request-URI Too Long',
  354. 415 => 'Unsupported Media Type',
  355. 416 => 'Requested Range Not Satisfiable',
  356. 417 => 'Expectation Failed',
  357. 500 => 'Internal Server Error',
  358. 501 => 'Not Implemented',
  359. 502 => 'Bad Gateway',
  360. 503 => 'Service Unavailable',
  361. 504 => 'Gateway Timeout',
  362. 505 => 'HTTP Version Not Supported'
  363. );
  364. if ($code == '' OR ! is_numeric($code))
  365. {
  366. show_error('Status codes must be numeric', 500);
  367. }
  368. if (isset($stati[$code]) AND $text == '')
  369. {
  370. $text = $stati[$code];
  371. }
  372. if ($text == '')
  373. {
  374. show_error('No status text available. Please check your status code number or supply your own message text.', 500);
  375. }
  376. $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
  377. if (substr(php_sapi_name(), 0, 3) == 'cgi')
  378. {
  379. header("Status: {$code} {$text}", TRUE);
  380. }
  381. elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
  382. {
  383. header($server_protocol." {$code} {$text}", TRUE, $code);
  384. }
  385. else
  386. {
  387. header("HTTP/1.1 {$code} {$text}", TRUE, $code);
  388. }
  389. }
  390. }
  391. // --------------------------------------------------------------------
  392. /**
  393. * Exception Handler
  394. *
  395. * This is the custom exception handler that is declaired at the top
  396. * of Codeigniter.php. The main reason we use this is to permit
  397. * PHP errors to be logged in our own log files since the user may
  398. * not have access to server logs. Since this function
  399. * effectively intercepts PHP errors, however, we also need
  400. * to display errors based on the current error_reporting level.
  401. * We do that with the use of a PHP error template.
  402. *
  403. * @access private
  404. * @return void
  405. */
  406. if ( ! function_exists('_exception_handler'))
  407. {
  408. function _exception_handler($severity, $message, $filepath, $line)
  409. {
  410. // We don't bother with "strict" notices since they tend to fill up
  411. // the log file with excess information that isn't normally very helpful.
  412. // For example, if you are running PHP 5 and you use version 4 style
  413. // class functions (without prefixes like "public", "private", etc.)
  414. // you'll get notices telling you that these have been deprecated.
  415. if ($severity == E_STRICT)
  416. {
  417. return;
  418. }
  419. $_error =& load_class('Exceptions', 'core');
  420. // Should we display the error? We'll get the current error_reporting
  421. // level and add its bits with the severity bits to find out.
  422. if (($severity & error_reporting()) == $severity)
  423. {
  424. $_error->show_php_error($severity, $message, $filepath, $line);
  425. }
  426. // Should we log the error? No? We're done...
  427. if (config_item('log_threshold') == 0)
  428. {
  429. return;
  430. }
  431. $_error->log_exception($severity, $message, $filepath, $line);
  432. }
  433. }
  434. // --------------------------------------------------------------------
  435. /**
  436. * Remove Invisible Characters
  437. *
  438. * This prevents sandwiching null characters
  439. * between ascii characters, like Java\0script.
  440. *
  441. * @access public
  442. * @param string
  443. * @return string
  444. */
  445. if ( ! function_exists('remove_invisible_characters'))
  446. {
  447. function remove_invisible_characters($str, $url_encoded = TRUE)
  448. {
  449. $non_displayables = array();
  450. // every control character except newline (dec 10)
  451. // carriage return (dec 13), and horizontal tab (dec 09)
  452. if ($url_encoded)
  453. {
  454. $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
  455. $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
  456. }
  457. $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
  458. do
  459. {
  460. $str = preg_replace($non_displayables, '', $str, -1, $count);
  461. }
  462. while ($count);
  463. return $str;
  464. }
  465. }
  466. // ------------------------------------------------------------------------
  467. /**
  468. * Returns HTML escaped variable
  469. *
  470. * @access public
  471. * @param mixed
  472. * @return mixed
  473. */
  474. if ( ! function_exists('html_escape'))
  475. {
  476. function html_escape($var)
  477. {
  478. if (is_array($var))
  479. {
  480. return array_map('html_escape', $var);
  481. }
  482. else
  483. {
  484. return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
  485. }
  486. }
  487. }
  488. /* End of file Common.php */
  489. /* Location: ./system/core/Common.php */