| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::Phoebe::WebEdit - allow edits of a Phoebe wiki via the web | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This package allows visitors on the web to edit your pages. | ||
| 24 | |||
| 25 | There is no configuration. Simply add it to your F<config> file: | ||
| 26 | |||
| 27 | use App::Phoebe::WebEdit; | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package App::Phoebe::WebEdit; | ||
| 32 | use App::Phoebe::Web qw(handle_http_header http_error); | ||
| 33 | use App::Phoebe qw(@footer @extensions @request_handlers @main_menu $server $log | ||
| 34 | port space host_regex space_regex text with_lock wiki_dir | ||
| 35 | bogus_hash to_url quote_html); | ||
| 36 | use Encode qw(decode_utf8 encode_utf8); | ||
| 37 | use File::Slurper qw(read_text write_text read_dir); | ||
| 38 | use Modern::Perl; | ||
| 39 | use URI::Escape; | ||
| 40 | |||
| 41 | unshift(@request_handlers, '^POST .* HTTP/1\.[01]$' => \&handle_http_header); | ||
| 42 | |||
| 43 | # edit from the web | ||
| 44 | |||
| 45 | push(@footer, \&add_edit_link_to_footer); | ||
| 46 | |||
| 47 | sub is_editable { | ||
| 48 | my ($space) = @_; | ||
| 49 | my $editable = { return test => 1, gemini => 1 }; | ||
| 50 | return $editable->{$space}; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub add_edit_link_to_footer { | ||
| 54 | my ($stream, $host, $space, $id, $revision, $format) = @_; | ||
| 55 | # only add the edit links to the web UI of the test space | ||
| 56 | # return if not $space or not is_editable($space); | ||
| 57 | return "" if $revision or not $id or $format ne "html"; | ||
| 58 | $id = uri_escape_utf8($id); | ||
| 59 | if ($space) { | ||
| 60 | $space = uri_escape_utf8($space); | ||
| 61 | return "=> /$space/do/edit/$id Edit"; | ||
| 62 | } else { | ||
| 63 | return "=> /do/edit/$id Edit"; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | # note that the requests handled here must be protected in | ||
| 68 | # App::Phoebe::RegisteredEditorsOnly! | ||
| 69 | push(@extensions, \&process_edit_requests); | ||
| 70 | |||
| 71 | sub process_edit_requests { | ||
| 72 | my ($stream, $request, $headers, $buffer) = @_; | ||
| 73 | my $host_regex = host_regex(); | ||
| 74 | my $spaces = space_regex(); | ||
| 75 | my $port = port($stream); | ||
| 76 | my ($host, $space, $id, $token, $query); | ||
| 77 | if (($space, $id) = $request =~ m!^GET (?:/($spaces))?/do/edit/([^/#?]+) HTTP/1\.[01]$! | ||
| 78 | and is_editable($space) | ||
| 79 | and ($host) = $headers->{host} =~ m!^($host_regex)(?::$port)$!) { | ||
| 80 | serve_edit_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id))); | ||
| 81 | } elsif (($space, $id) = $request =~ m!^POST (?:/($spaces))?/do/edit/([^/#?]+) HTTP/1\.[01]$! | ||
| 82 | and is_editable($space) | ||
| 83 | and ($host) = $headers->{host} =~ m!^($host_regex)(?::$port)$!) { | ||
| 84 | save_edit_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $headers, $buffer); | ||
| 85 | } else { | ||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub serve_edit_via_http { | ||
| 92 | my ($stream, $host, $space, $id) = @_; | ||
| 93 | $log->info("Serve edit page for $id via HTTP"); | ||
| 94 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 95 | $stream->write("Content-Type: text/html\r\n"); | ||
| 96 | $stream->write("\r\n"); | ||
| 97 | $stream->write("<!DOCTYPE html>\n"); | ||
| 98 | $stream->write("<html>\n"); | ||
| 99 | $stream->write("<head>\n"); | ||
| 100 | $stream->write("<meta charset=\"utf-8\">\n"); | ||
| 101 | $stream->write(encode_utf8 "<title>" . quote_html($id) . "</title>\n"); | ||
| 102 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n"); | ||
| 103 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"); | ||
| 104 | $stream->write("</head>\n"); | ||
| 105 | $stream->write("<body>\n"); | ||
| 106 | $stream->write(encode_utf8 "<h1>" . quote_html($id) . "</h1>\n"); | ||
| 107 | $stream->write("<form method=\"POST\">\n"); | ||
| 108 | if (@{$server->{wiki_token}} > 0) { | ||
| 109 | $stream->write("<p><label for=\"token\">Token:</label>\n"); | ||
| 110 | $stream->write("<br><input type=\"text\" id=\"token\" name=\"token\" required>\n"); | ||
| 111 | } | ||
| 112 | $stream->write("<p><label for=\"text\">Text:</label>\n"); | ||
| 113 | my $text = text($stream, $host, $space, $id); | ||
| 114 | # textarea can be empty in order to delete a page | ||
| 115 | $stream->write(encode_utf8 "<br><textarea style=\"width: 100%; height: 20em;\" id=\"text\" name=\"text\">$text</textarea>\n"); | ||
| 116 | $stream->write("<p><input type=\"submit\" value=\"Save\">\n"); | ||
| 117 | $stream->write("</form>\n"); | ||
| 118 | $stream->write("</body>\n"); | ||
| 119 | $stream->write("</html>\n"); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub save_edit_via_http { | ||
| 123 | my ($stream, $host, $space, $id, $headers, $buffer) = @_; | ||
| 124 | $log->info("Save edit for $id via HTTP"); | ||
| 125 | return http_error($stream, "Page name is missing") unless $id; | ||
| 126 | return http_error($stream, "Page names must not control characters") if $id =~ /[[:cntrl:]]/; | ||
| 127 | return http_error($stream, "Content type not known") | ||
| 128 | if not $headers->{"content-type"} or $headers->{"content-type"} ne "application/x-www-form-urlencoded"; | ||
| 129 | my %params; | ||
| 130 | for (split(/&/, $buffer)) { | ||
| 131 | my ($key, $value) = map { s/\+/ /g; decode_utf8(uri_unescape($_)) } split(/=/, $_, 2); | ||
| 132 | $params{$key} = $value; | ||
| 133 | } | ||
| 134 | $log->debug("Parameters: " . join(", ", map { "$_ => '$params{$_}'" } keys %params)); | ||
| 135 | my $token = quotemeta($params{token}||""); | ||
| 136 | my @tokens = @{$server->{wiki_token}}; | ||
| 137 | push(@tokens, @{$server->{wiki_space_token}->{$space}}) | ||
| 138 | if $space and $server->{wiki_space_token}->{$space}; | ||
| 139 | if (@tokens) { | ||
| 140 | if (not $token) { | ||
| 141 | $log->info("Token required (one of @tokens)"); | ||
| 142 | http_error($stream, "Token required"); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | if (not grep(/^$token$/, @tokens)) { | ||
| 146 | $log->info("Wrong token ($token)"); | ||
| 147 | http_error($stream, "Wrong token"); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | my $text = $params{text}||""; | ||
| 152 | $text =~ s/\r\n/\n/g; # fix DOS EOL convention | ||
| 153 | # We don't need to close the stream because this is called via process_gemini | ||
| 154 | # which always closes the stream in the end. | ||
| 155 | with_lock($stream, $host, $space, sub { write_page_for_http($stream, $host, $space, $id, $text) } ); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub write_page_for_http { | ||
| 159 | my $stream = shift; | ||
| 160 | my $host = shift; | ||
| 161 | my $space = shift; | ||
| 162 | my $id = shift; | ||
| 163 | my $text = shift; | ||
| 164 | $log->info("Writing page $id"); | ||
| 165 | my $dir = wiki_dir($host, $space); | ||
| 166 | my $file = "$dir/page/$id.gmi"; | ||
| 167 | my $revision = 0; | ||
| 168 | if (-e $file) { | ||
| 169 | my $old = read_text($file); | ||
| 170 | if ($old eq $text) { | ||
| 171 | $log->info("$id is unchanged"); | ||
| 172 | my $message = to_url($stream, $host, $space, "page/$id", "https"); | ||
| 173 | $stream->write("HTTP/1.1 302 Found\r\n"); | ||
| 174 | $stream->write("Location: $message\r\n"); | ||
| 175 | $stream->write("\r\n"); | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | mkdir "$dir/keep" unless -d "$dir/keep"; | ||
| 179 | if (-d "$dir/keep/$id") { | ||
| 180 | foreach (read_dir("$dir/keep/$id")) { | ||
| 181 | $revision = $1 if m/^(\d+)\.gmi$/ and $1 > $revision; | ||
| 182 | } | ||
| 183 | $revision++; | ||
| 184 | } else { | ||
| 185 | mkdir "$dir/keep/$id"; | ||
| 186 | $revision = 1; | ||
| 187 | } | ||
| 188 | rename $file, "$dir/keep/$id/$revision.gmi"; | ||
| 189 | } else { | ||
| 190 | my $index = "$dir/index"; | ||
| 191 | if (not open(my $fh, ">>:encoding(UTF-8)", $index)) { | ||
| 192 | $log->error("Cannot write index $index: $!"); | ||
| 193 | return http_error($stream, "Unable to write index"); | ||
| 194 | } else { | ||
| 195 | say $fh $id; | ||
| 196 | close($fh); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | my $changes = "$dir/changes.log"; | ||
| 200 | if (not open(my $fh, ">>:encoding(UTF-8)", $changes)) { | ||
| 201 | $log->error("Cannot write log $changes: $!"); | ||
| 202 | return http_error($stream, "Unable to write log"); | ||
| 203 | } else { | ||
| 204 | my $peerhost = $stream->handle->peerhost; | ||
| 205 | say $fh join("\x1f", scalar(time), $id, $revision + 1, bogus_hash($peerhost)); | ||
| 206 | close($fh); | ||
| 207 | } | ||
| 208 | mkdir "$dir/page" unless -d "$dir/page"; | ||
| 209 | eval { write_text($file, $text) }; | ||
| 210 | if ($@) { | ||
| 211 | $log->error("Unable to save $id: $@"); | ||
| 212 | return http_error($stream, "Unable to save $id"); | ||
| 213 | } else { | ||
| 214 | $log->info("Wrote $id"); | ||
| 215 | my $message = to_url($stream, $host, $space, "page/$id", "https"); | ||
| 216 | $stream->write("HTTP/1.1 302 Found\r\n"); | ||
| 217 | $stream->write("Location: $message\r\n"); | ||
| 218 | $stream->write("\r\n"); | ||
| 219 | } | ||
| 220 | } |