1 <?php defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: html.php 4376 2009-06-01 11:40:39Z samsoir $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
14 // Enable or disable automatic setting of target="_blank"
15 public static $windowed_urls = FALSE;
18 * Convert special characters to HTML entities
20 * @param string string to convert
21 * @param boolean encode existing entities
24 public static function specialchars($str, $double_encode = TRUE)
26 // Force the string to be a string
29 // Do encode existing HTML entities (default)
30 if ($double_encode === TRUE)
32 $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
36 // Do not encode existing HTML entities
37 // From PHP 5.2.3 this functionality is built-in, otherwise use a regex
38 if (version_compare(PHP_VERSION, '5.2.3', '>='))
40 $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', FALSE);
44 $str = preg_replace('/&(?!(?:#\d++|[a-z]++);)/ui', '&', $str);
45 $str = str_replace(array('<', '>', '\'', '"'), array('<', '>', ''', '"'), $str);
53 * Perform a html::specialchars() with additional URL specific encoding.
55 * @param string string to convert
56 * @param boolean encode existing entities
59 public static function specialurlencode($str, $double_encode = TRUE)
61 return str_replace(' ', '%20', html::specialchars($str, $double_encode));
65 * Create HTML link anchors.
67 * @param string URL or URI string
68 * @param string link text
69 * @param array HTML anchor attributes
70 * @param string non-default protocol, eg: https
71 * @param boolean option to escape the title that is output
74 public static function anchor($uri, $title = NULL, $attributes = NULL, $protocol = NULL, $escape_title = FALSE)
78 $site_url = url::base(FALSE);
80 elseif (strpos($uri, '#') === 0)
82 // This is an id target link, not a URL
85 elseif (strpos($uri, '://') === FALSE)
87 $site_url = url::site($uri, $protocol);
91 if (html::$windowed_urls === TRUE AND empty($attributes['target']))
93 $attributes['target'] = '_blank';
101 '<a href="'.html::specialurlencode($site_url, FALSE).'"'
102 // Attributes empty? Use an empty string
103 .(is_array($attributes) ? html::attributes($attributes) : '').'>'
104 // Title empty? Use the parsed URL
105 .($escape_title ? html::specialchars((($title === NULL) ? $site_url : $title), FALSE) : (($title === NULL) ? $site_url : $title)).'</a>';
109 * Creates an HTML anchor to a file.
111 * @param string name of file to link to
112 * @param string link text
113 * @param array HTML anchor attributes
114 * @param string non-default protocol, eg: ftp
117 public static function file_anchor($file, $title = NULL, $attributes = NULL, $protocol = NULL)
120 // Base URL + URI = full URL
121 '<a href="'.html::specialurlencode(url::base(FALSE, $protocol).$file, FALSE).'"'
122 // Attributes empty? Use an empty string
123 .(is_array($attributes) ? html::attributes($attributes) : '').'>'
124 // Title empty? Use the filename part of the URI
125 .(($title === NULL) ? end(explode('/', $file)) : $title) .'</a>';
129 * Similar to anchor, but with the protocol parameter first.
131 * @param string link protocol
132 * @param string URI or URL to link to
133 * @param string link text
134 * @param array HTML anchor attributes
137 public static function panchor($protocol, $uri, $title = NULL, $attributes = FALSE)
139 return html::anchor($uri, $title, $attributes, $protocol);
143 * Create an array of anchors from an array of link/title pairs.
145 * @param array link/title pairs
148 public static function anchor_array(array $array)
151 foreach ($array as $link => $title)
153 // Create list of anchors
154 $anchors[] = html::anchor($link, $title);
160 * Generates an obfuscated version of an email address.
162 * @param string email address
165 public static function email($email)
168 foreach (str_split($email) as $letter)
170 switch (($letter === '@') ? rand(1, 2) : rand(1, 3))
173 case 1: $safe .= '&#'.ord($letter).';'; break;
174 // Hex character code
175 case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
177 case 3: $safe .= $letter;
185 * Creates an email anchor.
187 * @param string email address to send to
188 * @param string link text
189 * @param array HTML anchor attributes
192 public static function mailto($email, $title = NULL, $attributes = NULL)
197 // Remove the subject or other parameters that do not need to be encoded
198 if (strpos($email, '?') !== FALSE)
200 // Extract the parameters from the email address
201 list ($email, $params) = explode('?', $email, 2);
203 // Make the params into a query string, replacing spaces
204 $params = '?'.str_replace(' ', '%20', $params);
212 // Obfuscate email address
213 $safe = html::email($email);
215 // Title defaults to the encoded email address
216 empty($title) and $title = $safe;
219 empty($attributes) or $attributes = html::attributes($attributes);
221 // Encoded start of the href="" is a static encoded version of 'mailto:'
222 return '<a href="mailto:'.$safe.$params.'"'.$attributes.'>'.$title.'</a>';
226 * Generate a "breadcrumb" list of anchors representing the URI.
228 * @param array segments to use as breadcrumbs, defaults to using Router::$segments
231 public static function breadcrumb($segments = NULL)
233 empty($segments) and $segments = Router::$segments;
236 while ($segment = array_pop($segments))
238 $array[] = html::anchor
240 // Complete URI for the URL
241 implode('/', $segments).'/'.$segment,
242 // Title for the current segment
243 ucwords(inflector::humanize($segment))
247 // Retrun the array of all the segments
248 return array_reverse($array);
252 * Creates a meta tag.
254 * @param string|array tag name, or an array of tags
255 * @param string tag "content" value
258 public static function meta($tag, $value = NULL)
263 foreach ($tag as $t => $v)
265 // Build each tag and add it to the array
266 $tags[] = html::meta($t, $v);
269 // Return all of the tags as a string
270 return implode("\n", $tags);
273 // Set the meta attribute value
274 $attr = in_array(strtolower($tag), Kohana::config('http.meta_equiv')) ? 'http-equiv' : 'name';
276 return '<meta '.$attr.'="'.$tag.'" content="'.$value.'" />';
280 * Creates a stylesheet link.
282 * @param string|array filename, or array of filenames to match to array of medias
283 * @param string|array media type of stylesheet, or array to match filenames
284 * @param boolean include the index_page in the link
287 public static function stylesheet($style, $media = FALSE, $index = FALSE)
289 return html::link($style, 'stylesheet', 'text/css', '.css', $media, $index);
293 * Creates a link tag.
295 * @param string|array filename
296 * @param string|array relationship
297 * @param string|array mimetype
298 * @param string specifies suffix of the file
299 * @param string|array specifies on what device the document will be displayed
300 * @param boolean include the index_page in the link
303 public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE)
309 foreach ($href as $_href)
311 $_rel = is_array($rel) ? array_shift($rel) : $rel;
312 $_type = is_array($type) ? array_shift($type) : $type;
313 $_media = is_array($media) ? array_shift($media) : $media;
315 $compiled .= html::link($_href, $_rel, $_type, $suffix, $_media, $index);
320 if (strpos($href, '://') === FALSE)
322 // Make the URL absolute
323 $href = url::base($index).$href;
326 $length = strlen($suffix);
328 if ( $length > 0 AND substr_compare($href, $suffix, -$length, $length, FALSE) !== 0)
330 // Add the defined suffix
341 if ( ! empty($media))
343 // Add the media type to the attributes
344 $attr['media'] = $media;
347 $compiled = '<link'.html::attributes($attr).' />';
350 return $compiled."\n";
354 * Creates a script link.
356 * @param string|array filename
357 * @param boolean include the index_page in the link
360 public static function script($script, $index = FALSE)
364 if (is_array($script))
366 foreach ($script as $name)
368 $compiled .= html::script($name, $index);
373 if (strpos($script, '://') === FALSE)
375 // Add the suffix only when it's not already present
376 $script = url::base((bool) $index).$script;
379 if (substr_compare($script, '.js', -3, 3, FALSE) !== 0)
381 // Add the javascript suffix
385 $compiled = '<script type="text/javascript" src="'.$script.'"></script>';
388 return $compiled."\n";
392 * Creates a image link.
394 * @param string image source, or an array of attributes
395 * @param string|array image alt attribute, or an array of attributes
396 * @param boolean include the index_page in the link
399 public static function image($src = NULL, $alt = NULL, $index = FALSE)
401 // Create attribute list
402 $attributes = is_array($src) ? $src : array('src' => $src);
408 elseif ( ! empty($alt))
410 // Add alt to attributes
411 $attributes['alt'] = $alt;
414 if (strpos($attributes['src'], '://') === FALSE)
416 // Make the src attribute into an absolute URL
417 $attributes['src'] = url::base($index).$attributes['src'];
420 return '<img'.html::attributes($attributes).' />';
424 * Compiles an array of HTML attributes into an attribute string.
426 * @param string|array array of attributes
429 public static function attributes($attrs)
434 if (is_string($attrs))
438 foreach ($attrs as $key => $val)
440 $compiled .= ' '.$key.'="'.html::specialchars($val).'"';