#!/usr/bin/perl -w
#<head><TITLE>tv fav</TITLE>
#<meta name="category" content="pl,perl">
#<meta name="description" content="read a xml file, be dom, and look like a guestbook or Dublin Core generator.">
# </head>
package TV::gist;

use CGI qw(:standard :html3);
use CGI;
use XML::Parser;
use Data::Dumper;
use Date::Manip;
use XML::DOM;
#use XML::XQL;
#use XML::XQL::DOM;
$|=1;$:=1;

$Q=new CGI;
print $Q->header(-type=>'text/html',
		 -expires=>'-1day');  
$Qself = $Q->self_url;    
$Qselfs = $Q->url; 
$action=$Q->param('action');
$action="show" unless ($action);

my $noti="";
my $file="";
my $ddns="kwark.penguinpowered.com";
$gistdir="/www/XML/gist";
$rssdir="/www/XML/rss";
my $furl="";
$nowso= &UnixDate('now',"%Y-%m-%d %H:%M:%S (%Z)");


$domdoc="";
$id="";
$id=$Q->param('id');

if ($id){
    $file="$gistdir/$id.xml";
    unless (-r $file){
	$noti= "file $file created";
	my $tdoc=&domdoc('/www/XML/gist.template');
	$tdoc->printToFile($file);
    }
    if (-r $file){
	$domdoc=&domdoc($file); 
	
	$furl=$file;
	$furl =~ s!/www!http://$ddns!;
    }else{
	print "fucked up geen file";
	exit  0;
    }
}

print <<GROK;
<html>
 <head>
  <title>kwark gist $action $id</title>
  <link rel=stylesheet href="/css/users.css" name="kwark stijl" type="text/css">
 </head>
 <body>
$noti
  <a 
   href="http://$ddns/"
   >Kwark</a>->
  <a href="$Qselfs">gist</a>->
  $action
GROK

@element_set=(
	      'dc.title',
	      'dc.creator',
	      'dc.subject',
	      'dc.description',
	      'dc.date',
	      'dc.type',
	      'dc.format',
	      'dc.indentifier',
	      'dc.source',
	      'dc.relation',
	      'edit-date',
	      'img.width',
	      'img.height',
	      'img.size',
	      'remote-date',
	      'local_file',
	      );
@comment_set=('name',
	      'email',
	      'url',
	      'date',
	      'remote_host',
	      'http_user_agent',
	      'text'
	      );

#-------------------------------------------------------------------main
if($action eq "addcomment" && $id){
    print <<GROK;
	(<b><a href="$Qselfs?id=$id&amp;action=show">$id</a></b>)<hr />
GROK
    print "you wrote:<br><br>\n";
    foreach my $i (@comment_set){
	my $v= $Q->param($i);
	print "$i: $v<br>\n" if ($v);
    }
    if ($Q->param('text')){
	my $frag=$domdoc->createElement("comment");
	foreach my $i (@comment_set){
	    my $v= $Q->param($i);
	    if ($v){
		my $el=$domdoc->createElement($i);
		my $tn=$domdoc->createTextNode($v);
		$el->appendChild($tn);
		$frag->appendChild($el);
	    }
	}
	my $gist=$domdoc->getElementsByTagName("gist");
	$gist->[0]->appendChild($frag);
	my $nl=$domdoc->createTextNode("\n");
	$gist->[0]->appendChild($nl);
	print "<xmp>".$frag->toString."</xmp>will be  ";
#	    print "<xmp>".$domdoc->toString."</xmp>";
	$domdoc->printToFile("$file");
	print "saved\n";
    }else{
	print "nothing. <br>geen text; geen dom: go back<br>";
    }

#----------------------------------------show
}elsif ($action eq "show" && $id){
    print "(<b>$id</b>)\n<hr />\n\n";
    if (-r $file){
	my $re = &spuidc($file);
	my $redo = &spuido($file);
	print <<GROK;
<h3><a href="$furl">$id</a>
<small>
<a href="$Qselfs?action=edit&amp;id=$id">edit</a>
</small>
</h3>
$re
$redo
GROK
    my @comment= $domdoc->getElementsByTagName('comment');
	print "<hr>\n";
	foreach my $c (@comment){
	    print &spuicomment($c);
	}
	print &spuiaddform;
	print "<hr>\n";
    }else {
	print "<br>no file fucked up<br>";
    }
}elsif($action eq "adddc" && $id && $Q->param('createdc') eq "yes"){
    print "(<b>$id</b>)\n<hr />\ncreate dc element";
    my $local_file=$Q->param('local_file');
    if ($local_file){
	print " for $local_file";
	print &spuidcformplus($local_file);
    }else{
	print "fucked up";
    }
}elsif($action eq "adddc" && $id){
    print "(<b>$id</b>)\n<hr>\n";
    print &adddc;

   
}elsif ($action eq "edit" && $id){
    print "(<b>$id</b>)";
    print "\n<hr />\n\n";
    my $r="";
#    print &spuiimg($id);
    print &spuidcform;
}elsif ($action){
#
#   
    print "all <br>\n<hr>\n\n";
    print &spuiall;
    
	#
        #
}else{
        #
        #
    print "nothing to do here. <br>\n<center><blink>(go away!)</blink></center><br>\n and try a legal url.";
}
#end of action?
print <<GROK;
<br />
 <a href="mailto:pip\@dds.nl">pip\@dds.nl</a> changed 
 <a href="http://www.casema.net/~jip/ftp/gist.pl">gist.pl</a>.
