Login   Register  
PHP Classes
elePHPant
Icontem

File: class.arbplate.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Paul Arbogast  >  arbplate  >  class.arbplate.php  >  Download  
File: class.arbplate.php
Role: ???
Content type: text/plain
Description: Main Class File
Class: arbplate
Author: By
Last change:
Date: 13 years ago
Size: 11,786 bytes
 

Contents

Class file image Download
<?PHP

//
// File: class.arbplate.php
//  Ver: 0.8a
// Desc: Main class file for arbplate
// Auth: Paul Arbogast 
// *******************
// Latest version is available at http://arbplate.sourceforge.net
// Send Questions or comments to arbo@att.net
//

/* Copyright (C) 2001 Paul Arbogast

   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 2
   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, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

// to do:
//
//   
//   {if] tagset  ?  dunno if this is needed
//   make arbdb - db abstraction routines for use by arbplate - mysql done
//   make arbthenticate - user tracking/authentication
//


$ARB_CONFIG = parse_ini_file( "arbplate.ini" );   // read config file

if ( ! is_array( $ARB_CONFIG ) ) {
  trigger_error( "[Could Not Read INI file!]" , E_USER_ERROR );
}

include( "arberr.php" );                 // include error handling routines

if ( ! defined( "INCLUDED_ARBDB" ) ) {
  include( "class.arbdb.php" ) ;         // include database class
}

$ADB = new arbdb();

// main class

class arbplate {

  var $VERSION          = "0.8a";
  var $TEMP_NAME        = "";           // Name of root template being parsed
  var $ROOT_DIR			= "";			// Contains root directory to Templates
  var $SKIN             = "";           // Contains directory to current Theme
  

  // Array of template variables
  var $VAR_STATIC       = array ();    

  // Array of link values
  var $VAR_LINKS        = array ( array () );  

  // Array of tags & assosicated functions
  var $TAG_MATRIX       = array ();     
  
   

  // --------  Methods  ----------

  // --------------------
  // arbplate: The main class constructor
  //   useage: $arb->arbplate( );
  //
  // You can send TemplatePath, changing the default path to the template files
  // and/or you can send Theme_Skin to define an other than default theme.
  //
  // By default your templates will be in templatepath/themeskin.  So you can
  // set up a multi-theme system because it will simply be in a different directory.
  //
  
  function arbplate ( $TemplatePath = "" , $Theme_Skin = "" )
  {
    global $ARB_CONFIG;

	if ( ! empty( $Theme_Skin ) ) 
	{
	  $this -> SKIN = $Theme_Skin;
	} 
	else 
	{
	  $this -> SKIN = $ARB_CONFIG['DEF_SKIN'];
	}

    if ( !empty( $TemplatePath ) ) 
	{
	  $this -> set_base( $TemplatePath );
	}
	else
	{
	  $this -> set_base( $ARB_CONFIG['DEF_ROOT'] );
	}

    $this -> def_tags ( array ( "func"  => "replace_functions" ,
	                            "inc"   => "replace_includes" ,
								"var"   => "replace_variables" ,
								"rep"   => "replace_repeats" ,
								"link"  => "replace_link" ,
								"query" => "replace_query" )); 
					
	// load up variables and links from database 
	$this -> load_to_static();
    $this -> load_to_links();


	return;
  }


  // --------------------
  // set_base: Sets root directory for templates
  //   useage: used by arbplate constructor
  //

  function set_base ( $Root_Dir )
  {
  
	if ( substr( $Root_Dir , -1 ) != 47 )
	{
	  $Root_Dir = $Root_Dir . chr(47);
	}
    
	$Root_Dir = $Root_Dir . $this -> SKIN . chr(47) ;

	if ( is_dir( $Root_Dir ) ) 
	{
	  $this -> ROOT_DIR = $Root_Dir;
	}
	else
	{
	  $this -> ROOT_DIR = "";
	  trigger_error ( "Specified ROOT directory incorrect: [$Root_Dir]" , E_USER_NOTICE ) ;
	}
	
  }


  // --------------------
  // def_tags: Set up Tag to Function matrix
  //   useage: used by arbplate constructor
  //

  function def_tags ( $VarList )
  {
	
	while ( list ( $VarName , $VarVal ) = each ( $VarList ) )
	{
	  $this -> TAG_MATRIX[ "$VarName" ] = $VarVal;
	}
	
	return;
  }

  
  // --------------------
  // def_static: 
  //

  function def_static ( $VarList )
  {
	
	while ( list ( $VarName , $VarVal ) = each ( $VarList ) )
	{
	  $this -> VAR_STATIC[ "$VarName" ] = $VarVal;
	}
	
	return;
  }


  // --------------------
  // load_to_static: Loads all variables from database to memory 
  //                 for quicker access.
  //
  //

  function load_to_static ( )
  {

    global $ADB;

  	$ADB -> arbdb_query( "SELECT * FROM varis" );

    while ( $row = $ADB -> arbdb_next() ) 
	{
	  $this -> def_static( array( $row["var_name"] => $row["var_val"] ) ); 
	}
	 
  }


  // --------------------
  // def_links: 
  //

  function def_links ( $VarList )
  {
	
	while ( list ( $VarName ) = each ( $VarList ) )
	{
	  
	  while ( list ( $Key , $Var2 ) = each ( $VarList["$VarName"] ) ) 
	  {
        $this -> VAR_LINKS[ $VarName ][ $Key ] = $Var2 ;
	  }
	}
	
	return;
  }

  // --------------------
  // load_to_links: Loads all link info from database to memory for 
  //                quicker access.
  //
  
  function load_to_links ( )
  {

    global $ADB;

  	$ADB -> arbdb_query( "SELECT * FROM links" );

    while ( $row = $ADB -> arbdb_next() ) 
	{
	  $this -> def_links( array( $row["l_name"] => array ( $row["l_desc"] , $row["l_link"] ) ) ); 
	}
	 
  }

  
  // --------------------
  //  parse: Read in template, replace everything, print result
  // useage: $arb->parse( "templatename.arb" );
  //
  // Templates can be named any legal file extension.
  //

  function parse ( $TemplateName ) 
  {

	$filename = $this -> ROOT_DIR . $TemplateName ;
	$contents = fread( $fp = fopen( $filename , 'r' ), filesize( $filename ) );
    fclose($fp);

    $this -> TEMP_NAME = $TemplateName;

    // replace all tags
	$contents = $this -> rep_func ( $contents  );

    print $contents;

  }


  // --------------------
  // rep_func: takes input and replaces all tagsets of type $type
  //

  function rep_func ( $line )
  {
  
	$nv = 0;
	$line = " " . $line . " ";

	while ( preg_match( "/^(.*)\{(inc|var|rep|func|link|query)\}(.+)$/sU", $line, $arr)) 

	{
	  $blah[$nv] = array ( $arr[1], " ", $arr[2], $arr[3] );
	  $line = $arr[3];
	  $repnum = 0;

	  while ( preg_match( "/^(.*)\{([\/]*)(inc|var|rep|func|link|query)\}(.+)$/sU", $line, $arrsub))

	  {
	    $nv++;
		$blah[$nv] = array ( $arrsub[1], $arrsub[2], $arrsub[3], $arrsub[4]);

 		if ($arrsub[2] =="")
		{
		  $line = $arrsub[4];
		}
		else
		{
		  if ($blah[$nv][2] == $blah[$nv-1][2])
		  {
		    $repnum++;
			$thefunc = $this->TAG_MATRIX[ $blah[$nv][2] ];
			$repl = $this->$thefunc( $blah[$nv][0] );

			$line = $blah[$nv-1][0] . $repl . $blah[$nv][3];

			if ($nv>1)
			{
			  $blah[$nv-2][3] = $tnts;
			}

			$nv--;$nv--;

			if ($nv<0)
			{
			  $nv = 0;
			  if (!(substr($line,0,1)==" "))
			    $line = " " . $line;

			  break;
			}
	      }
		}
	  }
	}
	return $line;
  }


  // --------------------
  // replace_count: used by replace_repeats to replace @@@@ tag with repetition number
  //

  function replace_count ( $line, $count )
  {
	
    $line = preg_replace( "|\@{4}|sU" , "$count" , $line ) ;
    // add another tok to replace count+1?

	return $line;
  }


  // --------------------
  // replace_repeats: {rep}X:string to repeat{/rep}
  //
  // returns X copies of 'string to repeat'. 
  // you can use @@@@ in string, which will be replaces
  // with the iteration number of that loop.
  //

  function replace_repeats ( $line )
  {
    
	$reparr = preg_split ("|:|sU", $line, 2 );
	
	$rtn     = "";
	$times = $reparr[0];

	settype( $times , "integer" );

	for ( $x = 0 ; $x < $times ; $x++ )
	{
      $rtn = $rtn . $this -> replace_count( $reparr[1] , $x );  // $reparr[2];
	}

	return $rtn;

  }


  // --------------------
  // replace_includes: {inc}filename.ext{/inc}
  //
  // Returns the contents of filename.ext, then will parse through that
  // template as well.
  //

  function replace_includes ( $incfile )
  {
   
    $filename = $this -> ROOT_DIR . $incfile ;

    if ( ! is_file( $filename ) ) 
	{
      trigger_error( "Not a good inc file: $filename" , E_USER_ERROR );
	}
	else
	{
	  $rtn = fread( $fp = fopen( $filename , 'r' ), filesize( $filename ) );

      fclose( $fp );
    }

	return $rtn;
  }


  // --------------------
  // replace_functions: replaces all {func}...{/func} tagsets
  //
  // NOTE: don't let unchecked user input go here!!!!
  //

  function replace_functions ( $funcname )
  {
   
   //echo "[".$funcname."]<br/>";
    ob_start(); 
    eval( $funcname ); 
    $rtn = ob_get_contents(); 
    ob_end_clean(); 
	    
	return $rtn;
  }


  // --------------------
  // replace_variables: {var}varname{/var}
  //
  // Returns value of variable.  Not only can you use the ones
  // defined in the varis table of your database, but you can
  // class variables (like ROOT_DIR), and PHP system variables.
  //

  function replace_variables ( $varname )
  {
   	
	$rtn = "";

    // need a way to prevent system vars (aka vars that aren't valid indexes
	//  for below line, from being checked below.. it causes a warning that is
	//  logged, and that could make for a big log file.
	//  doing an each(VAR_STATIC) would slow things down.. find another way.

    $rtn = $this -> VAR_STATIC[ $varname ];
	  
	if ($rtn == "")
	{

      // add ability to return system variables (php system that is)

	  $varname = "print \$this -> " . $varname . ";" ;
	  $rtn = $this -> replace_functions( $varname );

	}

	return $rtn;
  }


  // --------------------
  // replace_link: {link}A:name{/link}
  //
  // Returns actual link (i.e. http://some.link.here) if A is l.
  // Returns description of link if A is d.
  //
  // example:
  //
  //    <a href="{link}l:email{/link}">{link}d:email{/link}</a>
  //
  //    this allows you to change links and their descriptions
  //    from one spot (database) rather than searching all your
  //    template files.
  //

  function replace_link ( $linkname )
  {

    $reparr = preg_split ("|:|sU", $linkname, 2 );	

	if ( $reparr[0] == "l" ) 
	{
      $rtn = $this -> VAR_LINKS[ $reparr[ 1 ] ][1];
	} 
	if ( $reparr[0] == "d" ) 
	{
      $rtn = $this -> VAR_LINKS[ $reparr[ 1 ] ][0];
	} 

	return $rtn;
  }

 
  // --------------------
  // replace_query: {query}qstring:template{/query}
  //
  // run query with qstring, fill in returned row values into template
  //  (i.e. %1 is first field, $2 second, etc..)
  //
  // example: 
  //
  //    <table>
  //    {query}select username,email from users:<tr><td>%1</td><td>%2</td></tr>{/query}
  //    </table>
  //
  //    this outputs a table of users names and email addresses
  //

  function replace_query ( $query ) 
  {

    global $ADB;

    $rtn = "";
    $reparr = preg_split ("|:|sU", $query, 2 );	

  	$ADB -> arbdb_query( $reparr[0] );
    
	while ( $row = $ADB -> arbdb_next( "n" ) ) 
	{
  
      $xs = $reparr[1];

      for ( $x = 0; $x < count( $row ); $x++ ) 
	  {
        $xs = preg_replace( "|\%".($x + 1)."|sU" , $row[ $x ] , $xs ) ;
	  }

      $rtn = $rtn . $xs;
	}

    return $rtn;

  }

}  // end of arbplate class definition

?>