html.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 $input
  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, $attributes = array(), $https = null)
  127. {
  128. $url = URL::to($url, $https);
  129. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  130. }
  131. /**
  132. * Generate a HTTPS HTML link.
  133. *
  134. * @param string $url
  135. * @param string $title
  136. * @param array $attributes
  137. * @return string
  138. */
  139. public static function link_to_secure($url, $title, $attributes = array())
  140. {
  141. return static::link($url, $title, $attributes, true);
  142. }
  143. /**
  144. * Generate an HTML link to an asset.
  145. *
  146. * The application index page will not be added to asset links.
  147. *
  148. * @param string $url
  149. * @param string $title
  150. * @param array $attributes
  151. * @param bool $https
  152. * @return string
  153. */
  154. public static function link_to_asset($url, $title, $attributes = array(), $https = null)
  155. {
  156. $url = URL::to_asset($url, $https);
  157. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  158. }
  159. /**
  160. * Generate an HTTPS HTML link to an asset.
  161. *
  162. * @param string $url
  163. * @param string $title
  164. * @param array $attributes
  165. * @return string
  166. */
  167. public static function link_to_secure_asset($url, $title, $attributes = array())
  168. {
  169. return static::link_to_asset($url, $title, $attributes, true);
  170. }
  171. /**
  172. * Generate an HTML link to a route.
  173. *
  174. * An array of parameters may be specified to fill in URI segment wildcards.
  175. *
  176. * <code>
  177. * // Generate a link to the "profile" named route
  178. * echo HTML::link_to_route('profile', 'Profile');
  179. *
  180. * // Generate a link to the "profile" route and add some parameters
  181. * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
  182. * </code>
  183. *
  184. * @param string $name
  185. * @param string $title
  186. * @param array $parameters
  187. * @param array $attributes
  188. * @return string
  189. */
  190. public static function link_to_route($name, $title, $parameters = array(), $attributes = array())
  191. {
  192. return static::link(URL::to_route($name, $parameters), $title, $attributes);
  193. }
  194. /**
  195. * Generate an HTML link to a controller action.
  196. *
  197. * An array of parameters may be specified to fill in URI segment wildcards.
  198. *
  199. * <code>
  200. * // Generate a link to the "home@index" action
  201. * echo HTML::link_to_action('home@index', 'Home');
  202. *
  203. * // Generate a link to the "user@profile" route and add some parameters
  204. * echo HTML::link_to_action('user@profile', 'Profile', array('taylor'));
  205. * </code>
  206. *
  207. * @param string $action
  208. * @param string $title
  209. * @param array $parameters
  210. * @param array $attributes
  211. * @return string
  212. */
  213. public static function link_to_action($action, $title, $parameters = array(), $attributes = array())
  214. {
  215. return static::link(URL::to_action($action, $parameters), $title, $attributes);
  216. }
  217. /**
  218. * Generate an HTML mailto link.
  219. *
  220. * The E-Mail address will be obfuscated to protect it from spam bots.
  221. *
  222. * @param string $email
  223. * @param string $title
  224. * @param array $attributes
  225. * @return string
  226. */
  227. public static function mailto($email, $title = null, $attributes = array())
  228. {
  229. $email = static::email($email);
  230. if (is_null($title)) $title = $email;
  231. $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
  232. return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  233. }
  234. /**
  235. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  236. *
  237. * @param string $email
  238. * @return string
  239. */
  240. public static function email($email)
  241. {
  242. return str_replace('@', '&#64;', static::obfuscate($email));
  243. }
  244. /**
  245. * Generate an HTML image element.
  246. *
  247. * @param string $url
  248. * @param string $alt
  249. * @param array $attributes
  250. * @return string
  251. */
  252. public static function image($url, $alt = '', $attributes = array())
  253. {
  254. $attributes['alt'] = $alt;
  255. return '<img src="'.URL::to_asset($url).'"'.static::attributes($attributes).'>';
  256. }
  257. /**
  258. * Generate an ordered list of items.
  259. *
  260. * @param array $list
  261. * @param array $attributes
  262. * @return string
  263. */
  264. public static function ol($list, $attributes = array())
  265. {
  266. return static::listing('ol', $list, $attributes);
  267. }
  268. /**
  269. * Generate an un-ordered list of items.
  270. *
  271. * @param array $list
  272. * @param array $attributes
  273. * @return string
  274. */
  275. public static function ul($list, $attributes = array())
  276. {
  277. return static::listing('ul', $list, $attributes);
  278. }
  279. /**
  280. * Generate an ordered or un-ordered list.
  281. *
  282. * @param string $type
  283. * @param array $list
  284. * @param array $attributes
  285. * @return string
  286. */
  287. private static function listing($type, $list, $attributes = array())
  288. {
  289. $html = '';
  290. if (count($list) == 0) return $html;
  291. foreach ($list as $key => $value)
  292. {
  293. // If the value is an array, we will recurse the function so that we can
  294. // produce a nested list within the list being built. Of course, nested
  295. // lists may exist within nested lists, etc.
  296. if (is_array($value))
  297. {
  298. if (is_int($key))
  299. {
  300. $html .= static::listing($type, $value);
  301. }
  302. else
  303. {
  304. $html .= '<li>'.$key.static::listing($type, $value).'</li>';
  305. }
  306. }
  307. else
  308. {
  309. $html .= '<li>'.static::entities($value).'</li>';
  310. }
  311. }
  312. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  313. }
  314. /**
  315. * Build a list of HTML attributes from an array.
  316. *
  317. * @param array $attributes
  318. * @return string
  319. */
  320. public static function attributes($attributes)
  321. {
  322. $html = array();
  323. foreach ((array) $attributes as $key => $value)
  324. {
  325. // For numeric keys, we will assume that the key and the value are the
  326. // same, as this will conver HTML attributes such as "required" that
  327. // may be specified as required="required", etc.
  328. if (is_numeric($key)) $key = $value;
  329. if ( ! is_null($value))
  330. {
  331. $html[] = $key.'="'.static::entities($value).'"';
  332. }
  333. }
  334. return (count($html) > 0) ? ' '.implode(' ', $html) : '';
  335. }
  336. /**
  337. * Obfuscate a string to prevent spam-bots from sniffing it.
  338. *
  339. * @param string $value
  340. * @return string
  341. */
  342. protected static function obfuscate($value)
  343. {
  344. $safe = '';
  345. foreach (str_split($value) as $letter)
  346. {
  347. // To properly obfuscate the value, we will randomly convert each
  348. // letter to its entity or hexadecimal representation, keeping a
  349. // bot from sniffing the randomly obfuscated letters.
  350. switch (rand(1, 3))
  351. {
  352. case 1:
  353. $safe .= '&#'.ord($letter).';';
  354. break;
  355. case 2:
  356. $safe .= '&#x'.dechex(ord($letter)).';';
  357. break;
  358. case 3:
  359. $safe .= $letter;
  360. }
  361. }
  362. return $safe;
  363. }
  364. /**
  365. * Dynamically handle calls to custom macros.
  366. *
  367. * @param string $method
  368. * @param array $parameters
  369. * @return mixed
  370. */
  371. public static function __callStatic($method, $parameters)
  372. {
  373. if (isset(static::$macros[$method]))
  374. {
  375. return call_user_func_array(static::$macros[$method], $parameters);
  376. }
  377. throw new \Exception("Method [$method] does not exist.");
  378. }
  379. }