]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Be more paranoid with param in list context
[ndwebbie.git] / lib / NDWeb / Controller / Root.pm
1 package NDWeb::Controller::Root;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use ND::Include;
8 use Geo::IP;
9
10
11 #
12 # Sets the actions in this controller to be registered with no prefix
13 # so they function identically to actions created in MyApp.pm
14 #
15 __PACKAGE__->config->{namespace} = '';
16
17 =head1 NAME
18
19 NDWeb::Controller::Root - Root Controller for NDWeb
20
21 =head1 DESCRIPTION
22
23 [enter your description here]
24
25 =head1 METHODS
26
27 =cut
28
29 =head2 default
30
31 =cut
32
33 sub index : Local Path Args(0) {
34         my ( $self, $c ) = @_;
35
36         $c->visit('/wiki/main');
37 }
38
39 sub default : Path {
40         my ( $self, $c ) = @_;
41         $c->stash(template => 'default.tt2');
42         $c->response->status(410);
43 }
44
45 sub login : Local {
46         my ($self, $c) = @_;
47
48         if ($c->login){
49                 my $gi = Geo::IP->new("/usr/share/GeoIP/GeoIP.dat");
50                 my $country = $gi->country_code_by_addr($c->req->address) || '??';
51
52                 my $remember = 0;
53                 if ($c->req->param('remember')){
54                         $c->session_time_to_live( 604800 ); # expire in one week.
55                         $remember = 1;
56                 }
57                 my $log = $c->model->prepare(q{INSERT INTO session_log
58                         (uid,time,ip,country,session,remember)
59                         VALUES ($1,NOW(),$2,$3,$4,$5)
60                 });
61                 $log->execute($c->user->id,$c->req->address
62                         ,$country,$c->sessionid,$remember);
63
64                 $c->forward('redirect');
65                 return;
66         } elsif ($c->req->method eq 'POST'){
67                 $c->res->status(400);
68         }
69 }
70
71 sub logout : Local {
72         my ($self, $c) = @_;
73         $c->logout;
74         $c->delete_session("logout");
75         $c->res->redirect($c->uri_for('index'));
76 }
77
78 my %clickatellstatus = (
79         "001", "Message unknown. The delivering network did not recognise the message type or content.",
80         "002", "Message queued. The message could not be delivered and has been queued for attempted redelivery.",
81         "003", "Delivered. Delivered to the network or gateway (delivered to the recipient).",
82         "004", "Received by recipient. Confirmation of receipt on the handset of the recipient.",
83         "005", "Error with message. There was an error with the message, probably caused by the content of the message itself.",
84         "006", "User cancelled message delivery. Client cancelled the message by setting the validity period, or the message was terminated by an internal mechanism.",
85         "007", "Error delivering message An error occurred delivering the message to the handset.",
86         "008", " OK. Message received by gateway.",
87         "009", "Routing error. The routing gateway or network has had an error routing the message.",
88         "010", "Message expired. Message has expired at the network due to the handset being off, or out of reach.",
89         "011", "Message queued for later delivery. Message has been queued at the Clickatell gateway for delivery at a later time (delayed delivery).",
90         "012", "Out of credit. The message cannot be delivered due to a lack of funds in your account. Please re-purchase credits."
91 );
92
93
94 sub smsconfirm : Local {
95         my ($self, $c) = @_;
96         my $dbh = $c->model;
97
98         $c->stash(template => 'default.tt2');
99
100         return unless $c->req->param('apiMsgId');
101
102         my $sms = $dbh->prepare(q{
103 UPDATE sms SET status = $2, cost = $3
104         ,time = TIMESTAMP WITH TIME ZONE 'epoch' + $4 * INTERVAL '1 second'
105 WHERE msgid = $1
106                 });
107
108         my $apiMsgId = $c->req->param('apiMsgId');
109         my $status = $c->req->param('status');
110         my $charge = $c->req->param('charge');
111         my $timestamp = $c->req->param('timestamp');
112         $sms->execute($apiMsgId
113                 ,$clickatellstatus{$status}
114                 ,$charge
115                 ,$timestamp);
116
117 }
118
119
120 sub begin : Private {
121         my ($self, $c) = @_;
122
123          $c->res->header( 'Cache-Control' =>
124                 'no-store, no-cache, must-revalidate,'.
125                 'post-check=0, pre-check=0, max-age=0'
126         );
127         $c->res->header( 'Pragma' => 'no-cache' );
128         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
129 }
130
131 sub listTargets : Private {
132         my ($self, $c) = @_;
133
134         my $dbh = $c ->model;
135
136         my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick,
137                 released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
138 FROM raid_claims c
139         JOIN raid_targets t ON c.target = t.id
140         JOIN raids r ON t.raid = r.id
141         JOIN current_planet_stats p USING (pid)
142 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
143 ORDER BY r.tick+c.wave,x,y,z});
144         $query->execute($c->user->id) or die $dbh->errstr;
145         my @targets;
146         while (my $target = $query->fetchrow_hashref){
147                 push @targets, $target;
148         }
149
150         $c->stash(claimedtargets => \@targets);
151 }
152
153 sub listAlliances : Private {
154         my ($self, $c) = @_;
155         my @alliances;
156         push @alliances,{aid => '', alliance => ''};
157         my $query = $c->model->prepare(q{SELECT aid,alliance FROM alliances ORDER BY LOWER(alliance)});
158         $query->execute;
159         while (my $ally = $query->fetchrow_hashref){
160                 push @alliances,$ally;
161         }
162         $c->stash(alliances => \@alliances);
163 }
164
165 sub sslurl {
166         return $_[0];
167 }
168
169 sub auto : Private {
170         my ($self, $c) = @_;
171         my $dbh = $c ->model;
172
173         $c->stash(dbh => $dbh);
174
175         $c->stash(sslurl => \&sslurl);
176
177         $dbh->do(q{SET timezone = 'GMT'});
178
179         $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
180         $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
181         $c->stash->{game}->{tick} = $c->stash->{TICK};
182
183         if ($c->user_exists){
184                 $c->stash(UID => $c->user->id);
185         }else{
186                 $c->stash(UID => -4);
187         }
188 }
189
190 sub redirect : Private {
191         my ($self, $c) = @_;
192         $c->res->redirect($c->uri_for('/'.$c->session->{referrer}));
193 }
194
195 sub access_denied : Private {
196         my ($self, $c, $action) = @_;
197
198         $c->stash->{template} = 'access_denied.tt2';
199         $c->res->status(403);
200
201 }
202
203 =head2 end
204
205 Attempt to render a view, if needed.
206
207 =cut 
208
209 sub end : ActionClass('RenderView') {
210         my ($self, $c) = @_;
211
212         if ($c->res->status >= 300 && $c->res->status <= 400 ){
213                 return;
214         }
215
216         my $dbh = $c ->model;
217
218         if (scalar @{ $c->error } ){
219                 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
220                         $c->stash->{template} = 'access_denied.tt2';
221                         $c->res->status(403);
222                         $c->clear_errors;
223                 }elsif ($c->error->[0] =~ m/Missing roles: /){
224                         $c->stash->{template} = 'access_denied.tt2';
225                         $c->res->status(403);
226                         $c->clear_errors;
227                 }elsif ($c->error->[0] =~ m/No logged in user, and none supplied as argument/){
228                         $c->stash->{template} = 'access_denied.tt2';
229                         $c->res->status(403);
230                         $c->clear_errors;
231                 }
232         }
233
234         if ($c->user_exists){
235                 my $fleetupdate = 0;
236                 if ($c->check_user_roles(qw/member_menu/)){
237                         $fleetupdate = $dbh->selectrow_array(q{
238 SELECT tick FROM fleets WHERE pid = ? AND tick > tick() - 24
239 AND mission = 'Full fleet' AND name IN ('Main','Advanced Unit');
240                                 },undef,$c->user->planet);
241                         $fleetupdate = 0 unless defined $fleetupdate;
242                 }
243
244                 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
245                         ,undef,$c->user->id);
246
247                 $c->stash(user => {
248                         id => $c->user->id,
249                         name => $c->user->username,
250                         css => $c->user->css,
251                         newposts => $newposts,
252                         unreadposts => $unread
253                 });
254                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/);
255                 $c->forward('listTargets');
256         }
257         my $birthdays = $dbh->prepare(q{SELECT username
258                         ,date_part('year',age(birthday)) AS age
259                         FROM users WHERE birthday IS NOT NULL
260                                 AND mmdd(birthday) = mmdd(CURRENT_DATE)
261                 });
262         $birthdays->execute;
263         $c->stash(birthdays => $birthdays->fetchall_arrayref({}));
264
265         if ($c->res->status == 200 || $c->req->method eq 'GET'){
266                 $c->session->{referrer} = $c->req->path;
267         }
268 }
269
270 =head1 AUTHOR
271
272 Michael Andreen (harv@ruin.nu)
273
274 =head1 LICENSE
275
276 GPL 2, or later.
277
278 =cut
279
280 1;