<?php
/***************************************
** Title........: Template class
** Filename.....: class.template.inc
** Author.......: Richard Heyes
** Version......: 1.3
** Notes........:
** Last changed.: 13/08/2000
** Last change..: Added 5 new functions.
***************************************/

    
define("SEP","**");

/**
 * Template class
 *
 * This class is very fast in filling a template with specific values for
 * the aplication
 * @package   Include
 * @author    Richard Heyes
 */
        
class template{

                var 
$var_names = array();
                var 
$files = array();
                var 
$start '{';
                var 
$end '}'/* var $start = ' */

        /***************************************
        ** Function to load a template into
        ** the class.
        ***************************************/
                
function load_file($file_id$filename){
                        
$this->files[$file_id] = fread($fp fopen($filename'r'), filesize($filename));
                        
fclose($fp);
                } 
/* function load_file($file_id, $... */


        /***************************************
        ** Function to load a template into
        ** the class.
        ***************************************/
                
function set_identifiers($start$end){
                        
$this->start $start;
                        
$this->end $end;
                } 
/* function set_identifiers($star... */

        /***************************************
        ** This function is used only by the
        ** register() method, for going through
        ** arays and extracting the values.
        ***************************************/

    // Herman Suijs: 
    // Changed name to be array_key instead of array_value
    // Value of a specific key can be specified.
    // Array with only values should still work.
                
function traverse_array($file_id$array){
                        while(list(
$name,$value) = each($array)){
                if(
is_long($name)||empty($name)||!isset($name)) {
                  
$name=$value;
                  
$value="";
         
/* if(is_long($name)||empty($name...
          * */

                                if(
is_array($name)) $this->traverse_array($file_id$name);
                                else {
                  if (
$value!=""$valueSEP.serialize($value);
                  
$this->var_names[$file_id][] = $name .  $value;
           
/* else
            * */
}
                        } 
/* while(list($name,$value) = eac... */
                
/* function traverse_array($file_... */

        /***************************************
        ** Function to register a variable(s).
        ***************************************/
                
function register($file_id$var_name){
                        if(
is_array($var_name)){
                                
$this->traverse_array($file_id$var_name);
                        }elseif(
$var_name != ''){ /*
      if(is_array($var_name)) */
                                
if(is_long(strpos($var_name',')) == TRUE){
                                        
$var_name explode(','$var_name);
                                        for(
reset($var_name); $current current($var_name); next($var_name)) $this->var_names[$file_id][] = trim($current);
                                }else{ 
/* if(is_long(strpos($var_name, '...
    */
                                        
$this->var_names[$file_id][] = $var_name;
  
/* if(is_long(strpos($var_name, '... */                               }
  
/* if(is_array($var_name)){ ... }... */                       }
                } 
/* function register($file_id, $v... */

        /***************************************
        ** Function to include another file.
        ** eg. A header/footer.
        ***************************************/
                
function include_file($file_id$filename){
                        if(
file_exists($filename)){
                                
$include fread($fp fopen($filename'r'), filesize($filename));
                                
fclose($fp);
                        }else 
$include '[ERROR: "'.$filename.'" does not exist.]';

                        
$tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<include filename="'.$filename.'">'), strlen('<include filename="'.$filename.'">'));
                        
$this->files[$file_id] = str_replace($tag$include$this->files[$file_id]);
                } 
/* function include_file($file_id... */

        /***************************************
        ** Function for reading and parsing the
        ** html file for normal variables. Also
        ** now checks for include tags and if
        ** necessary calls include_file()
        ***************************************/
                
function parse($file_id){
                        
$file_ids explode(','$file_id);
                        for(
reset($file_ids); $file_id trim(current($file_ids)); next($file_ids)){
                                while(
is_long($pos strpos(strtolower($this->files[$file_id]), '<include filename="'))){
                                        
$pos += 19;
                                        
$endpos strpos($this->files[$file_id], '">'$pos);
                                        
$filename substr($this->files[$file_id], $pos$endpos-$pos);
                                        
$this->include_file($file_id$filename);
                                } 
/* while(is_long($pos = strpos(st... */

                                
if(isset($this->var_names[$file_id]) AND count($this->var_names[$file_id]) > 0){
                                        for(
$i=0$i<count($this->var_names[$file_id]); $i++){
                                                
$temp_var $this->var_names[$file_id][$i];

                        
// Make it possible to
                        // contain value.
                        
if (is_long($pos=strpos($temp_var,SEP))){
                          
$temp_name=substr($temp_var,0,$pos);
                          
$temp_value=unserialize(substr($temp_var,$pos+strlen(SEP)));
                } else {
                          global $
$temp_var;
                          
$temp_name=$temp_var;
                          
$temp_value=$$temp_var;
            }
                          
//echo "$temp_var - $temp_name - $temp_value<br>";

                                                
if(is_long(strpos($this->files[$file_id], $this->start.$temp_name.$this->end))){
                                                        
//global $$temp_var;
                                                        
$this->files[$file_id] = str_replace($this->start.$temp_name.$this->end$temp_value$this->files[$file_id]);

                                                }elseif(
is_long(strpos($this->files[$file_id
        
/* if(is_long(strpos($this->files...
         * */
], $this->start.$temp_name.'()'.$this->end))){
                                                        
//global $$temp_var;
                                                        
$arguments = array();
                                                        for(
$i=0$i<count($temp_value); $i++) $arguments[] = $temp_value[$i];
                                                        if(
count($arguments) > 0$arguments '"'.implode('", "'$arguments).'"'; else $arguments '';
                                                        eval(
'$output = '.$temp_name.'('.$arguments.');');
                                                        
$this->files[$file_id] = str_replace($this->start.$temp_name.'()'.$this->end$output$this->files[$file_id]);
                                                }
                                        } 
/* if(isset($this->var_names[$fil...  */
                                
/* for(reset($file_ids); $file_id... */
                        
/* function parse($file_id) */
                
/* class template */

        /***************************************
        ** Function for parsing an array.
        ***************************************/
                
function parse_loop($file_id$array_name){
                        global $
$array_name;
                        
$loop_code '';

                        
$start_pos strpos(strtolower($this->files[$file_id]), '<loop name="'.strtolower($array_name).'">') + strlen('<loop name="'.strtolower($array_name).'">');
                        
$end_pos strpos(strtolower($this->files[$file_id]), '</loop name="'.strtolower($array_name).'">');

                        
$loop_code substr($this->files[$file_id], $start_pos$end_pos-$start_pos);

                        
$start_tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.strtolower($array_name).'">'),strlen('<loop name="'.strtolower($array_name).'">'));
                        
$end_tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.strtolower($array_name).'">'),strlen('</loop name="'.strtolower($array_name).'">'));

                        if(
$loop_code != ''){
                                
$new_code '';
                                for(
$i=0$i<count($$array_name); $i++){
                                        
$temp_code $loop_code;
                                                                                while(list(
$key,) = each(${$array_name}[$i])){
                                                                                            
$temp_code str_replace($this->start.$key.$this->end,${$array_name}[$i][$key], $temp_code);
                                        }
                                        
$new_code .= $temp_code;
                                } 
/* for($i=0; $i<count($$array_nam... */
                                
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag$new_code$this->files[$file_id]);
                        } 
/* if($loop_code != '') */
                
/* function parse_loop($file_id, ... */

        /***************************************
        ** Function for parsing a Mysql result
        ** set.
        ***************************************/
                
function parse_sql($file_id$result_name){
                        global $
$result_name;
                        
$loop_code '';

                        
$start_pos strpos(strtolower($this->files[$file_id]), '<loop name="'.strtolower($result_name).'">') + strlen('<loop name="'.strtolower($result_name).'">');
                        
$end_pos strpos(strtolower($this->files[$file_id]), '</loop name="'.strtolower($result_name).'">');

                        
$loop_code substr($this->files[$file_id], $start_pos$end_pos-$start_pos);

                        
$start_tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.strtolower($result_name).'">'),strlen('<loop name="'.strtolower($result_name).'">'));
                        
$end_tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.strtolower($result_name).'">'),strlen('</loop name="'.strtolower($result_name).'">'));

