Location: PHPKode > scripts > HTML Document > html-document/HTMLDoc.class.php
<?php
  /**
   * A method for buffering the entire page before it is send, so it allows for a lot of flexibility. For example,
   * pages can be embeded inside other pages, while the structure stays as valid XHTML. Also it is possible to
   * send HTTP headers after you have "echo'ed" any output. The code is also designed to be used easly, so creating
   * a new complex page is just a matter of calling a few functions.
   *
   * The following is the simplest page:
   * <code>
   * <?php
   * include 'HTMLDoc.class.php';
   * HTMLDoc("Example");
   * ?>
   * That is all!
   * </code>
   *
   * @author Karol Krizka <hide@address.com>
   * @website http://www.krizka.net
   * @version 0.1
   * @license GPL 2
   */

  /**
   * Debugging function to display variables, while ignoring the output buffer.
   *
   * Since sometimes the web page crashes, you might not get any output because of
   * the internal buffering. What this functions does is pauses the buffering, and sends the output to the browser
   * right away. The output is also formatted by the var_dump function, which allows you to display information about
   * classes, arrays, and other complex objects. Also the output is wrapped in <pre> tags, so it will be displayed by
   * the browser for easy reading.
   *
   * Since this function pauses buffering, all the advantages of this framework are lost. The final page will not be
   * valid XHTML, and if you try to send any headers after calling this function, they will throw an error. This is why
   * it is not recommended to use this function in production environments, but it is still perfect for development.
   *
   * @param mixed $dump The variable you wish to be display.
   */
function dump($dump) {
    $level=ob_get_level();
    if($level>0) {
      $buffer=ob_get_contents();
      ob_clean();
      ob_end_flush();
    }
    echo '<pre>';
    var_dump($dump);
    echo '</pre>';
    if($level>0) {
      ob_start('puts');
      //echo $buffer;
    }
}

/**
 * Add an RSS feed to a page.
 * 
 * This function adds a link to an RSS feed in the HEAD section of the webpage. All you have to do is specify the URL
 * for the feed.
 *
 * @param string $title The title of the feed as seen by the user.
 * @param string $feed URL of the Feed
 */
function addRSSFeed($title,$feed) {
  	global $_htmldoc;
	$_htmldoc->addRSSFeed($title,$feed);
}

/**
 * Add a CSS stylesheet to a page.
 *
 * This function adds a link to a CSS stylesheet to the HEAD section of the webpage. All you have to do is specify
 * the URL of the stylesheet.
 * 
 * @param string URL to the stylehseet
 */
function addStylesheet($link) {
	global $_htmldoc;
	$_htmldoc->addStylesheet($link);
}

/**
 * Add a JavaScript file to a page.
 *
 * This function adds a link to a JavaScript file the HEAD section of the webpage. All you have to do is specify
 * the URL to the file.
 * 
 * @param string URL to the file
 */
function addJavaScript($link) {
  global $_htmldoc;
  $_htmldoc->addJavaScript($link);
}

/**
 * Add a custom meta tag.
 *
 * This function adds a custom META tag to the HEAD section of the webpage. This can be used for giving the description
 * and keywords to different search engines.
 * 
 * @param string $name The name of the data
 * @param string $data The actual data
 */
function addMetaData($name,$data) {
	global $_htmldoc;
	$_htmldoc->addMetaData($name,$data);
}

/**
 * Error handler.
 *
 * This is an error handler that takes care of errors thrown by buggy PHP code. Since some fatal errors can 'cause huge malfunctions
 * if the buffering is enabled, this function calls the {@link dump()} function to circumvent it.
 *
 * @access private
 */
function _HTMLDoc_error_handler($errno,$errstr,$errfile,$errline,$errcontext) {
  dump($errfile.' (line '.$errline.'): '.$errstr);
  if($errno==E_ERROR || $errno==E_USER_ERROR)
    die();
}

/**
 * Echo Text
 * 
 * This is the wrapper function for echo command. It just appends the text passed to the internal buffer. It can be used concurrently
 * with the echo command, because in essence they do the same thing. This function just bypasses the PHP buffering system (which is used 
 * by the echo command to call this function). In most cases, you can just ignore this function.
 *
 * @param string $buffer String to display.
 */
function puts($buffer) {
    global $_htmldoc;
    if(isset($_htmldoc)) {
        $_htmldoc->puts($buffer);
        return '';
    } else {
        return $buffer;
    }
}

/**
 * Display the webpage.
 *
 * This function is automatically called by the PHP server at the end to send all the buffered text to the browser.
 *
 * @access private
 */
function display() {
    global $_htmldoc;
    if(ob_get_level()>0)
      ob_end_flush();
    if(isset($_htmldoc)) {
        $_htmldoc->display();
    }
}

/**
 * Create a new HTML document.
 *
 * Calling this function initializes a new document. It HAS to be called any time you wish to use the power of HTMLDoc, because it sets
 * up all the handlers.
 *
 * If an HTML document was already initiliazed, calling this function will only append the title to the existing document. This is very useful
 * if you wish to embed pages within each other.
 *
 * @param string $title The title of the HTML document.
 */
