<?php
class tpl
{
// function __construct Variable
var $template = array();
// function tpl_get() Variablen
var $get = array();
var $pre = array();
// function tpl_edit_tpl() Variablen
var $preg = array();
// Konstrukt
function tpl($template,&$sql=NULL)
{
if(is_array($template))
{
for ($i=0;$i<count($template);$i++)
if (array_search($template[$i],$this->pre)===false)
$this->template[] = & $template[$i];
}
else
if (array_search($template,$this->pre)===false)
$this->template[] = & $template;
if (isset($sql))
{
$this->sql = &$sql;
$this->tpl_get_sql();
}
else
$this->tpl_get_ftp();
$this->tpl_edit_var();
$this->tpl_edit_tpl();
$this->tpl_output();
}
// Templatedaten per FTP ( Falls es mit Unterordner sein soll .$this->preg[$this->get[GLOBAL_PATH[$i]]].'/'. )
function tpl_get_ftp()
{
for ($i=0;$i<count($this->template);$i++) // StammTemplates werden geladen
if(!file_exists(GLOBAL_PATH.'templates/'.$this->template[$i].'.tpl.php'))
return $this->tpl_errmsg('Die Template <code>'.$this->template[$i].'.tpl.php</code> existiert nicht!');
else
$this->get[$this->template[$i]]=implode('',file(GLOBAL_PATH.'templates/'.$this->template[$i].'.tpl.php'));
$this->pre = $this->template;
for ($i=0;$i<count($this->pre);$i++) //SubTemplates werden geladen
{
preg_match_all('%{([^$](.+))}%iU',$this->get[$this->pre[$i]],$this->preg[$this->pre[$i]]);
for ($n=0;$n<count($this->preg[$this->pre[$i]][0]);$n++)
{
if(preg_match('%^{!.+}$%iU',$this->preg[$this->pre[$i]][0][$n]))
$this->preg[$this->pre[$i]][1][$n] = $GLOBALS['_tpl'][$this->preg[$this->pre[$i]][2][$n]];
if (!is_string($this->preg[$this->pre[$i]][1][$n]))
return $this->tpl_errmsg('Die Templatevariable <code>'.$this->preg[$this->pre[$i]][1][$n].'</code> ist kein string oder nicht definiert!<br>');
elseif(!file_exists(GLOBAL_PATH.'templates/'.$this->preg[$this->pre[$i]][1][$n].'.tpl.php'))
return $this->tpl_errmsg('Die Template <code>'.$this->preg[$this->pre[$i]][1][$n].'.tpl.php</code> existiert nicht!<br>');
else
{
if (array_search($this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n],$this->pre)===false)
$this->get[$this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n]]=implode('',file(GLOBAL_PATH.'templates/'.$this->preg[$this->pre[$i]][1][$n].'.tpl.php'));
$this->pre[] = $this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n];
}
}
}
}
// Templatedaten per SQL ( Die TemplateDaten müssen in der Spalte 'inhalt' stehen )
function tpl_get_sql()
{
$query['tpl_template'] = "SELECT `inhalt` FROM `template` WHERE "; // StammTemplates werden geladen
for ($i=0;$i<count($this->template);$i++)
$tpl_query[] = "`name`='".$this->template[$i]."'";
$query['tpl_template'] .= implode(' OR ',$tpl_query);
$this->sql->db_query($query); unset($query);
if (empty($this->sql->fetch['tpl_template']))
return $this->tpl_errmsg('Die Template(s) konnte(n) in der Datenbank nicht gefunden werden!');
$this->pre = $this->template;
for ($i=0;$i<count($this->sql->fetch['tpl_template']);$i++)
$this->get[$this->pre[$i]] = $this->sql->fetch['tpl_template'][$i]['inhalt'];
for ($i=0;$i<count($this->pre);$i++) //SubTemplates werden geladen
{
unset($tpl_query);
unset($this->sql->fetch);
preg_match_all('%{([^$](.+))}%iU',$this->get[$this->pre[$i]],$this->preg[$this->pre[$i]]);
for ($n=0;$n<count($this->preg[$this->pre[$i]][0]);$n++)
{
if(preg_match('%^{!.+}$%iU',$this->preg[$this->pre[$i]][0][$n]))
$this->preg[$this->pre[$i]][1][$n] = $GLOBALS['_tpl'][$this->preg[$this->pre[$i]][2][$n]];
$query['tpl_template'] = "SELECT `inhalt` FROM `template` WHERE `name`='".$this->preg[$this->pre[$i]][1][$n]."'";
$this->sql->db_query($query); unset($query);
if (!is_string($this->preg[$this->pre[$i]][1][$n]))
return $this->tpl_errmsg('Die Templatevariable <code>'.$this->preg[$this->pre[$i]][1][$n].'</code> ist leer oder nicht definiert!<br>');
elseif (empty($this->sql->fetch['tpl_template']))
return $this->tpl_errmsg('Die Template konnte in der Datenbank nicht gefunden werden!');
else
{
if (array_search($this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n],$this->pre)===false)
$this->get[$this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n]] = $this->sql->fetch['tpl_template'][0]['inhalt'];
$this->pre[] = $this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n];
}
}
}
}
// Ersetzen der pseudo-Variablen ( Variable: { VARNAME } )
function tpl_edit_var()
{
for ($i=0;$i<count($this->pre);$i++)
{
preg_match_all('%{\$(.*)}%iU',$this->get[$this->pre[$i]],$preg);
if (isset($preg[0]))
{
for ($n=0;$n<count($preg[0]);$n++)
$count[$i][] = count($GLOBALS['_tpl'][$preg[1][$n]]);
$max[$i] = (is_array($count[$i])) ? max($count[$i]) : 0;
}
$getbackup = $this->get[$this->pre[$i]];
for ($n=0;$n<$max[$i];$n++) // Falls Arrays als Variablen gesetzt wurden
{
for ($m=0;$m<count($preg[0]);$m++)
if (isset($GLOBALS['_tpl'][$preg[1][$m]]))
if (is_array($GLOBALS['_tpl'][$preg[1][$m]]))
$this->get[$this->pre[$i]] = str_replace($preg[0][$m],$GLOBALS['_tpl'][$preg[1][$m]][$n],$this->get[$this->pre[$i]]);
else
$this->get[$this->pre[$i]] = str_replace($preg[0][$m],$GLOBALS['_tpl'][$preg[1][$m]],$this->get[$this->pre[$i]]);
else
return $this->tpl_errmsg('Die Variable <code>'.$preg[1][$m].'</code> ( Template: <code>'.substr_replace($this->pre[$i],'',0,strrpos($this->pre[$i],__)).'.tpl.php</code> ) ist in <code>$GLOBALS</code> nicht zu finden!');
if ($n!=($max[$i]-1))
$this->get[$this->pre[$i]] = $this->get[$this->pre[$i]].$getbackup;
}
}
}
// Einfügen der SubTemplates und PseudoSubTemplates ( SubTemplate: { TEMPNAME } PseudoSubTemplates: {!TEMPNAME} )
function tpl_edit_tpl()
{
for ($i=count($this->pre);$i>=0;--$i)
for ($n=0;$n<(count($this->preg[$this->pre[$i]][0]));$n++)
$this->get[$this->pre[$i]] = str_replace($this->preg[$this->pre[$i]][0][$n],$this->get[$this->pre[$i].'__'.$this->preg[$this->pre[$i]][1][$n]],$this->get[$this->pre[$i]]);
}
// Output
function tpl_output()
{
#ob_start('ob_gzhandler');
for($i=0;$i<count($this->template);$i++)
echo $this->get[$this->template[$i]];
#ob_end_flush();
exit(0);
}
// Fehlermeldungen
function tpl_errmsg($errormsg)
{
if ($_SESSION['error']===true)
{
global $mailto;
$errormsg = '<pre><b>Fehler in der Template Klasse</b><br><br>'.$errormsg.'<br>Bitte probieren sie es erneut oder wenden sie sich an <a href="mailto:'.$mailto['name'].'%20%3C'.$mailto['email'].'%3E?subject=Fehler%20bei%20den%20Templates&body=Fehler:%20'.text_emailcodierung($errormsg).'%0D%0ADatum:%20'.date('d.m.Y%20@%20H:i').'%0D%0AUrl:%20'.getenv('REQUEST_URI').'">'.$mailto['email'].'</a><br>';
$errormsg .= '-----------------------------<br>';
$errormsg .= '<b>Datum: </b>'.date('d.m.Y @ H:i').'<br>';
$errormsg .= '<b>Url: </b>'.getenv('REQUEST_URI').'<br>';
$errormsg .= '<b>Referer: </b>';
(!getenv('HTTP_REFERER')) ? $errormsg .= 'unbekannt<br>' : $errormsg .= getenv('HTTP_REFERER').'<br>';
echo $errormsg;
exit(0);
}
else
require (GLOBAL_PATH.'error.php');
exit(0);
}
}
?>