html.php 9.1 KB

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