                        if(
$loop_code != ''){
                                
$new_code '';
                                
$field_names = array();
                                for(
$i=0$i<mysql_num_fields($$result_name); $i++) $field_names[] = mysql_field_name($$result_name,$i);
                                while(
$row_data mysql_fetch_array($$result_nameMYSQL_ASSOC)){
                                        
$temp_code $loop_code;
                                        for(
$i=0$i<count($field_names); $i++){
                                                
$temp_code str_replace($this->start.$field_names[$i].$this->end$row_data[$field_names[$i]], $temp_code);
                                        } 
/* for($i=0;
      $i<count($field_name... */
                                        
$new_code.= $temp_code;
                                } 
/* while($row_data = mysql_fetch_... */
                                
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag$new_code$this->files[$file_id]);
                        } 
/* if($loop_code != '') */
                
/* function parse_sql($file_id, $... */

        /***************************************
        ** Function for parsing a Postgres result
        ** set.
        ***************************************/
                
function parse_pgsql($file_id$result_name){
                        global $
$result_name;
                        
$loop_code '';

                        
$start_pos strpos(strtolower($this->files[$file_id]), '<loop name="'.strtolower($result_name).'">') + strlen('<loop name="'.strtolower($result_name).'">');
                        
$end_pos strpos(strtolower($this->files[$file_id]), '</loop name="'.strtolower($result_name).'">');

                        
$loop_code substr($this->files[$file_id], $start_pos$end_pos-$start_pos);

                        
$start_tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.strtolower($result_name).'">'),strlen('<loop name="'.strtolower($result_name).'">'));
                        
