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