diff options
author | Yorhel <git@yorhel.nl> | 2010-10-05 19:51:44 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2010-10-05 19:51:44 +0200 |
commit | 8b98ef0a20183170d9a985e729364e890b7348cf (patch) | |
tree | 0a4f987290417e05b91572935e3245fe5f9abd80 /index.cgi | |
parent | 46800ef12d8d81424e7a767eef02251fc5c5284d (diff) |
Stylish error messages
and also added a check for empty paste contents, with the obvious
exception of whitespace (the language).
Diffstat (limited to 'index.cgi')
-rwxr-xr-x | index.cgi | 24 |
1 files changed, 16 insertions, 8 deletions
@@ -31,7 +31,7 @@ my $db = DBI->connect('dbi:Pg:dbname=bpaste', 'bpaste', 'DJbj4nYZ', my @syntax = map /([^\/]+)\.vim$/?$1:(), glob "/usr/share/vim/vim72/syntax/*.vim"; my $cgi = CGI::Minimal->new(); -plain("Too large POST data\n") if $cgi->truncated; +msg('Too large POST data. Make sure your paste does not exceed 1MiB.', 'backform') if $cgi->truncated; my $q = $ENV{QUERY_STRING} || ''; @@ -42,7 +42,7 @@ view($1) if $q =~ /^([a-z0-9]+)$/; raw($1) if $q =~ /^([a-z0-9]+)\.txt$/; unpaste($1) if $q =~ /^([a-z0-9]+)\/unpaste$/; form() if !$q; -plain("404\n"); +msg('Holy cow! This page does not seem to exist.'); sub header { @@ -64,6 +64,13 @@ sub plain { } +sub msg { + my $end = $_[1] ? '<br /><a href="javascript:history.go(-1)">Back to the form</a>.' : ''; + html('newpaste', qq|<tr><td class="numbers"> </td><td class="top"><br />$_[0]$end</td></tr>|); + exit; +} + + sub escape { local $_ = shift; return '' if !$_ && $_ ne '0'; @@ -82,11 +89,12 @@ sub upload { my $s = $cgi->param('s') || 'nosyntax'; my $w = $cgi->param('w') ? 1 : 0; my $c = $cgi->param('c') ? 1 : 0; - plain("Unknown syntax code.") if !grep $s eq $_, @syntax; + msg('Unknown syntax code.', 'backform') if !grep $s eq $_, @syntax; my $dat = $cgi->param('u') || $cgi->param('f') || ''; $dat =~ s/\x0D\x0A?/\n/g; - plain("Only UTF-8 encoded data is allowed!\nMake sure you're not uploading a binary file.") + msg("Only UTF-8 encoded data is allowed!\nMake sure you're not uploading a binary file.", 'backform') if !eval { $dat = decode_utf8($dat, 1); 1; }; + msg("You don't have much to paste, do you?", 'backform') if $dat =~ /^[ \n\s\t]*$/s && $s ne 'whitespace'; require Text::VimColor if $s ne 'nosyntax'; @@ -108,7 +116,7 @@ sub get { }); $q->execute($code); my $r = $q->fetchrow_hashref(); - plain('No paste with that code') if !$r->{'exists'}; + msg('No paste with that code.') if !$r->{'exists'}; $db->do('UPDATE pastes SET lastvisit = NOW() WHERE code = ?', undef, $code); return $r; } @@ -149,9 +157,9 @@ sub raw { sub unpaste { my $code = shift; my $r = get $code, 'ip'; - plain("This is not your paste!") if !$r->{ip} || $r->{ip} ne $ENV{REMOTE_ADDR}; + msg("This is not your paste!") if !$r->{ip} || $r->{ip} ne $ENV{REMOTE_ADDR}; $db->do('DELETE FROM pastes WHERE code = ?', undef, $code); - plain("Unpasted!"); + msg("Unpasted!"); } @@ -192,7 +200,7 @@ __ sub mypastes { - plain("Feature disabled"); + msg("Feature disabled"); my $q = $db->prepare("SELECT code, to_char(date, 'YYYY-MM-DD HH24:MI:SS') AS date, syntax, substring(raw from 1 for 50) AS preview FROM pastes WHERE ip = ? ORDER BY date DESC"); $q->execute($ENV{REMOTE_ADDR}); html(sprintf <<' __', $ENV{REMOTE_ADDR}, |