PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/kohana/html.php

https://github.com/misa/core
PHP | 374 lines | 165 code | 38 blank | 171 comment | 13 complexity | d02e7e5f65adea73293a624a54ab33de MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * HTML helper class. Provides generic methods for generating various HTML
  4. * tags and making output HTML safe.
  5. *
  6. * @package Kohana
  7. * @category Helpers
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2010 Kohana Team
  10. * @license http://kohanaframework.org/license
  11. */
  12. class Kohana_HTML {
  13. /**
  14. * @var array preferred order of attributes
  15. */
  16. public static $attribute_order = array
  17. (
  18. 'action',
  19. 'method',
  20. 'type',
  21. 'id',
  22. 'name',
  23. 'value',
  24. 'href',
  25. 'src',
  26. 'width',
  27. 'height',
  28. 'cols',
  29. 'rows',
  30. 'size',
  31. 'maxlength',
  32. 'rel',
  33. 'media',
  34. 'accept-charset',
  35. 'accept',
  36. 'tabindex',
  37. 'accesskey',
  38. 'alt',
  39. 'title',
  40. 'class',
  41. 'style',
  42. 'selected',
  43. 'checked',
  44. 'readonly',
  45. 'disabled',
  46. );
  47. /**
  48. * @var boolean automatically target external URLs to a new window?
  49. */
  50. public static $windowed_urls = FALSE;
  51. /**
  52. * Convert special characters to HTML entities. All untrusted content
  53. * should be passed through this method to prevent XSS injections.
  54. *
  55. * echo HTML::chars($username);
  56. *
  57. * @param string string to convert
  58. * @param boolean encode existing entities
  59. * @return string
  60. */
  61. public static function chars($value, $double_encode = TRUE)
  62. {
  63. return htmlspecialchars( (string) $value, ENT_QUOTES, Kohana::$charset, $double_encode);
  64. }
  65. /**
  66. * Convert all applicable characters to HTML entities. All characters
  67. * that cannot be represented in HTML with the current character set
  68. * will be converted to entities.
  69. *
  70. * echo HTML::entities($username);
  71. *
  72. * @param string string to convert
  73. * @param boolean encode existing entities
  74. * @return string
  75. */
  76. public static function entities($value, $double_encode = TRUE)
  77. {
  78. return htmlentities( (string) $value, ENT_QUOTES, Kohana::$charset, $double_encode);
  79. }
  80. /**
  81. * Create HTML link anchors. Note that the title is not escaped, to allow
  82. * HTML elements within links (images, etc).
  83. *
  84. * echo HTML::anchor('/user/profile', 'My Profile');
  85. *
  86. * @param string URL or URI string
  87. * @param string link text
  88. * @param array HTML anchor attributes
  89. * @param string use a specific protocol
  90. * @return string
  91. * @uses URL::base
  92. * @uses URL::site
  93. * @uses HTML::attributes
  94. */
  95. public static function anchor($uri, $title = NULL, array $attributes = NULL, $protocol = NULL)
  96. {
  97. if ($title === NULL)
  98. {
  99. // Use the URI as the title
  100. $title = $uri;
  101. }
  102. if ($uri === '')
  103. {
  104. // Only use the base URL
  105. $uri = URL::base(FALSE, $protocol);
  106. }
  107. else
  108. {
  109. if (strpos($uri, '://') !== FALSE)
  110. {
  111. if (HTML::$windowed_urls === TRUE AND empty($attributes['target']))
  112. {
  113. // Make the link open in a new window
  114. $attributes['target'] = '_blank';
  115. }
  116. }
  117. elseif ($uri[0] !== '#')
  118. {
  119. // Make the URI absolute for non-id anchors
  120. $uri = URL::site($uri, $protocol);
  121. }
  122. }
  123. // Add the sanitized link to the attributes
  124. $attributes['href'] = $uri;
  125. return '<a'.HTML::attributes($attributes).'>'.$title.'</a>';
  126. }
  127. /**
  128. * Creates an HTML anchor to a file. Note that the title is not escaped,
  129. * to allow HTML elements within links (images, etc).
  130. *
  131. * echo HTML::file_anchor('media/doc/user_guide.pdf', 'User Guide');
  132. *
  133. * @param string name of file to link to
  134. * @param string link text
  135. * @param array HTML anchor attributes
  136. * @param string non-default protocol, eg: ftp
  137. * @return string
  138. * @uses URL::base
  139. * @uses HTML::attributes
  140. */
  141. public static function file_anchor($file, $title = NULL, array $attributes = NULL, $protocol = NULL)
  142. {
  143. if ($title === NULL)
  144. {
  145. // Use the file name as the title
  146. $title = basename($file);
  147. }
  148. // Add the file link to the attributes
  149. $attributes['href'] = URL::base(FALSE, $protocol).$file;
  150. return '<a'.HTML::attributes($attributes).'>'.$title.'</a>';
  151. }
  152. /**
  153. * Generates an obfuscated version of a string. Text passed through this
  154. * method is less likely to be read by web crawlers and robots, which can
  155. * be helpful for spam prevention, but can prevent legitimate robots from
  156. * reading your content.
  157. *
  158. * echo HTML::obfuscate($text);
  159. *
  160. * @param string string to obfuscate
  161. * @return string
  162. * @since 3.0.3
  163. */
  164. public static function obfuscate($string)
  165. {
  166. $safe = '';
  167. foreach (str_split($string) as $letter)
  168. {
  169. switch (rand(1, 3))
  170. {
  171. // HTML entity code
  172. case 1:
  173. $safe .= '&#'.ord($letter).';';
  174. break;
  175. // Hex character code
  176. case 2:
  177. $safe .= '&#x'.dechex(ord($letter)).';';
  178. break;
  179. // Raw (no) encoding
  180. case 3:
  181. $safe .= $letter;
  182. }
  183. }
  184. return $safe;
  185. }
  186. /**
  187. * Generates an obfuscated version of an email address. Helps prevent spam
  188. * robots from finding email addresses.
  189. *
  190. * echo HTML::email($address);
  191. *
  192. * @param string email address
  193. * @return string
  194. * @uses HTML::obfuscate
  195. */
  196. public static function email($email)
  197. {
  198. // Make sure the at sign is always obfuscated
  199. return str_replace('@', '&#64;', HTML::obfuscate($email));
  200. }
  201. /**
  202. * Creates an email (mailto:) anchor. Note that the title is not escaped,
  203. * to allow HTML elements within links (images, etc).
  204. *
  205. * echo HTML::mailto($address);
  206. *
  207. * @param string email address to send to
  208. * @param string link text
  209. * @param array HTML anchor attributes
  210. * @return string
  211. * @uses HTML::email
  212. * @uses HTML::attributes
  213. */
  214. public static function mailto($email, $title = NULL, array $attributes = NULL)
  215. {
  216. // Obfuscate email address
  217. $email = HTML::email($email);
  218. if ($title === NULL)
  219. {
  220. // Use the email address as the title
  221. $title = $email;
  222. }
  223. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.HTML::attributes($attributes).'>'.$title.'</a>';
  224. }
  225. /**
  226. * Creates a style sheet link element.
  227. *
  228. * echo HTML::style('media/css/screen.css');
  229. *
  230. * @param string file name
  231. * @param array default attributes
  232. * @param boolean include the index page
  233. * @return string
  234. * @uses URL::base
  235. * @uses HTML::attributes
  236. */
  237. public static function style($file, array $attributes = NULL, $index = FALSE)
  238. {
  239. if (strpos($file, '://') === FALSE)
  240. {
  241. // Add the base URL
  242. $file = URL::base($index).$file;
  243. }
  244. // Set the stylesheet link
  245. $attributes['href'] = $file;
  246. // Set the stylesheet rel
  247. $attributes['rel'] = 'stylesheet';
  248. // Set the stylesheet type
  249. $attributes['type'] = 'text/css';
  250. return '<link'.HTML::attributes($attributes).' />';
  251. }
  252. /**
  253. * Creates a script link.
  254. *
  255. * echo HTML::script('media/js/jquery.min.js');
  256. *
  257. * @param string file name
  258. * @param array default attributes
  259. * @param boolean include the index page
  260. * @return string
  261. * @uses URL::base
  262. * @uses HTML::attributes
  263. */
  264. public static function script($file, array $attributes = NULL, $index = FALSE)
  265. {
  266. if (strpos($file, '://') === FALSE)
  267. {
  268. // Add the base URL
  269. $file = URL::base($index).$file;
  270. }
  271. // Set the script link
  272. $attributes['src'] = $file;
  273. // Set the script type
  274. $attributes['type'] = 'text/javascript';
  275. return '<script'.HTML::attributes($attributes).'></script>';
  276. }
  277. /**
  278. * Creates a image link.
  279. *
  280. * echo HTML::image('media/img/logo.png', array('alt' => 'My Company'));
  281. *
  282. * @param string file name
  283. * @param array default attributes
  284. * @return string
  285. * @uses URL::base
  286. * @uses HTML::attributes
  287. */
  288. public static function image($file, array $attributes = NULL, $index = FALSE)
  289. {
  290. if (strpos($file, '://') === FALSE)
  291. {
  292. // Add the base URL
  293. $file = URL::base($index).$file;
  294. }
  295. // Add the image link
  296. $attributes['src'] = $file;
  297. return '<img'.HTML::attributes($attributes).' />';
  298. }
  299. /**
  300. * Compiles an array of HTML attributes into an attribute string.
  301. * Attributes will be sorted using HTML::$attribute_order for consistency.
  302. *
  303. * echo '<div'.HTML::attributes($attrs).'>'.$content.'</div>';
  304. *
  305. * @param array attribute list
  306. * @return string
  307. */
  308. public static function attributes(array $attributes = NULL)
  309. {
  310. if (empty($attributes))
  311. return '';
  312. $sorted = array();
  313. foreach (HTML::$attribute_order as $key)
  314. {
  315. if (isset($attributes[$key]))
  316. {
  317. // Add the attribute to the sorted list
  318. $sorted[$key] = $attributes[$key];
  319. }
  320. }
  321. // Combine the sorted attributes
  322. $attributes = $sorted + $attributes;
  323. $compiled = '';
  324. foreach ($attributes as $key => $value)
  325. {
  326. if ($value === NULL)
  327. {
  328. // Skip attributes that have NULL values
  329. continue;
  330. }
  331. // Add the attribute value
  332. $compiled .= ' '.$key.'="'.HTML::chars($value).'"';
  333. }
  334. return $compiled;
  335. }
  336. } // End html