]> ruin.nu Git - ndwebbie.git/blobdiff - ND.pm
graphs
[ndwebbie.git] / ND.pm
diff --git a/ND.pm b/ND.pm
index 70000c8094192ea30107bc00295b976982b0d4fd..997914241086d75eb46541e06dbd2e8b20121356 100755 (executable)
--- a/ND.pm
+++ b/ND.pm
@@ -24,19 +24,24 @@ use HTML::Template;
 use DBI;
 use DBD::Pg qw(:pg_types);
 use Apache2::Request;
-use ND::Include;
+use ND::Web::Include;
 use ND::DB;
+use ND::Include;
 use Tie::File;
 use Fcntl 'O_RDONLY';
 use strict;
-use warnings FATAL => 'all';
+use warnings;
 
-tie our @PAGES, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY or die $!;
+$SIG{__WARN__} = sub {$ND::ERROR .= p $_[0]};
+
+chdir '/var/www/ndawn/code';
+our %PAGES;
+our $NOACCESS = HTML::Template->new(filename => 'templates/NoAccess.tmpl', global_vars => 1, cache => 1);
 
 sub handler {
        local $ND::r = shift;
        local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
-       local $ND::DBH;
+       local $ND::DBH = ND::DB::DB();
        local $ND::USER;
        local $ND::UID;
        local $ND::PLANET;
@@ -44,25 +49,37 @@ sub handler {
        local $ND::BODY;
        local $ND::TICK;
        local %ND::GROUPS;
+       local $ND::ERROR;
+       local $ND::USETEMPLATE = 1;
        local $ND::PAGE = $ND::req->param('page');
 
        if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){
                $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl';
        }
-       page();
+       $ND::PAGE = 'main' unless (defined $ND::PAGE and exists $PAGES{$ND::PAGE});
+
+       $PAGES{$ND::PAGE}->{parse}->($ENV{REQUEST_URI});
+
+       if ($ND::USETEMPLATE){
+               page($ND::DBH,$ND::PAGE);
+       }else{
+               $PAGES{$ND::PAGE}->{render}->($ND::DBH,$ENV{REQUEST_URI});
+       }
+
        return Apache2::Const::OK;
 }
 
 sub page {
-       our $DBH = ND::DB::DB();
-       our $USER = $ENV{'REMOTE_USER'};
-       my $error = '';
+       my ($DBH,$PAGE) = @_;
+       $DBH->do(q{SET timezone = 'GMT'});
+
+       our $ERROR;
 
        chdir '/var/www/ndawn/code';
 
        our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
 
-       our ($UID,$PLANET) = $DBH->selectrow_array('SELECT uid,planet FROM users WHERE username = ?'
+       our ($UID,$PLANET,$USER) = $DBH->selectrow_array('SELECT uid,planet,username FROM users WHERE username ILIKE ?'
                ,undef,$ENV{'REMOTE_USER'});
 
        our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
@@ -79,16 +96,11 @@ sub page {
                $ATTACKER = 1 if $attack;
        }
 
-
-       our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
-
-       $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @PAGES;
-
        our $XML = 0;
-       $XML = 1 if param('xml') and $ND::PAGE =~ /^(raids)$/;
-
        our $AJAX = 1;
 
+       $PAGES{$PAGE}->{process}->();
+
        my $type = 'text/html';
        if ($XML){
                $type = 'text/xml';
@@ -99,17 +111,20 @@ sub page {
                $ND::BODY->param(PAGE => $ND::PAGE);
        }
 
-       unless (my $return = do "$ND::PAGE.pl"){
-               $error .= "<p><b>couldn't parse $ND::PAGE: $@</b></p>" if $@;
-               $error .= "<p><b>couldn't do $ND::PAGE: $!</b></p>"    unless defined $return && defined $!;
-               $error .= "<p><b>couldn't run $ND::PAGE</b></p>"       unless $return;
-       }
+       $ND::BODY = $PAGES{$PAGE}->{render}->($DBH,$ND::BODY);
 
        unless ($XML){
                my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID);
 
                $fleetupdate = 0 unless defined $fleetupdate;
 
+               my ($last_forum_visit) = $DBH->selectrow_array(q{SELECT last_forum_visit FROM users WHERE uid = $1}
+                       ,undef,$UID) or $ERROR .= p($DBH->errstr);
+               my ($unread,$newposts) = $DBH->selectrow_array(unread_query,undef,$UID,$last_forum_visit)
+                       or $ERROR .= p($DBH->errstr);
+               
+               $TEMPLATE->param(UnreadPosts => $unread);
+               $TEMPLATE->param(NewPosts => $newposts);
                $TEMPLATE->param(Tick => $TICK);
                $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
                $TEMPLATE->param(isHC => isHC());
@@ -121,9 +136,11 @@ sub page {
                        $ND::TEMPLATE->param(Targets => listTargets());
                }
                $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
+               my ($css) = $DBH->selectrow_array(q{SELECT css FROM users WHERE uid = $1},undef,$ND::UID);
+               $TEMPLATE->param(CSS => $css);
 
        }
-       $TEMPLATE->param(Error => $error);
+       $TEMPLATE->param(Error => $ERROR);
        $ND::TEMPLATE->param(BODY => $ND::BODY->output);
        my $output = $TEMPLATE->output;
        print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);