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