/system/libraries/Html.php
PHP | 282 lines | 124 code | 29 blank | 129 comment | 18 complexity | ef174fc4c6999270133ef4dec2c2af46 MD5 | raw file
- <?php
- /**
- * This file is part of the Yet Another PHP Framework package.
- * (c) 2010 Adrian Videnie <{@link mailto:avidenie@gmail.com avidenie@gmail.com}>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- * @package Framework
- * @subpackage Helpers
- * @author Adrian Videnie <avidenie@gmail.com>
- * @copyright 2010 Adrian Videnie
- * @license http://www.opensource.org/licenses/mit-license.php The MIT license
- */
- /**
- *
- * HTML helper class.
- *
- * @package Framework
- * @subpackage Helpers
- * @author Adrian Videnie <avidenie@gmail.com>
- * @copyright 2010 Adrian Videnie
- * @license http://www.opensource.org/licenses/mit-license.php The MIT license
- */
- class Html
- {
- /**
- * Convert special characters to HTML entities
- *
- * @param string string to convert
- * @param boolean encode existing entities
- * @return string
- */
- public static function escape($string, $doubleEncode = true)
- {
- return htmlspecialchars((string) $string, ENT_QUOTES, 'UTF-8', $doubleEncode);
- }
- /**
- * Compiles an array of HTML attributes into an attribute string.
- *
- * @param string|array array of attributes
- * @return string
- */
- public static function attributes($attributes)
- {
- if (empty($attributes)) {
- return '';
- }
- if (is_string($attributes)) {
- return ' ' . trim($attributes);
- }
- $compiled = '';
- foreach ($attributes as $key => $value) {
- $compiled .= ' ' . $key . '="' . $value . '"';
- }
- return $compiled;
- }
- /**
- * Fix generated URIs.
- *
- * @param string URI string.
- * @param mixed Force absolute URI or force non-default protocol, eg.https.
- * @param boolean Whether this is a public resource or not.
- * @return string The fixed, transformed URI.
- */
- public static function href($uri, $absolute = false, $publicResource = false)
- {
- if (false === strpos($uri, '://')) {
- // Make sure the uri is in a proper form.
- $uri = '/' . ltrim($uri, '/');
- // The Request method to use to generate the Uri.
- $method = 'baseUri';
- // Change method and add the path to the public directory.
- if (true == $publicResource) {
- $method = 'basePath';
- $uri = Green::config('framework.public_path') . $uri;
- }
- $request = Request::instance();
- // Create the Uri.
- $uri = $request->$method() . $uri;
- if (false !== $absolute) {
- $uri = $request->hostname($absolute) . $uri;
- }
- }
- return $uri;
- }
- /**
- * Generates an uri given the name of a route.
- *
- * @param mixed $name The name of a Route to use. If null it will use the current Route.
- * @param array $urlOptions Options passed to the assemble method of the Route object.
- * @param boolean $absolute Whether or not to return an absolute URI.
- * @param boolean $reset Whether or not to reset the route defaults with those provided.
- * @return string Url for the link href attribute.
- */
- public static function uri($name = null, array $urlOptions = array(), $absolute = false, $reset = true, $encode = true)
- {
- $router = Router::instance();
- $uri = $router->assemble($urlOptions, $name, $reset, $encode);
- return self::href($uri, $absolute);
- }
- /**
- * Generates an obfuscated version of an email address.
- *
- * @param string E-mail address
- * @return string Obfuscated version of the e-mail address
- */
- public static function email($email)
- {
- $safe = '';
- foreach (str_split($email) as $letter) {
- switch (($letter === '@') ? rand(1, 2) : rand(1, 3)) {
- // HTML entity code
- case 1: $safe .= '&#'.ord($letter).';'; break;
- // Hex character code
- case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
- // Raw (no) encoding
- case 3: $safe .= $letter;
- }
- }
- return $safe;
- }
- /**
- * Creates a image link.
- *
- * @param string Image source.
- * @param string|array Image alt attribute, or an array of attributes
- * @param boolean|string Absolute uri, force non-default protocol, eg.https
- * @return string The image link.
- */
- public static function image($src, $alt = null, $absolute = false)
- {
- // Create attribute list
- $attributes = array('src' => self::href($src, $absolute, true));
-
- if (is_array($alt)) {
- $attributes += $alt;
- } elseif (!empty($alt)) {
- // Add alt to attributes
- $attributes['alt'] = htmlspecialchars((string) $alt, ENT_QUOTES, 'UTF-8');
- }
- if (!isset($attributes['alt'])) {
- // Calculate a default alt.
- $attributes['alt'] = basename($attributes['src'], substr($attributes['src'], strrpos($attributes['src'], '.')));
- }
- return '<img' . self::attributes($attributes) . ' />';
- }
- /**
- * Creates an email anchor.
- *
- * @param string E-mail address to send to
- * @param string Link text
- * @param array HTML anchor attributes
- * @return string The e-mail anchor
- */
- public static function mailto($email, $title = null, $attributes = null)
- {
- // Remove the subject or other parameters that do not need to be encoded
- if (false !== strpos($email, '?')) {
- // Extract the parameters from the email address
- list ($email, $params) = explode('?', $email, 2);
- // Make the params into a query string, replacing spaces
- $params = '?' . str_replace(' ', '%20', $params);
- } else {
- // No parameters
- $params = '';
- }
- // Obfuscate email address
- $safe = self::email($email);
- // Title defaults to the encoded email address
- if (empty($title)) {
- $title = $safe;
- } else {
- $title = htmlspecialchars((string) $title, ENT_QUOTES, 'UTF-8');
- }
- // Parse attributes
- if (!empty($attributes)) {
- $attributes = self::attributes($attributes);
- }
- // Encoded start of the href="" is a static encoded version of 'mailto:'
- return '<a href="mailto:' . $safe . $params . '"' . $attributes . '>' . $title . '</a>';
- }
-
- /**
- * Returns a <script></script> tag.
- *
- * @param string The source href for the script.
- * @param array Additional attributes for the <script> tag.
- * @param mixed Whether to force absolute URI or a non-default protocol, eg.https.
- * @return string The <script></script> tag.
- */
- public static function script($src, array $attributes = array(), $absolute = false)
- {
- // Create a proper relative or absolute URI, as requested.
- $attributes['src'] = self::href($src, $absolute, true);
-
- if (isset($attributes['anti_cache'])) {
- $attributes['src'] .= '?' . date('U');
- unset($attributes['anti_cache']);
- }
-
- if (empty($attributes['type'])) {
- $attributes['type'] = 'text/javascript';
- }
-
- return '<script' . self::attributes($attributes) . '></script>';
- }
- /**
- * Returns a <script></script> block that properly commented for inclusion
- * in XHTML documents.
- *
- * @see http://developer.mozilla.org/en/docs/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents
- * @param string The source of the script.
- * @param array Additional attributes for the <script> tag.
- * @return string The <script></script> tag with the inline script.
- */
- public static function inlineScript($code, array $attributes = array())
- {
- // Make sure we don't accidentally write a source script href.
- unset($attributes['src']);
-
- if (empty($attributes['type'])) {
- $attributes['type'] = 'text/javascript';
- }
-
- return '<script'
- . self::attributes($attributes) . ">\n"
- . "//<![CDATA[\n"
- . trim($code)
- . "\n//]]>\n"
- . "</script>\n";
- }
- /**
- * Returns a <link>...</link> tag for external stylesheets.
- *
- * Adds "media" attribute if not specified, and always uses "type" attribute
- * of "text/css".
- *
- * @param string The source href for the stylesheet.
- * @param array Additional attributes for the <style> tag.
- * @param boolean|string Absolute uri, force non-default protocol, eg.https
- * @return string The <link>...</link> tag.
- *
- */
- public static function style($href, array $attributes = array(), $absolute = false)
- {
- // Build the URI as an href to a public resource.
- $attributes['href'] = self::href($href, $absolute, true);
-
- // Force type="text/css" and rel="stylesheet".
- $attributes['type'] = 'text/css';
- $attributes['rel'] = 'stylesheet';
-
- // Default to media="screen".
- if (empty($attributes['media'])) {
- $attributes['media'] = 'screen';
- }
-
- // Build and return the tag.
- return '<link' . self::attributes($attributes) . ' />';
- }
- } // End Html class.