PHP Classes
Icontem

File: db_bot.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Colin Sandy Pittendrigh  >  Site Bot  >  db_bot.php  
File: db_bot.php
Role: Class source
Content type: text/plain
Description: recurses the source dir, inserts to schema
Class: Site Bot
Recursive-automic site buider: static OR dynamic
 

Contents

Class file image Download
<?php 

include_once "db_sql.php";
include "page_config.inc";
include "utils.php";

class db_bot
{
   var $DBG;
   var $db_keywords_str;
   var $db_domain;
   var $dbconn;
   
   var $db_dir_id;
   var $db_dir_name;
   var $db_dir_parent_id;
   var $db_default_page_id;
   var $db_default_page_type;
   
   var $db_srcpath;
   var $db_outputpath;
   var $db_document_root;
   var $db_dynamic_painter;
   var $db_url_behavior=0;
   var $db_printmode;
   var $db_css_file_url="/styles/db_bot.css";
   var $db_isGlobalDir;
   // we might want to use the link to the page made for this directory
   // as a special "global" link in the displays
   // If it is global, then it will appear on all pages from this point on down
   // Files (scanned later on) can be global too.
   // global status of individual files (not directories) will be handled
   // by a local variable in readDir
   
   function db_bot($srcpath, $outputpath, $dir_parent_id, $css_file_url='')
   {
      
      tdbg( "ctor $srcpath, $outputpath, dir_parent_id: $dir_parent_id","brown");
      
      $this->getGlobals();
      $this->db_dir_name = basename($outputpath);
      $this->db_dynamic_painter = dirname($_SERVER['PHP_SELF']) . "/" . "paint.php?page_id=";
      $this->db_srcpath = fixpath($srcpath);
      $this->db_outputpath = fixPath($outputpath);
      $this->db_isGlobalDir = 0;
      if(strstr($this->db_outputpath,"GBL-"))
      {
         $this->db_outputpath = ereg_replace("GBL-","",$this->db_outputpath);
         $this->db_isGlobalDir = 1;
      }
      $this->db_dir_parent_id = $dir_parent_id;
      
      // possible override of getGlobals, from last found css, sent by descending parent caller
      if(isset($css_file_url) && $css_file_url != '')
      {
         $this->db_css_file_url = $css_file_url;
      }
      $this->db_setup();
      $this->db_Readdir();
   }
   
   function getGlobals()
   {
      global $dconn, $sys_document_root, $sys_css_file_url, $sys_printmode;
      $this->db_document_root = fixPath($sys_document_root);
      $this->dconn = $dconn;
      $this->db_printmode = $sys_printmode;
      $this->css_file_url = $sys_css_file_url;
   }
   
