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

/system/helpers/html.php

https://github.com/yamamoto123/Ushahidi_Web
PHP | 428 lines | 224 code | 50 blank | 154 comment | 20 complexity | 4a2ea7c99f58678daef93f0e3eece665 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * HTML helper class.
  4. *
  5. * $Id: html.php 3917 2009-01-21 03:06:22Z zombor $
  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. if (strpos($href, '://') === FALSE)
  273. {
  274. // Make the URL absolute
  275. $href = url::base($index).$href;
  276. }
  277. $length = strlen($suffix);
  278. if ( $length > 0 AND substr_compare($href, $suffix, -$length, $length, FALSE) !== 0)
  279. {
  280. // Add the defined suffix
  281. $href .= $suffix;
  282. }
  283. $attr = array
  284. (
  285. 'rel' => $rel,
  286. 'type' => $type,
  287. 'href' => $href,
  288. );
  289. if ( ! empty($media))
  290. {
  291. // Add the media type to the attributes
  292. $attr['media'] = $media;
  293. }
  294. $compiled = '<link'.html::attributes($attr).' />';
  295. }
  296. return $compiled."\n";
  297. }
  298. /**
  299. * Creates a script link.
  300. *
  301. * @param string|array filename
  302. * @param boolean include the index_page in the link
  303. * @return string
  304. */
  305. public static function script($script, $index = FALSE)
  306. {
  307. $compiled = '';
  308. if (is_array($script))
  309. {
  310. foreach ($script as $name)
  311. {
  312. $compiled .= html::script($name, $index);
  313. }
  314. }
  315. else
  316. {
  317. if (strpos($script, '://') === FALSE)
  318. {
  319. // Add the suffix only when it's not already present
  320. $script = url::base((bool) $index).$script;
  321. }
  322. if (substr_compare($script, '.js', -3, 3, FALSE) !== 0)
  323. {
  324. // Add the javascript suffix
  325. $script .= '.js';
  326. }
  327. $compiled = '<script type="text/javascript" src="'.$script.'"></script>';
  328. }
  329. return $compiled."\n";
  330. }
  331. /**
  332. * Creates a image link.
  333. *
  334. * @param string image source, or an array of attributes
  335. * @param string|array image alt attribute, or an array of attributes
  336. * @param boolean include the index_page in the link
  337. * @return string
  338. */
  339. public static function image($src = NULL, $alt = NULL, $index = FALSE)
  340. {
  341. // Create attribute list
  342. $attributes = is_array($src) ? $src : array('src' => $src);
  343. if (is_array($alt))
  344. {
  345. $attributes += $alt;
  346. }
  347. elseif ( ! empty($alt))
  348. {
  349. // Add alt to attributes
  350. $attributes['alt'] = $alt;
  351. }
  352. if (strpos($attributes['src'], '://') === FALSE)
  353. {
  354. // Make the src attribute into an absolute URL
  355. $attributes['src'] = url::base($index).$attributes['src'];
  356. }
  357. return '<img'.html::attributes($attributes).' />';
  358. }
  359. /**
  360. * Compiles an array of HTML attributes into an attribute string.
  361. *
  362. * @param string|array array of attributes
  363. * @return string
  364. */
  365. public static function attributes($attrs)
  366. {
  367. if (empty($attrs))
  368. return '';
  369. if (is_string($attrs))
  370. return ' '.$attrs;
  371. $compiled = '';
  372. foreach ($attrs as $key => $val)
  373. {
  374. $compiled .= ' '.$key.'="'.html::specialchars($val).'"';
  375. }
  376. return $compiled;
  377. }
  378. } // End html