3c989ac10fe533058e1b3d12bcf219d68d7ffcb3
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
1 package Plack::App::Gruntmaster;
2
3 use 5.014000;
4 use strict;
5 our $VERSION = '5999.000_001';
6
7 use Apache2::Authen::Passphrase qw/pwcheck pwset/;
8 use CSS::Minifier::XS;
9 use File::Slurp qw/read_file/;
10 use JavaScript::Minifier::XS;
11
12 use HTML::Template::Compiled;
13 use PerlX::Maybe;
14 use Scope::Upper qw/unwind SUB UP/;
15
16 use Gruntmaster::Data;
17 use Web::Simple;
18 no warnings FATAL => 'all';
19 use warnings;
20 no warnings::illegalproto;
21 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
22
23 ##################################################
24
25 sub read_templates {
26 my $name = shift;
27
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
31 }
32
33 my %header_templates = read_templates 'header';
34 my %footer_templates = read_templates 'footer';
35 my %templates;
36
37 sub render {
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};
44 }
45 $templates{$tmpl}{$_} .= $footer_templates{$_} for keys $templates{$tmpl};
46
47 }
48
49 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$tmpl}{en}, default_escape => 'HTML', use_perl => 1);
50 $htc->param(%params);
51 [200, ['Content-Type' => 'text/html'], [$htc->output]]
52 }
53
54 use constant USER_REGEX => qr/^\w{2,20}$/a;
55
56 use constant CONTENT_TYPES => +{
57 c => 'text/x-csrc',
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',
62 pl => 'text/x-perl',
63 py => 'text/x-python',
64 };
65
66 use constant FORMAT_EXTENSION => {
67 C => 'c',
68 CPP => 'cpp',
69 MONO => 'cs',
70 JAVA => 'java',
71 PASCAL => 'pas',
72 PERL => 'pl',
73 PYTHON => 'py',
74 };
75
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']];
78
79 my $env;
80
81 sub db { $env->{'gruntmaster.dbic'} }
82
83 sub remote_user {
84 my $user = $env->{'gruntmaster.user'};
85 $user &&= db->user($user);
86 $user
87 }
88
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}) }
94
95 sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
96 sub reply { [200, ['Content-Type' => 'text/plain'], \@_] }
97 sub response {
98 my ($template, $title, $params) = @_;
99 unless ($params) {
100 $params = $title;
101 $title = 'No title';
102 }
103 bless {template => $template, title => $title, params => $params}, __PACKAGE__.'::Response'
104 }
105 sub forbid {
106 return if !shift || admin;
107 unwind FORBIDDEN, SUB UP
108 }
109
110 sub dispatch_request{
111 $env = $_[PSGI_ENV];
112 sub (GET) {
113 sub (/css/:theme) {
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]]
120 },
121
122 sub (/js.js) {
123 my $js;
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]]
127 },
128
129 sub (/src/:job) {
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]]
134 },
135
136 sub (?:contest=) {
137 return NOT_FOUND if !contest;
138 forbid contest->is_pending;
139 response_filter { return shift }
140 },
141
142 sub (?:problem=) {
143 return NOT_FOUND if !problem;
144 forbid problem->is_private;
145 response_filter { return shift }
146 },
147
148 sub () {
149 response_filter {
150 my ($r) = @_;
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}}
154 },
155 },
156
157 sub (/st/) {
158 response st => 'Standings', { st => [db->standings] }
159 },
160
161 sub (/st/:contest) {
162 response st => 'Standings', {
163 st => [ db->standings($_{contest}) ],
164 problems => [map { $_->problem } contest->contest_problems]
165 }
166 },
167
168 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
169
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(%_) },
174
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);
182 },
183
184 sub (/) { redispatch_to '/index' },
185
186 sub (/:article) {
187 my $title = read_file "a/$_{article}.en.title";
188 response $_{article} => $title, {};
189 }
190 },
191
192 sub (POST) {
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};
198
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});
201
202 reply 'Registered successfully';
203 },
204
205 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
206 forbid !remote_user;
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';
211 },
212
213 sub (/action/submit + %:problem=&:contest~&prog_format=&private~ + *source_code=) {
214 forbid !remote_user;
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});
220
221 my $prog = read_file $_{source_code}->path;
222 unlink $_{source_code}->path;
223 db->jobs->create({
224 maybe contest => $_{contest},
225 maybe private => $_{private},
226 date => time,
227 extension => FORMAT_EXTENSION->{$_{prog_format}},
228 format => $_{prog_format},
229 problem => $_{problem},
230 source => $prog,
231 owner => remote_user->id,
232 });
233
234 redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/';
235 }
236 }
237 }
238
239
240 1;
241 __END__
This page took 0.03949 seconds and 3 git commands to generate.