#!/usr/bin/perl -w
#<head><title>gfx.pl</title>
#<meta name="category" content="pl,perl">
#<meta name="description" content="A scriptje to look naar plaatjes.">
#</head>

package gfx;

$|++;
use vars qw($G $L $ddns $ee $rr $ii $debug $mpc @text $stukjehead);
$G="/Gfx";
$G="/x/gfx.pl";
$L="/www/gfx";
use strict;
use jip;
$ddns=ddns();
use Date::Manip;
use CGI qw(:standard :html3);
use File::Find;

my $Q = new CGI;
$debug=$Q->param('debug');

print $Q->header(-type=>'text/html',
                 -expires=>'-1day');  
&maingfx($Q);

sub maingfx{
    my ($Q)=shift;
    my $id=$Q->param('id');
    my $cmd=$Q->param('cmd');
    my $Qself=$Q->self_url;

    my $Qselfs=$Q->url;
#    my ($Qselfu)=$Qselfs =~ m!http://[\w|\.]+/(.+)!;
#    print $Qselfu,lastaccess($Qselfu);
    my ($ufile)=$Qself =~ m!$Qselfs/(.*)!i;
    

    
    $ufile =~ s/\?.*//;
    unless ($Qself =~ /include/) {
	print <<GROK;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
 <head>
  <title>Kwark &gt; Plaatjes &gt; $ufile</title>
  <link rel="stylesheet" href="/css/kwark.css" type="text/css">
 </head>
 <body>
GROK
    }
 
    print $Qself,br,$Qselfs,br if $debug;
    print "ufile: $ufile <br>" if $debug;
    my $file="$L/$ufile";


    if (-f $file){
	showimg($file,$ufile,0);
    }elsif ($Q->param('random')){
	showrandom($file,$ufile);
    }elsif (-d $file){
	if ($Q->param('latest')){
	    showlatest($file,$ufile);
	}else{
	    showdir($file,$ufile);
	}
    }else{
	print "<blink>?</blink>",br;
    }
    print "(i:$ii e:$ee r:$rr)"	if ($debug);
    unless ($Qself =~ /include/) {
	my $check=htmlcheck($Qself);
	my $last=lastaccess($ufile);
	$last="<h3>last access</h3>$last" if $last;
	$last.=" ";
	print <<GROK;
  <div class="foot">
	  $last
   <small> 
    <a href="http://kwark.penguinpowered.com:8080/bord/956091037"
     ><img src="/gfx/jiplogo.zwart.gif"
      width="45" height="40"  style="border:0em;" alt="jip"></a>
      made 
     <a href="/bin/x/gfx.pl">gfx.pl</a>
     to view 
     <a href="/gfx/?M=D">$L</a>
     in <a href="$check">html</a>
	 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
     <a href="/x/lsgfx">laatste 25 aangeraakte plaatjes</a>
   </small>
  </div>
 </body>
</html>
GROK
    }
}

sub showlatest{
    my ($file,$ufile)=@_;
    my ($rfile,$i,@names,%when);
 
    print "directory $file",br if $debug;
    %when=getfiles($file);
    @names = sort { $when{$b} <=> $when{$a}; } keys %when;
    splice  (@names, '15');  
    foreach my $tt (@names){
	print $when{$tt},$tt,br if $debug;
    }
#    splice  (@names, '1');  
#    my $rfile=$names[rand @names];
    $rfile=$names[0];
    print "random file: $rfile ",br if $debug;
#    print %when if $debug;
    $ufile=$rfile;
    $ufile =~ s!$L/!!;
    showimg($rfile,$ufile,'latest');


}


sub getfiles{
    my ($file)=@_;
    my %when;
    use File::Find;
    find (sub {
	return unless -f;
	return if (-d $File::Find::name);
	return if ($_ =~ /rdf/);
	return if ($File::Find::name =~ /\.xvpics/);
	return if ($File::Find::name =~ /\.html/);
	return if ($File::Find::name =~ /thumb/);
	return if ($File::Find::name =~ /\.pl$/);
	return if ($File::Find::name =~ /Small/);
	return if ($File::Find::name =~ /\.xml$/);
	return if ($File::Find::name =~ /orig/);

	
	$when{$File::Find::name} = (stat _)[9];
    },$file);
    return %when; 
}


sub showrandom{
    my ($file,$ufile)=@_;
    print "directory $file",br if $debug;
    my %when;
    %when=getfiles($file);
    my @names = sort { $when{$b} <=> $when{$a}; } keys %when;
    my $rfile=$names[rand @names];
    print "random file: $rfile ",br if $debug;
#    print %when if $debug;
    $ufile=$rfile;
    $ufile =~ s!$L/!!;
    showimg($rfile,$ufile,'random');
    
}



sub showdir{
    my ($file,$ufile)=@_;
    print "<div>";
    print "directory $file",br if $debug;
    print dirhead($ufile);
    opendir (DIR,$file);
    my @files=readdir DIR;
    closedir DIR;
    foreach my $i (@files){
	print direntry($ufile,$i);
    }
    print "</div>";
}
sub dirhead{
    my $i=shift;
    return <<GROK;
<h3>Plaatjes in $i</h3>
GROK
}

sub chksize{
    my ($w,$h)=@_;
    
    while ( ($w > 600) || ($h > 540)){
	$w = $w * .9;
	$h = $h * .9;
    }
    return (int $w, int $h);
}

