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