html.php 11 KB

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