1 package Plack
::App
::Gruntmaster
;
5 our $VERSION = '5999.000_001';
7 use Apache2
::Authen
::Passphrase qw
/pwcheck pwset/;
9 use File
::Slurp qw
/read_file/;
10 use JavaScript
::Minifier
::XS
;
12 use HTML
::Template
::Compiled
;
14 use Scope
::Upper qw
/unwind SUB UP/;
16 use Gruntmaster
::Data
;
18 no warnings FATAL
=> 'all';
20 no warnings
::illegalproto
;
21 no if $] >= 5.017011, warnings
=> 'experimental::smartmatch';
23 ##################################################
28 my %tmpl = map { m/\.(.+)$/; $1 => scalar read_file
$_ } <tmpl
/$name.*>;
29 my %arti = map { m/\.(.+)$/; $1 => scalar read_file
$_ } <a
/$name.*>;
30 return %tmpl ?
%tmpl : %arti
33 my %header_templates = read_templates
'header';
34 my %footer_templates = read_templates
'footer';
38 my ($tmpl, $title, %params) = @_;
39 unless ($templates{$tmpl}) {
40 $templates{$tmpl} = { read_templates
$tmpl };
41 for my $lang (keys $templates{$tmpl}) {
42 my $header = $header_templates{$lang} =~ s/TITLE_GOES_HERE/$title/rg;
43 $templates{$tmpl}{$lang} = $header . $templates{$tmpl}{$lang};
45 $templates{$tmpl}{$_} .= $footer_templates{$_} for keys $templates{$tmpl};
49 my $htc = HTML
::Template
::Compiled
->new(scalarref
=> \
$templates{$tmpl}{en
}, default_escape
=> 'HTML', use_perl
=> 1);
51 [200, ['Content-Type' => 'text/html'], [$htc->output]]
54 use constant USER_REGEX
=> qr/^\w{2,20}$/a;
56 use constant CONTENT_TYPES
=> +{
58 cpp
=> 'text/x-c++src',
59 cs
=> 'text/x-csharp', # Used by GNOME. Not in mime.types.
60 java
=> 'text/x-java',
61 pas
=> 'text/x-pascal',
63 py
=> 'text/x-python',
66 use constant FORMAT_EXTENSION
=> {
76 use constant NOT_FOUND
=> [404, ['Content-Type' => 'text/plain'], ['Not found']];
77 use constant FORBIDDEN
=> [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => ' Basic realm="Gruntmaster 6000"'], ['Forbidden']];
81 sub db
{ $env->{'gruntmaster.dbic'} }
84 my $user = $env->{'gruntmaster.user'};
85 $user &&= db
->user($user);
89 sub admin
{ remote_user
&& remote_user
->isadmin }
90 sub contest
{ db
->contest ($_{contest
}) }
91 sub problem
{ db
->problem ($_{problem
}) }
92 sub job
{ db
->job ($_{job
}) }
93 sub user
{ db
->user ($_{user
}) }
95 sub redirect
{ [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
96 sub reply
{ [200, ['Content-Type' => 'text/plain'], \
@_] }
98 my ($template, $title, $params) = @_;
103 bless {template
=> $template, title
=> $title, params
=> $params}, __PACKAGE__
.'::Response'
106 return if !shift || admin
;
107 unwind FORBIDDEN
, SUB UP
110 sub dispatch_request
{
114 my $theme = $_{theme
};
115 return NOT_FOUND
unless -e
"css/themes/$theme.css";
116 my $css = read_file
"css/themes/$theme.css";
117 $css .= read_file
$_ for <css
/*.css
>;
118 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
119 [200, \
@headers, [CSS
::Minifier
::XS
::minify
$css]]
124 $js .= read_file
$_ for <js
/*.js
>;
125 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
126 [200, \
@headers, [JavaScript
::Minifier
::XS
::minify
$js]]
130 return NOT_FOUND
if !job
;
131 forbid job
->private || job
->problem->private || job
->contest && job
->contest->private;
132 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => CONTENT_TYPES
->{job
->format});
133 [200, \
@headers, [job
->source]]
137 return NOT_FOUND
if !contest
;
138 forbid contest
->is_pending;
139 response_filter
{ return shift }
143 return NOT_FOUND
if !problem
;
144 forbid problem
->is_private;
145 response_filter
{ return shift }
151 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
152 return [200, ['Content-Type' => 'application/json', 'X-Forever' => 1], [encode_json
$r->{params
}]] if $env->{HTTP_ACCEPT
} =~ m
,^\s
*application
/json\s
*$,g
;
153 render
$r->{template
}, $r->{title
}, %{$r->{params
}}
158 response st
=> 'Standings', { st
=> [db
->standings] }
162 response st
=> 'Standings', {
163 st
=> [ db
->standings($_{contest
}) ],
164 problems
=> [map { $_->problem } contest
->contest_problems]
168 sub (/ct/:contest
/log/st
) { redirect
"/st/$_{contest}" },
170 sub (/us/) { response us
=> 'Users', {us
=> db
->user_list} },
171 sub (/ct/ + ?
:owner
~) { response ct
=> 'Contests', db
->contest_list(%_) },
172 sub (/log/ + ?
:contest
~&:owner
~&:page
~&:problem
~) { response
log => 'Job list', {log => db
->job_list(%_)} },
173 sub (/pb/ + ?
:owner
~&:contest
~) { response pb
=> 'Problems', db
->problem_list(%_) },
175 sub (/us/:user
) { response us_entry
=> user
->name, db
->user_entry($_{user
}) },
176 sub (/ct/:contest
) { response ct_entry
=> contest
->name, db
->contest_entry($_{contest
}) },
177 sub (/log/:job
) { response log_entry
=> "Job $_{job}", db
->job_entry($_{job
}) },
178 sub (/pb/:problem
+ ?
:contest
~) {
179 return NOT_FOUND
if !contest
&& !problem
->is_in_archive || contest
&& !db
->contest_problems->find($_{contest
}, $_{problem
});
180 forbid problem
->is_private;
181 response pb_entry
=> problem
->name, db
->problem_entry($_{problem
}, $_{contest
}, remote_user
&& remote_user
->id);
184 sub (/) { redispatch_to '/index
' },
187 my $title = read_file "a/$_{article}.en.title";
188 response $_{article} => $title, {};
193 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:level=) {
194 return reply 'Parameter too long
' if grep { length > 200 } values %_;
195 return reply 'Bad username
. Allowed characters are letters
, digits
and underscores
, and the username must be between
2 and 20 characters long
.' unless $_{username} =~ USER_REGEX;
196 return reply 'Username already
in use' if db->user($_{username});
197 return reply 'The two passwords
do not match
' unless $_{password} eq $_{confirm_password};
199 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, level => $_{level}});
200 db->user($_{username})->set_passphrase($_{password});
202 reply 'Registered successfully
';
205 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
207 return reply 'Incorrect password
' unless remote_user->check_passphrase($_{password});
208 return reply 'The two passwords
do not match
' unless $_{new_password} eq $_{confirm_new_password};
209 remote_user->set_passphrase($_{new_password});
210 reply 'Password changed successfully
';
213 sub (/action/submit + %:problem=&:contest~&prog_format=&private~ + *source_code=) {
215 return reply 'This contest has finished
' if contest->is_finished;
216 return reply 'This contest has
not yet started
' if !admin && contest->is_pending;
217 return reply 'Maximum source size is
10KB
' if $_{source_code}->size > 25 * 1024;
218 return reply 'You must
wait 30 seconds between jobs
' if !admin && time <= remote_user->lastjob + 30;
219 remote_user->update({lastjob => time});
221 my $prog = read_file $_{source_code}->path;
222 unlink $_{source_code}->path;
224 maybe contest => $_{contest},
225 maybe private => $_{private},
227 extension => FORMAT_EXTENSION->{$_{prog_format}},
228 format => $_{prog_format},
229 problem => $_{problem},
231 owner => remote_user->id,
234 redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/';