X-Git-Url: https://git.llucax.com/mecon/meconlib.git/blobdiff_plain/e77639a0e0e540810a85ff7db40ec17629636e4a..358b117f798a3872f31486c84ae863c80c36abd6:/pear_lib_tmp/HTML/Page.php?ds=sidebyside diff --git a/pear_lib_tmp/HTML/Page.php b/pear_lib_tmp/HTML/Page.php index 801ffab..233b416 100644 --- a/pear_lib_tmp/HTML/Page.php +++ b/pear_lib_tmp/HTML/Page.php @@ -5,10 +5,10 @@ // +----------------------------------------------------------------------+ // | Copyright (c) 1997 - 2003 The PHP Group | // +----------------------------------------------------------------------+ -// | This source file is subject to version 2.0 of the PHP license, | +// | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | -// | http://www.php.net/license/2_02.txt. | +// | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | @@ -17,9 +17,12 @@ // | Klaus Guenther | // +----------------------------------------------------------------------+ // +// $Id$ require_once 'PEAR.php'; -require_once 'Common.php'; +require_once 'HTML/Common.php'; +// HTML/Page/Doctypes.php is required in _getDoctype() +// HTML/Page/Namespaces.php is required in _getNamespace() /** * Base class for XHTML pages @@ -54,11 +57,12 @@ require_once 'Common.php'; * * // The initializing code can also be in in the form of an HTML * // attr="value" string. - * // Possible attributes are: charset, lineend, tab, doctype, language and cache + * // Possible attributes are: charset, mime, lineend, tab, doctype, namespace, language and cache * * $p = new HTML_Page(array ( * * // Sets the charset encoding + * // utf-8 is default * 'charset' => 'utf-8', * * // Sets the line end character @@ -97,7 +101,7 @@ require_once 'Common.php'; * // for more details * if ($error) { * $p->setTitle("Error!"); - * $p->setBodyContent("

oops, we have an error: $error

"); + * $p->setBodyContent("

Houston, we have a problem: $error

"); * $p->display(); * die; * } // end error handling @@ -112,7 +116,7 @@ require_once 'Common.php'; * // An XHTML compliant page (with title) is automatically generated * * // This overrides the XHTML 1.0 Transitional default - * $p->setDoctype('xhtml'); + * $p->setDoctype('XHTML 1.0 Strict'); * * // Put some content in here * $p->addBodyContent("

some text

"); @@ -143,7 +147,7 @@ require_once 'Common.php'; * * @author Adam Daniel * @author Klaus Guenther - * @version 0.8b2 + * @version 2.0 * @since PHP 4.0.3pl1 */ class HTML_Page extends HTML_Common { @@ -196,6 +200,22 @@ class HTML_Page extends HTML_Common { */ var $_metaTags = array( 'standard' => array ( 'Generator' => 'PEAR HTML_Page' ) ); + /** + * Document mime type + * + * @var string + * @access private + */ + var $_mime = 'text/html'; + + /** + * Document namespace + * + * @var string + * @access private + */ + var $_namespace = ''; + /** * Array of linked scripts * @@ -204,6 +224,14 @@ class HTML_Page extends HTML_Common { */ var $_scripts = array(); + /** + * Array of scripts placed in the header + * + * @var array + * @access private + */ + var $_script = array(); + /** * Array of linked scripts * @@ -241,24 +269,25 @@ class HTML_Page extends HTML_Common { * Possible attributes are: * - general options: * - "lineend" => "unix|win|mac" (Sets line ending style; defaults to unix.) - * - "tab" => string (Sets line ending style; defaults to \t.) + * - "tab" => string (Sets line ending style; defaults to \t.) * - "cache" => "false|true" * - "charset" => charset string (Sets charset encoding; defaults to utf-8) + * - "mime" => mime encoding string (Sets document mime type; defaults to text/html) * - XHTML specific: - * - "doctype" => mixed (Sets XHTML doctype; defaults to XHTML 1.0 Transitional.) + * - "doctype" => string (Sets XHTML doctype; defaults to XHTML 1.0 Transitional.) * - "language" => two letter language designation. (Defines global document language; defaults to "en".) + * - "namespace" => string (Sets document namespace; defaults to the W3C defined namespace.) * * @param mixed $attributes Associative array of table tag attributes * or HTML attributes name="value" pairs * @access public */ - function HTML_Page($attributes = "") + function HTML_Page($attributes = array()) { $commonVersion = 1.7; if (HTML_Common::apiVersion() < $commonVersion) { return PEAR::raiseError("HTML_Page version " . $this->apiVersion() . " requires " . - "HTML_Common version $commonVersion or greater. You can get the (still) unofficial ". - "update here: http://www.geist.uni-freiburg.de/pear/HTML/Common.txt", 0, PEAR_ERROR_TRIGGER); + "HTML_Common version 1.2 or greater.", 0, PEAR_ERROR_TRIGGER); } if ($attributes) { @@ -285,6 +314,18 @@ class HTML_Page extends HTML_Common { $this->setLang($attributes['language']); } + if (isset($attributes['mime'])) { + $this->setMimeEncoding($attributes['mime']); + } + + if (isset($attributes['namespace'])) { + $this->setNamespace($attributes['namespace']); + } + + if (isset($attributes['tab'])) { + $this->setTab($attributes['tab']); + } + if (isset($attributes['cache'])) { $this->setCache($attributes['cache']); } @@ -357,22 +398,6 @@ class HTML_Page extends HTML_Common { return $strHtml; } // end func _generateHead - /** - * Outputs a page containing the error message and then dies - * - * @return void - * @access private - */ - function _error(&$error){ - - $this->setTitle('HTML_Page doctype Error'); - $this->setBody('

