<?
// blorpscript v1.5.9
// (this one was modified by steve gedikian)
// last modified 9/10/00
// copyright (c) 2000 justin frankel
// source distributed under the gnu general public license.
// let me know if you find any security holes (I think I got most of them)
// (justin@landoleet.org)

// todo: figure out what to do with gif thumbnails. for now you have to
// manually create them (damned unisys)

  // this is the one real config item in this file. specifies how the page 
  // should reference itself.
  $thisfn="index.phtml";

  // load configuration
  include '.blorpconfig.php';

  if (isset($width) && strlen($width) > 1)
  {
    $wstr="WIDTH=$width";
    $wstr_pass="&width=$width";
  } 
  else
  {
    $width="";
    $wstr="";
    $wstr_pass="";
  }

  if (!isset($path)) $path="";

  if (strstr($path = rawurldecode($path),"..")) $path="";
  if (substr($path,0,1)=="/") $path="";

  $wdp2=$path;
  if (strlen($wdp2) < 1) $wdp2=$defaultroottitle;

  if (isset($img)) 
  {
    if (strstr($img = rawurldecode($img),"/")) $img="";
  }
  else $img="";
  if (isset($text)) 
  {
    if (strstr($text = rawurldecode($text),"/")) $text="";
  }
  else $text="";

  echo "<HTML><HEAD>" . 
       "<style type=\"text/css\"> .myform { border-top:$formborder solid thin; border-bottom" .
       ":$formborder solid thin; border-right:$formborder solid thin; border-left:$formborder solid" . 
       " thin; color:$formtextcolor; background:" . $formbg . "; $buttonfontconfig border" .
       "-width:1px; } </style>" .
       "<TITLE>$sitename - $wdp2" . "</TITLE></HEAD><BODY $bodybgconfig text=\"" . 
       "$textcolor\" link=\"$linkcolor\" alink=\"$linkcolor\" vlink=\"$linkcolor\">\n";

  echo "<CENTER>$topline</CENTER>";
  echo "<CENTER><TABLE $tableconfig bgcolor=\"$tablebg\">";

  if (isset($setrescale) && $setrescale!="")  // rescale selection
  {
    echo "<TR><TD><FORM METHOD=URL ACTION=\"$thisfn\">" .
         "<INPUT class=myform TYPE=submit VALUE=\"Rescale\"> images to" .
         " <INPUT Class=myform TYPE=NUMBER NAME=width VALUE=\"$width\" size=4> pixels wide.&nbsp;&nbsp;";
    if ($path!="") echo "<INPUT TYPE=hidden NAME=path VALUE=\"" . rawurlencode($path) . "\">";
    if ($text!="") echo  "<INPUT TYPE=hidden NAME=text VALUE=\"" . rawurlencode($text) . "\">";
    if ($img!="") echo "<INPUT TYPE=hidden NAME=img VALUE=\"" . rawurlencode($img) . "\">";
    echo "</FORM>(Leave blank for no rescaling)";
    echo "</TR></TD>";
  }
  else if ($text != "") 
  {
    echo "<TR><TD VALIGN=TOP><CENTER>";

    // current path display

    if ($path=="")
    {
      echo " <A HREF=\"$thisfn?path=$wstr_pass\">/$sitename</A>/<BR>";
    }
    else
    {
      echo " ";
      $pd_arr=split("/","$sitename/" . $path);
      $parent="";
      for($g=sizeof($pd_arr)-1;$g>=0;$g--)
      {
        $pd="";
        for($i=1;$i<sizeof($pd_arr)-$g;$i++) $pd.="/".$pd_arr[$i];
        $newpd=rawurlencode(substr($pd,1));
        $parent=$pd_arr[sizeof($pd_arr)-$g-1];

        if ($g == sizeof($pd_arr)-1) $parent = "/" . $parent;
        else echo "/";

        echo "<A HREF=\"$thisfn?path=$newpd$wstr_pass\">$parent</A>";
      }
      echo "/<BR>"; 
    }

    if (!eregi("\.(asm|inc|c|cpp|h|phps)$",$text))
      $fnt=substr($text,0,strlen($text)-strlen(strrchr($text,".")));
    else $fnt=$text;
    echo "<BR><FONT size=+2> <B>$fnt</B></font><BR><BR>";
    if ($path!="") $pathstr=str_replace("%2F","/",rawurlencode($path)) . "/";
    else $pathstr="";
    echo "</center><PRE>";
    if ($path == "") $openfile = $text;
    else $openfile = $path . "/" . $text;
    $fp=fopen($openfile,"r");
    if (eregi("\.(asm|inc|c|cpp|phps|h)$",$text)) { 
      while ($line = fgets($fp,1024)) 
      {
        $line = str_replace('&','&amp',$line);
        $line = str_replace('<','&lt;',$line);
        $line = str_replace('>','&gt;',$line);
        echo $line;
      }
    }
    else
    {
      while ($line = fgets($fp,1024)) 
      {
        echo $line;
      }
    }
    fclose($fp);
    echo "</PRE></TD></TR>";
  }
  else if ($img != "") 
  {
    echo "<TR><TD VALIGN=TOP><CENTER>";
    if ($path=="") $newwd="";
    else $newwd=rawurlencode($path);

    $openpath = ($path=="") ? "." : $path;
    $dp=opendir("./" . $openpath);
    if (!$dp)
    { 
      die("Error in URL (" . $openpath . "). Click <a href=\"$thisfn\">here</A>.<BR>");
    }
    $t=(int)0;
    while ($fnt = readdir($dp)) {
      $full_fn=$wd."/".$fnt;
      if (!is_dir($full_fn) && eregi("\.(jpg|jpeg|gif|png)$",$fnt)) 
      {
        if(substr($fnt,0,1) != ".") 
        {
          $t++;
          $fnlist[$fnt]=""; 
        }
      }
    }
    if (!isset($fnlist))
    {
      die("Error in URL (no images found) (" . $openpath . "). Click <a href=\"$thisfn\">here</A>.<BR>");
    }
    closedir($dp);
    ksort($fnlist);
    reset($fnlist);

    $lastfn="";
    $n=(int)0;
    $fixedimg=str_replace("\\'","'",$img);
    while ($fntmp = key($fnlist))
    {
      next($fnlist);
      $n++;
      if ($fntmp == $fixedimg) { break; }
      $lastfn=$fntmp;
    }
    $nextfn=key($fnlist);

    // current path display

    if ($path=="")
    {
      echo " <A HREF=\"$thisfn?path=$wstr_pass\">/$sitename</A>/<BR>";
    }
    else
    {
      echo " ";
      $pd_arr=split("/","$sitename/" . $path);
      $parent="";
      for($g=sizeof($pd_arr)-1;$g>=0;$g--)
      {
        $pd="";
        for($i=1;$i<sizeof($pd_arr)-$g;$i++) $pd.="/".$pd_arr[$i];
        $newpd=rawurlencode(substr($pd,1));
        $parent=$pd_arr[sizeof($pd_arr)-$g-1];

        if ($g == sizeof($pd_arr)-1) $parent = "/" . $parent;
        else echo "/";

        echo "<A HREF=\"$thisfn?path=$newpd$wstr_pass\">$parent</A>";
      }
      echo "/<BR>"; 
    }

    if (strlen($lastfn) > 0)  // print previous img
    {
      $lastfn=rawurlencode($lastfn);
      echo "<A HREF=\"$thisfn?path=$newwd&img=$lastfn$wstr_pass\">$previmagestr</A>&nbsp;&nbsp;&nbsp;";
    }

    echo $fixedimg . " ($n/$t)";

    if (strlen($nextfn)>0) // next image
    {
      $nextfn=rawurlencode($nextfn);
      if (strlen($nextfn) > 0) 
        echo "&nbsp;&nbsp;&nbsp;<A HREF=\"$thisfn?path=$newwd&img=$nextfn$wstr_pass\">$nextimagestr</A>";
    }
    echo "<BR>";
    if ($path == "") $txtfile=$fn . ".txt";
    else $txtfile="$path/$fn.txt";
    if (substr($txtfile,0,1)!=".") $txtfile="." . $txtfile;
    if (file_exists($txtfile) && $file=fopen($txtfile,"r"))
    {
      echo "<FONT SIZE=-1>";
      while ($line = fgets($file,4096))
      {
        echo "$line<BR>";
      }
      echo "</FONT>";
      fclose($file);
    }
    $fixedimg=rawurlencode($fixedimg);
    $newpath = $path;
    if ($newpath != "") $newpath .= "/";
    echo "&nbsp;&nbsp;<IMG $wstr SRC=\"$newpath$fixedimg\">&nbsp;&nbsp;$postimgparams";

    echo "</CENTER>";
    echo "</TD></TR>";
  } else { // browse mode

    echo "<TR><TD COLSPAN=2 NOWRAP VALIGN=TOP><PRE>";

    // read directory
    $openpath = ($path=="") ? "." : $path;
    if (!is_dir($openpath) || !($dp=opendir($openpath)))
    {
      if (!$dp=opendir("."))
        die("Error in URL (" . $path . "). Click <a href=\"$thisfn\">here</A>.<BR>");
      else $path="";
    }

    while ($fn = readdir($dp)) 
    { 
      if(substr($fn,0,1) != ".") {
        $fnlist[$fn]=""; 
      }
    };
    if (!isset($fnlist)) $fnlist[""]="";

    closedir($dp);
    ksort($fnlist);
    reset($fnlist);

    // display index text
    if ($path != "") $idxfile=$path . "/.index.txt";
    else $idxfile=".index.txt";

    if (file_exists($idxfile) && $file=fopen($idxfile,"r"))
    {
      $line = fgets($file,4096);
      if ($line)
      {
        echo "<B><FONT SIZE=\"+2\">" . chop($line) . "</FONT></B>\n";
        while ($line = fgets($file,4096)) echo chop($line) . "\n";
        echo "\n";
      }
      fclose($file);
    }
    else
    {
      $temp=strrchr($wdp2,"/");
      if (!$temp) $temp=$wdp2;
      else $temp=substr($temp,1);
      echo "<B><FONT SIZE=\"+2\">" . $temp . "</FONT></B>\n\n";
    }

    // current path display
    echo $pathindents;

    if ($path=="")
    {
      echo "/$sitename/\n";
    }
    else
    {
      $pd_arr=split("/","$sitename/" . $path);
      $parent="";
      for($g=sizeof($pd_arr)-1;$g>=1;$g--)
      {
        $pd="";
        for($i=1;$i<sizeof($pd_arr)-$g;$i++) $pd.="/".$pd_arr[$i];
        $newpd=rawurlencode(substr($pd,1));
        $parent=$pd_arr[sizeof($pd_arr)-$g-1];

        if ($g == sizeof($pd_arr)-1) $parent = "/" . $parent;
        else echo "/";

        echo "<A HREF=\"$thisfn?path=$newpd$wstr_pass\">$parent</A>";
      }
      echo "/" . $pd_arr[sizeof($pd_arr)-1] . "/\n";
    }

    // directory listing

    reset($fnlist);
    if ($path=="") $newwd="";
    else $newwd=rawurlencode($path . "/");
    while ($fn = key($fnlist))
    {
      next($fnlist);
      if ($path == "") $full_fn="./".$fn;
      else $full_fn=$path."/".$fn;
      if (is_dir($full_fn)) {
        $newfn=rawurlencode($fn);
        echo "$pathindents$subdirindent<A HREF=\"$thisfn?path=$newwd$newfn$wstr_pass\">$fn</a>\n";
      }
    }
    echo "</PRE></TD></TR>";

    // image listing

    reset($fnlist);
    $tablestat=0;
    if ($path=="") $newwd="";
    else $newwd=rawurlencode($path);
    $thumbnaildir = ".thumbnails/" . $path;
    while ($fn = key($fnlist))
    {
      next($fnlist);

      if ($path=="") $full_fn=$fn;
      else $full_fn=$path."/".$fn;

      if (!is_dir($full_fn) && eregi("\.(jpg|jpeg|gif|png)$",$fn)) 
      {
        if ($tablestat == 0)
        {
          $tablestat=1;
          echo "<TR>";
        } else if ($tablestat == 1)
        {
          $tablestat=2;
        } else if ($tablestat == 2)
        {
          echo "</TR><TR>";
          $tablestat=1;
        }
        $fnu=rawurlencode($fn);
        $stfn = substr($fn,0,strlen($fn)-strlen(strrchr($fn,".")));
        echo "<TD WIDTH=50% $thumbnailcellopts><font size=-1><A HREF=\"$thisfn?path=$newwd&img=$fnu$wstr_pass\">";
        if ($path=="") 
        {
          $thumbnailfile = $thumbnaildir . $fn;
          $createfn=$fn;
        }
        else 
        {
          $thumbnailfile = $thumbnaildir . "/" . $fn;
          $createfn=$path . "/" . $fn;
        }
        $sizestr="";
        if (eregi("\.gif$",$fn))
        {
          $thumbnailfile=$createfn;
          $tmp=GetImageSize($createfn);
          $newh=$thumbnailheight;
          $neww=$newh/$tmp[1] * $tmp[0];
          if ($neww > $tmp[0])
          {
            $neww=$tmp[0];
            $newh=$tmp[1];
          }
          if ($neww > $maxthumbnailwidth)
          {
            $neww=$maxthumbnailwidth;
            $newh=$neww/$tmp[0] * $tmp[1];
          }

          $sizestr="WIDTH=$neww HEIGHT=$newh";
        }
        if (!file_exists($thumbnailfile))
        {
          if (eregi("\.(jpg|jpeg)$",$fn))
            $im = imagecreatefromjpeg($createfn);
          else if (eregi("\.png$",$fn))
            $im = imagecreatefrompng($createfn);
          if ($im != "")
          {
            if (!file_exists($thumbnaildir))
            {
              $oldumask = umask(0); 
              mkdir($thumbnaildir,0770);
              umask($oldumask); 
            }
            if (file_exists($thumbnailfile)) unlink($thumbnailfile);
            $newh=$thumbnailheight;
            $neww=$newh/imagesy($im) * imagesx($im);
            if ($neww > imagesx($im))
            {
              $neww=imagesx($im);
              $newh=imagesy($im);
            }
            if ($neww > $maxthumbnailwidth)
            {
              $neww=$maxthumbnailwidth;
              $newh=$neww/imagesx($im) * imagesy($im);
            }
            $im2=ImageCreate($neww,$newh);
            ImageCopyResized($im2,$im,0,0,0,0,$neww,$newh,
              imagesx($im),imagesy($im));
            if (eregi("\.(jpg|jpeg)$",$fn))
              imagejpeg($im2,$thumbnailfile,50);
            else if (eregi("\.png$",$fn))
              imagepng($im2,$thumbnailfile);
            ImageDestroy($im);
            ImageDestroy($im2);
          }
        }
        if ($path=="") $wdtmp="";
        else $wdtmp=str_replace("%2F","/",rawurlencode($path . "/"));
          
        if ($sizestr=="") echo "<img src=\".thumbnails/$wdtmp$fnu\" border=0" .
             " align=\"center\" alt=\"\"></a>$thumbnailspacer$stfn";
        else echo "<img src=\"$wdtmp$fnu\" $sizestr border=0" .
             " align=\"center\" alt=\"\"></a>$thumbnailspacer$stfn";
        echo "</font></TD>";
      }
    }
    if ($tablestat != 0)
    {
      if ($tablestat == 1) echo "<TD WIDTH=50%></TD>";
      echo "</TR>";
    }

    // audio listing

    $tablestat=0;
    reset($fnlist);
    if ($path=="") $newwd="";
    else $newwd=str_replace("%2F","/",rawurlencode($path));
    while ($fn = key($fnlist))
    {
      next($fnlist);
      if ($path=="") $full_fn=$fn;
      else $full_fn=$path."/".$fn;
      $fs=floor(filesize($full_fn)/1000);
      if (!is_dir($full_fn) && eregi("\.(wav|mp2|mp3)$",$fn)) {
        $fnu=rawurlencode($fn);
        if ($tablestat == 0) {
          $tablestat=1;
          echo "<TR><TD COLSPAN=2><pre>";
        }
        echo "AUDIO: <A HREF=\"$newwd/$fnu\">$fn</A> ($fs"."k)\n";
      }
    }

    // movie listing
    reset($fnlist);
    while ($fn = key($fnlist))
    {
      next($fnlist);
      if ($path=="") $full_fn=$fn;
      else $full_fn=$path."/".$fn;
      $fs=floor(filesize($full_fn)/1000);
      if (!is_dir($full_fn) && eregi("\.(avi|mov|mpg|mpeg)$",$fn)) { 
        $fnu=rawurlencode($fn);
        if ($tablestat == 0) {
          $tablestat=1;
          echo "<TR><TD COLSPAN=2><pre>";
        }
        echo "VIDEO: <A HREF=\"$newwd/$fnu\">$fn</A> ($fs"."k)\n";
      }
    }

    // code listing
    reset($fnlist);
    if ($path=="") $newwd="";
    else $newwd=rawurlencode($path);
    while ($fn = key($fnlist))
    {
      next($fnlist);
      if ($path=="") $full_fn=$fn;
      else $full_fn=$path."/".$fn;
      if (!is_dir($full_fn) && eregi("\.(asm|inc|c|cpp|phps|h)$",$fn)) { 
        $fnu=rawurlencode($fn);
        if ($tablestat == 0) {
          $tablestat=1;
          echo "<TR><TD COLSPAN=2><PRE>";
        }
        echo "CODE: <A HREF=\"$thisfn?path=$newwd&text=$fnu$wstr_pass\">$fn</A>\n";
      }
    }

    // text listing

    reset($fnlist);
    while ($fn = key($fnlist))
    {
      next($fnlist);
      $full_fn=$wd."/".$fn;
      if (!is_dir($full_fn) && eregi("\.TXT$",$fn)) {
        $fnu=rawurlencode($fn);
        if ($tablestat == 0) {
          $tablestat=1;
          echo "<TR><TD COLSPAN=2><PRE>";
        }
        echo "TEXT: <A HREF=\"$thisfn?path=$newwd&text=$fnu$wstr_pass\">" . substr($fn,0,strlen($fn)-4) . "</A>\n";
      }
    }
    if ($tablestat == 1) echo "</TD></TR>";

  }
  echo "</TABLE></CENTER>";
  echo $bottomlinestart;
  echo "image rescaling ";
  if ($width!="") echo "set to ";
  else echo "is turned ";
  echo "<A HREF=\"$thisfn?setrescale=1&width=$width";
  if ($path!="") echo "&path=" . rawurlencode($path);
  if ($img!="") echo "&img=" . rawurlencode($img);
  if ($text!="") echo "&text=" . rawurlencode($text);
  echo "\">";
  if (strstr($width,"%")) echo "$width</a> wide";
  else if ($width != "") echo "$width</a> pixels wide";
  else echo "off</a>";
  echo " - $copyrightstring<BR>";
  echo $bottomlineend;
  echo "</BODY></HTML>\n";
  exit;
?>
