#!/usr/bin/perl # # Webinterface fuer XML-basierende To-Do-Liste # # ----- the todo xml file my $todo_file = '/wwwroot/prinzess.dyndns.org/serverdata/todo.xml'; my $php_file = 'http://prinzess.dyndns.org/db/todo/index.php'; my $db; # ----- used modules use CGI_Lite; use HTML::Entities; use MIME::QuotedPrint; use XML::Parser; use XML::Writer; use Time::localtime; # ----- helper subs sub datestr ($) { my $unixdate = shift; my $tm = localtime ($unixdate); my $result = sprintf ("%02d.%02d.%04d %02d:%02d", $tm->mday, $tm->mon + 1, $tm->year + 1900, $tm->hour, $tm->min); return $result; } sub ubb2html ($) { my $text = shift; $text =~ s#\[img\](.*?)\[/img\]##gi; $text =~ s#\[url\]http://(.*?)\[/url\]#http://$1#gi; $text =~ s#\[url\](.*?)\[/url\]#$1#gi; $text =~ s#\[url=(.*?)\](.*?)\[/url\]#$2#gi; $text =~ s#\[email\](.*?)\[/email\]#$1#gi; $text =~ s#\[b\](.*?)\[/b\]#$1#gi; $text =~ s#\[i\](.*?)\[/i\]#$1#gi; $text =~ s#\[quote\](.*?)\[/quote\]#
$1
#gi; $text =~ s#\[code\](.*?)\[/code\]#
$1
#gi; $text =~ s#(\s)http://(.*?)(\s)#$1http://$2$3#g; return $text; } # ----- xml writing sub sub write_todo { umask 002; my $xmlfile = new IO::File (">$todo_file") or die $!; my $xml = new XML::Writer ( DATA_MODE => 1, DATA_INDENT => 2, OUTPUT => $xmlfile ); $xml->xmlDecl ("ISO-8859-1", "no"); $xml->doctype ("todo", "", "todo.dtd"); $xml->startTag("todo"); foreach (@{$db}) { $xml->startTag ("entry", "date" => encode_qp ($$_{date}), "author" => encode_qp ($$_{author})); $xml->startTag ("text"); $xml->characters (encode_qp ($$_{text})); $xml->endTag ("text"); $xml->endTag("entry"); } $xml->endTag ("todo"); $xml->end; $xmlfile->close(); print "Location: $php_file\n\n"; } # ----- xml parsing subs my %this = (); my $textbuffer = ""; sub StartTag { my ($expat, $element) = @_; if ($element eq "entry") { $this{date} = decode_qp ($_{date}); $this{author} = decode_qp ($_{author}); } } sub Text { my $expat = shift; $textbuffer = $_; $textbuffer = decode_qp($textbuffer); } sub EndTag { my ($expat, $element) = @_; if ($element eq "text") { $this{text} = decode_qp ($textbuffer); } if ($element eq "entry") { my %that = %this; push @{$db}, \%that; } } # ----- parse xml data my $xml = new XML::Parser(Style => 'Stream', ErrorContext => 2); $xml->parsefile ($todo_file); # ----- sort by date @{$db} = sort { $$b{date} <=> $$a{date} } @{$db}; # ----- determine desired action my $CGI = new CGI_Lite; $CGI->set_platform('unix'); my %form = $CGI->parse_form_data; $form{action} ||= "list"; if (lc $form{action} eq "list") { # ----- begin html output print "Content-type: text/html\n\n"; print<<"...";

Todo-Liste

 Eintrag hinzufügen

Name
Eintrag

 Aktuelle Einträge


... foreach (@{$db}) { $$_{datum} = datestr($$_{date}); $author = encode_entities ($$_{author}); $text = ubb2html (encode_entities ($$_{text}) ); print " \n"; print " \n"; print " \n"; print " \n"; } print<<"...";
$author\n"; print " [Entfernen]\n"; print "  $$_{datum}
$text
\"\"
... } elsif (lc $form{action} eq "add") { # ----- add new entry to the hash reference my %newentry = ( author => encode_qp ($form{author}), date => time (), text => encode_qp ($form{text})); push @{$db}, \%newentry; # ----- write hash reference to the xml file &write_todo; } elsif ( (lc $form{action} eq "del") and ($form{id}) ) { foreach (@{$db}) { if ($$_{date} ne $form{id}) { %this = ( date => $$_{date}, author => $$_{author}, text => $$_{text} ); my %that = %this; push @{$rest}, \%that; } } $db = $rest; &write_todo; } else { print "Content-type: text/html\n\n"; print "Falscher Parameter action angegeben!\n"; exit (); }