<!--
Obscura - Image Gallery Renderer - v2.2
Alex Leigh - (aleigh at tessier dot com)
http://www.ashpool.com/

$Header: /cvsroot/obscura/o2.php,v 1.5 2003/11/20 21:48:52 aleigh Exp $

Copyright (c) 2003, Alex Leigh. All Rights Reserved.
Free for use. This notice must be retained.
-->

<?

/*********************************************************************************/
/* Change these as appropriate for the look you want: */

/* Define this if you want users to be able to see the full-size images by clicking
 * them.
 */

$allow_fullsize_images=true;

function obscura_header($nav) {
  print "<LINK TYPE=\"text/css\" REL=\"stylesheet\" HREF=\"o2.css\">
<CENTER><TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH=799>
<TR><TD HEIGHT=87 ALIGN=CENTER>$nav</TD></TR>
<TR><TD HEIGHT=400><CENTER>";
}

function obscura_footer($nav) {
  print "</CENTER></TD></TR><TR><TD HEIGHT=87 ALIGN=CENTER>$nav</TD></TR></TABLE></CENTER>";
}

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

$d = makePaths();

if($d['ARGNAME']=="") {
  handleDir($d);
  return;
}

// If the FP happens to point to a directory, eval this as a request for
// a gallery.
if(is_dir($d['FULLPATH'])) {
  handleGallery($d);
  return;
}

if(is_file($d['GALLERY']."/index.cat")) {
  handleImage($d);
  return;
} else {
  if(is_file($d['FULLPATH'])) {
    handleDir($d);
    return;
  }
  print "ERROR: No gallery here.";
  return;
}

print "ERROR: No such object";


function makePaths() {
  // overflow path
  $d['OVERFLOW'] = $_SERVER['QUERY_STRING'];
  $d['OVERFLOW'] =ltrim($d['OVERFLOW'],"/");

  // full path to requested object
  $d['FULLPATH'] = $_SERVER['DOCUMENT_ROOT']."/".TruePath($d['OVERFLOW']);
  
  $d[URI] = "/".TruePath($d['OVERFLOW']);

  $d[SCRIPTURI] = $_SERVER['PHP_SELF'];

  $patharray = explode("/",$d['OVERFLOW']);
  $d['ARGNAME'] = array_pop($patharray);
  $d['GALLERY_URI'] = implode("/",$patharray);
  $d['GALLERY'] = $_SERVER['DOCUMENT_ROOT']."/".$d['GALLERY_URI'];

  return $d;
}

function catReader($file) {
  $fh = @fopen($file,"r");

  if(!$fh) {
    return null;
  }

  while($data = fgetcsv($fh,1000,"|")) {
    $cat[$row]=$data;

    $row++;
  }
  fclose ($fh);
  return $cat;
}

function handleImage($d) {
  $catfile = $d['GALLERY']."/index.cat";

  if(!is_file($catfile)) {
    print "ERROR: $catfile not found.";
    return;
  }
 
  $catstack = catReader($catfile);

  if($catstack==null) {
    print "ERROR: Catfile invalid.";
    return;
  }

  $rowcount=count($catstack);
  $rowcount-=$start;

  if($rowcount == "0" ) {
    print "No Images.";
    return;
  } 

  obscura_header("");

  $last = "NOTLIKELY";
  foreach($catstack as $key => $value) {
    if($value[0] == $d['ARGNAME']) {
      $imgpath = "/".$d['GALLERY_URI']."/scale/".$value[0]."-obs.jpg";
global $allow_fullsize_images;
      if($allow_fullsize_images==true) {
        $bigpath = "/".$d['GALLERY_URI']."/".$value[0].".jpg";
        print "<CENTER><A HREF=\"$bigpath\"><IMG SRC=\"$imgpath\" BORDER=0></A></CENTER>";
      } else {
        print "<CENTER><IMG SRC=\"$imgpath\" BORDER=0></CENTER>";
      }
      $k = $key;
      break;
    }
    $last=$key;
  }

  if($last!="NOTLIKELY") {
    $imgpath = $d['SCRIPT_URI']."?/".$d['GALLERY_URI']."/".$catstack[$last][0];
    $l_nav = "( <A HREF=\"$imgpath\">Prev</A> )";
  }

  $k++;

  $next = $catstack[$k][0];
  if($next!="") {
    $imgpath = $d['SCRIPT_URI']."?/".$d['GALLERY_URI']."/".$next;
    $n_nav = "( <A HREF=\"$imgpath\">Next</A> )";
  }

  obscura_footer("$d[$key]<BR>$date<P>$l_nav $n_nav");
}

