PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helpers/html.php

https://bitbucket.org/tomina/freenetisrest
PHP | 334 lines | 167 code | 34 blank | 133 comment | 17 complexity | c4933281dbff9a22c3bfe0db83ca8c3e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, GPL-3.0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * HTML helper class.
  4. *
  5. * $Id: html.php 1971 2008-02-06 21:55:46Z 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. /**
  14. * Convert special characters to HTML entities
  15. *
  16. * @param string string to convert
  17. * @param boolean encode existing entities
  18. * @return string
  19. */
  20. public static function specialchars($str, $double_encode = TRUE)
  21. {
  22. // Do encode existing HTML entities (default)
  23. if ($double_encode == TRUE)
  24. {
  25. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  26. }
  27. else
  28. {
  29. // Do not encode existing HTML entities
  30. // From PHP 5.2.3 this functionality is built-in, otherwise use a regex
  31. if (version_compare(PHP_VERSION, '5.2.3', '>='))
  32. {
  33. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', FALSE);
  34. }
  35. else
  36. {
  37. $str = preg_replace('/&(?!(?:#\d+|[a-z]+);)/i', '&amp;', $str);
  38. $str = str_replace(array('<', '>', '\'', '"'), array('&lt;', '&gt;', '&#39;', '&quot;'), $str);
  39. }
  40. }
  41. return $str;
  42. }
  43. /**
  44. * Creates an HTTP query string from an array. Deprecated.
  45. *
  46. * @param array array of data to convert to string
  47. * @return string
  48. */
  49. public static function query_string($array)
  50. {
  51. return http_build_query($array);
  52. }
  53. /**
  54. * Create HTML link anchors.
  55. *
  56. * @param string URL or URI string
  57. * @param string link text
  58. * @param array HTML anchor attributes
  59. * @param string non-default protocol, eg: https
  60. * @return string
  61. */
  62. public static function anchor($uri, $title = FALSE, $attributes = FALSE, $protocol = FALSE)
  63. {
  64. if ($uri === '')
  65. {
  66. $site_url = url::base(FALSE);
  67. }
  68. elseif (strpos($uri, '://') === FALSE)
  69. {
  70. $site_url = url::site($uri, $protocol);
  71. }
  72. else
  73. {
  74. $site_url = $uri;
  75. }
  76. return
  77. // Parsed URL
  78. '<a href="'.html::specialchars($site_url, FALSE).'"'
  79. // Attributes empty? Use an empty string
  80. .(empty($attributes) ? '' : html::attributes($attributes)).'>'
  81. // Title empty? Use the parsed URL
  82. .(empty($title) ? $site_url : $title).'</a>';
  83. }
  84. /**
  85. * Creates an HTML anchor to a file.
  86. *
  87. * @param string name of file to link to
  88. * @param string link text
  89. * @param array HTML anchor attributes
  90. * @param string non-default protocol, eg: ftp
  91. * @return string
  92. */
  93. public static function file_anchor($file, $title = FALSE, $attributes = FALSE, $protocol = FALSE)
  94. {
  95. return
  96. // Base URL + URI = full URL
  97. '<a href="'.html::specialchars(url::base(FALSE, $protocol).$file, FALSE).'"'
  98. // Attributes empty? Use an empty string
  99. .(empty($attributes) ? '' : html::attributes($attributes)).'>'
  100. // Title empty? Use the filename part of the URI
  101. .(empty($title) ? end(explode('/', $file)) : $title) .'</a>';
  102. }
  103. /**
  104. * Similar to anchor, but with the protocol parameter first.
  105. *
  106. * @param string link protocol
  107. * @param string URI or URL to link to
  108. * @param string link text
  109. * @param array HTML anchor attributes
  110. * @return string
  111. */
  112. public static function panchor($protocol, $uri, $title = FALSE, $attributes = FALSE)
  113. {
  114. return html::anchor($uri, $title, $attributes, $protocol);
  115. }
  116. /**
  117. * Creates a email anchor.
  118. *
  119. * @param string email address to send to
  120. * @param string link text
  121. * @param array HTML anchor attributes
  122. * @return string
  123. */
  124. public static function mailto($email, $title = FALSE, $attributes = FALSE)
  125. {
  126. // Remove the subject or other parameters that do not need to be encoded
  127. $subject = FALSE;
  128. if (strpos($email, '?') !== FALSE)
  129. {
  130. list ($email, $subject) = explode('?', $email);
  131. }
  132. $safe = '';
  133. foreach(str_split($email) as $i => $letter)
  134. {
  135. switch (($letter == '@') ? rand(1, 2) : rand(1, 3))
  136. {
  137. // HTML entity code
  138. case 1: $safe .= '&#'.ord($letter).';'; break;
  139. // Hex character code
  140. case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
  141. // Raw (no) encoding
  142. case 3: $safe .= $letter;
  143. }
  144. }
  145. // Title defaults to the encoded email address
  146. $title = ($title == FALSE) ? $safe : $title;
  147. // URL encode the subject line
  148. $subject = ($subject == TRUE) ? '?'.rawurlencode($subject) : '';
  149. // Parse attributes
  150. $attributes = ($attributes == TRUE) ? html::attributes($attributes) : '';
  151. // Encoded start of the href="" is a static encoded version of 'mailto:'
  152. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$safe.$subject.'"'.$attributes.'>'.$title.'</a>';
  153. }
  154. /**
  155. * Generate a "breadcrumb" list of anchors representing the URI.
  156. *
  157. * @param array segments to use as breadcrumbs, defaults to using Router::$segments
  158. * @return string
  159. */
  160. public static function breadcrumb($segments = NULL)
  161. {
  162. empty($segments) and $segments = Router::$segments;
  163. $array = array();
  164. while ($segment = array_pop($segments))
  165. {
  166. $array[] = html::anchor
  167. (
  168. // Complete URI for the URL
  169. implode('/', $segments).'/'.$segment,
  170. // Title for the current segment
  171. ucwords(inflector::humanize($segment))
  172. );
  173. }
  174. // Retrun the array of all the segments
  175. return array_reverse($array);
  176. }
  177. /**
  178. * Creates a stylesheet link.
  179. *
  180. * @param string|array filename, or array of filenames to match to array of medias
  181. * @param string|array media type of stylesheet, or array to match filenames
  182. * @param boolean include the index_page in the link
  183. * @return string
  184. */
  185. public static function stylesheet($style, $media = FALSE, $index = FALSE)
  186. {
  187. return html::link($style, 'stylesheet', 'text/css', '.css', $media, $index);
  188. }
  189. /**
  190. * Creates a link tag.
  191. *
  192. * @param string|array filename
  193. * @param string|array relationship
  194. * @param string|array mimetype
  195. * @param string specifies suffix of the file
  196. * @param string|array specifies on what device the document will be displayed
  197. * @param boolean include the index_page in the link
  198. * @return string
  199. */
  200. public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE)
  201. {
  202. $compiled = '';
  203. if (is_array($href))
  204. {
  205. foreach($href as $_href)
  206. {
  207. $_rel = is_array($rel) ? array_shift($rel) : $rel;
  208. $_type = is_array($type) ? array_shift($type) : $type;
  209. $_media = is_array($media) ? array_shift($media) : $media;
  210. $compiled .= html::link($_href, $_rel, $_type, $suffix, $_media, $index);
  211. }
  212. }
  213. else
  214. {
  215. // Add the suffix only when it's not already present
  216. $suffix = ( ! empty($suffix) AND strpos($href, $suffix) === FALSE) ? $suffix : '';
  217. $media = empty($media) ? '' : ' media="'.$media.'"';
  218. $compiled = '<link rel="'.$rel.'" type="'.$type.'" href="'.url::base((bool) $index).$href.$suffix.'"'.$media.' />';
  219. }
  220. return $compiled."\n";
  221. }
  222. /**
  223. * Creates a script link.
  224. *
  225. * @param string|array filename
  226. * @param boolean include the index_page in the link
  227. * @return string
  228. */
  229. public static function script($script, $index = FALSE)
  230. {
  231. $compiled = '';
  232. if (is_array($script))
  233. {
  234. foreach($script as $name)
  235. {
  236. $compiled .= html::script($name, $index);
  237. }
  238. }
  239. else
  240. {
  241. // Add the suffix only when it's not already present
  242. $suffix = (strpos($script, '.js') === FALSE) ? '.js' : '';
  243. $compiled = '<script type="text/javascript" src="'.url::base((bool) $index).$script.$suffix.'"></script>';
  244. }
  245. return $compiled."\n";
  246. }
  247. /**
  248. * Creates a script link.
  249. *
  250. * @param string|array filename
  251. * @param boolean include the index_page in the link
  252. * @return string
  253. */
  254. public static function execute_script($script)
  255. {
  256. $compiled = '<script>';
  257. $compiled .= $script.'';
  258. $compiled = '</script>';
  259. return $compiled."\n";
  260. }
  261. /**
  262. * Creates a image link.
  263. *
  264. * @param string|array array of html attributes, or an image name
  265. * @param boolean include the index_page in the link
  266. * @return string
  267. */
  268. public static function image($attr = NULL, $index = FALSE)
  269. {
  270. if ( ! is_array($attr))
  271. {
  272. $attr = array('src' => $attr);
  273. }
  274. if (strpos($attr['src'], '://') === FALSE)
  275. {
  276. // Make the src attribute into an absolute URL
  277. $attr['src'] = url::base($index).$attr['src'];
  278. }
  279. return '<img'.html::attributes($attr).' />';
  280. }
  281. /**
  282. * Compiles an array of HTML attributes into an attribute string.
  283. *
  284. * @param string|array array of attributes
  285. * @return string
  286. */
  287. public static function attributes($attrs)
  288. {
  289. if (empty($attrs))
  290. return '';
  291. if (is_string($attrs))
  292. return ' '.$attrs;
  293. $compiled = '';
  294. foreach($attrs as $key => $val)
  295. {
  296. $compiled .= ' '.$key.'="'.$val.'"';
  297. }
  298. return $compiled;
  299. }
  300. } // End html