PHP Classes

File: TemplateBuilder.php

Recommend this page to a friend!
  Classes of E_MAIL FALSO   TemplateBuilder   TemplateBuilder.php   Download  
File: TemplateBuilder.php
Role: Class source
Content type: text/plain
Description: Class Source
Class: TemplateBuilder
Powerful and easy class to manage templates
Author: By
Last change: lala
Date: 21 years ago
Size: 10,429 bytes
 

Contents

Class file image Download
<?php /* +------------------------------------------------------------+ | Class TemplateBuilder - Version 2.5 - Copyright (C) 2002. | +------------------------------------------------------------+ | Authors: | | - Edson C. Melo <eneto@emcinfo.com.br> | | - Frederico R. Galvão <fred@nbconsultoria.com.br> | +------------------------------------------------------------+ */ Class TemplateBuilder { var $start_tag = "<%"; var $end_tag = "%>"; var $arquivo; var $buffer; var $line; var $BUGA_EXP; function print_error($msg) { echo("<font face=Verdana size=-2><b><font color=#ff0000>[Template Error]: </font></b>" . $msg . "</font><br><br>"); } function print_file_info() { $size = filesize($this->arquivo); $note = " \r<!-- \r::[Buga File Info]::::::::::::::::::::::::::::::::: \r::Arquivo:: $this->arquivo \r::Tamanho:: $size bytes \r::::::::::::::::::::::::::::::::::::::::::::::::::: \r!--> "; print($note); } function parse_template($arquivo) { $this->arquivo = $arquivo; $fd = $this->open_file(); $this->line = 0; while(!feof($fd)) { $this->line++; $this->buffer = fgets($fd, 8192); $BUGA_TAG = strstr($this->buffer, $this->start_tag); if(!$BUGA_TAG) print($this->buffer); else $this->interpreta_buga_exp(); } fclose($fd); $this->print_file_info(); } function open_file() { if(file_exists($this->arquivo)) { if(is_readable($this->arquivo)) { $fd = fopen($this->arquivo, "r"); if($fd) { $ret = $fd; } else { $ret = false; $this->print_error($this->arquivo . " - Não foi possível abrir o arquivo!"); exit; } } else { $ret = false; $this->print_error($this->arquivo . " - Permissão negada para a leitura do arquivo!"); exit; } } else { $ret = false; $this->print_error($this->arquivo . " - O arquivo não existe!"); exit; } return $ret; } function interpreta_buga_exp() { $n_char = strlen($this->buffer); if($n_char > 0) { $BUGA_TAG = strstr($this->buffer, $this->start_tag); if($BUGA_TAG) { $start_position = strpos($this->buffer, $this->start_tag); if(strstr($this->buffer, $this->end_tag)) $end_position = strpos($this->buffer, $this->end_tag); else { $this->print_error("Erro de sintaxe na linha : " . $this->line); exit; } for($i = 0; $i < $start_position; $i++) { print($this->buffer[$i]); } $this->BUGA_EXP = substr($this->buffer, ($start_position + 2), ($end_position - $start_position - 2)); $check = $this->valida_sintaxe(); if($check) { if($this->BUGA_EXP_TP == "Variável") $parse = $this->parse_var(); else // $this->BUGA_EXP_TP == "Função" $parse = $this->parse_func(); print($parse); $this->buffer = substr($this->buffer, ($end_position + 2)); return $this->interpreta_buga_exp(); } else { $this->print_error($this->BUGA_EXP_TP . " Inválida! Verifique sua sintaxe. ( <i>" . $this->BUGA_EXP . "</i> ) na linha : " . $this->line); exit; } } else { print($this->buffer); } } } function valida_sintaxe() { if(($this->BUGA_EXP[0] == "@") && (!strstr($this->BUGA_EXP, "(")) && (!strstr($this->BUGA_EXP, ")"))) { $this->BUGA_EXP_TP = "Variável"; if((ereg("^@[a-zA-Z_]", $this->BUGA_EXP) && ereg("[a-zA-Z_]$", $this->BUGA_EXP)) || (ereg("^@[a-zA-Z_]", $this->BUGA_EXP) && ereg("[0-9_]$", $this->BUGA_EXP)) || (ereg("^@[a-zA-Z_]", $this->BUGA_EXP) && ereg("]$", $this->BUGA_EXP))) $ret = true; else $ret = false; } else { $this->BUGA_EXP_TP = "Função"; if(ereg("^[a-zA-Z].+)$", $this->BUGA_EXP) || ereg("^@.+->[a-zA-Z].+)$", $this->BUGA_EXP)) $ret = true; else $ret = false; } return $ret; } function parse_var() { if((ereg("^@[a-zA-Z_]", $this->BUGA_EXP) && ereg("[a-zA-Z_]$", $this->BUGA_EXP)) || (ereg("^@[a-zA-Z_]", $this->BUGA_EXP) && ereg("[0-9_]$", $this->BUGA_EXP)) || (ereg("^@[a-zA-Z_]", $this->BUGA_EXP) && ereg("]$", $this->BUGA_EXP))) { $this->BUGA_EXP = ereg_replace("@", "", $this->BUGA_EXP); if(!strstr($this->BUGA_EXP, "->") && !strstr($this->BUGA_EXP, "[")) return $this->call_var($this->BUGA_EXP, '', '', "N"); elseif(strstr($this->BUGA_EXP, "->")) { if(!strstr($this->BUGA_EXP, "[") && !strstr($this->BUGA_EXP, "]")) { if(ereg("^(.+)->(.+)$", $this->BUGA_EXP, $arr)) return $this->call_var($arr[1], $arr[2], '', "O"); else { $this->print_error("Variável Inválida! Verifique sua sintaxe. ( <i>" . $this->BUGA_EXP . "</i> ) na linha : " . $this->line); exit; } } else { if(ereg("^(.+)->(.+)\[(.+)\]$", $this->BUGA_EXP, $arr)) { $arr[3] = ereg_replace("\"", "", $arr[3]); $arr[3] = ereg_replace("\'", "", $arr[3]); return $this->call_var($arr[1], $arr[2], $arr[3], "O"); } else { $this->print_error("Variável Inválida! Verifique sua sintaxe. ( <i>" . $this->BUGA_EXP . "</i> ) na linha : " . $this->line); exit; } } } else // strstr($this->BUGA_EXP, "[") { if(ereg("^(.+)\[(.+)\]$", $this->BUGA_EXP, $arr)) { $arr[2] = ereg_replace("\"", "", $arr[2]); $arr[2] = ereg_replace("\'", "", $arr[2]); return $this->call_var($arr[1], $arr[2], '', "A"); } else { $this->print_error("Variável Inválida! Verifique sua sintaxe. ( <i>" . $this->BUGA_EXP . "</i> ) na linha : " . $this->line); exit; } } } else { $this->print_error("Variável Inválida! Verifique sua sintaxe. ( <i>" . $this->BUGA_EXP . "</i> ) na linha : " . $this->line); exit; } } function call_var($part_i, $part_m, $part_f, $tipo) { global $$part_i; if($tipo == "N") return $$part_i; elseif($tipo == "A") { $var = $$part_i; return $var[$part_m]; } else // $tipo == "O" { if($part_f == "") return $$part_i->$part_m; else { $var = $$part_i->$part_m; return $var[$part_f]; } } } function parse_func() { $start_position = strpos($this->BUGA_EXP, "("); $end_position = strpos($this->BUGA_EXP, ")"); if(ereg("^@(.+)->(.+)\(", $this->BUGA_EXP, $arr)) { $this->is_class = true; $class_name = $arr[1]; $func_name = $arr[2]; } else { $this->is_class = false; $func_name = substr($this->BUGA_EXP, 0, $start_position); } if($start_position != ($end_position - 1)) $PARAMETER_LIST = substr($this->BUGA_EXP, ($start_position + 1), ($end_position - $start_position - 1)); else $PARAMETER_LIST = false; if($PARAMETER_LIST) { $this->PARAMETERS = explode(",", $PARAMETER_LIST); $N_PARAMETER = count($this->PARAMETERS); $args = ""; $this->ARG = $this->interpreta_parameter($N_PARAMETER, $args); } else $N_PARAMETER = 0; if($this->is_class) return $this->call_func($N_PARAMETER, $class_name, $func_name); else return $this->call_func($N_PARAMETER, '', $func_name); } function interpreta_parameter($N_PARAMETER, $argument) { if($N_PARAMETER == 0) { return $argument; } else { $atual_arg = array_shift($this->PARAMETERS); $atual_arg = trim($atual_arg); if($atual_arg[0] == "@") { $this->BUGA_EXP = $atual_arg; $parse = $this->parse_var(); $argument[] = $parse; } elseif(($atual_arg[0] == "\"") || ($atual_arg[0] == "'")) { $atual_arg = ereg_replace("\"", "", $atual_arg); $atual_arg = ereg_replace("\'", "", $atual_arg); $argument[] = $atual_arg; } elseif(ereg("[0-9]", $atual_arg)) { $argument[] = $atual_arg; } elseif(strstr($atual_arg, "(") && strstr($atual_arg, ")")) { $this->BUGA_EXP = $atual_arg; $check = $this->valida_sintaxe(); if($check) { $parse = $this->parse_func(); $argument[] = $parse; } else { $this->print_error("Função Inválida! Verifique sua sintaxe. ( <i>" . $this->BUGA_EXP . "</i> ) na linha : " . $this->line); exit; } } else { $this->print_error("Argumento Inválido! Verifique sua sintaxe. ( <i>" . $atual_arg . "</i> ) na linha : " . $this->line); exit; } $N_PARAMETER--; return $this->interpreta_parameter($N_PARAMETER, $argument); } } function call_func($N_PARAMETER, $class_name, $func_name) { global $$class_name; $list = ""; // variável utilizada na recursão da função build_arg_list(); if(!$class_name) { if(!$N_PARAMETER) return $func_name(); else return $this->build_parameter_list($N_PARAMETER, '', $func_name, $list); } else { if(!$N_PARAMETER) return $$class_name->$func_name(); else return $this->build_parameter_list($N_PARAMETER, $class_name, $func_name, $list); } } function build_parameter_list($N_PARAMETER, $class_name, $func_name, $list) { global $$class_name; if($N_PARAMETER == 0) { if(!$class_name) return $func_name($list); else return $$class_name->$func_name($list); } else { $atual_arg = array_shift($this->ARG); if(($N_PARAMETER - 1) == 0) $list .= $atual_arg; else $list .= $atual_arg . ", "; $N_PARAMETER--; return $this->build_parameter_list($N_PARAMETER, $class_name, $func_name, $list); } } } ?>