$end_tag substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.strtolower($result_name).'">'),strlen('</loop name="'.strtolower($result_name).'">'));

                        if(
$loop_code != ''){
                                
$new_code '';
                                
$field_names = array();
                                for(
$i=0$i<pg_numfields($$result_name); $i++) $field_names[] = pg_fieldname($$result_name,$i);
                                for(
$i=0$i<pg_numrows($$result_name) AND $row_data pg_fetch_array($$result_name$i); $i++){
                                        
$temp_code $loop_code;
                                        for(
$j=0$j<count($field_names); $j++){
                                                
$temp_code str_replace($this->start.$field_names[$j].$this->end$row_data[$field_names[$j]], $temp_code);
                                        } 
/* for($j=0;
      $j<count($field_name... */
                                        
$new_code.= $temp_code;
                                } 
/* for($i=0; $i<pg_numrows($$resu... */
                                
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag$new_code$this->files[$file_id]);
                        } 
/* if($loop_code != '') */
                
/* function parse_pgsql($file_id,... */

        /***************************************
        ** Function for printing the resulting
        ** file(s).
        ***************************************/
                
function print_file($file_id){
                        if(
is_long(strpos($file_id',')) == TRUE){
                                
$file_id explode(','$file_id);
                                for(
reset($file_id); $current current($file_id); next($file_id)) echo $this->files[trim($current)];
                        }else{ 
/* if(is_long(strpos($file_id, ',... */
                                
echo $this->files[$file_id];
  
/* if(is_long(strpos($file_id, ',... */                       }
                } 
/* function print_file($file_id) */

        /***************************************
        ** Function for returning the resulting
        ** file(s).
        ***************************************/
                
function return_file($file_id){
                        
$ret '';
                        if(
is_long(strpos($file_id',')) == TRUE){
                                
$file_id explode(','$file_id);
                                for(
reset($file_id); $current current($file_id); next($file_id)) $ret .= $this->files[trim($current)];
                        }else{ 
/* if(is_long(strpos($file_id, ',... */
                                
$ret .= $this->files[$file_id];
  
/* if(is_long(strpos($file_id, ',... */                       }
                        return 
$ret;
                } 
/* function return_file($file_id) */

        /***************************************
        ** Parses and then immediately prints
        ** the file. This function added by
        ** Bruce Christensen.
        ***************************************/
                
function pprint($file_id$replacements ''){
                        
$this->register($file_id$replacements);
                        
$this->parse($file_id);
                        
$this->print_file($file_id);
                } 
/* function pprint($file_id, $rep... */

        /***************************************
        ** Parses and then immediately returns
        ** the file's contents. Function added
        ** by Bruce Christensen.
        ***************************************/
                
function pget($file_id$replacements ''){
                        
$this->register($file_id$replacements);
                        
$this->parse($file_id);
                        return 
$this->return_file($file_id);
                } 
/* function pget($file_id, $repla... */

        /***************************************
        ** Loads a file, parses it, and prints it.
        ** This function added by Bruce Christensen.
        ***************************************/
                
function pprint_file($filename$replacements ''){
                        for(
$file_id=1; isset($this->files[$file_id]); $file_id++);
                        
$this->load_file($file_id$filename);
                        
$this->pprint($file_id$replacements);
                        unset(
$this->files[$file_id]);
                } 
/* function pprint_file($filename... */

        /***************************************
        ** Loads, parses and then immediately
        ** returns the file's contents.
        ** Function added by Bruce Christensen.
        ***************************************/
                
function pget_file($filename$replacements ''){
                        for(
$file_id=1; isset($this->files[$file_id]); $file_id++);
                        
$this->load_file($file_id$filename);
                        return 
$this->pget($file_id$replacements);
                } 
/* function pget_file($filename, ... */

        
// End of class