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

/system/classes/kohana/html.php

https://github.com/jongwook/ksa14-kohana
PHP | 328 lines | 145 code | 33 blank | 150 comment | 13 complexity | dc57086bf637e5f917163ca7376c88a9 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  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-2011 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 mixed protocol to pass to URL::base()
  90. * @param boolean include the index page
  91. * @return string
  92. * @uses URL::base
  93. * @uses URL::site
  94. * @uses HTML::attributes
  95. */
  96. public static function anchor($uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = TRUE)
  97. {
  98. if ($title === NULL)
  99. {
  100. // Use the URI as the title
  101. $title = $uri;
  102. }
  103. if ($uri === '')
  104. {
  105. // Only use the base URL
  106. $uri = URL::base($protocol, $index);
  107. }
  108. else
  109. {
  110. if (strpos($uri, '://') !== FALSE)
  111. {
  112. if (HTML::$windowed_urls === TRUE AND empty($attributes['target']))
  113. {
  114. // Make the link open in a new window
  115. $attributes['target'] = '_blank';
  116. }
  117. }
  118. elseif ($uri[0] !== '#')
  119. {
  120. // Make the URI absolute for non-id anchors
  121. $uri = URL::site($uri, $protocol, $index);
  122. }
  123. }
  124. // Add the sanitized link to the attributes
  125. $attributes['href'] = $uri;
  126. return '<a'.HTML::attributes($attributes).'>'.$title.'</a>';
  127. }
  128. /**
  129. * Creates an HTML anchor to a file. Note that the title is not escaped,
  130. * to allow HTML elements within links (images, etc).
  131. *
  132. * echo HTML::file_anchor('media/doc/user_guide.pdf', 'User Guide');
  133. *
  134. * @param string name of file to link to
  135. * @param string link text
  136. * @param array HTML anchor attributes
  137. * @param mixed protocol to pass to URL::base()
  138. * @param boolean include the index page
  139. * @return string
  140. * @uses URL::base
  141. * @uses HTML::attributes
  142. */
  143. public static function file_anchor($file, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  144. {
  145. if ($title === NULL)
  146. {
  147. // Use the file name as the title
  148. $title = basename($file);
  149. }
  150. // Add the file link to the attributes
  151. $attributes['href'] = URL::base($protocol, $index).$file;
  152. return '<a'.HTML::attributes($attributes).'>'.$title.'</a>';
  153. }
  154. /**
  155. * Creates an email (mailto:) anchor. Note that the title is not escaped,
  156. * to allow HTML elements within links (images, etc).
  157. *
  158. * echo HTML::mailto($address);
  159. *
  160. * @param string email address to send to
  161. * @param string link text
  162. * @param array HTML anchor attributes
  163. * @return string
  164. * @uses HTML::attributes
  165. */
  166. public static function mailto($email, $title = NULL, array $attributes = NULL)
  167. {
  168. if ($title === NULL)
  169. {
  170. // Use the email address as the title
  171. $title = $email;
  172. }
  173. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.HTML::attributes($attributes).'>'.$title.'</a>';
  174. }
  175. /**
  176. * Creates a style sheet link element.
  177. *
  178. * echo HTML::style('media/css/screen.css');
  179. *
  180. * @param string file name
  181. * @param array default attributes
  182. * @param mixed protocol to pass to URL::base()
  183. * @param boolean include the index page
  184. * @return string
  185. * @uses URL::base
  186. * @uses HTML::attributes
  187. */
  188. public static function style($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  189. {
  190. if (strpos($file, '://') === FALSE)
  191. {
  192. // Add the base URL
  193. $file = URL::base($protocol, $index).$file;
  194. }
  195. // Set the stylesheet link
  196. $attributes['href'] = $file;
  197. // Set the stylesheet rel
  198. $attributes['rel'] = 'stylesheet';
  199. // Set the stylesheet type
  200. $attributes['type'] = 'text/css';
  201. return '<link'.HTML::attributes($attributes).' />';
  202. }
  203. /**
  204. * Creates a script link.
  205. *
  206. * echo HTML::script('media/js/jquery.min.js');
  207. *
  208. * @param string file name
  209. * @param array default attributes
  210. * @param mixed protocol to pass to URL::base()
  211. * @param boolean include the index page
  212. * @return string
  213. * @uses URL::base
  214. * @uses HTML::attributes
  215. */
  216. public static function script($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  217. {
  218. if (strpos($file, '://') === FALSE)
  219. {
  220. // Add the base URL
  221. $file = URL::base($protocol, $index).$file;
  222. }
  223. // Set the script link
  224. $attributes['src'] = $file;
  225. // Set the script type
  226. $attributes['type'] = 'text/javascript';
  227. return '<script'.HTML::attributes($attributes).'></script>';
  228. }
  229. /**
  230. * Creates a image link.
  231. *
  232. * echo HTML::image('media/img/logo.png', array('alt' => 'My Company'));
  233. *
  234. * @param string file name
  235. * @param array default attributes
  236. * @param mixed protocol to pass to URL::base()
  237. * @param boolean include the index page
  238. * @return string
  239. * @uses URL::base
  240. * @uses HTML::attributes
  241. */
  242. public static function image($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  243. {
  244. if (strpos($file, '://') === FALSE)
  245. {
  246. // Add the base URL
  247. $file = URL::base($protocol, $index).$file;
  248. }
  249. // Add the image link
  250. $attributes['src'] = $file;
  251. return '<img'.HTML::attributes($attributes).' />';
  252. }
  253. /**
  254. * Compiles an array of HTML attributes into an attribute string.
  255. * Attributes will be sorted using HTML::$attribute_order for consistency.
  256. *
  257. * echo '<div'.HTML::attributes($attrs).'>'.$content.'</div>';
  258. *
  259. * @param array attribute list
  260. * @return string
  261. */
  262. public static function attributes(array $attributes = NULL)
  263. {
  264. if (empty($attributes))
  265. return '';
  266. $sorted = array();
  267. foreach (HTML::$attribute_order as $key)
  268. {
  269. if (isset($attributes[$key]))
  270. {
  271. // Add the attribute to the sorted list
  272. $sorted[$key] = $attributes[$key];
  273. }
  274. }
  275. // Combine the sorted attributes
  276. $attributes = $sorted + $attributes;
  277. $compiled = '';
  278. foreach ($attributes as $key => $value)
  279. {
  280. if ($value === NULL)
  281. {
  282. // Skip attributes that have NULL values
  283. continue;
  284. }
  285. if (is_int($key))
  286. {
  287. // Assume non-associative keys are mirrored attributes
  288. $key = $value;
  289. }
  290. // Add the attribute value
  291. $compiled .= ' '.$key.'="'.HTML::chars($value).'"';
  292. }
  293. return $compiled;
  294. }
  295. } // End html