PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Common.php

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