</body>
</html>
GROK
undef($domdoc);
undef($parser);
undef($doc);
1;
#-------------------------------------------------------------------endmain

sub adddc{
    $nl=$domdoc->createTextNode("\n");
    $nt=$domdoc->createTextNode("\t");    
    my $gist=$domdoc->getElementsByTagName('gist');
    my $dc=$domdoc->getElementsByTagName('dc');
   
    if ($dc->[0]){
	print "dc element daar";
    }else{
	print "add a dc element";
	my $frag=$domdoc->createElement("dc");
	$frag->appendChild($domdoc->createTextNode("\n"));
	$gist->[0]->appendChild($frag);
	$gist->[0]->appendChild($domdoc->createTextNode("\n"));
	$dc=$gist->[0]->getElementsByTagName('dc');
    }
    print "<xmp>".$domdoc->toString."</xmp>";
    foreach my $i (@element_set){
	my $v=$Q->param($i);
	if ($v){
	    print "<b>$i:</b> $v<br>\n";
	    my @frag= $dc->[0]->getElementsByTagName($i);
	    if (@frag){
		print "frag da, empty and fill next" . @frag;
	    }else{
		print "geen frag tag create en vul";
		my $el=$domdoc->createElement($i);
		my $tn=$domdoc->createTextNode($v);
		my $nl=$domdoc->createTextNode("\n");
		my $nt=$domdoc->createTextNode("\t");    
		$el->appendChild($tn);
		$dc->[0]->appendChild($nt);
		$dc->[0]->appendChild($el);
		$dc->[0]->appendChild($nl);
	    }
	    print br;
	}
    }
    print "<xmp>".$domdoc->toString."</xmp>";

}


