PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/system/classes/kohana/html.php

https://github.com/pratikdhaboo/kodelearn
PHP | 386 lines | 169 code | 39 blank | 178 comment | 14 complexity | 3e2819c582d5ed3f211ea7a61b17d6cd 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 = FALSE)
  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. * Generates an obfuscated version of a string. Text passed through this
  156. * method is less likely to be read by web crawlers and robots, which can
  157. * be helpful for spam prevention, but can prevent legitimate robots from
  158. * reading your content.
  159. *
  160. * echo HTML::obfuscate($text);
  161. *
  162. * @param string string to obfuscate
  163. * @return string
  164. * @since 3.0.3
  165. */
  166. public static function obfuscate($string)
  167. {
  168. $safe = '';
  169. foreach (str_split($string) as $letter)
  170. {
  171. switch (rand(1, 3))
  172. {
  173. // HTML entity code
  174. case 1:
  175. $safe .= '&#'.ord($letter).';';
  176. break;
  177. // Hex character code
  178. case 2:
  179. $safe .= '&#x'.dechex(ord($letter)).';';
  180. break;
  181. // Raw (no) encoding
  182. case 3:
  183. $safe .= $letter;
  184. }
  185. }
  186. return $safe;
  187. }
  188. /**
  189. * Generates an obfuscated version of an email address. Helps prevent spam
  190. * robots from finding email addresses.
  191. *
  192. * echo HTML::email($address);
  193. *
  194. * @param string email address
  195. * @return string
  196. * @uses HTML::obfuscate
  197. */
  198. public static function email($email)
  199. {
  200. // Make sure the at sign is always obfuscated
  201. return str_replace('@', '&#64;', HTML::obfuscate($email));
  202. }
  203. /**
  204. * Creates an email (mailto:) anchor. Note that the title is not escaped,
  205. * to allow HTML elements within links (images, etc).
  206. *
  207. * echo HTML::mailto($address);
  208. *
  209. * @param string email address to send to
  210. * @param string link text
  211. * @param array HTML anchor attributes
  212. * @return string
  213. * @uses HTML::email
  214. * @uses HTML::attributes
  215. */
  216. public static function mailto($email, $title = NULL, array $attributes = NULL)
  217. {
  218. // Obfuscate email address
  219. $email = HTML::email($email);
  220. if ($title === NULL)
  221. {
  222. // Use the email address as the title
  223. $title = $email;
  224. }
  225. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.HTML::attributes($attributes).'>'.$title.'</a>';
  226. }
  227. /**
  228. * Creates a style sheet link element.
  229. *
  230. * echo HTML::style('media/css/screen.css');
  231. *
  232. * @param string file name
  233. * @param array default attributes
  234. * @param mixed protocol to pass to URL::base()
  235. * @param boolean include the index page
  236. * @return string
  237. * @uses URL::base
  238. * @uses HTML::attributes
  239. */
  240. public static function style($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  241. {
  242. if (strpos($file, '://') === FALSE)
  243. {
  244. // Add the base URL
  245. $file = URL::base($protocol, $index).$file;
  246. }
  247. // Set the stylesheet link
  248. $attributes['href'] = $file;
  249. // Set the stylesheet rel
  250. $attributes['rel'] = 'stylesheet';
  251. // Set the stylesheet type
  252. $attributes['type'] = 'text/css';
  253. return '<link'.HTML::attributes($attributes).' />';
  254. }
  255. /**
  256. * Creates a script link.
  257. *
  258. * echo HTML::script('media/js/jquery.min.js');
  259. *
  260. * @param string file name
  261. * @param array default attributes
  262. * @param mixed protocol to pass to URL::base()
  263. * @param boolean include the index page
  264. * @return string
  265. * @uses URL::base
  266. * @uses HTML::attributes
  267. */
  268. public static function script($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  269. {
  270. if (strpos($file, '://') === FALSE)
  271. {
  272. // Add the base URL
  273. $file = URL::base($protocol, $index).$file;
  274. }
  275. // Set the script link
  276. $attributes['src'] = $file;
  277. // Set the script type
  278. $attributes['type'] = 'text/javascript';
  279. return '<script'.HTML::attributes($attributes).'></script>';
  280. }
  281. /**
  282. * Creates a image link.
  283. *
  284. * echo HTML::image('media/img/logo.png', array('alt' => 'My Company'));
  285. *
  286. * @param string file name
  287. * @param array default attributes
  288. * @param mixed protocol to pass to URL::base()
  289. * @param boolean include the index page
  290. * @return string
  291. * @uses URL::base
  292. * @uses HTML::attributes
  293. */
  294. public static function image($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
  295. {
  296. if (strpos($file, '://') === FALSE)
  297. {
  298. // Add the base URL
  299. $file = URL::base($protocol, $index).$file;
  300. }
  301. // Add the image link
  302. $attributes['src'] = $file;
  303. return '<img'.HTML::attributes($attributes).' />';
  304. }
  305. /**
  306. * Compiles an array of HTML attributes into an attribute string.
  307. * Attributes will be sorted using HTML::$attribute_order for consistency.
  308. *
  309. * echo '<div'.HTML::attributes($attrs).'>'.$content.'</div>';
  310. *
  311. * @param array attribute list
  312. * @return string
  313. */
  314. public static function attributes(array $attributes = NULL)
  315. {
  316. if (empty($attributes))
  317. return '';
  318. $sorted = array();
  319. foreach (HTML::$attribute_order as $key)
  320. {
  321. if (isset($attributes[$key]))
  322. {
  323. // Add the attribute to the sorted list
  324. $sorted[$key] = $attributes[$key];
  325. }
  326. }
  327. // Combine the sorted attributes
  328. $attributes = $sorted + $attributes;
  329. $compiled = '';
  330. foreach ($attributes as $key => $value)
  331. {
  332. if ($value === NULL)
  333. {
  334. // Skip attributes that have NULL values
  335. continue;
  336. }
  337. if (is_int($key))
  338. {
  339. // Assume non-associative keys are mirrored attributes
  340. $key = $value;
  341. }
  342. // Add the attribute value
  343. $compiled .= ' '.$key.'="'.HTML::chars($value).'"';
  344. }
  345. return $compiled;
  346. }
  347. } // End html