   function db_setup()
   {
      if(!@stat($this->db_outputpath))
      {
         $ret = @mkdir($this->db_outputpath,0755);
      }
      $this->DBG=1;
   }
   
   
   function db_Readdir()
   {
      $scancnt=0;
      
      $ImageInfos=array();
      $Link_IDs=array();
      $Page_IDs=array();
      $isGlobalFile=array();
      
      $Captions=array();
      $Subdirs=array();
      
      tdbg("<font size=+1>top of db_Readdir on <b>$this->db_srcpath</b></font>","#2222f8");
      
      $dir_label = mkLabel($this->db_srcpath);
      
      // <scan one>   // in scan one we look for special non-page files, keywords, image captions, global links, etc
      $handle=@opendir($this->db_srcpath);
      $this->b_keywords_str = dispLabel($this->db_srcpath);
      
      $kwflag=0;
      while ($FILE = readdir($handle))
      {
         if($FILE[0] == '.') continue;
         if(strstr($FILE, "robopage")) continue;
         if(strstr($FILE,"index")) continue;
         
         $page_type = getPageType($this->db_srcpath . $FILE);
         if($page_type == "unknown")
         {
            echo "$FILE is unknown type<br>";
            continue;
         }
         
         $suffix = getSuffix($FILE);
         $label = mkLabel($FILE);
         $srcfile = $this->db_srcpath . $FILE;
         
         $isGlobalFile[ereg_replace("GBL-","",$srcfile)] = 0;
         if(strstr($srcfile,"GBL-"))
         {
            $isGlobalFile[ereg_replace("GBL-","",$srcfile)] = 1;
         }
         
         if($suffix == "css")
         {
            $this->db_css_file_url = $this->mkStatic_Link($this->db_outputpath . basename($srcfile));
         }
         elseif($suffix == "cap")
         {
            $Captions[$label]=$srcfile;
         }
         if($page_type != "unknown" && $page_type != "keywords" && !strstr($label,"skip"))
         {
            if($kwflag)
            $this->db_keywords_str .= ",";
            $this->db_keywords_str .= displabel($label);
            if(!$kwflag)
            $kwflag++;
         }
         elseif($page_type == "keywords" )
         {
            $fp = fopen($this->db_srcpath.$FILE,"r");
            $str = fgets($fp,120);
            if($kwflag > 0)
            $this->db_keywords_str .= ",";
            $this->db_keywords_str .= $str;
            fclose($fp);
         }
      }
      closedir($handle);
      // </scan one>
      
      $oont = ereg_replace("GBL-","",$this->db_srcpath);
      $this->db_dir_id = dir_insert($this->db_dir_parent_id, $oont, $dir_label,
      $this->db_css_file_url, $this->db_keywords_str);
      // <scan two> // now we have the needed background info on this directory,
      // so we scan again to make the various pages
      $skip = null;
      $handle=@opendir($this->db_srcpath);
      $lastimage=$lasttext=$lasthtm=$lasthtml=null;
      
      while ($FILE = readdir($handle))
      {
         if($FILE[0] == '.') continue;
         if(strstr($FILE,"robopage")) continue;
         if(strstr($FILE,"index")) continue;
         
         $label=$layout=$page_type=$static_link=$dynamic_link=$target=null;
         
         $srcfile = $this->db_srcpath . $FILE;
         tdbg($srcfile,"green");
         
         if(strstr($srcfile, "skip") && !strstr($srcfile, "cap"))
         {
            $skip = $srcfile;
         }
         $outputfile = $this->db_outputpath . $FILE;
         
         $page_type = getPageType($this->db_srcpath . $FILE);
         
         $target = $this->getTarget($page_type);
         $label = mkLabel($srcfile);
         
         if(strstr($FILE, "GBL-"))
         {
            $isGlobalFile[$label]=1;
         }
         else
         $isGlobalFile[$label]=0;
         
         $layout = $this->getLayoutType($page_type);
         
         if($page_type != "unknown")
         {
            $scancnt++;
            if(is_dir($srcfile))
            {
               if(!strstr($srcfile,"hidden"))
               {
                  $Subdirs[$srcfile]=$outputfile;
               }
            }
            else
            {
               if(strstr($srcfile, "default"))
               {
                  $default_page = $srcfile;
               }
               
               if($page_type == "css")
               continue;
               
               elseif($page_type == "url" )
               {
                  $dynamic_link = $this->mkUrlLink($srcfile);
                  $static_link = $dynamic_link;
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, $dynamic_link, $static_link,0, $isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("url page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  $Page_IDs[$label] = $page_id;
               }
               elseif($page_type == "downloadable")
               {
                  $static_link = $this->mkStatic_Link($srcfile);
                  $dynamic_link = $static_link;
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout,
                  $dynamic_link, $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("downloadable page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label] = $page_id;
               }
               elseif($page_type == "lbl")
               {
                  $lines=file($srcfile);
                  $lbl = $lines[0];
                  $static_link = $lbl;
                  $dynamic_link = $static_link;
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, $dynamic_link, $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("lbl page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label] = $page_id;
               }
               elseif($page_type == "html")
               {
                  // then this is a link to a page that aleady exists, no an s_page to make
                  $static_link = $this->mkStatic_Link($srcfile);
                  $dynamic_link = $static_link;
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, "xx", $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("html page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label] = $page_id;
                  $lasthtml=$srcfile;
               }
               elseif($page_type == "iframe")
               {
                  continue;
                  // might be in a hidden directory
                  $label .= "_iframe";
                  $static_link = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath) . $label.".html";
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, "xx", $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("iframe page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label] = $page_id;
                  $iframe_link = $this->mkUrlLink($srcfile);
                  iframe_insert($iframe_link, $page_id);
               }
               elseif($page_type == "std_one_image")
               {
                  if($this->db_printmode==1)
                  $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath);
                  else
                  $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_srcpath);
                  
                  
                  $static_imagelink = $dbg . basename($srcfile);
                  $static_pagelink = $dbg . swapSuffix(basename($srcfile), "html");
                  
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, "xx", $static_pagelink,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("std_one_image page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_pagelink, 0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label] = $page_id;
                  
                  if(!isset($Captions[$label]))
                  $Captions[$label] = "";
                  $image_id = image_insert($srcfile, $static_imagelink, mkLabel($srcfile), getMimeType($srcfile), $Captions[$label]);
                  $imageInfo = new ImageInfo($srcfile, $static_imagelink, $image_id);
                  $ImageInfos[$label] = $imageInfo;
                  
                  image_list_insert ($image_id, $page_id);
                  $lastimage = $srcfile;
               }
               elseif($page_type == "htm")
               {
                  $dbg = swapSuffix(basename($srcfile),"html");
                  $static_link = "/".ereg_replace($this->db_document_root,"",$this->db_outputpath).swapSuffix(basename($srcfile),"html");
                  
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, "xxx", $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("htm page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label]=$page_id;
                  
                  $htmlfragment_id = htmlfragment_insert($srcfile);
                  htmlfragment_list_insert($htmlfragment_id, $page_id);
                  $lasthtm = $srcfile;
               }
               elseif($page_type == "text")
               {
                  $static_link = "/".ereg_replace($this->db_document_root,"",$this->db_outputpath).swapSuffix(basename($srcfile),"html");
                  
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, "xxx", $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("text page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label]=$page_id;
                  
                  $textfile_id = textfile_insert($srcfile);
                  textfile_list_insert($textfile_id, $page_id);
                  $lasttext = $srcfile;
               }
               elseif($page_type == "furl")
               {
                  // then this is a frames s_page to make and a link to make
                  $static_link =
                  "/".ereg_replace($this->db_document_root,"",$this->db_outputpath).swapSuffix(basename($srcfile),"html");
                  $page_id = page_insert($this->db_dir_id, $label, $page_type, $layout, "xxx", $static_link,0,$isGlobalFile[$label]);
                  if($this->DBG)
                  tdbg("furl page_insert($this->db_dir_id, $label, $page_type, $layout, dynamic_link, $static_link,0, ".$isGlobalFile[$label].")");
                  update_s_page_dynamic_link($page_id, $this->db_dynamic_painter . $page_id);
                  $Page_IDs[$label] = $page_id;
                  
                  $furl_link = $this->mkUrlLink($srcfile);
                  furl_insert($furl_link, $page_id);
               }
            }
         }
      }
      closedir($handle);
      // </scan two>
      
      $default_file=null;
      if($skip != null)
      {
         $default_file = $skip;
      }
      elseif($lastimage != null)
      {
         $default_file = $lastimage;
      }
      elseif($lasthtm != null)
      {
         $default_file = $lasthtm;
      }
      elseif($lasttext != null)
      {
         $default_file = $lasttext;
      }
      elseif($lasthtml != null)
      {
         $default_file = $lasthtml;
      }
      else
      {
         $default_file = $this->db_outputpath . basename($this->db_srcpath) . ".htm";
         echo "No default file (as a source file) to make a dir page with.<br>";
         echo "Apache might not have write permission to $this->db_srcpath,
              so we'll put the source file (".$default_file.")in the output directory. <br>";
         copy ($srcfile, $default_file);
      }
      
      $default_page_type = getPageType($default_file);
      
      // make the dir page
      $file = mkLabel($this->db_outputpath) . ".html";
      $static_link = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath) . $file;
      $default_dir_label = $this->db_dir_name;
      
      /*
      echo "<br/><b>default file summary:</b><br/>";
      echo "this->db_outputpath: ",$this->db_outputpath ,"<br/>";
      echo "default_file: ",$default_file ,"<br/>";
      echo "default_page_type: ",$default_page_type ,"<br/>";
      echo "default_dir_label: ",$default_dir_label ,"<br/>";
      echo "static_link: ",$static_link ,"<br/>";
      */
      
      $this->db_default_page_id =
      page_insert($this->db_dir_id,$default_dir_label,$default_page_type,"std_two_column","yyy",$static_link,1,$this->db_isGlobalDir);
      $default_global_status = $this->db_isGlobalDir;
      
      tdbg($this->db_dir_id.", $default_dir_label, $default_page_type, std_two_column,yyy,$static_link,1,$default_global_status)" ,"orange");
      
      if(!isset($this->db_default_page_id))
      {
         echo "exiting on no $this->db_default_page_id<br/>";
         exit();
      }
      update_s_page_dynamic_link($this->db_default_page_id,$this->db_dynamic_painter.$this->db_default_page_id);
      
      
      $Page_IDs['index']=$this->db_default_page_id;
      
      // following: for now, if we found a default image, then
      // the dir page uses that image, else it blows up...needs fixed
      if($default_page_type == "std_one_image")
      {
         if($this->db_printmode==1)
         $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath);
         else
         $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_srcpath);
         $label = mkLabel($default_file);
         $default_file_label = mkLabel($default_file);
         
         
         $static_imagelink = $dbg . basename($default_file);
         $static_pagelink = $dbg . swapSuffix(basename($default_file), "html");
         
         if(!isset($Captions[$default_file_label]))
         $Captions[$default_file_label] = "";
         $dimage_id = image_insert($default_file, $static_imagelink, $default_file_label, getMimeType($default_file), $Captions[$default_file_label]);
         image_list_insert ($dimage_id, $this->db_default_page_id);
      }
      elseif($default_page_type == "htm")
      {
         $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath);
         $label = mkLabel($default_file);
         $static_htmlink = $dbg . basename($default_file);
         $static_pagelink = $dbg . swapSuffix(basename($default_file), "html");
         $default_file_label = mkLabel($default_file);
         $htmlfragment_id = htmlfragment_insert($default_file);
         htmlfragment_list_insert($htmlfragment_id, $this->db_default_page_id);
      }
      elseif($default_page_type == "text")
      {
         $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath);
         $label = mkLabel($default_file);
         $static_htmlink = $dbg . basename($default_file);
         $static_pagelink = $dbg . swapSuffix(basename($default_file), "html");
         $default_file_label = mkLabel($default_file);
         $textfile_id = textfile_insert($default_file);
         textfile_list_insert($textfile_id, $this->db_default_page_id);
      }
      elseif($default_page_type == "html")
      {
         $dbg = "/" . ereg_replace($this->db_document_root, "" , $this->db_outputpath);
         $label = mkLabel($default_file);
         $static_htmlink = $dbg . basename($default_file);
         $static_pagelink = $dbg . swapSuffix(basename($default_file), "html");
         $default_file_label = mkLabel($default_file);
         $htmlfile_id = htmlfragment_insert($default_file);
         htmlfile_list_insert($htmlfragment_id, $this->db_default_page_id);
      }
      
      // recursively descend into any subdirectories
      reset($Subdirs);
      while(list($srcp,$outp) = each($Subdirs))
      {
         if(isset($this->db_default_page_id))
         {
            $sb = new db_bot($srcp,$outp, $this->db_dir_id, $this->db_css_file_url);
            link_list_insert($sb->getPage_id(), $this->db_dir_id);
         }
         else
         {
            tdbg("Bad db_default_page_id on (".$this->db_default_page_id.") , $srcp, &nbsp; &nbsp; $outp", "purple");
         }
      }
      
      while(list($label,$page_id)=each($Page_IDs))
      {
         if(!isset($page_id))
         {
            tdbg("bad page_id for $label","green");
         }
         else
         {
            $dir_id = $this->db_dir_id;
            link_list_insert($page_id, $dir_id);
         }
      }
   }
   
