Initial commit.
[odin-cgi] / mason / pastebin / dhandler
1 <%perl>
2 my $tag = $m->dhandler_arg;
3
4 sub set_handoff_cookie ($$%) {
5 my ($tag, $edit, %attr) = @_;
6 Odin::bake_cookie $r, "odin-handoff.$tag=$edit",
7 -path => "$Odin::PASTEBIN_PATH/", %attr;
8 }
9
10 my %props = (
11 lang => $lang, title => $title,
12 content => Odin::tidy_pastebin_content $content
13 );
14
15 if (length $tag) {
16
17 if (!defined $edit) {
18 my $db = Odin::open_db;
19 Odin::get_pastebin $db, $tag, my %old;
20 if ($op eq "raw") {
21 $r->content_type("text/plain; charset=utf8");
22 $m->print($old{content});
23 } else {
24 $m->comp("%show", tag => $tag, %old,
25 edit => $Odin::COOKIE{"odin-handoff.$tag"});
26 }
27 } else {
28 if ($op eq "del") {
29 Odin::delete_pastebin $tag, $edit;
30 set_handoff_cookie $tag, "nil", -max_age => 5;
31 $m->redirect("$Odin::PASTEBIN/");
32 } else {
33 my $editp = Odin::update_pastebin $tag, $edit, %props;
34 set_handoff_cookie $tag, $edit;
35 if ($editp) { $m->redirect("$Odin::PASTEBIN/$tag"); }
36 else { $m->comp("%edit", tag => $tag, edit => $edit, %props); }
37 }
38 }
39 } elsif (defined $content) {
40 ($tag, $edit) = Odin::new_pastebin %props;
41 set_handoff_cookie $tag, $edit;
42 $m->redirect("$Odin::PASTEBIN/$tag");
43 } else {
44 Odin::path_info($r) =~ m:/$:
45 or $m->redirect("$Odin::PASTEBIN/", 301);
46 $m->comp("%edit");
47 }
48 </%perl>
49 %#
50 <%args>
51 $content => undef
52 $edit => undef
53 $lang => undef
54 $title => undef
55 $op => "edit"
56 </%args>
57 %#
58 <%def .notfound>
59 <&| SELF:error, title => "not found", status => 404 &>\
60 tag &lsquo;<% $tag %>&rsquo; not found
61 </&>
62 <%args>
63 $tag
64 </%args>
65 </%def>
66 %#
67 <%def .badhash>
68 <&| SELF:error, status => 404 &>\
69 incorrect edit key
70 </&>
71 <%args>
72 $tag
73 </%args>
74 </%def>
75 %#
76 <%once>
77 use utf8;
78 use Digest::SHA qw(sha256_hex);
79 use Odin;
80 </%once>