2 #**************************************************************************
3 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
5 # This program is free software; you can redistribute it and/or modify *
6 # it under the terms of the GNU General Public License as published by *
7 # the Free Software Foundation; either version 2 of the License, or *
8 # (at your option) any later version. *
10 # This program is distributed in the hope that it will be useful, *
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 # GNU General Public License for more details. *
15 # You should have received a copy of the GNU General Public License *
16 # along with this program; if not, write to the *
17 # Free Software Foundation, Inc., *
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 #**************************************************************************/
22 use CGI qw/:standard/;
25 use DBD::Pg qw(:pg_types);
32 use warnings FATAL => 'all';
34 tie our @PAGES, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY or die $!;
38 local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
47 local $ND::PAGE = $ND::req->param('page');
49 if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){
50 $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl';
52 $ND::PAGE = '' unless defined $ND::PAGE;
54 return Apache2::Const::OK;
58 our $DBH = ND::DB::DB();
61 chdir '/var/www/ndawn/code';
63 our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
65 our ($UID,$PLANET,$USER) = $DBH->selectrow_array('SELECT uid,planet,username FROM users WHERE username ILIKE ?'
66 ,undef,$ENV{'REMOTE_USER'});
68 our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
69 $TICK = 0 unless defined $TICK;
72 my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
73 $query->execute($UID);
77 while (my ($name,$attack,$gid) = $query->fetchrow()){
78 $GROUPS{$name} = $gid;
79 $ATTACKER = 1 if $attack;
83 our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
85 $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @PAGES;
88 $XML = 1 if param('xml') and $ND::PAGE =~ /^(raids)$/;
92 my $type = 'text/html';
95 $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
96 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.xml.tmpl", cache => 1);
98 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
99 $ND::BODY->param(PAGE => $ND::PAGE);
102 unless (my $return = do "$ND::PAGE.pl"){
103 $error .= "<p><b>couldn't parse $ND::PAGE: $@</b></p>" if $@;
104 $error .= "<p><b>couldn't do $ND::PAGE: $!</b></p>" unless defined $return && defined $!;
105 $error .= "<p><b>couldn't run $ND::PAGE</b></p>" unless $return;
109 my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID);
111 $fleetupdate = 0 unless defined $fleetupdate;
113 $TEMPLATE->param(Tick => $TICK);
114 $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
115 $TEMPLATE->param(isHC => isHC());
116 $TEMPLATE->param(isDC => isDC());
117 $TEMPLATE->param(isBC => isBC());
118 $TEMPLATE->param(isIntel => isBC());
119 $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET)));
120 if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){
121 $ND::TEMPLATE->param(Targets => listTargets());
123 $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
126 $TEMPLATE->param(Error => $error);
127 $ND::TEMPLATE->param(BODY => $ND::BODY->output);
128 my $output = $TEMPLATE->output;
129 print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);