   function getDirParent_id() { return($this->db_dir_parent_id); }
   function getDir_id() { return($this->db_dir_id); }
   function getDirName() { return($this->db_dir_name); }
   function getPage_id() { return($this->db_default_page_id); }
   
   function tmpLabel($str)
   {
      $tmpLabel = basename($str);
      $tmpLabel = ereg_replace ("\..*$", "", $tmpLabel);
      $tmpLabel = ereg_replace ("^.+\+", "", $tmpLabel);
      return($tmpLabel);
   }
   
   function getTarget($page_type)
   {
      $ret = "_top";
      if($page_type == "url" && $this->db_url_behavior > 0)
      $ret = "_other";
      return($ret);
   }
   
   function mkUrlLink($file)
   {
      $lines = file($file);
      return(trim($lines[0]));
   }
   
   function getLayoutType($page_type)
   {
      $ret = "std_two_column";
      if($page_type == "furl")
      $ret = "std_two_frame";
      elseif($page_type == "iframe")
      $ret = "std_iframe";
      return ($ret);
   }
   
   
   function mkStatic_Link($srcfile)
   {
      return ($lcl_static_link);
   }
   
}

// provides no way to reference remote images....these assumed to be on the server, in one of two modes
class ImageInfo
{
   var $info=array();
   
   function ImageInfo($filepath, $htmlpath, $image_id)
   {
      $this->info=array();
      $this->info["filepath"]=$filepath;
      $this->info["htmlpath"]=$htmlpath;
      $this->info["image_title"]=mkLabel(basename($filepath));
      $this->info["mime_type"]=getMimeType($filepath);
      $this->info["image_id"]=$image_id;
   }
   
   function dbg()
   {
      echo "<font color=cyan><b>ImageInfo->dbg:</b> ".$this->get("label"). " <br>";
      while(list($key,$val)=each($this->info)) echo " &nbsp; &nbsp; $key=$val<br>";
      echo "</font>";  reset($this->info);
   }
   function put($key, $val){ $this->info[$key]=$val; }
   function get($key){ return ($this->info[$key]); }
}

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products