PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/fuel.php

https://bitbucket.org/sriedel/iccrm-wip
PHP | 446 lines | 290 code | 49 blank | 107 comment | 12 complexity | f0a8df186f400cf867eaebc7f86ebf23 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * General Fuel Exception class
  15. */
  16. class FuelException extends \Exception {}
  17. /**
  18. * The core of the framework.
  19. *
  20. * @package Fuel
  21. * @subpackage Core
  22. */
  23. class Fuel
  24. {
  25. /**
  26. * @var string The version of Fuel
  27. */
  28. const VERSION = '1.4';
  29. /**
  30. * @var string constant used for when in testing mode
  31. */
  32. const TEST = 'test';
  33. /**
  34. * @var string constant used for when in development
  35. */
  36. const DEVELOPMENT = 'development';
  37. /**
  38. * @var string constant used for when in production
  39. */
  40. const PRODUCTION = 'production';
  41. /**
  42. * @var string constant used for when testing the app in a staging env.
  43. */
  44. const STAGE = 'stage';
  45. /**
  46. * @var int No logging
  47. */
  48. const L_NONE = 0;
  49. /**
  50. * @var int Log errors only
  51. */
  52. const L_ERROR = 1;
  53. /**
  54. * @var int Log warning massages and below
  55. */
  56. const L_WARNING = 2;
  57. /**
  58. * @var int Log debug massages and below
  59. */
  60. const L_DEBUG = 3;
  61. /**
  62. * @var int Log info massages and below
  63. */
  64. const L_INFO = 4;
  65. /**
  66. * @var int Log everything
  67. */
  68. const L_ALL = 5;
  69. /**
  70. * @var bool Whether Fuel has been initialized
  71. */
  72. public static $initialized = false;
  73. /**
  74. * @var string The Fuel environment
  75. */
  76. public static $env = \Fuel::DEVELOPMENT;
  77. /**
  78. * @var bool Whether to display the profiling information
  79. */
  80. public static $profiling = false;
  81. public static $locale = 'en_US';
  82. public static $timezone = 'UTC';
  83. public static $encoding = 'UTF-8';
  84. public static $path_cache = array();
  85. public static $caching = false;
  86. /**
  87. * The amount of time to cache in seconds.
  88. * @var int $cache_lifetime
  89. */
  90. public static $cache_lifetime = 3600;
  91. protected static $cache_dir = '';
  92. public static $paths_changed = false;
  93. public static $is_cli = false;
  94. public static $is_test = false;
  95. public static $volatile_paths = array();
  96. protected static $_paths = array();
  97. protected static $packages = array();
  98. final private function __construct() { }
  99. /**
  100. * Initializes the framework. This can only be called once.
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. public static function init($config)
  106. {
  107. if (static::$initialized)
  108. {
  109. throw new \FuelException("You can't initialize Fuel more than once.");
  110. }
  111. static::$_paths = array(APPPATH, COREPATH);
  112. // Is Fuel running on the command line?
  113. static::$is_cli = (bool) defined('STDIN');
  114. \Config::load($config);
  115. // Start up output buffering
  116. ob_start(\Config::get('ob_callback', null));
  117. static::$profiling = \Config::get('profiling', false);
  118. if (static::$profiling)
  119. {
  120. \Profiler::init();
  121. \Profiler::mark(__METHOD__.' Start');
  122. }
  123. static::$cache_dir = \Config::get('cache_dir', APPPATH.'cache/');
  124. static::$caching = \Config::get('caching', false);
  125. static::$cache_lifetime = \Config::get('cache_lifetime', 3600);
  126. if (static::$caching)
  127. {
  128. \Finder::instance()->read_cache('Fuel::paths');
  129. }
  130. // set a default timezone if one is defined
  131. static::$timezone = \Config::get('default_timezone') ?: date_default_timezone_get();
  132. date_default_timezone_set(static::$timezone);
  133. static::$encoding = \Config::get('encoding', static::$encoding);
  134. MBSTRING and mb_internal_encoding(static::$encoding);
  135. static::$locale = \Config::get('locale', static::$locale);
  136. if ( ! static::$is_cli)
  137. {
  138. if (\Config::get('base_url') === null)
  139. {
  140. \Config::set('base_url', static::generate_base_url());
  141. }
  142. }
  143. // Run Input Filtering
  144. \Security::clean_input();
  145. \Event::register('shutdown', 'Fuel::finish');
  146. // Always load classes, config & language set in always_load.php config
  147. static::always_load();
  148. // Load in the routes
  149. \Config::load('routes', true);
  150. \Router::add(\Config::get('routes'));
  151. // Set locale, log warning when it fails
  152. if (static::$locale)
  153. {
  154. setlocale(LC_ALL, static::$locale) or
  155. logger(\Fuel::L_WARNING, 'The configured locale '.static::$locale.' is not installed on your system.', __METHOD__);
  156. }
  157. static::$initialized = true;
  158. // fire any app created events
  159. \Event::instance()->has_events('app_created') and \Event::instance()->trigger('app_created', '', 'none');
  160. if (static::$profiling)
  161. {
  162. \Profiler::mark(__METHOD__.' End');
  163. }
  164. }
  165. /**
  166. * Cleans up Fuel execution, ends the output buffering, and outputs the
  167. * buffer contents.
  168. *
  169. * @access public
  170. * @return void
  171. */
  172. public static function finish()
  173. {
  174. if (static::$caching)
  175. {
  176. \Finder::instance()->write_cache('Fuel::paths');
  177. }
  178. if (static::$profiling)
  179. {
  180. // Grab the output buffer and flush it, we will rebuffer later
  181. $output = ob_get_clean();
  182. $headers = headers_list();
  183. $show = true;
  184. foreach ($headers as $header)
  185. {
  186. if (stripos($header, 'content-type') === 0 and stripos($header, 'text/html') === false)
  187. {
  188. $show = false;
  189. }
  190. }
  191. if ($show)
  192. {
  193. \Profiler::mark('End of Fuel Execution');
  194. if (preg_match("|</body>.*?</html>|is", $output))
  195. {
  196. $output = preg_replace("|</body>.*?</html>|is", '', $output);
  197. $output .= \Profiler::output();
  198. $output .= '</body></html>';
  199. }
  200. else
  201. {
  202. $output .= \Profiler::output();
  203. }
  204. }
  205. // Restart the output buffer and send the new output
  206. ob_start();
  207. echo $output;
  208. }
  209. }
  210. /**
  211. * Generates a base url.
  212. *
  213. * @return string the base url
  214. */
  215. protected static function generate_base_url()
  216. {
  217. $base_url = '';
  218. if(\Input::server('http_host'))
  219. {
  220. $base_url .= \Input::protocol().'://'.\Input::server('http_host');
  221. }
  222. if (\Input::server('script_name'))
  223. {
  224. $base_url .= str_replace('\\', '/', dirname(\Input::server('script_name')));
  225. }
  226. // Add a slash if it is missing and return it
  227. return rtrim($base_url, '/').'/';
  228. }
  229. /**
  230. * Includes the given file and returns the results.
  231. *
  232. * @param string the path to the file
  233. * @return mixed the results of the include
  234. */
  235. public static function load($file)
  236. {
  237. return include $file;
  238. }
  239. /**
  240. * This method does basic filesystem caching. It is used for things like path caching.
  241. *
  242. * This method is from KohanaPHP's Kohana class.
  243. *
  244. * @param string the cache name
  245. * @param array the data to cache (if non given it returns)
  246. * @param int the number of seconds for the cache too live
  247. */
  248. public static function cache($name, $data = null, $lifetime = null)
  249. {
  250. // Cache file is a hash of the name
  251. $file = sha1($name).'.txt';
  252. // Cache directories are split by keys to prevent filesystem overload
  253. $dir = rtrim(static::$cache_dir, DS).DS.$file[0].$file[1].DS;
  254. if ($lifetime === NULL)
  255. {
  256. // Use the default lifetime
  257. $lifetime = static::$cache_lifetime;
  258. }
  259. if ($data === null)
  260. {
  261. if (is_file($dir.$file))
  262. {
  263. if ((time() - filemtime($dir.$file)) < $lifetime)
  264. {
  265. // Return the cache
  266. return unserialize(file_get_contents($dir.$file));
  267. }
  268. else
  269. {
  270. try
  271. {
  272. // Cache has expired
  273. unlink($dir.$file);
  274. }
  275. catch (Exception $e)
  276. {
  277. // Cache has mostly likely already been deleted,
  278. // let return happen normally.
  279. }
  280. }
  281. }
  282. // Cache not found
  283. return NULL;
  284. }
  285. if ( ! is_dir($dir))
  286. {
  287. // Create the cache directory
  288. mkdir($dir, octdec(\Config::get('file.chmod.folders', 0777)), true);
  289. // Set permissions (must be manually set to fix umask issues)
  290. chmod($dir, octdec(\Config::get('file.chmod.folders', 0777)));
  291. }
  292. // Force the data to be a string
  293. $data = serialize($data);
  294. try
  295. {
  296. // Write the cache
  297. return (bool) file_put_contents($dir.$file, $data, LOCK_EX);
  298. }
  299. catch (Exception $e)
  300. {
  301. // Failed to write cache
  302. return false;
  303. }
  304. }
  305. /**
  306. * Always load packages, modules, classes, config & language files set in always_load.php config
  307. *
  308. * @param array what to autoload
  309. */
  310. public static function always_load($array = null)
  311. {
  312. is_null($array) and $array = \Config::get('always_load', array());
  313. isset($array['packages']) and \Package::load($array['packages']);
  314. isset($array['modules']) and \Module::load($array['modules']);
  315. if (isset($array['classes']))
  316. {
  317. foreach ($array['classes'] as $class)
  318. {
  319. if ( ! class_exists($class = ucfirst($class)))
  320. {
  321. throw new \FuelException('Always load class does not exist. Unable to load: '.$class);
  322. }
  323. }
  324. }
  325. /**
  326. * Config and Lang must be either just the filename, example: array(filename)
  327. * or the filename as key and the group as value, example: array(filename => some_group)
  328. */
  329. if (isset($array['config']))
  330. {
  331. foreach ($array['config'] as $config => $config_group)
  332. {
  333. \Config::load((is_int($config) ? $config_group : $config), (is_int($config) ? true : $config_group));
  334. }
  335. }
  336. if (isset($array['language']))
  337. {
  338. foreach ($array['language'] as $lang => $lang_group)
  339. {
  340. \Lang::load((is_int($lang) ? $lang_group : $lang), (is_int($lang) ? true : $lang_group));
  341. }
  342. }
  343. }
  344. /**
  345. * Takes a value and checks if it is a Closure or not, if it is it
  346. * will return the result of the closure, if not, it will simply return the
  347. * value.
  348. *
  349. * @param mixed $var The value to get
  350. * @return mixed
  351. */
  352. public static function value($var)
  353. {
  354. return ($var instanceof \Closure) ? $var() : $var;
  355. }
  356. /**
  357. * Cleans a file path so that it does not contain absolute file paths.
  358. *
  359. * @param string the filepath
  360. * @return string the clean path
  361. */
  362. public static function clean_path($path)
  363. {
  364. static $search = array(APPPATH, COREPATH, PKGPATH, DOCROOT, '\\');
  365. static $replace = array('APPPATH/', 'COREPATH/', 'PKGPATH/', 'DOCROOT/', '/');
  366. return str_ireplace($search, $replace, $path);
  367. }
  368. }