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 ND::DB;
-use ND::Include;
-use Tie::File;
-use Fcntl 'O_RDONLY';
+use ND::Web::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);
sub handler {
local $ND::r = shift;
local $ND::req = Apache2::Request->new($ND::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 = $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 = '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});
+ $page = $1 unless $1 eq 'index' and $3 eq 'pl';
}
+ $page = ND::Web::Page->new(PAGE => $page, DBH => $ND::DBH, URI => $ENV{REQUEST_URI});
+ $page->render;
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());
- }
- $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;
-}
-
1;
--- /dev/null
+#**************************************************************************
+# Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
+# *
+# This program is free software; you can redistribute it and/or modify *
+# it under the terms of the GNU General Public License as published by *
+# the Free Software Foundation; either version 2 of the License, or *
+# (at your option) any later version. *
+# *
+# This program is distributed in the hope that it will be useful, *
+# but WITHOUT ANY WARRANTY; without even the implied warranty of *
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+# GNU General Public License for more details. *
+# *
+# You should have received a copy of the GNU General Public License *
+# along with this program; if not, write to the *
+# Free Software Foundation, Inc., *
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+#**************************************************************************/
+
+package ND::Web::Image;
+use strict;
+use warnings;
+use CGI qw/:standard/;
+
+our @ISA = qw/ND::Web::Page/;
+
+
+sub render {
+ my $self = shift;
+
+ my $img;
+ eval {
+ $img = $self->render_body;
+ };
+ if (defined $img){
+ print header(-type=> 'image/png', -Content_Length => length $img);
+ binmode STDOUT;
+ print $img;
+ }else{
+ print header;
+ print $@;
+ }
+};
+
+1;
our @ISA = qw/Exporter/;
-our @EXPORT = qw/isMember isHC isDC isBC isOfficer isScanner isIntel isTech parseMarkup min max listTargets
+our @EXPORT = qw/parseMarkup min max listTargets
alliances intelquery generateClaimXml/;
-sub isMember {
- return exists $ND::GROUPS{Members} || isTech();
-}
-
-sub isHC {
- return exists $ND::GROUPS{HC} || isTech();
-}
-
-sub isDC {
- return exists $ND::GROUPS{DC} || isTech();
-}
-
-sub isBC {
- return exists $ND::GROUPS{BC} || isTech();
-}
-
-sub isOfficer {
- return exists $ND::GROUPS{Officers} || isTech();
-}
-
-sub isScanner {
- return exists $ND::GROUPS{Scanners} || isTech();
-}
-
-sub isIntel {
- return exists $ND::GROUPS{Intel} || isTech();
-}
-
-sub isTech {
- return exists $ND::GROUPS{Tech};
-}
-
sub parseMarkup ($) {
my ($text) = @_;
--- /dev/null
+#**************************************************************************
+# Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
+# *
+# This program is free software; you can redistribute it and/or modify *
+# it under the terms of the GNU General Public License as published by *
+# the Free Software Foundation; either version 2 of the License, or *
+# (at your option) any later version. *
+# *
+# This program is distributed in the hope that it will be useful, *
+# but WITHOUT ANY WARRANTY; without even the implied warranty of *
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+# GNU General Public License for more details. *
+# *
+# You should have received a copy of the GNU General Public License *
+# along with this program; if not, write to the *
+# Free Software Foundation, Inc., *
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+#**************************************************************************/
+package ND::Web::Page;
+use strict;
+use warnings;
+use CGI qw/:standard/;
+
+our %PAGES = {};
+
+sub new {
+ my $invocant = shift;
+ my $class = ref($invocant) || $invocant;
+ my $self = {@_};
+ $self->{PAGE} = 'main' unless (defined $self->{PAGE} and exists $PAGES{$self->{PAGE}});
+ $class = $PAGES{$self->{PAGE}};
+ bless $self, $class;
+ $self->parse;
+ $self->initiate;
+ return $self;
+}
+
+sub initiate : method {
+ my $self = shift;
+ my $DBH = $self->{DBH};
+
+ $DBH->do(q{SET timezone = 'GMT'});
+
+ ($self->{UID},$self->{PLANET},$self->{USER}) = $DBH->selectrow_array('SELECT uid,planet,username FROM users WHERE username ILIKE ?'
+ ,undef,$ENV{'REMOTE_USER'});
+ $ND::UID = $self->{UID};
+
+ ($self->{TICK}) = $DBH->selectrow_array('SELECT tick()',undef);
+ $self->{TICK} = 0 unless defined $self->{TICK};
+
+
+ my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
+ $query->execute($self->{UID});
+
+ while (my ($name,$attack,$gid) = $query->fetchrow()){
+ $self->{GROUPS}{$name} = $gid;
+ $self->{ATTACKER} = 1 if $attack;
+ }
+
+
+}
+
+sub parse : method {
+}
+
+sub render_body : method {
+ return "";
+}
+
+sub render : method {
+ my $self = shift;
+
+ print header;
+ print $self->render_body;
+}
+
+sub isInGroup ($) : method {
+ my $self = shift;
+ my $group = shift;
+ return exists $self->{GROUPS}{$group} || exists $self->{GROUPS}{Tech};
+}
+
+sub isMember () : method {
+ my $self = shift;
+ $self->isInGroup('Members');
+}
+
+sub isHC () : method {
+ my $self = shift;
+ $self->isInGroup('HC');
+}
+
+sub isDC () : method {
+ $_[0]->isInGroup('DC');
+}
+
+sub isBC () : method {
+ $_[0]->isInGroup('BC');
+}
+
+sub isOfficer () : method {
+ $_[0]->isInGroup('Officers');
+}
+
+sub isScanner () : method {
+ $_[0]->isInGroup('Scanners');
+}
+
+sub isIntel () : method {
+ $_[0]->isInGroup('Intel');
+}
+
+sub isTech () : method {
+ $_[0]->isInGroup('Tech');
+}
+
+
+
+1;
use ND::Web::Forum;
use ND::Web::Include;
-$ND::PAGES{addintel} = {parse => \&parse, process => \&process, render=> \&render};
+our @ISA = qw/ND::Web::XMLPage/;
-sub parse {
-}
+$ND::Web::Page::PAGES{addintel} = 'ND::Web::Pages::AddIntel';
-sub process {
+sub render_body {
+ my $self = shift;
+ my ($BODY) = @_;
-}
+ my $DBH = $self->{DBH};
-sub render {
- my ($DBH,$BODY) = @_;
- $ND::TEMPLATE->param(TITLE => 'Add Intel and Scans');
+ $self->{TITLE} = 'Add Intel and Scans';
my $error;
- return $ND::NOACCESS unless isMember();
+ return $self->noAccess unless $self->isMember;
if (defined param('cmd')){
if (param('cmd') eq 'submit' || param('cmd') eq 'submit_message'){
push @scans,\%scan;
}
$BODY->param(Scans => \@scans);
- my $tick = $ND::TICK;
+ my $tick = $self->{TICK};
$tick = param('tick') if $tick =~ /^(\d+)$/;
my $addintel = $DBH->prepare(qq{SELECT add_intel(?,?,?,?,?,?,?,?,?,?,?)});
while ($intel =~ m/(\d+):(\d+):(\d+)\*?\s+(\d+):(\d+):(\d+)\*?\s+.+(?:Ter|Cat|Xan|Zik)?\s+(\d+)\s+(Attack|Defend)\s+(\d+)/g){
}
}
}
- $BODY->param(Tick => $ND::TICK);
+ $BODY->param(Tick => $self->{TICK});
$BODY->param(Error => $error);
return $BODY;
}
use ND::Include;
use ND::Web::Graph;
-$ND::PAGES{graph} = {parse => \&parse, process => \&process, render=> \&render};
+our @ISA = qw/ND::Web::Image/;
-sub parse {
- my ($uri) = @_;
- $ND::USETEMPLATE = 0;
-}
+$ND::Web::Page::PAGES{graph} = 'ND::Web::Pages::Graph';
-sub process {
-
-}
-
-sub render {
- my ($DBH,$uri) = @_;
+sub render_body {
+ my $self = shift;
+ my $DBH = $self->{DBH};
my $type;
my ($x,$y,$z);
- if ($uri =~ m{^/\w+/(stats|ranks)/(.*)}){
+ if ($self->{URI} =~ m{^/\w+/(stats|ranks)/(.*)}){
$type = $1;
if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){
$x = $1;
die 'no image' unless defined $img;
- print header(-type=> 'image/png', -Content_Length => length $img);
- binmode STDOUT;
- print $img;
-}
+ return $img;
+};
1;
use ND::Include;
use ND::Web::Include;
-$ND::PAGES{main} = {parse => \&parse, process => \&process, render=> \&render};
+our @ISA = qw/ND::Web::XMLPage/;
-sub parse {
-}
-
-sub process {
-
-}
+$ND::Web::Page::PAGES{main} = 'ND::Web::Pages::Main';
-sub render {
- my ($DBH,$BODY) = @_;
- $ND::TEMPLATE->param(TITLE => 'Main Page');
+sub render_body {
+ my $self = shift;
+ my ($BODY) = @_;
+ $self->{TITLE} = 'Main Page';
+ my $DBH = $self->{DBH};
my $error;
my ($id) = $DBH->selectrow_array($fleet,undef,$ND::UID);
unless ($id){
my $insert = $DBH->prepare(q{INSERT INTO fleets (uid,target,mission,landing_tick,fleet,eta,back) VALUES (?,?,'Full fleet',0,0,0,0)});
- $insert->execute($ND::UID,$ND::PLANET);
+ $insert->execute($ND::UID,$self->{PLANET});
($id) = $DBH->selectrow_array($fleet,undef,$ND::UID);
}
my $delete = $DBH->prepare("DELETE FROM fleet_ships WHERE fleet = ?");
if (param('sms')){ my $query = $DBH->prepare('UPDATE users SET sms = ? WHERE uid = ?');
$query->execute(escapeHTML(param('sms')),$ND::UID);
}
- if (isMember() && !$ND::PLANET && defined param('planet') && (param('planet') =~ m/(\d+)(?: |:)(\d+)(?: |:)(\d+)/)){
+ if ($self->isMember() && !$self->{PLANET} && defined param('planet') && (param('planet') =~ m/(\d+)(?: |:)(\d+)(?: |:)(\d+)/)){
my $query = $DBH->prepare(q{
UPDATE users SET planet =
(SELECT id from current_planet_stats where x = ? AND y = ? AND z = ?)
my ($motd) = $DBH->selectrow_array("SELECT value FROM misc WHERE id='MOTD'");
$BODY->param(MOTD => parseMarkup($motd));
- $BODY->param(Username => $ND::USER);
- $BODY->param(isMember => isMember());
- $BODY->param(isHC => isHC());
- my @groups = map {name => $_}, sort keys %ND::GROUPS;
+ $BODY->param(Username => $self->{USER});
+ $BODY->param(isMember => $self->isMember());
+ $BODY->param(isHC => $self->isHC());
+ my @groups = map {name => $_}, sort keys %{$self->{GROUPS}};
$BODY->param(Groups => \@groups);
my ($planet,$defense_points,$attack_points,$scan_points,$humor_points,$total_points,$sms,$rank) = $DBH->selectrow_array($query,undef,$ND::UID);
- $ND::PLANET = $planet unless $ND::PLANET;
+ $self->{PLANET} = $planet unless $self->{PLANET};
$BODY->param(NDRank => $rank);
$BODY->param(DefensePoints => $defense_points);
ORDER BY f.fleet
});
- $query->execute($ND::UID,$ND::TICK) or $error .= '<p>'.$DBH->errstr.'</p>';
+ $query->execute($ND::UID,$self->{TICK}) or $error .= '<p>'.$DBH->errstr.'</p>';
my @fleets;
my $i = 0;
while (my $fleet = $query->fetchrow_hashref){
--- /dev/null
+#**************************************************************************
+# Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
+# *
+# This program is free software; you can redistribute it and/or modify *
+# it under the terms of the GNU General Public License as published by *
+# the Free Software Foundation; either version 2 of the License, or *
+# (at your option) any later version. *
+# *
+# This program is distributed in the hope that it will be useful, *
+# but WITHOUT ANY WARRANTY; without even the implied warranty of *
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+# GNU General Public License for more details. *
+# *
+# You should have received a copy of the GNU General Public License *
+# along with this program; if not, write to the *
+# Free Software Foundation, Inc., *
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+#**************************************************************************/
+
+package ND::Web::XMLPage;
+use strict;
+use warnings;
+use CGI qw/:standard/;
+use HTML::Template;
+
+use ND::Include;
+use ND::Web::Page;
+use ND::Web::Include;
+
+our @ISA = qw/ND::Web::Page/;
+
+sub noAccess {
+ HTML::Template->new(filename => 'templates/NoAccess.tmpl', global_vars => 1, cache => 1);
+};
+
+sub process : method {
+}
+
+sub render : method {
+ my $self = shift;
+ my $DBH = $self->{DBH};
+
+
+ chdir '/var/www/ndawn/code';
+
+ my $template = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
+
+ my $TICK = $self->{TICK};
+ my $ATTACKER = $self->{ATTACKER};
+
+ $self->{XML} = 0;
+ $self->{AJAX} = 1;
+
+ $self->process;
+
+ my $type = 'text/html';
+ my $body;
+ if ($self->{XML}){
+ $type = 'text/xml';
+ $template = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
+ $body = HTML::Template->new(filename => "templates/$self->{PAGE}.xml.tmpl", cache => 1);
+ }else{
+ $body = HTML::Template->new(filename => "templates/$self->{PAGE}.tmpl", global_vars => 1, cache => 1);
+ $body->param(PAGE => $self->{PAGE});
+ }
+
+ $body = $self->render_body($body);
+
+ unless ($self->{XML}){
+ my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$self->{UID});
+
+ $fleetupdate = 0 unless defined $fleetupdate;
+
+ my ($last_forum_visit) = $DBH->selectrow_array(q{SELECT last_forum_visit FROM users WHERE uid = $1}
+ ,undef,$self->{UID}) or $ND::ERROR .= p($DBH->errstr);
+ my ($unread,$newposts) = $DBH->selectrow_array(unread_query(),undef,$self->{UID},$last_forum_visit)
+ or $ND::ERROR .= p($DBH->errstr);
+
+ $template->param(UnreadPosts => $unread);
+ $template->param(NewPosts => $newposts);
+ $template->param(Tick => $TICK);
+ $template->param(isMember => (($TICK - $fleetupdate < 24) || $self->isScanner()) && $self->{PLANET} && $self->isMember);
+ $template->param(isHC => $self->isHC);
+ $template->param(isDC => $self->isDC());
+ $template->param(isBC => $self->isBC());
+ $template->param(isIntel => $self->isBC());
+ $template->param(isAttacker => $ATTACKER && (!$self->isMember() || ((($TICK - $fleetupdate < 24) || $self->isScanner()) && $self->{PLANET})));
+ if ($ATTACKER && (!$self->isMember() || ((($TICK - $fleetupdate < 24) || $self->isScanner()) && $self->{PLANET}))){
+ $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(TITLE => $self->{TITLE});
+
+ }
+ $template->param(Error => $ND::ERROR);
+ $template->param(BODY => $body->output);
+ my $output = $template->output;
+ print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
+ print $output;
+
+
+ $DBH->rollback unless $DBH->{AutoCommit};
+ $DBH->disconnect;
+
+};
+
+1;
use ND::Web::Forum;
use ND::Web::Graph;
+use ND::Web::Page;
+use ND::Web::Image;
+use ND::Web::XMLPage;
+
use ND::Web::Pages::Main;
use ND::Web::Pages::AddIntel;
-use ND::Web::Pages::Points;
-use ND::Web::Pages::LaunchConfirmation;
-use ND::Web::Pages::CovOp;
-use ND::Web::Pages::Top100;
-use ND::Web::Pages::DefRequest;
-use ND::Web::Pages::Check;
-use ND::Web::Pages::Raids;
-use ND::Web::Pages::EditRaid;
-use ND::Web::Pages::Calls;
-use ND::Web::Pages::Users;
-use ND::Web::Pages::Intel;
-use ND::Web::Pages::Alliances;
-use ND::Web::Pages::MemberIntel;
-use ND::Web::Pages::Resources;
-use ND::Web::Pages::PlanetNaps;
-use ND::Web::Pages::Motd;
-use ND::Web::Pages::Forum;
-use ND::Web::Pages::Settings;
-use ND::Web::Pages::Graph;
+#use ND::Web::Pages::Points;
+#use ND::Web::Pages::LaunchConfirmation;
+#use ND::Web::Pages::CovOp;
+#use ND::Web::Pages::Top100;
+#use ND::Web::Pages::DefRequest;
+#use ND::Web::Pages::Check;
+#use ND::Web::Pages::Raids;
+#use ND::Web::Pages::EditRaid;
+#use ND::Web::Pages::Calls;
+#use ND::Web::Pages::Users;
+#use ND::Web::Pages::Intel;
+#use ND::Web::Pages::Alliances;
+#use ND::Web::Pages::MemberIntel;
+#use ND::Web::Pages::Resources;
+#use ND::Web::Pages::PlanetNaps;
+#use ND::Web::Pages::Motd;
+#use ND::Web::Pages::Forum;
+#use ND::Web::Pages::Settings;
+#use ND::Web::Pages::Graph;