<?php
/*
* Project: esTigiTpl, esTigi Templates
* File: template.inc.php
* Author: Felipe Escoto <hide@address.com>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The latest version of template_lite can be obtained from:
* http://estigi.org
*
*/
/* v 0.0.7 */
/* Simple template class */
class tpl{
var $template; //The template as it comes
var $block;
var $a_Extra_Vars;
//Constructor: Opens the desired tpl file, can be the exact file location or a relative one
function tpl($s_tpl) {
if(file_exists($s_tpl)){
$this->template = file_get_contents($s_tpl);
}
else{
die ("No template found");
}
}
//Gets a copy of the block, can be used in iterations
function get_Block($s_Block_Name){
//Find the begining of the block
$i_Take_Out = strlen($i_Find_Start = "<!--- START_BLOCK_$s_Block_Name --->");
//Beggining of the block
$i_Block_Start = strpos($this->template, $i_Find_Start) + $i_Take_Out;
//End of the block
$i_Block_End = strpos($this->template, "<!--- END_BLOCK_$s_Block_Name --->");
//Block size
$i_Block_Length = $i_Block_End - $i_Block_Start;
//This is what you where looking for
return $s_Block = substr($this->template, $i_Block_Start, $i_Block_Length);
}
//Replace the block with something. It can be the entire template or one single block
function rBlock($a_Replace, $s_Block_Name, $extras = 0, $return = 0) {
// Look for an specific block or just use the entire template
$s_Tmp_Block = ($s_Block_Name == "" ? $this->template : $s_Block = $this->get_Block($s_Block_Name));
// If will process all the extra values
if($extras == 1){
//$a_Replace = array_merge($a_Replace, $this->a_Extra_Vars);
}
$a_keys = array_keys($a_Replace); /* List with all the keys to replace */
$a_values = array_values($a_Replace); /* List with the values to use */
// Now I will replace each value
for($b = 0; $b < count($a_keys); $b++){
$s_Tmp_Block = str_replace($a_keys[$b], $a_values[$b], $s_Tmp_Block);
}
// If we are just replacing a block will replace it in the full template, else will replace the full template
if($s_Block_Name == ""){
$this->template = $s_Tmp_Block;
}
elseif($return == 0){ // Replace a block and replace the template
$this->template = str_replace($s_Block, $s_Tmp_Block, $this->template);
}
else{
return $s_Tmp_Block;
}
}
/* If you won't use a block*/
function hideBlock($s_Block_Name){
$this->template = str_replace($this->get_Block($s_Block_Name), "", $this->template);
}
/* Replace the same block several times*/
function rBlock_Several($a_Keys, $a_Values, $s_Block_Name, $return = 0){
/* One block to work with, and another one to make the final replacement */
$s_Block = $this->get_Block($s_Block_Name); /* Original Block */
$s_Block_Tmp = ""; /* Temporary blocks */
$s_Block_Final = ""; /* Final Block */
$i_Limit_J = count($a_Values); /* # of arrays */
$i_Limit_I = count($a_Values[0]); /* Elements per array */
/* Iterate replacing each time */
for($j = 0; $j < $i_Limit_J; $j++) {
$s_Block_Tmp = $s_Block;
for($i = 0; $i < $i_Limit_I; $i++) {
$s_Block_Tmp = str_replace($a_Keys[$i], $a_Values[$j][$i], $s_Block_Tmp);
}
$s_Block_Final .= $s_Block_Tmp;
}
/* Finally, put everything in the main tpl or return */
if($return == 0){
$this->template = str_replace($s_Block, $s_Block_Final, $this->template);
}
else{
return $s_Block_Final;
}
}
function rBlock_Dinamic($external_Block, $internal_Block, $limit, $a_Keys, $a_Values, $return = 0){
$s_Block_External = $this->get_Block($external_Block); // External Block
$s_Block_Internal = $this->get_Block($internal_Block); // Internal Block
$external = ""; //Empty
$internal = ""; //Empty
//How many times will I do this? This should be an even number, I will not verify that you are calling this properly
$total = count($a_Values)/$limit;
$split_Array = (array_chunk($a_Values, $limit));
for($i = 0; $i < $total; $i++){
$internal = $this->rBlock_Several($a_Keys, $split_Array[$i], $internal_Block, 1);
$external .= str_replace($s_Block_Internal, $internal, $s_Block_External);
}
//Replace this in the main template
$this->template = str_replace($s_Block_External, $external, $this->template);
}
/* Function to replace all the LANG fields of the template with their respective value
or to create a lang file.
This works in the whole template, not just in blocks
*/
function localize_It($a_Lang, /* Array with lang values, will be used send back the new localization files */
$new_File = 0, /* Create or not a lang file */
$lang_Pre = "_lang" /* Prefix for new lang array */
){
$pos = 0; /* To get it started */
$pos2 = 0;
$open = "{LANG_";
$ends = "_}";
$total_Size = strlen($this->template); /* Limit */
while($pos < $total_Size){
$pos = strpos($this->template, $open, $pos); /* Case Sensitive */
/* If it found somenthing lets see where it ends */
if($pos !== FALSE){
$pos2 = strpos($this->template, $ends, $pos); /* Case Sensitive */
$size = $pos2 + 2 - $pos; /* Add two spaces because pos2 will be where the string starts */
/* Extract the substring */
$rep_String = substr($this->template, $pos, $size);
/* Lang Name */
$lang_String = str_replace($ends, "", str_replace($open, "", $rep_String));
@$this->template = str_replace($rep_String, $a_Lang[$lang_String], $this->template); /* If I don't find one of the lang values, will just ignore it */
if($new_File == 1){
static $new_Lang = "";
$new_Lang .= ($new_File == 1 ? "$".$lang_Pre."['".$lang_String."'] = \"\"; <br/>": "");
}
$pos++; //= strlen($lang_String); /* Walk a bit */
}
else{
/* Terminate if it finds nothing */
$pos = $total_Size;
}
/* Security */
$pos = ($pos > 0 ? $pos : $total_Size);
}
return ($new_File == 1 ? $new_Lang : "");
}
// Replaces all the predefined variables, you can always add more your own
// Not doing anything jet
function constants($a_Extras = "" // If you want extra predefined
){
$a_Pres = array(
"{PRE_THIS_FILE_}" => $_SERVER['PHP_SELF'], /* Usefull for forms */
"{PRE_SERVER_ADDR_}" => $_SERVER['SERVER_ADDR'],
"{PRE_SERVER_NAME_}" => $_SERVER['SERVER_NAME'],
"{PRE_DOCUMENT_ROOT_}" => $_SERVER['DOCUMENT_ROOT'],
"{PRE_REMOTE_ADDR_}" => $_SERVER['REMOTE_ADDR'],
"{PRE_SCRIPT_FILENAME_}" => $_SERVER['SCRIPT_FILENAME'],
"{PRE_HOME_PATH_}" => "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), /* This is the directory under which your script is running */
"{PRE_FILE_}" => (__FILE__)
);
// Extra defaults
if(is_array($a_Extras)){
$a_Pres = array_merge($a_Extras, $a_Pres);
}
$pos = 0; // To get it started
$pos2 = 0;
$open = "{PRE_";
$ends = "_}";
$total_Size = strlen($this->template); // Limit
while($pos < $total_Size){
$pos = strpos($this->template, $open, $pos); /* Case Sensitive */
/* If it found somenthing lets see where it ends */
if($pos !== FALSE){
$pos2 = strpos($this->template, $ends, $pos); /* Case Sensitive */
$size = $pos2 + 2 - $pos; /* Add two spaces because pos2 will be where the string starts */
/* Extract the substring */
$rep_String = substr($this->template, $pos, $size);
/* If I don't find one of the lang values, I will just ignore it */
@$this->template = str_replace($rep_String, $a_Pres[$rep_String], $this->template);
$pos++;
}
else{
/* Terminate at once if it finds nothing */
$pos = $total_Size;
}
/* Security */
$pos = ($pos > 0 ? $pos : $total_Size);
}
}
/* For storing vars for later replacement */
function add_Vars($var, $value = ""){
// add arrays
if(is_array($var)){
$keys = array_keys($var);
for($i = 0; $i < count($keys); $i++){
$this->a_Extra_Vars[$keys[$i]] = $var[$keys[$i]];
}
}
else{
$this->a_Extra_Vars[$var] = $value;
}
}
/* If all you need is the proccesed tpl*/
function getTpl(){
return $this->template;
}
function print_Tpl(){
echo $this->template;
}
//Parse the entire template and print it or return it
function parse_Tpl($main_Array, $constants, $lang, $return = 1){
//Replace main template with extras by default
$this->rBlock($main_Array, "", 1);
$this->constants($constants);
$this->localize_It($lang);
if($return == 1)
$this->print_Tpl();
else
return $this->template;
}
}
/* Extra Utilities */
/* Generation of BBCode */
function bbCode($img_Path, $text, $rel_Path, /* Relative path to the icons directory */
$a_Blocks = "" /* BlockS to replace emulating html tags like [CODE][/CODE]*/
){
/* Smiles */
if($img_Path != ""){
/* Open the incon.pkg and generate the array */
if(is_readable($img_Path."/icon.pkg")){
$icons = file_get_contents($img_Path."/icon.pkg");
/* Generate an array with all the icon and their bbCode */
$each_Icon = explode("\n", $icons);
foreach ($each_Icon as $value) {
$a_Per_Icon[] = explode("||", $value); /* Array of arrays with each icon and their bbCode */
}
/* Iterate the array per icon and replace all that there is */
for($i = 0; $i < count($a_Per_Icon); $i++){
$text = str_replace($a_Per_Icon[$i][0], sprintf("<img src=\"%s/%s\">", $rel_Path, $a_Per_Icon[$i][1]), $text);
}
$text = str_replace(":confused:", "confused.gif", $text);
}
else{
echo "File not readable";
}
}
/* Blocks */
if($a_Blocks != ""){
for($j = 0; $j < count($a_Blocks); $j++){
/* Open */
$text = str_replace($a_Blocks[$j][1], $a_Blocks[$j][0].$a_Blocks[$j][3], $text);
/* Close */
$text = str_replace($a_Blocks[$j][2], $a_Blocks[$j][4], $text);
}
}
return $text = str_replace("\n", "<br />", $text);
}
?>