Return to Snippet

Revision: 52360
at October 21, 2011 02:15 by claudiowebdesign


Initial Code
<?php
        /*  --------------------------------------------------------------------
         *  Document.class.php - PHP class to quick start web page development
         *  Distributed under the Do-wathever-the-hell-you-want-with-it License
         *
         *  Web site:   http://claudiobonifazi.com
         *  Blog:       http://claudiobonifazi.com?p=4
         *  Email:      [email protected]
         *  Twitter:    @ClaudioBonifazi
         *  -------------------------------------------------------------------
         *
         *      Start any webpage this way:
         *
         *      <?php
         *              include '/path/Document.class.php';
         *              $html = new Document (
         *                                      'title'           => 'The page title tag',
         *                                      'description' => 'description tag',
         *                                      'keywords'        => 'keywords tag',
         *                                      'author'          => 'author tag',
         *                                      'language'        => 'en/de/fr/it/sp/whatever',
         *                                      'charset'         => 'utf-8/whatever',
         *                                      'favicon'         => 'faviconFileName.ico',
         *                                      'stylesheets' => array( 'reset.css'=>'all', 'main.css'=>'all', 'another.css'=>'media' ),
         *                                      'ieFallback'  => 'theCssFileReservedForOldBrowsers.css',
         *                                      'googleFonts' => array('that cool font i saw', 'that other one', 'great Scott!'),
         *                                      'scripts'         => array( 'jQuery.js', 'head.js', 'my.js', 'whatever.js' )
         *                                              );
         *      ?>
         *      You can leave blank anyone of the parameters, they all have a default value
         *      Anything written after this will be placed inside the <body> tag
         *      The closure tags will be automatically added.
         *
         */

        class Document{

                private $startTime;
                private $adminIP = '127.0.0.1';

                const FAVICONFOLDER = './';
                const CSSFOLDER         = './components/css/';
                const JSFOLDER          = './components/js/';
                private $head = array(
                                                'title'           => 'Untitled Document',
                                                'description' => '',
                                                'keywords'        => '',
                                                'author'          => '',
                                                'language'        => 'en',
                                                'charset'         => 'utf-8',
                                                'favicon'         => '',
                                                'stylesheets' => array(),
                                                'ieFallback'  => '',
                                                'googleFonts' => array(),
                                                'scripts'         => array()
                                        );

        
                public function __construct( $headOverride = array() ){

                        // this can be a solution if you can't set error reporting on php.ini or the .htaccess file
                        // error outputs get printed only to you ( if you properly set the $adminIP property )
                        if ( !empty($_SERVER['HTTP_CLIENT_IP']) ){
                                $ip = $_SERVER['HTTP_CLIENT_IP'];
                        }else{
                                if( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) )
                                        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                                else
                                        $ip = $_SERVER['REMOTE_ADDR'];
                        }
                        if( $ip == $this->adminIP ){
                                ini_set( 'display_errors', 'On' );
                                error_reporting( E_ALL | E_STRICT );
                        }else{
                                ini_set( 'display_errors', 0 );
                                error_reporting( NULL );
                        }

                        // output buffering via script (if not set by server settings)
                        ob_start ("ob_gzhandler");

                        // storing the script execution start time could be useful
                        $this->startTime = time();

                        //data management
                        $head = $this->head;
                        foreach( $headOverride as $tag=>$val ){
                                if( !isset($head[$tag]) ){
                                        trigger_error("There's no point in adding $tag head property, it won't be considered. Maybe you mispelled it?");
                                        unset($headOverride[$tag]);
                                }else{
                                        if( $head[$tag]!=$headOverride )
                                                $head[$tag] = $headOverride[$tag];
                                }
                        }
                        

                        // starting tags
                        echo "<!doctype html>",
                                 "\n<html lang=\"$head[language]\">",
                                 "\n\t<head>";

                        // initial meta tags
                        echo "\n\t\t<title>$head[title]</title>",
                                 "\n\t\t<meta name=\"description\" content=\"$head[description]\">",
                                 "\n\t\t<meta name=\"keyword\" content=\"$head[keywords]\">",
                                 "\n\t\t<meta name=\"author\" content=\"$head[author]\">",
                                 "\n\t\t<meta name=\"language\" content=\"$head[language]\">",
                                 "\n\t\t<meta name=\"charset\" content=\"$head[charset]\">";

                        // google web fonts
                        if( !empty($head['googleFonts']) )
                                foreach( $head['googleFonts'] as $n=>$name ){
                                        echo "\n\t\t<link rel=\"stylesheet\" href=\"http://fonts.googleapis.com/css?family=".urlencode($name)."\">";
                                }

                        // stylesheets
                        foreach( $head['stylesheets'] as $file=>$media )
                                echo "\n\t\t<link rel=\"stylesheet\" href=\"".Document::CSSFOLDER.$file."\" media=\"$media\">";

                        // Internet Explorer fallbacks
                        if( $head['ieFallback']!='.css' && $head['ieFallback']!='' )
                                echo "\n\t\t<!--[if lt IE 9]>",
                                         "\n\t\t\t<script type=\"text/javascript\" src=\"http://html5shiv.googlecode.com/svn/trunk/html5.js\"></script>",
                                         "\n\t\t\t<link rel=\"stylesheet\" href=\"".Document::CSSFOLDER.$head['ieFallback']."\" media=\"screen\">",
                                         "\n\t\t<![endif]-->";

                        // scripts ( I suggest you to use http://headjs.com )
                        foreach( $head['scripts'] as $n=>$file )
                                echo "\n\t\t<script src=\"".Document::JSFOLDER.$file.'"></script>';

                        // favicon
                        if( !empty($head['favicon']) )
                                echo "\n\t\t<link rel=\"shortcut icon\" href=\"".Document::FAVICONFOLDER.$head['favicon'].'">';

                        // close and initialize body section
                        echo "\n\t</head>",
                                 "\n\t<body>",
                                 "\n";
                }
                
                public function __destruct(){
                        // when execution ends those ending tags will be automatically added
                        echo "\n\t</body>",
                                 "\n</html>";
                }
        }
?>

Initial URL
http://claudiobonifazi.com/snippets/Document

Initial Description
This is a little PHP class that permits to start quickly the creation of a webpage.

When you declare an instance of the class it automatically starts the output buffering and gzip compression, it outputs all the proper tags (doctype, meta, stylesheets, scripts, etc.) until the .

Everything you write after that gets actually placed inside the body, and when the page ends the and tags gets automatically added at the bottom.

In addition to this, the class gets your IP so to display all the possible error/warning messages to you and none to the random users.

EXAMPLES/USAGE: http://claudiobonifazi.com/snippets/Document

Initial Title
Document.class.php

Initial Tags
php

Initial Language
PHP