html.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 type="text/javascript" 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. foreach ($defaults as $attribute => $default)
  56. {
  57. if ( ! array_key_exists($attribute, $attributes)) $attributes[$attribute] = $default;
  58. }
  59. return '<link href="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>'.PHP_EOL;
  60. }
  61. /**
  62. * Generate a HTML span.
  63. *
  64. * @param string $value
  65. * @param array $attributes
  66. * @return string
  67. */
  68. public static function span($value, $attributes = array())
  69. {
  70. return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
  71. }
  72. /**
  73. * Generate a HTML link.
  74. *
  75. * <code>
  76. * // Generate a link to a location within the application
  77. * echo HTML::link('user/profile', 'User Profile');
  78. *
  79. * // Generate a link to a location outside of the application
  80. * echo HTML::link('http://google.com', 'Google');
  81. * </code>
  82. *
  83. * @param string $url
  84. * @param string $title
  85. * @param array $attributes
  86. * @param bool $https
  87. * @param bool $asset
  88. * @return string
  89. */
  90. public static function link($url, $title, $attributes = array(), $https = false, $asset = false)
  91. {
  92. $url = static::entities(URL::to($url, $https, $asset));
  93. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  94. }
  95. /**
  96. * Generate a HTTPS HTML link.
  97. *
  98. * @param string $url
  99. * @param string $title
  100. * @param array $attributes
  101. * @return string
  102. */
  103. public static function link_to_secure($url, $title, $attributes = array())
  104. {
  105. return static::link($url, $title, $attributes, true);
  106. }
  107. /**
  108. * Generate an HTML link to an asset.
  109. *
  110. * The application index page will not be added to asset links.
  111. *
  112. * @param string $url
  113. * @param string $title
  114. * @param array $attributes
  115. * @return string
  116. */
  117. public static function link_to_asset($url, $title, $attributes = array(), $https = false)
  118. {
  119. return static::link($url, $title, $attributes, $https, true);
  120. }
  121. /**
  122. * Generate an HTTPS HTML link to an asset.
  123. *
  124. * @param string $url
  125. * @param string $title
  126. * @param array $attributes
  127. * @return string
  128. */
  129. public static function link_to_secure_asset($url, $title, $attributes = array())
  130. {
  131. return static::link_to_asset($url, $title, $attributes, true);
  132. }
  133. /**
  134. * Generate an HTML link to a route.
  135. *
  136. * An array of parameters may be specified to fill in URI segment wildcards.
  137. *
  138. * <code>
  139. * // Generate a link to the "profile" named route
  140. * echo HTML::link_to_route('profile', 'Profile');
  141. *
  142. * // Generate a link to the "profile" route and add some parameters
  143. * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
  144. * </code>
  145. *
  146. * @param string $name
  147. * @param string $title
  148. * @param array $parameters
  149. * @param array $attributes
  150. * @return string
  151. */
  152. public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
  153. {
  154. return static::link(URL::to_route($name, $parameters, $https), $title, $attributes);
  155. }
  156. /**
  157. * Generate an HTTPS HTML link to a route.
  158. *
  159. * @param string $name
  160. * @param string $title
  161. * @param array $parameters
  162. * @param array $attributes
  163. * @return string
  164. */
  165. public static function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
  166. {
  167. return static::link_to_route($name, $title, $parameters, $attributes, true);
  168. }
  169. /**
  170. * Generate an HTML mailto link.
  171. *
  172. * The E-Mail address will be obfuscated to protect it from spam bots.
  173. *
  174. * @param string $email
  175. * @param string $title
  176. * @param array $attributes
  177. * @return string
  178. */
  179. public static function mailto($email, $title = null, $attributes = array())
  180. {
  181. $email = static::email($email);
  182. if (is_null($title)) $title = $email;
  183. $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
  184. return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  185. }
  186. /**
  187. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  188. *
  189. * @param string $email
  190. * @return string
  191. */
  192. public static function email($email)
  193. {
  194. return str_replace('@', '&#64;', static::obfuscate($email));
  195. }
  196. /**
  197. * Generate an HTML image element.
  198. *
  199. * @param string $url
  200. * @param string $alt
  201. * @param array $attributes
  202. * @return string
  203. */
  204. public static function image($url, $alt = '', $attributes = array())
  205. {
  206. $attributes['alt'] = $alt;
  207. return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
  208. }
  209. /**
  210. * Generate an ordered list of items.
  211. *
  212. * @param array $list
  213. * @param array $attributes
  214. * @return string
  215. */
  216. public static function ol($list, $attributes = array())
  217. {
  218. return static::list_elements('ol', $list, $attributes);
  219. }
  220. /**
  221. * Generate an un-ordered list of items.
  222. *
  223. * @param array $list
  224. * @param array $attributes
  225. * @return string
  226. */
  227. public static function ul($list, $attributes = array())
  228. {
  229. return static::list_elements('ul', $list, $attributes);
  230. }
  231. /**
  232. * Generate an ordered or un-ordered list.
  233. *
  234. * @param string $type
  235. * @param array $list
  236. * @param array $attributes
  237. * @return string
  238. */
  239. private static function list_elements($type, $list, $attributes = array())
  240. {
  241. $html = '';
  242. foreach ($list as $key => $value)
  243. {
  244. $html .= (is_array($value)) ? static::list_elements($type, $value) : '<li>'.static::entities($value).'</li>';
  245. }
  246. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  247. }
  248. /**
  249. * Build a list of HTML attributes from an array.
  250. *
  251. * @param array $attributes
  252. * @return string
  253. */
  254. public static function attributes($attributes)
  255. {
  256. $html = array();
  257. foreach ((array) $attributes as $key => $value)
  258. {
  259. // Assume numeric-keyed attributes to have the same key and value.
  260. // Example: required="required", autofocus="autofocus", etc.
  261. if (is_numeric($key)) $key = $value;
  262. if ( ! is_null($value))
  263. {
  264. $html[] = $key.'="'.static::entities($value).'"';
  265. }
  266. }
  267. return (count($html) > 0) ? ' '.implode(' ', $html) : '';
  268. }
  269. /**
  270. * Obfuscate a string to prevent spam-bots from sniffing it.
  271. *
  272. * @param string $value
  273. * @return string
  274. */
  275. protected static function obfuscate($value)
  276. {
  277. $safe = '';
  278. foreach (str_split($value) as $letter)
  279. {
  280. switch (rand(1, 3))
  281. {
  282. // Convert the letter to its entity representation.
  283. case 1:
  284. $safe .= '&#'.ord($letter).';';
  285. break;
  286. // Convert the letter to a Hex character code.
  287. case 2:
  288. $safe .= '&#x'.dechex(ord($letter)).';';
  289. break;
  290. // No encoding.
  291. case 3:
  292. $safe .= $letter;
  293. }
  294. }
  295. return $safe;
  296. }
  297. /**
  298. * Magic Method for handling dynamic static methods.
  299. *
  300. * This method primarily handles dynamic calls to create links to named routes.
  301. *
  302. * <code>
  303. * // Generate a link to the "profile" named route
  304. * echo HTML::link_to_profile('Profile');
  305. *
  306. * // Generate a link to the "profile" route and add some parameters
  307. * echo HTML::link_to_profile('Profile', array('taylor'));
  308. *
  309. * // Generate a link to the "profile" named route using HTTPS
  310. * echo HTML::link_to_secure_profile('Profile');
  311. * </code>
  312. */
  313. public static function __callStatic($method, $parameters)
  314. {
  315. if (strpos($method, 'link_to_secure_') === 0)
  316. {
  317. array_unshift($parameters, substr($method, 15));
  318. return forward_static_call_array('HTML::link_to_secure_route', $parameters);
  319. }
  320. if (strpos($method, 'link_to_') === 0)
  321. {
  322. array_unshift($parameters, substr($method, 8));
  323. return forward_static_call_array('HTML::link_to_route', $parameters);
  324. }
  325. throw new \Exception("Method [$method] is not defined on the HTML class.");
  326. }
  327. }