PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/laravel/html.php

http://github.com/ericbarnes/Status-Board
PHP | 344 lines | 121 code | 39 blank | 184 comment | 5 complexity | 82ded1a16280e374b333c92911462e36 MD5 | raw file
Possible License(s): MIT
  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. * Convert entities to HTML characters.
  17. *
  18. * @param string $value
  19. * @return string
  20. */
  21. public static function decode($value)
  22. {
  23. return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding'));
  24. }
  25. /**
  26. * Generate a link to a JavaScript file.
  27. *
  28. * <code>
  29. * // Generate a link to a JavaScript file
  30. * echo HTML::script('js/jquery.js');
  31. *
  32. * // Generate a link to a JavaScript file and add some attributes
  33. * echo HTML::script('js/jquery.js', array('defer'));
  34. * </code>
  35. *
  36. * @param string $url
  37. * @param array $attributes
  38. * @return string
  39. */
  40. public static function script($url, $attributes = array())
  41. {
  42. $url = static::entities(URL::to_asset($url));
  43. return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
  44. }
  45. /**
  46. * Generate a link to a CSS file.
  47. *
  48. * If no media type is selected, "all" will be used.
  49. *
  50. * <code>
  51. * // Generate a link to a CSS file
  52. * echo HTML::style('css/common.css');
  53. *
  54. * // Generate a link to a CSS file and add some attributes
  55. * echo HTML::style('css/common.css', array('media' => 'print'));
  56. * </code>
  57. *
  58. * @param string $url
  59. * @param array $attributes
  60. * @return string
  61. */
  62. public static function style($url, $attributes = array())
  63. {
  64. $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
  65. $attributes = $attributes + $defaults;
  66. $url = static::entities(URL::to_asset($url));
  67. return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
  68. }
  69. /**
  70. * Generate a HTML span.
  71. *
  72. * @param string $value
  73. * @param array $attributes
  74. * @return string
  75. */
  76. public static function span($value, $attributes = array())
  77. {
  78. return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
  79. }
  80. /**
  81. * Generate a HTML link.
  82. *
  83. * <code>
  84. * // Generate a link to a location within the application
  85. * echo HTML::link('user/profile', 'User Profile');
  86. *
  87. * // Generate a link to a location outside of the application
  88. * echo HTML::link('http://google.com', 'Google');
  89. * </code>
  90. *
  91. * @param string $url
  92. * @param string $title
  93. * @param array $attributes
  94. * @param bool $https
  95. * @return string
  96. */
  97. public static function link($url, $title, $attributes = array(), $https = false)
  98. {
  99. $url = static::entities(URL::to($url, $https));
  100. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  101. }
  102. /**
  103. * Generate a HTTPS HTML link.
  104. *
  105. * @param string $url
  106. * @param string $title
  107. * @param array $attributes
  108. * @return string
  109. */
  110. public static function link_to_secure($url, $title, $attributes = array())
  111. {
  112. return static::link($url, $title, $attributes, true);
  113. }
  114. /**
  115. * Generate an HTML link to an asset.
  116. *
  117. * The application index page will not be added to asset links.
  118. *
  119. * @param string $url
  120. * @param string $title
  121. * @param array $attributes
  122. * @param bool $https
  123. * @return string
  124. */
  125. public static function link_to_asset($url, $title, $attributes = array(), $https = null)
  126. {
  127. $url = static::entities(URL::to_asset($url, $https));
  128. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  129. }
  130. /**
  131. * Generate an HTTPS HTML link to an asset.
  132. *
  133. * @param string $url
  134. * @param string $title
  135. * @param array $attributes
  136. * @return string
  137. */
  138. public static function link_to_secure_asset($url, $title, $attributes = array())
  139. {
  140. return static::link_to_asset($url, $title, $attributes, true);
  141. }
  142. /**
  143. * Generate an HTML link to a route.
  144. *
  145. * An array of parameters may be specified to fill in URI segment wildcards.
  146. *
  147. * <code>
  148. * // Generate a link to the "profile" named route
  149. * echo HTML::link_to_route('profile', 'Profile');
  150. *
  151. * // Generate a link to the "profile" route and add some parameters
  152. * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
  153. * </code>
  154. *
  155. * @param string $name
  156. * @param string $title
  157. * @param array $parameters
  158. * @param array $attributes
  159. * @return string
  160. */
  161. public static function link_to_route($name, $title, $parameters = array(), $attributes = array())
  162. {
  163. return static::link(URL::to_route($name, $parameters), $title, $attributes);
  164. }
  165. /**
  166. * Generate an HTML mailto link.
  167. *
  168. * The E-Mail address will be obfuscated to protect it from spam bots.
  169. *
  170. * @param string $email
  171. * @param string $title
  172. * @param array $attributes
  173. * @return string
  174. */
  175. public static function mailto($email, $title = null, $attributes = array())
  176. {
  177. $email = static::email($email);
  178. if (is_null($title)) $title = $email;
  179. $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
  180. return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  181. }
  182. /**
  183. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  184. *
  185. * @param string $email
  186. * @return string
  187. */
  188. public static function email($email)
  189. {
  190. return str_replace('@', '&#64;', static::obfuscate($email));
  191. }
  192. /**
  193. * Generate an HTML image element.
  194. *
  195. * @param string $url
  196. * @param string $alt
  197. * @param array $attributes
  198. * @return string
  199. */
  200. public static function image($url, $alt = '', $attributes = array())
  201. {
  202. $attributes['alt'] = $alt;
  203. return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
  204. }
  205. /**
  206. * Generate an ordered list of items.
  207. *
  208. * @param array $list
  209. * @param array $attributes
  210. * @return string
  211. */
  212. public static function ol($list, $attributes = array())
  213. {
  214. return static::listing('ol', $list, $attributes);
  215. }
  216. /**
  217. * Generate an un-ordered list of items.
  218. *
  219. * @param array $list
  220. * @param array $attributes
  221. * @return string
  222. */
  223. public static function ul($list, $attributes = array())
  224. {
  225. return static::listing('ul', $list, $attributes);
  226. }
  227. /**
  228. * Generate an ordered or un-ordered list.
  229. *
  230. * @param string $type
  231. * @param array $list
  232. * @param array $attributes
  233. * @return string
  234. */
  235. private static function listing($type, $list, $attributes = array())
  236. {
  237. $html = '';
  238. foreach ($list as $key => $value)
  239. {
  240. // If the value is an array, we will recurse the function so that we can
  241. // produce a nested list within the list being built. Of course, nested
  242. // lists may exist within nested lists, etc.
  243. if (is_array($value))
  244. {
  245. $html .= static::listing($type, $value);
  246. }
  247. else
  248. {
  249. $html .= '<li>'.static::entities($value).'</li>';
  250. }
  251. }
  252. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  253. }
  254. /**
  255. * Build a list of HTML attributes from an array.
  256. *
  257. * @param array $attributes
  258. * @return string
  259. */
  260. public static function attributes($attributes)
  261. {
  262. $html = array();
  263. foreach ((array) $attributes as $key => $value)
  264. {
  265. // For numeric keys, we will assume that the key and the value are the
  266. // same, as this will conver HTML attributes such as "required" that
  267. // may be specified as required="required".
  268. if (is_numeric($key)) $key = $value;
  269. if ( ! is_null($value))
  270. {
  271. $html[] = $key.'="'.static::entities($value).'"';
  272. }
  273. }
  274. return (count($html) > 0) ? ' '.implode(' ', $html) : '';
  275. }
  276. /**
  277. * Obfuscate a string to prevent spam-bots from sniffing it.
  278. *
  279. * @param string $value
  280. * @return string
  281. */
  282. protected static function obfuscate($value)
  283. {
  284. $safe = '';
  285. foreach (str_split($value) as $letter)
  286. {
  287. // To properly obfuscate the value, we will randomly convert each
  288. // letter to its entity or hexadecimal representation, keeping a
  289. // bot from sniffing the randomly obfuscated letters from the
  290. // page and guarding against e-mail harvesting.
  291. switch (rand(1, 3))
  292. {
  293. case 1:
  294. $safe .= '&#'.ord($letter).';';
  295. break;
  296. case 2:
  297. $safe .= '&#x'.dechex(ord($letter)).';';
  298. break;
  299. case 3:
  300. $safe .= $letter;
  301. }
  302. }
  303. return $safe;
  304. }
  305. }