]> ruin.nu Git - ndwebbie.git/blob - ND.pm
use Tie::File instead of hardcoded regex for valid pages
[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 Tie::File;
30 use Fcntl 'O_RDONLY';
31 use strict;
32 use warnings FATAL => 'all';
33
34 tie our @PAGES, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY or die $!;
35
36 sub handler {
37         local $ND::r = shift;
38         local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
39         local $ND::DBH;
40         local $ND::USER;
41         local $ND::UID;
42         local $ND::PLANET;
43         local $ND::TEMPLATE;
44         local $ND::BODY;
45         local $ND::TICK;
46         local %ND::GROUPS;
47         local $ND::PAGE = $ND::req->param('page');
48
49         if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){
50                 $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl';
51         }
52         page();
53         return Apache2::Const::OK;
54 }
55
56 sub page {
57         our $DBH = ND::DB::DB();
58         our $USER = $ENV{'REMOTE_USER'};
59         my $error;# = $ND::r->param('page');
60
61         chdir '/var/www/ndawn/code';
62
63         our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
64
65         our ($UID,$PLANET) = $DBH->selectrow_array('SELECT uid,planet FROM users WHERE username = ?'
66                 ,undef,$ENV{'REMOTE_USER'});
67
68         our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
69
70
71         my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
72         $query->execute($UID);
73
74         our $ATTACKER = 0;
75         undef our %GROUPS;
76         while (my ($name,$attack,$gid) = $query->fetchrow()){
77                 $GROUPS{$name} = $gid;
78                 $ATTACKER = 1 if $attack;
79         }
80
81
82         our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
83
84         $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @PAGES;
85
86         our $XML = 0;
87         $XML = 1 if param('xml') and $ND::PAGE =~ /^(raids)$/;
88
89         our $AJAX = 1;
90
91         my $type = 'text/html';
92         if ($XML){
93                 $type = 'text/xml';
94                 $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
95                 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.xml.tmpl", cache => 1);
96         }else{
97                 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
98                 $ND::BODY->param(PAGE => $ND::PAGE);
99         }
100
101
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;
105                 $error .= "<p><b>couldn't run $ND::page</b></p>"       unless $return;
106         }
107
108         unless ($XML){
109                 my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID);
110
111
112                 $TEMPLATE->param(Tick => $TICK);
113                 $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
114                 $TEMPLATE->param(isHC => isHC());
115                 $TEMPLATE->param(isDC => isDC());
116                 $TEMPLATE->param(isBC => isBC());
117                 $TEMPLATE->param(isIntel => isBC());
118                 $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET)));
119                 if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){
120                         $ND::TEMPLATE->param(Targets => listTargets());
121                 }
122                 $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
123                 $TEMPLATE->param(Error => $error);
124
125         }
126         $ND::TEMPLATE->param(BODY => $ND::BODY->output);
127         my $output = $TEMPLATE->output;
128         print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
129         print $output;
130
131
132         $DBH->disconnect;
133         $DBH = undef;
134         $UID = undef;
135         $USER = undef;
136         $PLANET = undef;
137         $TEMPLATE = undef;
138         $TICK = undef;
139         undef %GROUPS;
140         $ND::BODY = undef;
141 }
142
143 1;