]> ruin.nu Git - ndwebbie.git/blob - ND.pm
restructing to modules
[ndwebbie.git] / ND.pm
1 #!/usr/bin/perl -w -T
2 #**************************************************************************
3 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
4 #                                                                         *
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.                                   *
9 #                                                                         *
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.                          *
14 #                                                                         *
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 #**************************************************************************/
20
21 package ND;
22 use CGI qw/:standard/;
23 use HTML::Template;
24 use DBI;
25 use DBD::Pg qw(:pg_types);
26 use Apache2::Request;
27 use ND::Include;
28 use ND::DB;
29 use strict;
30 use warnings FATAL => 'all';
31
32 sub handler {
33         local $ND::r = shift;
34         local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
35         local $ND::DBH;
36         local $ND::USER;
37         local $ND::UID;
38         local $ND::PLANET;
39         local $ND::TEMPLATE;
40         local $ND::BODY;
41         local $ND::TICK;
42         local %ND::GROUPS;
43         local $ND::PAGE = $ND::req->param('page');
44
45         if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){
46                 $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl';
47         }
48         page();
49         return Apache2::Const::OK;
50 }
51
52 sub page {
53         our $DBH = ND::DB::DB();
54         our $USER = $ENV{'REMOTE_USER'};
55         my $error;# = $ND::r->param('page');
56
57         chdir '/var/www/ndawn/code';
58
59         our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
60
61         our ($UID,$PLANET) = $DBH->selectrow_array('SELECT uid,planet FROM users WHERE username = ?'
62                 ,undef,$ENV{'REMOTE_USER'});
63
64         our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
65
66
67         my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
68         $query->execute($UID);
69
70         our $ATTACKER = 0;
71         undef our %GROUPS;
72         while (my ($name,$attack,$gid) = $query->fetchrow()){
73                 $GROUPS{$name} = $gid;
74                 $ATTACKER = 1 if $attack;
75         }
76
77
78         our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
79
80         my $page = 'main';
81         if ($ND::PAGE =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users|alliances|memberIntel|resources|planetNaps)$/){
82                 $page = $1;
83         }
84
85         our $XML = 0;
86         $XML = 1 if param('xml') and $page =~ /^(raids)$/;
87
88         our $AJAX = 1;
89
90         my $type = 'text/html';
91         if ($XML){
92                 $type = 'text/xml';
93                 $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
94                 $ND::BODY = HTML::Template->new(filename => "templates/${page}.xml.tmpl", cache => 1);
95         }else{
96                 $ND::BODY = HTML::Template->new(filename => "templates/${page}.tmpl", global_vars => 1, cache => 1);
97                 $ND::BODY->param(PAGE => $page);
98         }
99
100
101         unless (my $return = do "${page}.pl"){
102                 $error .= "<p><b>couldn't parse $page: $@</b></p>" if $@;
103                 $error .= "<p><b>couldn't do $page: $!</b></p>"    unless defined $return;
104                 $error .= "<p><b>couldn't run $page</b></p>"       unless $return;
105         }
106
107         unless ($XML){
108                 my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID);
109
110
111                 $TEMPLATE->param(Tick => $TICK);
112                 $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
113                 $TEMPLATE->param(isHC => isHC());
114                 $TEMPLATE->param(isDC => isDC());
115                 $TEMPLATE->param(isBC => isBC());
116                 $TEMPLATE->param(isIntel => isBC());
117                 $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET)));
118                 if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){
119                         $ND::TEMPLATE->param(Targets => listTargets());
120                 }
121                 $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
122                 $TEMPLATE->param(Error => $error);
123
124         }
125         $ND::TEMPLATE->param(BODY => $ND::BODY->output);
126         my $output = $TEMPLATE->output;
127         print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
128         print $output;
129
130
131         $DBH->disconnect;
132         $DBH = undef;
133         $UID = undef;
134         $USER = undef;
135         $PLANET = undef;
136         $TEMPLATE = undef;
137         $TICK = undef;
138         undef %GROUPS;
139         $ND::BODY = undef;
140 }
141
142 1;