Location: PHPKode > projects > BBS_Bulletin_Board > bbs_bulletin_board/cbbs.php
<?php 

require_once("cgi.php");
require_once("isPorno.inc"); // utils.inc includes class.inputfilter_clean.php
/**
 download and use this class for added power:
 http://www.phpclasses.org/browse/file/8942.html
 then uncomment related code in isPorno.inc httm
 require_once("class.inputfilter_clean.php"); 
**/

class BBS
{
   var $BBS_DIR;
   
   var $threadkeys= array();
   var $forums= array();
   
   var $current_forum;
   var $mode;
   var $logicalStart=0;
   var $incr=40;
   var $totalcnt;

   // following set in sys_config.inc
   var $realhome_link;
   var $home_link;
   var $home_link_label;
   var $quote;
   var $whoami;
   var $banner;
   var $stylesheet;
   var $keywords;
   var $showGoogleAds=1;
   
   function BBS()
   {
      global $sys_default_forum;
      $out = '';
      session_start();

      // <hacked backward compatibility for me (sandy at mrboats) only>
                if(isset($_GET['thingy']))
                   $_GET['forum'] = $_GET['thingy'];
                if(isset($_GET['pmode']))
                   $_GET['mode'] = $_GET['pmode'];
                if(isset($_GET['forum']) && strstr($_GET['forum'],"2006"))
                         $_GET['forum'] = "2006";
                if(isset($_GET['forum']) && strstr($_GET['forum'],"2001"))
                         $_GET['forum'] = "2001";
                if(isset($_GET['forum']) && strstr($_GET['forum'],"2002"))
                         $_GET['forum'] = "2002";
                if(isset($_GET['forum']) && strstr($_GET['forum'],"2003"))
                         $_GET['forum'] = "2003";
                if(isset($_GET['forum']) && strstr($_GET['forum'],"2004"))
                         $_GET['forum'] = "2004";
                if(isset($_GET['forum']) && strstr($_GET['forum'],"2005"))
                         $_GET['forum'] = "2005";
      // </hacked backward compatibility for me (sandy at mrboats) only>


      if($_SERVER['REQUEST_METHOD']== 'GET')
      {
         if(isset($_GET['forum']))
         $this->current_forum = $_GET['forum'];
         else
         $this->current_forum = $sys_default_forum;
         if(isset($_GET['mode']))
         $this->mode= $_GET['mode'];
         else
         $this->mode= 'list';
      }
      else
      {
         $this->current_forum= $_POST['forum'];
         $this->mode= $_POST['mode'];
      }
      if(isset($_GET['start']))
      {
         $this->logicalStart = $_GET['start'];
      }
      if(isset($_GET['incr']))
      $this->incr = $_GET['incr'];
      
   }

  function initGlobals()
  {
   global $sys_whoami, $sys_realhome_link, $sys_home_link, $sys_home_link_label, $sys_bbs_dir, 
          $sys_keywords,$sys_quote,$sys_stylesheet,$sys_show_google_ads;
   $this->realhome_link = $sys_realhome_link;
   $this->home_link = $sys_home_link;
   $this->home_link_label = $sys_home_link_label;
   $this->whoami = $sys_whoami;
   $this->BBS_DIR = $sys_bbs_dir;
   $this->keywords = $sys_keywords; 
   $this->quote = $sys_quote;
   $this->stylesheet = $sys_stylesheet;
   $this->showGoogleAds = $sys_show_google_ads;
  }
   
  function setup()
  {
      $forums = $this->filteredDirList($this->BBS_DIR, "/\./", 1, " ctor");
      while(list($idx, $lforum)= each($forums))
      {
         $path= $this->BBS_DIR.$lforum;
         $link= "";
         if(is_dir($path) && !strstr($path, "."))
         {
            $forum = "Driftboats_2006";
            if(isset($_GET['forum']))
            $forum = $_GET['forum'];
            
            $link=
            $this->home_link.$_SERVER['PHP_SELF']."?forum=".$forum."&amp;mode=list";
            $label=
            a(array("class"=>"lnkDispClr", "href"=>$link),
            $this->get_label($this->current_forum));
            $rows[]= $label;
         }
      }
      
      if ($this->mode == 'other_years')
      {
         echo $this->other_years($rows);
      }
  }

  function mark_read_button()
  {
    $str=
      a(array("class"=>"lnkDispClr", 
      "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=mark_read&amp;forum=$this->current_forum"),
      b("Mark All Read"));
    return($str);
  }

