Files
bm/public_html/public/include/template.inc.php
2025-09-24 13:26:28 +02:00

122 lines
2.9 KiB
PHP

<?php
# Template class
# @version 1.0.5
# @date 2007-02-13
# @author Martin Lenzelbauer
#
# variables in html template: {$$$}
# blocks in html template: <!-- BEGINBLOCK $$$ -->
# <!-- ENDBLOCK $$$ -->
# $$$ is the name of the variable/block
# there must be exactly one space between '<!--', 'BEGINBLOCK', '$$$' and '-->'
# variable and block names must be uppercase
class Template{
var $file;
//C'tor
function Template($file=NULL){
if($file != NULL){
$this->setFile($file);
}
}
//reads the html template file
function setFile($file){
$fp = fopen($file,"rb");
$this->file = fread($fp,filesize($file));
fclose($fp);
}
//sets a predefined strins as template
function setString($file){
$this->file = $file;
}
//replaces a variable by a value
function setVar($var,$content){
$pos2 = 0;
while($pos1 = strpos($this->file,"{".$var."}",$pos2)){
$pos2 = strpos($this->file,"}",$pos1);
$this->file = substr($this->file,0,$pos1).$content.substr($this->file,$pos2+1);
$pos2 = $pos1 + strlen($content);
}
}
//replaces a variable with html content
function setHtmlVar($var,$html){
$fp = fopen($html,"rb");
$content = fread($fp,filesize($html));
fclose($fp);
$this->setVar($var,$content);
}
//removes a block
function removeBlock($block){
$start = strpos($this->file, "<!-- BEGINBLOCK $block -->");
$end = strpos($this->file, "<!-- ENDBLOCK $block -->");
if($start === false || $end === false){
return false;
}
$end += 18 + strlen($block);
$this->file = substr($this->file, 0, $start) . substr($this->file, $end);
}
//removes all blocks that contain unreplaced variables
function removeUnusedBlocks(){
$start = strpos($this->file, "<!-- BEGINBLOCK");
while($start !== false){
$blockname = substr($this->file, $start+16, strpos($this->file, "-->", $start+16)-$start-17);
$end = strpos($this->file, "<!-- ENDBLOCK $blockname -->");
if($end === false){
$start = false;
}
else{
$end += 18 + strlen($blockname);
$block = substr($this->file, $start, $end-$start);
$var = ereg("\{[A-Z]+\}", $block);
if($var !== false){
$this->file = substr($this->file, 0, $start) . substr($this->file, $end);
}
if($start+1 < strlen($this->file)){
$start = strpos($this->file, "<!-- BEGINBLOCK", $start+1);
}
else{
$start = false;
}
}
}
}
//prints the data
function parse(){
echo $this->file;
}
//prints the data to a string
function toString(){
return $this->file;
}
//saves the data to a file
function saveTo($path){
if(ini_get('safe_mode')){
$f = tmpfile();
$ftp = ftp_connect(FTPHOST);
ftp_login($ftp, FTPUSER, FTPPW);
ftp_chdir($ftp, "html/cms/");
ftp_fput($ftp, $path, $f, FTP_ASCII);
ftp_site($ftp, "CHMOD 0777 $path");
ftp_close($ftp);
fclose($f);
}
$f = fopen($path, "w");
fwrite($f, $this->file);
fclose($f);
}
};
?>