html.php 9.0 KB

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