]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Use proper session instead of flash
[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->res->redirect($c->uri_for('/wiki'));
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(GEOIP_STANDARD);
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 sub begin : Private {
79         my ($self, $c) = @_;
80
81          $c->res->header( 'Cache-Control' =>
82                 'no-store, no-cache, must-revalidate,'.
83                 'post-check=0, pre-check=0, max-age=0'
84         );
85         $c->res->header( 'Pragma' => 'no-cache' );
86         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
87 }
88
89 sub listTargets : Private {
90         my ($self, $c) = @_;
91
92         my $dbh = $c ->model;
93
94         my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, 
95                 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
96 FROM raid_claims c
97         JOIN raid_targets t ON c.target = t.id
98         JOIN raids r ON t.raid = r.id
99         JOIN current_planet_stats p ON t.planet = p.id
100 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
101 ORDER BY r.tick+c.wave,x,y,z});
102         $query->execute($c->user->id) or die $dbh->errstr;
103         my @targets;
104         while (my $target = $query->fetchrow_hashref){
105                 push @targets, $target;
106         }
107
108         $c->stash(claimedtargets => \@targets);
109 }
110
111 sub listAlliances : Private {
112         my ($self, $c) = @_;
113         my @alliances;
114         push @alliances,{id => -1, name => ''};
115         my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
116         $query->execute;
117         while (my $ally = $query->fetchrow_hashref){
118                 push @alliances,$ally;
119         }
120         $c->stash(alliances => \@alliances);
121 }
122
123 sub sslurl {
124         return $_[0];
125 }
126
127 sub auto : Private {
128         my ($self, $c) = @_;
129         my $dbh = $c ->model;
130
131         $c->stash(dbh => $dbh);
132
133         $c->stash(sslurl => \&sslurl);
134
135         $dbh->do(q{SET timezone = 'GMT'});
136
137         $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
138         $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
139         $c->stash->{game}->{tick} = $c->stash->{TICK};
140
141         if ($c->user_exists){
142                 $c->stash(UID => $c->user->id);
143         }else{
144                 $c->stash(UID => -4);
145         }
146 }
147
148 sub redirect : Private {
149         my ($self, $c) = @_;
150         $c->res->redirect($c->uri_for('/'.$c->session->{referrer}));
151 }
152
153 sub access_denied : Private {
154         my ($self, $c, $action) = @_;
155
156         $c->stash->{template} = 'access_denied.tt2';
157         $c->res->status(403);
158
159 }
160
161 =head2 end
162
163 Attempt to render a view, if needed.
164
165 =cut 
166
167 sub end : ActionClass('RenderView') {
168         my ($self, $c) = @_;
169
170         if ($c->res->status >= 300 && $c->res->status <= 400 ){
171                 return;
172         }
173
174         my $dbh = $c ->model;
175
176         if (scalar @{ $c->error } ){
177                 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
178                         $c->stash->{template} = 'access_denied.tt2';
179                         $c->res->status(403);
180                         $c->clear_errors;
181                 }elsif ($c->error->[0] =~ m/Missing roles: /){
182                         $c->stash->{template} = 'access_denied.tt2';
183                         $c->res->status(403);
184                         $c->clear_errors;
185                 }
186         }
187
188         if ($c->user_exists){
189                 my $fleetupdate = 0;
190                 if ($c->check_user_roles(qw/member_menu/)){
191                         $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
192                                 AND mission = 'Full fleet' AND tick > tick() - 24
193                                 },undef,$c->user->planet);
194                         $fleetupdate = 0 unless defined $fleetupdate;
195                 }
196
197                 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
198                         ,undef,$c->user->id);
199
200                 $c->stash(user => {
201                         id => $c->user->id,
202                         name => $c->user->username,
203                         css => $c->user->css,
204                         newposts => $newposts,
205                         unreadposts => $unread
206                 });
207                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
208                         && (!$c->check_user_roles(qw/member_menu/)
209                                 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
210                                         || $c->check_user_roles(qw/no_fleet_update/)))),
211                 $c->forward('listTargets');
212         }
213         my $birthdays = $dbh->prepare(q{SELECT username
214                         ,date_part('year',age(birthday)) AS age
215                         FROM users WHERE birthday IS NOT NULL
216                                 AND mmdd(birthday) = mmdd(CURRENT_DATE)
217                 });
218         $birthdays->execute;
219         $c->stash(birthdays => $birthdays->fetchall_arrayref({}));
220
221         if ($c->res->status == 200 || $c->req->method eq 'GET'){
222                 $c->session->{referrer} = $c->req->path;
223         }
224 }
225
226 =head1 AUTHOR
227
228 Michael Andreen (harv@ruin.nu)
229
230 =head1 LICENSE
231
232 GPL 2, or later.
233
234 =cut
235
236 1;