freenetis-github/application/helpers/html.php @ 19bccc86
8baed187 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/**
|
|||
* HTML helper class.
|
|||
*
|
|||
* $Id: html.php 1971 2008-02-06 21:55:46Z Shadowhand $
|
|||
*
|
|||
* @package Core
|
|||
* @author Kohana Team
|
|||
* @copyright (c) 2007-2008 Kohana Team
|
|||
* @license http://kohanaphp.com/license.html
|
|||
*/
|
|||
class html {
|
|||
/**
|
|||
* Convert special characters to HTML entities
|
|||
*
|
|||
* @param string string to convert
|
|||
* @param boolean encode existing entities
|
|||
* @return string
|
|||
*/
|
|||
public static function specialchars($str, $double_encode = TRUE)
|
|||
{
|
|||
if (is_array($str))
|
|||
return array_map ("html::specialchars", $str);
|
|||
// Do encode existing HTML entities (default)
|
|||
if ($double_encode == TRUE)
|
|||
{
|
|||
$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
|
|||
}
|
|||
else
|
|||
{
|
|||
// Do not encode existing HTML entities
|
|||
// From PHP 5.2.3 this functionality is built-in, otherwise use a regex
|
|||
if (version_compare(PHP_VERSION, '5.2.3', '>='))
|
|||
{
|
|||
$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', FALSE);
|
|||
}
|
|||
else
|
|||
{
|
|||
$str = preg_replace('/&(?!(?:#\d+|[a-z]+);)/i', '&', $str);
|
|||
$str = str_replace(array('<', '>', '\'', '"'), array('<', '>', ''', '"'), $str);
|
|||
}
|
|||
}
|
|||
return $str;
|
|||
}
|
|||
/**
|
|||
* Creates an HTTP query string from an array. Deprecated.
|
|||
*
|
|||
* @param array array of data to convert to string
|
|||
* @return string
|
|||
*/
|
|||
public static function query_string($array)
|
|||
{
|
|||
if (!is_array($array))
|
|||
{
|
|||
$array = array();
|
|||
}
|
|||
return http_build_query($array);
|
|||
}
|
|||
/**
|
|||
* Create HTML link anchors.
|
|||
*
|
|||
* @param string URL or URI string
|
|||
* @param string link text
|
|||
* @param array HTML anchor attributes
|
|||
* @param string non-default protocol, eg: https
|
|||
c1bdc1c4 | Michal Kliment | * @param $id_as_title if title is empty and link ends with ID, use ID or full uri as title?
|
|
8baed187 | Michal Kliment | * @return string
|
|
*/
|
|||
c1bdc1c4 | Michal Kliment | public static function anchor($uri, $title = FALSE, $attributes = FALSE,
|
|
$protocol = FALSE, $id_as_title = TRUE)
|
|||
8baed187 | Michal Kliment | {
|
|
if ($uri === '')
|
|||
{
|
|||
$site_url_modified = $site_url = url::base(FALSE);
|
|||
}
|
|||
else
|
|||
{
|
|||
if (strpos($uri, '://') === FALSE)
|
|||
{
|
|||
$site_url = url_lang::site($uri, FALSE, $protocol);
|
|||
}
|
|||
else
|
|||
{
|
|||
$site_url = $uri;
|
|||
}
|
|||
$site_url_modified = $site_url;
|
|||
if (strpos($site_url_modified, Path::QSNAME . '=') === FALSE)
|
|||
{
|
|||
$qs_var = Path::QSNAME . '=' . urlencode(url_lang::current());
|
|||
if (strpos($site_url_modified, '?') !== FALSE)
|
|||
{
|
|||
list ($uri_no_qs, $qs) = explode('?', $site_url_modified, 2);
|
|||
$queries = explode('&', $qs);
|
|||
array_push($queries, $qs_var);
|
|||
$qs = implode('&', $queries);
|
|||
$site_url_modified = $uri_no_qs . '?' . $qs;
|
|||
}
|
|||
else
|
|||
{
|
|||
$site_url_modified .= '?' . $qs_var;
|
|||
}
|
|||
}
|
|||
}
|
|||
// Title empty? Use the parsed URL or ID
|
|||
if (empty($title))
|
|||
{
|
|||
c1bdc1c4 | Michal Kliment | if ($id_as_title && mb_eregi('/([0-9]+)$', $site_url, $r))
|
|
8baed187 | Michal Kliment | {
|
|
$title = "<span style=\"color: red\">[$r[1]]</span>";
|
|||
}
|
|||
else
|
|||
{
|
|||
$title = $site_url;
|
|||
}
|
|||
}
|
|||
return
|
|||
// Parsed URL
|
|||
'<a href="'.html::specialchars($site_url_modified, FALSE).'"'
|
|||
// Attributes empty? Use an empty string
|
|||
.(empty($attributes) ? '' : html::attributes($attributes)).'>'
|
|||
// title
|
|||
. $title .'</a>';
|
|||
}
|
|||
/**
|
|||
* Creates an HTML anchor to a file.
|
|||
*
|
|||
* @param string name of file to link to
|
|||
* @param string link text
|
|||
* @param array HTML anchor attributes
|
|||
* @param string non-default protocol, eg: ftp
|
|||
* @return string
|
|||
*/
|
|||
public static function file_anchor($file, $title = FALSE, $attributes = FALSE, $protocol = FALSE)
|
|||
{
|
|||
return
|
|||
// Base URL + URI = full URL
|
|||
'<a href="'.html::specialchars(url::base(FALSE, $protocol).$file, FALSE).'"'
|
|||
// Attributes empty? Use an empty string
|
|||
.(empty($attributes) ? '' : html::attributes($attributes)).'>'
|
|||
// Title empty? Use the filename part of the URI
|
|||
.(empty($title) ? end(explode('/', $file)) : $title) .'</a>';
|
|||
}
|
|||
/**
|
|||
* Similar to anchor, but with the protocol parameter first.
|
|||
*
|
|||
* @param string link protocol
|
|||
* @param string URI or URL to link to
|
|||
* @param string link text
|
|||
* @param array HTML anchor attributes
|
|||
* @return string
|
|||
*/
|
|||
public static function panchor($protocol, $uri, $title = FALSE, $attributes = FALSE)
|
|||
{
|
|||
return html::anchor($uri, $title, $attributes, $protocol);
|
|||
}
|
|||
/**
|
|||
* Creates a email anchor.
|
|||
*
|
|||
* @param string email address to send to
|
|||
* @param string link text
|
|||
* @param array HTML anchor attributes
|
|||
* @return string
|
|||
*/
|
|||
public static function mailto($email, $title = FALSE, $attributes = FALSE)
|
|||
{
|
|||
// Remove the subject or other parameters that do not need to be encoded
|
|||
$subject = FALSE;
|
|||
if (strpos($email, '?') !== FALSE)
|
|||
{
|
|||
list ($email, $subject) = explode('?', $email);
|
|||
}
|
|||
$safe = '';
|
|||
foreach(str_split($email) as $i => $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;
|
|||
}
|
|||
}
|
|||
// Title defaults to the encoded email address
|
|||
$title = ($title == FALSE) ? $safe : $title;
|
|||
// URL encode the subject line
|
|||
$subject = ($subject == TRUE) ? '?'.rawurlencode($subject) : '';
|
|||
// Parse attributes
|
|||
$attributes = ($attributes == TRUE) ? html::attributes($attributes) : '';
|
|||
// Encoded start of the href="" is a static encoded version of 'mailto:'
|
|||
return '<a href="mailto:'.$safe.$subject.'"'.$attributes.'>'.$title.'</a>';
|
|||
}
|
|||
/**
|
|||
* Generate a "breadcrumb" list of anchors representing the URI.
|
|||
*
|
|||
* @param array segments to use as breadcrumbs, defaults to using Router::$segments
|
|||
* @return string
|
|||
*/
|
|||
public static function breadcrumb($segments = NULL)
|
|||
{
|
|||
empty($segments) and $segments = Router::$segments;
|
|||
$array = array();
|
|||
while ($segment = array_pop($segments))
|
|||
{
|
|||
$array[] = html::anchor
|
|||
(
|
|||
// Complete URI for the URL
|
|||
implode('/', $segments).'/'.$segment,
|
|||
// Title for the current segment
|
|||
ucwords(inflector::humanize($segment))
|
|||
);
|
|||
}
|
|||
// Retrun the array of all the segments
|
|||
return array_reverse($array);
|
|||
}
|
|||
/**
|
|||
* Creates a stylesheet link.
|
|||
*
|
|||
* @param string|array filename, or array of filenames to match to array of medias
|
|||
* @param string|array media type of stylesheet, or array to match filenames
|
|||
* @param boolean include the index_page in the link
|
|||
* @return string
|
|||
*/
|
|||
public static function stylesheet($style, $media = FALSE, $index = FALSE)
|
|||
{
|
|||
return html::link($style, 'stylesheet', 'text/css', '.css', $media, $index);
|
|||
}
|
|||
/**
|
|||
* Creates a link tag.
|
|||
*
|
|||
* @param string|array filename
|
|||
* @param string|array relationship
|
|||
* @param string|array mimetype
|
|||
* @param string specifies suffix of the file
|
|||
* @param string|array specifies on what device the document will be displayed
|
|||
* @param boolean include the index_page in the link
|
|||
* @return string
|
|||
*/
|
|||
public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE)
|
|||
{
|
|||
$compiled = '';
|
|||
if (is_array($href))
|
|||
{
|
|||
foreach($href as $_href)
|
|||
{
|
|||
$_rel = is_array($rel) ? array_shift($rel) : $rel;
|
|||
$_type = is_array($type) ? array_shift($type) : $type;
|
|||
$_media = is_array($media) ? array_shift($media) : $media;
|
|||
$compiled .= html::link($_href, $_rel, $_type, $suffix, $_media, $index);
|
|||
}
|
|||
}
|
|||
else
|
|||
{
|
|||
// Add the suffix only when it's not already present
|
|||
$suffix = ( ! empty($suffix) AND strpos($href, $suffix) === FALSE) ? $suffix : '';
|
|||
$media = empty($media) ? '' : ' media="'.$media.'"';
|
|||
$compiled = '<link rel="'.$rel.'" type="'.$type.'" href="'.url::base((bool) $index).$href.$suffix.'"'.$media.' />';
|
|||
}
|
|||
return $compiled."\n";
|
|||
}
|
|||
/**
|
|||
* Creates a script link.
|
|||
*
|
|||
* @param string|array filename
|
|||
* @param boolean include the index_page in the link
|
|||
* @return string
|
|||
*/
|
|||
public static function script($script, $index = FALSE)
|
|||
{
|
|||
$compiled = '';
|
|||
if (is_array($script))
|
|||
{
|
|||
foreach($script as $name)
|
|||
{
|
|||
$compiled .= html::script($name, $index);
|
|||
}
|
|||
}
|
|||
else
|
|||
{
|
|||
// Add the suffix only when it's not already present
|
|||
$suffix = (strpos($script, '.js') === FALSE) ? '.js' : '';
|
|||
$compiled = '<script type="text/javascript" src="'.url::base((bool) $index).$script.$suffix.'"></script>';
|
|||
}
|
|||
return $compiled."\n";
|
|||
}
|
|||
/**
|
|||
* Creates a script link.
|
|||
*
|
|||
* @param string|array filename
|
|||
* @param boolean include the index_page in the link
|
|||
* @return string
|
|||
*/
|
|||
public static function execute_script($script)
|
|||
{
|
|||
$compiled = '<script>';
|
|||
$compiled .= $script.'';
|
|||
$compiled = '</script>';
|
|||
return $compiled."\n";
|
|||
}
|
|||
/**
|
|||
* Creates a image link.
|
|||
*
|
|||
* @param string|array array of html attributes, or an image name
|
|||
* @param boolean include the index_page in the link
|
|||
* @return string
|
|||
*/
|
|||
public static function image($attr = NULL, $index = FALSE)
|
|||
{
|
|||
if ( ! is_array($attr))
|
|||
{
|
|||
$attr = array('src' => $attr);
|
|||
}
|
|||
if (strpos($attr['src'], '://') === FALSE)
|
|||
{
|
|||
// Make the src attribute into an absolute URL
|
|||
$attr['src'] = url::base($index).$attr['src'];
|
|||
}
|
|||
return '<img'.html::attributes($attr).' />';
|
|||
}
|
|||
/**
|
|||
* Compiles an array of HTML attributes into an attribute string.
|
|||
*
|
|||
* @param string|array array of attributes
|
|||
* @return string
|
|||
*/
|
|||
public static function attributes($attrs)
|
|||
{
|
|||
if (empty($attrs))
|
|||
return '';
|
|||
if (is_string($attrs))
|
|||
return ' '.$attrs;
|
|||
$compiled = '';
|
|||
foreach($attrs as $key => $val)
|
|||
{
|
|||
$compiled .= ' '.$key.'="'.$val.'"';
|
|||
}
|
|||
return $compiled;
|
|||
}
|
|||
/**
|
|||
* Menu item counter for view menu
|
|||
*
|
|||
* @author Ondřej Fibich
|
|||
* @param integer $count
|
|||
* @param string $color
|
|||
*/
|
|||
public static function menu_item_counter($count, $color = NULL)
|
|||
{
|
|||
echo '<span class="menu_item_counter ' . ($count ? 'rbg' : '') . '"'
|
|||
. ((!empty($color) && $count) ? " style=\"background-color: $color\"" : "")
|
|||
. '>' . $count . '</span>';
|
|||
}
|
|||
} // End html
|