]> ruin.nu Git - ndwebbie.git/blob - ND.pm
graphs
[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::Web::Include;
28 use ND::DB;
29 use ND::Include;
30 use Tie::File;
31 use Fcntl 'O_RDONLY';
32 use strict;
33 use warnings;
34
35 $SIG{__WARN__} = sub {$ND::ERROR .= p $_[0]};
36
37 chdir '/var/www/ndawn/code';
38 our %PAGES;
39 our $NOACCESS = HTML::Template->new(filename => 'templates/NoAccess.tmpl', global_vars => 1, cache => 1);
40
41 sub handler {
42         local $ND::r = shift;
43         local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
44         local $ND::DBH = ND::DB::DB();
45         local $ND::USER;
46         local $ND::UID;
47         local $ND::PLANET;
48         local $ND::TEMPLATE;
49         local $ND::BODY;
50         local $ND::TICK;
51         local %ND::GROUPS;
52         local $ND::ERROR;
53         local $ND::USETEMPLATE = 1;
54         local $ND::PAGE = $ND::req->param('page');
55
56         if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){
57                 $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl';
58         }
59         $ND::PAGE = 'main' unless (defined $ND::PAGE and exists $PAGES{$ND::PAGE});
60
61         $PAGES{$ND::PAGE}->{parse}->($ENV{REQUEST_URI});
62
63         if ($ND::USETEMPLATE){
64                 page($ND::DBH,$ND::PAGE);
65         }else{
66                 $PAGES{$ND::PAGE}->{render}->($ND::DBH,$ENV{REQUEST_URI});
67         }
68
69         return Apache2::Const::OK;
70 }
71
72 sub page {
73         my ($DBH,$PAGE) = @_;
74         $DBH->do(q{SET timezone = 'GMT'});
75
76         our $ERROR;
77
78         chdir '/var/www/ndawn/code';
79
80         our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
81
82         our ($UID,$PLANET,$USER) = $DBH->selectrow_array('SELECT uid,planet,username FROM users WHERE username ILIKE ?'
83                 ,undef,$ENV{'REMOTE_USER'});
84
85         our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
86         $TICK = 0 unless defined $TICK;
87
88
89         my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
90         $query->execute($UID);
91
92         our $ATTACKER = 0;
93         undef our %GROUPS;
94         while (my ($name,$attack,$gid) = $query->fetchrow()){
95                 $GROUPS{$name} = $gid;
96                 $ATTACKER = 1 if $attack;
97         }
98
99         our $XML = 0;
100         our $AJAX = 1;
101
102         $PAGES{$PAGE}->{process}->();
103
104         my $type = 'text/html';
105         if ($XML){
106                 $type = 'text/xml';
107                 $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
108                 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.xml.tmpl", cache => 1);
109         }else{
110                 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
111                 $ND::BODY->param(PAGE => $ND::PAGE);
112         }
113
114         $ND::BODY = $PAGES{$PAGE}->{render}->($DBH,$ND::BODY);
115
116         unless ($XML){
117                 my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID);
118
119                 $fleetupdate = 0 unless defined $fleetupdate;
120
121                 my ($last_forum_visit) = $DBH->selectrow_array(q{SELECT last_forum_visit FROM users WHERE uid = $1}
122                         ,undef,$UID) or $ERROR .= p($DBH->errstr);
123                 my ($unread,$newposts) = $DBH->selectrow_array(unread_query,undef,$UID,$last_forum_visit)
124                         or $ERROR .= p($DBH->errstr);
125                 
126                 $TEMPLATE->param(UnreadPosts => $unread);
127                 $TEMPLATE->param(NewPosts => $newposts);
128                 $TEMPLATE->param(Tick => $TICK);
129                 $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
130                 $TEMPLATE->param(isHC => isHC());
131                 $TEMPLATE->param(isDC => isDC());
132                 $TEMPLATE->param(isBC => isBC());
133                 $TEMPLATE->param(isIntel => isBC());
134                 $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET)));
135                 if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){
136                         $ND::TEMPLATE->param(Targets => listTargets());
137                 }
138                 $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
139                 my ($css) = $DBH->selectrow_array(q{SELECT css FROM users WHERE uid = $1},undef,$ND::UID);
140                 $TEMPLATE->param(CSS => $css);
141
142         }
143         $TEMPLATE->param(Error => $ERROR);
144         $ND::TEMPLATE->param(BODY => $ND::BODY->output);
145         my $output = $TEMPLATE->output;
146         print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
147         print $output;
148
149
150         $DBH->rollback;
151         $DBH->disconnect;
152         $DBH = undef;
153         $UID = undef;
154         $USER = undef;
155         $PLANET = undef;
156         $TEMPLATE = undef;
157         $TICK = undef;
158         undef %GROUPS;
159         $ND::BODY = undef;
160 }
161
162 1;