PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/system/core/Common.php

https://bitbucket.org/naando_araujo/pagseguro
PHP | 505 lines | 248 code | 75 blank | 182 comment | 43 complexity | caa1907e4215f17e84addf183f59c804 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. 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 ( ! is_file($file) OR ($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.EXT))
  115. {
  116. $name = $prefix.$class;
  117. if (class_exists($name) === FALSE)
  118. {
  119. require($path.$directory.'/'.$class.EXT);
  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.EXT))
  126. {
  127. $name = config_item('subclass_prefix').$class;
  128. if (class_exists($name) === FALSE)
  129. {
  130. require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT);
  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. exit('Unable to locate the specified class: '.$class.EXT);
  139. }
  140. // Keep track of what we just loaded
  141. is_loaded($class);
  142. $_classes[$class] = new $name();
  143. return $_classes[$class];
  144. }
  145. // --------------------------------------------------------------------
  146. /**
  147. * Keeps track of which libraries have been loaded. This function is
  148. * called by the load_class() function above
  149. *
  150. * @access public
  151. * @return array
  152. */
  153. function is_loaded($class = '')
  154. {
  155. static $_is_loaded = array();
  156. if ($class != '')
  157. {
  158. $_is_loaded[strtolower($class)] = $class;
  159. }
  160. return $_is_loaded;
  161. }
  162. // ------------------------------------------------------------------------
  163. /**
  164. * Loads the main config.php file
  165. *
  166. * This function lets us grab the config file even if the Config class
  167. * hasn't been instantiated yet
  168. *
  169. * @access private
  170. * @return array
  171. */
  172. function &get_config($replace = array())
  173. {
  174. static $_config;
  175. if (isset($_config))
  176. {
  177. return $_config[0];
  178. }
  179. // Is the config file in the environment folder?
  180. if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT))
  181. {
  182. $file_path = APPPATH.'config/config'.EXT;
  183. }
  184. // Fetch the config file
  185. if ( ! file_exists($file_path))
  186. {
  187. exit('The configuration file does not exist.');
  188. }
  189. require($file_path);
  190. // Does the $config array exist in the file?
  191. if ( ! isset($config) OR ! is_array($config))
  192. {
  193. exit('Your config file does not appear to be formatted correctly.');
  194. }
  195. // Are any values being dynamically replaced?
  196. if (count($replace) > 0)
  197. {
  198. foreach ($replace as $key => $val)
  199. {
  200. if (isset($config[$key]))
  201. {
  202. $config[$key] = $val;
  203. }
  204. }
  205. }
  206. return $_config[0] =& $config;
  207. }
  208. // ------------------------------------------------------------------------
  209. /**
  210. * Returns the specified config item
  211. *
  212. * @access public
  213. * @return mixed
  214. */
  215. function config_item($item)
  216. {
  217. static $_config_item = array();
  218. if ( ! isset($_config_item[$item]))
  219. {
  220. $config =& get_config();
  221. if ( ! isset($config[$item]))
  222. {
  223. return FALSE;
  224. }
  225. $_config_item[$item] = $config[$item];
  226. }
  227. return $_config_item[$item];
  228. }
  229. // ------------------------------------------------------------------------
  230. /**
  231. * Error Handler
  232. *
  233. * This function lets us invoke the exception class and
  234. * display errors using the standard error template located
  235. * in application/errors/errors.php
  236. * This function will send the error page directly to the
  237. * browser and exit.
  238. *
  239. * @access public
  240. * @return void
  241. */
  242. function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
  243. {
  244. $_error =& load_class('Exceptions', 'core');
  245. echo $_error->show_error($heading, $message, 'error_general', $status_code);
  246. exit;
  247. }
  248. // ------------------------------------------------------------------------
  249. /**
  250. * 404 Page Handler
  251. *
  252. * This function is similar to the show_error() function above
  253. * However, instead of the standard error template it displays
  254. * 404 errors.
  255. *
  256. * @access public
  257. * @return void
  258. */
  259. function show_404($page = '', $log_error = TRUE)
  260. {
  261. $_error =& load_class('Exceptions', 'core');
  262. $_error->show_404($page, $log_error);
  263. exit;
  264. }
  265. // ------------------------------------------------------------------------
  266. /**
  267. * Error Logging Interface
  268. *
  269. * We use this as a simple mechanism to access the logging
  270. * class and send messages to be logged.
  271. *
  272. * @access public
  273. * @return void
  274. */
  275. function log_message($level = 'error', $message, $php_error = FALSE)
  276. {
  277. static $_log;
  278. if (config_item('log_threshold') == 0)
  279. {
  280. return;
  281. }
  282. $_log =& load_class('Log');
  283. $_log->write_log($level, $message, $php_error);
  284. }
  285. // ------------------------------------------------------------------------
  286. /**
  287. * Set HTTP Status Header
  288. *
  289. * @access public
  290. * @param int the status code
  291. * @param string
  292. * @return void
  293. */
  294. function set_status_header($code = 200, $text = '')
  295. {
  296. $stati = array(
  297. 200 => 'OK',
  298. 201 => 'Created',
  299. 202 => 'Accepted',
  300. 203 => 'Non-Authoritative Information',
  301. 204 => 'No Content',
  302. 205 => 'Reset Content',
  303. 206 => 'Partial Content',
  304. 300 => 'Multiple Choices',
  305. 301 => 'Moved Permanently',
  306. 302 => 'Found',
  307. 304 => 'Not Modified',
  308. 305 => 'Use Proxy',
  309. 307 => 'Temporary Redirect',
  310. 400 => 'Bad Request',
  311. 401 => 'Unauthorized',
  312. 403 => 'Forbidden',
  313. 404 => 'Not Found',
  314. 405 => 'Method Not Allowed',
  315. 406 => 'Not Acceptable',
  316. 407 => 'Proxy Authentication Required',
  317. 408 => 'Request Timeout',
  318. 409 => 'Conflict',
  319. 410 => 'Gone',
  320. 411 => 'Length Required',
  321. 412 => 'Precondition Failed',
  322. 413 => 'Request Entity Too Large',
  323. 414 => 'Request-URI Too Long',
  324. 415 => 'Unsupported Media Type',
  325. 416 => 'Requested Range Not Satisfiable',
  326. 417 => 'Expectation Failed',
  327. 500 => 'Internal Server Error',
  328. 501 => 'Not Implemented',
  329. 502 => 'Bad Gateway',
  330. 503 => 'Service Unavailable',
  331. 504 => 'Gateway Timeout',
  332. 505 => 'HTTP Version Not Supported'
  333. );
  334. if ($code == '' OR ! is_numeric($code))
  335. {
  336. show_error('Status codes must be numeric', 500);
  337. }
  338. if (isset($stati[$code]) AND $text == '')
  339. {
  340. $text = $stati[$code];
  341. }
  342. if ($text == '')
  343. {
  344. show_error('No status text available. Please check your status code number or supply your own message text.', 500);
  345. }
  346. $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
  347. if (substr(php_sapi_name(), 0, 3) == 'cgi')
  348. {
  349. header("Status: {$code} {$text}", TRUE);
  350. }
  351. elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
  352. {
  353. header($server_protocol." {$code} {$text}", TRUE, $code);
  354. }
  355. else
  356. {
  357. header("HTTP/1.1 {$code} {$text}", TRUE, $code);
  358. }
  359. }
  360. // --------------------------------------------------------------------
  361. /**
  362. * Exception Handler
  363. *
  364. * This is the custom exception handler that is declaired at the top
  365. * of Codeigniter.php. The main reason we use this is to permit
  366. * PHP errors to be logged in our own log files since the user may
  367. * not have access to server logs. Since this function
  368. * effectively intercepts PHP errors, however, we also need
  369. * to display errors based on the current error_reporting level.
  370. * We do that with the use of a PHP error template.
  371. *
  372. * @access private
  373. * @return void
  374. */
  375. function _exception_handler($severity, $message, $filepath, $line)
  376. {
  377. // We don't bother with "strict" notices since they tend to fill up
  378. // the log file with excess information that isn't normally very helpful.
  379. // For example, if you are running PHP 5 and you use version 4 style
  380. // class functions (without prefixes like "public", "private", etc.)
  381. // you'll get notices telling you that these have been deprecated.
  382. if ($severity == E_STRICT)
  383. {
  384. return;
  385. }
  386. $_error =& load_class('Exceptions', 'core');
  387. // Should we display the error? We'll get the current error_reporting
  388. // level and add its bits with the severity bits to find out.
  389. if (($severity & error_reporting()) == $severity)
  390. {
  391. $_error->show_php_error($severity, $message, $filepath, $line);
  392. }
  393. // Should we log the error? No? We're done...
  394. if (config_item('log_threshold') == 0)
  395. {
  396. return;
  397. }
  398. $_error->log_exception($severity, $message, $filepath, $line);
  399. }
  400. // --------------------------------------------------------------------
  401. /**
  402. * Remove Invisible Characters
  403. *
  404. * This prevents sandwiching null characters
  405. * between ascii characters, like Java\0script.
  406. *
  407. * @access public
  408. * @param string
  409. * @return string
  410. */
  411. function remove_invisible_characters($str, $url_encoded = TRUE)
  412. {
  413. $non_displayables = array();
  414. // every control character except newline (dec 10)
  415. // carriage return (dec 13), and horizontal tab (dec 09)
  416. if ($url_encoded)
  417. {
  418. $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
  419. $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
  420. }
  421. $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
  422. do
  423. {
  424. $str = preg_replace($non_displayables, '', $str, -1, $count);
  425. }
  426. while ($count);
  427. return $str;
  428. }
  429. /* End of file Common.php */
  430. /* Location: ./system/core/Common.php */