<!-- 
Disclosure - Web Log Management - v1.0.9
Alex Leigh - (aleigh at tessier dot com) - http://www.ashpool.com/
Copyright (c) 2003, Alex Leigh. All Rights Reserved.
Free for use. This notice must be retained.
-->

<?
// Supported script options: num, type, days, search, begin, end

// Set your style file here. The style file controls how your web log looks.
include "disclosure.inc";

// Define this to be true if your disclosure files are out of chronological order.
// Enabling sorting causes a performance penalty, especially for larger files.
$sortDisclosureFile=false;

// Set this to the pathame of the disclosure file you want to use by default
// if nothing is specified in the URL. Set this to the full path.
$defaultDisclosure="/web/content/ta/tessier/blog.txt";

// Set this to be true if you want Disclosure to automatically <P> block
// blank lines in your entries, so you don't have to.
$autoPBlock=true;

// Set this if you want a default limit.
//if($_GET['num']=="") $_GET['num']=10;

// Set this if you want a default type
//if($_GET['type']=="") $_GET['type']=2;

// Set this if you want a default days limit
//if($_GET['days']=="") $_GET['days']=14;

/*************************************************************************************/

print $htmlHeader;
$disclosure=readDisclosure($defaultDisclosure);
if($disclosure==false) {
  print "Unable to open disclosure file: $defaultDisclosure.";
} else {  
  printEntry($disclosure,$template);
}
print $htmlFooter;

function printEntry($entries,$template) {
  $count=0;
  $now=time();
  $lastdate="NOTLIKELY";

  if($_GET['search']!="")
    $search_array=explode(" ",$_GET['search']);
   else 
    $search_array=false;

  if($_GET['begin']!="")
    $begin=strtotime($_GET['begin']);
  else
    $begin=false;

  if($_GET['end']!="")
    $end=strtotime($_GET['end']);
  else
    $end=false;

  preg_match("/<DiscDateHeader>(.*)<\/DiscDateHeader>/",$template,$match);
  $tmpl['dateheader']=$match[1];

  preg_match("/<DiscDateFooter>(.*)<\/DiscDateFooter>/",$template,$match);
  $tmpl['datefooter']=$match[1];

  preg_match("/<DiscItem>(.*)<\/DiscItem>/",$template,$match);
  $tmpl['item']=$match[1];

  foreach($entries as $key => $entry) {
    if($_GET['type']!="")
      if($entry['type']!=$_GET['type']) continue;
 
    if($begin!=false)
      if($entry['_unixtime']<$begin) break;

    if($end!=false)
      if($entry['_unixtime']>$end) continue;

    if($_GET['days']!="") 
      if($now-$entry['_unixtime'] > ($_GET['days']*86400)) {
        if($lastdate!="NOTLIKELY") print $tmpl['datefooter'];
        break;
      }

    if($search_array!=false) {
      $matched=1;
      foreach($search_array as $value) {
        if(strpos(strtolower($entry['_text']),strtolower($value))!=0) {
          $entry['_text']=preg_replace("/".$value."/i","<EM CLASS=\"search_match\">".strtoupper($value)."</EM>",$entry['_text']);
        } else {
          $matched=0; break;
        }
      }
      if($matched==0) continue;
    }
       
    $count++;

    if($_GET['num']!="")
      if($count>$_GET['num']) break;

    if($lastdate!=$entry['_date']) {
      if($lastdate!="NOTLIKELY") print $tmpl['datefooter'];
      $output=$tmpl['dateheader'];
      $output=str_replace("%DiscDate%",$entry['_date'],$output);
      print $output;
      $lastdate=$entry['_date'];
    }

    $output = $tmpl['item'];
    if($entry['link']=="")
      $output = ereg_replace("%DiscItemTitle%",$entry['title'],$output);
    else {
      $tmp="<A HREF=\"";
      $tmp.=$entry['link'];
      $tmp.="\">";
      $tmp.=$entry['title'];
      $tmp.="</A>"; 
      $output = ereg_replace("%DiscItemTitle%",$tmp,$output);
    }

    $output = ereg_replace("%DiscItemText%",$entry['_text'],$output);
    $output = ereg_replace("%DiscItemTime%",$entry['_time'],$output);

  print $output;

  }

  if($lastdate!="NOTLIKELY") print $tmpl['datefooter'];
}

function readDisclosure($filename) {
  global $autoPBlock;

  $fh=fopen($filename,"r");

  if(!$fh) {
    return false;
  }

  $count=0;
  $state = 1;

  while(!feof($fh)) {
    $buf = fgets($fh,2048);

    if($state==1) {
      $buf=rtrim($buf);
      if($buf=="::DISCLOSURE::") {
        $state=2;
        continue;
      }
    }
   
    if($state==2) {
      $buf=rtrim($buf);
      if($buf=="") {
        $state=3;
        continue;
      } else {
        $array = explode(":",$buf,2);
        if(count($array)!=2) continue;
        $tuples[$array[0]]=ltrim($array[1]);
      }
    }

    if($state==3) {
      $buf=rtrim(ltrim($buf));
      if($buf=="::DISCLOSURE::" || feof($fh)) {
        $tuples['_text']=$text;
        if($autoPBlock==true) 
          $tuples['_text']=str_replace("\n \n","<P>",$tuples['_text']);
        $tuples['_unixtime']=strtotime($tuples['date']);
        $array = explode(" ",$tuples['date'],2);
        $tuples['_date']=$array[0];
        $tuples['_time']=$array[1];
        $disclosure[$count]=$tuples;
        $text=$values=$tuples="";
        $count++;
        $state=2;
        continue;
      }
      $text.=" ".$buf."\n";
    }
  }
  global $sortDisclosureFile;
  if($sortDisclosureFile==true) 
    $disclosure = arraySubSort($disclosure,'_unixtime');
  return $disclosure;
}

function arraySubSort($a, $c) {
  foreach ($a as $r) {
    $sa[] = $r[$c];
  }
  array_multisort($sa, SORT_NUMERIC, SORT_DESC, $a);
  return $a;
}

?>
