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

/includes/libraries/html/html.php

http://joostina.googlecode.com/
PHP | 397 lines | 183 code | 51 blank | 163 comment | 25 complexity | 805fcaabae586497c5df87f3846541ea MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * HTML helper class.
  4. *
  5. * $Id: html.php 4376 2009-06-01 11:40:39Z samsoir $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class html {
  13. // Enable or disable automatic setting of target="_blank"
  14. public static $windowed_urls = FALSE;
  15. /**
  16. * Convert special characters to HTML entities
  17. *
  18. * @param string string to convert
  19. * @param boolean encode existing entities
  20. * @return string
  21. */
  22. public static function specialchars($str, $double_encode = TRUE) {
  23. // Force the string to be a string
  24. $str = (string) $str;
  25. // Do encode existing HTML entities (default)
  26. if ($double_encode === TRUE) {
  27. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  28. }
  29. else {
  30. // Do not encode existing HTML entities
  31. // From PHP 5.2.3 this functionality is built-in, otherwise use a regex
  32. if (version_compare(PHP_VERSION, '5.2.3', '>=')) {
  33. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', FALSE);
  34. }
  35. else {
  36. $str = preg_replace('/&(?!(?:#\d++|[a-z]++);)/ui', '&amp;', $str);
  37. $str = str_replace(array('<', '>', '\'', '"'), array('&lt;', '&gt;', '&#39;', '&quot;'), $str);
  38. }
  39. }
  40. return $str;
  41. }
  42. /**
  43. * Perform a html::specialchars() with additional URL specific encoding.
  44. *
  45. * @param string string to convert
  46. * @param boolean encode existing entities
  47. * @return string
  48. */
  49. public static function specialurlencode($str, $double_encode = TRUE) {
  50. return str_replace(' ', '%20', html::specialchars($str, $double_encode));
  51. }
  52. /**
  53. * Create HTML link anchors.
  54. *
  55. * @param string URL or URI string
  56. * @param string link text
  57. * @param array HTML anchor attributes
  58. * @param string non-default protocol, eg: https
  59. * @param boolean option to escape the title that is output
  60. * @return string
  61. */
  62. public static function anchor($uri, $title = NULL, $attributes = NULL, $protocol = NULL, $escape_title = FALSE) {
  63. if ($uri === '') {
  64. $site_url = JPATH_SITE;
  65. }
  66. elseif (strpos($uri, '#') === 0) {
  67. // This is an id target link, not a URL
  68. $site_url = $uri;
  69. }else {
  70. if (html::$windowed_urls === TRUE AND empty($attributes['target'])) {
  71. $attributes['target'] = '_blank';
  72. }
  73. $site_url = $uri;
  74. }
  75. return
  76. // Parsed URL
  77. '<a href="'.html::specialurlencode($site_url, FALSE).'"'
  78. // Attributes empty? Use an empty string
  79. .(is_array($attributes) ? html::attributes($attributes) : '').'>'
  80. // Title empty? Use the parsed URL
  81. .($escape_title ? html::specialchars((($title === NULL) ? $site_url : $title), FALSE) : (($title === NULL) ? $site_url : $title)).'</a>';
  82. }
  83. /**
  84. * Creates an HTML anchor to a file.
  85. *
  86. * @param string name of file to link to
  87. * @param string link text
  88. * @param array HTML anchor attributes
  89. * @param string non-default protocol, eg: ftp
  90. * @return string
  91. */
  92. public static function file_anchor($file, $title = NULL, $attributes = NULL, $protocol = NULL) {
  93. return
  94. // Base URL + URI = full URL
  95. '<a href="'.html::specialurlencode(url::base(FALSE, $protocol).$file, FALSE).'"'
  96. // Attributes empty? Use an empty string
  97. .(is_array($attributes) ? html::attributes($attributes) : '').'>'
  98. // Title empty? Use the filename part of the URI
  99. .(($title === NULL) ? end(explode('/', $file)) : $title) .'</a>';
  100. }
  101. /**
  102. * Similar to anchor, but with the protocol parameter first.
  103. *
  104. * @param string link protocol
  105. * @param string URI or URL to link to
  106. * @param string link text
  107. * @param array HTML anchor attributes
  108. * @return string
  109. */
  110. public static function panchor($protocol, $uri, $title = NULL, $attributes = FALSE) {
  111. return html::anchor($uri, $title, $attributes, $protocol);
  112. }
  113. /**
  114. * Create an array of anchors from an array of link/title pairs.
  115. *
  116. * @param array link/title pairs
  117. * @return array
  118. */
  119. public static function anchor_array(array $array) {
  120. $anchors = array();
  121. foreach ($array as $link => $title) {
  122. // Create list of anchors
  123. $anchors[] = html::anchor($link, $title);
  124. }
  125. return $anchors;
  126. }
  127. /**
  128. * Generates an obfuscated version of an email address.
  129. *
  130. * @param string email address
  131. * @return string
  132. */
  133. public static function email($email) {
  134. $safe = '';
  135. foreach (str_split($email) as $letter) {
  136. switch (($letter === '@') ? rand(1, 2) : rand(1, 3)) {
  137. // HTML entity code
  138. case 1: $safe .= '&#'.ord($letter).';';
  139. break;
  140. // Hex character code
  141. case 2: $safe .= '&#x'.dechex(ord($letter)).';';
  142. break;
  143. // Raw (no) encoding
  144. case 3: $safe .= $letter;
  145. }
  146. }
  147. return $safe;
  148. }
  149. /**
  150. * Creates an email anchor.
  151. *
  152. * @param string email address to send to
  153. * @param string link text
  154. * @param array HTML anchor attributes
  155. * @return string
  156. */
  157. public static function mailto($email, $title = NULL, $attributes = NULL) {
  158. if (empty($email))
  159. return $title;
  160. // Remove the subject or other parameters that do not need to be encoded
  161. if (strpos($email, '?') !== FALSE) {
  162. // Extract the parameters from the email address
  163. list ($email, $params) = explode('?', $email, 2);
  164. // Make the params into a query string, replacing spaces
  165. $params = '?'.str_replace(' ', '%20', $params);
  166. }
  167. else {
  168. // No parameters
  169. $params = '';
  170. }
  171. // Obfuscate email address
  172. $safe = html::email($email);
  173. // Title defaults to the encoded email address
  174. empty($title) and $title = $safe;
  175. // Parse attributes
  176. empty($attributes) or $attributes = html::attributes($attributes);
  177. // Encoded start of the href="" is a static encoded version of 'mailto:'
  178. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$safe.$params.'"'.$attributes.'>'.$title.'</a>';
  179. }
  180. /**
  181. * Generate a "breadcrumb" list of anchors representing the URI.
  182. *
  183. * @param array segments to use as breadcrumbs, defaults to using Router::$segments
  184. * @return string
  185. */
  186. public static function breadcrumb($segments = NULL) {
  187. empty($segments) and $segments = Router::$segments;
  188. $array = array();
  189. while ($segment = array_pop($segments)) {
  190. $array[] = html::anchor
  191. (
  192. // Complete URI for the URL
  193. implode('/', $segments).'/'.$segment,
  194. // Title for the current segment
  195. ucwords(inflector::humanize($segment))
  196. );
  197. }
  198. // Retrun the array of all the segments
  199. return array_reverse($array);
  200. }
  201. /**
  202. * Creates a meta tag.
  203. *
  204. * @param string|array tag name, or an array of tags
  205. * @param string tag "content" value
  206. * @return string
  207. */
  208. public static function meta($tag, $value = NULL) {
  209. if (is_array($tag)) {
  210. $tags = array();
  211. foreach ($tag as $t => $v) {
  212. // Build each tag and add it to the array
  213. $tags[] = html::meta($t, $v);
  214. }
  215. // Return all of the tags as a string
  216. return implode("\n", $tags);
  217. }
  218. // Set the meta attribute value
  219. $attr = in_array(strtolower($tag), Kohana::config('http.meta_equiv')) ? 'http-equiv' : 'name';
  220. return '<meta '.$attr.'="'.$tag.'" content="'.$value.'" />';
  221. }
  222. /**
  223. * Creates a stylesheet link.
  224. *
  225. * @param string|array filename, or array of filenames to match to array of medias
  226. * @param string|array media type of stylesheet, or array to match filenames
  227. * @param boolean include the index_page in the link
  228. * @return string
  229. */
  230. public static function stylesheet($style, $media = FALSE, $index = FALSE) {
  231. return html::link($style, 'stylesheet', 'text/css', '.css', $media, $index);
  232. }
  233. /**
  234. * Creates a link tag.
  235. *
  236. * @param string|array filename
  237. * @param string|array relationship
  238. * @param string|array mimetype
  239. * @param string specifies suffix of the file
  240. * @param string|array specifies on what device the document will be displayed
  241. * @param boolean include the index_page in the link
  242. * @return string
  243. */
  244. public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE) {
  245. $compiled = '';
  246. if (is_array($href)) {
  247. foreach ($href as $_href) {
  248. $_rel = is_array($rel) ? array_shift($rel) : $rel;
  249. $_type = is_array($type) ? array_shift($type) : $type;
  250. $_media = is_array($media) ? array_shift($media) : $media;
  251. $compiled .= html::link($_href, $_rel, $_type, $suffix, $_media, $index);
  252. }
  253. }
  254. else {
  255. if (strpos($href, '://') === FALSE) {
  256. // Make the URL absolute
  257. $href = url::base($index).$href;
  258. }
  259. $length = strlen($suffix);
  260. if ( $length > 0 AND substr_compare($href, $suffix, -$length, $length, FALSE) !== 0) {
  261. // Add the defined suffix
  262. $href .= $suffix;
  263. }
  264. $attr = array
  265. (
  266. 'rel' => $rel,
  267. 'type' => $type,
  268. 'href' => $href,
  269. );
  270. if ( ! empty($media)) {
  271. // Add the media type to the attributes
  272. $attr['media'] = $media;
  273. }
  274. $compiled = '<link'.html::attributes($attr).' />';
  275. }
  276. return $compiled."\n";
  277. }
  278. /**
  279. * Creates a script link.
  280. *
  281. * @param string|array filename
  282. * @param boolean include the index_page in the link
  283. * @return string
  284. */
  285. public static function script($script, $index = FALSE) {
  286. $compiled = '';
  287. if (is_array($script)) {
  288. foreach ($script as $name) {
  289. $compiled .= html::script($name, $index);
  290. }
  291. }
  292. else {
  293. if (strpos($script, '://') === FALSE) {
  294. // Add the suffix only when it's not already present
  295. $script = url::base((bool) $index).$script;
  296. }
  297. if (substr_compare($script, '.js', -3, 3, FALSE) !== 0) {
  298. // Add the javascript suffix
  299. $script .= '.js';
  300. }
  301. $compiled = '<script type="text/javascript" src="'.$script.'"></script>';
  302. }
  303. return $compiled."\n";
  304. }
  305. /**
  306. * Creates a image link.
  307. *
  308. * @param string image source, or an array of attributes
  309. * @param string|array image alt attribute, or an array of attributes
  310. * @param boolean include the index_page in the link
  311. * @return string
  312. */
  313. public static function image($src = NULL, $alt = NULL, $index = FALSE) {
  314. // Create attribute list
  315. $attributes = is_array($src) ? $src : array('src' => $src);
  316. if (is_array($alt)) {
  317. $attributes += $alt;
  318. }
  319. elseif ( ! empty($alt)) {
  320. // Add alt to attributes
  321. $attributes['alt'] = $alt;
  322. }
  323. if (strpos($attributes['src'], '://') === FALSE) {
  324. // Make the src attribute into an absolute URL
  325. $attributes['src'] = url::base($index).$attributes['src'];
  326. }
  327. return '<img'.html::attributes($attributes).' />';
  328. }
  329. /**
  330. * Compiles an array of HTML attributes into an attribute string.
  331. *
  332. * @param string|array array of attributes
  333. * @return string
  334. */
  335. public static function attributes($attrs) {
  336. if (empty($attrs))
  337. return '';
  338. if (is_string($attrs))
  339. return ' '.$attrs;
  340. $compiled = '';
  341. foreach ($attrs as $key => $val) {
  342. $compiled .= ' '.$key.'="'.html::specialchars($val).'"';
  343. }
  344. return $compiled;
  345. }
  346. } // End html