PHP Classes

File: class.globals.php

Recommend this page to a friend!
  Classes of Richard Vrijhof   Globals   class.globals.php   Download  
File: class.globals.php
Role: ???
Content type: text/plain
Description: PHP class that generates globally needed HTML tags
Class: Globals
PHP class that generates globally needed HTML tags
Author: By
Last change:
Date: 23 years ago
Size: 16,564 bytes
 

Contents

Class file image Download
<?php /* The documentation in this file conforms to PHPDoc beta 1 standard, see http://www.phpdoc.de/ for more info. IMPORTANT! Mind that this file presumes a certain number of things, like: -it can generate HTML 4.01 Transitional or XHTML 1.0 Transitional output -the character set used is UTF-8 -the language used is English -the inline client-side scripts are of type JavaScript -etc., etc.... Just change the class variables as needed further up in this file. Most of them are used in method meta(). */ /** * constant for use with robot meta tag * * @const ROBOTS_NOINDEX * @access public * @see meta() */ define("ROBOTS_NOINDEX", 1); /** * constant for use with robot meta tag * * @const ROBOTS_NOFOLLOW * @access public * @see meta() */ define("ROBOTS_NOFOLLOW", 2); /** * constant for use with robot meta tag * * @const ROBOTS_INDEX_NOFOLLOW * @access public * @see meta() */ define("ROBOTS_INDEX_NOFOLLOW", 3); /** * constant for use with robot meta tag * * @const ROBOTS_NOINDEX_FOLLOW * @access public * @see meta() */ define("ROBOTS_NOINDEX_FOLLOW", 4); /** * constant for use with robot meta tag * * @const ROBOTS_NOINDEX_NOFOLLOW * @access public * @see meta() */ define("ROBOTS_NOINDEX_NOFOLLOW", 5); /** * methods that generate globally needed HTML tags * * Note: the documentation in this file conforms to the PHPDoc standard (see www.phpdoc.de) * Usage:<br> * first of all, include this file in your script * $g = new Globals; * $g-&gt;set(&quot;xhtml&quot;, true); // generate XHTML 1&#46;0 output * // Print DTD, &laquo;HTML&raquo; tag and title, but no &laquo;HTML&raquo; tag if no parameter! * $g-&gt;dtd(&quot;Title&quot;); * // Print meta tags and headers * $g-&gt;meta(&quot;Description&quot;, &quot;keyword1, keyword2&quot;, ROBOTS_NOINDEX, true, false); * <br> * License: GPL, see GPL.txt * * @author Richard Vrijhof <R.J.Vrijhof@bigfoot.com> * @version 1.1 * @access public */ class Globals { /*************************************************************************************************** Public variables. Get or set them with get() and set() (except $self and $self_result). ***************************************************************************************************/ /** * automatically generated by method <code>setSelf</code>. * <p>Contains path to the script calling this class.</p> * * @var string $var * @access public * @see setSelf() */ var $self = ""; /** * tells class <code>Globals</code> to generate XHTML 1&#46;0 output * * @var boolean $xhtml * @access public */ var $xhtml = false; /*************************************************************************************************** Private variables. ***************************************************************************************************/ /** * automatically generated by constructor method. * <p>Contains result of method <code>setSelf</code>.</p> * * @var integer $self_result * @access private * @see setSelf() */ var $self_result = 0; /** * first required line in HTML body when conforming to XHTML 1&#46;0 * * @var string $xml * @access private * @see dtd() */ var $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; /** * document type definition, either HTML 4&#46;01 or XHTML 1&#46;0 * * @var string $dtd * @access private * @see dtd() */ var $dtd = array( "xhtml" => "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n", "html" => "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\n<html>\n" ); /** * XML namespace tag * * @var string $xmlns * @access private * @see dtd() */ var $xmlns = "\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"; /** * content-type header * * @var string $content * @access private * @see meta() */ var $content = "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\""; /** * content-script-type header * * @var string $contentST * @access private * @see meta() */ var $contentST = "<meta http-equiv=\"content-script-type\" content=\"text/javascript\""; /** * copyright meta tag * * @var string $copyright * @access private * @see meta() */ var $copyright = "<meta name=\"copyright\" content=\"2001 Put your name here\""; /** * reply-to meta tag * * @var string $reply * @access private * @see meta() */ var $reply = "<meta name=\"reply-to\" content=\"Put your name here - email@address.com\""; /** * generator meta tag * * @var string $generator * @access private * @see meta() */ var $generator = "<meta name=\"generator\" content=\"Put the name and version of your text/HTML editor here\""; /** * PICS-Label header * * @var string $reply * @access private * @see meta() */ var $pics = "Get your own PICS-Label on http://www.icra.org/ before ever using this class or change the code in method meta() further up!!!"; /** * Cache-control header * * @var string $nocache * @access private * @see meta() */ var $nocache = "<meta http-equiv=\"cache-control\" content=\"no-cache, must-revalidate\""; /** * Pragma header * * @var string $nocache1 * @access private * @see meta() */ var $nocache1 = "<meta http-equiv=\"pragma\" content=\"no-cache\""; /** * Expires header * * @var string $expires * @access private * @see meta() */ var $expires = "<meta http-equiv=\"expires\" content=\"Sat, 01 Jan 2000 00:00:00 GMT\""; /** * Last-modified header * * @var string $last_modified * @access private * @see meta() */ var $last_modified = "<meta http-equiv=\"last-modified\" content=\""; /*************************************************************************************************** Public methods. ***************************************************************************************************/ /** * retrieves a class variable * * @param string $var name of the class variable * @return mixed value of the class variable * @access public */ function get($var) { return($this->$var); } /** * sets a class variable * * @param string $var name of the class variable * @param mixed $val value of the class variable * @access public */ function set($var, $val) { $this->$var = $val; } /** * prints a Document Type Definition (and the &laquo;html&raquo; tag) * <p>As an added bonus, it can also print the title (and so, of course, it * also has to print the &laquo;head&raquo; tag).</p> * * @param string $title name of the class variable * @access public */ function dtd($title = "") { if ($this->xhtml) { echo $this->xml; echo $this->dtd["xhtml"]; echo $this->xmlns; } else echo $this->dtd["html"]; if ($title) echo "\n<head>\n\n<title>$title</title>\n"; } /** * prints the meta tags defined in the meta tag class variables * <p>Can also print description and keywords meta tags, if desired.</p> * * @param string $description description meta tag content * @param string $keywords keywords meta tag content * @param int $robots whether a robots meta tag must be printed and which (use 0 to disable) * @param boolean $nocache whether the no-cache meta tags must be printed * @param boolean $last_modified whether the last-modified meta tag must be printed * @access public */ function meta($description = "", $keywords = "", $robots = 0, $nocache = false, $last_modified = false) { if ($this->xhtml) { echo "$this->content />\n"; echo "$this->contentST />\n"; echo "$this->copyright />\n"; echo "$this->reply />\n"; if ($description) echo "<meta name=\"description\" content=\"$description\" />\n"; if ($keywords) echo "<meta name=\"keywords\" content=\"$keywords\" />\n"; echo "$this->generator />\n"; echo "$this->pics />\n"; switch ($robots) { case ROBOTS_NOINDEX : echo "<meta name=\"robots\" content=\"noindex\" />\n"; break; case ROBOTS_NOFOLLOW : echo "<meta name=\"robots\" content=\"nofollow\" />\n"; break; case ROBOTS_INDEX_NOFOLLOW : echo "<meta name=\"robots\" content=\"index,nofollow\" />\n"; break; case ROBOTS_NOINDEX_FOLLOW : echo "<meta name=\"robots\" content=\"noindex,follow\" />\n"; break; case ROBOTS_NOINDEX_NOFOLLOW : echo "<meta name=\"robots\" content=\"noindex,nofollow\" />\n"; } if ($nocache) { echo "$this->nocache />\n"; echo "$this->nocache1 />\n"; echo "$this->expires />\n"; } if ($last_modified) echo $this->last_modified.gmdate("D, d M Y H:i:s")." GMT\" />\n"; } else { echo "$this->content>\n"; echo "$this->contentST>\n"; echo "$this->copyright>\n"; echo "$this->reply>\n"; if ($description) echo "<meta name=\"description\" content=\"$description\">\n"; if ($keywords) echo "<meta name=\"keywords\" content=\"$keywords\">\n"; echo "$this->generator>\n"; // Get your own PICS-Label or comment this out: echo "$this->pics>\n"; switch ($robots) { case ROBOTS_NOINDEX : echo "<meta name=\"robots\" content=\"noindex\">\n"; break; case ROBOTS_NOFOLLOW : echo "<meta name=\"robots\" content=\"nofollow\">\n"; break; case ROBOTS_INDEX_NOFOLLOW : echo "<meta name=\"robots\" content=\"index,nofollow\">\n"; break; case ROBOTS_NOINDEX_FOLLOW : echo "<meta name=\"robots\" content=\"noindex,follow\">\n"; break; case ROBOTS_NOINDEX_NOFOLLOW : echo "<meta name=\"robots\" content=\"noindex,nofollow\">\n"; } if ($nocache) { echo "$this->nocache>\n"; echo "$this->nocache1>\n"; echo "$this->expires>\n"; } if ($last_modified) echo $this->last_modified.gmdate("D, d M Y H:i:s")." GMT\">\n"; } } /** * returns a hyperlink/anchor * <p>Optionally with a text in the status bar (you know, with onmouseover()), * an id (in case you want to make an anchor) and a target attribute (handy if * you&acute;re working with frames).</p> * <p>It returns the HTML tag up to the first <em>&gt;</em> character (so it * reads something like <em>&laquo;a href=&quot;&quot; .....&raquo;</em>), but you * can give it the parameter <code>$end</code> with whatever you want * (but something like <em>click here&laquo;/a&raquo;</em> is what it&acute;s * actually meant for).</p> * * @param string $url URL you want to link to * @param string $status text in the status bar when hovering over the link * @param string $target target frame * @param string $id ID/name if you want to make an anchor * @param string $end text to print at the end * @return string needed HTML tag * @access public */ function a($url = "", $status = "", $target = "", $id = "", $end = "") { $str = "<a"; if ($url) $str .= " href=\"$url\""; if ($id) $str .= " name=\"$id\" id=\"$id\""; if ($status) $str .= " title=\"$status\" onmouseover=\"self.status='$status';return true;\" onmouseout=\"self.status='';return true;\""; if ($target) $str .= " target=\"$target\">"; else $str .= ">"; $str .= $end; return $str; } /** * returns an IMG tag * <p>Optionally with width, height, and alt attributes (border attribute is * always printed though, but 0 if unset, so can't do any harm, I presume).</p> * <p>It returns the HTML tag up to the first <em>&gt;</em> character (so it * reads something like <em>&laquo;img src=&quot;&quot; .....&raquo;</em>), but you can give * it the parameter <code>$end</code> with whatever you want for some text to * put after the img tag.</p> * <p>The idea behind this is that if you want to make an image which is a * link, you first put an <em>&laquo;a&raquo;</em> tag followed by an <em>&laquo;img&raquo;</em> tag. * Then you want to have the extra text (namely <em>&laquo;/a&raquo;</em> at least) after the <em>&laquo;img&raquo;</em> * tag, not after the <em>&laquo;a&raquo;</em> tag and some text. That&acute;s why <code>img</code> also * has the <code>$end</code> parameter.</p> * * @param string $url URL of the image * @param int $width width of the image in pixels * @param int $height height of the image in pixels * @param string $alt text for the ALT attribute * @param int $border width of the image border * @param boolean $ismap whether the image is a server-side map or not * @param string $end text to put after the <em>&laquo;img&raquo;</em> tag * @return string needed HTML tag * @access public * @see Globals a() */ function img($url, $width = 0, $height = 0, $alt = "", $border = 0, $ismap = false, $end = "") { $str = "<img src=\"$url\""; if ($width > 0) $str .= " width=\"$width\""; if ($height > 0) $str .= " height=\"$height\""; if ($alt) $str .= " alt=\"".addslashes($alt)."\""; $str .= " border=\"$border\""; if ($ismap) if ($this->xhtml) $str .= " ismap=\"ismap\""; else $str .= " ismap"; if ($this->xhtml) $str .= " />"; else $str .= ">"; $str .= $end; return $str; } /*************************************************************************************************** Private methods. Do not call! ***************************************************************************************************/ /** * sets the <code>$self</code> variable to a sensible value * <p>Called automatically when the class is instantiated. Do not call yourself!</p> * * @return int * 1 if used <code>REQUEST_URI</code> from the environment to set <code>$self</code><br> * 2 if used <code>PATH_INFO</code> from the environment to set <code>$self</code><br> * 3 if used <code>PHP_SELF</code> from the environment to set <code>$self</code><br> * @access private */ function setSelf() { if (eregi("^(.*[^\?])", getenv("REQUEST_URI"), $regs)) { // Should be present on both *NIX/Apache module & NT/CGI $this->self = $regs[1]; return(1); } elseif ($temp = getenv("PATH_INFO") != "") { // NT/CGI $this->self = $temp; return(2); } else { $this->self = getenv("PHP_SELF"); // If still NT/CGI, then this will be undesired (something like "/php4/php.exe/path/name.ext" instead of "/path/name.exe"), but should be left with *NIX/Apache module only now return(3); } } /** * constructor, only calls method <code>setSelf</code> on instantiation */ function Globals() { $this->self_result = $this->setSelf(); } } ?>