PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/html.php

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