summaryrefslogtreecommitdiff
path: root/html.class
diff options
context:
space:
mode:
authorUrban Müller2007-07-26 13:02:24 +0000
committerUrban Müller2007-07-26 13:02:24 +0000
commit806a5297e7e99d455b97a4f0acaba2f40f470584 (patch)
treeb9fc43ef227da87d873cf3676c08c49fa0dc1240 /html.class
parentc3cba034c8009b65c25dd4ef5f54b18d9c8ee7d4 (diff)
downloaditools-806a5297e7e99d455b97a4f0acaba2f40f470584.tar.gz
itools-806a5297e7e99d455b97a4f0acaba2f40f470584.tar.bz2
itools-806a5297e7e99d455b97a4f0acaba2f40f470584.zip
renamed files for autoloader
Diffstat (limited to 'html.class')
-rw-r--r--html.class551
1 files changed, 0 insertions, 551 deletions
diff --git a/html.class b/html.class
deleted file mode 100644
index 3834251..0000000
--- a/html.class
+++ /dev/null
@@ -1,551 +0,0 @@
-<?php
-/*
-** $Id$
-**
-** ITools - the Internet Tools Library
-**
-** Copyright (C) 1995-2003 by the ITools Authors.
-** This program is free software; you can redistribute it and/or
-** modify it under the terms of either the GNU General Public License
-** or the GNU Lesser General Public License, as published by the Free
-** Software Foundation. See http://www.gnu.org/licenses/ for details.
-**
-** UltraHTML 3000 tool layer. Create functions for html tags.
-**
-** new it_html;
-** echo h1('hello.php') . div('contents', 'Hello world!');
-**/
-
-class it_html
-{
- # Default configuration of html class
- var $_defaultconfig = array
- (
- 'name' => 'it_html', # Name of global variable to use
- 'oldhtml' => false,
- 'prettyprint' => false,
- 'tags' => 'a,br,form,h1,h2,h3,h4,input,li,meta,table,td,th,tr,ul',
- 'moretags' => '',
- 'nonewlinetags' => 'a,img,span',
- 'preprocess_attr' => array(),
- 'charset' => "iso-8859-1",
- 'ie_png_fix' => false, # To enable, supply URL of a transparent gif (like /images/0.gif)
- 'show_content_type' => true,
- 'show_favicon' => true,
- 'show_boot_dom' => true,
- 'staticallycallable' => "q,u,select", # Those methods are statically callable (have same arguments as global stubs) but are a bit slower
- 'notexported' => "sanitize", # Those methods are not exported
- );
- var $tags_seen = array('body' => true); # body always counts as seen
- var $_hasnonewline = array();
-
-/**
- * Create a HTML object and global functions for all methods (exlcluding
- * methods starting with '_') in this class plus the default tags (see below).
- *
- * @param $config Configuration settings:
- * name => Name of global variable $this is assigned to (string)
- * prettyprint => Should output be indented? (bool)
- * oldhtml => Should output be in old-style html? (bool)
- * tags => Comma-separated list of default tag-functions to generate (string)
- * moretags => Comma-separated list of tag-functions to generate additionally to 'tags' (string)
- * nonewlinetags => Comma-separated list of tags that do not like newlines before/after them
- * use_it_state => If true, generate code needed by state.js (aka 'history iframe')
- */
-function it_html($config = array())
-{
- # Create current setting vars
- foreach ($config + $this->_defaultconfig as $key => $value)
- {
- $var = "_$key";
- $this->$var = $value;
- }
-
- if (isset($this->_oldhtml) && !$this->_htmltype)
- $this->_htmltype = $this->_oldhtml ? "html" : "xhtml";
- $this->_oldhtml = $this->_htmltype == "html";
- $this->_hasnonewline = array_flip(explode(',', "dummy," . $this->_nonewlinetags)); # dummy keeps values >0
- $notexported = array_flip(explode(',', "dummy," . $this->_notexported)); # dummy keeps values >0
-
- # Create global functions for _tags
- foreach (array_merge(explode(',', $this->_tags), explode(',', $this->_moretags)) as $func)
- {
- if (!function_exists($func) && $func)
- $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['$this->_name']->_tag('$func', \$args);}";
- }
-
- # Create global functions for it_html methods
- foreach (get_class_methods(get_class($this)) as $func)
- {
- if (!preg_match('/^_/', $func) && !is_a($this, $func) && $func && !function_exists($func) && !$notexported[$func])
- $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['$this->_name']->$func(\$args); }";
- }
-
- # Create global functions for methods that are statically callable (have same arguments as global stubs)
- foreach (explode(",", $this->_staticallycallable) as $func)
- {
- if ($func && !function_exists($func))
- $code[$func] = "function $func() { \$args = func_get_args(); return call_user_func_array(array(&\$GLOBALS['$this->_name'], '$func'), \$args); }";
- }
-
- eval(join("", (array)$code));
-
- # Since name is given as param, it is our duty to store it, not our caller's.
- $GLOBALS[$this->_name] =& $this;
-}
-
-
-/**
- * Return a <tag> containing optional data.
- * @param $name tag name ('h1', 'div' etc.)
- * @param ... any number optional data or array of key => value arguments
- * @return string containing XML/HTML tag
- */
-function tag($args)
-{
- $name = array_shift($args);
-
- return $this->_tag($name, $args);
-}
-
-/**
- * Internal: Create html tag from name and args array
- */
-function _tag($name, $args)
-{
- $data = null;
- $attr = array();
-
- foreach($args as $arg)
- {
- if (is_array($arg))
- {
- foreach ($arg as $key => $value)
- {
- if (is_int($key))
- $data .= $value;
- else if ($this->_preprocess_attr[$key])
- $attr[$key] = call_user_func(($this->_preprocess_attr[$key]), $value, $name);
- else
- $attr[$key] = $value;
- }
- }
- else
- $data .= $arg;
- }
-
- $newline = $this->_hasnonewline[$name] ? "" : "\n";
-
- # Ultra XML PrettyPrinter 3000 [\] by SCA
- if ($this->_prettyprint && $newline && (substr($data, -1, 1) == "\n") && (strpos($data, '<textarea') === false) && ($data != strip_tags($data)))
- $data = str_replace("\n", "\n ", "\n".trim($data))."\n";
-
- $result = "<$name";
-
- # add attributes. If $value === true, use key only (<td nowrap> instead of <td nowrap=""> for old html, <td nowrap="nowrap"> for xhtml style)
- foreach($attr as $key => $value)
- {
- if (($value === null) || ($value === false)) # null or false: omit whole tag
- ;
- else if (isset($value) && $value !== true) # normal case: value
- $result .= " $key=\"" . (preg_match("/[<>&\"'\n\x80-\x9f]/", $value) ? str_replace("\n", "&#10;", it_html::Q($value)) : $value) . '"';
- else # true: tag without value
- $result .= $this->_oldhtml ? " $key" : " $key=\"$key\"";
- }
-
- # Apply a kind of magic... this needs further investigation
- if (isset($data) || preg_match('/^(a|div|iframe|script|span|td|textarea)$/i', $name))
- $result .= ">$data</$name>$newline";
- elseif ($this->_oldhtml)
- $result .= ">$newline";
- else
- $result .= " />$newline";
-
- if ($GLOBALS['debug_srclines'])
- {
- $trace = debug_backtrace();
- $trace = $trace[2];
- $result = "<!-- " . basename($trace['file']) . ":" . $trace['line'] . " -->" . $result;
- }
-
- $this->tags_seen[$name] = true;
-
- return $result;
-}
-
-
-/**
- * Shortcut: return a div of a specific class
- * @param $class class name or null for no class= tag
- * @param ... any number optional data or array of key => value arguments
- * @return <div class="$class"...>...</div>
- */
-function div($args)
-{
- if (!is_array($args[0]) && ($class = array_shift($args)) !== null)
- array_unshift($args, compact('class'));
-
- return $this->_tag("div", $args);
-}
-
-
-/**
- * Special img() function patches png transparency for IE 5.5-6 if ie_png_fix is set
- * @param ... any number optional data or array of key => value arguments
- * @return <img ... />
- */
-function img($args)
-{
- if ($this->_ie_png_fix && preg_match('/MSIE [56]/', $_SERVER['HTTP_USER_AGENT']))
- {
- foreach($args as $id => $arg)
- if (preg_match('/\.png(\?.*)?$/', $arg['src']))
- {
- $args[$id]['style'] = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='{$arg['src']}',sizingMethod='scale');" . $arg['style'];
- $args[$id]['src'] = $this->_ie_png_fix;
- }
- }
-
- return $this->_tag("img", $args);
-}
-
-
-/**
- * Shortcut: return a span of a specific class
- * @param $class class name or null for no class= tag
- * @param ... any number optional data or array of key => value arguments
- * @return <span class="$class"...>...</span>
- */
-function span($args)
-{
- if (!is_array($args[0]) && ($class = array_shift($args)) !== null)
- array_unshift($args, compact('class'));
-
- return $this->_tag("span", $args);
-}
-
-
-/**
- * Return HTML with all evil things stripped. Allowed are a coupld of simple
- * tags like div, p, i, b, br without attributes, a with absolute href,
- * img with absolute src url. Also ensures that tags are balanced.
- * @param $html HTML string to be sanitized
- * @return Sanitized HTML
- */
-function sanitize($html)
-{
- $result = "";
- $html = it::replace(array('[\0\n\r\s]+' => " "), $html);
- $urlpattern = 'https?://[^">]+';
-
- if ($tag = it::match("(.*)<(div|p|i|b)[^>]*>(.*?)</\\2>(.*)", $html))
- {
- # Simple tags with content, no attributes kept
- list($head, $tagname, $content, $tail) = $tag;
- $result .= it_html::sanitize($head) . "<$tagname>" . it_html::sanitize($content) . "</$tagname>" . it_html::sanitize($tail);
- }
- else if ($tag = it::match('(.*)<a[^>]+?href="(' . $urlpattern . ')"[^>]*?>(.*?)</a>(.*)', $html))
- {
- # Link tags, keeps only href attribute
- list($head, $href, $content, $tail) = $tag;
- $result .= it_html::sanitize($head) . '<a href="' . it_html::Q(html_entity_decode($href)) . '">' . it_html::sanitize($content) . "</a>" . it_html::sanitize($tail);
- }
- else if ($tag = it::match('(.*)<img[^>]+?src="(' . $urlpattern . ')"[^>]*?>(.*)', $html))
- {
- # Image tags, keeps only src attribute
- list($head, $src, $tail) = $tag;
- $result .= it_html::sanitize($head) . '<img src="' . it_html::Q(html_entity_decode($src)) . '" alt="" />' . it_html::sanitize($tail);
- }
- else if ($tag = it::match("(.*)<(br)[^>]*>(.*)", $html))
- {
- # Simple tags without content, no attributes kept
- list($head, $tagname, $tail) = $tag;
- $result .= it_html::sanitize($head) . "<$tagname />" . it_html::sanitize($tail);
- }
- else
- $result = it_html::Q(it::replace('&#\d+;' => "", html_entity_decode(strip_tags($html))));
-
- return $result;
-}
-
-
-/**
- * Shortcut: return htmlspecialchars($string);
- * @param $string String to encode with htmlspecialchars()
- * @return htmlspecialchars($string)
- */
-function q($string)
-{
- if ($GLOBALS['it_html']->_charset == "iso-8859-1")
- $string = preg_replace('/[\x80-\x9f]/', ' ', strtr($string, array("\x80"=>"EUR", "\x82"=>"'", "\x84"=>"\"", "\x85"=>"...", "\x8a"=>"S", "\x8c"=>"OE", "\x8e"=>"Z", "\x91"=>"'", "\x92"=>"'", "\x93"=>"\"", "\x94"=>"\"", "\x96"=>"-", "\x97"=>"-", "\x9a"=>"s", "\x9e"=>"z")));
-
- return htmlspecialchars($string);
-}
-
-
-/**
- * Build a complete url from base-url and params
- * @param ... scalar args and numeric indices build base-url, rest as params
- */
-function u(/* ... */)
-{
- $args = func_get_args();
- $base = null;
- $params = array();
-
- foreach($args as $arg)
- {
- if (is_array($arg))
- {
- foreach ($arg as $key => $value)
- {
- if (is_int($key))
- $base .= $value;
- else
- $params[$key] = $value;
- }
- }
- else
- $base .= $arg;
- }
-
- if (!isset($base))
- $base = $_SERVER['PHP_SELF'];
-
- $base = preg_replace('|\0|', '', $base);
- $base = preg_replace('|[^\w.+!*(),:@&=/~$-]|e', 'urlencode("$0")', $base);
- $base = preg_replace('|^(\w+:)?//[^/]*$|', '$0/', $base); # Add slash if absolute url without a path, e.g. http://gna.ch
- $queryparams = it_url::params($params);
-
- return $base . ($queryparams ? "?$queryparams" : "");
-}
-
-
-/**
- * Create a dropdown menu object. Warning: encodes html code within options!
- * @param $tags key => value pairs of <select> tag
- * @param $options array (value => text) of available options or
- * string key:val{,key:val} where key will be rawurldecoded so it may contain %2C as comma
- * supports optgroups as array (value => optgroup => array(value => text))
- * @param $selected optional currently selected value
- */
-function select($tags, $options, $selected = null)
-{
- # Transmogrify key:val{,key:val} to array(key => val)
- if (!is_array($options))
- {
- $opts = explode(',', $options);
- $options = array();
-
- foreach($opts as $opt)
- {
- list($key, $value) = explode(':', $opt);
- $options[rawurldecode($key)] = $value;
- }
- }
-
- $html = "";
- foreach($options as $value => $option)
- {
- if (is_array($option))
- {
- $grouphtml = "";
- foreach($option as $optval => $opt)
- $grouphtml .= $this->_tag("option", array(array('value' => $optval, 'selected' => isset($selected) ? $optval == $selected : false), it_html::Q($opt)));
- $html .= $this->_tag("optgroup", array(array('label' => $value, $grouphtml)));
- }
- else
- $html .= $this->_tag("option", array(array('value' => $value, 'selected' => isset($selected) ? $value == $selected : false, 'disabled' => $option === ""), (trim($option) === "") ? "&nbsp;" : it_html::Q($option)));
- }
-
- return $this->_tag("select", array($tags, $html));
-}
-
-
-/**
- * Insert a javascript script
- * @param ... any number optional data or array of key => value arguments
- * @return <script type="text/javascript"...>...</script>
- */
-function js($args)
-{
- if (!$this->_oldhtml && isset($args[0]))
- {
- array_unshift($args, "<!--//--><![CDATA[//><!--\n");
- $args[] = "\n//--><!]]>";
- }
-
- array_unshift($args, array('type' => 'text/javascript'));
- return $this->_tag("script", $args);
-}
-
-
-/**
- * Include javascript code or generate HTML to include it
- */
-function _itjs($files, $mode)
-{
- $result = "";
-
- if ($files)
- {
- $filenames = itjs::filenames($files);
-
- if ($mode == "files")
- {
- foreach ($filenames as $file)
- $result .= tag('script', array('type' => "text/javascript", 'src' => "/itjs/" . basename($file) . "?v=" . itjs::checksum($file)));
- }
- else if ($mode == "inline")
- {
- $jsfile = "";
-
- foreach ($filenames as $file)
- $jsfile .= @file_get_contents($file);
-
- $result .= itjs::strip($jsfile);
- }
- else
- $result .= tag('script', array('type' => "text/javascript", 'src' => "/itjs/$files?v=" . itjs::checksum($filenames)));
- }
-
- return $result;
-}
-
-
-/**
- * Return HTML header with correct doctype.
- *
- * @param any number of text args or array of key => value:
- * 'title' optional HTML title tag
- * 'content-type' optional content type (default: "text/html; charset=iso-8859-1")
- * 'description' optional data for <meta name="description"> tag
- * 'doctype' optional <!DOCTYPE HTML PUBLIC...> tag
- * 'keywords' optional data for <meta name="keywords"> tag
- * 'lang' optional language (defaults to 'de')
- * 'stylesheets' optional array mediatype => url of styleshests
- */
-function head($args = array())
-{
- if (!$this->head_sent++)
- {
- $p = array();
-
- foreach ($args as $arg)
- {
- if (is_array($arg))
- {
- foreach ($arg as $key => $value)
- {
- if (is_int($key))
- $data .= $value;
- else
- $p[$key] = $value;
- }
- }
- else
- $data .= $arg;
- }
-
- if ($this->_htmltype == "xhtml-mobile")
- $defdoctype = '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">';
- else if ($this->_htmltype == "xhtml")
- $defdoctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
- else
- $defdoctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
-
- $p += array
- (
- 'content-type' => "text/html; charset=$this->_charset",
- 'doctype' => $defdoctype,
- 'lang' => "de",
- 'js' => $this->_js,
- 'jsboot' => $this->_jsboot,
- 'jsinline' => $this->_jsinline,
- );
-
- $header = $p['show_content_type'] ? meta(array('http-equiv' => "Content-Type", 'content' => $p['content-type'])) : "";
-
- if (isset($p['title']))
- $header .= tag('title', it_html::Q($p['title']));
-
- foreach(array('description', 'keywords') as $name)
- if (!empty($p[$name]))
- $header .= meta(array('name' => $name, 'content' => $p[$name]));
-
- # Add favicon if file exists
- if ($p['show_favicon'] && @file_exists($_SERVER['DOCUMENT_ROOT'].'/favicon.ico'))
- $header .= tag('link', array('rel' => "shortcut icon", 'href' => "/favicon.ico"));
-
- foreach((array)$p['stylesheets'] as $type => $url)
- $header .= tag('link', array('rel' => "stylesheet", 'type' => "text/css", 'href' => $url) + (is_int($type) ? array() : array('media' => $type)));
-
- if (!empty($p['cssinline']))
- $header .= tag('style', array('type' => "text/css", "\n" . preg_replace(array('/\s*\/\*[^\*]+\*\//Um', '/\s*\{\s*/', '/;\s+/'), array('', '{', ';'), $p['cssinline'])));
-
- if($this->_htmltype == "xhtml-mobile" && ereg('W3C_Validator', $_SERVER['HTTP_USER_AGENT']))
- header("Content-Type: application/xhtml+xml; charset=$this->_charset"); # for validation
- else if (!headers_sent()) # prevent warnings when ED() in use
- header("Content-Type: " . $p['content-type']);
-
- $langvar = $this->_htmltype == "xhtml-mobile" ? "xml:lang" : "lang";
- $prefix = $GLOBALS['SENT_PREAMBLE'] ? "" : $p['doctype'] . "\n<html $langvar=\"" . it_html::Q($p['lang']) . "\"><head>";
-
- # override use_it_state property
- if (isset($p['use_it_state']))
- $this->_use_it_state = $p['use_it_state'];
-
- $js = "";
-
- if ($p['jsenv'])
- $js .= "var env = " . itjs::serialize($p['jsenv']) . ";\n";
-
- if ($p['js'])
- {
- $js .= $this->_itjs("boot.js", "inline");
- $js .= "function it_boot_start(){ " . trim($p['jsboot']) . " }\n";
- $js .= "it_boot('/itjs/" . $p['js'] . "');\n";
- }
-
- $js .= $this->_itjs($p['jsinline'], 'inline');
-
- if ($js)
- $header .= $this->js(array($js));
-
- return "$prefix$header$data</head>";
- }
-}
-
-function body($args)
-{
- foreach ($args as $arg)
- {
- if (is_array($arg))
- {
- foreach ($arg as $key => $value)
- {
- if (is_int($key))
- $body .= $value;
- else
- $attributes .= " $key=\"" . it_html::Q($value) . '"';
- }
- }
- else
- $body .= $arg;
- }
-
- if ($this->_use_it_state)
- $body = tag('iframe', array('id' => "it_state", 'src' => "/itjs/state.html", 'width' => 1, 'height' => 1, 'frameborder' => 0)) . $body;
-
- $body .= $this->_show_boot_dom ? div(array('id' => "it_boot_dom", 'style' => "visibility:hidden")) : "";
-
- return $this->head() . ($this->in_body++ ? "" : "<body$attributes>$body");
-}
-
-function endhtml()
-{
- return $this->head() . ($this->in_body ? "</body>" : "") . "</html>";
-}
-
-}
-?>