SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Html.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18.  
  19. /**
  20.  * Helper functions for outputting HTML elements.
  21.  */
  22. class Html
  23. {
  24.  
  25. // Enable or disable automatic setting of target="_blank"
  26. public static $windowed_urls = FALSE;
  27.  
  28. /**
  29.   * Create HTML link anchors.
  30.   *
  31.   * @param string URL or URI string
  32.   * @param string link text
  33.   * @param array HTML anchor attributes
  34.   * @param string non-default protocol, eg: https
  35.   * @param boolean option to escape the title that is output
  36.   * @return string
  37.   */
  38. public static function anchor($uri, $title = NULL, $attributes = NULL, $protocol = NULL, $escape_title = TRUE)
  39. {
  40. if ($uri === '')
  41. {
  42. $site_url = Url::base(FALSE);
  43. }
  44. elseif (strpos($uri, '#') === 0)
  45. {
  46. // This is an id target link, not a URL
  47. $site_url = $uri;
  48. }
  49. elseif (strpos($uri, '://') === FALSE)
  50. {
  51. $site_url = Url::site($uri, $protocol);
  52. }
  53. else
  54. {
  55. if (Html::$windowed_urls === TRUE AND empty($attributes['target']))
  56. {
  57. $attributes['target'] = '_blank';
  58. }
  59.  
  60. $site_url = $uri;
  61. }
  62.  
  63. return
  64. // Parsed URL
  65. '<a href="'.Enc::html($site_url).'"'
  66. // Attributes empty? Use an empty string
  67. .(is_array($attributes) ? Html::attributes($attributes) : '').'>'
  68. // Title empty? Use the parsed URL
  69. .($escape_title ? Enc::html((($title === NULL) ? $site_url : $title), FALSE) : (($title === NULL) ? $site_url : $title)).'</a>';
  70. }
  71.  
  72. /**
  73.   * Generates an obfuscated version of an email address.
  74.   *
  75.   * @param string email address
  76.   * @return string
  77.   */
  78. public static function email($email)
  79. {
  80. $safe = '';
  81. foreach (str_split($email) as $letter)
  82. {
  83. switch (($letter === '@') ? mt_rand(1, 2) : mt_rand(1, 3))
  84. {
  85. // HTML entity code
  86. case 1: $safe .= '&#'.ord($letter).';'; break;
  87. // Hex character code
  88. case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
  89. // Raw (no) encoding
  90. case 3: $safe .= $letter;
  91. }
  92. }
  93.  
  94. return $safe;
  95. }
  96.  
  97. /**
  98.   * Creates an email anchor.
  99.   *
  100.   * @param string email address to send to
  101.   * @param string link text
  102.   * @param array HTML anchor attributes
  103.   * @return string
  104.   */
  105. public static function mailto($email, $title = NULL, $attributes = NULL)
  106. {
  107. if (empty($email))
  108. return $title;
  109.  
  110. // Remove the subject or other parameters that do not need to be encoded
  111. if (strpos($email, '?') !== FALSE)
  112. {
  113. // Extract the parameters from the email address
  114. list ($email, $params) = explode('?', $email, 2);
  115.  
  116. // Make the params into a query string, replacing spaces
  117. $params = '?'.str_replace(' ', '%20', $params);
  118. }
  119. else
  120. {
  121. // No parameters
  122. $params = '';
  123. }
  124.  
  125. // Obfuscate email address
  126. $safe = Html::email($email);
  127.  
  128. // Title defaults to the encoded email address
  129. empty($title) and $title = $safe;
  130.  
  131. // Parse attributes
  132. empty($attributes) or $attributes = Html::attributes($attributes);
  133.  
  134. // Encoded start of the href="" is a static encoded version of 'mailto:'
  135. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$safe.$params.'"'.$attributes.'>'.$title.'</a>';
  136. }
  137.  
  138. /**
  139.   * Creates a link tag.
  140.   *
  141.   * @param string|array filename
  142.   * @param string|array relationship
  143.   * @param string|array mimetype
  144.   * @param string specifies suffix of the file
  145.   * @param string|array specifies on what device the document will be displayed
  146.   * @param boolean include the index_page in the link
  147.   * @return string
  148.   */
  149. public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE)
  150. {
  151. $compiled = '';
  152.  
  153. if (is_array($href))
  154. {
  155. foreach ($href as $_href)
  156. {
  157. $_rel = is_array($rel) ? array_shift($rel) : $rel;
  158. $_type = is_array($type) ? array_shift($type) : $type;
  159. $_media = is_array($media) ? array_shift($media) : $media;
  160.  
  161. $compiled .= Html::link($_href, $_rel, $_type, $suffix, $_media, $index);
  162. }
  163. }
  164. else
  165. {
  166. if (strpos($href, '://') === FALSE)
  167. {
  168. // Make the URL absolute
  169. $href = Url::base($index).$href;
  170. }
  171.  
  172. $length = strlen($suffix);
  173.  
  174. if ( $length > 0 AND substr_compare($href, $suffix, -$length, $length, FALSE) !== 0)
  175. {
  176. // Add the defined suffix
  177. $href .= $suffix;
  178. }
  179.  
  180. $attr = array
  181. (
  182. 'rel' => $rel,
  183. 'type' => $type,
  184. 'href' => $href,
  185. );
  186.  
  187. if ( ! empty($media))
  188. {
  189. // Add the media type to the attributes
  190. $attr['media'] = $media;
  191. }
  192.  
  193. $compiled = '<link'.Html::attributes($attr).' />';
  194. }
  195.  
  196. return $compiled."\n";
  197. }
  198.  
  199. /**
  200.   * Creates a script link.
  201.   *
  202.   * @param string|array filename
  203.   * @param boolean include the index_page in the link
  204.   * @return string
  205.   */
  206. public static function script($script, $index = FALSE)
  207. {
  208. $compiled = '';
  209.  
  210. if (is_array($script))
  211. {
  212. foreach ($script as $name)
  213. {
  214. $compiled .= Html::script($name, $index);
  215. }
  216. }
  217. else
  218. {
  219. if (strpos($script, '://') === FALSE)
  220. {
  221. // Add the suffix only when it's not already present
  222. $script = Url::base((bool) $index).$script;
  223. }
  224.  
  225. if (substr_compare($script, '.js', -3, 3, FALSE) !== 0)
  226. {
  227. // Add the javascript suffix
  228. $script .= '.js';
  229. }
  230.  
  231. $compiled = '<script type="text/javascript" src="'.$script.'"></script>';
  232. }
  233.  
  234. return $compiled."\n";
  235. }
  236.  
  237. /**
  238.   * Creates a image link.
  239.   *
  240.   * @param string image source, or an array of attributes
  241.   * @param string|array image alt attribute, or an array of attributes
  242.   * @param boolean include the index_page in the link
  243.   * @return string
  244.   */
  245. public static function image($src = NULL, $alt = NULL, $index = FALSE)
  246. {
  247. // Create attribute list
  248. $attributes = is_array($src) ? $src : array('src' => $src);
  249.  
  250. if (is_array($alt))
  251. {
  252. $attributes += $alt;
  253. }
  254. elseif ( ! empty($alt))
  255. {
  256. // Add alt to attributes
  257. $attributes['alt'] = $alt;
  258. }
  259.  
  260. if (strpos($attributes['src'], '://') === FALSE)
  261. {
  262. // Make the src attribute into an absolute URL
  263. $attributes['src'] = Url::base($index).$attributes['src'];
  264. }
  265.  
  266. return '<img'.Html::attributes($attributes).' />';
  267. }
  268.  
  269. /**
  270.   * Compiles an array of HTML attributes into an attribute string.
  271.   *
  272.   * @param string|array array of attributes
  273.   * @return string
  274.   */
  275. public static function attributes($attrs)
  276. {
  277. if (empty($attrs))
  278. return '';
  279.  
  280. if (is_string($attrs))
  281. return ' '.$attrs;
  282.  
  283. $compiled = '';
  284. foreach ($attrs as $key => $val)
  285. {
  286. $compiled .= ' '.Enc::html($key).'="'.Enc::html($val).'"';
  287. }
  288.  
  289. return $compiled;
  290. }
  291.  
  292. } // End html
  293.