function handleGallery($d) {
  $catfile = $d['FULLPATH']."/index.cat";

  if(!is_file($catfile)) {
    print "ERROR: $catfile not found.\n";
    return;
  }
 
  $catstack = catReader($catfile);

  if($catstack==null) {
    print "ERROR: Catfile invalid.";
    return;
  }

  $start = $_POST['start'];

  $rowcount=count($catstack);
  $rowcount-=$start;
  $realcount=0;
  $i=0;

  foreach($catstack as $key => $value) {
    $realcount++;
    if($realcount<=$start) continue;
    $p[$i]=$value[0];
    $i++;
  }

  $total=12;
  $count=0;

  obscura_header("");

  print "<CENTER><TABLE WIDTH=525 HEIGHT=395 CELLSPACING=5 CELLPADDING=0 BORDER=0>";
  for($rc=0;$rc<3;$rc++) {
    print "<TR>";
    for($cc=0;$cc<4;$cc++) {
      if($count >= $rowcount) {
        print "<TD CLASS=\"dircell\" WIDTH=125 HEIGHT=125><!-- nop --></TD>";
      } else {
        $linkurl = $d['SCRIPTURI']."?/".$d['OVERFLOW']."/".$p[$count];
        $jpgurl = $d['URI']."/scale/".$p[$count]."-sq.jpg";
        print "<TD WIDTH=125 HEIGHT=125 VALIGN=TOP><A HREF=\"$linkurl\"><IMG WIDTH=125 HEIGHT=125 SRC=\"$jpgurl\" BORDER=0></A></TD>\n";
      }
      $count++;
    }
    print "</TR>\n";
  }

  print "</TABLE></CENTER>";

  if($start>0) {
    $newstart = $start - 8;
    if($newstart < 0 ) $newstart = 0;
    $prev = makePostLink("Prev",$newstart);
  }

  if($rowcount>12) {
    $newstart = $start + 8;
    $next = makePostLink("Next",$newstart);
  }
  
  obscura_footer("$prev $next");

}

function makePostLink($desc,$start) {
  $u = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
  $formname = $desc."_form";

  $r = "<FORM ACTION=\"$u\" METHOD=\"POST\" NAME=\"$formname\">\n".
  "<INPUT TYPE=HIDDEN NAME=\"start\" VALUE=\"$start\">\n".
  "<A HREF=\"javascript:document.$formname.submit();\" onmouseover=\"window.status='$start';return true;\">$desc</A></FORM>\n";

  return $r;
}

function TruePath ($path){
    $patharray = explode ("/", $path);
    $path="";$count=count($patharray);//We have multiple count($patharray) so a minor bit faster.
    for ($i=0;$i<$count; $i++){       
        if ( $patharray[$i] == "." || $patharray[$i]== ".." ||  ( (($i+1) < $count) && ($patharray[$i+1]== "..")) ) {
            //If the current pos is a . ignore, if pos+1 == .. ignore, lastly if current = .. ignore.
        }
        else {
            $path .= $patharray[$i]."/";
        }
    }
    //Trim trailing slash
    $path = substr($path,0,-1);
    return ($path);
}

function handleDir($d) {
  $galstack = catReader($d['FULLPATH']);

  if($galstack==null) {
    print "Bad gallery listing.";
    return;
  }

/*
 * 0 URL
 * 1 Short
 * 2 Desc
 * 3 Long
 * 4 Square
 */

  srand((double)microtime()*1000000);
  $randval = (rand()%5);

  obscura_header("");
  print "<TABLE WIDTH=400>";

  foreach($galstack as $key => $value) {
    if($value[0]=="") continue; // Blank line check

    $catpath = $_SERVER['DOCUMENT_ROOT']."/".$value[0]."/index.cat";
    $catstack = catReader($catpath);

    if($catstack==NULL) continue; // Not a gallery

    $randval = rand()%(count($catstack)-1);
    $randval++;

    print "<TR><TD WIDTH=700 CLASS=\"dircell\">";

    $vp = ltrim($value[0],"/");

    $imgurl = "/".$vp."/scale/".$catstack[$randval][0]."-sq.jpg";
    $galurl = $d['SCRIPTURI']."?/".$vp;
    
    print "<P CLASS=\"dirtitle\"><A HREF=\"$galurl\"><IMG SRC=\"$imgurl\" BORDER=0 ALIGN=LEFT></A>$value[1]</P><P CLASS=\"dirdesc\">$value[2]";
  }

  print "</TABLE>";

  obscura_footer("");
}

?>
