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

/laravel/html.php

https://bitbucket.org/Maron1/taqman
PHP | 481 lines | 172 code | 55 blank | 254 comment | 13 complexity | d20cdbd0bf6cc2108144df0afeddbf8a MD5 | raw file
  1. <?php namespace Laravel;
  2. class HTML {
  3. /**
  4. * The registered custom macros.
  5. *
  6. * @var array
  7. */
  8. public static $macros = array();
  9. /**
  10. * Cache application encoding locally to save expensive calls to config::get().
  11. *
  12. * @var string
  13. */
  14. public static $encoding = null;
  15. /**
  16. * Registers a custom macro.
  17. *
  18. * @param string $name
  19. * @param Closure $macro
  20. * @return void
  21. */
  22. public static function macro($name, $macro)
  23. {
  24. static::$macros[$name] = $macro;
  25. }
  26. /**
  27. * Convert HTML characters to entities.
  28. *
  29. * The encoding specified in the application configuration file will be used.
  30. *
  31. * @param string $value
  32. * @return string
  33. */
  34. public static function entities($value)
  35. {
  36. return htmlentities($value, ENT_QUOTES, static::encoding(), false);
  37. }
  38. /**
  39. * Convert entities to HTML characters.
  40. *
  41. * @param string $value
  42. * @return string
  43. */
  44. public static function decode($value)
  45. {
  46. return html_entity_decode($value, ENT_QUOTES, static::encoding());
  47. }
  48. /**
  49. * Convert HTML special characters.
  50. *
  51. * The encoding specified in the application configuration file will be used.
  52. *
  53. * @param string $value
  54. * @return string
  55. */
  56. public static function specialchars($value)
  57. {
  58. return htmlspecialchars($value, ENT_QUOTES, static::encoding(), false);
  59. }
  60. /**
  61. * Generate a link to a JavaScript file.
  62. *
  63. * <code>
  64. * // Generate a link to a JavaScript file
  65. * echo HTML::script('js/jquery.js');
  66. *
  67. * // Generate a link to a JavaScript file and add some attributes
  68. * echo HTML::script('js/jquery.js', array('defer'));
  69. * </code>
  70. *
  71. * @param string $url
  72. * @param array $attributes
  73. * @return string
  74. */
  75. public static function script($url, $attributes = array())
  76. {
  77. $url = URL::to_asset($url);
  78. return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
  79. }
  80. /**
  81. * Generate a link to a CSS file.
  82. *
  83. * If no media type is selected, "all" will be used.
  84. *
  85. * <code>
  86. * // Generate a link to a CSS file
  87. * echo HTML::style('css/common.css');
  88. *
  89. * // Generate a link to a CSS file and add some attributes
  90. * echo HTML::style('css/common.css', array('media' => 'print'));
  91. * </code>
  92. *
  93. * @param string $url
  94. * @param array $attributes
  95. * @return string
  96. */
  97. public static function style($url, $attributes = array())
  98. {
  99. $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
  100. $attributes = $attributes + $defaults;
  101. $url = URL::to_asset($url);
  102. return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
  103. }
  104. /**
  105. * Generate a HTML span.
  106. *
  107. * @param string $value
  108. * @param array $attributes
  109. * @return string
  110. */
  111. public static function span($value, $attributes = array())
  112. {
  113. return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
  114. }
  115. /**
  116. * Generate a HTML link.
  117. *
  118. * <code>
  119. * // Generate a link to a location within the application
  120. * echo HTML::link('user/profile', 'User Profile');
  121. *
  122. * // Generate a link to a location outside of the application
  123. * echo HTML::link('http://google.com', 'Google');
  124. * </code>
  125. *
  126. * @param string $url
  127. * @param string $title
  128. * @param array $attributes
  129. * @param bool $https
  130. * @return string
  131. */
  132. public static function link($url, $title = null, $attributes = array(), $https = null)
  133. {
  134. $url = URL::to($url, $https);
  135. if (is_null($title)) $title = $url;
  136. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  137. }
  138. /**
  139. * Generate a HTTPS HTML link.
  140. *
  141. * @param string $url
  142. * @param string $title
  143. * @param array $attributes
  144. * @return string
  145. */
  146. public static function link_to_secure($url, $title = null, $attributes = array())
  147. {
  148. return static::link($url, $title, $attributes, true);
  149. }
  150. /**
  151. * Generate an HTML link to an asset.
  152. *
  153. * The application index page will not be added to asset links.
  154. *
  155. * @param string $url
  156. * @param string $title
  157. * @param array $attributes
  158. * @param bool $https
  159. * @return string
  160. */
  161. public static function link_to_asset($url, $title = null, $attributes = array(), $https = null)
  162. {
  163. $url = URL::to_asset($url, $https);
  164. if (is_null($title)) $title = $url;
  165. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  166. }
  167. /**
  168. * Generate an HTTPS HTML link to an asset.
  169. *
  170. * @param string $url
  171. * @param string $title
  172. * @param array $attributes
  173. * @return string
  174. */
  175. public static function link_to_secure_asset($url, $title = null, $attributes = array())
  176. {
  177. return static::link_to_asset($url, $title, $attributes, true);
  178. }
  179. /**
  180. * Generate an HTML link to a route.
  181. *
  182. * An array of parameters may be specified to fill in URI segment wildcards.
  183. *
  184. * <code>
  185. * // Generate a link to the "profile" named route
  186. * echo HTML::link_to_route('profile', 'Profile');
  187. *
  188. * // Generate a link to the "profile" route and add some parameters
  189. * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
  190. * </code>
  191. *
  192. * @param string $name
  193. * @param string $title
  194. * @param array $parameters
  195. * @param array $attributes
  196. * @return string
  197. */
  198. public static function link_to_route($name, $title = null, $parameters = array(), $attributes = array())
  199. {
  200. return static::link(URL::to_route($name, $parameters), $title, $attributes);
  201. }
  202. /**
  203. * Generate an HTML link to a controller action.
  204. *
  205. * An array of parameters may be specified to fill in URI segment wildcards.
  206. *
  207. * <code>
  208. * // Generate a link to the "home@index" action
  209. * echo HTML::link_to_action('home@index', 'Home');
  210. *
  211. * // Generate a link to the "user@profile" route and add some parameters
  212. * echo HTML::link_to_action('user@profile', 'Profile', array('taylor'));
  213. * </code>
  214. *
  215. * @param string $action
  216. * @param string $title
  217. * @param array $parameters
  218. * @param array $attributes
  219. * @return string
  220. */
  221. public static function link_to_action($action, $title = null, $parameters = array(), $attributes = array())
  222. {
  223. return static::link(URL::to_action($action, $parameters), $title, $attributes);
  224. }
  225. /**
  226. * Generate an HTML link to a different language
  227. *
  228. * @param string $language
  229. * @param string $title
  230. * @param array $attributes
  231. * @return string
  232. */
  233. public static function link_to_language($language, $title = null, $attributes = array())
  234. {
  235. return static::link(URL::to_language($language), $title, $attributes);
  236. }
  237. /**
  238. * Generate an HTML mailto link.
  239. *
  240. * The E-Mail address will be obfuscated to protect it from spam bots.
  241. *
  242. * @param string $email
  243. * @param string $title
  244. * @param array $attributes
  245. * @return string
  246. */
  247. public static function mailto($email, $title = null, $attributes = array())
  248. {
  249. $email = static::email($email);
  250. if (is_null($title)) $title = $email;
  251. $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
  252. return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  253. }
  254. /**
  255. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  256. *
  257. * @param string $email
  258. * @return string
  259. */
  260. public static function email($email)
  261. {
  262. return str_replace('@', '&#64;', static::obfuscate($email));
  263. }
  264. /**
  265. * Generate an HTML image element.
  266. *
  267. * @param string $url
  268. * @param string $alt
  269. * @param array $attributes
  270. * @return string
  271. */
  272. public static function image($url, $alt = '', $attributes = array())
  273. {
  274. $attributes['alt'] = $alt;
  275. return '<img src="'.URL::to_asset($url).'"'.static::attributes($attributes).'>';
  276. }
  277. /**
  278. * Generate an ordered list of items.
  279. *
  280. * @param array $list
  281. * @param array $attributes
  282. * @return string
  283. */
  284. public static function ol($list, $attributes = array())
  285. {
  286. return static::listing('ol', $list, $attributes);
  287. }
  288. /**
  289. * Generate an un-ordered list of items.
  290. *
  291. * @param array $list
  292. * @param array $attributes
  293. * @return string
  294. */
  295. public static function ul($list, $attributes = array())
  296. {
  297. return static::listing('ul', $list, $attributes);
  298. }
  299. /**
  300. * Generate an ordered or un-ordered list.
  301. *
  302. * @param string $type
  303. * @param array $list
  304. * @param array $attributes
  305. * @return string
  306. */
  307. private static function listing($type, $list, $attributes = array())
  308. {
  309. $html = '';
  310. if (count($list) == 0) return $html;
  311. foreach ($list as $key => $value)
  312. {
  313. // If the value is an array, we will recurse the function so that we can
  314. // produce a nested list within the list being built. Of course, nested
  315. // lists may exist within nested lists, etc.
  316. if (is_array($value))
  317. {
  318. if (is_int($key))
  319. {
  320. $html .= static::listing($type, $value);
  321. }
  322. else
  323. {
  324. $html .= '<li>'.$key.static::listing($type, $value).'</li>';
  325. }
  326. }
  327. else
  328. {
  329. $html .= '<li>'.static::entities($value).'</li>';
  330. }
  331. }
  332. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  333. }
  334. /**
  335. * Generate a definition list.
  336. *
  337. * @param array $list
  338. * @param array $attributes
  339. * @return string
  340. */
  341. public static function dl($list, $attributes = array())
  342. {
  343. $html = '';
  344. if (count($list) == 0) return $html;
  345. foreach ($list as $term => $description)
  346. {
  347. $html .= '<dt>'.static::entities($term).'</dt>';
  348. $html .= '<dd>'.static::entities($description).'</dd>';
  349. }
  350. return '<dl'.static::attributes($attributes).'>'.$html.'</dl>';
  351. }
  352. /**
  353. * Build a list of HTML attributes from an array.
  354. *
  355. * @param array $attributes
  356. * @return string
  357. */
  358. public static function attributes($attributes)
  359. {
  360. $html = array();
  361. foreach ((array) $attributes as $key => $value)
  362. {
  363. // For numeric keys, we will assume that the key and the value are the
  364. // same, as this will convert HTML attributes such as "required" that
  365. // may be specified as required="required", etc.
  366. if (is_numeric($key)) $key = $value;
  367. if ( ! is_null($value))
  368. {
  369. $html[] = $key.'="'.static::entities($value).'"';
  370. }
  371. }
  372. return (count($html) > 0) ? ' '.implode(' ', $html) : '';
  373. }
  374. /**
  375. * Obfuscate a string to prevent spam-bots from sniffing it.
  376. *
  377. * @param string $value
  378. * @return string
  379. */
  380. protected static function obfuscate($value)
  381. {
  382. $safe = '';
  383. foreach (str_split($value) as $letter)
  384. {
  385. // To properly obfuscate the value, we will randomly convert each
  386. // letter to its entity or hexadecimal representation, keeping a
  387. // bot from sniffing the randomly obfuscated letters.
  388. switch (rand(1, 3))
  389. {
  390. case 1:
  391. $safe .= '&#'.ord($letter).';';
  392. break;
  393. case 2:
  394. $safe .= '&#x'.dechex(ord($letter)).';';
  395. break;
  396. case 3:
  397. $safe .= $letter;
  398. }
  399. }
  400. return $safe;
  401. }
  402. /**
  403. * Get the appliction.encoding without needing to request it from Config::get() each time.
  404. *
  405. * @return string
  406. */
  407. protected static function encoding()
  408. {
  409. return static::$encoding ?: static::$encoding = Config::get('application.encoding');
  410. }
  411. /**
  412. * Dynamically handle calls to custom macros.
  413. *
  414. * @param string $method
  415. * @param array $parameters
  416. * @return mixed
  417. */
  418. public static function __callStatic($method, $parameters)
  419. {
  420. if (isset(static::$macros[$method]))
  421. {
  422. return call_user_func_array(static::$macros[$method], $parameters);
  423. }
  424. throw new \Exception("Method [$method] does not exist.");
  425. }
  426. }