html.php 9.2 KB

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