]> ruin.nu Git - ndwebbie.git/blobdiff - ND.pm
minor fixes
[ndwebbie.git] / ND.pm
diff --git a/ND.pm b/ND.pm
index d62a7ea89537bc94d3f6f16c3af452c9624ba26a..7ac8e354487e720876fad29ea593fab385090ad8 100755 (executable)
--- a/ND.pm
+++ b/ND.pm
@@ -31,7 +31,6 @@ use Fcntl 'O_RDONLY';
 use strict;
 use warnings FATAL => 'all';
 
-tie our @PAGES, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY or die $!;
 
 sub handler {
        local $ND::r = shift;
@@ -44,28 +43,32 @@ sub handler {
        local $ND::BODY;
        local $ND::TICK;
        local %ND::GROUPS;
+       local $ND::ERROR;
        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';
        }
+       $ND::PAGE = '' unless defined $ND::PAGE;
        page();
        return Apache2::Const::OK;
 }
 
 sub page {
        our $DBH = ND::DB::DB();
-       our $USER = $ENV{'REMOTE_USER'};
-       my $error;# = $ND::r->param('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);
+       $TICK = 0 unless defined $TICK;
 
 
        my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
@@ -81,7 +84,8 @@ sub page {
 
        our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
 
-       $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @PAGES;
+       tie my @pages, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY, memory => 0 or die $!;
+       $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @pages;
 
        our $XML = 0;
        $XML = 1 if param('xml') and $ND::PAGE =~ /^(raids)$/;
@@ -98,17 +102,27 @@ 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;
-               $error .= "<p><b>couldn't run $ND::page</b></p>"       unless $return;
+               $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;
        }
 
        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 ($unread) = $DBH->selectrow_array(q{
+                       SELECT count(*) AS unread
+FROM forum_boards fb NATURAL JOIN forum_threads ft 
+       JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN 
+               (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
+WHERE ftv.time IS NULL OR fp.time > ftv.time AND
+       fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
+                       },undef,$UID) or $ERROR .= p($DBH->errstr);
 
+               $TEMPLATE->param(UnreadPosts => $unread);
                $TEMPLATE->param(Tick => $TICK);
                $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
                $TEMPLATE->param(isHC => isHC());
@@ -120,15 +134,16 @@ sub page {
                        $ND::TEMPLATE->param(Targets => listTargets());
                }
                $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
-               $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);
        print $output;
 
 
+       $DBH->rollback;
        $DBH->disconnect;
        $DBH = undef;
        $UID = undef;