$templates=array();
/* Variables used in templates
$from1; $f - num of first doc on page
$to; $l - num of last doc on page
$found; $t - total docs found
$o; $o - output format
$query; $Q - query, $q - urlencoded query
$error; $E - error
$wordsinfo; $W - words info
$nav; $V - naVigator
$nav_left; $NL
$nav_bar; $NB
$nav_right; $NR
$nav_href; $NH
$nav_num; $NP
$self; $A - form Action
$ue; $UE - Document URL
$url; $DU - Document URL
$title; $DT - Document Title
$rating; $DR - Document Rating
$contype; $DC - Document Content-type
$lastmod; $DM - Document last Modified
$docsize; $DS - Document Size
$ndoc; $DN - Document No
$desc; $DD - Document Description
$keyw; $DK - Document Keywords
$text; $DX - Document teXt
*/
// -----------------------------------------------
// load_file()
// -----------------------------------------------
function load_file($file){
if (!($fp=fopen($file, 'r'))){
echo "Can't open template file $template_file";
exit_local(1);
}
while(!feof($fp)) {
$str.=fgets($fp, 1024);
}
fclose($fp);
return $str;
}
// -----------------------------------------------
// read_template()
// -----------------------------------------------
function read_template(){
global $Randoms;
global $template_file;
global $templates;
global $ps, $ul, $ue, $o, $tag, $m;
global $clones;
global $affix_file;
global $spell_file;
$affix_file=array();
$spell_file=array();
$names=array('top', 'bottom', 'restop', 'resbot', 'navigator',
'res', 'ftpres', 'httpres', 'notfound', 'error', 'variables', 'clone',
'navleft_nop', 'navright_nop',
'navleft', 'navbar1', 'navbar0', 'navright', 'noquery' );
$where='';
if (!($fp=fopen($template_file, 'r'))){
echo "Can't open template file $template_file";
exit_local(1);
} do {
$str=fgets($fp, 1024);
if (preg_match('/value="(.+)"[\s\t]+selected="\$ps"/i',$str,$param)) {
if (($param[1]) == $ps) {
$str=preg_replace('/selected="\$ps"/i',"selected",$str);
} else {
$str=preg_replace('/selected="\$ps"/i',"",$str);
}
}
if (preg_match('/value="(.*)"[\s\t]+selected="\$ul"/i',$str,$param)) {
if (($param[1]) == $ul) {
$str=preg_replace('/selected="\$ul"/i',"selected",$str);
} else {
$str=preg_replace('/selected="\$ul"/i',"",$str);
}
}
if (preg_match('/value="(.*)"[\s\t]+selected="\$o"/i',$str,$param)) {
if (($param[1]) == $o) {
$str=preg_replace('/selected="\$o"/i',"selected",$str);
} else {
$str=preg_replace('/selected="\$o"/i',"",$str);
}
}
if (preg_match('/value="(.*)"[\s\t]+selected="\$m"/i',$str,$param)) {
if (($param[1]) == $m) {
$str=preg_replace('/selected="\$m"/i',"selected",$str);
} else {
$str=preg_replace('/selected="\$m"/i',"",$str);
}
}
if (preg_match('/value="(.*)"[\s\t]+selected="\$g"/i',$str,$param)) {
if (($param[1]) == $tag) {
$str=preg_replace('/selected="\$g"/i',"selected",$str);
} else {
$str=preg_replace('/selected="\$g"/i',"",$str);
}
}
if (preg_match('/\$IF\s?\((.+)\)/i',$str,$param)) {
$param[1]=trim($param[1]);
$str=preg_replace('/\$IF\s?\((.+)\)/i',load_file($param[1]),$str);
}
$comment_flag=0;
if (substr($str, 0, 4) == "")) {
$where='';
$comment_flag=1;
break;
}
next($names);
}
}
if ($comment_flag) continue;
if ($where=='variables') {
if (ereg("^#",$str)) continue;
if (preg_match("/^([^\s\t]+)[\s\t]+(.+)/",$str,$param)) {
$var=$param[1];
$value=$param[2];
$var=strtolower($var);
$var=trim($var);
$value=trim($value);
if ($var=='affix') {
if (preg_match("/([^\s\t]+)[\s\t]+(.+)/",$value,$param)) {
$lang=trim($param[1]);
$file=trim($param[2]);
$affix_file[$lang]=$file;
}
} elseif ($var=='spell') {
if (preg_match("/([^\s\t]+)[\s\t]+(.+)/",$value,$param)) {
$lang=trim($param[1]);
$file=trim($param[2]);
$spell_file[$lang][]=$file;
}
} elseif ($var=='r') {
$rand=0+substr($var,1);
$frand=rand();
$frand=$frand/getrandmax()*$value;
$Randoms[$rand]=floor($frand);
} else {
$GLOBALS["$var"]="$value";
}
}
}
else
if ($where!=''){
$templates["$where"][count($templates["$where"])-1] .= $str;
}
} while (!feof($fp));
fclose($fp);
}
// -----------------------------------------------
// print_template($t,$echo=1)
// -----------------------------------------------
function print_template($t,$echo=1){
global $templates;
global $from, $to, $found, $query, $query_orig, $error, $self;
global $nav, $wordsinfo;
global $url, $ue, $o;
global $clones;
global $title, $rating, $desc, $contype, $lastmod, $docsize, $ndoc;
global $keyw, $text;
global $crc, $Randoms, $rec_id, $DEBUG;
global $max_url_width;
$from1=$from+1;
$urlt=$url;
if (($max_url_width>0)and
(strlen($urlt)>$max_url_width)) {
$urlt=substr($urlt,0,$max_url_width);
$urlt .= "...";
}
$str=$templates["$t"][$o];
if ($str == '') $str=$templates["$t"][0];
$str=ereg_replace('\$f', "$from1", $str);
$str=ereg_replace('\$l', "$to", $str);
$str=ereg_replace('\$t', "$found", $str);
$str=ereg_replace('\$A', $self, $str);
$str=ereg_replace('\$Q', $query, $str);
$str=ereg_replace('\$q', urlencode($query_orig), $str);
$str=eregi_replace('\$UE', $ue, $str);
$str=ereg_replace('\$E', $error, $str);
$str=ereg_replace('\$W', $wordsinfo, $str);
$str=ereg_replace('\$V', $nav, $str);
$str=ereg_replace('\$DUT', $urlt, $str);
$str=ereg_replace('\$DU', $url, $str);
$str=ereg_replace('\$DT', $title, $str);
$str=ereg_replace('\$DR', "$rating", $str);
$str=ereg_replace('\$DX', $text, $str);
$str=ereg_replace('\$DC', $contype, $str);
$str=ereg_replace('\$DM', $lastmod, $str);
$str=ereg_replace('\$DS', "$docsize", $str);
$str=ereg_replace('\$DN', "$ndoc", $str);
$str=ereg_replace('\$DD', $desc, $str);
$str=ereg_replace('\$DK', $keyw, $str);
$sub=$str;
while($sub=strstr($sub,'$r')){
$sub=substr($sub,2);
$pos=0;
$num='';
while(($sub[$pos]>='0')&&($sub[$pos]<='9')){
$num.=$sub[$pos++];
}
$str=ereg_replace('\$r'.$num, ''.$Randoms[$num], $str);
}
if (strstr($str,'$CL')&&($t=='res')) {
if (strtolower($clones) == 'no') {
$str=ereg_replace('\$CL', '', $str);
} elseif ($crc<>'') {
$myurl=$url;
$myct=$contype;
$mylm=$lastmod;
$query="SELECT url,
content_type,
last_modified
FROM url
WHERE crc='$crc'
AND status=200
AND rec_id<>'$rec_id'";
if($DEBUG) echo $query,"