From: Marius Gavrilescu Date: Mon, 20 Oct 2014 18:56:33 +0000 (+0300) Subject: Merge branch 'master' into gruntmaster X-Git-Url: http://git.ieval.ro/?p=gruntmaster-page.git;a=commitdiff_plain;h=124680e8c318f83b91c80e5edb62f287f42de846;hp=bd86f820f0ea665d33aaa6c27ad5bede7d1d1f8c Merge branch 'master' into gruntmaster --- diff --git a/MANIFEST b/MANIFEST index 3bbc496..2829d3b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -18,21 +18,11 @@ js/00-zepto.js js/10-bootstrap-dropdown.js js/90-custom.js js/90-form.js -lib/Gruntmaster/Page/Base.pm -lib/Gruntmaster/Page/CSS.pm -lib/Gruntmaster/Page/Generic.pm -lib/Gruntmaster/Page/JS.pm -lib/Gruntmaster/Page/Log.pm -lib/Gruntmaster/Page/Passwd.pm -lib/Gruntmaster/Page/Pb/Entry.pm -lib/Gruntmaster/Page/Register.pm -lib/Gruntmaster/Page/Src.pm -lib/Gruntmaster/Page/St.pm -lib/Gruntmaster/Page/Submit.pm lib/Plack/App/Gruntmaster.pm +lib/Plack/App/Gruntmaster/HTML.pm log.conf Makefile.PL -MANIFEST +MANIFEST This list of files README run static/gm.css @@ -40,13 +30,11 @@ t/Gruntmaster-Page.t t/mech.t tmpl/ct.en tmpl/ct_entry.en -tmpl/footer.en -tmpl/header.en tmpl/log.en tmpl/log_entry.en tmpl/pb.en tmpl/pb_entry.en +tmpl/skel.en tmpl/st.en tmpl/us.en tmpl/us_entry.en - diff --git a/Makefile.PL b/Makefile.PL index 246d041..f1b1a73 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -16,6 +16,7 @@ WriteMakefile( PREREQ_PM => { qw/Carp 0 Digest::SHA 0 + Encode 0 List::Util 0 POSIX 0 constant 0 @@ -24,19 +25,20 @@ WriteMakefile( strict 0 warnings 0 - Apache2::Authen::Passphrase 0 - Apache2::AuthzCaps 0 CSS::Minifier::XS 0 File::Slurp 0 Gruntmaster::Data 0 - HTML::Template::Compiled 0 - HTTP::Negotiate 0 + HTML::Seamstress 0 JavaScript::Minifier::XS 0 - JSON 0 + JSON::MaybeXS 0 Log::Log4perl 0 - LWP::UserAgent 0 + PerlX::Maybe 0 Plack::Builder 0 - Plack::Request 0/, + Plack::Request 0 + Plack::Util 0 + Scope::Upper 0 + Tie::Hash::Expire 0 + Web::Simple 0/, }, META_MERGE => { dynamic_config => 0, diff --git a/app.psgi b/app.psgi index 856d82c..2a0c979 100644 --- a/app.psgi +++ b/app.psgi @@ -2,103 +2,42 @@ use v5.14; no if $] >= 5.017011, warnings => 'experimental::smartmatch'; -use Apache2::Authen::Passphrase qw/pwcheck/; -use Apache2::AuthzCaps qw/hascaps/; use Gruntmaster::Data; use Plack::App::Gruntmaster; use Plack::Builder; use Plack::Request; +use Plack::Util; use Digest::SHA qw/sha256/; use Log::Log4perl; +use Tie::Hash::Expire; +use constant AUTH_TIMEOUT => 5 * 60; use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i|%h %u "%r" %>s %b "%{Referer}i" "%{User-agent}i"'; -use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self' www.google-analytics.com; style-src 'self'; img-src 'self'; connect-src 'self',; +use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self' www.google-analytics.com; style-src 'self'; img-src 'self'; connect-src 'self'; frame-src 'free.timeanddate.com',; -$Apache2::AuthzCaps::rootdir = $Apache2::Authen::Passphrase::rootdir; -my $word = qr,(\w+),a; my $db = Gruntmaster::Data->connect('dbi:Pg:'); -sub debug { - local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; - $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) -} - -sub some_auth_required { - my $r = Plack::Request->new($_[0]); - return 1 if $_[0]->{'gruntmaster.reqadmin'} || $r->path eq '/action/passwd' || $r->path eq '/submit'; - return 1 if $r->path =~ m,^/ct/$word/pb/$word, && time < $db->contest($1)->stop; - '' -} - -sub is_problem_private { - my $pb = $_[0]; - return 1 if $db->problem($pb)->private; - my $prv = 0; - for my $cp ($db->problem($pb)->contest_problems) { - $prv = 1; - return '' if $cp->contest->start <= time; - } - - $prv -} - -sub admin_required { - local $_ = $_[0]; - my $env = $_[1]; - return $db->contest($env->{'gruntmaster.contest'})->owner->id if $env->{'gruntmaster.contest'} && $db->contest($env->{'gruntmaster.contest'})->start > time; - return $db->problem($1)->owner->id if m,^/pb/$word, && is_problem_private $1 || $env->{'gruntmaster.problem'} && is_problem_private $env->{'gruntmaster.problem'}; - return $db->job ($1)->owner->id if m,^/log/(?:src/)?$word, && ($db->job($1)->private || is_problem_private $db->job($1)->problem->id || $db->job($1)->contest && $db->contest($db->job($1)->contest)->start > time); - '' -} +tie my %auth, 'Tie::Hash::Expire', {expire_seconds => 300}; -sub require_admin { - my $app = $_[0]; - sub { - local *__ANON__ = "require_admin_middleware"; - my $env = $_[0]; - my $r = Plack::Request->new($env); - $env->{'gruntmaster.reqadmin'} = admin_required $r->path, $env; - $app->($env) - } +sub authenticate { + my ($user, $pass, $env) = @_; + my $key = sha256 "$user:$pass"; + $env->{'gruntmaster.user'} = $user; + return 1 if exists $auth{$key}; + return unless $db->user($user) && $db->user($user)->check_passphrase($pass); + $auth{key} = 1; } -sub mangle_request { +sub add_headers { my $app = $_[0]; sub { - local *__ANON__ = 'mangle_request_middleware'; - my $env = $_[0]; - my ($number, $word) = (qr,(\d+),a, qr,(\w+),a); - for ($env->{PATH_INFO}) { - $env->{'gruntmaster.page'} = $1 if s,/page/$number$,/,; - $env->{'gruntmaster.problem'} = $1 if s,^/pb/$word/,/,; - $env->{'gruntmaster.contest'} = $1 if s,^/ct/$word/,/,; - $env->{'gruntmaster.user'} = $1 if s,^/us/$word/,/,; - $env->{'gruntmaster.page'} //= -1 if m,^/log/$,; - } - $app->($env); - } -} - -my %authen_cache; - -sub authenticate { - my ($user, $pass, $env) = @_; - my $cache_key = sha256 "$user:$pass"; - my $time = $authen_cache{$cache_key} // 0; - if ($time >= time - 300) { - return 1; - } else { - delete $authen_cache{$cache_key}; + my $resp = $app->($_[0]); + my $hdrs = Plack::Util::headers($resp->[1]); + $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY); + $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO} =~ qr,^/static/,; + $resp->[1] = $hdrs->headers; + $resp; } - - return unless eval { - pwcheck $user, $pass; - 1 - }; - $authen_cache{$cache_key} = time; - - return if $env->{'gruntmaster.reqadmin'} && $env->{'gruntmaster.reqadmin'} ne $user && !hascaps $user, 'gmadm'; - 1 } Log::Log4perl->init('log.conf'); @@ -106,16 +45,13 @@ my $access_logger = Log::Log4perl->get_logger('access'); $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1; builder { - enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }}; enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) }; enable 'ContentLength'; - enable Header => set => ['Content-Security-Policy', CONTENT_SECURITY_POLICY]; - enable_if { $_[0]->{PATH_INFO} =~ qr,^/static/,} Header => set => ['Cache-Control', 'public, max-age=604800']; + enable \&add_headers; enable 'Static', path => qr,^/static/,; enable 'Log4perl', category => 'plack'; - enable \&mangle_request; - enable \&require_admin; - enable_if \&some_auth_required, 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000'; + enable_if { shift->{HTTP_AUTHORIZATION} } 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000'; + enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }}; enable sub { my $app = $_[0]; sub { $_[0]->{'gruntmaster.dbic'} = $db; $app->($_[0]) } }; - Plack::App::Gruntmaster->to_app + Plack::App::Gruntmaster->run_if_script } diff --git a/css/custom.css b/css/custom.css index 9872ee8..3c62f01 100644 --- a/css/custom.css +++ b/css/custom.css @@ -8,7 +8,6 @@ footer{ width: 60em; margin: auto; margin-top: 6em; - white-space: pre-wrap; color: #777; } @@ -54,4 +53,9 @@ a.dropdown-toggle{ .table-fixed{ table-layout: fixed; -} \ No newline at end of file +} + +#clock{ + float: right; + margin-right: 1em; +} diff --git a/lib/Gruntmaster/Page/Base.pm b/lib/Gruntmaster/Page/Base.pm deleted file mode 100644 index 99ca432..0000000 --- a/lib/Gruntmaster/Page/Base.pm +++ /dev/null @@ -1,107 +0,0 @@ -package Gruntmaster::Page::Base; - -use 5.014000; -use strict; -use warnings; -our $VERSION = '5999.000_001'; - -use File::Slurp qw/read_file/; -use HTML::Template::Compiled; - -################################################## - -sub read_templates { - my $root = 'tmpl'; - my $name = shift; - - map { m/\.(.+)$/; $1 => scalar read_file $_ } ; -} - -my %header_templates = read_templates 'header'; -my %footer_templates = read_templates 'footer'; - -sub header{ - my ($language, $title) = @_; - $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; -} - -sub footer{ - $footer_templates{$_[0]}; -} - -################################################## - -use POSIX (); -use List::Util (); -use LWP::UserAgent; -use Plack::Request (); -use feature (); - -my $ua = LWP::UserAgent->new; -my %templates; - -use Carp qw/cluck/; - -sub import_to { - my ($self, $caller, $name, $title) = @_; - - strict->import; - feature->import(':5.14'); - warnings->import; - File::Slurp->export_to_level(1, $caller, qw/read_file/); - List::Util->export_to_level(1, $caller, qw/sum/); - - no strict 'refs'; - *{"${caller}::ISA"} = [__PACKAGE__]; - *{"${caller}::VERSION"} = $VERSION; - *{"${caller}::strftime"} = \&POSIX::strftime; - *{"${caller}::debug"} = sub { - local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; - $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) - }; - *{"${caller}::db"} = sub { $_[0]->{'gruntmaster.dbic'} }; - *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] }; - *{"${caller}::purge"} = sub { - return unless $ENV{PURGE_HOST}; - my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]"); - $ua->request($req) - }; - - if ($name) { - $templates{$caller} = { read_templates $name }; - $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller}; - $templates{$caller}{$_} .= footer $_ for keys $templates{$caller}; - } -} - -sub import { - return unless $_[0] eq __PACKAGE__; - splice @_, 1, 0, scalar caller; - goto &import_to -} - -################################################## - -sub generate{ - my ($self, $lang, @args) = @_; - - my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML', use_perl => 1); - $self->_generate($htc, $lang, @args); - my $out = $htc->output; - utf8::encode($out); - my $vary = 'Accept-Language, ' . $self->vary; - [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ] -} - -sub _generate {} - -sub vary { '' } - -sub max_age { 60 } - -sub variants { - return [] unless exists $templates{$_[0]}; - [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ] -} - -1 diff --git a/lib/Gruntmaster/Page/CSS.pm b/lib/Gruntmaster/Page/CSS.pm deleted file mode 100644 index 912e948..0000000 --- a/lib/Gruntmaster/Page/CSS.pm +++ /dev/null @@ -1,15 +0,0 @@ -package Gruntmaster::Page::CSS; - -use Gruntmaster::Page::Base; -use CSS::Minifier::XS qw/minify/; - -sub generate{ - my ($self, $format, $env, $theme) = @_; - debug $env => "theme is $theme"; - return [404, ['Content-Type' => 'text/plain'], [ 'Not found' ]] unless -e "css/themes/$theme.css"; - my $css = read_file "css/themes/$theme.css"; - $css .= read_file $_ for ; - [200, ['Content-Type' => 'text/css', 'Cache-Control' => 'public, max-age=604800', 'X-Forever' => 1], [minify $css] ] -} - -1 diff --git a/lib/Gruntmaster/Page/Generic.pm b/lib/Gruntmaster/Page/Generic.pm deleted file mode 100644 index 9e07d4c..0000000 --- a/lib/Gruntmaster/Page/Generic.pm +++ /dev/null @@ -1,175 +0,0 @@ -package Gruntmaster::Page::Generic; - -use 5.014000; -use strict; -use warnings; -our $VERSION = '5999.000_001'; - -use Gruntmaster::Page::Base; -use JSON qw/encode_json decode_json/; -use Plack::Request; -use Sub::Name qw/subname/; - -use constant PAGE_SIZE => 10; - -sub putsym { - my ($key, $value) = @_; - no strict 'refs'; - subname $key => $value if ref $value eq 'CODE'; - *{"$key"} = $value; -} - -sub makepkg { - my ($pkg, $id, $title) = @_; - my $fn = $pkg =~ s,::,/,gr; - return if $INC{"$fn.pm"}; - $INC{"$fn.pm"} = 1; - Gruntmaster::Page::Base->import_to($pkg, $id, $title); - 1 -} - -sub list { - my ($thing, $lang, $env) = @_; - my %thing = %$thing; - my %params; - #debug $env => "Contest is $ct"; - $thing{makers} //= sub { db(shift)->resultset($thing{rsname}) }; - my $rs = $thing{makers}->($env); - $rs = $rs->search(undef, {order_by => 'me.id'}) unless $rs->is_ordered; - if (my $page = $env->{'gruntmaster.page'}) { - my $pages = int ($rs->count / PAGE_SIZE); - $page = $pages if $page == -1; - @params{'page', 'pages'} = ($page, $pages); - $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))}); - } - $rs = $rs->search(undef, { - exists $thing{prefetch} ? (prefetch => $thing{prefetch}) : (), - exists $thing{columns} ? (columns => $thing{columns}) : (), - }); - my @thing = map +{rs => $_, $_->get_columns}, $rs->all; - @thing = map { $thing{mangle}->(); $_ } @thing if exists $thing{mangle}; - @thing = grep { $thing{choose}->() } @thing if exists $thing{choose}; - @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby}; - $thing{group} //= sub { $thing{id} }; - for (@thing) { - my $group = $thing{group}->(); - $params{$group} //= []; - push $params{$group}, $_ - } - wantarray ? %params : \%params -} - -sub entry { - my ($thing, $lang, $env, $id) = @_; - my %thing = %$thing; - debug $env => "Rsname is $thing{rsname} and id is $id"; - my %params = map {+ rs => $_, $_->get_columns } db($env)->resultset($thing{rsname})->find($id); - $thing{mangle}->(local $_ = \%params) if exists $thing{mangle}; - wantarray ? %params : \%params -} - -sub headers ($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] } - -sub create_thing { - my %thing = @_; - my $ucid = ucfirst $thing{id}; - my $pkg = "Gruntmaster::Page::$ucid"; - - putsym "${pkg}::_generate", sub { $_[1]->param(list \%thing, @_[2..$#_]) } if makepkg $pkg, @thing{qw/id title/}; - putsym "${pkg}::Entry::_generate", sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", $thing{entry_title} // ''; - putsym "${pkg}::Read::generate", sub { [200, headers shift, [encode_json list \%thing, @_]] } if makepkg "${pkg}::Read"; - putsym "${pkg}::Entry::Read::generate", sub { [200, headers shift, [encode_json entry \%thing, @_]] } if makepkg "${pkg}::Entry::Read"; -} - -sub params; -sub makers (&); -sub choose (&); -sub sortby (&); -sub group (&); -sub mangle (&); -sub prefetch; -sub columns; - -sub thing (&){ - my %thing; - no strict 'refs'; - local *{"params"} = sub { @thing{qw/id rsname title entry_title/} = @_ }; - local *{"choose"} = sub { $thing{choose} = shift }; - local *{"sortby"} = sub { $thing{sortby} = shift }; - local *{"mangle"} = sub { $thing{mangle} = shift }; - local *{"group"} = sub { $thing{group} = shift }; - local *{"makers"} = sub { $thing{makers} = shift }; - local *{"prefetch"} = sub { $thing{prefetch} = \@_ }; - local *{"columns"} = sub { $thing{columns} = \@_ }; - use strict 'refs'; - - shift->(); - create_thing %thing -} - -################################################## - -thing { - params qw/us User Users/; - choose { $_->{name} =~ /\w/ }; - sortby { lc $a->{name} cmp lc $b->{name} }; -}; - -thing { - params qw/pb Problem Problems/; - prefetch 'owner'; - makers { - my $env = $_[0]; - my $db = db $env; - return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'}; - return $db->problems->search({'contest_problems.contest' => $env->{'gruntmaster.contest'}}, {join => 'contest_problems'}) if exists $env->{'gruntmaster.contest'}; - $db->problems->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct => 1}); - }; - sortby { $a->{name} cmp $b->{name}}; - group { $_->{level} }; - mangle { - my $env = shift; - $_->{owner_name} = $_->{rs}->owner->name; - $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1; - eval { - db($env)->open->create({ - contest => $env->{'gruntmaster.contest'}, - problem => $_->{id}, - owner => $env->{REMOTE_USER}, - }) - } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start; - }; -}; - -thing { - params qw/ct Contest Contests/; - prefetch 'owner'; - sortby { $b->{start} <=> $a->{start} }; - group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' }; - mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = $_->{rs}->owner->name }; -}; - -thing { - params qw/log Job/, 'Job log', 'Job '; - prefetch 'owner', 'problem'; - makers { - my $env = $_[0]; - my $db = db $env; - return $db->jobs->search({'me.owner' => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'}; - return $db->jobs->search({problem => $env->{'gruntmaster.problem'}}) if exists $env->{'gruntmaster.problem'}; - $db->jobs->search({contest => $env->{'gruntmaster.contest'}}) - }; - sortby { $b->{id} <=> $a->{id}}; - mangle { - $_->{results} &&= decode_json $_->{results}; - $_->{owner_name} = $_->{rs}->owner->name; - $_->{problem_name} = $_->{rs}->problem->name; - $_->{size} = length $_->{source}; - delete $_->{source}; - } -}; - -putsym 'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' }; -putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 }; - -1 diff --git a/lib/Gruntmaster/Page/JS.pm b/lib/Gruntmaster/Page/JS.pm deleted file mode 100644 index f51000e..0000000 --- a/lib/Gruntmaster/Page/JS.pm +++ /dev/null @@ -1,14 +0,0 @@ -package Gruntmaster::Page::JS; - -use Gruntmaster::Page::Base; -use JavaScript::Minifier::XS qw/minify/; - -sub generate{ - my ($self, $format, $env) = @_; - debug $env => ""; - my $js; - $js .= read_file $_ for ; - [200, ['Content-Type' => 'application/javascript', 'Cache-Control' => 'public, max-age=604800', 'X-Forever' => 1], [minify $js] ] -} - -1 diff --git a/lib/Gruntmaster/Page/Passwd.pm b/lib/Gruntmaster/Page/Passwd.pm deleted file mode 100644 index 3a7522e..0000000 --- a/lib/Gruntmaster/Page/Passwd.pm +++ /dev/null @@ -1,18 +0,0 @@ -package Gruntmaster::Page::Passwd; - -use Gruntmaster::Page::Base; -use Apache2::Authen::Passphrase qw/pwcheck pwset/; - -sub generate{ - my ($self, $format, $env) = @_; - my $r = Plack::Request->new($env); - my ($oldpass, $newpass, $confirm) = map {scalar $r->param($_)} 'password', 'new_password', 'confirm_new_password'; - - return reply 'Incorrect password' unless eval { pwcheck $r->user, $oldpass; 1 }; - return reply 'The two passwords do not match' unless $newpass eq $confirm; - - pwset $r->user, $newpass; - reply 'Password changed successfully'; -} - -1 diff --git a/lib/Gruntmaster/Page/Register.pm b/lib/Gruntmaster/Page/Register.pm deleted file mode 100644 index ed25660..0000000 --- a/lib/Gruntmaster/Page/Register.pm +++ /dev/null @@ -1,23 +0,0 @@ -package Gruntmaster::Page::Register; - -use Gruntmaster::Page::Base; -use Apache2::Authen::Passphrase qw/pwcheck pwset USER_REGEX/; - -sub generate{ - my ($self, $format, $env) = @_; - my $r = Plack::Request->new($env); - my ($username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level) = map { die if length > 200; $_ } map {scalar $r->param($_)} qw/username password confirm_password name email phone town university level/; - - 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; - return reply 'Username already in use' if -e "$Apache2::Authen::Passphrase::rootdir/$username.yml"; - return reply 'The two passwords do not match' unless $password eq $confirm_password; - return reply 'All fields are required' if grep { !length } $username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level; - pwset $username, $password; - - db($env)->create({id => $username, name => $name, email => $email, phone => $phone, town => $town, university => $university, level => $level}); - - purge "/us/"; - reply 'Registered successfully'; -} - -1 diff --git a/lib/Gruntmaster/Page/Src.pm b/lib/Gruntmaster/Page/Src.pm deleted file mode 100644 index 4fc2550..0000000 --- a/lib/Gruntmaster/Page/Src.pm +++ /dev/null @@ -1,22 +0,0 @@ -package Gruntmaster::Page::Src; - -use Gruntmaster::Page::Base; - -use constant CONTENT_TYPES => +{ - c => 'text/x-csrc', - cpp => 'text/x-c++src', - cs => 'text/x-chsarp', # Used by GNOME. Not in mime.types. - java => 'text/x-java', - pas => 'text/x-pascal', - pl => 'text/x-perl', - py => 'text/x-python', -}; - -sub generate{ - my ($self, $format, $env, $job, $ext) = @_; - debug $env => "Job is $job and extension is $ext"; - - [200, ['Content-Type' => CONTENT_TYPES->{$ext}, 'Cache-Control' => 'max-age=604800', 'X-Forever' => 1], [db($env)->job($job)->source] ] -} - -1 diff --git a/lib/Gruntmaster/Page/St.pm b/lib/Gruntmaster/Page/St.pm deleted file mode 100644 index 547eaba..0000000 --- a/lib/Gruntmaster/Page/St.pm +++ /dev/null @@ -1,61 +0,0 @@ -package Gruntmaster::Page::St; - -use Gruntmaster::Page::Base st => 'Standings'; - -use constant LEVEL_VALUES => { - beginner => 100, - easy => 250, - medium => 500, - hard => 1000, -}; - -sub calc_score{ - my ($mxscore, $time, $tries, $totaltime) = @_; - my $score = $mxscore; - $time = 0 if $time < 0; - $time = 300 if $time > $totaltime; - $score = ($totaltime - $time) / $totaltime * $score; - $score -= $tries / 10 * $mxscore; - $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10; - int $score + 0.5 -} - -sub _generate{ - my ($self, $htc, $lang, $env) = @_; - #debug $env => "language is '$lang'"; - - my $ct = $env->{'gruntmaster.contest'} && db($env)->contest($env->{'gruntmaster.contest'}); - - my @problems = map { $_->problem } db($env)->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/}); - my (%scores, %tries); - for my $job (db($env)->jobs->search({contest => $ct && $ct->id})) { - - if ($ct) { - my $time = $job->date - $ct->start; - next if $time < 0; - my $value = $job->problem->value // LEVEL_VALUES->{$job->problem->level}; - $scores{$job->owner->id}{$job->problem->id} = $job->result ? 0 : calc_score ($value, $time, $tries{$job->owner}{$job->problem}, $ct->stop - $ct->start); - $tries{$job->owner->id}{$job->problem->id}++; - } else { - no warnings 'numeric'; - $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100) - } - } - - my @st = sort { $b->{score} <=> $a->{score} or $a->{user}->id cmp $b->{user}->id} map { - my $user = $_; - +{ - user => db($env)->user($user), - score => sum (values $scores{$user}), - scores => [map { $scores{$user}{$_->id} // '-'} @problems], - problems => $ct, - } - } keys %scores; - - $st[0]->{rank} = 1; - $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st; - $htc->param(problems => \@problems) if $ct; - $htc->param(st => \@st); -} - -1 diff --git a/lib/Gruntmaster/Page/Submit.pm b/lib/Gruntmaster/Page/Submit.pm deleted file mode 100644 index 0c42de9..0000000 --- a/lib/Gruntmaster/Page/Submit.pm +++ /dev/null @@ -1,49 +0,0 @@ -package Gruntmaster::Page::Submit; - -use Gruntmaster::Page::Base; - -use constant FORMAT_EXTENSION => { - C => 'c', - CPP => 'cpp', - MONO => 'cs', - JAVA => 'java', - PASCAL => 'pas', - PERL => 'pl', - PYTHON => 'py', -}; - -sub generate{ - my ($self, $frm, $env) = @_; - my $r = Plack::Request->new($env); - my ($problem, $format, $contest, $private, $prog) = map {scalar $r->param($_)} 'problem', 'prog_format', 'contest', 'private', 'source_code'; - $problem //= $env->{'gruntmaster.problem'}; - $contest //= $env->{'gruntmaster.contest'}; - my $upload = $r->upload('prog'); - if (defined $upload) { - my $temp = read_file $upload->path; - $prog = $temp if $temp - } - die if defined $contest && $contest !~ /^\w+$/ ; - die if defined $contest && (time > db($env)->contest($contest)->stop); - return reply 'A required parameter was not supplied' if grep { !defined } $problem, $format, $prog; - return reply 'Maximum source size is 10KB' if length $prog > 25 * 1024; - return reply 'You must wait 30 seconds between jobs' unless time > db($env)->user($r->user)->lastjob + 30; - db($env)->user($r->user)->update({lastjob => time}); - - db($env)->jobs->create({ - defined $contest ? (contest => $contest) : (), - date => time, - extension => FORMAT_EXTENSION->{$format}, - format => $format, - defined $private ? (private => $private) : (), - problem => $problem, - source => $prog, - owner => $r->user - }); - - $contest //= ''; - #PUBLISH 'jobs', "$contest.$job"; - [303, [Location => ($contest ? "/ct/$contest/log/" : '/log/')], ['']] -} - -1 diff --git a/lib/Plack/App/Gruntmaster.pm b/lib/Plack/App/Gruntmaster.pm index e009d79..f647783 100644 --- a/lib/Plack/App/Gruntmaster.pm +++ b/lib/Plack/App/Gruntmaster.pm @@ -2,88 +2,214 @@ package Plack::App::Gruntmaster; use 5.014000; use strict; -use warnings; -use parent qw/Plack::Component/; -no if $] >= 5.017011, warnings => 'experimental::smartmatch'; our $VERSION = '5999.000_001'; +use CSS::Minifier::XS; +use Encode qw/encode decode/; use File::Slurp qw/read_file/; -use HTTP::Negotiate qw/choose/; -use Plack::Request; -use Gruntmaster::Page::Generic; - -my %handlers; - -sub call { - my $env = $_[1]; - my $r = Plack::Request->new($env); - my @handlers = @{ $handlers{$r->method} // [] }; - for my $handler (@handlers) { - my ($re, $obj) = @$handler; - my @args; - next unless @args = $r->path =~ m/^$re$/a; - my $format = choose $obj->variants, $r->headers; - return $obj->generate($format, $env, map { $_ // '' } @args); - } +use JavaScript::Minifier::XS; +use JSON::MaybeXS qw/encode_json/; +use PerlX::Maybe; +use Scope::Upper qw/unwind SUB UP/; +use Web::Simple; - if ($r->method eq 'GET' || $r->method eq 'HEAD') { - my $article = $r->path eq '/' ? '/index' : $r->path; - $article = substr $article, 1; - $article =~ tr,/,_,; - my @variants = grep { !/\.title$/ } ; - if (@variants) { - my $lang = choose [ map { [$_, 1, 'text/html', undef, undef, $_, undef] } map { /\.(.+)$/ } @variants ], $r->headers; - my $content = read_file "a/$article.$lang"; - my $title = read_file "a/$article.$lang.title"; - my $html = Gruntmaster::Page::Base::header($lang, $title) . $content . Gruntmaster::Page::Base::footer($lang); - return [200, ['Content-Type' => 'text/html', 'Content-Language' => $lang, 'Vary' => 'Accept-Language', 'X-Forever' => 1, 'Cache-Control' => 'max-age=300'], [$html] ] - } - } +use Gruntmaster::Data; +use Plack::App::Gruntmaster::HTML; + +use warnings NONFATAL => 'all'; +no warnings 'illegalproto'; +no if $] >= 5.017011, warnings => 'experimental::smartmatch'; + +################################################## + +use constant USER_REGEX => qr/^\w{2,20}$/a; + +use constant CONTENT_TYPES => +{ + c => 'text/x-csrc', + cpp => 'text/x-c++src', + cs => 'text/x-csharp', # Used by GNOME. Not in mime.types. + go => 'text/plain', # ? + hs => 'text/x-haskell', + java => 'text/x-java', + pas => 'text/x-pascal', + pl => 'text/x-perl', + py => 'text/x-python', +}; - [404, ['Content-Type' => 'text/plain'], ['Not found']] +use constant FORMAT_EXTENSION => { + C => 'c', + CPP => 'cpp', + GCCGO => 'go', + GOLANG => 'go', + HASKELL => 'hs', + MONO => 'cs', + JAVA => 'java', + PASCAL => 'pas', + PERL => 'pl', + PYTHON => 'py', +}; + +use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']]; +use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']]; + +my $env; + +sub db { $env->{'gruntmaster.dbic'} } + +sub remote_user { + my $user = $env->{'gruntmaster.user'}; + $user &&= db->user($user); + $user } -sub get { - my ($re, $obj) = @_; - eval "require Gruntmaster::Page::$obj" or die $@; - push @{$handlers{GET }}, [ $re, "Gruntmaster::Page::$obj" ] +sub admin { remote_user && remote_user->admin } +sub contest { db->contest ($_{contest}) } +sub problem { db->problem ($_{problem}) } +sub job { db->job ($_{job}) } +sub user { db->user ($_{user}) } + +sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] } +sub reply { [200, ['Content-Type' => 'text/plain'], \@_] } +sub response { + my ($template, $title, $params) = @_; + unless ($params) { + $params = $title; + $title = 'No title'; + } + bless {template => $template, title => $title, params => $params}, __PACKAGE__.'::Response' } -sub post { - my ($re, $obj) = @_; - eval "require Gruntmaster::Page::$obj" or die $@; - push @{$handlers{POST}}, [ $re, "Gruntmaster::Page::$obj" ] +sub forbid { + return if !shift || admin; + unwind FORBIDDEN, SUB UP } -BEGIN{ - my $word = qr,(\w+),a; - my $number = qr,(\d+),a; - - sub generic { - for my $thing (@_) { - my $pkg = ucfirst $thing; - get qr,/$thing/, => $pkg; - get qr,/$thing/read, => "${pkg}::Read"; - get qr,/$thing/$word, => "${pkg}::Entry"; -# post qr,/$thing/$word/create, => "${pkg}::Entry::Create"; - get qr,/$thing/$word/read, => "${pkg}::Entry::Read"; -# post qr,/$thing/$word/update, => "${pkg}::Entry::Update"; -# post qr,/$thing/$word/delete, => "${pkg}::Entry::Delete"; - } - } +sub dispatch_request{ + $env = $_[PSGI_ENV]; + sub (GET) { + sub (/css/:theme) { + my $theme = $_{theme}; + return NOT_FOUND unless -e "css/themes/$theme.css"; + my $css = read_file "css/themes/$theme.css"; + $css .= read_file $_ for ; + my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css'); + [200, \@headers, [CSS::Minifier::XS::minify $css]] + }, - get qr,/css/$word\.css, => 'CSS'; - get qr,/js\.js, => 'JS'; + sub (/js.js) { + my $js; + $js .= read_file $_ for ; + my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript'); + [200, \@headers, [JavaScript::Minifier::XS::minify $js]] + }, - get qr,/log/st, => 'St'; - generic qw/us ct pb log/; + sub (/src/:job) { + return NOT_FOUND if !job; + forbid job->private || job->problem->private || job->contest && job->contest->private; + my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => CONTENT_TYPES->{job->format}); + [200, \@headers, [job->source]] + }, - get qr,/log/src/$number\.$word, => 'Src'; - post qr,/submit, => 'Submit'; + sub (?:contest=) { + return NOT_FOUND if !contest; + forbid contest->is_pending; + response_filter { return shift } + }, - post qr,/action/register, => 'Register'; - post qr,/action/passwd, => 'Passwd'; + sub (?:problem=) { + return NOT_FOUND if !problem; + forbid problem->is_private; + response_filter { return shift } + }, + + sub () { + response_filter { + my ($r) = @_; + return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; + return [200, ['Content-Type' => 'application/json', 'X-Forever' => 1], [encode 'UTF-8', encode_json $r->{params}]] if $env->{HTTP_ACCEPT} =~ m,^\s*application/json\s*$,g; + my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; + [200, ['Content-Type' => 'text/html'], [encode 'UTF-8', $ret]] + }, + }, + + sub (/st/) { + response st => 'Standings', { st => [db->standings] } + }, + + sub (/st/:contest) { + response st => 'Standings', { + st => [ db->standings($_{contest}) ], + problems => [map { $_->problem } contest->contest_problems] + } + }, + + sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, + + sub (/us/) { response us => 'Users', {us => db->user_list} }, + sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) }, + sub (/log/ + ?:contest~&:owner~&:page~&:problem~) { response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest}} }, + sub (/pb/ + ?:owner~&:contest~) { response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}} }, + + sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) }, + sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) }, + sub (/log/:job) { response log_entry => "Job $_{job}", db->job_entry($_{job}) }, + sub (/pb/:problem + ?:contest~) { + return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem}); + forbid problem->is_private; + response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id); + }, + + sub (/) { redispatch_to '/index' }, + sub (/:article) { [200, ['Content-Type' => 'text/html'], [render_article $_{article}, 'en']] } + }, + + sub (POST) { + sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:level=) { + return reply 'Parameter too long' if grep { length > 200 } values %_; + 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; + return reply 'Username already in use' if db->user($_{username}); + return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password}; + + db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, level => $_{level}}); + db->user($_{username})->set_passphrase($_{password}); + + reply 'Registered successfully'; + }, + + sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) { + forbid !remote_user; + return reply 'Incorrect password' unless remote_user->check_passphrase($_{password}); + return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password}; + remote_user->set_passphrase($_{new_password}); + reply 'Password changed successfully'; + }, + + sub (/action/submit + %:problem=&:contest~&:prog_format=&:private~&:source_code~ + *:prog~) { + forbid !remote_user; + return reply 'This contest has finished' if contest && contest->is_finished; + return reply 'This contest has not yet started' if !admin && contest && contest->is_pending; + return reply 'Maximum source size is 10KB' if ($_{prog} ? $_{prog}->size : length $_{source_code}) > 10 * 1024; + return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30; + remote_user->update({lastjob => time}); + + my $prog = $_{prog} ? read_file $_{prog}->path : $_{source_code}; + unlink $_{prog}->path if $_{prog}; + db->jobs->create({ + maybe contest => $_{contest}, + maybe private => $_{private}, + date => time, + extension => FORMAT_EXTENSION->{$_{prog_format}}, + format => $_{prog_format}, + problem => $_{problem}, + source => $prog, + owner => remote_user->id, + }); + + redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/'; + } + } } + 1; __END__ diff --git a/lib/Plack/App/Gruntmaster/HTML.pm b/lib/Plack/App/Gruntmaster/HTML.pm new file mode 100644 index 0000000..16ae617 --- /dev/null +++ b/lib/Plack/App/Gruntmaster/HTML.pm @@ -0,0 +1,204 @@ +package Plack::App::Gruntmaster::HTML; +use v5.14; +use parent qw/Exporter/; +our @EXPORT = qw/render render_article/; + +use File::Slurp qw/read_file/; +use HTML::Seamstress; +use POSIX qw//; +use Data::Dumper qw/Dumper/; + +sub ftime ($) { POSIX::strftime '%c', localtime shift } +sub literal ($) { HTML::Element::Library::super_literal shift // '' } + +sub HTML::Element::edit_href { + my ($self, $sub) = @_; + local $_ = $self->attr('href'); + $sub->(); + $self->attr(href => $_); +} + +sub HTML::Element::iter3 { + my ($self, $data, $code) = @_; + my $orig = $self; + my $prev = $orig; + for my $el (@$data) { + my $current = $orig->clone; + $code->($el, $current); + $prev->postinsert($current); + $prev = $current; + } + $orig->detach; +} + +sub HTML::Element::fid { shift->look_down(id => shift) } +sub HTML::Element::fclass { shift->look_down(class => shift) } + +sub HTML::Element::namedlink { + my ($self, $id, $name) = @_; + $name = $id unless $name =~ /[[:graph:]]/; + $self = $self->find('a'); + $self->edit_href(sub {s/id/$id/}); + $self->replace_content($name); +} + +sub render { + my ($tmpl, $lang, %args) = @_; + $lang //= 'en'; + my $meat = _render($tmpl, $lang, %args); + _render('skel', $lang, %args, meat => $meat) +} + +sub render_article { + my ($art, $lang) = @_; + $lang //= 'en'; + my $title = read_file "a/$art.$lang.title"; + my $meat = read_file "a/$art.$lang"; + _render('skel', $lang, title => $title , meat => $meat) +} + +sub _render { + my ($tmpl, $lang, %args) = @_; + my $builder = HTML::Seamstress->new; + $builder->ignore_unknown(0); + my $tree = $builder->parse_file("tmpl/$tmpl.$lang"); + $tree = $tree->guts unless $tmpl eq 'skel'; + $tree->defmap(smap => \%args); + my $process = __PACKAGE__->can("process_$tmpl"); + $process->($tree, %args) if $process; + $tree->as_HTML; +} + +sub process_skel { + my ($tree, %args) = @_; + $tree->content_handler( + title => $args{title}, + content => literal $args{meat}); +} + +sub process_us_entry { + my ($tree, %args) = @_; + $tree->fid($_)->attr('href', "/$_/?owner=$args{id}") for qw/log pb/; +} + +sub process_us { + my ($tree, %args) = @_; + my $item = $tree->fclass('list-group-item'); + $item->replace_with(map { + my $new = $item->clone; + $new->attr(href => $_->{id}); + $new->replace_content($_->{name} || $_->{id}); + $new + } @{$args{us}}); +} + +sub process_ct_entry { + my ($tree, %args) = @_; + $_->edit_href (sub {s/contest_id/$args{id}/}) for $tree->find('a'); + $tree->fid('links')->detach unless $args{started}; + $tree->content_handler( + start => ftime $args{start}, + stop => ftime $args{stop}, + description => literal $args{description}); +} + +sub process_ct { + my ($tree, %args) = @_; + my $iter = sub { + my ($data, $tr) = @_; + $data->{$_} = ftime $data->{$_} for qw/start stop/; + $tr->hashmap(class => $data, [qw/name owner/]); + $tr->fclass('name')->namedlink($data->{id}, $data->{name}); + $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name}); + }; + $args{$_} ? $tree->fid($_)->find('tbody')->find('tr')->iter3($args{$_}, $iter) : $tree->fid($_)->detach for qw/running pending finished/; +} + +sub process_pb_entry { + my ($tree, %args) = @_; + $tree->fid('owner')->edit_href(sub{s/owner_id/$args{owner}/}); + $tree->fid('job_log')->edit_href(sub{s/problem_id/$args{id}/}); + $tree->content_handler( + statement => literal $args{statement}, + author => $args{author}, + owner => $args{owner_name} || $args{owner}); + if ($args{cansubmit}) { + $tree->look_down(name => 'problem')->attr(value => $args{id}); + my $contest = $tree->look_down(name => 'contest'); + $contest->attr(value => $args{contest}) if $args{contest}; + $contest->detach unless $args{contest} + } else { + $tree->fid('submit')->detach + } +} + +sub process_pb { + my ($tree, %args) = @_; + my $titer = sub { + my ($data, $tr) = @_; + $tr->set_child_content(class => 'author', $data->{author}); + $tr->fclass('name')->namedlink($data->{id}, $data->{name}); + $tr->fclass('name')->find('a')->edit_href(sub {$_ .= "?contest=$args{contest}"}) if $args{contest}; + $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name}); + }; + my $iter = sub { + my ($data, $div) = @_; + $div->attr(id => $data); + $div->find('h2')->replace_content(ucfirst $data); + $div->find('tbody')->iter3($args{$data}, $titer); + }; + $tree->fid('beginner')->iter3([grep {$args{$_}} qw/beginner easy medium hard/], $iter); +} + +sub process_log_entry { + my ($tree, %args) = @_; + $args{errors} ? $tree->fid('errors')->find('pre')->replace_content($args{errors}) : $tree->fid('errors')->detach; + my $iter = sub { + my ($data, $tr) = @_; + $data->{time} = sprintf '%.4fs', $data->{time}; + $tr->defmap(class => $data); + $tr->fclass('result_text')->attr(class => "r$data->{result}") + }; + @{$args{results}} ? $tree->fid('results')->find('tbody')->find('tr')->iter3($args{results}, $iter) : $tree->fid('results')->detach; +} + +sub process_log { + my ($tree, %args) = @_; + my $iter = sub { + my ($data, $tr) = @_; + $tr->fclass('id')->namedlink($data->{id}); + $tr->fclass('problem')->namedlink($data->{problem}, $data->{problem_name}); + $tr->fclass('problem')->find('a')->edit_href(sub{$_ .= "?contest=$args{contest}"}) if $args{contest}; + $tr->fclass('date')->replace_content(ftime $data->{date}); + $tr->fclass('size')->namedlink("$data->{id}.$data->{extension}", sprintf "%.2fKB", $data->{size}/1024); + $tr->fclass('size')->attr('data-private', '') if $data->{private}; + $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name}); + $tr->fclass('result_text')->replace_content($data->{result_text}); + $tr->fclass('result_text')->attr(class => "r$data->{result}"); + }; + $tree->find('table')->find('tbody')->find('tr')->iter3($args{log}, $iter); + $args{next_page} ? $tree->fclass('next')->namedlink($args{next_page}, 'Next') : $tree->fclass('next')->attr(class => 'next disabled'); + $args{previous_page} ? $tree->fclass('previous')->namedlink($args{previous_page}, 'Previous') : $tree->fclass('previous')->attr(class => 'previous disabled'); + $tree->fclass('current')->replace_content("Page $args{current_page} of $args{last_page}"); +} + +sub process_st { + my ($tree, %args) = @_; + $args{problems} //= []; + my $pbiter = sub { + my ($data, $th) = @_; + $th->attr(class => undef); + $th->namedlink($data->id, $data->name); + }; + $tree->fclass('problem')->iter3($args{problems}, $pbiter); + my $iter = sub { + my ($st, $tr) = @_; + $tr->set_child_content(class => 'rank', $st->{rank}); + $tr->set_child_content(class => 'score', $st->{score}); + $tr->fclass('user')->namedlink($st->{user}->id, $st->{user}->name); + my $pbscore = $tr->fclass('pbscore'); + $pbscore->detach unless $st->{problems}; + $pbscore->iter($pbscore => @{$st->{scores}}); + }; + $tree->find('tbody')->find('tr')->iter3($args{st}, $iter); +} diff --git a/tmpl/ct.en b/tmpl/ct.en index 9420708..31642df 100644 --- a/tmpl/ct.en +++ b/tmpl/ct.en @@ -1,41 +1,35 @@ - +

