"olinuris"); $template_ok= array( "ref/", "ref/info/*", "ref/research/*", "ref/hss315/*", "ref/lit/*", "ref/cet/*", "ref/newrefbooks/*", "access/laptop/*", "access/reservenews/*", "access/reservenew/*", "access/carrels/*", "access/", "studies/*", "media/"); $template_name=$templates[$template]; if (!$template_name) die("invalid template"); if (!isset($url)) die("template: url not set"); # XXX -- not secure; we should make sure that it's a well-formed filename # and that it doesn't do anything funny with .. # truncate initial slash if it's there if($url{0}=="/") { $url=substr($url,1); }; # limit access to directories in $template_ok, terminal * is a wildcard, # which means prefix match (/access/ matches only toplevel content, # /access/* matches everything below) $url_parts=explode("/",$url); array_pop($url_parts); $dir_part=implode("/",$url_parts); $dir_part="$dir_part/"; $template_this=false; foreach($template_ok as $ok_dir) { $last_char=$ok_dir{strlen($ok_dir)-1}; if ($last_char=="*") { $test_prefix=substr($ok_dir,0,strlen($ok_dir)-1); $probe_prefix=substr($dir_part,0,strlen($ok_dir)-1); if ($test_prefix==$probe_prefix) $template_this=true; } else { if ($ok_dir==$dir_part) { $template_this=true; }; } } $search_dir=$system; $file="$search_dir/$url"; # # "@" eliminates warning message # $s=@stat($file); if(!$s) die("file $file does not exist"); $o_file=$file; if(is_dir($file)) { $last_char=$file{strlen($file)-1}; if ($last_char!="/") { header("Location: $base_url$url/"); exit(); } $file="{$file}index.html"; $s=@stat($file); if(!$s) { $file="{$o_file}index.htm"; $s=@stat($file); if(!$s) die("file $file does not exist"); } }; $ext=""; if(preg_match("/\.(.*)?$/",$file,$matches)) { $ext=$matches[1]; }; $ext=strtolower($ext); $types=array("pdf"=>"application/pdf", "html"=>"text/html", "htm"=>"text/html", "gif"=>"image/gif", "jpg"=>"image/jpg", "js"=>"application/javascript", "bmp"=>"image/bmp", "doc"=>"application/msword", "css"=>"text/css"); $type=$types[$ext]; if(!$type) // $type="text/plain"; $type="text/html"; header("Content-type: $type"); if(!$template_this || !preg_match("/(.html$|.htm$)/i",$file)) { readfile($file); exit(); }; # # handle HTML # $x=implode("\n",file($file)); $date=$s[9]; # date of last change $date_formatted=date("F j Y",$date); # # extract head for metadata # preg_match("/(.*)<\/HEAD>/is",$x,$matches); $head=$matches[1]; preg_match("/(.*)<\/TITLE>/is",$x,$matches); $title=$matches[1]; $shortTitle=$title; # # extract metatags for pass-thru/interpretation # # a "gateway.notemplate" meta tag turns off templating # $script_text=""; preg_match_all("/<META (.*?)>/is",$head,$meta_tags,PREG_SET_ORDER); foreach($meta_tags as $m) { $meta_tag=$m[1]; preg_match_all("/([A-Z][-A-Z0-9_:.]*)=('[^']*|\"[^\"]*\")/is",$meta_tag,$attributes,PREG_SET_ORDER); foreach($attributes as $a) { $name=$a[1]; $value=substr($a[2],1,strlen($a[2])-2); # strip quotes if (strtolower($name)=="name" && strtolower($value)=="gateway.notemplate") { print $x; exit; }; }; $script_text .= "<META $meta_tag>"; }; # # extract scripts for pass-thru # preg_match_all("/<SCRIPT.*?>.*?<\/SCRIPT>/is",$head,$matches); $scripts=$matches[0]; $script_text .= implode("\n",$scripts); # # extract body # preg_match("/<BODY.*?>(.*)<\/BODY>/is",$x,$matches); $body=$matches[1]; # # nuke the existing footer # $body=preg_replace("/<FOOTER>.*<\/FOOTER>/is","",$body); # # nuke blockquote # $body=preg_replace("/<BLOCKQUOTE>/is","",$body); $body=preg_replace("/<\/BLOCKQUOTE>/is","",$body); # # determine root path # $x=path_depth($file); $y=path_depth($system); $depth=path_depth($file)-path_depth($system); $root_path="."; $i=$depth; if($i>1) { $root_path=rtrim(str_repeat("../",$depth-1),"/"); } # # output # $variables=array("title"=>$title, "shortTitle"=>$shortTitle, "body"=>$body, "root_path"=>$root_path, "script_text"=>$script_text, "date_formatted"=>$date_formatted); $template=join("",file("$system/template/$template_name.html")); foreach($variables as $key=>$value) { $template=preg_replace("/\{\\$$key\}/",$value,$template); }; echo $template; function path_depth($path) { $path=rtrim($path,"/"); $path=preg_replace("/\/+/","/",$path); $depth=count(explode("/",$path)); return $depth; } ?>