From 3b1dd27f40d42cbf1fc97df2acec906896c14bb6 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Tue, 10 Oct 2006 00:12:10 +0200 Subject: [PATCH] Factor out CGI submitted data error processing --- cgi/Git/RepoCGI.pm | 13 +++++++++++++ cgi/p/editproj.cgi | 14 +++++--------- cgi/regproj.cgi | 20 ++++++++------------ cgi/reguser.cgi | 14 +++++--------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/cgi/Git/RepoCGI.pm b/cgi/Git/RepoCGI.pm index fdfa20f..4b0c4fa 100644 --- a/cgi/Git/RepoCGI.pm +++ b/cgi/Git/RepoCGI.pm @@ -67,6 +67,19 @@ sub cgi { $self->{cgi}; } +sub err { + my $self = shift; + print "

@_

\n"; + $self->{err}++; +} + +sub err_check { + my $self = shift; + my $err = $self->{err}; + $err and print "

Operation aborted due to $err errors.

\n"; + $err; +} + ### Random utility functions diff --git a/cgi/p/editproj.cgi b/cgi/p/editproj.cgi index 05d356b..9012d7e 100755 --- a/cgi/p/editproj.cgi +++ b/cgi/p/editproj.cgi @@ -93,8 +93,6 @@ sub update_project { if ($cgi->param('email')) { # submitted, let's see # FIXME: racy, do a lock - my $err = 0; - sub err { print "

@_

\n"; $err++; } $email = $cgi->param('email'); $email =~ s/^\s*(.*?)\s*$/$1/; $url = $cgi->param('url'); $url =~ s/^\s*(.*?)\s*$/$1/; $desc = $cgi->param('desc'); $desc =~ s/^\s*(.*?)\s*$/$1/; @@ -104,19 +102,17 @@ if ($cgi->param('email')) { if ($url) { $url =~ /^http:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ or $url =~ /^git:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ - or err "Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me."; + or $repo->err "Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me."; } if ($hp) { $hp =~ /^http:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ - or err "Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me."; + or $repo->err "Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me."; } $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/ - or err "Your email sure looks weird...?"; + or $repo->err "Your email sure looks weird...?"; length($desc) <= 1024 - or err "Short description length > 1kb!"; - if ($err) { - print "

Registration aborted due to $err errors.

\n"; - } else { + or $repo->err "Short description length > 1kb!"; + unless ($repo->err_check) { if ($pwd) { $pwd = scrypt($pwd); } else { diff --git a/cgi/regproj.cgi b/cgi/regproj.cgi index 57fa44b..ab9ce93 100755 --- a/cgi/regproj.cgi +++ b/cgi/regproj.cgi @@ -66,8 +66,6 @@ sub setup_mirror { if ($cgi->param('name')) { # submitted, let's see # FIXME: racy, do a lock - my $err = 0; - sub err { print "

@_

\n"; $err++; } my $name = $cgi->param('name'); $name =~ s/^\s*(.*?)\s*$/$1/; my $email = $cgi->param('email'); $email =~ s/^\s*(.*?)\s*$/$1/; my $url = $cgi->param('url'); $url =~ s/^\s*(.*?)\s*$/$1/; @@ -75,28 +73,26 @@ if ($cgi->param('name')) { my $hp = $cgi->param('hp'); $hp =~ s/^\s*(.*?)\s*$/$1/; my $pwd = $cgi->param('pwd'); $name =~ /^[a-zA-Z0-9_+-]+$/ - or err "Name contains invalid characters."; + or $repo->err "Name contains invalid characters."; (-d "/srv/git/$name.git" or -d "/home/repo/repodata/cloning/$name" or -d "/home/repo/repodata/to-clone/$name") - and err "Project with that name already exists."; + and $repo->err "Project with that name already exists."; if ($url) { $url =~ /^http:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ or $url =~ /^git:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ - or err "Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me."; + or $repo->err "Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me."; } else { $cgi->param('mode') eq 'mirror' - and err "Missing URL."; + and $repo->err "Missing URL."; } if ($hp) { $hp =~ /^http:\/\/[a-zA-Z0-9-.]+\/[_\%a-zA-Z0-9.\/~-]+$/ - or err "Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me."; + or $repo->err "Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me."; } $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/ - or err "Your email sure looks weird...?"; + or $repo->err "Your email sure looks weird...?"; length($desc) <= 1024 - or err "Short description length > 1kb!"; - if ($err) { - print "

Registration aborted due to $err errors.

\n"; - } else { + or $repo->err "Short description length > 1kb!"; + unless ($repo->err_check) { if ($cgi->param('mode') eq 'mirror') { setup_mirror($name, $pwd, $email, $url, $desc, $hp); } elsif ($cgi->param('mode') eq 'push') { diff --git a/cgi/reguser.cgi b/cgi/reguser.cgi index 3da2706..e8c979e 100755 --- a/cgi/reguser.cgi +++ b/cgi/reguser.cgi @@ -48,22 +48,18 @@ EOT if ($cgi->param('name')) { # submitted, let's see # FIXME: racy, do a lock - my $err = 0; - sub err { print "

@_

\n"; $err++; } my $name = $cgi->param('name'); $name =~ s/^\s*(.*?)\s*$/$1/; my $email = $cgi->param('email'); $email =~ s/^\s*(.*?)\s*$/$1/; my $keys = $cgi->param('keys'); $name =~ /^[a-zA-Z0-9_+-]+$/ - or err "Name contains invalid characters."; + or $repo->err "Name contains invalid characters."; (-e "/home/repo/j/etc/sshkeys/$name") - and err "User with that name already exists."; + and $repo->err "User with that name already exists."; $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/ - or err "Your email sure looks weird...?"; + or $repo->err "Your email sure looks weird...?"; length($keys) <= 4096 - or err "The list of keys is more than 4kb. Do you really need that much?"; - if ($err) { - print "

Registration aborted due to $err errors.

\n"; - } else { + or $repo->err "The list of keys is more than 4kb. Do you really need that much?"; + unless ($rep->err_check) { setup_user($name, $email, $keys); exit; } -- 2.11.4.GIT