PageRenderTime 159ms CodeModel.GetById 38ms RepoModel.GetById 8ms app.codeStats 0ms

/system/classes/kohana/html.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 377 lines | 167 code | 38 blank | 172 comment | 13 complexity | d540ce06ce18fd2cf663a5b3e9a2f9cd 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://kohanaphp.com/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: $safe .= '&#'.ord($letter).';'; break;
  173. // Hex character code
  174. case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
  175. // Raw (no) encoding
  176. case 3: $safe .= $letter;
  177. }
  178. }
  179. return $safe;
  180. }
  181. /**
  182. * Generates an obfuscated version of an email address. Helps prevent spam
  183. * robots from finding email addresses.
  184. *
  185. * echo HTML::email($address);
  186. *
  187. * @param string email address
  188. * @return string
  189. * @uses HTML::obfuscate
  190. */
  191. public static function email($email)
  192. {
  193. // Make sure the at sign is always obfuscated
  194. return str_replace('@', '&#64;', HTML::obfuscate($email));
  195. }
  196. /**
  197. * Creates an email (mailto:) anchor. Note that the title is not escaped,
  198. * to allow HTML elements within links (images, etc).
  199. *
  200. * echo HTML::mailto($address);
  201. *
  202. * @param string email address to send to
  203. * @param string link text
  204. * @param array HTML anchor attributes
  205. * @return string
  206. * @uses HTML::email
  207. * @uses HTML::attributes
  208. */
  209. public static function mailto($email, $title = NULL, array $attributes = NULL)
  210. {
  211. // Obfuscate email address
  212. $email = HTML::email($email);
  213. if ($title === NULL)
  214. {
  215. // Use the email address as the title
  216. $title = $email;
  217. }
  218. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.HTML::attributes($attributes).'>'.$title.'</a>';
  219. }
  220. /**
  221. * Creates a style sheet link element.
  222. *
  223. * echo HTML::style('media/css/screen.css');
  224. *
  225. * @param string file name
  226. * @param array default attributes
  227. * @param boolean include the index page
  228. * @return string
  229. * @uses URL::base
  230. * @uses HTML::attributes
  231. */
  232. public static function style($file, array $attributes = NULL, $index = FALSE)
  233. {
  234. if (strpos($file, '://') === FALSE)
  235. {
  236. // Add the base URL
  237. $file = URL::base($index).$file;
  238. }
  239. // Set the stylesheet link
  240. $attributes['href'] = $file;
  241. // Set the stylesheet rel
  242. $attributes['rel'] = 'stylesheet';
  243. // Set the stylesheet type
  244. $attributes['type'] = 'text/css';
  245. return '<link'.HTML::attributes($attributes).' />';
  246. }
  247. /**
  248. * Creates a script link.
  249. *
  250. * echo HTML::script('media/js/jquery.min.js');
  251. *
  252. * @param string file name
  253. * @param array default attributes
  254. * @param boolean include the index page
  255. * @return string
  256. * @uses URL::base
  257. * @uses HTML::attributes
  258. */
  259. public static function script($file, array $attributes = NULL, $index = FALSE)
  260. {
  261. if (strpos($file, '://') === FALSE)
  262. {
  263. // Add the base URL
  264. $file = URL::base($index).$file;
  265. }
  266. // Set the script link
  267. $attributes['src'] = $file;
  268. // Set the script type
  269. $attributes['type'] = 'text/javascript';
  270. return '<script'.HTML::attributes($attributes).'></script>';
  271. }
  272. /**
  273. * Creates a image link.
  274. *
  275. * echo HTML::image('media/img/logo.png', array('alt' => 'My Company'));
  276. *
  277. * @param string file name
  278. * @param array default attributes
  279. * @return string
  280. * @uses URL::base
  281. * @uses HTML::attributes
  282. */
  283. public static function image($file, array $attributes = NULL, $index = FALSE)
  284. {
  285. if (strpos($file, '://') === FALSE)
  286. {
  287. // Add the base URL
  288. $file = URL::base($index).$file;
  289. }
  290. // Add the image link
  291. $attributes['src'] = $file;
  292. return '<img'.HTML::attributes($attributes).' />';
  293. }
  294. /**
  295. * Compiles an array of HTML attributes into an attribute string.
  296. * Attributes will be sorted using HTML::$attribute_order for consistency.
  297. *
  298. * echo '<div'.HTML::attributes($attrs).'>'.$content.'</div>';
  299. *
  300. * @param array attribute list
  301. * @return string
  302. */
  303. public static function attributes(array $attributes = NULL)
  304. {
  305. if (empty($attributes))
  306. return '';
  307. $sorted = array();
  308. foreach (HTML::$attribute_order as $key)
  309. {
  310. if (isset($attributes[$key]))
  311. {
  312. // Add the attribute to the sorted list
  313. $sorted[$key] = $attributes[$key];
  314. }
  315. }
  316. // Combine the sorted attributes
  317. $attributes = $sorted + $attributes;
  318. $compiled = '';
  319. foreach ($attributes as $key => $value)
  320. {
  321. if ($value === NULL)
  322. {
  323. // Skip attributes that have NULL values
  324. continue;
  325. }
  326. // Add the attribute value
  327. $compiled .= ' '.$key.'="'.htmlspecialchars($value, ENT_QUOTES, Kohana::$charset).'"';
  328. }
  329. return $compiled;
  330. }
  331. public static function date($date = 'now', $format = '%Y.%m.%d')
  332. {
  333. return Date::factory($date)->toFormat($format);
  334. }
  335. final private function __construct()
  336. {
  337. // This is a static class
  338. }
  339. } // End html