sub spuiall{
    my (@gistfiles)=`ls -1t $gistdir/*.xml`;
    chop (@gistfiles);
    my $re="";
    foreach my $i (@gistfiles){
	($i) = $i =~ /$gistdir\/(.+?)\.xml$/gi;
	$re .= <<GROK;
	<li><a href="$Qselfs?action=show&amp;id=$i">$i</a></li>
GROK
        my $gfile="$gistdir/$i.xml";
	my $doc=&domdoc($gfile);
	*comment= $doc->getElementsByTagName('comment');
	if (@comment){
	    $re .= "\n<ul>\n";
	    foreach my $c (@comment){
		$re .= "<li>" . &spuicomment($c) . "\n</li>\n";
	    }
	    $re .= "\n</ul>\n";
	}
    }
    return <<GROK;
<ul>
$re
</ul>
GROK
}
sub spuidcformplus{
    
    my $local_file=shift;
    my $r="";
    my @dc=$domdoc->getElementsByTagName('dc');
    if (@dc){
	print "fucked up ";
    }else{
	print $local_file,br;
	my $mf;
	*mf=&getHME($local_file);
	if ($local_file =~ /\.jpg$/){
	    use Image::Size;
	    ($mf{'img.width'},$mf{'img.height'})=imgsize($local_file); 
	}
	foreach my $i (keys %mf){
	    print "$i: ",$mf{$i},br;
	}
    }
    foreach  my $i (@element_set){
	    $value = &di($domdoc,$i);
	    unless ($value){
		$value=$mf{$i} if $mf{$i};
	    }
	    $r.= "\t<tr>\n\t\t<td>$i</td>\n";
	    $r.= "\t\t<td>". $Q->textfield(
				       -name => $i,
				       -default => $value,
				       -size => 40,
				       -maxlength => 240,
				       ) . "</td>\n\t</tr>";
	}

    print <<GROK;
<form name="adddc" method="get" action="$Qselfs">
<input type="hidden" name="action" value="adddc">
<input type="hidden" name="id" value="$id">
<table>

$r
<tr><td><input type="submit" value="Submit"></td><td></td></tr>
</table></form>
GROK
}




