]> ruin.nu Git - ndwebbie.git/blobdiff - ND.pm
possible to chose stylesheet
[ndwebbie.git] / ND.pm
diff --git a/ND.pm b/ND.pm
index c2bb6c0dc3ea39989602a4add5e6de8fee143e45..8b2fdb3d9b2e872213e899edf86c686a1fa490c2 100755 (executable)
--- a/ND.pm
+++ b/ND.pm
@@ -24,11 +24,18 @@ 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';
 
+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");
@@ -40,28 +47,36 @@ 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';
        }
-       page();
+       $ND::PAGE = 'main' unless (defined $ND::PAGE and exists $PAGES{$ND::PAGE});
+
+       $PAGES{$ND::PAGE}->{parse}->($ENV{REQUEST_URI});
+
+       page($ND::PAGE);
        return Apache2::Const::OK;
 }
 
 sub page {
+       my ($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 = ?');
@@ -74,40 +89,31 @@ sub page {
                $ATTACKER = 1 if $attack;
        }
 
-
-       our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
-
-       my $page = 'main';
-       if ($ND::PAGE =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users|alliances|memberIntel|resources|planetNaps)$/){
-               $page = $1;
-       }
-
        our $XML = 0;
-       $XML = 1 if param('xml') and $page =~ /^(raids)$/;
-
        our $AJAX = 1;
 
+       $PAGES{$PAGE}->{process}->();
+
        my $type = 'text/html';
        if ($XML){
                $type = 'text/xml';
                $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
-               $ND::BODY = HTML::Template->new(filename => "templates/${page}.xml.tmpl", cache => 1);
+               $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.xml.tmpl", cache => 1);
        }else{
-               $ND::BODY = HTML::Template->new(filename => "templates/${page}.tmpl", global_vars => 1, cache => 1);
-               $ND::BODY->param(PAGE => $page);
+               $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
+               $ND::BODY->param(PAGE => '/'.$ND::PAGE);
        }
 
-
-       unless (my $return = do "${page}.pl"){
-               $error .= "<p><b>couldn't parse $page: $@</b></p>" if $@;
-               $error .= "<p><b>couldn't do $page: $!</b></p>"    unless defined $return;
-               $error .= "<p><b>couldn't run $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 ($unread,$lastv) = $DBH->selectrow_array(unread_query,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());
@@ -119,15 +125,18 @@ sub page {
                        $ND::TEMPLATE->param(Targets => listTargets());
                }
                $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
-               $TEMPLATE->param(Error => $error);
+               my ($css) = $DBH->selectrow_array(q{SELECT css FROM users WHERE uid = $1},undef,$ND::UID);
+               $TEMPLATE->param(CSS => $css);
 
        }
+       $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;