<?php
$PHP_SELF = $_SERVER['PHP_SELF'];
function dbg($msg)
{
echo "<script language=javascript> alert('" . $msg . "');</script>";
}
// Robopage by Sandy Pittendrigh (hide@address.com)
// future (possible) todo list:
// 2) build some new layout managers, for pages with multiple images, top (rather than leftside)
// navigation, etc.
// 3) enhance framesLayout to handle top and bottom panels as additional frames (top and bottom
// panels are (currently) recognized only by the tableLayout manager
// 5) Build an interactive form (with mouse picking and graphic feedback)
// for editing css files (to interactively change colors, etc)
// 6) NOTE: let me know if you like and use this, and especially if you
// add new gizmos I might want to incorporate into the base code.
//
$RecognizedFileTypes = array( "cap"=>"known","doc" => "known", "xls" => "known", "zip" => "known", "tgz" => "known", "gz" => "known", "zip" => "known", "tar" => "known");
class Link
{
var $I;
function Link($href, $label, $targettype, $target="_top", $cssclass='lnkNavClr', $label_decoration=null)
{
$this->I = Array();
$this->I['href']=$href;
$this->I['label']=$label;
$this->I['target_type']=$targettype;
if($this->I['target_type'] == "dir")
$this->I['label'] .= "/";
$this->I['css_class']=$cssclass;
$this->I['target']=$target;
$this->I['label_decoration']=$label_decoration;
if($label_decoration != null)
$this->decorateLabel();
}
function qry($key){ return $Arr[$key]; }
function getArrArray(){ return Arr; }
function decorateLabel()
{
$tags = array();
$tags = explode(":", $this->I['label_decoration']);
$cnt = count($tags);
for($i=0; $i<$cnt; $i++)
{
$tag = $tags[$i];
$this->decorate($tag);
}
}
function decorate($dec)
{
$left=$right="";
$right = "</".$dec.">";
$left = "<" . $dec . ">";
$this->I['label'] = $left.$this->I['label'].$right;
}
function getLink()
{
$ret = "<a";
if($this->I['css_class'] != null)
$ret .= " class=\"" . $this->I['css_class'] . "\"";
$ret .= " href=\"".$this->I['href']."\"";
$ret .= " target=\"".$this->I['target']."\">";
$ret .= $this->I['label'];
$ret .= "</a>";
return $ret;
}
}
class robopage
{
var $rp_collection_title;
var $rp_page_title;
var $rp_page_type;
var $rp_file;
var $rp_page_layout;
var $rp_links = Array (); // htm is a page fragment, to go in a rightside table cell
var $pirjo_labels = Array ();
var $rp_keywords="";
var $rp_stylesheet = "robopage.css";
var $rp_menuwidth = "200";
var $rp_show_navigation_catetories = 1;
var $rp_show_up_link = 1;
var $rp_home_link = "/";
var $show_unknown_links=0;
var $rp_dir;
var $rp_uplink;
var $rp_php_self;
var $rp_document_root;
var $rp_script_home;
var $rp_mode;
function robopage($thePage, $tdir, $mode)
{
$this->init($tdir);
if($thePage == null || $thePage=="NULL")
$thePage = $this->findNUseDefaultPage();
$this->rp_page_title=$this->getLabel($thePage);
$tmpType = $this->getPageType($thePage);
$this->rp_page_type=$tmpType;
$this->rp_file=trim($thePage);
$this->mkLayoutManager($mode);
}
function init($tdir)
{
global $sys_stylesheet, $sys_menuwidth,
$sys_home_link, $sys_show_navigation_catetories,$sys_document_root,$sys_script_home;
$this->rp_php_self = $_SERVER['PHP_SELF'];
if(isset($sys_document_root))
$this->rp_document_root = $sys_document_root;
else
$this->rp_document_root = $_SERVER['DOCUMENT_ROOT'];
if(isset($sys_script_home))
$this->rp_script_home = $sys_script_home;
else
$this->rp_script_home = $this->rp_document_root.dirname($_SERVER['PHP_SELF']);
$dir = $this->rp_document_root . $tdir;
if(isset($sys_stylesheet)) $this->rp_stylesheet=$sys_stylesheet;
if(isset($sys_home_link)) $this->rp_home_link=$sys_home_link;
if(isset($sys_menuwidth)) $this->rp_menuwidth=$sys_menuwidth;
if(isset($sys_show_navigation_catetories))
$this->rp_show_navigation_catetories=$sys_show_navigation_catetories;
$this->figureDir($dir);
$this->figureUpLink();
}
function figureDir($tdir)
{
if ($tdir == "NULL")
{
$this->rp_dir = getcwd();
}
else
{
$tmp = trim($tdir);
if(!strstr($tmp, $this->rp_script_home))
{
$this->rp_dir = $this->rp_script_home;
}
else
{
$this->rp_dir = $tmp;
}
}
}
function figureUpLink()
{
$tmp = dirname($this->rp_dir);
if(!strstr ($tmp, $this->rp_script_home))
$updir = $this->rp_script_home;
else
$updir = $tmp;
$updir = ereg_replace($this->rp_document_root,"",$updir);
$this->rp_uplink = $this->rp_php_self."?thePage=NULL&dir=".$updir."&mode=NULL";
}
function mkLayoutManager($mode)
{
if($mode == null || $mode == "NULL")
$mode = "mkframeset";
$this->rp_mode = $mode;
$this->mkLinks();
if($this->rp_page_type == 'html'
|| $this->rp_page_type == 'text'
|| $this->rp_page_type == 'furl'
|| $mode == 'fnavigation'
|| $mode == 'fdisplay'
|| count($this->rp_links) > 20)
{
$this->rp_page_layout = "frames";
$this->layoutManager = new frameLayout($this, $mode);
}
elseif ($this->rp_page_type == 'image')
{
$this->rp_page_layout = "frames";
$image = $this->rp_dir . "/" . trim($this->rp_file);
list($width, $height, $type, $attr) = getimagesize($image);
if($width > 600 || $height > 600)
{
$this->layoutManager = new tableLayout($this);
}
else
{
$this->layoutManager = new tableLayout($this);
}
}
else
{
$this->layoutManager = new tableLayout($this);
}
}
function getPageType ($file)
{
$retval="unknown";
if (stristr ($file, "html") )
$retval = "html";
elseif (stristr ($file, "htm") && !strstr ($file, "html"))
$retval = "htm";
elseif (strstr ($file, ".url"))
$retval = "url";
elseif (strstr ($file, ".furl"))
$retval = "furl";
elseif (strstr ($file, "php") && !strstr ($file, "url"))
$retval = "html";
elseif (strstr ($file, ".txt"))
$retval = "text";
elseif (stristr ($file, ".jpg") || stristr ($file, ".gif")
|| stristr ($file, ".png") || stristr ($file, ".jpeg"))
$retval = "image";
else
{
$suffix = $this->getSuffix($file);
$tmp = null;
if($suffix)
{
global $RecognizedFileTypes;
$tmp = $RecognizedFileTypes[$suffix];
}
if($tmp)
$retval = $tmp;
}
return ($retval);
}
function getSuffix($str)
{
$suffix="";
$tmp = basename($str);
$pos = strrpos($str,".");
if($pos != null)
$suffix = substr($str,$pos+1);
return $suffix;
}
function stripSuffix($str)
{
$tmp = basename($str);
$ret = ereg_replace ("\..*$", "", $tmp);
return($ret);
}
function tmpLabel($str)
{
$tmp = basename($str);
$getLabel = ereg_replace ("\..*$", "", $tmp);
$getLabel = ereg_replace ("^.+\-", "", $getLabel);
return($getLabel);
}
function getLabel ($str)
{
$getLabel = $this->tmpLabel($str);
if(isset($this->pirjoLabels[$getLabel]))
$label = $this->pirjoLabels[$getLabel];
else
$label = $getLabel;
$label = ereg_replace("_", " " , $label);
return ($label);
}
function determinePirjoLabel($filename)
{
$lines = file($filename);
$pirjoLabel = $lines[0];
return ($pirjoLabel);
}
function findNUseDefaultPage()
{
$thePage=null;
$lastimage=null;
$lasttext=null;
$lasthtm=null;
$found=0;
$handle = opendir ($this->rp_dir);
while (false !== ($file = readdir($handle)))
{
if($file[0]== "" || $file[0] == "." || strstr ($file, "index") || strstr ($file, "hidden")
|| strstr ($file, "robopage") || strstr ($file, "keywords"))
continue;
$tmpType = $this->getPageType ($file);
if($tmpType == "unknown")
continue; // until we find a useable file, for the openning page
if(strstr($file, "skip"))
{
$thePage=$file;
}
elseif ($tmpType == "image")
$lastimage = $file;
elseif ($tmpType == "htm")
$lasthtm = $file;
elseif ($tmpType == "text")
$lasttext = $file;
}
// At this point, only a 'skip' file could have set found
// If found is set, then we're done.
// A skip file is any file (of recognized types) manually copied into this directory,
// whose name contains the substring 'skip'
// The skip file will determine the openning display, if it exists.
// However, the skip file will *not* become part of any page's clickable navigation links
//
if($thePage == null)
{
if($lastimage != null)
$thePage = $lastimage;
elseif($lasttext != null)
$thePage = $lasttext;
elseif($lasthtm != null)
$thePage = $lasthtm;
}
closedir ($handle);
return ($thePage);
}
function mkLinks ()
{
$filenames = Array();
$handle = opendir ($this->rp_dir);
while (($file = readdir ($handle)) !== false)
{
if($file[0] == "." || strstr ($file, "index") || strstr ($file, "hidden")
|| strstr ($file, "robopage") || strstr ($file, "keywords"))
continue;
$label = $this->getLabel ($file);
$key = $this->stripSuffix($file);
$path = $this->rp_dir . "/" . $file ;
if (is_dir($path))
{
$tmp = ereg_replace($this->rp_document_root, "", $this->rp_dir);
$dir = $tmp . "/" . $file;
$href = $this->rp_php_self."?thePage=NULL&dir=".$dir."&mode=NULL";
$link = new Link($href, $label, "dir", "_top", "lnkDirClr", "b:i");
$this->rp_links[$key] = $link;
}
elseif ($file == "keywords")
{
$this->rp_keywords = `cat $file`; // need to process this better?
continue;
}
if(strstr($file, "lbld"))
{
$this->pirjoLabels[$key] = $this->determinePirjoLabel($file);
}
$filenames[] = $file;
}
$fcnt = count($filenames);
for($i=0; $i<$fcnt; $i++)
{
$file = $filenames[$i];
$linkType = $this->getPageType ($file);
if(!stristr($file,"skip"))
$this->mkLink($file, $linkType);
}
}
function getCollectionTitle ()
{
global $PHP_SELF;
$base = basename ($PHP_SELF);
$path = ereg_replace ($base, "", $PHP_SELF);
$dir = basename ($path);
return ($dir);
}
function mkLink ($file, $type)
{
global $PHP_SELF;
$key = $this->stripSuffix($file);
$label = $this->getLabel($file);
$href=null;
// furl links get made by the default clause,
// get created in mkFrameset hack (opens this->rp_file) to make frame tag
if($type == "url")
{
$href = $this->lineOneFromFile($file) ;
$link = new Link($href, $label, "url", "_top", "lnkNavClr");
$this->rp_links[$key] = $link;
}
else
{
switch($type)
{
case "script":
$href=$file;
break;
case "unknown":
if($this->show_unknown_links)
$href=$file;
break;
default:
$h1 = $PHP_SELF."?thePage=".$file;
$dir_param = ereg_replace ($this->rp_document_root, "", $this->rp_dir);
$h2 = "&dir=". $dir_param;
$h3 = "&mode=NULL"; // mode gets set differently by framesLayout ctors
$href = $h1.$h2.$h3;
}
if($href != null)
{
$link = new Link($href, $label, $type, "_top", "lnkNavClr");
$this->rp_links[$key] = $link;
}
}
}
function lineOneFromFile($file)
{
$lines = file($file);
return($lines[0]);
}
function fileToUrl($file)
{
$patt = $this->rp_document_root;
$string = $this->rp_dir;
$tmp = ereg_replace($patt, "", $string);
$url = $tmp . "/" . $file;
//dbg($url);
return($url);
}
}
// The base class of the layout manager handles atomic, page-area displays: for instance a
// vertical navigation area, that appears inside a table, that can be plugged into a
// another table, and/or a frame.
class layoutManager
{
var $parent_page;
var $lm_mode;
function layoutManager($inparent_page, $mode)
{
$this->parent_page = $inparent_page;
$this->lm_mode=$mode;
$this->init();
}
function init() { }
function mkHeader ()
{
$title = "robopages";
if(!isset($this->rp_keywords))
$this->rp_keywords = "";
if(isset($this->rp_page_title))
$title = $this->rp_page_title ? $this->rp_page_title : "robopage" ;
echo $this->header_decl;
echo "\n<html><head><title>$title</title>\n";
echo "<link rel=\"shortcut icon\" href=\"favicon.ico\">\n";
echo "<meta name=\"keywords\" CONTENT=\"".$this->rp_keywords."\">\n";
echo "<link rel=\"stylesheet\" href=\"".$this->parent_page->rp_stylesheet."\" type=\"text/css\">\n";
echo "</head>\n";
}
function mkVerticalLinksArea ()
{
ksort($this->parent_page->rp_links);
$str="";
$str .= "<table class=\"tblNavBkg\" style=\"height:100%;\" cellpadding=\"0\" cellspacing=\"2\" border=\"0\">\n";
$str .= "<tr><td height=\"400\" valign=\"middle\" align=\"center\">";
$str .= "<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\">\n";
$str .= "<tr><td> </td></tr>";
while (list ($dontcare, $Link) = each ($this->parent_page->rp_links))
{
$tmp = $Link->getLink();
$str .= "<tr><td align=\"left\">" . $tmp . "</td></tr>\n";
}
global $PHP_SELF;
if ($this->parent_page->rp_show_up_link > 0)
{
$str .= "<tr><td> </td></tr>";
$str .= "<tr><td>\n";
$str .= "<a class=\"lnkDirClr\"";
$str .= " href=\"" . $this->parent_page->rp_uplink . "\" target=\"_top\">";
$str .= "<b class=\"xlarge\">Up</b></a></td></tr>\n";
}
global $PHP_SELF;
$str .= "<tr><td>";
$str .= "<a class=\"lnkDirClr\" href=\"". $this->parent_page->rp_home_link."\" target=\"_top\">\n";
$str .= " <b class=\"xlarge\"> Home </b></a></td></tr>\n";
$str .= "</table>\n";
$str .= "</td></tr></table>";
return($str);
}
function mkTextArea ()
{
$tmp = $this->parent_page->rp_dir . "/" . $this->parent_page->rp_file ;
if (file_exists ($tmp))
{
echo "<table class=\"tblNavBkg\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td>";
echo " <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
$title = isset($this->rp_page_title) ? ereg_replace ("\..*$", "", $this->rp_page_title) : "";
echo "<tr><td colspan=\"2\"><h2>$title</h2></tr></tr>\n";
$lines = Array ();
$lines = file($tmp);
$lcnt = count ($lines);
echo "<tr><td> </td><td><b>".trim($lines[0])."</b></td></tr>\n";
for ($i=1; $i < $lcnt; $i++)
{
echo "<tr><td> </td><td><b>".$lines[$i]."</b></td></tr>\n";
}
}
echo "</table>\n</td></tr></table>\n";
}
function mkHtmlArea ()
{
$tmp = $this->parent_page->rp_dir . "/" . $this->parent_page->rp_file;
//echo "$tmp<br>";
$lines=file($tmp);
$cnt = count($lines);
echo "<table class=\"tblNavBkg\"><tr><td>";
for($i=0; $i<$cnt; $i++)
{
echo $lines[$i];
}
echo "</td></tr></table>\n";
}
function show_caption ($file)
{
$suffix = ereg_replace ("\..*$", "", $file);
$capfile = $this->parent_page->rp_dir."/".$suffix.".cap";
//echo $capfile, "<br>";
if (@stat ($capfile))
{
echo "<table border=\"0\">";
$fcontents = file ($capfile);
if ($fcontents)
{
echo "<tr>
<td align=\"center\">
<table>";
while (list ($line_num, $line) = each ($fcontents))
echo "<tr>
<td align=\"left\"><b>".$line."</b></td>
</tr>\n";
}
echo "</table>\n";
}
else
echo " ";
}
function mkImageArea ()
{
$img = $this->parent_page->fileToUrl($this->parent_page->rp_file);
//echo "<br><br> rp_file: ",$this->parent_page->rp_file, " img: ", $img, " <br>";
$label = $this->parent_page->getLabel($img);
echo "<table class=\"tblDispBkg\" style=\"height:100%;\" width=\"100%\" border=\"0\">\n";
echo " <tr>
<td class=\"tblDispBkg\" height=\"400\">\n";
echo " <table class=\"tblDispBkg\" width=\"100%\" >\n";
if (!strstr ($label, "skip"))
echo "<tr><td align=\"center\"> <b>$label</b></td></tr>\n";
echo "<tr><td> </td></tr>\n";
echo "<tr>
<td align=\"center\">
<img src = \"$img\" alt=\"$label\" border=\"0\">
</td>
</tr>\n";
echo "<tr><td align=center>"; $this->show_caption ($this->parent_page->rp_file);
echo "</td></tr>";
echo "</table></td></tr></table>";
}
function mkTop ()
{
if (@stat ("toppanel"))
{
$fp = @fopen ("toppanel", "r");
@fpassthru ($fp);
@fclose ($fp);
}
}
function mkBottom ()
{
if (@stat ("bottompanel"))
{
$fp = @fopen ("bottompanel", "r");
@fpassthru ($fp);
@fclose ($fp);
}
}
}
class tableLayout extends layoutManager
{
function tableLayout ($parent)
{
$this->layoutManager($parent, $parent->rp_mode);
}
function init ()
{
$this->header_decl = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
$this->parent_page->mkLinks();
$this->mkHeader();
echo "<body class=\"tblNavBkg\">\n";
echo "<table style=\"height:100%;\" width=\"100%\" border=\"0\">\n";
echo "\n<tr>\n";
echo "<td width=\"25%\">"; echo $this->mkVerticalLinksArea(); echo "</td>\n";
echo "<td>"; $this->mkDisplay(); echo "</td>\n";
echo "\n</tr>\n";
echo "</table>\n";
echo "</td></tr></table>\n";
echo "</body>\n";
echo "</html>\n";
}
function mkTop ()
{
if (@stat ("toppanel"))
{
$fp = @fopen ("toppanel", "r");
@fpassthru ($fp);
@fclose ($fp);
}
}
function mkBottom ()
{ if (@stat ("bottompanel"))
{
$fp = @fopen ("bottompanel", "r");
@fpassthru ($fp);
@fclose ($fp);
}
}
function mkRightTable()
{
if($this->parent_page->rp_page_type == "image")
$this->mkImageArea();
elseif($this->parent_page->rp_page_type == "htm")
$this->mkHtmlArea();
elseif($this->parent_page->rp_page_type == "text")
$this->mkTextArea();
}
function mkNavigation () { $this->mkVerticalLinksArea(); }
function mkDisplay () { $this->mkRightTable (); }
}
class frameLayout extends layoutManager
{
function frameLayout ($parent, $mode)
{
$this->layoutManager($parent, $mode);
}
function init ()
{
$this->header_decl = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">';
switch ($this->lm_mode)
{
case "fnavigation":
$this->mkNavigation ();
return;
case "fdisplay":
$this->mkDisplay ();
return;
case "mkframeset":
$this->mkFrameset();
return;
}
}
function mkFrameset ()
{
global $PHP_SELF;
$thePage = $this->parent_page->rp_file;
$this->mkHeader ();
$dir_fragment = ereg_replace($this->parent_page->rp_document_root,"", $this->parent_page->rp_dir);
echo "<frameset cols=\"".$this->parent_page->rp_menuwidth.",*\" frameborder=0>\n";
echo "\n<frame SRC=\"$PHP_SELF?thePage=".$thePage."&dir=".$dir_fragment."&mode=fnavigation\" name=\"leftframe\" frameborder=0>\n";
if($this->parent_page->rp_page_type == "furl")
{
$the_FURL_file = $this->parent_page->rp_dir ."/". $this->parent_page->rp_file;
$lines = file($the_FURL_file);
$link = $lines[0];
echo "\n<frame SRC=\"".$link."\" name=\"rightframe\">\n";
}
else
echo "\n<frame src=\"$PHP_SELF?thePage=".$thePage."&dir=".$dir_fragment."&mode=fdisplay\" name=\"rightframe\" frameborder=0>\n";
echo "</frameset>\n<noframes>oops, frames</noframes>\n</html>";
}
function mkNavigation ()
{
$this->parent_page->mkLinks();
$this->parent_page->rp_collection_title = $this->parent_page->getCollectionTitle ();
$this->mkLeftFrame ();
}
function mkLeftFrame ()
{
$this->mkHeader ();
echo
"<body>";
echo "<table style=\"height:100%;\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n";
echo "<tr><td height=\"400\" valign=\"middle\" align=center>";
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n";
if(isset($this->parent_page->rp_show_collection))
{
echo "<tr><td class=\"lnkNavClr\'> $this->parent_page->rp_collection_title </td></tr>";
echo "<tr> <td class=\"lnkNavClr\">\n";
}
$str = $this->mkVerticalLinksArea();
echo "$str";
echo "</tr></table>";
echo "<td></tr></table>";
echo "</body></html>\n";
}
function mkDisplay ()
{
$this->mkRightFrame ();
}
function mkRightFrame ()
{
if ($this->parent_page->rp_page_type == 'furl')
$this->mkFurlArea ();
else
{
if (@stat ("keywords"))
{
$keywords = "keywords";
$this->parent_page->rp_keywords = `cat $keywords `;
}
$this->mkHeader ();
echo "<body> <table style=\"height:100%;\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n";
echo "<tr> <td height=\"400\"> </td><td height=\"400\">\n";
if ($this->parent_page->rp_page_type == "null")
$this->parent_page->rp_page_type = "image";
if ($this->parent_page->rp_page_type == 'image')
$this->mkImageArea ();
if ($this->parent_page->rp_page_type == 'text')
$this->mkTextArea ();
if ($this->parent_page->rp_page_type == 'htm' || $this->parent_page->rp_page_type == 'html')
$this->mkHtmlArea ();
if ($this->parent_page->rp_page_type == 'url')
$this->mkUrlArea ();
echo "</td></tr></table>\n";
echo "</body></html>\n";
}
}
}
class personal_site_page extends robopage
{
function fileToUrl($file)
{
$tmp = ereg_replace($this->rp_document_root, "/~sandy/", $this->rp_dir);
$url = $tmp . "/" . $file;
return($url);
}
}
?>