/halogy/codeigniter/Common.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 421 lines · 220 code · 58 blank · 143 comment · 38 complexity · 03d6e454dc152f9503b2a4f7316cb080 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 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, 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. * Determines if the current version of PHP is greater then the supplied value
  18. *
  19. * Since there are a few places where we conditionally test for PHP > 5
  20. * we'll set a static variable.
  21. *
  22. * @access public
  23. * @param string
  24. * @return bool
  25. */
  26. function is_php($version = '5.0.0')
  27. {
  28. static $_is_php;
  29. $version = (string)$version;
  30. if ( ! isset($_is_php[$version]))
  31. {
  32. $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
  33. }
  34. return $_is_php[$version];
  35. }
  36. // ------------------------------------------------------------------------
  37. /**
  38. * Tests for file writability
  39. *
  40. * is_writable() returns TRUE on Windows servers when you really can't write to
  41. * the file, based on the read-only attribute. is_writable() is also unreliable
  42. * on Unix servers if safe_mode is on.
  43. *
  44. * @access private
  45. * @return void
  46. */
  47. function is_really_writable($file)
  48. {
  49. // If we're on a Unix server with safe_mode off we call is_writable
  50. if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
  51. {
  52. return is_writable($file);
  53. }
  54. // For windows servers and safe_mode "on" installations we'll actually
  55. // write a file then read it. Bah...
  56. if (is_dir($file))
  57. {
  58. $file = rtrim($file, '/').'/'.md5(rand(1,100));
  59. if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  60. {
  61. return FALSE;
  62. }
  63. fclose($fp);
  64. @chmod($file, DIR_WRITE_MODE);
  65. @unlink($file);
  66. return TRUE;
  67. }
  68. elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  69. {
  70. return FALSE;
  71. }
  72. fclose($fp);
  73. return TRUE;
  74. }
  75. // ------------------------------------------------------------------------
  76. /**
  77. * Class registry
  78. *
  79. * This function acts as a singleton. If the requested class does not
  80. * exist it is instantiated and set to a static variable. If it has
  81. * previously been instantiated the variable is returned.
  82. *
  83. * @access public
  84. * @param string the class name being requested
  85. * @param bool optional flag that lets classes get loaded but not instantiated
  86. * @return object
  87. */
  88. function &load_class($class, $instantiate = TRUE)
  89. {
  90. static $objects = array();
  91. // Does the class exist? If so, we're done...
  92. if (isset($objects[$class]))
  93. {
  94. return $objects[$class];
  95. }
  96. // If the requested class does not exist in the application/libraries
  97. // folder we'll load the native class from the system/libraries folder.
  98. if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
  99. {
  100. require(BASEPATH.'libraries/'.$class.EXT);
  101. require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
  102. $is_subclass = TRUE;
  103. }
  104. else
  105. {
  106. if (file_exists(APPPATH.'libraries/'.$class.EXT))
  107. {
  108. require(APPPATH.'libraries/'.$class.EXT);
  109. $is_subclass = FALSE;
  110. }
  111. else
  112. {
  113. require(BASEPATH.'libraries/'.$class.EXT);
  114. $is_subclass = FALSE;
  115. }
  116. }
  117. if ($instantiate == FALSE)
  118. {
  119. $objects[$class] = TRUE;
  120. return $objects[$class];
  121. }
  122. if ($is_subclass == TRUE)
  123. {
  124. $name = config_item('subclass_prefix').$class;
  125. $objects[$class] =& instantiate_class(new $name());
  126. return $objects[$class];
  127. }
  128. $name = ($class != 'Controller') ? 'CI_'.$class : $class;
  129. $objects[$class] =& instantiate_class(new $name());
  130. return $objects[$class];
  131. }
  132. /**
  133. * Instantiate Class
  134. *
  135. * Returns a new class object by reference, used by load_class() and the DB class.
  136. * Required to retain PHP 4 compatibility and also not make PHP 5.3 cry.
  137. *
  138. * Use: $obj =& instantiate_class(new Foo());
  139. *
  140. * @access public
  141. * @param object
  142. * @return object
  143. */
  144. function &instantiate_class(&$class_object)
  145. {
  146. return $class_object;
  147. }
  148. /**
  149. * Loads the main config.php file
  150. *
  151. * @access private
  152. * @return array
  153. */
  154. function &get_config()
  155. {
  156. static $main_conf;
  157. if ( ! isset($main_conf))
  158. {
  159. if ( ! file_exists(APPPATH.'config/config'.EXT))
  160. {
  161. exit('The configuration file config'.EXT.' does not exist.');
  162. }
  163. require(APPPATH.'config/config'.EXT);
  164. if ( ! isset($config) OR ! is_array($config))
  165. {
  166. exit('Your config file does not appear to be formatted correctly.');
  167. }
  168. $main_conf[0] =& $config;
  169. }
  170. return $main_conf[0];
  171. }
  172. /**
  173. * Gets a config item
  174. *
  175. * @access public
  176. * @return mixed
  177. */
  178. function config_item($item)
  179. {
  180. static $config_item = array();
  181. if ( ! isset($config_item[$item]))
  182. {
  183. $config =& get_config();
  184. if ( ! isset($config[$item]))
  185. {
  186. return FALSE;
  187. }
  188. $config_item[$item] = $config[$item];
  189. }
  190. return $config_item[$item];
  191. }
  192. /**
  193. * Error Handler
  194. *
  195. * This function lets us invoke the exception class and
  196. * display errors using the standard error template located
  197. * in application/errors/errors.php
  198. * This function will send the error page directly to the
  199. * browser and exit.
  200. *
  201. * @access public
  202. * @return void
  203. */
  204. function show_error($message, $status_code = 500)
  205. {
  206. $error =& load_class('Exceptions');
  207. echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
  208. exit;
  209. }
  210. /**
  211. * 404 Page Handler
  212. *
  213. * This function is similar to the show_error() function above
  214. * However, instead of the standard error template it displays
  215. * 404 errors.
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. function show_404($page = '')
  221. {
  222. $error =& load_class('Exceptions');
  223. $error->show_404($page);
  224. exit;
  225. }
  226. /**
  227. * Error Logging Interface
  228. *
  229. * We use this as a simple mechanism to access the logging
  230. * class and send messages to be logged.
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. function log_message($level = 'error', $message, $php_error = FALSE)
  236. {
  237. static $LOG;
  238. $config =& get_config();
  239. if ($config['log_threshold'] == 0)
  240. {
  241. return;
  242. }
  243. $LOG =& load_class('Log');
  244. $LOG->write_log($level, $message, $php_error);
  245. }
  246. /**
  247. * Set HTTP Status Header
  248. *
  249. * @access public
  250. * @param int the status code
  251. * @param string
  252. * @return void
  253. */
  254. function set_status_header($code = 200, $text = '')
  255. {
  256. $stati = array(
  257. 200 => 'OK',
  258. 201 => 'Created',
  259. 202 => 'Accepted',
  260. 203 => 'Non-Authoritative Information',
  261. 204 => 'No Content',
  262. 205 => 'Reset Content',
  263. 206 => 'Partial Content',
  264. 300 => 'Multiple Choices',
  265. 301 => 'Moved Permanently',
  266. 302 => 'Found',
  267. 304 => 'Not Modified',
  268. 305 => 'Use Proxy',
  269. 307 => 'Temporary Redirect',
  270. 400 => 'Bad Request',
  271. 401 => 'Unauthorized',
  272. 403 => 'Forbidden',
  273. 404 => 'Not Found',
  274. 405 => 'Method Not Allowed',
  275. 406 => 'Not Acceptable',
  276. 407 => 'Proxy Authentication Required',
  277. 408 => 'Request Timeout',
  278. 409 => 'Conflict',
  279. 410 => 'Gone',
  280. 411 => 'Length Required',
  281. 412 => 'Precondition Failed',
  282. 413 => 'Request Entity Too Large',
  283. 414 => 'Request-URI Too Long',
  284. 415 => 'Unsupported Media Type',
  285. 416 => 'Requested Range Not Satisfiable',
  286. 417 => 'Expectation Failed',
  287. 500 => 'Internal Server Error',
  288. 501 => 'Not Implemented',
  289. 502 => 'Bad Gateway',
  290. 503 => 'Service Unavailable',
  291. 504 => 'Gateway Timeout',
  292. 505 => 'HTTP Version Not Supported'
  293. );
  294. if ($code == '' OR ! is_numeric($code))
  295. {
  296. show_error('Status codes must be numeric', 500);
  297. }
  298. if (isset($stati[$code]) AND $text == '')
  299. {
  300. $text = $stati[$code];
  301. }
  302. if ($text == '')
  303. {
  304. show_error('No status text available. Please check your status code number or supply your own message text.', 500);
  305. }
  306. $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
  307. if (substr(php_sapi_name(), 0, 3) == 'cgi')
  308. {
  309. header("Status: {$code} {$text}", TRUE);
  310. }
  311. elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
  312. {
  313. header($server_protocol." {$code} {$text}", TRUE, $code);
  314. }
  315. else
  316. {
  317. header("HTTP/1.1 {$code} {$text}", TRUE, $code);
  318. }
  319. }
  320. /**
  321. * Exception Handler
  322. *
  323. * This is the custom exception handler that is declaired at the top
  324. * of Codeigniter.php. The main reason we use this is permit
  325. * PHP errors to be logged in our own log files since we may
  326. * not have access to server logs. Since this function
  327. * effectively intercepts PHP errors, however, we also need
  328. * to display errors based on the current error_reporting level.
  329. * We do that with the use of a PHP error template.
  330. *
  331. * @access private
  332. * @return void
  333. */
  334. function _exception_handler($severity, $message, $filepath, $line)
  335. {
  336. // We don't bother with "strict" notices since they will fill up
  337. // the log file with information that isn't normally very
  338. // helpful. For example, if you are running PHP 5 and you
  339. // use version 4 style class functions (without prefixes
  340. // like "public", "private", etc.) you'll get notices telling
  341. // you that these have been deprecated.
  342. if ($severity == E_STRICT)
  343. {
  344. return;
  345. }
  346. $error =& load_class('Exceptions');
  347. // Should we display the error?
  348. // We'll get the current error_reporting level and add its bits
  349. // with the severity bits to find out.
  350. if (($severity & error_reporting()) == $severity)
  351. {
  352. $error->show_php_error($severity, $message, $filepath, $line);
  353. }
  354. // Should we log the error? No? We're done...
  355. $config =& get_config();
  356. if ($config['log_threshold'] == 0)
  357. {
  358. return;
  359. }
  360. $error->log_exception($severity, $message, $filepath, $line);
  361. }
  362. /* End of file Common.php */
  363. /* Location: ./system/codeigniter/Common.php */