<?
////////////////////////////////////////////////////////////////////////////////
// HTML Template Data Parser //
////////////////////////////////////////////////////////////////////////////////
// //
// VERSION : 1.0 //
// AUTHOR : H0b0s //
// CREATED : 16 Dec 2003 //
// WEBSITE : //
// SUPPORT : hide@address.com //
// BUG-REPORT : hide@address.com //
// COMMENT : HTML template oriented data parser class //
// LEGAL : Copyright (C) 2003 H0b0s. //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// This code may be used and modified by anyone so long as this header and //
// copyright information remains intact. //
// //
// The code is provided "as-is" and without warranty of any kind, //
// expressed, implied or otherwise, including and without limitation, any //
// warranty of merchantability or fitness for a particular purpose. //
// //
// In no event shall the author be liable for any special, incidental, //
// indirect or consequential damages whatsoever (including, without //
// limitation, damages for loss of profits, business interruption, loss //
// of information, or any other loss), whether or not advised of the //
// possibility of damage, and on any theory of liability, arising out of //
// or in connection with the use or inability to use this software. //
// //
////////////////////////////////////////////////////////////////////////////////
// HISTORY : //
////////////////////////////////////////////////////////////////////////////////
require("parser.php");
class HTMLTemplateDataParser_Class extends htmlparser_class
{
var $curpos=0;
var $curtemplate=0;
var $template=array();
var $sensitive=false; // insensitive string comparison
var $values=array();
var $lb="{{";
var $rb="}}";
var $orb="||";
var $at=0;
function LoadTemplate($filename)
{
$lines=file($filename);
while (list($lkey, $lvalue) = each($lines)){
$strexp_ = array();
$strexp = explode(" ", $lvalue);
while (list($key, $value) = each($strexp)){ if($value != "") $strexp_[] = $value; }
$lines[$lkey] = implode(" ", $strexp_);
}
return $this->SetTemplate($lines);
}
function SetTemplate($lines)
{
$this->template=array();
$templates = $this->SplitTemplates($lines);
for($i=0;$i < count($templates);$i++)
for($j = 0;$j < count($templates[$i]);$j++)
$this->template[$i][] = $this->SplitFullTemplateStr($templates[$i][$j]);
if(count($this->template) == 0) { $this->template=array(); return false; }
else return true;
}
function GetTemplate(&$result)
{
if (count($this->template)==0) { $result=array(); return false; }
$result=$this->template;
return true;
}
function FindFirst()
{
$this->curpos = 0;
return $this->FindNext();
}
function FindNext()
{
$i=0;
$index=0;
$done=false;
$pos = $this->curpos;
while(true){
$done=true;
for($index=0;$index < count($this->template);$index++){
if($pos > (count($this->elements) - count($this->template[$index]))) continue;
$done=false;
if(count($this->values)!= 0) $this->values = array();
for($i=0;($i < count($this->template[$index]))&&($this->CmpTemplateStr($this->elements[$pos+$i],$this->template[$index][$i])==true);$i++);
if($i == count($this->template[$index])){
$this->curpos = $pos + count($this->template[$index]);
$this->at=$pos;
$this->curtemplate=$index;
return true;
}
}
if($done) break;
$pos++;
}
return false;
}
function GetValues(&$result)
{
if (count($this->values)==0) { $result=array(); return false; }
$result=$this->values;
return true;
}
function SplitTemplateStr($str)
{
$i = 0;
$index = 0;
$s = "";
$result = array();
if($str == "") return $result;
while(true){
$index = strpos($str,$this->lb, $i);
if($index === false){
if(strpos($str,$this->rb, $i)!== false) break; //error
if((strlen($str)-$i)!= 0)
if(trim(substr($str, $i, strlen($str)-$i)) != "")
$result[]=trim(substr($str, $i, strlen($str)-$i));
break;
}
$s = trim(substr($str,$i,$index-$i));
if($s!="")$result[] = $s;
$i = strpos($str,$this->rb, $i);
if(($i === false)||($i < $index)) break; //error
$i+=strlen($this->rb);
if(trim(substr($str,$index,$i-$index)) != "")
$result[]=trim(substr($str,$index,$i-$index));
}
return $result;
}
function SplitFullTemplateStr($str)
{
$i=0;
$index=0;
$result=array();
while(true){
$index = strpos($str,$this->orb, $i);
if($index === false){
$result[]=$this->SplitTemplateStr(trim(substr($str, $i, strlen($str)-$i)));
break;
}
$result[]=$this->SplitTemplateStr(trim(substr($str, $i, $index-$i)));
$i=$index+strlen($this->orb);
}
return $result;
}
function SplitTemplates($lines)
{
$index=0;
$result=array();
for($i=0;$i<count($lines);$i++){
if(trim($lines[$i])==$this->orb){$index++; continue;}
if(trim($lines[$i])!="")$result[$index][]=trim($lines[$i]);
}
return $result;
}
function CmpTemplateStr($str, $tmplstr)
{
$s = "";
$varname = "";
$n = 0;
$index1 = 0;
$index2 = 0;
$values = array();
$result = true;
$strexp_ = array();
$strexp = explode(" ", $str);
while (list($key, $value) = each($strexp)){ if($value != "") $strexp_[] = $value; }
$str = implode(" ", $strexp_);
for($i=0;$i < count($tmplstr);$i++){
for($j=0;$j < count($tmplstr[$i]);$j++){
if(sscanf($tmplstr[$i][$j],$this->lb."%s".$this->rb,&$varname)!= 0) continue;
$index2 = strpos($str, $tmplstr[$i][$j], $index1);
if($index2 === false){
$result=false;
break;
}
if($varname!=""){
$varname=substr($varname,0,strlen($varname)-strlen($this->rb));
$values[$varname]=trim(substr($str, $index1 + strlen($tmplstr[$i][$n]), $index2 - ($index1 + strlen($tmplstr[$i][$n]))));
$varname="";
$n=$j;
}else{
if(($index2 > $index1)&&(trim(substr($str, $index1, $index2-$index1))!="")){
$result=false;
break;
}
}
$index1 = $index2 + strlen($tmplarr[$i][$j]);
}
if($result==true){
if($varname != ""){
$varname=substr($varname,0,strlen($varname) - strlen($this->rb));
$values[$varname]=trim(substr($str, $index1, strlen($str) - $index1));
}
break;
}
}
$this->values += $values;
return $result;
}
}
?>