<?php
// ---------------------------------
// Static Parser Engine v1.0
// (c) 2007 by Sergey V. Bogomolov
// web: http://bogomolov.in.ua
// ---------------------------------
if(get_magic_quotes_gpc()==1) die("PHP settings error!<br> Please set magic_quotes_gpc=0. Otherwise parser will fail.");
include "config.php";
// -- Engine Functions --
function parseCode($Body, $path="", $fname=""){
global $toparse, $document_root;
$ar=array();
foreach($toparse as $key=>$value){
$bodyLowered=strtolower($Body);
$n1=strpos($bodyLowered, $value[0]);
$n2=strpos($bodyLowered, $value[1], $n1+strlen($value[0]));
if($n1>0 && $n1!=FALSE)$n1+=strlen($value[0]);
if($n1>0 && $n2>0 && $n2>$n1){
$l=$n2-$n1;
$content=substr($Body, $n1, $l);
$ar[$key]=$content;
//echo $content;
}
}
// get fileinfo
$ar['file_size_bytes']=filesize($path.$fname);
$ar['modify_date_t']=filemtime($path.$fname);
$ar['url']=str_replace($document_root, '', $path.$fname);
return $ar;
}
function unparseCode($Ar, $Path="", $Fname=""){
global $toparse;
// get body from file
if($Fname!=""){
if($Path!=""){
$f=fopen($Path.$Fname, "rb");
$fs=filesize($Path.$Fname);
}else{
$f=fopen($Fname, "rb");
$fs=filesize($Fname);
}
$body=fread($f, $fs);
fclose($f);
}
// replace pieces in $body using $Ar and $toparse
foreach($Ar as $key=>$value){
$bodyLowered=strtolower($body);
$n1=strpos($bodyLowered, $toparse[$key][0]);
$n2=strpos($bodyLowered, $toparse[$key][1], $n1+strlen($toparse[$key][0]));
if($n1>0 && $n1!=FALSE)$n1+=strlen($toparse[$key][0]);
if($n1>0 && $n2>0 && $n2>$n1){
$l=$n2-$n1;
$content=substr($body, $n1, $l);
$body=str_replace($toparse[$key][0].$content.$toparse[$key][1], $toparse[$key][0].$value.$toparse[$key][1], $body);
}
}
return $body;
}
function savePage($Path, $Fname, $Body){
$f=fopen($Path.$Fname, 'wb');
fwrite($f, $Body);
fclose($f);
return true;
}
function getPage($Path, $Fname=''){
global $template_file;
if($Fname==''){$Fname=$template_file; $Path='';}
$f=fopen($Path.$Fname, 'rb');
$body=fread($f, filesize($Path.$Fname));
fclose($f);
return $body;
}
function getList($path=""){
global $toparse, $dirs, $exts;
$ar=array();
foreach($dirs as $value){
if($path!="" && $path!=$value) continue;
$current_dir=opendir($value);
while($entryname=readdir($current_dir)){
if(!is_dir($value.$entryname) && $entryname != "." && $entryname!=".."){
// check file extension
list($fname, $fext)=explode(".", $entryname);
$is_ext=false;
foreach($exts as $ext){
if($fext==$ext) { $is_ext=true; break; }
}
if($is_ext){
// get title
$toparse_bak=$toparse; $toparse="";
$toparse['title'][0]='<title>'; $toparse['title'][1]='</title>';
$parsed_var=parseCode(getPage($value, $entryname), $value, $entryname);
$toparse=$toparse_bak;
$file_var=$parsed_var;
$file_var['path']=$value;
$file_var['fname']=$entryname;
array_push($ar, $file_var);
}
}
}
}
return $ar;
}
function delPage($Path, $Fname){
return unlink($Path.$Fname);
}
function renPage($Path, $Fname, $New_fname){
return rename($Path.$Fname, $Path.$New_fname);
}
// - debug code (remove when finish)
//print_r(parseCode(getPage($dirs[0],'detskaya.html')));
//print_r(getList());
?>