' . $error->getMessage() . '

'); - $this->setDoctype(); - $this->display(); - die; - - } // - /** * Generates the HTML string for the <head< tag * @@ -381,6 +406,12 @@ class HTML_Page extends HTML_Common { */ function _generateHead() { + // close empty tags if XHTML + if ($this->_doctype['type'] == 'html'){ + $tagEnd = '>'; + } else { + $tagEnd = ' />'; + } // get line endings $lnEnd = $this->_getLineEnd(); @@ -393,46 +424,106 @@ class HTML_Page extends HTML_Common { foreach ($this->_metaTags as $type => $tag) { foreach ($tag as $name => $content) { if ($type == 'http-equiv') { - $strHtml .= $tab . "" . $lnEnd; + $strHtml .= $tab . "" . $lnEnd; + $strHtml .= $tab . "_styleSheets); $intCounter++) { - $strStyleSheet = $this->_styleSheets[$intCounter]; - $strHtml .= $tab . "" . $lnEnd; + foreach ($this->_styleSheets as $strStyleSheet) { + $strHtml .= $tab . "_style as $type => $content) { $strHtml .= $tab . '" . $lnEnd; + + // See above note + + if ($this->_mime == 'text/html' ) { + $strHtml .= $tab . $tab . '-->' . $lnEnd; + } else { + $strHtml .= $tab . $tab . ']]>' . $lnEnd; + } + $strHtml .= $tab . '' . $lnEnd; } // Generate script file links - for($intCounter=0; $intCounter_scripts); $intCounter++) { - $strType = $this->_scripts[$intCounter]["type"]; - $strSrc = $this->_scripts[$intCounter]["src"]; + foreach ($this->_scripts as $strSrc => $strType) { $strHtml .= $tab . "" . $lnEnd; } + // Generate script declarations + foreach ($this->_script as $type => $content) { + $strHtml .= $tab . '' . $lnEnd; + } + // Close tag $strHtml .= '' . $lnEnd; @@ -443,26 +534,34 @@ class HTML_Page extends HTML_Common { /** * Returns the doctype declaration * - * @return string + * @return mixed * @access private */ function _getDoctype() { - include('Page/Doctypes.php'); + require('HTML/Page/Doctypes.php'); + + if (isset($this->_doctype['type'])) { + $type = $this->_doctype['type']; + } - $type = $this->_doctype['type']; - $version = $this->_doctype['version']; - $variant = $this->_doctype['variant']; + if (isset($this->_doctype['version'])) { + $version = $this->_doctype['version']; + } + + if (isset($this->_doctype['variant'])) { + $variant = $this->_doctype['variant']; + } $strDoctype = ''; - if ($variant != '') { + if (isset($variant)) { if (isset($doctype[$type][$version][$variant][0])) { foreach ( $doctype[$type][$version][$variant] as $string) { $strDoctype .= $string.$this->_getLineEnd(); } } - } elseif ($version != '') { + } elseif (isset($version)) { if (isset($doctype[$type][$version][0])) { foreach ( $doctype[$type][$version] as $string) { $strDoctype .= $string.$this->_getLineEnd(); @@ -473,7 +572,7 @@ class HTML_Page extends HTML_Common { $strDoctype = $this->_getDoctype(); } } - } elseif ($type != '') { + } elseif (isset($type)) { if (isset($default[$type][0])){ $this->_doctype = $this->_parseDoctypeString($default[$type][0]); $strDoctype = $this->_getDoctype(); @@ -487,11 +586,65 @@ class HTML_Page extends HTML_Common { return $strDoctype; } else { return PEAR::raiseError('Error: "'.$this->getDoctypeString().'" is an unsupported or illegal document type.', - 0,PEAR_ERROR_RETURN); + 0,PEAR_ERROR_TRIGGER); } } // end func _getDoctype + /** + * Retrieves the document namespace + * + * @return mixed + * @access private + */ + function _getNamespace() + { + require('HTML/Page/Namespaces.php'); + + if (isset($this->_doctype['type'])) { + $type = $this->_doctype['type']; + } + + if (isset($this->_doctype['version'])) { + $version = $this->_doctype['version']; + } + + if (isset($this->_doctype['variant'])) { + $variant = $this->_doctype['variant']; + } + + $strNamespace = ''; + + if (isset($variant)){ + if (isset($namespace[$type][$version][$variant][0]) && is_string($namespace[$type][$version][$variant][0])) { + $strNamespace = $namespace[$type][$version][$variant][0]; + } elseif (isset($namespace[$type][$version][0]) && is_string($namespace[$type][$version][0]) ) { + $strNamespace = $namespace[$type][$version][0]; + } elseif (isset($namespace[$type][0]) && is_string($namespace[$type][0]) ) { + $strNamespace = $namespace[$type][0]; + } + } elseif (isset($version)) { + if (isset($namespace[$type][$version][0]) && is_string($namespace[$type][$version][0]) ) { + $strNamespace = $namespace[$type][$version][0]; + } elseif (isset($namespace[$type][0]) && is_string($namespace[$type][0]) ) { + $strNamespace = $namespace[$type][0]; + } + } else { + if (isset($namespace[$type][0]) && is_string($namespace[$type][0]) ) { + $strNamespace = $namespace[$type][0]; + } + } + + + if ($strNamespace) { + return $strNamespace; + } else { + return PEAR::raiseError('Error: "'.$this->getDoctypeString().'" does not have a default namespace. Use setNamespace() to define your namespace.', + 0,PEAR_ERROR_TRIGGER); + } + + } // end func _getNamespace + /** * Parses a doctype declaration like "XHTML 1.0 Strict" to an array * @@ -504,7 +657,13 @@ class HTML_Page extends HTML_Common { $split = explode(' ',strtolower($string)); $elements = count($split); - $array = array('type'=>$split[0],'version'=>$split[1],'variant'=>$split[2]); + if (isset($split[2])){ + $array = array('type'=>$split[0],'version'=>$split[1],'variant'=>$split[2]); + } elseif (isset($split[1])){ + $array = array('type'=>$split[0],'version'=>$split[1]); + } else { + $array = array('type'=>$split[0]); + } return $array; } // end func _parseDoctypeString @@ -515,61 +674,97 @@ class HTML_Page extends HTML_Common { * If you wish to overwrite whatever is in the body, use {@link setBody}; * {@link unsetBody} completely empties the body without inserting new content. * It is possible to add objects, strings or an array of strings and/or objects + * Objects must have a toString method. * - * @param mixed $content New <body> tag content. + * @param mixed $content New <body> tag content (may be passed as a reference) * @access public */ function addBodyContent($content) { $this->_body[] =& $content; - } // end addBodyContent + if (is_object($content)) { + if (method_exists($content, "toStyleSheet")) { + $this->addStyleSheet($content->toStyleSheet()); + } + if (method_exists($content, "toScript")) { + $script = $content->toScript(); + if (is_array($script)) { + $this->addScript($script[0], $script[1]); + } else { + $this->addScript($script); + } + } + } elseif (is_array($content)) { + foreach ($content as $element) { + if (is_object($content)) { + if (method_exists($element, "toStyleSheet")) { + $this->addStyleSheet($element->toStyleSheet()); + } + if (method_exists($element, "toScript")) { + $script = $element->toScript(); + if (is_array($script)) { + $this->addScript($script[0], $script[1]); + } else { + $this->addScript($script); + } + } + } + } + } + } // end addBodyContent /** - * Depreciated. Sets or alters a meta tag. Use {@link setMetaData} instead. - * This function only allows for standard META tags. + * Adds a linked script to the page * - * @param string $name Value of name or http-equiv tag - * @param string $content Value of the content tag - * @access public + * @param string $url URL to the linked script + * @param string $type Type of script. Defaults to 'text/javascript' + * @access public */ - function addMetaData($name, $content) + function addScript($url, $type="text/javascript") { - $this->setMetaData($name, $content); - } // end func addMetaData + $this->_scripts[$url] = $type; + } // end func addScript /** - * Adds a linked script to the page + * Adds a script to the page. + * Content can be a string or an object with a toString method. + * Defaults to text/javascript. * - * @param string $url URL to the linked style sheet - * @param string $type Type of script. Defaults to 'text/javascript' * @access public + * @param mixed $content Script (may be passed as a reference) + * @param string $type Scripting mime (defaults to 'text/javascript') + * @return void */ - function addScript($url, $type="text/javascript") + function addScriptDeclaration($content, $type = 'text/javascript') { - $this->_scripts[] = array("type"=>$type, "src"=>$url); - } // end func addScript + $this->_script[strtolower($type)] =& $content; + } // end func addScriptDeclaration /** - * Adds a linked style sheet to the page + * Adds a linked stylesheet to the page * * @param string $url URL to the linked style sheet * @access public + * @return void */ function addStyleSheet($url) { - $this->_styleSheets[] = $url; + $this->_styleSheets[$url] = $url; } // end func addStyleSheet /** - * Adds a linked style sheet to the page + * Adds a stylesheet declaration to the page. + * Content can be a string or an object with a toString method. + * Defaults to text/css. * - * @param string $type Type of stylesheet (e.g., text/css) - * @param string $content Style declarations * @access public + * @param mixed $content Style declarations (may be passed as a reference) + * @param string $type Type of stylesheet (defaults to 'text/css') + * @return void */ - function addStyleDeclaration($type, $content) + function addStyleDeclaration($content, $type = 'text/css') { - $this->_style[$type] =& $content; + $this->_style[strtolower($type)] =& $content; } // end func addStyleDeclaration /** @@ -580,35 +775,14 @@ class HTML_Page extends HTML_Common { */ function apiVersion() { - return 0.8; + return 2.0; } // end func apiVersion /** - * Outputs the HTML content to the screen. + * Returns the document charset encoding. * - * @access public - */ - function display() - { - if(! $this->_cache) { - header("Expires: Tue, 1 Jan 1980 12:00:00 GMT"); - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); - header("Cache-Control: no-cache"); - header("Pragma: no-cache"); - } - - // set character encoding - header("Content-Type: text/html; charset=" . $this->_charset); - - $strHtml = $this->toHTML(); - print $strHtml; - } // end func display - - /** - * Defines if the document should be cached by the browser. Defaults to false. - * - * @param string $cache Options are currently 'true' or 'false'. Defaults to 'false'. * @access public + * @returns string */ function getCharset() { @@ -662,43 +836,29 @@ class HTML_Page extends HTML_Common { } // end func getTitle /** - * Sets or alters the XHTML !DOCTYPE declaration. Can be set to "strict", - * "transitional" or "frameset". Defaults to "transitional". This must come - * _after_ declaring the character encoding with {@link setCharset} or directly - * when the class is initiated {@link HTML_Page}. - * - * @param mixed $type String containing a document type. Defaults to "XHTML 1.0 Transitional" - * @access public - */ - function setDoctype($type = "XHTML 1.0 Transitional") - { - $this->_doctype = $this->_parseDoctypeString($type); - } // end func setDoctype - - /** - * Sets the global document language declaration. Default is English. + * Sets the content of the <body> tag. If content exists, it is overwritten. + * If you wish to use a "safe" version, use {@link addBodyContent} + * Objects must have a toString method. * + * @param mixed $content New <body> tag content. May be an object. (may be passed as a reference) * @access public - * @param string $lang Two-letter language designation. */ - function setLang($lang = "en") + function setBody($content) { - $this->_language = strtolower($lang); - } // end setLang + $this->unsetBody(); + $this->addBodyContent($content); + } // end setBody /** - * Sets the content of the <body> tag. If content exists, it is overwritten. - * If you wish to use a "safe" version, use {@link addBodyContent} + * Unsets the content of the <body> tag. * - * @param mixed &$content New <body> tag content. * @access public */ - function setBody(&$content) + function unsetBody() { $this->_body = ''; - $this->_body[] =& $content; - } // end setBody - + } // end unsetBody + /** * Defines if the document should be cached by the browser. Defaults to false. * @@ -719,29 +879,68 @@ class HTML_Page extends HTML_Common { * * @param string $cache Options are currently 'true' or 'false'. Defaults to 'false'. * @access public + * @returns void */ function setCharset($type = 'utf-8') { $this->_charset = $type; } // end setCache + /** + * Sets or alters the XHTML !DOCTYPE declaration. Can be set to "strict", + * "transitional" or "frameset". Defaults to "transitional". This must come + * _after_ declaring the character encoding with {@link setCharset} or directly + * when the class is initiated {@link HTML_Page}. + * + * @param string $type String containing a document type. Defaults to "XHTML 1.0 Transitional" + * @access public + * @returns void + */ + function setDoctype($type = "XHTML 1.0 Transitional") + { + $this->_doctype = $this->_parseDoctypeString($type); + } // end func setDoctype + + /** + * Sets the global document language declaration. Default is English. + * + * @access public + * @param string $lang Two-letter language designation. + */ + function setLang($lang = "en") + { + $this->_language = strtolower($lang); + } // end setLang + /** * Sets or alters a meta tag. * * @param string $name Value of name or http-equiv tag * @param string $content Value of the content tag * @param bool $http_equiv META type "http-equiv" defaults to NULL + * @return void * @access public */ function setMetaData($name, $content, $http_equiv = false) { if ($http_equiv == true) { - $this->_meta['http-equiv'][$name] = $content; + $this->_metaTags['http-equiv'][$name] = $content; } else { - $this->_meta['standard'][$name] = $content; + $this->_metaTags['standard'][$name] = $content; } } // end func setMetaData + /** + * Sets an http-equiv Content-Type meta tag + * + * @access public + * @returns void + */ + function setMetaContentType() + { + $this->setMetaData('Content-Type', $this->_mime . '; charset=' . $this->_charset , true ); + } // end func setMetaContentType + /** * Easily sets or alters a refresh meta tag. * If no $url is passed, "self" is presupposed, and the appropriate URL @@ -749,7 +948,8 @@ class HTML_Page extends HTML_Common { * * @param string $time Time till refresh (in seconds) * @param string $url Absolute URL or "self" - * @param bool $https If $url = self, this allows for the https protocol + * @param bool $https If $url = self, this allows for the https protocol defaults to NULL + * @return void * @access public */ function setMetaRefresh($time, $url = 'self', $https = false) @@ -765,11 +965,40 @@ class HTML_Page extends HTML_Common { $this->setMetaData("Refresh", "$time; url=$url", true); } // end func setMetaRefresh + /** + * Sets the document MIME encoding that is sent to the browser. + * + * @param string $type + * @access public + * @returns void + */ + function setMimeEncoding($type = 'text/html') + { + $this->_mime = strtolower($type); + } // end func setMimeEncoding + + /** + * Sets the document namespace + * + * @param string $namespace Optional. W3C namespaces are used by default. + * @access public + * @returns void + */ + function setNamespace($namespace = '') + { + if (isset($namespace)){ + $this->_namespace = $namespace; + } else { + $this->_namespace = $this->_getNamespace(); + } + } // end func setTitle + /** * Sets the title of the page * * @param string $title * @access public + * @returns void */ function setTitle($title) { @@ -787,39 +1016,59 @@ class HTML_Page extends HTML_Common { // get line endings $lnEnd = $this->_getLineEnd(); + + // get the doctype declaration $strDoctype = $this->_getDoctype(); - if (!PEAR::isError($strDoctype)){ + + // This determines how the doctype is declared + if ($this->_simple) { - if ($this->_simple) { - $strHtml = '' . $lnEnd; - } elseif ($this->_doctype['type'] == 'xhtml') { - $strHtml = '_charset . '"?>' . $lnEnd; - $strHtml .= $strDoctype . $lnEnd; - $strHtml .= ''; - } else { - $strHtml = $strDoctype . $lnEnd; - $strHtml .= '' . $lnEnd; + $strHtml = '' . $lnEnd; + + } elseif ($this->_doctype['type'] == 'xhtml') { + + // get the namespace if not already set + if (!$this->_namespace){ + $this->_namespace = $this->_getNamespace(); } - + + $strHtml = '_charset . '"?>' . $lnEnd; + $strHtml .= $strDoctype . $lnEnd; + $strHtml .= '' . $lnEnd; + } else { - $this->_error($strDoctype); + $strHtml = $strDoctype . $lnEnd; + $strHtml .= '' . $lnEnd; } + $strHtml .= $this->_generateHead(); $strHtml .= $this->_generateBody(); $strHtml .= ''; return $strHtml; } // end func toHtml - + /** - * Unsets the content of the <body> tag. + * Outputs the HTML content to the screen. * - * @access public + * @access public */ - function unsetBody() + function display() { - $this->_body = ''; - } // end unsetBody + if(! $this->_cache) { + header("Expires: Tue, 1 Jan 1980 12:00:00 GMT"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + header("Cache-Control: no-cache"); + header("Pragma: no-cache"); + } + + // set character encoding + header('Content-Type: ' . $this->_mime . '; charset=' . $this->_charset); + + $strHtml = $this->toHTML(); + print $strHtml; + } // end func display + } -?> +?> \ No newline at end of file