package ND;
use CGI qw/:standard/;
-use HTML::Template;
use DBI;
use DBD::Pg qw(:pg_types);
use Apache2::Request;
-use ND::Web::Include;
+use Apache2::Response;
+use Apache2::RequestUtil;
use ND::DB;
-use ND::Include;
-use Tie::File;
-use Fcntl 'O_RDONLY';
+use NDWeb::Page;
use strict;
use warnings;
$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);
+chdir '/var/www/ndawn';
sub handler {
- local $ND::r = shift;
- local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
+ my $r = shift;
+ my $req = Apache2::Request->new($r, POST_MAX => "1M");
local $ND::DBH = ND::DB::DB();
- local $ND::USER;
local $ND::UID;
- local $ND::PLANET;
- local $ND::TEMPLATE;
- local $ND::BODY;
- local $ND::TICK;
- local %ND::GROUPS;
local $ND::ERROR;
- local $ND::USETEMPLATE = 1;
- local $ND::PAGE = $ND::req->param('page');
+ my $page = $req->param('page');
+ $r->no_cache;
if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){
- $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl';
+ $page = $1 unless $1 eq 'index' and $3 eq 'pl';
}
- $ND::PAGE = 'main' unless (defined $ND::PAGE and exists $PAGES{$ND::PAGE});
+ $page = NDWeb::Page->new(PAGE => $page, DBH => $ND::DBH, URI => $ENV{REQUEST_URI}, USER_AGENT => $ENV{HTTP_USER_AGENT}, HTTP_ACCEPT => $ENV{HTTP_ACCEPT}, R => $r);
+ $page->render;
- $PAGES{$ND::PAGE}->{parse}->($ENV{REQUEST_URI});
+ $ND::DBH->rollback unless $ND::DBH->{AutoCommit};
+ $ND::DBH->disconnect;
- if ($ND::USETEMPLATE){
- page($ND::DBH,$ND::PAGE);
- }else{
- $PAGES{$ND::PAGE}->{render}->($ND::DBH,$ENV{REQUEST_URI});
- }
-
- return Apache2::Const::OK;
-}
-
-sub page {
- 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,$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 = ?');
- $query->execute($UID);
-
- our $ATTACKER = 0;
- undef our %GROUPS;
- while (my ($name,$attack,$gid) = $query->fetchrow()){
- $GROUPS{$name} = $gid;
- $ATTACKER = 1 if $attack;
- }
-
- our $XML = 0;
- 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/$ND::PAGE.xml.tmpl", cache => 1);
- }else{
- $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
- $ND::BODY->param(PAGE => $ND::PAGE);
- }
-
- $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());
- $TEMPLATE->param(isDC => isDC());
- $TEMPLATE->param(isBC => isBC());
- $TEMPLATE->param(isIntel => isBC());
- $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET)));
- if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){
- $ND::TEMPLATE->param(Targets => listTargets());
+ if ($page->{RETURN}){
+ if($page->{RETURN} eq 'REDIRECT'){
+ $r->headers_out->set(Location => $page->{REDIR_LOCATION});
+ $r->status(Apache2::Const::REDIRECT);
+ $r->rflush;
}
- $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);
- $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;
- $USER = undef;
- $PLANET = undef;
- $TEMPLATE = undef;
- $TICK = undef;
- undef %GROUPS;
- $ND::BODY = undef;
+ return Apache2::Const::OK;
}
1;