html.php 9.1 KB

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