PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Html.php

https://github.com/avidenie/Yet-Another-PHP-Framework
PHP | 282 lines | 124 code | 29 blank | 129 comment | 18 complexity | ef174fc4c6999270133ef4dec2c2af46 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Yet Another PHP Framework package.
  4. * (c) 2010 Adrian Videnie <{@link mailto:avidenie@gmail.com avidenie@gmail.com}>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. *
  9. * @package Framework
  10. * @subpackage Helpers
  11. * @author Adrian Videnie <avidenie@gmail.com>
  12. * @copyright 2010 Adrian Videnie
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT license
  14. */
  15. /**
  16. *
  17. * HTML helper class.
  18. *
  19. * @package Framework
  20. * @subpackage Helpers
  21. * @author Adrian Videnie <avidenie@gmail.com>
  22. * @copyright 2010 Adrian Videnie
  23. * @license http://www.opensource.org/licenses/mit-license.php The MIT license
  24. */
  25. class Html
  26. {
  27. /**
  28. * Convert special characters to HTML entities
  29. *
  30. * @param string string to convert
  31. * @param boolean encode existing entities
  32. * @return string
  33. */
  34. public static function escape($string, $doubleEncode = true)
  35. {
  36. return htmlspecialchars((string) $string, ENT_QUOTES, 'UTF-8', $doubleEncode);
  37. }
  38. /**
  39. * Compiles an array of HTML attributes into an attribute string.
  40. *
  41. * @param string|array array of attributes
  42. * @return string
  43. */
  44. public static function attributes($attributes)
  45. {
  46. if (empty($attributes)) {
  47. return '';
  48. }
  49. if (is_string($attributes)) {
  50. return ' ' . trim($attributes);
  51. }
  52. $compiled = '';
  53. foreach ($attributes as $key => $value) {
  54. $compiled .= ' ' . $key . '="' . $value . '"';
  55. }
  56. return $compiled;
  57. }
  58. /**
  59. * Fix generated URIs.
  60. *
  61. * @param string URI string.
  62. * @param mixed Force absolute URI or force non-default protocol, eg.https.
  63. * @param boolean Whether this is a public resource or not.
  64. * @return string The fixed, transformed URI.
  65. */
  66. public static function href($uri, $absolute = false, $publicResource = false)
  67. {
  68. if (false === strpos($uri, '://')) {
  69. // Make sure the uri is in a proper form.
  70. $uri = '/' . ltrim($uri, '/');
  71. // The Request method to use to generate the Uri.
  72. $method = 'baseUri';
  73. // Change method and add the path to the public directory.
  74. if (true == $publicResource) {
  75. $method = 'basePath';
  76. $uri = Green::config('framework.public_path') . $uri;
  77. }
  78. $request = Request::instance();
  79. // Create the Uri.
  80. $uri = $request->$method() . $uri;
  81. if (false !== $absolute) {
  82. $uri = $request->hostname($absolute) . $uri;
  83. }
  84. }
  85. return $uri;
  86. }
  87. /**
  88. * Generates an uri given the name of a route.
  89. *
  90. * @param mixed $name The name of a Route to use. If null it will use the current Route.
  91. * @param array $urlOptions Options passed to the assemble method of the Route object.
  92. * @param boolean $absolute Whether or not to return an absolute URI.
  93. * @param boolean $reset Whether or not to reset the route defaults with those provided.
  94. * @return string Url for the link href attribute.
  95. */
  96. public static function uri($name = null, array $urlOptions = array(), $absolute = false, $reset = true, $encode = true)
  97. {
  98. $router = Router::instance();
  99. $uri = $router->assemble($urlOptions, $name, $reset, $encode);
  100. return self::href($uri, $absolute);
  101. }
  102. /**
  103. * Generates an obfuscated version of an email address.
  104. *
  105. * @param string E-mail address
  106. * @return string Obfuscated version of the e-mail address
  107. */
  108. public static function email($email)
  109. {
  110. $safe = '';
  111. foreach (str_split($email) as $letter) {
  112. switch (($letter === '@') ? rand(1, 2) : rand(1, 3)) {
  113. // HTML entity code
  114. case 1: $safe .= '&#'.ord($letter).';'; break;
  115. // Hex character code
  116. case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
  117. // Raw (no) encoding
  118. case 3: $safe .= $letter;
  119. }
  120. }
  121. return $safe;
  122. }
  123. /**
  124. * Creates a image link.
  125. *
  126. * @param string Image source.
  127. * @param string|array Image alt attribute, or an array of attributes
  128. * @param boolean|string Absolute uri, force non-default protocol, eg.https
  129. * @return string The image link.
  130. */
  131. public static function image($src, $alt = null, $absolute = false)
  132. {
  133. // Create attribute list
  134. $attributes = array('src' => self::href($src, $absolute, true));
  135. if (is_array($alt)) {
  136. $attributes += $alt;
  137. } elseif (!empty($alt)) {
  138. // Add alt to attributes
  139. $attributes['alt'] = htmlspecialchars((string) $alt, ENT_QUOTES, 'UTF-8');
  140. }
  141. if (!isset($attributes['alt'])) {
  142. // Calculate a default alt.
  143. $attributes['alt'] = basename($attributes['src'], substr($attributes['src'], strrpos($attributes['src'], '.')));
  144. }
  145. return '<img' . self::attributes($attributes) . ' />';
  146. }
  147. /**
  148. * Creates an email anchor.
  149. *
  150. * @param string E-mail address to send to
  151. * @param string Link text
  152. * @param array HTML anchor attributes
  153. * @return string The e-mail anchor
  154. */
  155. public static function mailto($email, $title = null, $attributes = null)
  156. {
  157. // Remove the subject or other parameters that do not need to be encoded
  158. if (false !== strpos($email, '?')) {
  159. // Extract the parameters from the email address
  160. list ($email, $params) = explode('?', $email, 2);
  161. // Make the params into a query string, replacing spaces
  162. $params = '?' . str_replace(' ', '%20', $params);
  163. } else {
  164. // No parameters
  165. $params = '';
  166. }
  167. // Obfuscate email address
  168. $safe = self::email($email);
  169. // Title defaults to the encoded email address
  170. if (empty($title)) {
  171. $title = $safe;
  172. } else {
  173. $title = htmlspecialchars((string) $title, ENT_QUOTES, 'UTF-8');
  174. }
  175. // Parse attributes
  176. if (!empty($attributes)) {
  177. $attributes = self::attributes($attributes);
  178. }
  179. // Encoded start of the href="" is a static encoded version of 'mailto:'
  180. return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;' . $safe . $params . '"' . $attributes . '>' . $title . '</a>';
  181. }
  182. /**
  183. * Returns a <script></script> tag.
  184. *
  185. * @param string The source href for the script.
  186. * @param array Additional attributes for the <script> tag.
  187. * @param mixed Whether to force absolute URI or a non-default protocol, eg.https.
  188. * @return string The <script></script> tag.
  189. */
  190. public static function script($src, array $attributes = array(), $absolute = false)
  191. {
  192. // Create a proper relative or absolute URI, as requested.
  193. $attributes['src'] = self::href($src, $absolute, true);
  194. if (isset($attributes['anti_cache'])) {
  195. $attributes['src'] .= '?' . date('U');
  196. unset($attributes['anti_cache']);
  197. }
  198. if (empty($attributes['type'])) {
  199. $attributes['type'] = 'text/javascript';
  200. }
  201. return '<script' . self::attributes($attributes) . '></script>';
  202. }
  203. /**
  204. * Returns a <script></script> block that properly commented for inclusion
  205. * in XHTML documents.
  206. *
  207. * @see http://developer.mozilla.org/en/docs/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents
  208. * @param string The source of the script.
  209. * @param array Additional attributes for the <script> tag.
  210. * @return string The <script></script> tag with the inline script.
  211. */
  212. public static function inlineScript($code, array $attributes = array())
  213. {
  214. // Make sure we don't accidentally write a source script href.
  215. unset($attributes['src']);
  216. if (empty($attributes['type'])) {
  217. $attributes['type'] = 'text/javascript';
  218. }
  219. return '<script'
  220. . self::attributes($attributes) . ">\n"
  221. . "//<![CDATA[\n"
  222. . trim($code)
  223. . "\n//]]>\n"
  224. . "</script>\n";
  225. }
  226. /**
  227. * Returns a <link>...</link> tag for external stylesheets.
  228. *
  229. * Adds "media" attribute if not specified, and always uses "type" attribute
  230. * of "text/css".
  231. *
  232. * @param string The source href for the stylesheet.
  233. * @param array Additional attributes for the <style> tag.
  234. * @param boolean|string Absolute uri, force non-default protocol, eg.https
  235. * @return string The <link>...</link> tag.
  236. *
  237. */
  238. public static function style($href, array $attributes = array(), $absolute = false)
  239. {
  240. // Build the URI as an href to a public resource.
  241. $attributes['href'] = self::href($href, $absolute, true);
  242. // Force type="text/css" and rel="stylesheet".
  243. $attributes['type'] = 'text/css';
  244. $attributes['rel'] = 'stylesheet';
  245. // Default to media="screen".
  246. if (empty($attributes['media'])) {
  247. $attributes['media'] = 'screen';
  248. }
  249. // Build and return the tag.
  250. return '<link' . self::attributes($attributes) . ' />';
  251. }
  252. } // End Html class.