Running contests

+ -
NameStart dateStop dateOwner +
-<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{start}; %> -<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{stop}; %> - - +
Contest name......Owner name
- +
- +

Pending contests

+ -
NameStart dateStop dateOwner +
-<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{start}; %> -<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{stop}; %> - - +
Contest name......Owner name
- +
- +

Finished contests

+ -
NameStart dateStop dateOwner +
-<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{start}; %> -<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{stop}; %> - - +
Contest name......Owner name
- +
diff --git a/tmpl/ct_entry.en b/tmpl/ct_entry.en index 0ad32c9..6f0a580 100644 --- a/tmpl/ct_entry.en +++ b/tmpl/ct_entry.en @@ -1,8 +1,12 @@ -Contest start time: <%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{start}; %>
-Contest stop time: <%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{stop}; %>

+

+
Contest start time
start
+
Contest stop time
stop
+
- +
description
-
Problems
-Job log
-Standings + diff --git a/tmpl/footer.en b/tmpl/footer.en deleted file mode 100644 index c0f928d..0000000 --- a/tmpl/footer.en +++ /dev/null @@ -1,5 +0,0 @@ -
-Dilmom: Why don't you call your product the Gruntmaster 6000? -Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? -Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. -
diff --git a/tmpl/header.en b/tmpl/header.en deleted file mode 100644 index c4f51f6..0000000 --- a/tmpl/header.en +++ /dev/null @@ -1,42 +0,0 @@ - -TITLE_GOES_HERE - - - - - - - - -
- -

