html.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php namespace System;
  2. class HTML {
  3. /**
  4. * Generate a JavaScript reference.
  5. *
  6. * @param string $url
  7. * @return string
  8. */
  9. public static function script($url)
  10. {
  11. return '<script type="text/javascript" src="'.trim(URL::to($url), '.js').'.js"></script>'.PHP_EOL;
  12. }
  13. /**
  14. * Generate a CSS reference.
  15. *
  16. * @param string $url
  17. * @return string
  18. */
  19. public static function style($url, $media = 'all')
  20. {
  21. return '<link href="'.trim(URL::to($url), '.css').'.css" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL;
  22. }
  23. /**
  24. * Generate a HTML link.
  25. *
  26. * @param string $url
  27. * @param string $title
  28. * @param array $attributes
  29. * @param bool $https
  30. * @return string
  31. */
  32. public static function link($url, $title, $attributes = array(), $https = false)
  33. {
  34. return '<a href="'.URL::to($url, $https).'"'.static::attributes($attributes).'>'.Str::entities($title).'</a>';
  35. }
  36. /**
  37. * Generate a HTTPS HTML link.
  38. *
  39. * @param string $url
  40. * @param string $title
  41. * @param array $attributes
  42. * @return string
  43. */
  44. public static function secure_link($url, $title, $attributes)
  45. {
  46. return static::link($url, $title, $attributes, true);
  47. }
  48. /**
  49. * Generate an HTML mailto link.
  50. *
  51. * @param string $email
  52. * @param string $title
  53. * @param array $attributes
  54. * @return string
  55. */
  56. public static function mailto($email, $title = null, $attributes = array())
  57. {
  58. // -------------------------------------------------------
  59. // Obfuscate the e-mail address.
  60. // -------------------------------------------------------
  61. $email = static::email($email);
  62. if (is_null($title))
  63. {
  64. $title = $email;
  65. }
  66. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.static::attributes($attributes).'>'.$title.'</a>';
  67. }
  68. /**
  69. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  70. *
  71. * @param string $email
  72. * @return string
  73. */
  74. public static function email($email)
  75. {
  76. return str_replace('@', '&#64;', static::obfuscate($email));
  77. }
  78. /**
  79. * Generate an HTML image.
  80. *
  81. * @param string $url
  82. * @param string $alt
  83. * @param array $attributes
  84. * @return string
  85. */
  86. public static function image($url, $alt = '', $attributes = array())
  87. {
  88. $attributes['alt'] = Str::entities($alt);
  89. return '<img src="'.URL::to($url).'"'.static::attributes($attributes).' />';
  90. }
  91. /**
  92. * Generate HTML breaks.
  93. *
  94. * @param int $count
  95. * @return string
  96. */
  97. public static function breaks($count = 1)
  98. {
  99. return str_repeat('<br />', $count);
  100. }
  101. /**
  102. * Generate non-breaking spaces.
  103. *
  104. * @param int $count
  105. * @return string
  106. */
  107. public static function spaces($count = 1)
  108. {
  109. return str_repeat('&nbsp;', $count);
  110. }
  111. /**
  112. * Generate an ordered list.
  113. *
  114. * @param array $list
  115. * @param array $attributes
  116. * @return string
  117. */
  118. public static function ol($list, $attributes = array())
  119. {
  120. return static::list_elements('ol', $list, $attributes);
  121. }
  122. /**
  123. * Generate an un-ordered list.
  124. *
  125. * @param array $list
  126. * @param array $attributes
  127. * @return string
  128. */
  129. public static function ul($list, $attributes = array())
  130. {
  131. return static::list_elements('ul', $list, $attributes);
  132. }
  133. /**
  134. * Generate an ordered or un-ordered list.
  135. *
  136. * @param string $type
  137. * @param array $list
  138. * @param array $attributes
  139. * @return string
  140. */
  141. private static function list_elements($type, $list, $attributes)
  142. {
  143. if ( ! is_array($list))
  144. {
  145. return '';
  146. }
  147. $html = '';
  148. foreach ($list as $key => $value)
  149. {
  150. $html .= '<li>'.Str::entities($value).'</li>';
  151. }
  152. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  153. }
  154. /**
  155. * Build a list of HTML attributes.
  156. *
  157. * @param array $attributes
  158. * @return string
  159. */
  160. public static function attributes($attributes)
  161. {
  162. $html = array();
  163. foreach ($attributes as $key => $value)
  164. {
  165. if ( ! is_null($value))
  166. {
  167. $html[] = $key.'="'.Str::entities($value).'"';
  168. }
  169. }
  170. if (count($html) > 0)
  171. {
  172. return ' '.implode(' ', $html);
  173. }
  174. else
  175. {
  176. return '';
  177. }
  178. }
  179. /**
  180. * Obfuscate a string to prevent spam-bots from sniffing it.
  181. *
  182. * @param string $value
  183. * @return string
  184. */
  185. public static function obfuscate($value)
  186. {
  187. $safe = '';
  188. // -------------------------------------------------------
  189. // Spin through the string letter by letter.
  190. // -------------------------------------------------------
  191. foreach (str_split($value) as $letter)
  192. {
  193. switch (rand(1, 3))
  194. {
  195. // -------------------------------------------------------
  196. // Convert the letter to its entity representation.
  197. // -------------------------------------------------------
  198. case 1:
  199. $safe .= '&#'.ord($letter).';';
  200. break;
  201. // -------------------------------------------------------
  202. // Convert the letter to a Hex character code.
  203. // -------------------------------------------------------
  204. case 2:
  205. $safe .= '&#x'.dechex(ord($letter)).';';
  206. break;
  207. // -------------------------------------------------------
  208. // No encoding.
  209. // -------------------------------------------------------
  210. case 3:
  211. $safe .= $letter;
  212. }
  213. }
  214. return $safe;
  215. }
  216. }