html.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php namespace Laravel;
  2. class HTML {
  3. /**
  4. * Convert HTML characters to entities.
  5. *
  6. * The encoding specified in the application configuration file will be used.
  7. *
  8. * @param string $value
  9. * @return string
  10. */
  11. public static function entities($value)
  12. {
  13. return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
  14. }
  15. /**
  16. * Generate a link to a JavaScript file.
  17. *
  18. * <code>
  19. * // Generate a link to a JavaScript file
  20. * echo HTML::script('js/jquery.js');
  21. *
  22. * // Generate a link to a JavaScript file and add some attributes
  23. * echo HTML::script('js/jquery.js', array('defer'));
  24. * </code>
  25. *
  26. * @param string $url
  27. * @param array $attributes
  28. * @return string
  29. */
  30. public static function script($url, $attributes = array())
  31. {
  32. $url = static::entities(URL::to_asset($url));
  33. return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
  34. }
  35. /**
  36. * Generate a link to a CSS file.
  37. *
  38. * If no media type is selected, "all" will be used.
  39. *
  40. * <code>
  41. * // Generate a link to a CSS file
  42. * echo HTML::style('css/common.css');
  43. *
  44. * // Generate a link to a CSS file and add some attributes
  45. * echo HTML::style('css/common.css', array('media' => 'print'));
  46. * </code>
  47. *
  48. * @param string $url
  49. * @param array $attributes
  50. * @return string
  51. */
  52. public static function style($url, $attributes = array())
  53. {
  54. $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
  55. $attributes = $attributes + $defaults;
  56. $url = static::entities(URL::to_asset($url));
  57. return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
  58. }
  59. /**
  60. * Generate a HTML span.
  61. *
  62. * @param string $value
  63. * @param array $attributes
  64. * @return string
  65. */
  66. public static function span($value, $attributes = array())
  67. {
  68. return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
  69. }
  70. /**
  71. * Generate a HTML link.
  72. *
  73. * <code>
  74. * // Generate a link to a location within the application
  75. * echo HTML::link('user/profile', 'User Profile');
  76. *
  77. * // Generate a link to a location outside of the application
  78. * echo HTML::link('http://google.com', 'Google');
  79. * </code>
  80. *
  81. * @param string $url
  82. * @param string $title
  83. * @param array $attributes
  84. * @param bool $https
  85. * @return string
  86. */
  87. public static function link($url, $title, $attributes = array(), $https = false)
  88. {
  89. $url = static::entities(URL::to($url, $https));
  90. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  91. }
  92. /**
  93. * Generate a HTTPS HTML link.
  94. *
  95. * @param string $url
  96. * @param string $title
  97. * @param array $attributes
  98. * @return string
  99. */
  100. public static function link_to_secure($url, $title, $attributes = array())
  101. {
  102. return static::link($url, $title, $attributes, true);
  103. }
  104. /**
  105. * Generate an HTML link to an asset.
  106. *
  107. * The application index page will not be added to asset links.
  108. *
  109. * @param string $url
  110. * @param string $title
  111. * @param array $attributes
  112. * @param bool $https
  113. * @return string
  114. */
  115. public static function link_to_asset($url, $title, $attributes = array(), $https = null)
  116. {
  117. $url = static::entities(URL::to_asset($url, $https));
  118. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  119. }
  120. /**
  121. * Generate an HTTPS HTML link to an asset.
  122. *
  123. * @param string $url
  124. * @param string $title
  125. * @param array $attributes
  126. * @return string
  127. */
  128. public static function link_to_secure_asset($url, $title, $attributes = array())
  129. {
  130. return static::link_to_asset($url, $title, $attributes, true);
  131. }
  132. /**
  133. * Generate an HTML link to a route.
  134. *
  135. * An array of parameters may be specified to fill in URI segment wildcards.
  136. *
  137. * <code>
  138. * // Generate a link to the "profile" named route
  139. * echo HTML::link_to_route('profile', 'Profile');
  140. *
  141. * // Generate a link to the "profile" route and add some parameters
  142. * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
  143. * </code>
  144. *
  145. * @param string $name
  146. * @param string $title
  147. * @param array $parameters
  148. * @param array $attributes
  149. * @return string
  150. */
  151. public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
  152. {
  153. return static::link(URL::to_route($name, $parameters, $https), $title, $attributes);
  154. }
  155. /**
  156. * Generate an HTTPS HTML link to a route.
  157. *
  158. * @param string $name
  159. * @param string $title
  160. * @param array $parameters
  161. * @param array $attributes
  162. * @return string
  163. */
  164. public static function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
  165. {
  166. return static::link_to_route($name, $title, $parameters, $attributes, true);
  167. }
  168. /**
  169. * Generate an HTML mailto link.
  170. *
  171. * The E-Mail address will be obfuscated to protect it from spam bots.
  172. *
  173. * @param string $email
  174. * @param string $title
  175. * @param array $attributes
  176. * @return string
  177. */
  178. public static function mailto($email, $title = null, $attributes = array())
  179. {
  180. $email = static::email($email);
  181. if (is_null($title)) $title = $email;
  182. $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
  183. return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  184. }
  185. /**
  186. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  187. *
  188. * @param string $email
  189. * @return string
  190. */
  191. public static function email($email)
  192. {
  193. return str_replace('@', '&#64;', static::obfuscate($email));
  194. }
  195. /**
  196. * Generate an HTML image element.
  197. *
  198. * @param string $url
  199. * @param string $alt
  200. * @param array $attributes
  201. * @return string
  202. */
  203. public static function image($url, $alt = '', $attributes = array())
  204. {
  205. $attributes['alt'] = $alt;
  206. return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
  207. }
  208. /**
  209. * Generate an ordered list of items.
  210. *
  211. * @param array $list
  212. * @param array $attributes
  213. * @return string
  214. */
  215. public static function ol($list, $attributes = array())
  216. {
  217. return static::listing('ol', $list, $attributes);
  218. }
  219. /**
  220. * Generate an un-ordered list of items.
  221. *
  222. * @param array $list
  223. * @param array $attributes
  224. * @return string
  225. */
  226. public static function ul($list, $attributes = array())
  227. {
  228. return static::listing('ul', $list, $attributes);
  229. }
  230. /**
  231. * Generate an ordered or un-ordered list.
  232. *
  233. * @param string $type
  234. * @param array $list
  235. * @param array $attributes
  236. * @return string
  237. */
  238. private static function listing($type, $list, $attributes = array())
  239. {
  240. $html = '';
  241. foreach ($list as $key => $value)
  242. {
  243. // If the value is an array, we will recurse the function so that we can
  244. // produce a nested list within the list being built. Of course, nested
  245. // lists may exist within nested lists, etc.
  246. if (is_array($value))
  247. {
  248. $html .= static::listing($type, $value);
  249. }
  250. else
  251. {
  252. $html .= '<li>'.static::entities($value).'</li>';
  253. }
  254. }
  255. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  256. }
  257. /**
  258. * Build a list of HTML attributes from an array.
  259. *
  260. * @param array $attributes
  261. * @return string
  262. */
  263. public static function attributes($attributes)
  264. {
  265. $html = array();
  266. foreach ((array) $attributes as $key => $value)
  267. {
  268. // For numeric keys, we will assume that the key and the value are the
  269. // same, as this will conver HTML attributes such as "required" that
  270. // may be specified as required="required".
  271. if (is_numeric($key)) $key = $value;
  272. if ( ! is_null($value))
  273. {
  274. $html[] = $key.'="'.static::entities($value).'"';
  275. }
  276. }
  277. return (count($html) > 0) ? ' '.implode(' ', $html) : '';
  278. }
  279. /**
  280. * Obfuscate a string to prevent spam-bots from sniffing it.
  281. *
  282. * @param string $value
  283. * @return string
  284. */
  285. protected static function obfuscate($value)
  286. {
  287. $safe = '';
  288. foreach (str_split($value) as $letter)
  289. {
  290. // To properly obfuscate the value, we will randomly convert each
  291. // letter to its entity or hexadecimal representation, keeping a
  292. // bot from sniffing the randomly obfuscated letters from the
  293. // page and guarding against e-mail harvesting.
  294. switch (rand(1, 3))
  295. {
  296. case 1:
  297. $safe .= '&#'.ord($letter).';';
  298. break;
  299. case 2:
  300. $safe .= '&#x'.dechex(ord($letter)).';';
  301. break;
  302. case 3:
  303. $safe .= $letter;
  304. }
  305. }
  306. return $safe;
  307. }
  308. }