1 package Gruntmaster
::Handlers
;
6 our $VERSION = '0.001';
9 use Apache2
::Authen
::Passphrase qw
/pwcheck pwset USER_REGEX/;
10 use Apache2
::AuthzCaps qw
/hascaps/;
11 use Apache2
::RequestRec
;
12 use Apache2
::RequestIO
;
14 use Apache2
::Const qw
/OK DECLINED/;
19 use File
::Basename qw
/fileparse/;
20 use File
::Temp qw
/tempdir/;
21 use File
::Copy qw
/move/;
22 use Gruntmaster
::Data qw
/contest_start contest_end push_job set_job_inmeta insert_user PUBLISH/;
24 use constant FORMAT_EXTENSION
=> {
37 $r->set_content_length(length $str);
39 $r->content_type('text/plain');
45 my $req = Apache2
::Request
->new($r);
46 my ($problem, $format, $contest, $private, $prog) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest', 'private', 'source_code';
47 my $upload = $req->upload('prog');
48 if (defined $upload) {
50 $upload->slurp($temp);
51 $prog = $temp if $temp
53 die if defined $contest && $contest !~ /^\w+$/ ;
54 die if defined $contest && (time < contest_start
$contest || time > contest_end
$contest);
60 defined $private ?
(private
=> $private) : (),
61 defined $contest ?
(contest
=> $contest, private
=> 1) : (),
62 filesize
=> length $prog,
63 extension
=> FORMAT_EXTENSION
->{$format},
66 set_job_inmeta
$job, {
70 name
=> 'prog.' . FORMAT_EXTENSION
->{$format},
77 $r->print("Job submitted");
83 my $req = Apache2
::Request
->new($r);
84 my ($username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level) = map { die if length > 200; $_ } map {scalar $req->param($_)} qw
/username password confirm_password name email phone town university level/;
86 local $Apache2::Authen
::Passphrase
::rootdir
= $r->dir_config('AuthenPassphraseRootdir');
87 return aputs
$r, 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $username =~ USER_REGEX
;
88 return aputs
$r, 'Username already in use' if -e
"$Apache2::Authen::Passphrase::rootdir/$username.yml";
89 return aputs
$r, 'The two passwords do not match' unless $password eq $confirm_password;
90 return aputs
$r, 'All fields are required' if grep { !length } $username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level;
91 pwset
$username, $password;
93 insert_user
$username, name
=> $name, email
=> $email, phone
=> $phone, town
=> $town, university
=> $university, level
=> $level;
95 PUBLISH genpage
=> "us/index.html";
96 PUBLISH genpage
=> "us/$username.html";
97 aputs
$r, 'Registered successfully';
102 my $req = Apache2
::Request
->new($r);
103 my ($oldpass, $newpass, $confirm) = map {scalar $req->param($_)} 'password', 'new_password', 'confirm_new_password';
105 local $Apache2::Authen
::Passphrase
::rootdir
= $r->dir_config('AuthenPassphraseRootdir');
106 return aputs
$r, 'Incorrect password' unless eval { pwcheck
$r->user, $oldpass; 1 };
107 return aputs
$r, 'The two passwords do not match' unless $newpass eq $confirm;
109 pwset
$r->user, $newpass;
110 aputs
$r, 'Password changed successfully';
117 my $dir = (fileparse $r->uri)[1];
119 chdir $r->dir_config('root') . $dir;
121 for my $requirement (map { $_->{requirement} } @{$r->requires}) {
122 my ($command, @args) = split ' ', $requirement;
125 when('admin-if-private'){
126 my $meta = LoadFile 'meta.yml';
127 return OK if !$meta->{private} || ($r->user && hascaps $r->user, 'gmadm')
142 # Below is stub documentation for your module. You'd better edit it!
146 Gruntmaster::Handlers - Perl extension for blah blah blah
150 use Gruntmaster::Handlers;
155 Stub documentation for Gruntmaster::Handlers, created by h2xs. It looks like the
156 author of the extension was negligent enough to leave the stub
164 Mention other useful documentation such as the documentation of
165 related modules or operating system documentation (such as man pages
166 in UNIX), or any relevant external documentation such as RFCs or
169 If you have a mailing list set up for your module, mention it here.
171 If you have a web site set up for your module, mention it here.
175 Marius Gavrilescu, E<lt>marius@E<gt>
177 =head1 COPYRIGHT AND LICENSE
179 Copyright (C) 2013 by Marius Gavrilescu
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself, either Perl version 5.18.1 or,
183 at your option, any later version of Perl 5 you may have available.