// +----------------------------------------------------------------------+
// | 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. |
// | Klaus Guenther <klaus@capitalfocus.org> |
// +----------------------------------------------------------------------+
//
+// $Id$
require_once 'PEAR.php';
-require_once 'Common.php';
+require_once 'HTML/Common.php';
+// HTML/Page/Doctypes.php is required in _getDoctype()
/**
* Base class for XHTML pages
* $p = new HTML_Page(array (
*
* // Sets the charset encoding
+ * // utf-8 is default
* 'charset' => 'utf-8',
*
* // Sets the line end character
* // 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("<p>some text</p>");
*
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Klaus Guenther <klaus@capitalfocus.org>
- * @version 0.8b2
+ * @version 2.0
* @since PHP 4.0.3pl1
*/
class HTML_Page extends HTML_Common {
$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) {
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('<p>' . $error->getMessage() . '</p>');
- $this->setDoctype();
- $this->display();
- die;
-
- } //
-
/**
* Generates the HTML string for the <head< tag
*
}
// Generate stylesheet links
- for($intCounter=0; $intCounter<count($this->_styleSheets); $intCounter++) {
+ $count = count($this->_styleSheets);
+ for($intCounter=0; $intCounter < $count; $intCounter++) {
$strStyleSheet = $this->_styleSheets[$intCounter];
$strHtml .= $tab . "<link rel=\"stylesheet\" href=\"$strStyleSheet\" type=\"text/css\" />" . $lnEnd;
}
$strHtml .= $tab . '<style type="' . $type . '">' . $lnEnd;
$strHtml .= $tab . $tab . '<!--' . $lnEnd;
if (is_object($content)) {
+
+ // first let's propagate line endings and tabs for other HTML_Common-based objects
if (is_subclass_of($content, "html_common")) {
$content->setTab($tab);
$content->setTabOffset(3);
$content->setLineEnd($lnEnd);
}
+
+ // now let's get a string from the object
if (method_exists($content, "toString")) {
$strHtml .= $content->toString() . $lnEnd;
+ } else {
+ return PEAR::raiseError('Error: Body content object does not support method toString().',
+ 0,PEAR_ERROR_TRIGGER);
}
+
} else {
$strHtml .= $content . $lnEnd;
}
}
// Generate script file links
- for($intCounter=0; $intCounter<count($this->_scripts); $intCounter++) {
+ $count = count($this->_scripts);
+ for($intCounter=0; $intCounter < $count; $intCounter++) {
$strType = $this->_scripts[$intCounter]["type"];
$strSrc = $this->_scripts[$intCounter]["src"];
$strHtml .= $tab . "<script type=\"$strType\" src=\"$strSrc\"></script>" . $lnEnd;
*/
function _getDoctype()
{
- include('Page/Doctypes.php');
+ require('HTML/Page/Doctypes.php');
$type = $this->_doctype['type'];
$version = $this->_doctype['version'];
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
* 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
-
- /**
- * Depreciated. Sets or alters a meta tag. Use {@link setMetaData} instead.
- * This function only allows for standard META tags.
- *
- * @param string $name Value of name or http-equiv tag
- * @param string $content Value of the content tag
- * @access public
- */
- function addMetaData($name, $content)
- {
- $this->setMetaData($name, $content);
- } // end func addMetaData
+ } // end addBodyContent
/**
* Adds a linked script to the page
*/
function addScript($url, $type="text/javascript")
{
- $this->_scripts[] = array("type"=>$type, "src"=>$url);
+ $this->_scripts["$type$url"] = array("type"=>$type, "src"=>$url);
} // end func addScript
/**
- * 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;
} // end func addStyleDeclaration
*/
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()
{
} // 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->_body[] =& $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.
*
*
* @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
*
* @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)
*
* @param string $title
* @access public
+ * @returns void
*/
function setTitle($title)
{
// get line endings
$lnEnd = $this->_getLineEnd();
+
+ // get the doctype declaration
$strDoctype = $this->_getDoctype();
- if (!PEAR::isError($strDoctype)){
-
- if ($this->_simple) {
- $strHtml = '<html>' . $lnEnd;
- } elseif ($this->_doctype['type'] == 'xhtml') {
- $strHtml = '<?xml version="1.0" encoding="' . $this->_charset . '"?>' . $lnEnd;
- $strHtml .= $strDoctype . $lnEnd;
- $strHtml .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$this->_language.'">';
- } else {
- $strHtml = $strDoctype . $lnEnd;
- $strHtml .= '<html>' . $lnEnd;
- }
-
+
+ //
+ if ($this->_simple) {
+ $strHtml = '<html>' . $lnEnd;
+ } elseif ($this->_doctype['type'] == 'xhtml') {
+ $strHtml = '<?xml version="1.0" encoding="' . $this->_charset . '"?>' . $lnEnd;
+ $strHtml .= $strDoctype . $lnEnd;
+ $strHtml .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$this->_language.'">';
} else {
-
- $this->_error($strDoctype);
-
+ $strHtml = $strDoctype . $lnEnd;
+ $strHtml .= '<html>' . $lnEnd;
}
$strHtml .= $this->_generateHead();
$strHtml .= $this->_generateBody();
$strHtml .= '</html>';
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: text/html; charset=" . $this->_charset);
+
+ $strHtml = $this->toHTML();
+ print $strHtml;
+ } // end func display
+
}
-?>
+?>
\ No newline at end of file