sub spuido{
    my $r="";
    my @do=$domdoc->getElementsByTagName('do');
    if (@do){
	my $uri=&di($domdoc->getElementsByTagName('dc')->[0],'dc.source');
	($buri)= $uri =~ m!(\w+://.+)/!;
	my $dist= &di($do[0],'dist');
	my $last= &di($do[0],'last');
        my $poll= &di($do[0],'poll');
	$r.=<<GROK;
<br><hr>Gist milked at <b>$last</b> for <b>$dist</b>, every <b>$poll</b> in $uri and found: 
GROK
        my $now=&ParseDate("now");
	my $futd=&DateCalc($last,$poll);
	if ($futd lt $now || !$last){
	    print "checkwanted<br>\n";
	    &UpPage($id,$uri);
	    $r.="<br>last: $last now: $now poll: $poll futd:$futd<br>";
	}
	&UpRes($id,$domdoc,$now);
	$r.=&dodist($id,$uri,$dist);
	$r .= &mkrss($id);
	$domdoc->printToFile("$gistdir/$id.xml")||die "file not saved";
	$r.="saved $gistdir/$id.xml\n";
    }
    $r;
}    

sub mkrss{
    my ($id)=shift;
    my $r="";
    local (%dtit,%crea);
    (*dtit,*crea)=&getDtit($domdoc);
    my @items= sort {$crea{$b} cmp $crea{$a}; } keys %crea;
    my $cnt=scalar(@items);
    $r.="$cnt\n<ol>\n";foreach my $i (@items){
	#print $i.$crea{$i}.$tit{$i}."<br>";
	my $d= &UnixDate($crea{$i},"%e %B");
	$r .= <<GROK
 <li>
  <a href="$i">$dtit{$i}</a> ($d)
  <br>
 </li>
GROK
}
    $r.="</ol>\n";
    use XML::RSS;
    my $rss = new XML::RSS (version => "0.91") ;
    $rss->channel(title       => $id,
              link        => "http://kwark.penguinpowered.com/tv/gist/?action=show&amp;id=$id",
              language    => "en", 
              description => "test",
              );
    splice(@items,15);
    foreach my $i (@items){
#	my $d= &UnixDate($crea{$i},"%a %e %b %Y %H:%M");
	my $d= &UnixDate($crea{$i},"%e %b");
#	warn "$d, $dtit{$i} $i\n";
        my $tit= $dtit{$i};	
	$tit=~ s/&amp;/&/g;
	$tit=~ s/&/&amp;/g;
	$i=~ s/&amp;/&/g;
	$i=~ s/&/&amp;/g;
	 $rss->add_item(title => "$dtit{$i}",
		       link  => $i,
		       ) if ($i && $dtit{$i});
    }
   
    $rss->save("$rssdir/$id.rss");
    $r.="rss saved<br>\n";
    $r;
}
sub UpPage{
    my $id=shift;
    my $uri=shift;
    my $r="";
    my $mfile="/www/XML/gist/Mirror/$id.html";
    my ($buri)= $uri =~ m!(\w+://.+)/!;
    `/usr/local/bin/webget '$uri' > $mfile`;
    my $now=&ParseDate("now");
    &setlast($domdoc,$now);
}
#-------------------------------------------------------------res
sub UpRes{
    my ($id,$domdoc,$now)=@_;
    my $mfile="/www/XML/gist/Mirror/$id.html";
    local %tits;
    *tits=&getRefs($mfile);
    my @res=$domdoc->getElementsByTagName('res');
    if (@res){print "";}else{
	print "geen res";
	my $frag=$domdoc->createElement("res");
	my $nl=$domdoc->createTextNode("\n");
	my $nt=$domdoc->createTextNode("\t");    
	my $gist=$domdoc->getElementsByTagName("gist");
	$gist->[0]->appendChild($nt);
	$gist->[0]->appendChild($frag);
	$gist->[0]->appendChild($nl);
    }
    @res=$domdoc->getElementsByTagName('res');
    (*dtit,*crea)=&getDtit($domdoc);
    print "Upres $id <br>";
    foreach my $i (keys %tits){
#	print "$i";
	unless ($dtit{$i} ){
	    &addTit($domdoc,$i,$tits{$i},$now);
	}
    }
}
sub addTit{
    local($domdoc,$ref,$tit,$created)=@_;
    unless ($ref =~ m!tv/index\.pl! && $id eq "melk"){
    print "Add item <b>$tit</b> $ref<br>\n";
     my @res=$domdoc->getElementsByTagName('res');
     unless(@res){die "geen res";}
     local $frag=$domdoc->createElement("item");
     my $nl=$domdoc->createTextNode("\n");
     my $nt=$domdoc->createTextNode("\t");    
     foreach my $i ('ref','tit','created'){
 	print "$i:".$$i." \n";
	my $iit=$domdoc->createElement($i);
	my $iitt=$domdoc->createTextNode($$i);
	$iit->appendChild($iitt);
	$frag->appendChild($iit);
     }
     $res[0]->appendChild($nt);
     $res[0]->appendChild($frag);
     $res[0]->appendChild($nl);
     if($ref =~ /momus/ && $id =~ /momus/){
 	print "Going to email<br>\n";
 	print `echo '$tit $ref' | elm -s$id pip\@dds.nl`;
 	print `echo '$tit $ref' | elm -s$id isty\@dds.nl`;
 	print `echo '$tit $ref' | elm -s$id jip`;
 	print "emailed<br>\n";
     }
   }
} 
sub getDtit{
    my ($domdoc)=@_;
    my %tits;
    my @res=$domdoc->getElementsByTagName('res');
    unless(@res){die "geen res";}
    my @item=$res[0]->getElementsByTagName('item');
    if (@item){
	foreach my $i (@item){
	    my $created=&di($i,'created');
	    my $ref=&di($i,'ref');
	    my $tit=&di($i,'tit');
	    $tits{$ref}=$tit;
	    $crea{$ref}=$created;
	}
    }else{print "No items in res? ";}
    return \%tits,\%crea;
}
sub setlast{
    my ($domdoc,$now)=@_;
    my @do=$domdoc->getElementsByTagName('do');
    if (@do){
	my $pdlast=$do[0]->getElementsByTagName("last");
	die "no do/last" unless($pdlast->[0]);
	my $dlast=$pdlast->[0]->getFirstChild;
	$dlast->setNodeValue($now);
#	print "save to file $gistdir $id .xml".$domdoc->toString;
	$domdoc->printToFile("$gistdir/$id.xml")||die "file not saved";
    }else{die "no do while we do";}
}

sub dodist{
    my ($id,$uri,$dist)=@_;
    my $r="";
    my $mfile="/www/XML/gist/Mirror/$id.html";
    my ($buri)= $uri =~ m!(\w+://.+)/!;
    unless (-r $mfile){
	warn `/usr/local/bin/webget '$uri' > $mfile`;
    }
#    (*dtit,*crea)=&getDtit($domdoc);
#    (*refs,*tits)=&getrefs($mfile);
#    local %tits;
#    (*tits)=&getRefs($mfile);
#    my $cnt=0;
#    foreach $i (keys %tits){
#	$cnt++;
#	$r .=<<GROK;
#<li><a href="$i">$tits{$i}</a></li>
#    
#GROK
#    }
#    return $cnt."<ol>$r</ol>\n\n";
    return $r;
}
sub getRefs{
    my $mfile=shift;
    my (%tits);
    open (FILE, "<$mfile") || die "Can't open $mfile" ;
    my $lines;
    while (<FILE>){$lines.=$_};
    close(FILE);
    #warn ($lines);
    while ($lines =~ s!(<a\s*href\s*=.+?</a>)!!i){
	my ($ref,$tit)= $1 =~ m!<a\s*href\s*=\"*([a-z|.|=|\d|?|+|\/|@|\:|\~|\_|A-Z|&|;|\$]+)\"*?.*?>(.+)</A>!i;
	 $tit =~ s!<.*?>!!g;
	$tit =~ s!>!!g;
	$tit =~ s!<!!g;
	$tit =~ s!\"!!g;
	$tit =~ tr/A-Z/a-z/ if ($id eq 'astronet');
	if ($tit && $ref){
#	print "\n\nref:$ref\n tit:$tit\n\n<br>";
#	warn ("\n\nref:$ref\n tit:$tit\n\n") if($tit&&$ref) ;
#	print "<b>\n\nref:$ref\n tit:$tit\n\n</b><br>" if ($tit&&$ref);
	    $ref=$buri."/".$ref unless($ref =~ m!http:|mailto:|news:|ftp:!);
	    $tits{$ref}=$tit;
	}
    }
#    print "<hr>\n";
    return \%tits;
}

sub getrefs{
    my $mfile=shift;
    my (@titles,@refs);
    open (FILE, "<$mfile") || die "Can't open $mfile" ;
    my $lines;
    while (<FILE>){$lines.=$_};
    close(FILE);
    #warn ($lines);
    while ($lines =~ s!(<a\s+href=.+?</a>)!!i){
	my ($ref,$tit)= $1 =~ m!<a\s+href=\"*(.*)\"*.*?>(.+)</A>!i;
	push (@titles,$tit);
	push (@refs,$ref);
    }
    return \@refs,\@titles;
}

#----------------------------------------------------------------------

sub spuidc{
    my $r="";
    my @dc=$domdoc->getElementsByTagName('dc');
    if (@dc){
	foreach  my $i (@element_set){
	    $value = &di($dc[0],$i);
	    if ($value && $value =~ m!http://!){
		$value= <<GROK;
<a href="$value">$value</a>
GROK
}
	    $r .= "<b>$i:</b> $value<br />" if ($value);
	}
    }
    $r;
}

sub spuidcform{
    my $r="";
    my @dc=$domdoc->getElementsByTagName('dc');
    if (@dc){
	foreach  my $i (@element_set){
	    $value = &di($domdoc,$i);
	    $r.= "\t<tr>\n\t\t<td>$i</td>\n";
	    $r.= "\t\t<td>". $Q->textfield(
				       -name => $i,
				       -default => $value,
				       -size => 40,
				       -maxlength => 240,
				       ) . "</td>\n\t</tr>";
	}
    }else{
	$r .= <<GROK;
  <tr><td>local_file:</td><td><input type="text" name="local_file" /></td></tr>
  <tr><td><input type="hidden" name="createdc" value="yes"></td>
      <td>click submit to create a dc element for this <b>$id</b></td>
  </tr>
GROK

    }
 return <<GROK;
<table>
<form name="adddc" method="get" action="$Qselfs">
<input type="hidden" name="action" value="adddc">
<input type="hidden" name="id" value="$id">

$r
<tr><td><input type="submit" value="Submit"></td><td></td></tr>
</form>
</table>
GROK
}


sub spuiimg{
    my $ifile=shift;
    #my ($w,$h)=imgsize($F->{$id}{'lfile'});
    print <<GROK;
<br />$ifile $w $h <br />
GROK
}

sub spuiaddform{
    my $remh=$ENV{'REMOTE_HOST'};
    my $browser= $ENV{'HTTP_USER_AGENT'};
    return <<GROK;
<br><br><br>
<table>
<tr><td colspan="2">
<h3>Add a comment.</h3>
</tr>
<form name="addcomment" method="get" action="/tv/gist">
<input type="hidden" name="action" value="addcomment">
<input type="hidden" name="id" value="$id">
<input type="hidden" name="date" value="$nowso">
<input type="hidden" name="remote_host" value="$remh">
<input type="hidden" name="http_user_agent" value="$browser">
<tr><td><b>name:</b>  <td><input type="text" name="name"></tr>
<tr><td><b>email:</b> <td><input type="text" name="email"></tr>
<tr><td><b>url:</b>   <td><input type="text" name="url"></tr>
<tr><td colspan="2"><textarea  type="text" name="text" cols="40" rows="4"
></textarea>
<br>
<input type="submit" value="Submit">
</tr>
</form>
</table>
GROK
}


sub spuicomment{
    my $c=shift;
    my $r="";
    my (%h);
    foreach my $i ('date','name','email','url','text'){
	my $v=&di($c,$i);
	$v="<a href=\"mailto:$v\">$v</a>" if ($i eq 'email' && $v);
	$v="<a href=\"$v\">$v</a>" if ($i eq 'url' && $v);

	$r.= "<tr><td><b>$i:</b></td><td>$v</td></tr>\n" if ($v);
    }

    return <<GROK;
    
<div class="comment">
<table>
$r
</table>
</div>
<br>


GROK
}




sub prut{
    *UF=$F{$i};
    my @favs= keys %UF;
    print" <ul class=\"favs\">\n";
    foreach $ii (@favs){
	my $n=$UF{$ii};
	#print " ",$ii," ",$n,"\n";
	print &spuiFI($i,$ii,$n);
    }
    $FE=&spuiFE($i,'','weetniet');
    print <<GROK;
 </ul>
$FE
</div>

    GROK
 
$doc->printToFile("users.xml");
    &spuidom;


    print <<GROK;
  <a href="mailto:pip\@dds.nl">pip\@dds.nl</a> changed
  <a href="http://www.casema.net/~jip/ftp/fav.pl">fav.pl</a>.
 </body>
</html>

GROK

}


#   my $frag=$doc->createElement("pname");
#    $oF->appendChild($doc->createTextNode("\t\t"));

sub di{
        my $user=shift;
        my $tagname=shift;
	my $re;
        my $t = $user->getElementsByTagName("$tagname");
	my $ar=$t->[0];
	#print "<b>geen $tagname</b>" unless $ar;
	my $arr=$ar->getFirstChild if $ar;
        $re = $arr->getData if $ar;
        $re;
}

sub domdoc{
        my $file=shift;
	my $parser = new XML::DOM::Parser;
	$parser->parsefile ("$file") || die "Cant domopen file $file $!\n";
}

#endom sub

sub toencode{
    my($toencode) = @_;
    $toencode=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
    return $toencode;
}


sub getHME{
    my ($file) = shift;
    my ($headl,$title,$name,$cont,%page);
    if (open(IN, "<$file")) {
      line: while (<IN>) {
          $headl = $_;
          ($name,$cont)= $headl =~ m!<meta name="(.+)" content="(.+)"!gi;
          $page{$name}=$cont if ($name);
          ($title) = $headl =~ m!.*<title>(.+)</title>.*!gi;
          $page{"title"} = $title if ($title);
          if ( m!</head>!i ){
              last line;
          }
      }
    }
    my($ddd)= $page{"begun"} if $page{"begun"};
    if ($ddd){
	$ddd =~ s/DST//ig;
	$page{"begund"}   = &ParseDate($ddd );
    }
    return \%page;
}



1;