  function traffic_switcher()
  {
    $out = "";
    $name = $subject=$message="";

    if ($this->mode == 'list')
    {
      $out = $this->show_post_list() ;
      return $out;
    }
      
    elseif ($this->mode == 'show_msg')
    {
      $out = $this->show_msg();
    }
      
    elseif ($this->mode == 'new_post_form')
    {
      $out = $this->new_post_form() ;
    }
   
    elseif ($this->mode == 'new_post')
    {
     if(isset($_POST['message']))
     {
       if(isPorno($message))
       {
          exit();
       }
       $name = $_POST['name'];
       $subject = $_POST['subject'];
       $message = "<br>" . $_POST['message'];

       $_SESSION['name'] = $name;
       $_SESSION['subject'] = $subject;
       $_SESSION['message'] = $message;
     }

     if(isset($_POST['get_image']))
     {
       $images = array();
       $out = "<script language=javascript> window.location.replace('upload.php?forum=".$this->current_forum."&return_mode=new_post') </script>";
     }
     elseif(isset($_POST['next_image']))
     {
        $img = $_POST['next_image'];
        $images = $_SESSION['images'];
        $images[$img] = $img;
        $_SESSION['images'] = $images;

        $out = "<script language=javascript> 
                 window.location.replace('upload.php?forum=".$this->current_forum.
                 "&return_mode=new_post') </script>";
     }
     elseif(isset($_POST['last_image']))
     {
        $img = $_POST['last_image'];
        $images = $_SESSION['images'];
        $images[$img] = $img;
        $_SESSION['images'] = $images;
        $name = $_SESSION['name'];
        $subject = $_SESSION['subject'];
        $message = $_SESSION['message'];
        $_SESSION['images'] = "";
        session_destroy();
        $out = $this->new_post($name, $subject, $message,$images); 
     }
     else
     {
        $out = $this->new_post($name, $subject, $message,null);
     }
    }
      
    elseif ($this->mode == 'reply_form')
    {
      $out = $this->reply_form();
    }
      
    else if ($this->mode == 'post_reply')
    {
     if(isset($_POST['message']))
     {
      if(isPorno($message))
      {
        //$this->tortureSpammerLongAndHard();
        exit();
      }
      $name = $_POST['name'];
      $subject = $_POST['subject'];
      $message = "<br>" . $_POST['message'];
      $reply_to = $_POST['reply_to'];

      $_SESSION['reply_to'] = $reply_to;
      $_SESSION['name'] = $name;
      $_SESSION['subject'] = $subject;
      $_SESSION['message'] = $message;
     }

     if(isset($_POST['get_image']))
     {
       $out = "<script language=javascript> window.location.replace('upload.php?forum=".
       $this->current_forum."&return_mode=post_reply') </script>";
     }
     elseif(isset($_POST['next_image']))
     {
       $img = $_POST['next_image'];
       $images = $_SESSION['images'];
       $images[$img] = $img;
       $_SESSION['images'] = $images;

       $out = "<script language=javascript> window.location.replace('upload.php?forum=".
               $this->current_forum."&return_mode=post_reply') </script>";
     }
     elseif(isset($_POST['last_image']))
     {
       $img = $_POST['last_image'];
       $images = $_SESSION['images'];
       $images[$img] = $img;
       $_SESSION['images'] = null;
       session_destroy();
       $out = $this->post_reply($images); 
     }
     else
     {
       $out = $this->post_reply();
     }
    }
      
    if ($this->mode == 'mark_read')
    {
       $out = $this->mark_read() ;
    }

   return $out;
  }

  function mark_read()
  {
    $time_stamp = time();
    $lifetime = time() + 60*60*24*300;
    setcookie("mrb".$this->current_forum, $time_stamp, $lifetime);
    return $this->show_post_list();
  }

  function post_reply($images=null)
  {
         $out = "";
         $name=$subject=$message="";
         if($_POST['message'] != null)
         {   
     $message = $_POST['message'];
     $reply_to = $_POST['reply_to'];
     $subject = $_POST['subject'];
     $name = trim($_POST['name']);
     $date = $this->get_date(time());
         }
         else
         {   
     $message = $_SESSION['message'];
     $reply_to = $_SESSION['reply_to'];
     $subject = $_SESSION['subject'];
     $name = trim($_SESSION['name']);
     $date = $this->get_date(time());
         }

   if(isPorno($message))
   {
     return b("input format error"); 
   }
         $imgtag = "";
         if(isset($images) && $images != null)
         {
           while(list($dc,$image)=each($images))
           {   
              $tmp = sprintf("<img src=\"Uploads/%s\" border=\"0\" align=\"right\" />", $image);
              $imgtag .= $tmp . "<br>";
           }
         }

    $subject = preg_replace("/\(mrb\)|\(mbb\)|\(rtch\)|\(mfb\)/","",$subject);
         $whoami = "(" . $this->whoami . ")";
    $subject = $subject . $whoami;

    $out .=  html(). myheader($this->stylesheet). body();

    if (!preg_match("/^\d\d\d\d(\.\d\d\d\d)*$/", $reply_to))
    {
     $this->bad_input("reply to error");
    }
     
    $this->check_required($name,$subject,$message);
    $time_stamp = time();
    $date = $this->get_date($time_stamp);
    $forum_dir =  $this->BBS_DIR . $this->current_forum;
     
    $pattern = "/^$reply_to\.\d\d\d\d$/";
    $replies = $this->filteredDirList($forum_dir, $pattern,0);
     
    $replycnt = count($replies);
    if (!$replycnt)
    {
     $new_post = $reply_to . '.' .'0001';
    }
    else
    {
        $last_post = $replies[$replycnt-1];
        preg_match("/^(.+)(\d\d\d\d)$/", $last_post, $regs);
        $start = $regs[1];
        $end = $regs[2];
        $tmp = $end + 1;
        $end = sprintf("%04d", $tmp);
        $new_post = $start . $end;
    }
     
    chdir($forum_dir); // or die "Cant chdir to $forum_dir";
   
    if($whandle = fopen($new_post,"w"))
    {
        ereg("(.+)(\d\d\d\d)$",$new_post,$regs);
        $start = $regs[0];
        $end = $regs[1];
        $tmp = $end + 1;
        $end = sprintf("%04d", $tmp);
        $new_post = $start . $end;
    }
  
    $message = $message . $imgtag;   

    if(isset($whandle))
       $this->store_and_confirm($whandle,$name,$subject,$message);
  }

  function reply_form()
  {
    $out = "";
    $msg= $_GET['msg'];
    if(preg_match("/^\d\d\d\d(\.\d\d\d\d)*$/", $msg, $regs))
    {
      $msg= $regs[0];
    }
    else
    {
      bad_input();
    }

    $file= $this->BBS_DIR.$this->current_forum."/".$msg;
    $fp= fopen("$file", "r");   //|| die "$msg unopenable\n";
    $size= filesize($file);

    $subject= fgets($fp, 120);
    $author= fgets($fp);
    $dontcare= fgets($fp);
    $date= fgets($fp);
    $message= fread($fp, $size);
    fclose($fp);

    $date= $this->get_date($date);
    $quote= 0;
    if($quote)
    {
      $attribution=
         "In reply to \"$subject\", posted by $author on $date:\n";
      $message= br().$attribution.$message.br()."\n";
    }
    else
    {
        $message= '';
    }

    $out .= html() . myheader($this->stylesheet) . body();

    $out .=  h4($this->current_forum);
    $out .=  h4("Post Reply");
    $out .= table(array("cellspacing"=>"4", "cellpadding"=>"8", "border"=>"0"));
    $out .= tr(); 
    $out .= td(array("bgcolor"=>"#eeeeff"),
            a(array("class"=>"lnkDispClr", 
            "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=list&amp;forum=$this->current_forum"),  
            b("Thread List")));

    $out .=  end_tr() . end_table();
    $out .=  'Enter and edit your reply below. ';
    $out .=  p(b("In reply to \"$subject\", posted by $author on $date:"));

    $out .=  start_form($_SERVER['PHP_SELF'], "post");
    $out .=  hidden(array("name"=>"forum", "value"=>$this->current_forum));
    $out .=  hidden(array("name"=>"mode", "value"=>"post_reply"));
    $out .=  hidden(array("name"=>"reply_to", "value"=>$msg));

    $out .=  table(array("border"=>"0","cellpadding"=>"8","cellspacing"=>"4"));
    $out .=  tr(array("align"=>"left"));
    $out .=  th(array("align"=>"right"), font(array("color"=>"red"), "*")."Name");
    $out .=  td(array("align"=>"left"), textfield(array("name"=>"name", "size"=>"30")));
    $out .=  end_tr();

    $out .=  tr();
    $out .=  th(array("align"=>'right'),
        font(array("color"=>'red'), "*")."Subject: ");
    $out .=  td(array("align"=>"left"),
        textfield(array("name"=>"subject", "value"=>$subject, "size"=>"30")));
    $out .=  end_tr();


/**
    // Note, I use an upload.php I'm not ready to include in this distribution.
    // If you have an upload script you like, uncomment these following markers
    // and look for commented lines including 'upload.php' in several places  
    $out .=  tr();
    $out .=  th(array("align"=>"right", "valign"=>"top"), 
         "Check This now to Upload an Image Later") .
    "<td align=\"left\"><input type=\"checkbox\" name=\"get_image\" /></td>" .
    end_tr();
**/

    $out .=  tr();
    $out .=  th(array("align"=>"right", "valign"=>"top"), font(array("color"=>"red"), "*")."Message: ");
    $out .= td(textarea (array("name"=>"message", "rows"=>"15", "cols"=>"80"), $message));
    $out .=  end_tr();

    $out .=  tr(td(array("colspan"=>"2"), submit("Send Reply")));
    $out .=  end_table() . end_form();
    $out .=  end_body() . end_html();
    return $out;
  }


  function new_post_form()
  {
    $out = "";
    $out .=  html() . myheader($this->stylesheet).
      body();
    $out .= h1(array("align"=>"center"), "$this->current_forum").
      h4(array("align"=>"center"), "New Post");

    $out .=
      table(array
       ("cellspacing"=>"4", "cellpadding"=>"8", "border"=>"0"));

    $out .= tr(td(array("bgcolor"=>"#eeeeff"),
            a(array("class"=>"linkDispClr",
            "href"=>$this->realhome_link),
            b($this->home_link_label))), 
            td(array("bgcolor"=>"#eeeeff","align"=>"center", 
                 "valign"=>"middle"), 
                 a(array("class"=>"lnkDispClr", 
                 "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=list"),  
                 b("Thread List"))));

    $out .= end_table();

    $action=
      $_SERVER['PHP_SELF']."?mode=new_post&amp;forum=".$this->current_forum;
    $out .= start_form($action, "post") .
      hidden(array("name"=>"forum", "value"=>$this->current_forum)).
      hidden(array("name"=>"mode", "value"=>"new_post"));

    $out .= table();
    $out .= tr(array("align"=>"left")).
      th(array("align"=>'right'), 'Name: ') . td(array("align"=>"left"),
              textfield(array("name" =>"name", "size" =>"30"))).
      end_tr();

    $out .= tr();
    $out .= th(array("align"=>"right"),
        font(array("color"=>"red"), "*")."Subject: ").
      td(array("align"=>"left"), textfield(array("name"=>"subject", "size"=>"30"))). end_tr();


/**
    // Note, I use an upload.php I'm not ready to include in this distribution.
    // If you have an upload script you like, uncomment these following markers
    // and look for commented lines including 'upload.php' in several places  
    $out .= tr();
    $out .= th(array("align"=>"right", "valign"=>"top"), "Check This now to Upload an Image Later").
    "<td align=\"left\"><input type=\"checkbox\" name=\"get_image\" /></td>".
    end_tr();
**/

    $out .= tr();
    $out .= th(array("align"=>"right", "valign"=>"top"), font(array("color"=>"red"), '*')." Message: ").
      td(textarea(array("name"=>"message", "cols"=>"80", "rows"=>"15"))).
      end_tr();

    $out .= tr(td(array("colspan"=>"2"), submit("Send Post"))).
      end_table();
    $out .= end_form();

    $out .= end_body(). end_html();
    return $out;
  }

  function new_post($name, $subject, $message, $images)
  {
         $imgtag = "";
         if(isset($images))
         {
            while(list($dc,$image)=each($images))
            { 
               $tmp = sprintf("<img src=\"Uploads/%s\" align=right border=\"0\">", $image);
               $imgtag .= $tmp . "<br>";
            }
         }
  
         $message .= $imgtag;   
    $subject = $subject . "(".$this->whoami.")";
    $this->check_required($name,$subject,$message);
     
    $forum_dir =  $this->BBS_DIR . $this->current_forum;
     
    $posts =  $this->filteredDirList( $forum_dir, "/^\d\d\d\d$/",0);
    $pcnt = count($posts);
    $last_post = $posts[$pcnt-1];
     
    if (!$last_post) 
         {
         $new_post = '0001';
    }
    else 
         {
         $tmp = $last_post + 1;
         $new_post = sprintf("%04d", $tmp);
    }
    chdir($forum_dir) ;// || die "Can't cd to $forum_dir $!\n";
   if(isPorno($message))
   {
       return b("input format error");
   }
    $FILE = fopen ($new_post,"w");
    $this->store_and_confirm($FILE,$name,$subject,$message);
  }

  function getMsgBase($msg)
  {
      $bits = explode(".",$msg);
      return ($bits[0]);
  }

  function replyCount($msgBase)
  {
    $handle = opendir($this->BBS_DIR."/".$this->current_forum);
    $cnt=0;
    $new = "";

    $contents = array();
    $new = false;
    $last_visit = "";
    if(isset($_COOKIE["mrb".$this->current_forum]))
    {
      $last_visit = $_COOKIE["mrb".$this->current_forum];
    }
    while(($file = readdir($handle)) !== false)
    {
      if($msgBase && $file && ereg($msgBase, $file))
      {
           $contents[$cnt] = $file;
         $fp = fopen($this->BBS_DIR."/".$this->current_forum."/".$file,"r");
         if($fp)
         {
         $dc = fgets($fp, 80);
         $dc = fgets($fp, 80);
         $dc = fgets($fp, 80);
         $post_time = fgets($fp, 80);
         fclose($fp);
         }
         $cnt++;

         if($post_time > $last_visit)
         {
            //echo "post_time: ", $post_time," ", "last_visit: ", $last_visit,"  <br/>";
             $new = ' <font color= "red"> at least one NEW</font>';
         }
       }
   }
   if($handle)
      closedir($handle);
   $ret = count($contents) - 1;
   if($ret < 0)
       $ret = 0;
   $ret .= " replies ".  $new;

   return($ret);
  }

  function show_msg()
  {
    $msg= $_GET['msg'];
    $msgBase = $this->getMsgBase($msg);
    $posties = $this->filteredDirList($this->BBS_DIR."/".$this->current_forum, "/^$msgBase/",0);
    sort($posties);
    $out =  html() . myheader($this->stylesheet);
    $out .=  "<body>";


    if(preg_match("/(^\d\d\d\d(\.\d\d\d\d)*$)/", $msg, $regs))
      $msg = $regs[0];
    else
      $this->bad_input();

    $last_visit = "";
    if(isset($_COOKIE["mrb".$this->current_forum]))
        $last_visit = $_COOKIE["mrb".$this->current_forum];

    if($this->showGoogleAds == 1 )
        $out .= $this->mkGoogleAds();
    while(list($k,$v)=each($posties))
    {
         $lnkstr = "";
         $depth = $this->HowDeep($v) * 4; 
         for($i=0; $i<$depth; $i++)
             $lnkstr .= " &nbsp; ";
         $lnk = $this->home_link.$_SERVER['PHP_SELF'] . 
                "?mode=show_msg&amp;msg=" . 
                $v."&amp;start=".$this->logicalStart.
                "&amp;forum=".$this->current_forum;

         $file =  $this->BBS_DIR.$this->current_forum."/".$v;
         $fp = fopen($file, "r");
         $msgSubject = fgets($fp, 80);
         $msgAuthor = fgets($fp, 80);
         $dontCare = fgets($fp, 80);
         $post_time = fgets($fp, 80);

         $new = "";
         if($post_time > $last_visit)
         { 
             $new = ' <font color= "red">NEW</font>';
         }
         $msgDate = $this->get_date($post_time);
         fclose($fp);

         $lnkstr .= a(array("class"=>"lnkDispClr","href"=>$lnk), b(array("style"=>"font-size: large"),$msgSubject));
         $lnkstr .= " &nbsp; &nbsp; &nbsp; &nbsp; $msgAuthor ($msgDate)";

         if($v == $msg)
             $lnkstr .= b(array("style"=>"font-size: x-large; color: red"),"&nbsp; &lt;~ ".$new. "  <br/>");
         else
             $lnkstr .= $new . "<br/>";
         $out .= $lnkstr;
    }

    $forum_dir= $this->BBS_DIR.$this->current_forum;
    $file= $this->BBS_DIR.$this->current_forum."/".$msg;
    $fp= fopen($file, "r");
    $subject= fgets($fp, 80);
    $author= fgets($fp);
    $dontcare= fgets($fp);
    $date= fgets($fp);
    $tmp = fread($fp, filesize($file));
    $message= text_to_html($tmp);
    fclose($fp);

    $date= $this->get_date($date);

    $posts= array();
    $posts= $this->listThisforum($forum_dir);
    sort($posts, SORT_NUMERIC);

    $out .=  h2($this-> current_forum);
    $out .=  h4(text_to_html($subject));

    $reply =
      $this->home_link.$_SERVER['PHP_SELF']."?mode=reply_form&amp;forum=".$this->current_forum.
      "&amp;msg=".$msg;

    $out .= table(array("cellspacing"=>"8","cellpadding"=>"4", "border"=>"0"),
        tr(
         td(array("bgcolor"=>"#eeeeff"),a(array("class"=>"lnkDispClr","href"=>$this->realhome_link),b($this->home_link_label))),
         td(array("bgcolor"=>"#eeeeff", "align"=>"center", "valign"=>"middle"), 
            a(array("class"=>"lnkDispClr", 
              "href"=>$this->home_link.
               $_SERVER['PHP_SELF'].
               "?mode=list&amp;forum=".
               $this->current_forum.
               "&amp;start=".$this->logicalStart), 
               b('Thread List'))),
         td(array("bgcolor"=>"#eeeeff"), a(array("class"=>"lnkDispClr","href"=>$reply),b("Post Reply"))),
         td(array("bgcolor"=>"#eeeeff", "align"=>"center", "valign"=>"middle"),
         a(array ("class"=>"lnkDispClr", 
           "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=other_years"), 
           b("Other Years &nbsp; ")))));
     
    $out .=  hr(array("style"=>"width: 75%"));

    $out .=  
     table(array("border"=>"0", "width"=>"100%"),
      tr(array("align"=>'left'), td(array("colspan"=>"2"), "<b>Posted by: </b> $author")),
      tr(array("align"=>'left'), th('Date:').td($date)),
      tr(td(array("colspan"=>"2"), ' &nbsp; ')),
      tr(array("align"=>'left', "valign"=>'top'), 
         th(array("valign"=>"top"), 'Message:'),
         td(array("valign"=>"top"), $message)));

    $out .=  end_body(). end_html();
    return $out;
  }

  function HowDeep($msg)
  {
    $cnt= 0;
    $notdone= $msg;
    while($notdone)
      {
   $notdone= strstr($msg, ".");
   $msg= substr($notdone, 1);
   $cnt++;
      }
    return($cnt);
  }

  function listThisforum($forum_dir)
  {
    $idx=0;
    $posts=array();
    //$this->filteredDirList($forum_dir, "/(^\d\d\d\d(\.\d\d\d\d)*$)/");
    $posts = $this->filteredDirList($forum_dir, "/\./",1," listThisforum");

    $returnArray = array();
    $totalPostLen = count($posts);
    $realEndIdx = $totalPostLen - $this->logicalStart-1;
    $realEndIdx -= $this->incr;

    if($realEndIdx < 0)
          $realEndIdx = -1;
    $realStartIdx = $totalPostLen - $this->logicalStart-1;
    if($realStartIdx > $totalPostLen -1)
          $realStartIdx = $totalPostLen -1;
    for($i=$realStartIdx; $i>$realEndIdx; $i--)
    {
       $returnArray[$idx] = $posts[$i];
       $idx++;
    }
    return($returnArray);
  }

  function threadLink($link, $last_visit, $post_time, $author, $subject, $date)
  {
    $str = "";
    //$str .= "lv: " . $last_visit . " pt: " . $post_time;
    $str .=
      "<a class=\"lnkDispClr\" href=\"".$link."\" ><b>".$subject."</b></a>";
    $str.= " by $author ";
    $str.= "($date) ";

    if($post_time > $last_visit)
    {
       $str.= ' <font color= "red">NEW</font>';
    }

   return($str);
  }

  function getLabel($str)
  {
    $label= ereg_replace("\..*$", "", $str);
    return($label);
  }

  function mkNext()
  {
     $out = '<table border="0"><tr><td style="font-size: x-large; color: #ffffff; background-color: #eeeeff;">';
     $start = $this->logicalStart + $this->incr -1;
     if($start > ($this->totalcnt - $this->incr))
       $start = $this->totalcnt - $this->incr ;
     $out .= '<a href="'.$this->home_link.$_SERVER['PHP_SELF'].
             '?mode=list&amp;forum='.
             $this->current_forum.
             '&amp;start='.$start.
             '"> &nbsp; Next '.$this->incr.
             ' &nbsp; </a>';
     $out .= '</td></tr></table>';
     return($out);
  }

  function mkPrevious()
  {
     $out = '<table border="0"><tr><td style="font-size: x-large; color: #ffffff; background-color: #eeeeff;">';
     $start = $this->logicalStart - $this->incr + 1;
     if($start < 0)
        $start=0;
     $out .= '<a href="'.
             $this->home_link.
             $_SERVER['PHP_SELF'].
             '?mode=list&amp;forum='.
             $this->current_forum.
             '&amp;start='.
             $start.
             '"> &nbsp; Previous '.
             $this->incr.' &nbsp; </a>';
     $out .= '</td></tr></table>';
     return($out);
  }

  function navLinks()
  {
     $out = '<table border="0"><tr><td style="font-size: x-large; color: #ffffff; background-color: #eeeeff;">';
     $start = $this->logicalStart - $this->incr + 1;
     if($start < 0)
        $start=0;
     $out .= '<a href="'.
              $this->home_link.
              $_SERVER['PHP_SELF'].'?mode=list&amp;forum='.
              $this->current_forum.
              '&amp;start=0"> &nbsp; Rewind &nbsp; </a>';
     $out .= '</td></tr></table>';
     return($out);
  }

  function mkGoogleAds()
  {
   if($this->showGoogleAds == 0)
      return;
   else return "
       <script type=\"text/javascript\"><!--
       google_ad_client= \"pub-9963550438549427\";
       google_ad_width= 728;
       google_ad_height= 90;
       google_ad_format= \"728x90_as\";
       google_ad_type= \"text\";
       google_ad_channel=\"\";
       google_color_border= \"336699\";
       google_color_bg= \"FFFFFF\";
       google_color_link= \"0000FF\";
       google_color_url= \"008000\";
       google_color_text= \"000000\";
       //--></script>
       <script type=\"text/javascript\"
         src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
       </script>
       <br/>";
  }

  function show_post_list()
  {
    $current_forum = "mrb" . $this->current_forum;
    $last_visit="";
    if(isset($_COOKIE[$current_forum]))
         $last_visit = $_COOKIE[$current_forum];
    $forum_dir= $this->BBS_DIR.$this->current_forum;

    $posts = $this->listThisforum($forum_dir);
    $forum_label= $this->get_label($this->current_forum);

    $out = html() . 
           myheader($this->stylesheet) . 
           body(array("background"=>"http://montana-riverboats.com/images/bkgOldGreenBoat.jpg"));

    $out .= "
    <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">
     <tr>
      <td align=\"left\" valign=\"top\">
       <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";

   if($this->showGoogleAds==1)
   {
    $out .= "
    <tr>
     <td align=\"left\" valign=\"top\">";
          $out .= $this->mkGoogleAds();
$out .= "
     </td>
    </tr>
    <tr>
     <td><b style=\"color: #661111;\">Help support this website, click and explore the(above) Google adds!</b></td>
    </tr>
    <tr>
     <td valign=\"bottom\" align=\"center\">";
   }

   $out .= table(array("cellspacing"=>"4", "cellpadding"=>"2", "border"=>"0"),
           tr(td(array("style"=>"font-weight: bold"),$this->banner)), 
           tr(td(b($this->current_forum . " (", $this->totalcnt . " topics) ")))
           );

   $out .= "</td></tr>";
   $out .= tr($this->topLinksTD());

$out .= '
      </table>   
     </td>
    </tr>
   </table>';

    $cnt = count($posts);

/*
  need following realStartIdx stuff for display only
*/
    $displayStartIdx = $this->logicalStart +1;
    if($this->totalcnt == 0)
       $displayStartIdx = 0;

    $displayEndIdx = $this->logicalStart + $this->incr;
    if($displayEndIdx > $this->totalcnt)
        $displayEndIdx =  $this->totalcnt;

    if($this->totalcnt == 0)
      $displayEndIdx = 0;

    $out .= '<br/>showing topics '. $displayStartIdx. ' to '.$displayEndIdx.' out of '.$this->totalcnt; 
    $out .= '<table cellpadding="4" border="0"><tr><td>'.$this->mkNext();
    $out .= '</td><td>'.$this->mkPrevious().'</td>';
    $out .= '<td>'.$this->navLinks().'</td>';

    $out .= '</tr></table>';

    for($m = 0; $m < $cnt; $m++)
    {
      $item = $posts[$m];
      $file= $this->BBS_DIR.$this->current_forum."/$item";
      $stuff= file($file);
      $subject= isset($stuff[0]) ? $stuff[0] : "";
      $author= isset($stuff[1]) ? $stuff[1] : "";
      $post_time= isset($stuff[3]) ? $stuff[3] : "";
      $date= $this->get_date($post_time);
       
      $replyCnt = $this->replyCount($this->getMsgBase($item));
      $link=
      $this->home_link.$_SERVER['PHP_SELF'].
         "?forum=$this->current_forum&amp;mode=show_msg&amp;msg=".$item."&amp;start=".$this->logicalStart;

      $out .= "<table cellspacing=\"1\" cellpadding=\"4\" border=\"1\"><tr><td style=\"background-color: #eeeeff; border: 1px;\">";
      $out .= $this->threadLink($link, $last_visit, $post_time,
              $author, $subject, $date) . " ". $replyCnt;
      $out .= "</td></tr></table>";
    }

    $out .= "<br/>Keywords: " . $this->keywords;
    $out .= end_body() . end_html();
    return $out;
  }


  function get_label($str)
  {
    $label = isset($str[0]) ? $str[0] : "";
    $label= ereg_replace("_", "\x20", $str);
    return $label;
  }

  function gget_date($time)
  {
    $date= date("l dS \of F Y h:i:s A'", intval($time));
    return $date;
  }

  function get_date($time)
  {
    $date = date("F d ", intval($time));
    return $date;
  }

  function check_required($name, $subject, $message)
  {
    $empty= array();
    if(!$name)
    {
      $empty[]= 'Name';
    }
    if(!$subject)
    {
      $empty[]= 'Subject';
    }
    if(!$message)
    {
      $empty[]= 'Message';
    }

    if(count($empty) != 0)
    {
      echo html(), myheader($this->stylesheet), body(),
      h4('Empty Form Fields'),
     'The following required fields were not filled in', br();

     while(list($idx, $key)= each($empty))
     {
       echo b($key);
     }

     echo a(array
       ("class"=>"lnkDispClr", "href"=>"javascript:history.go(-1);"),
       "&nbsp; <u><b> Please try again </b> </u>"), 
       end_body(),
       end_html();
    }
  }

  function pornoLogger($name, $subject, $message)
  {
    $ph = fopen("LOGS/porno.log","a");
    fwrite($ph, $this->get_date(time()) . " name: " . $name . " subject: " 
               . $subject . " message: " . $message . "\n");  
    fclose($ph);
  }

  function store_and_confirm($fh, $name, $subject, $message)
  {
    if(isPorno($message))
    {
        $this->pornoLogger($name, $subject, $message);
        exit();
    }

    $time_stamp = time();
    $date= $this->get_date($time_stamp);

    $message = fixInput($message);
    fwrite($fh, $subject."\n");
    fwrite($fh, $name."\n");
    fwrite($fh, " "."\n");
    fwrite($fh, $time_stamp."\n");
    fwrite($fh, $message."\n");
    fclose($fh);

    $forum_label= $this->get_label($this->current_forum);

    echo html(), myheader($this->stylesheet),
      h1($forum_label),
      h4('Message Posted');

     echo $this->current_forum, "<br/>";
      echo table(array("cellspacing"=>"4", "cellpadding"=>"2", "border"=>"0"));
    echo tr() . td(array("bgcolor"=>"#eeeeff"),
      a(array ("class"=>"lnkDispClr", "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=list&amp;forum=$this->current_forum"),
    b('Thread List')));

    echo td(array("bgcolor"=>"#eeeeff"),
        a(array ("class"=>"lnkDispClr", "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=other_years"), b('Other Years')),
        td(array("bgcolor"=>"#eeeeff"), $this->mark_read_button()));
    echo end_tr(), end_table();

    echo 'The following message has been posted:', p(),
         table(array ("cellspacing"=>"4", "cellpadding"=>"2", "border"=>"0"));
         tr(array("align"=>'left'), th('Posted by:').td($name));
         echo tr(array("align"=>'left'), th('Date:').td($date));
         echo tr(array("align"=>'left'), th('Subject:').td($subject));
         echo tr(array("align"=>"left", "valign"=>"top"),
         th('Message:').td(text_to_html($message)));
         echo p(), end_table(), end_body(), end_html();
  }

  function topLinksTD()
  {
    $out = td(array("align"=>"center", "valign"=>"bottom"));
       
    $out .= table(array("cellpadding"=>"8","cellspacing"=>"4","border"=>"0")) . tr(); 

    $nplink=
      $this->home_link.$_SERVER['PHP_SELF']."?mode=new_post_form&amp;forum=".$this->current_forum;

    $out.=
      td(array("bgcolor"=>"#eeeeff"),a(array("class"=>"lnkDispClr","href"=>$this->realhome_link),b($this->home_link_label)));

    $out.=
      td(array("bgcolor"=>"#eeeeff"),a(array("class"=>"lnkDispClr", "href"=>$nplink), b("New Post")));

    $out.=
      td(array("bgcolor"=>"#eeeeff"),
      a(array("class"=>"lnkDispClr", 
      "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=other_years"),
      b(' Other Years')));

    $out.=
      td(array("bgcolor"=>"#eeeeff"),a(array("class"=>"lnkDispClr", "href"=>$this->home_link.$_SERVER['PHP_SELF'].
     "?mode=mark_read&amp;forum=$this->current_forum"),
    b(' Mark All Read <br/> (click twice)')));

    $max = $this->totalcnt - 1;
    $out.=
      td(array("bgcolor"=>"#eeeeff"),a(array("class"=>"lnkDispClr", "href"=>$this->home_link.$_SERVER['PHP_SELF'].
     "?mode=mark_read&amp;forum=$this->current_forum&amp;incr=".$max),
    b(' Show All Topics ')));

    $out.=
      td(array("bgcolor"=>"#eeeeff"),a(array("class"=>"lnkDispClr", "href"=>"ggg.php"),
    b(' Search ')));

    $out.= end_tr() . end_table() . end_td();
    return($out);
  }

  function _error($msg= null)
  {
    echo "Something went wrong<br/>";
    echo "Send email to <a href=\"formmessage.php\">MRBoats email</a><br/>";
    if($msg)
      echo "<br/>$msg<br/><br/>";
  }

  function bad_input($msg= null)
  {
    echo html(), myheader($this->stylesheet), body(),
      h4('Bad News! '.$msg),
      'It appears that your input contains illegal characters.',
      end_body(), end_html();
  }


  function threadBase($msg)
  {
    $substr= ereg_replace(strstr($msg, "."), "", $msg);
    return($substr);
  }

  function readDir($dir)
  {
    $contents= array();
    $handle= opendir($dir);

    while(($file= readdir($handle)) !== false)
    {
      if(!strstr($file[0], "."))
         $contents[]= $file;
    }
    closedir($handle);
    return($contents);
  }

  function hashedReadDir($dir)
  {
    $contents = array();
    $handle= opendir($dir);
    while(($file= readdir($handle)) !== false)
    {
      $contents[$file]= $file;
    }
    closedir($handle);
    return($contents);
  }

  function filteredDirList($dir, $pattern, $matchMode,$dbg=null)
  {
    $handle=null;
    $contents= array();
    if(is_dir($dir))
    {
      $handle = opendir($dir);
    }

    $cnt=0;

    while(($file = readdir($handle)) !== false)
    {
       if($matchMode)
       {
         if(!preg_match($pattern, $file))
         {
           $contents[$cnt] = $file;
           $cnt++;
         }
     }
     else
     {
       if(preg_match($pattern, $file))
         {
           $contents[$cnt]=$file;
           $cnt++;
         }
     }
   }

   if($handle)
      closedir($handle);

   if(count($contents) > 0)
       sort($contents, SORT_NUMERIC);
   $this->totalcnt=$cnt;
   return($contents);
  }

  function other_years()
  {
    $out = ""; 
    $forums= $this->filteredDirList($this->BBS_DIR, "/\./", 1,"other years");
    $fcnt= count($forums);
    $colspan= $fcnt + 1;
    $out .=  
       table(array("border"=>"0", "cellpadding"=>"8", "cellspacing"=>"4"));

    $out .= tr(td(array("align"=>"center", "colspan"=>$colspan), b("Other Years:")));
    $out .= tr();
    
    for($i= 0; $i < $fcnt; $i++)
    {
      $out .= td(array("bgcolor"=>"#eeeeff"),
              a(array("class"=>"lnkDispClr", 
              "href"=>$this->home_link.$_SERVER['PHP_SELF']."?mode=list&amp;forum=$forums[$i]"), 
              b(" &nbsp; $forums[$i] ")));
    }
    $out .=  end_tr() . end_table(). end_body(). end_html();
    return $out;
  }
}

?>
Return current item: BBS_Bulletin_Board