sub showimg{
    my ($file,$ufile,$random)=@_;
    my $cmd=$random;

    my $sub="";
    my $rex="";
    my $tablewidth="";
    my $xdir="";
    my ($dir,$sfile,$lowsrc);

    print "no file defined " if ($ufile eq"");

    use Image::Size;
    my ($w,$h)=imgsize($file);
    my $mesg = $w."x".$h;
    my ($pw,$ph)=chksize($w,$h);
    my $des=imgdes($file);
  
    ($sfile)= $ufile =~ m!.*/(.+\..+)$!; 
    $dir=$ufile;
    $dir=~ s!$sfile!!;
    $dir =~ s!/$!!g if $dir;
    $dir="" unless $dir;
    unless($sfile){
	$sfile=$dir;
	$dir="";
    }
    print "ufile $ufile dir $dir sfile $sfile",br if $debug;

    $xdir = "<a href=\"$G/$dir$sub\" title=\"View index of $dir\">$dir</a> &gt; " if $dir;
    $rex="random" if $random;
    $rex="$cmd" if $cmd;
    $sub="?" if $cmd;
    # $sub .="random=1" if $random;
    $sub.="&amp;debug=1" if $debug;
    $sub.="&amp;$cmd=1" if $cmd;
    my $extra;
    $rex="rnd" if ($rex eq 'random');
    if ($rex){
    $extra=<<GROK;
<a href="$G/$sub">$rex</a>
GROK
    }else{
	$extra=" ";
    }

    print <<GROK;

   <table class="imgblok"> 
    <tr>
     <td valign="top">
      <p class="imgtitle">
       <a href="http://$ddns/"
	   title="Goto Kwark's homepage">Kwark</a> &gt;
       <a href="$G/"
	   title="Goto Kwark's gfx index page">Gfx</a> $extra &gt;
       $xdir
       <a href="/gfx/$ufile"
	   title="View the raw image">$sfile</a>
      </p> 
     </td>
    </tr>
    <tr>
     <td>
     <a href="$G/$dir$sub"><img src="/gfx/$ufile" width="$pw" height="$ph" 
         alt="$ufile $mesg   [ k l i k   f o r   m e e r ] "
	 title="$ufile $mesg   [ k l i k   f o r   m e e r ] "
        style="border:0;"></a> 

     </td>
    </tr>
    <tr><td>$des $w x $h
	<a href="/y/jpg.pl/$ufile">.</a></td></tr>
   </table>

GROK
}

sub thumb{
    my ($dir,$file,$des)=@_;
    my ($thfile,$r);
    $dir="/$dir" if $dir;
    $thfile="ietniettoepasselijks";
    $thfile="$L/$dir/thumb-$file" unless (-r $thfile);
    $thfile="$L/$dir/Small/$file.jpg" unless (-r $thfile);
    $thfile="$L/$dir/.thumbnails/$file.png" unless (-r $thfile);

    print $thfile if $debug;
    if (-r $thfile){
	use Image::Size;
	my ($w,$h)=imgsize($thfile);
	my $lfile="$L/$dir/$file";
	print $lfile if $debug;
	my ($ow,$oh)=imgsize($lfile);
	$thfile =~ s!/www!!;
        $r=<<GROK;
<img src="$thfile" width="$w" height="$h" border="0" 
 alt="$file ($des $ow x $oh)"
 title="$file ($des $ow x $oh)"
>
GROK
}else{
    $r=""
    }
    $r;
}

sub direntry{
    my ($dir,$file)=@_;
    my $pre="$G";
    return if ($file =~ /thumb/);
    return if ($file eq ".");
    return if ($file =~ /^\.\w+/);
    return if ($file =~ /rdf$/);
    return if ($file =~ /html$/);
    return if ($file eq 'Small');
    $dir = "$dir/" if $dir;
    $dir =~ s!//!/!;
    my $lfile="$L/$dir$file";

    my $r;
    
    my $description;
    $description = jpg_comment($lfile);

    $description="" if ($description eq "no description") ;
    $description="" if ($description =~ /XV/);
    $description="" if ($description =~ /LEAD/);
    $description="" if ($description =~ /Adobe/);

    my  $des=$description;
    if ($des =~ /rdf:RDF/){
	my $de;
	$des =~ s!<\?xml version.*#image">!!gcs;
	$des =~ s!<s\d:(\w+)>(.*)<\/s\d:.*>!$1: $2!gc;
	($de)=$des=~ /Title:(.*?)\n/s;
	
        $des=$de;
    }else{ $des =~ s/\n.*//g;};
    
    $description=$des;
    $description =~ s!<.*?>!!g if $description;
    my $thumb=thumb($dir,$file,$description);
    $des=$thumb; 
    $des=$file unless($des);   
    $des="<b title=\"Go one level up\">&lt;$file&gt;</b>" if (-d $lfile);
    $des=~ s/\s*$//;
    $r =  <<GROK;

      <a href="$pre/$dir$file">$des</a>

GROK
    $r;
}





sub htmlchecktmp{
    my $i=shift;
    my $nameu=toencode($i);
    my $htmlhelp="http://www.htmlhelp.com/cgi-bin/validate.cgi?url=$nameu&amp;input=yes";
    return $htmlhelp;
}