TITLE_GOES_HERE

-
diff --git a/tmpl/log.en b/tmpl/log.en index f2c7983..eb4bf6f 100644 --- a/tmpl/log.en +++ b/tmpl/log.en @@ -1,17 +1,18 @@ - - + -
IDProblemDateSizeUserResult +
IDProblemDateSizeUserResult
- -<%perl __OUT__ POSIX::strftime '%c', localtime __CURRENT__->{date}; %> - data-private><%perl __OUT__ sprintf '%.2fKB', __CURRENT__->{size}/1024; %> - - +
Job ID +Problem name +Date +3.14KB +Owner name +
    -<%perl if (__CURRENT__->{page} > 1) { %>
  • Page 1 of 100 +
diff --git a/tmpl/log_entry.en b/tmpl/log_entry.en index adf6bfd..eb05db8 100644 --- a/tmpl/log_entry.en +++ b/tmpl/log_entry.en @@ -1,15 +1,16 @@ - +

Compiler output

-
- +

+
- +

Results

+ -
Test numberResultTime +
<%perl __OUT__ sprintf "%.4fs", __CURRENT__->{time}; %> - +
- \ No newline at end of file +
\ No newline at end of file diff --git a/tmpl/pb.en b/tmpl/pb.en index 35cfbd4..ae1a002 100644 --- a/tmpl/pb.en +++ b/tmpl/pb.en @@ -1,39 +1,11 @@ - +
+

Beginner

- -
NameAuthorOwner -
- -
- - - -

Easy

- - -
NameAuthorOwner -
- -
-
+NameAuthorOwner - -

Medium

- - -
NameAuthorOwner -
- -
-
- - -

Hard

- - -
NameAuthorOwner
- +
NameauthorOwner name
-
+
+
\ No newline at end of file diff --git a/tmpl/pb_entry.en b/tmpl/pb_entry.en index f3d0f75..8189d0b 100644 --- a/tmpl/pb_entry.en +++ b/tmpl/pb_entry.en @@ -1,35 +1,38 @@
- +
-
Author
-
Owner
+
Author
author
+
Owner
owner
-Job log +Job log - +

Submit solution

-
- - + + +
- +
diff --git a/tmpl/skel.en b/tmpl/skel.en new file mode 100644 index 0000000..a5a3fef --- /dev/null +++ b/tmpl/skel.en @@ -0,0 +1,53 @@ + +TITLE + + + + + + + + + + + +
+ +

TITLE

+
+ +
Content goes here
+ +
+Dilmom: Why don't you call your product the Gruntmaster 6000?
+Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
+Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. +
diff --git a/tmpl/st.en b/tmpl/st.en index 6417f48..1c4c592 100644 --- a/tmpl/st.en +++ b/tmpl/st.en @@ -1,12 +1,8 @@ - -
RankUserTotal -
RankUserScore - +
RankUserProblem nameScore +
- - - +
1User noame100100
diff --git a/tmpl/us.en b/tmpl/us.en index 2b77d08..dad8844 100644 --- a/tmpl/us.en +++ b/tmpl/us.en @@ -1,2 +1,3 @@ -
-
+
+Name +
diff --git a/tmpl/us_entry.en b/tmpl/us_entry.en index 69d95da..413e897 100644 --- a/tmpl/us_entry.en +++ b/tmpl/us_entry.en @@ -1,8 +1,8 @@
-
Town
-
University
-
Level
+
Town
Town
+
University
University
+
Level
Level
-Job log
-Owned problems +Job log
+Owned problems