]> iEval git - gruntmaster-handlers.git/blobdiff - lib/Gruntmaster/Handlers.pm
Move FORMAT_EXTENSION here
[gruntmaster-handlers.git] / lib / Gruntmaster / Handlers.pm
index c77e9239e6664608f04f7f62d0836031dc2b368a..f0281f2826f939da53e258a279818821d7e934b0 100644 (file)
@@ -6,6 +6,7 @@ use warnings;
 our $VERSION = '0.001';
 
 use Apache2::Access;
+use Apache2::Authen::Passphrase qw/pwcheck pwset USER_REGEX/;
 use Apache2::AuthzCaps qw/hascaps/;
 use Apache2::RequestRec;
 use Apache2::RequestIO;
@@ -18,15 +19,29 @@ use Cwd qw/cwd/;
 use File::Basename qw/fileparse/;
 use File::Temp qw/tempdir/;
 use File::Copy qw/move/;
-use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta/;
-use Gruntmaster::Page::Log;
-use Redis;
-
-my $redis = Redis->new;
+use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta insert_user PUBLISH/;
+
+use constant FORMAT_EXTENSION => {
+       C => 'c',
+       CPP => 'cpp',
+       MONO => 'cs',
+       JAVA => 'java',
+       PASCAL => 'pas',
+       PERL => 'pl',
+       PYTHON => 'py',
+       RUBY => 'rb',
+};
+
+sub aputs{
+       my ($r, $str) = @_;
+       $r->set_content_length(length $str);
+       $r->puts($str);
+       $r->content_type('text/plain');
+       OK
+}
 
 sub submit{
   my $r = shift;
-  chdir $r->dir_config('root');
   my $req = Apache2::Request->new($r);
   my ($problem, $format, $contest, $private) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest', 'private';
   my $prog;
@@ -41,24 +56,56 @@ sub submit{
          defined $private ? (private => $private) : (),
          defined $contest ? (contest => $contest, private => 1) : (),
          filesize => length $prog,
-         extension => Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format},
+         extension => FORMAT_EXTENSION->{$format},
   );
 
   set_job_inmeta $job, {
          files => {
                  prog => {
                          format => $format,
-                         name => 'prog.' . Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format},
+                         name => 'prog.' . FORMAT_EXTENSION->{$format},
                          content => $prog,
                  }
          }
   };
 
-  $redis->publish('jobs', $job);
+  PUBLISH 'jobs', $job;
   $r->print("Job submitted");
   OK
 }
 
+sub register{
+       my $r = shift;
+       my $req = Apache2::Request->new($r);
+       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/;
+
+       local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir');
+       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;
+       return aputs $r, 'Username already in use' if -e "$Apache2::Authen::Passphrase::rootdir/$username.yml";
+       return aputs $r, 'The two passwords do not match' unless $password eq $confirm_password;
+       return aputs $r, 'All fields are required' if grep { !length } $username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level;
+       pwset $username, $password;
+
+       insert_user $username, name => $name, email => $email, phone => $phone, town => $town, university => $university, level => $level;
+
+       PUBLISH genpage =>  "us/index.html";
+       PUBLISH genpage =>  "us/$username.html";
+       aputs $r, 'Registered successfully';
+}
+
+sub passwd{
+       my $r = shift;
+       my $req = Apache2::Request->new($r);
+       my ($oldpass, $newpass, $confirm) = map {scalar $req->param($_)} 'password', 'new_password', 'confirm_new_password';
+
+       local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir');
+       return aputs $r, 'Incorrect password' unless eval { pwcheck $r->user, $oldpass; 1 };
+       return aputs $r, 'The two passwords do not match' unless $newpass eq $confirm;
+
+       pwset $r->user, $newpass;
+       aputs $r, 'Password changed successfully';
+}
+
 =begin comment
 
 sub private{
This page took 0.025381 seconds and 4 git commands to generate.