]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Root.pm
Bugfix, redirect to https version after login
[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->response->status(404);
42 }
43
44 sub login : Local {
45         my ($self, $c) = @_;
46
47         if ($c->login){
48                 my $gi = Geo::IP->new(GEOIP_STANDARD);
49                 my $country = $gi->country_code_by_addr($c->req->address) || '??';
50
51                 my $remember = 0;
52                 if ($c->req->param('remember')){
53                         $c->session_time_to_live( 604800 ); # expire in one week.
54                         $remember = 1;
55                 }
56                 my $log = $c->model->prepare(q{INSERT INTO session_log
57                         (uid,time,ip,country,session,remember)
58                         VALUES ($1,NOW(),$2,$3,$4,$5)
59                 });
60                 $log->execute($c->user->id,$c->req->address
61                         ,$country,$c->sessionid,$remember);
62
63                 my $ref = $c->req->referer;
64                 $ref =~ s/^http:/https:/;
65                 $c->res->redirect($ref);
66                 return;
67         }
68 }
69
70 sub logout : Local {
71         my ($self, $c) = @_;
72         $c->logout;
73         $c->delete_session("logout");
74         $c->res->redirect($c->uri_for('index'));
75 }
76
77 sub begin : Private {
78         my ($self, $c) = @_;
79
80          $c->res->header( 'Cache-Control' =>
81                 'no-store, no-cache, must-revalidate,'.
82                 'post-check=0, pre-check=0, max-age=0'
83         );
84         $c->res->header( 'Pragma' => 'no-cache' );
85         $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
86 }
87
88 sub listTargets : Private {
89         my ($self, $c) = @_;
90
91         my $dbh = $c ->model;
92
93         my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, 
94                 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
95 FROM raid_claims c
96         JOIN raid_targets t ON c.target = t.id
97         JOIN raids r ON t.raid = r.id
98         JOIN current_planet_stats p ON t.planet = p.id
99 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
100 ORDER BY r.tick+c.wave,x,y,z});
101         $query->execute($c->user->id) or die $dbh->errstr;
102         my @targets;
103         while (my $target = $query->fetchrow_hashref){
104                 push @targets, $target;
105         }
106
107         $c->stash(claimedtargets => \@targets);
108 }
109
110 sub listAlliances : Private {
111         my ($self, $c) = @_;
112         my @alliances;
113         push @alliances,{id => -1, name => ''};
114         my $query = $c->model->prepare(q{SELECT id,name FROM alliances ORDER BY LOWER(name)});
115         $query->execute;
116         while (my $ally = $query->fetchrow_hashref){
117                 push @alliances,$ally;
118         }
119         $c->stash(alliances => \@alliances);
120 }
121
122 sub auto : Private {
123         my ($self, $c) = @_;
124         my $dbh = $c ->model;
125
126         $c->stash(dbh => $dbh);
127
128         $c->stash(sslurl => sub {
129                         $_[0]->scheme('https') unless $c->debug;
130                         return $_[0];
131                 });
132
133         $dbh->do(q{SET timezone = 'GMT'});
134
135         $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
136         $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
137         $c->stash->{game}->{tick} = $c->stash->{TICK};
138
139         if ($c->user_exists){
140                 $c->stash(UID => $c->user->id);
141         }else{
142                 $c->stash(UID => -4);
143         }
144
145 }
146
147 sub access_denied : Private {
148         my ($self, $c, $action) = @_;
149
150         $c->stash->{template} = 'access_denied.tt2';
151
152 }
153
154 =head2 end
155
156 Attempt to render a view, if needed.
157
158 =cut 
159
160 sub end : ActionClass('RenderView') {
161         my ($self, $c) = @_;
162
163         my $dbh = $c ->model;
164
165         if (scalar @{ $c->error } ){
166                 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
167                         $c->stash->{template} = 'access_denied.tt2';
168                         $c->clear_errors;
169                 }elsif ($c->error->[0] =~ m/Missing roles: /){
170                         $c->stash->{template} = 'access_denied.tt2';
171                         $c->clear_errors;
172                 }
173         }
174
175         if ($c->user_exists && $c->res->status == 200){
176                 my $fleetupdate = 0;
177                 if ($c->check_user_roles(qw/member_menu/)){
178                         $fleetupdate = $dbh->selectrow_array(q{SELECT tick FROM fleets WHERE sender = ?
179                                 AND mission = 'Full fleet' AND tick > tick() - 24
180                                 },undef,$c->user->planet);
181                         $fleetupdate = 0 unless defined $fleetupdate;
182                 }
183
184                 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
185                         ,undef,$c->user->id);
186
187                 $c->stash(user => {
188                         id => $c->user->id,
189                         name => $c->user->username,
190                         css => $c->user->css,
191                         newposts => $newposts,
192                         unreadposts => $unread
193                 });
194                 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
195                         && (!$c->check_user_roles(qw/member_menu/)
196                                 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
197                                         || $c->check_user_roles(qw/no_fleet_update/)))),
198                 $c->forward('listTargets');
199         }
200 }
201
202 =head1 AUTHOR
203
204 Michael Andreen (harv@ruin.nu)
205
206 =head1 LICENSE
207
208 GPL 2, or later.
209
210 =cut
211
212 1;