This class allows you to easily manipulate every aspect of an HTML page while it is being created. I wrote this class to be the main foundation for all of the code that I write. With this class you will be able to:
This class allows each and every one of your pages to be truly dynamic. Something that is extremely difficult when using templates with separate header and footer files for layout.
This is the constructor for the class, and it creates a page object.
$title | You can set the page title here if you already know what it's going to be. The default is your website's name as defined in SITE_NAME. |
Example | $page = new Page ('Page Title'); |
This sets the page's doctype, and makes the appropriate declaration for it in the head section of your html.
You can set this site wide by defining DOCTYPE, and still override it at any time by using this method.
$type | Either 'html', or 'xhtml'. The default is 'html'. |
$standard | Either 'strict', 'transitional', or 'frameset'. The default is 'strict'. |
Example | $page->doctype ('xhtml', 'transitional'); |
If you're particular about who views a certain page you can set permissions here.
$users | Can be 'users', 'admin', or 'others'. If 'users', only those who have signed in and have a $_SESSION['user_id'] can view the page. If 'admin', only those who have $_SESSION['admin'] set can access the page. If 'others', only those who have no $_SESSION['user_id'] can access the page. |
$admin | This is for 'admin' access, and allows you to create unlimited levels of access based on the users $_SESSION['admin'] value. The default is 1. |
Example | $page->access ('users'); |
This is a handy method for forms especially. It sends the user somewhere else if needed.
$url | Where the user is sent. The default is an empty string which sends them to the BASE_URL. |
$message | What you would like to tell the user when they are ejected. Use '<br />' tags for newlines. |
Example | $page->eject ('sorry.html', "It's nothing personal."); |
Sets or changes the page title.
$title | What you want the page title to be. Leave blank if you just want the return value. |
Returns | The current page title. |
Example | $page->title ('PHP Page Class'); |
Sets the content for the meta description tag - default is the $page->title().
$description | a string |
Example | $page->description ('Allows you to easily manipulate every aspect of an html page while it is being created.'); |
Sets the content for the meta keywords tag - default is the $page->title().
$keywords | a string |
Example | $page->keywords ('php, html, page, class'); |
Tells the search engines if you would like them to crawl and index your page.
$allow | Set to false if you don't want robots (crawlers) to index or follow this page. Default is true. |
Example | $page->robots (false); |
This declares the character set for your page.
$charset | Default is 'utf-8'. |
Example | $page->charset ('iso-8859-1'); |
Use this method to include any external files in the head section of your page.
$external_file | You can link to one file or many (in an array), and you can call this method as many times as you want. If there are any duplicates they will be taken out. To include anything else such as a google maps api you can include it here, you just need to spell everything out. |
$prepend | Set to 'true' if you want the $external_file(s) to come first. Default is 'false'. |
Example | $page->link ('/include/head/style.css'); $page->link (array('/include/head/layout.css', '/include/head/style.css', '/include/head/fancy.js', '/include/head/favicon.ico')); // the duplicate style.css will be thrown out $page->link ('<script type="text/javascript" src="http://maps.google.com/maps?file=api&v=2&key=' . $key . '"></script>'); |
This method allows you to aggregate all of your jQuery calls and put them all under one $(document).ready(function(){}) in the head section of your page.
$code | Your jQuery code |
$oneliner | If you don't want this code to be a oneliner in your head section then set this to false. It comes in handy for debugging. |
Example | $page->jquery('$("p.neat").addClass("ohmy").show("slow");'); |
Sometimes you need to put something in the body tag, so here you go.
$insert | a string |
Example | $page->body ('onload="googleMaps()" onunload="GUnload()"'); |
This method allows you to add or remove key and value pairs from a url string.
$action | What you would like to do to the url. Either 'add', 'delete', 'ampify', or just leave it blank if you just want the current url returned. |
$url | The url string, if you have one. The default is the page's current url. |
$key | The key you either want to add, or delete. |
$value | The value of the key you want to add. |
Returns | Your modified url. |
Example | $url = 'http://www.mysite.com/content.php?showme=more&now=please'; $url = $page->url ('delete', $url, 'now'); $url = $page->url ('add', $url, 'showme', 'themoney'); $url = $page->url ('add', $url, 'hurryit', 'up'); $url = $page->url (); // Returns the current page's url $url = $page->url ('ampify'); // Turn the &'s into &'s (handy for inserting into a logout string) |
Returns the entire page, so this is the last method of all that you will call.
$html | The entire content of your page between the body tags. If you echo or print anything before this method call, it will screw up the whole (x)html layout. So instead of echo or print, just keep adding everything to the $html variable then echo $page->display ($html); and there you have it - fast, efficient, beautiful code. |
Returns | Everything from beginning to end. |
Example | echo $page->display ($html); |
<?php /* * author: Kyle Gadd * documentation: http://www.php-ease.com/classes/page.html * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ define ('SITE_NAME', 'Your Website'); define ('BASE', str_replace($_SERVER['SERVER_NAME'], '', $_SERVER['DOCUMENT_ROOT'])); // The Base Root Directory Name define ('BASE_URI', $_SERVER['DOCUMENT_ROOT'] . '/'); // The HTML Directory Name define ('BASE_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/'); // Your Website's Full URL Address define ('DOCTYPE', 'xhtml strict'); class Page { private $title = ''; private $description = ''; private $keywords = ''; private $robots = true; private $doctype = ''; private $xhtml = true; private $charset = 'utf-8'; private $include = array(); private $jquery = array(); private $body = ''; function __construct ($title='') { if (!empty($title)) { $this->title = $title; } elseif (defined('SITE_NAME')) { $this->title = SITE_NAME; } if (defined('DOCTYPE')) { list($type, $standard) = explode(' ', DOCTYPE); $this->doctype ($type, $standard); } else { $this->doctype ('xhtml', 'strict'); } } public function doctype ($type='html', $standard='strict') { if (in_array($standard, array('strict', 'transitional', 'frameset'))) { if ($type == 'html') { $this->xhtml = ''; switch ($standard) { case 'strict': $this->doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'; break; case 'transitional': $this->doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'; break; case 'frameset': $this->doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'; break; } } elseif ($type == 'xhtml') { $this->xhtml = ' /'; switch ($standard) { case 'strict': $this->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; break; case 'transitional': $this->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; break; case 'frameset': $this->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'; break; } } } } public function access ($user, $level=1) { switch ($user) { case 'users': if (!isset($_SESSION['user_id'])) $this->eject('sign_in/'); break; case 'admin': if (!isset($_SESSION['admin']) || $_SESSION['admin'] == 0 || $_SESSION['admin'] > $level) $this->eject(); break; case 'others': if (isset($_SESSION['user_id'])) $this->eject(); break; } } public function eject ($where='', $msg='') { if (stristr($where, BASE_URL)) { $url = $where; } else { $url = BASE_URL . $where; } if (ob_get_length()) ob_end_clean(); if (empty($msg)) { $url = str_replace('&', '&', $url); header("Location: $url"); } else { echo '<script type="text/javascript"> var msg = confirm("' . str_replace(array('<br />', '<br>'), "\\n", addslashes($msg)) . '"); if (msg == true) { window.location = "' . $url . '"; } else { window.location = "' . $url . '"; } </script>'; echo $msg . '<br /><br /><a href="' . $url . '">Click here to continue.</a>'; } exit; } public function title ($title='') { if (!empty($title)) $this->title = $title; return $this->title; } public function description ($description) { $this->description = $description; } public function keywords ($keywords) { $this->keywords = $keywords; } public function robots ($robots) { if (is_bool($robots)) $this->robots = $robots; } public function charset ($charset) { $this->charset = $charset; } public function link ($link, $prepend=false) { if (!is_array($link)) $link = array($link); if ($prepend) { $this->include = array_merge($link, $this->include); } else { foreach ($link as $value) $this->include[] = $value; } } public function jquery ($code, $oneliner=true) { if ($oneliner) $code = $this->oneliner($code); $this->jquery[] = $code; } public function body ($body) { $this->body = $body; } public function url ($action='', $url='', $key='', $value=NULL) { $protocol = ($_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://'; if (empty($url)) $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if ($action == 'ampify') return $this->ampify($url); $url = str_replace ('&', '&', $url); if (empty($action) && empty($key)) { // clean the slate $url = explode('?', $url); return $url[0]; // no amps to convert } $fragment = parse_url ($url, PHP_URL_FRAGMENT); if (!empty($fragment)) { $fragment = '#' . $fragment; // to add on later $url = str_replace ($fragment, '', $url); } if ($key == '#') { if ($action == 'delete') $fragment = ''; elseif ($action == 'add') $fragment = '#' . urlencode($value); return $this->ampify($url . $fragment); } $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); $url = substr($url, 0, -1); $value = urlencode($value); if ($action == 'delete') { return $this->ampify($url . $fragment); } elseif ($action == 'add') { if (strpos($url, '?') === false) { return ($url . '?' . $key . '=' . $value . $fragment); // no amps to convert } else { return $this->ampify($url . '&' . $key . '=' . $value . $fragment); } } } public function display ($content='') { $html = ''; $type = ($this->xhtml) ? 'xhtml' : 'html'; $frameset = false; if (strpos($content, '<frame ') !== false) { // Then this is a frameset ... $frameset = true; $this->doctype($type, 'frameset'); } elseif (strpos($this->doctype, 'frameset') !== false) { // If we're here then it's not ... $this->doctype($type, 'transitional'); } $html .= $this->doctype . "\n"; $html .= '<html'; if ($this->xhtml) $html .= ' lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"'; $html .= '>' . "\n"; $html .= '<head>' . "\n"; $html .= $this->meta_tags(); $html .= $this->include_scripts(); $html .= '</head>' . "\n"; $html .= ($frameset) ? '<frameset' : '<body'; if (!empty($this->body)) $html .= ' ' . $this->body; $html .= '>'; $html .= "\n " . trim($content); $html .= ($frameset) ? "\n</frameset>" : "\n</body>"; $html .= "\n</html>"; if (!$this->xhtml) $html = str_replace(' />', '>', $html); return $html; } private function meta_tags () { $tags = ' <meta http-equiv="content-type" content="text/html; charset=' . $this->charset . '" />' . "\n"; $tags .= ' <title>' . $this->title . '</title>' . "\n"; $description = (!empty($this->description)) ? $this->description : $this->title; $keywords = (!empty($this->keywords)) ? $this->keywords : $this->title; $tags .= ' <meta name="description" content="' . $description . '" />' . "\n"; $tags .= ' <meta name="keywords" content="' . $keywords . '" />' . "\n"; if (!$this->robots) $tags .= ' <meta name="robots" content="noindex, nofollow" />' . "\n"; return $tags; } private function include_scripts () { $scripts = $this->combine_scripts($this->sort_scripts($this->include)); if (!empty($this->jquery)) { $this->jquery = array_unique($this->jquery); $scripts .= ' <script type="text/javascript">$(document).ready(function(){' . "\n "; $scripts .= implode("\n ", $this->jquery); $scripts .= "\n })</script>\n"; } return $scripts; } private function sort_scripts ($array) { // used in $this->include_scripts() $array = array_unique($array); $scripts = array(); foreach ($array as $script) { $parts = explode('.', $script); $ext = array_pop($parts); $name = implode('.', $parts); switch ($ext) { case 'ico': $scripts['ico'] = $script; break; case 'css': $scripts['css'][] = $name; break; case 'js': $scripts['js'][] = $name; break; default: $scripts['other'][] = $script; break; } } return $scripts; } private function combine_scripts ($sorted) { // used in $this->include_scripts() if (empty($sorted)) return ''; $scripts = array(); if (isset($sorted['ico'])) { $scripts[] = '<link rel="shortcut icon" type="image/x-icon" href="' . $sorted['ico'] . '" />'; } if (isset($sorted['css'])) { foreach ($sorted['css'] as $script) { $scripts[] = '<link rel="stylesheet" type="text/css" href="' . $script . '.css" />'; } } if (isset($sorted['js'])) { foreach ($sorted['js'] as $script) { $scripts[] = '<script type="text/javascript" src="' . $script . '.js"></script>'; } } if (isset($sorted['other'])) $scripts = array_merge($scripts, $sorted['other']); return ' ' . implode("\n ", $scripts) . "\n"; } private function oneliner ($code) { return preg_replace('/\s(?=\s)/', '', str_replace(array("\r\n", "\r", "\n"), ' ', $code)); } private function ampify ($string) { // used in $this->url return str_replace(array('&', '&'), array('&', '&'), $string); } } ?>