PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/base.php

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