1 package NDWeb::Controller::Root;
5 use parent 'Catalyst::Controller';
12 # Sets the actions in this controller to be registered with no prefix
13 # so they function identically to actions created in MyApp.pm
15 __PACKAGE__->config->{namespace} = '';
19 NDWeb::Controller::Root - Root Controller for NDWeb
23 [enter your description here]
33 sub index : Local Path Args(0) {
34 my ( $self, $c ) = @_;
36 $c->visit('/wiki/main');
40 my ( $self, $c ) = @_;
41 $c->stash(template => 'default.tt2');
42 $c->response->status(410);
49 my $gi = Geo::IP->new("/usr/share/GeoIP/GeoIP.dat");
50 my $country = $gi->country_code_by_addr($c->req->address) || '??';
53 if ($c->req->param('remember')){
54 $c->session_time_to_live( 604800 ); # expire in one week.
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)
61 $log->execute($c->user->id,$c->req->address
62 ,$country,$c->sessionid,$remember);
64 $c->forward('redirect');
66 } elsif ($c->req->method eq 'POST'){
74 $c->delete_session("logout");
75 $c->res->redirect($c->uri_for('index'));
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."
94 sub smsconfirm : Local {
98 $c->stash(template => 'default.tt2');
100 return unless $c->req->param('apiMsgId');
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'
108 $sms->execute($c->req->param('apiMsgId')
109 ,$clickatellstatus{$c->req->param('status')}
110 ,$c->req->param('charge')
111 ,$c->req->param('timestamp'));
116 sub begin : Private {
119 $c->res->header( 'Cache-Control' =>
120 'no-store, no-cache, must-revalidate,'.
121 'post-check=0, pre-check=0, max-age=0'
123 $c->res->header( 'Pragma' => 'no-cache' );
124 $c->res->header( 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT' );
127 sub listTargets : Private {
130 my $dbh = $c ->model;
132 my $query = $dbh->prepare(q{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick,
133 (released_coords AND old_claim(timestamp)) AS released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
135 JOIN raid_targets t ON c.target = t.id
136 JOIN raids r ON t.raid = r.id
137 JOIN current_planet_stats p USING (pid)
138 WHERE c.uid = $1 AND r.tick+c.wave > tick() AND r.open AND not r.removed
139 ORDER BY r.tick+c.wave,x,y,z});
140 $query->execute($c->user->id) or die $dbh->errstr;
142 while (my $target = $query->fetchrow_hashref){
143 push @targets, $target;
146 $c->stash(claimedtargets => \@targets);
149 sub listAlliances : Private {
152 push @alliances,{aid => '', alliance => ''};
153 my $query = $c->model->prepare(q{SELECT aid,alliance FROM alliances ORDER BY LOWER(alliance)});
155 while (my $ally = $query->fetchrow_hashref){
156 push @alliances,$ally;
158 $c->stash(alliances => \@alliances);
167 my $dbh = $c ->model;
169 $c->stash(dbh => $dbh);
171 $c->stash(sslurl => \&sslurl);
173 $dbh->do(q{SET timezone = 'GMT'});
175 $c->stash(TICK =>$dbh->selectrow_array('SELECT tick()',undef));
176 $c->stash(STICK =>$dbh->selectrow_array('SELECT max(tick) FROM planet_stats',undef));
177 $c->stash->{game}->{tick} = $c->stash->{TICK};
179 if ($c->user_exists){
180 $c->stash(UID => $c->user->id);
182 $c->stash(UID => -4);
186 sub redirect : Private {
188 $c->res->redirect($c->uri_for('/'.$c->session->{referrer}));
191 sub access_denied : Private {
192 my ($self, $c, $action) = @_;
194 $c->stash->{template} = 'access_denied.tt2';
195 $c->res->status(403);
201 Attempt to render a view, if needed.
205 sub end : ActionClass('RenderView') {
208 if ($c->res->status >= 300 && $c->res->status <= 400 ){
212 my $dbh = $c ->model;
214 if (scalar @{ $c->error } ){
215 if ($c->error->[0] =~ m/Can't call method "id" on an undefined value at/){
216 $c->stash->{template} = 'access_denied.tt2';
217 $c->res->status(403);
219 }elsif ($c->error->[0] =~ m/Missing roles: /){
220 $c->stash->{template} = 'access_denied.tt2';
221 $c->res->status(403);
226 if ($c->user_exists){
228 if ($c->check_user_roles(qw/member_menu/)){
229 $fleetupdate = $dbh->selectrow_array(q{
230 SELECT tick FROM fleets WHERE pid = ? AND tick > tick() - 24
231 AND mission = 'Full fleet' AND name IN ('Main','Advanced Unit');
232 },undef,$c->user->planet);
233 $fleetupdate = 0 unless defined $fleetupdate;
236 my ($unread,$newposts) = $dbh->selectrow_array(q{SELECT * FROM unread_posts($1)}
237 ,undef,$c->user->id);
241 name => $c->user->username,
242 css => $c->user->css,
243 newposts => $newposts,
244 unreadposts => $unread
246 $c->stash->{user}->{attacker} = $c->check_user_roles(qw/attack_menu/)
247 && (!$c->check_user_roles(qw/member_menu/)
248 || ($c->user->planet && (($c->stash->{TICK} - $fleetupdate < 24)
249 || $c->check_user_roles(qw/no_fleet_update/)))),
250 $c->forward('listTargets');
252 my $birthdays = $dbh->prepare(q{SELECT username
253 ,date_part('year',age(birthday)) AS age
254 FROM users WHERE birthday IS NOT NULL
255 AND mmdd(birthday) = mmdd(CURRENT_DATE)
258 $c->stash(birthdays => $birthdays->fetchall_arrayref({}));
260 if ($c->res->status == 200 || $c->req->method eq 'GET'){
261 $c->session->{referrer} = $c->req->path;
267 Michael Andreen (harv@ruin.nu)