function HTMLDoc($title="Default Document") {
  global $_htmldoc;
  if(!isset($_htmldoc)) {
    set_error_handler('_HTMLDoc_error_handler');
    $_htmldoc=new HTMLDoc($title);
    register_shutdown_function('display');
    ob_start('puts');
  } else {
    $_htmldoc->addTitle($title);
  }
}

/**
 * Append title.
 *
 * This function appends a string to the existing HTML document.
 *
 * @param string $title The title to be appended.
 */
function addTitle($title) {
  global $_htmldoc;
  $_htmldoc->addTitle($title);
}

/**
 * Get the current title
 *
 * Returns the entire title of the currently defined document.
 *
 * @return string The title.
 */
function getTitle() {
  global $_htmldoc;
  return $_htmldoc->title();
}

/**
 * Set the base URL of the objects in the page.
 *
 * This sets the base URL of all objects (ie: CSS stylesheets, RSS feeds, images), so you do not have to specify the full URL
 * everytime you add an object.
 *
 * @param string $url The base URL
 */
function baseURL($url) {
  global $_htmldoc;
  $_htmldoc->baseURL($url);
}

/**
 * The HTMLDoc class that holds all the information about the current page. All of the information is handled by the wrapper
 * functions defined in this page, so you do not have to worry about it!
 *
 * @access private
 * @package HTMLDoc
 */
class HTMLDoc {
    var $title;
    var $body;
    var $head;
    var $stylesheets=array();
    var $rssfeeds=array();
    var $javascripts=array();
    var $metadata=array();
    var $baseURL;

    function HTMLDoc($title="New Document") {
      $this->title($title);
      $this->puts('<body>');
    }

    function title($title="") {
        if(is_string($title) && $title!="") {
            $this->title=trim(strip_tags($title));
        }
        return $this->title;
    }

    function body($body="") {
        if(is_string($body) && $body!="") {
            $this->body=$body;
        }
        return $this->body;
    }

    function head($head="") {
        if(is_string($head) && $head!="") {
            $this->head=$head;
        }
        return $this->head;
    }

    function stylesheets($stylesheets="") {
        if(is_array($stylesheets)) {
            $this->stylesheets=$stylesheets;
        }
        return $this->stylesheets;
    }

    function rssfeeds($rssfeeds="") {
        if(is_array($rssfeeds)) {
            $this->rssfeeds=$rssfeeds;
        }
        return $this->rssfeeds;
    }

    function javascripts($javascripts="") {
        if(is_array($javascripts)) {
            $this->javascripts=$javascripts;
        }
        return $this->javascripts;
    }


    function metadata($metadata="") {
      if(is_array($metadata)) {
	$this->metadata=$metadata;
      }
      return $this->metadata;
    }

    function baseURL($baseURL="") {
        if($baseURL!='') {
            $this->baseURL=$baseURL;
        }
        return $this->baseURL;
    }

    function addTitle($add2title) {
      $this->title($this->title().' - '.$add2title);
    }

    function addHead($add2head) {
      $this->head($this->head().$add2head);
    }

    function addRSSFeed($title,$rssfeed) {
        $rssfeeds=$this->rssfeeds();
        $rssfeeds[$title]=$rssfeed;
        $this->rssfeeds($rssfeeds);
    }

    function addStylesheet($stylesheet) {
        $stylesheets=$this->stylesheets();
        $stylesheets[]=$stylesheet;
        $this->stylesheets($stylesheets);
    }

    function addJavaScript($javascript) {
        $javascripts=$this->javascripts();
        $javascripts[]=$javascript;
        $this->javascripts($javascripts);
    }

    function addMetaData($name,$data) {
      $md=$this->metadata();
      $md[$name]=$data;
      $this->metadata($md);
    }
    
    function puts($text) {
      $this->body.=$text;
      //$this->parseString($text);
    }
   
    function display() {
      $this->puts('</body>');
      
      echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
      $page='<html>';
      $page.='<head>';
      $page.='<title>'.$this->title().'</title>';
      foreach($this->stylesheets() as $style) {
	$page.='<link rel="stylesheet" type="text/css" href="'.$style.'"/>';
      }
      foreach($this->rssfeeds() as $title=>$rssfeed) {
	$page.='<link rel="alternate" type="application/rss+xml" title="'.$title.'" href="'.$rssfeed.'"/>';
      }
      
      foreach($this->javascripts() as $js) {
	$page.='<script type="text/javascript" src="'.$js.'"></script>';
      }
      foreach($this->metadata() as $name => $data) {
	$page.='<meta name="'.$name.'" content="'.$data.'">';
      }
      
      $base=$this->baseURL();
      if($base!='') {
	$page.='<base href="'.$base.'" />';
      }
      
      $page.=$this->head();
      $page.='</head>';
      //$page.='<body>';
      $page.=$this->body();
      //$page.='</body>';
      $page.='</html>';
      echo $page;
    }
}
?>
Return current item: HTML Document