base.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. /**
  13. * Loads in a core class and optionally an app class override if it exists.
  14. *
  15. * @param string $path
  16. * @param string $folder
  17. * @return void
  18. */
  19. if ( ! function_exists('import'))
  20. {
  21. function import($path, $folder = 'classes')
  22. {
  23. $path = str_replace('/', DIRECTORY_SEPARATOR, $path);
  24. require_once COREPATH.$folder.DIRECTORY_SEPARATOR.$path.'.php';
  25. if (is_file(APPPATH.$folder.DIRECTORY_SEPARATOR.$path.'.php'))
  26. {
  27. require_once APPPATH.$folder.DIRECTORY_SEPARATOR.$path.'.php';
  28. }
  29. }
  30. }
  31. if ( ! function_exists('logger'))
  32. {
  33. function logger($level, $msg, $method = null)
  34. {
  35. static $labels = array(
  36. 100 => 'DEBUG',
  37. 200 => 'INFO',
  38. 250 => 'NOTICE',
  39. 300 => 'WARNING',
  40. 400 => 'ERROR',
  41. 500 => 'CRITICAL',
  42. 550 => 'ALERT',
  43. 600 => 'EMERGENCY',
  44. 700 => 'ALL',
  45. );
  46. // make sure $level has the correct value
  47. if ((is_int($level) and ! isset($labels[$level])) or (is_string($level) and ! array_search(strtoupper($level), $labels)))
  48. {
  49. throw new \FuelException('Invalid level "'.$level.'" passed to logger()');
  50. }
  51. // get the levels defined to be logged
  52. $loglabels = \Config::get('log_threshold');
  53. // bail out if we don't need logging at all
  54. if ($loglabels == \Fuel::L_NONE)
  55. {
  56. return false;
  57. }
  58. // if profiling is active log the message to the profile
  59. if (\Config::get('profiling'))
  60. {
  61. \Console::log($method.' - '.$msg);
  62. }
  63. // if it's not an array, assume it's an "up to" level
  64. if ( ! is_array($loglabels))
  65. {
  66. $a = array();
  67. foreach ($labels as $l => $label)
  68. {
  69. $l >= $loglabels and $a[] = $l;
  70. }
  71. $loglabels = $a;
  72. }
  73. // do we need to log the message with this level?
  74. if ( ! in_array($level, $loglabels))
  75. {
  76. return false;
  77. }
  78. ! class_exists('Log') and \Package::load('log');
  79. return \Log::instance()->log($level, (empty($method) ? '' : $method.' - ').$msg);
  80. }
  81. }
  82. /**
  83. * Takes an array of attributes and turns it into a string for an html tag
  84. *
  85. * @param array $attr
  86. * @return string
  87. */
  88. if ( ! function_exists('array_to_attr'))
  89. {
  90. function array_to_attr($attr)
  91. {
  92. $attr_str = '';
  93. foreach ((array) $attr as $property => $value)
  94. {
  95. // Ignore null/false
  96. if ($value === null or $value === false)
  97. {
  98. continue;
  99. }
  100. // If the key is numeric then it must be something like selected="selected"
  101. if (is_numeric($property))
  102. {
  103. $property = $value;
  104. }
  105. $attr_str .= $property.'="'.$value.'" ';
  106. }
  107. // We strip off the last space for return
  108. return trim($attr_str);
  109. }
  110. }
  111. /**
  112. * Create a XHTML tag
  113. *
  114. * @param string The tag name
  115. * @param array|string The tag attributes
  116. * @param string|bool The content to place in the tag, or false for no closing tag
  117. * @return string
  118. */
  119. if ( ! function_exists('html_tag'))
  120. {
  121. function html_tag($tag, $attr = array(), $content = false)
  122. {
  123. $has_content = (bool) ($content !== false and $content !== null);
  124. $html = '<'.$tag;
  125. $html .= ( ! empty($attr)) ? ' '.(is_array($attr) ? array_to_attr($attr) : $attr) : '';
  126. $html .= $has_content ? '>' : ' />';
  127. $html .= $has_content ? $content.'</'.$tag.'>' : '';
  128. return $html;
  129. }
  130. }
  131. /**
  132. * A case-insensitive version of in_array.
  133. *
  134. * @param mixed $needle
  135. * @param array $haystack
  136. * @return bool
  137. */
  138. if ( ! function_exists('in_arrayi'))
  139. {
  140. function in_arrayi($needle, $haystack)
  141. {
  142. return in_array(strtolower($needle), array_map('strtolower', $haystack));
  143. }
  144. }
  145. /**
  146. * Gets all the public vars for an object. Use this if you need to get all the
  147. * public vars of $this inside an object.
  148. *
  149. * @return array
  150. */
  151. if ( ! function_exists('get_object_public_vars'))
  152. {
  153. function get_object_public_vars($obj)
  154. {
  155. return get_object_vars($obj);
  156. }
  157. }
  158. /**
  159. * Renders a view and returns the output.
  160. *
  161. * @param string The view name/path
  162. * @param array The data for the view
  163. * @param bool Auto filter override
  164. * @return string
  165. */
  166. if ( ! function_exists('render'))
  167. {
  168. function render($view, $data = null, $auto_filter = null)
  169. {
  170. return \View::forge($view, $data, $auto_filter)->render();
  171. }
  172. }
  173. /**
  174. * A wrapper function for Lang::get()
  175. *
  176. * @param mixed The string to translate
  177. * @param array The parameters
  178. * @return string
  179. */
  180. if ( ! function_exists('__'))
  181. {
  182. function __($string, $params = array(), $default = null)
  183. {
  184. return \Lang::get($string, $params, $default);
  185. }
  186. }
  187. /**
  188. * Encodes the given string. This is just a wrapper function for Security::htmlentities()
  189. *
  190. * @param mixed The string to encode
  191. * @return string
  192. */
  193. if ( ! function_exists('e'))
  194. {
  195. function e($string)
  196. {
  197. return Security::htmlentities($string);
  198. }
  199. }
  200. /**
  201. * Takes a classname and returns the actual classname for an alias or just the classname
  202. * if it's a normal class.
  203. *
  204. * @param string classname to check
  205. * @return string real classname
  206. */
  207. if ( ! function_exists('get_real_class'))
  208. {
  209. function get_real_class($class)
  210. {
  211. static $classes = array();
  212. if ( ! array_key_exists($class, $classes))
  213. {
  214. $reflect = new ReflectionClass($class);
  215. $classes[$class] = $reflect->getName();
  216. }
  217. return $classes[$class];
  218. }
  219. }
  220. /**
  221. * Takes an associative array in the layout of parse_url, and constructs a URL from it
  222. *
  223. * see http://www.php.net/manual/en/function.http-build-url.php#96335
  224. *
  225. * @param mixed (Part(s) of) an URL in form of a string or associative array like parse_url() returns
  226. * @param mixed Same as the first argument
  227. * @param int A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default
  228. * @param array If set, it will be filled with the parts of the composed url like parse_url() would return
  229. *
  230. * @return string constructed URL
  231. */
  232. if (!function_exists('http_build_url'))
  233. {
  234. define('HTTP_URL_REPLACE', 1); // Replace every part of the first URL when there's one of the second URL
  235. define('HTTP_URL_JOIN_PATH', 2); // Join relative paths
  236. define('HTTP_URL_JOIN_QUERY', 4); // Join query strings
  237. define('HTTP_URL_STRIP_USER', 8); // Strip any user authentication information
  238. define('HTTP_URL_STRIP_PASS', 16); // Strip any password authentication information
  239. define('HTTP_URL_STRIP_AUTH', 32); // Strip any authentication information
  240. define('HTTP_URL_STRIP_PORT', 64); // Strip explicit port numbers
  241. define('HTTP_URL_STRIP_PATH', 128); // Strip complete path
  242. define('HTTP_URL_STRIP_QUERY', 256); // Strip query string
  243. define('HTTP_URL_STRIP_FRAGMENT', 512); // Strip any fragments (#identifier)
  244. define('HTTP_URL_STRIP_ALL', 1024); // Strip anything but scheme and host
  245. function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false)
  246. {
  247. $keys = array('user','pass','port','path','query','fragment');
  248. // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
  249. if ($flags & HTTP_URL_STRIP_ALL)
  250. {
  251. $flags |= HTTP_URL_STRIP_USER;
  252. $flags |= HTTP_URL_STRIP_PASS;
  253. $flags |= HTTP_URL_STRIP_PORT;
  254. $flags |= HTTP_URL_STRIP_PATH;
  255. $flags |= HTTP_URL_STRIP_QUERY;
  256. $flags |= HTTP_URL_STRIP_FRAGMENT;
  257. }
  258. // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
  259. else if ($flags & HTTP_URL_STRIP_AUTH)
  260. {
  261. $flags |= HTTP_URL_STRIP_USER;
  262. $flags |= HTTP_URL_STRIP_PASS;
  263. }
  264. // parse the original URL
  265. $parse_url = is_array($url) ? $url : parse_url($url);
  266. // make sure we always have a scheme, host and path
  267. empty($parse_url['scheme']) and $parse_url['scheme'] = 'http';
  268. empty($parse_url['host']) and $parse_url['host'] = \Input::server('http_host');
  269. isset($parse_url['path']) or $parse_url['path'] = '';
  270. // make the path absolute if needed
  271. if ( ! empty($parse_url['path']) and substr($parse_url['path'], 0, 1) != '/')
  272. {
  273. $parse_url['path'] = '/'.$parse_url['path'];
  274. }
  275. // scheme and host are always replaced
  276. isset($parts['scheme']) and $parse_url['scheme'] = $parts['scheme'];
  277. isset($parts['host']) and $parse_url['host'] = $parts['host'];
  278. // replace the original URL with it's new parts (if applicable)
  279. if ($flags & HTTP_URL_REPLACE)
  280. {
  281. foreach ($keys as $key)
  282. {
  283. if (isset($parts[$key]))
  284. $parse_url[$key] = $parts[$key];
  285. }
  286. }
  287. else
  288. {
  289. // join the original URL path with the new path
  290. if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH))
  291. {
  292. if (isset($parse_url['path']))
  293. $parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/');
  294. else
  295. $parse_url['path'] = $parts['path'];
  296. }
  297. // join the original query string with the new query string
  298. if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY))
  299. {
  300. if (isset($parse_url['query']))
  301. $parse_url['query'] .= '&' . $parts['query'];
  302. else
  303. $parse_url['query'] = $parts['query'];
  304. }
  305. }
  306. // strips all the applicable sections of the URL
  307. // note: scheme and host are never stripped
  308. foreach ($keys as $key)
  309. {
  310. if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key)))
  311. unset($parse_url[$key]);
  312. }
  313. $new_url = $parse_url;
  314. return
  315. ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '')
  316. .((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') .'@' : '')
  317. .((isset($parse_url['host'])) ? $parse_url['host'] : '')
  318. .((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '')
  319. .((isset($parse_url['path'])) ? $parse_url['path'] : '')
  320. .((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '')
  321. .((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : '')
  322. ;
  323. }
  324. }