PageRenderTime 80ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/3.0/obsolete/web_client/system/helpers/html.php

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