]> ruin.nu Git - ndwebbie.git/blob - ND.pm
82a477ab5a60e17c064b9b829fd399d92bb22163
[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 Tie::File;
30 use Fcntl 'O_RDONLY';
31 use strict;
32 use warnings FATAL => 'all';
33
34
35 sub handler {
36         local $ND::r = shift;
37         local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
38         local $ND::DBH;
39         local $ND::USER;
40         local $ND::UID;
41         local $ND::PLANET;
42         local $ND::TEMPLATE;
43         local $ND::BODY;
44         local $ND::TICK;
45         local %ND::GROUPS;
46         local $ND::ERROR;
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         $ND::PAGE = '' unless defined $ND::PAGE;
53         page();
54         return Apache2::Const::OK;
55 }
56
57 sub page {
58         our $DBH = ND::DB::DB();
59         $DBH->do(q{SET timezone = 'GMT'});
60
61         our $ERROR = '';
62
63         chdir '/var/www/ndawn/code';
64
65         our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
66
67         our ($UID,$PLANET,$USER) = $DBH->selectrow_array('SELECT uid,planet,username FROM users WHERE username ILIKE ?'
68                 ,undef,$ENV{'REMOTE_USER'});
69
70         our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
71         $TICK = 0 unless defined $TICK;
72
73
74         my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
75         $query->execute($UID);
76
77         our $ATTACKER = 0;
78         undef our %GROUPS;
79         while (my ($name,$attack,$gid) = $query->fetchrow()){
80                 $GROUPS{$name} = $gid;
81                 $ATTACKER = 1 if $attack;
82         }
83
84         tie my @pages, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY, memory => 0 or die $!;
85         $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @pages;
86
87         our $XML = 0;
88         $XML = 1 if param('xml') and $ND::PAGE =~ /^(raids)$/;
89
90         our $AJAX = 1;
91
92         my $type = 'text/html';
93         if ($XML){
94                 $type = 'text/xml';
95                 $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
96                 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.xml.tmpl", cache => 1);
97         }else{
98                 $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
99                 $ND::BODY->param(PAGE => $ND::PAGE);
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 && defined $!;
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                 $fleetupdate = 0 unless defined $fleetupdate;
112
113                 my ($unread) = $DBH->selectrow_array(q{
114                         SELECT count(*) AS unread
115 FROM forum_boards fb NATURAL JOIN forum_threads ft 
116         JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN 
117                 (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
118 WHERE (ftv.time IS NULL OR fp.time > ftv.time) AND fbid > 0 AND
119         fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
120                         },undef,$UID) or $ERROR .= p($DBH->errstr);
121
122                 $TEMPLATE->param(UnreadPosts => $unread);
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
135         }
136         $TEMPLATE->param(Error => $ERROR);
137         $ND::TEMPLATE->param(BODY => $ND::BODY->output);
138         my $output = $TEMPLATE->output;
139         print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
140         print $output;
141
142
143         $DBH->rollback;
144         $DBH->disconnect;
145         $DBH = undef;
146         $UID = undef;
147         $USER = undef;
148         $PLANET = undef;
149         $TEMPLATE = undef;
150         $TICK = undef;
151         undef %GROUPS;
152         $ND::BODY = undef;
153 }
154
155 1;