gopher-server.pl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. #!/usr/bin/env perl
  2. # Copyright (C) 2017–2018 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. package OddMuse;
  15. use strict;
  16. use 5.10.0;
  17. use base qw(Net::Server::Fork); # any personality will do
  18. use MIME::Base64;
  19. use Text::Wrap;
  20. use List::Util qw(first);
  21. our($RunCGI, $DataDir, %IndexHash, @IndexList, $IndexFile, $TagFile, $q,
  22. %Page, $OpenPageName, $MaxPost, $ShowEdits, %Locks, $CommentsPattern,
  23. $CommentsPrefix, $EditAllowed, $NoEditFile, $SiteName, $ScriptName);
  24. my $external_image_path = '/home/alex/alexschroeder.ch/pics/';
  25. # Sadly, we need this information before doing anything else
  26. my %args = (proto => 'ssl');
  27. for (grep(/--wiki_(key|cert)_file=/, @ARGV)) {
  28. $args{SSL_cert_file} = $1 if /--wiki_cert_file=(.*)/;
  29. $args{SSL_key_file} = $1 if /--wiki_key_file=(.*)/;
  30. }
  31. if ($args{SSL_cert_file} and not $args{SSL_key_file}
  32. or not $args{SSL_cert_file} and $args{SSL_key_file}) {
  33. die "I must have both --wiki_key_file and --wiki_cert_file\n";
  34. } elsif ($args{SSL_cert_file} and $args{SSL_key_file}) {
  35. OddMuse->run(%args);
  36. } else {
  37. OddMuse->run;
  38. }
  39. sub options {
  40. my $self = shift;
  41. my $prop = $self->{'server'};
  42. my $template = shift;
  43. # setup options in the parent classes
  44. $self->SUPER::options($template);
  45. # add a single value option
  46. $prop->{wiki} ||= undef;
  47. $template->{wiki} = \$prop->{wiki};
  48. $prop->{wiki_dir} ||= undef;
  49. $template->{wiki_dir} = \$prop->{wiki_dir};
  50. $prop->{wiki_pages} ||= [];
  51. $template->{wiki_pages} = $prop->{wiki_pages};
  52. $prop->{menu} ||= [];
  53. $template->{menu} = $prop->{menu};
  54. $prop->{menu_file} ||= [];
  55. $template->{menu_file} = $prop->{menu_file};
  56. # $prop->{wiki_pem_file} ||= undef;
  57. # $template->{wiki_pem_file} = $prop->{wiki_pem_file};
  58. }
  59. sub post_configure_hook {
  60. my $self = shift;
  61. $self->write_help if $ARGV[0] eq '--help';
  62. $DataDir = $self->{server}->{wiki_dir} || $ENV{WikiDataDir} || '/tmp/oddmuse';
  63. $self->log(3, "PID $$");
  64. $self->log(3, "Host " . ("@{$self->{server}->{host}}" || "*"));
  65. $self->log(3, "Port @{$self->{server}->{port}}");
  66. $self->log(3, "Wiki data dir is $DataDir\n");
  67. $RunCGI = 0;
  68. my $wiki = $self->{server}->{wiki} || "./wiki.pl";
  69. $self->log(1, "Running $wiki\n");
  70. unless (my $return = do $wiki) {
  71. $self->log(1, "couldn't parse wiki library $wiki: $@") if $@;
  72. $self->log(1, "couldn't do wiki library $wiki: $!") unless defined $return;
  73. $self->log(1, "couldn't run wiki library $wiki") unless $return;
  74. }
  75. # make sure search is sorted newest first because NewTagFiltered resorts
  76. *OldGopherFiltered = \&Filtered;
  77. *Filtered = \&NewGopherFiltered;
  78. }
  79. my $usage = << 'EOT';
  80. This server serves a wiki as a gopher site.
  81. It implements Net::Server and thus all the options available to
  82. Net::Server are also available here. Additional options are available:
  83. wiki - this is the path to the Oddmuse script
  84. wiki_dir - this is the path to the Oddmuse data directory
  85. wiki_pages - this is a page to show on the entry menu
  86. menu - this is the description of a gopher menu to prepend
  87. menu_file - this is the filename of the gopher menu to prepend
  88. wiki_cert_file - the filename containing a certificate in PEM format
  89. wiki_key_file - the filename containing a private key in PEM format
  90. For many of the options, more information can be had in the Net::Server
  91. documentation. This is important if you want to daemonize the server. You'll
  92. need to use --pid_file so that you can stop it using a script, --setsid to
  93. daemonize it, --log_file to write keep logs, and you'll net to set the user or
  94. group using --user or --group such that the server has write access to the data
  95. directory.
  96. For testing purposes, you can start with the following:
  97. --port=7070
  98. The port to listen to, defaults to a random port.
  99. --log_level=4
  100. The log level to use, defaults to 2.
  101. --wiki_dir=/var/oddmuse
  102. The wiki directory, defaults to the value of the "WikiDataDir" environment
  103. variable or "/tmp/oddmuse".
  104. --wiki_lib=/home/alex/src/oddmuse/wiki.pl
  105. The Oddmuse main script, defaults to "./wiki.pl".
  106. --wiki_pages=SiteMap
  107. This adds a page to the main index. Can be used multiple times.
  108. --help
  109. Prints this message.
  110. Example invocation:
  111. /home/alex/src/oddmuse/stuff/gopher-server.pl \
  112. --port=7070 \
  113. --wiki=/home/alex/src/oddmuse/wiki.pl \
  114. --pid_file=/tmp/oddmuse/gopher.pid \
  115. --wiki_dir=/tmp/oddmuse \
  116. --wiki_pages=Homepage \
  117. --wiki_pages=Gopher
  118. Run the script and test it:
  119. echo | nc localhost 7070
  120. lynx gopher://localhost:7070
  121. If you want to use SSL, you need to provide PEM files containing certificate and
  122. private key. To create self-signed files, for example:
  123. openssl req -new -x509 -days 365 -nodes -out \
  124. gopher-server-cert.pem -keyout gopher-server-key.pem
  125. Make sure the common name you provide matches your domain name!
  126. Note that parameters should not contain spaces. Thus:
  127. /home/alex/src/oddmuse/stuff/gopher-server.pl \
  128. --port=7070 \
  129. --log_level=3 \
  130. --wiki=/home/alex/src/oddmuse/wiki.pl \
  131. --wiki_dir=/home/alex/alexschroeder \
  132. --menu=Moku_Pona_Updates \
  133. --menu_file=~/.moku-pona/updates.txt \
  134. --menu=Moku_Pona_Sites \
  135. --menu_file=~/.moku-pona/sites.txt
  136. EOT
  137. run();
  138. sub NewGopherFiltered {
  139. my @pages = OldGopherFiltered(@_);
  140. @pages = sort newest_first @pages;
  141. return @pages;
  142. }
  143. sub normal_to_free {
  144. my $title = shift;
  145. $title =~ s/_/ /g;
  146. return $title;
  147. }
  148. sub print_text {
  149. my $self = shift;
  150. my $text = shift;
  151. print($text); # bytes
  152. }
  153. sub print_menu {
  154. my $self = shift;
  155. my $display = shift;
  156. my $selector = shift;
  157. my $host = shift
  158. || $self->{server}->{host}->[0]
  159. || $self->{server}->{sockaddr};
  160. my $port = shift
  161. || $self->{server}->{port}->[0]
  162. || $self->{server}->{sockport};
  163. my $encoded = shift;
  164. $selector = join('/', map { UrlEncode($_) } split(/\//, $selector)) unless $encoded;
  165. $self->print_text(join("\t", $display, $selector, $host, $port)
  166. . "\r\n");
  167. }
  168. sub print_info {
  169. my $self = shift;
  170. my $info = shift;
  171. $self->print_menu("i$info", "");
  172. }
  173. sub print_error {
  174. my $self = shift;
  175. my $error = shift;
  176. $self->print_menu("3$error", "");
  177. }
  178. sub serve_main_menu {
  179. my $self = shift;
  180. my $more = shift;
  181. $self->log(3, "Serving main menu");
  182. $self->print_info("Welcome to the Gopher version of this wiki.");
  183. $self->print_info("");
  184. $self->print_info("Phlog:");
  185. my @pages = sort { $b cmp $a } grep(/^\d\d\d\d-\d\d-\d\d/, @IndexList);
  186. for my $id (@pages[0..9]) {
  187. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  188. }
  189. $self->print_menu("1" . "More...", "do/more");
  190. $self->print_info("");
  191. for my $id (@{$self->{server}->{wiki_pages}}) {
  192. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  193. }
  194. for my $id (@{$self->{server}->{menu}}) {
  195. $self->print_menu("1" . normal_to_free($id), "map/$id");
  196. }
  197. $self->print_menu("1" . "Recent Changes", "do/rc");
  198. $self->print_menu("0" . "Gopher RSS", "do/rss");
  199. $self->print_menu("7" . "Find matching page titles", "do/match");
  200. $self->print_menu("7" . "Full text search", "do/search");
  201. $self->print_menu("1" . "Index of all pages", "do/index");
  202. if ($TagFile) {
  203. $self->print_menu("1" . "Index of all tags", "do/tags");
  204. }
  205. if ($EditAllowed and not IsFile($NoEditFile)) {
  206. $self->print_menu("w" . "New page", "do/new");
  207. }
  208. }
  209. sub serve_phlog_archive {
  210. my $self = shift;
  211. $self->log(3, "Serving phlog archive");
  212. my @pages = sort { $b cmp $a } grep(/^\d\d\d\d-\d\d-\d\d/, @IndexList);
  213. for my $id (@pages) {
  214. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  215. }
  216. }
  217. sub serve_index {
  218. my $self = shift;
  219. $self->log(3, "Serving index of all pages");
  220. for my $id (sort newest_first @IndexList) {
  221. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  222. }
  223. }
  224. sub serve_match {
  225. my $self = shift;
  226. my $match = shift;
  227. $self->log(3, "Serving pages matching " . UrlEncode($match));
  228. $self->print_info("Use a regular expression to match page titles.");
  229. $self->print_info("Spaces in page titles are underlines, '_'.");
  230. for my $id (sort newest_first grep(/$match/i, @IndexList)) {
  231. $self->print_menu( "1" . normal_to_free($id), "$id/menu");
  232. }
  233. }
  234. sub serve_search {
  235. my $self = shift;
  236. my $str = shift;
  237. $self->log(3, "Serving search result for " . UrlEncode($str));
  238. $self->print_info("Use regular expressions separated by spaces.");
  239. SearchTitleAndBody($str, sub {
  240. my $id = shift;
  241. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  242. });
  243. }
  244. sub serve_tags {
  245. my $self = shift;
  246. $self->log(3, "Serving tag cloud");
  247. # open the DB file
  248. my %h = TagReadHash();
  249. my %count = ();
  250. foreach my $tag (grep !/^_/, keys %h) {
  251. $count{$tag} = @{$h{$tag}};
  252. }
  253. foreach my $id (sort { $count{$b} <=> $count{$a} } keys %count) {
  254. $self->print_menu("1" . normal_to_free($id), "$id/tag");
  255. }
  256. }
  257. sub serve_rc {
  258. my $self = shift;
  259. my $showedit = $ShowEdits = shift;
  260. $self->log(3, "Serving recent changes"
  261. . ($showedit ? " including minor changes" : ""));
  262. $self->print_info("Recent Changes");
  263. if ($showedit) {
  264. $self->print_menu("1" . "Skip minor edits", "do/rc");
  265. } else {
  266. $self->print_menu("1" . "Show minor edits", "do/rc/showedits");
  267. }
  268. ProcessRcLines(
  269. sub {
  270. my $date = shift;
  271. $self->print_info("");
  272. $self->print_info("$date");
  273. $self->print_info("");
  274. },
  275. sub {
  276. my($id, $ts, $author_host, $username, $summary, $minor, $revision,
  277. $languages, $cluster, $last) = @_;
  278. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  279. for my $line (split(/\n/, wrap(' ', ' ', $summary))) {
  280. $self->print_info($line);
  281. }
  282. });
  283. }
  284. sub serve_rss {
  285. my $self = shift;
  286. $self->log(3, "Serving Gopher RSS");
  287. my $host = shift
  288. || $self->{server}->{host}->[0]
  289. || $self->{server}->{sockaddr};
  290. my $port = shift
  291. || $self->{server}->{port}->[0]
  292. || $self->{server}->{sockport};
  293. my $gopher = "gopher://$host:$port/"; # use gophers for TLS?
  294. local $ScriptName = $gopher;
  295. my $rss = GetRcRss();
  296. $rss =~ s!$ScriptName\?action=rss!${gopher}1do/rss!g;
  297. $rss =~ s!$ScriptName\?action=history;id=([^[:space:]<]*)!${gopher}1$1/history!g;
  298. $rss =~ s!$ScriptName/([^[:space:]<]*)!${gopher}0$1!g;
  299. $rss =~ s!<wiki:diff>.*</wiki:diff>\n!!g;
  300. print $rss;
  301. }
  302. sub serve_map {
  303. my $self = shift;
  304. my $id = shift;
  305. $self->log(3, "Serving map " . UrlEncode($id));
  306. my @menu = @{$self->{server}->{menu}};
  307. my $i = first { $id eq $menu[$_] } 0..$#menu;
  308. my $file = $self->{server}->{menu_file}->[$i];
  309. if (-f $file and open(my $fh, '<:encoding(UTF-8)', $file)) {
  310. local $/ = undef;
  311. my $text = <$fh>;
  312. $self->log(4, "Map has " . length($text) . " characters");
  313. $self->print_text($text);
  314. } else {
  315. $self->log(1, "Error reading $file");
  316. }
  317. }
  318. sub serve_page_comment_link {
  319. my $self = shift;
  320. my $id = shift;
  321. my $revision = shift;
  322. if (not $revision and $CommentsPattern) {
  323. if ($id =~ /$CommentsPattern/) {
  324. my $original = $1;
  325. # sometimes we are on a comment page and cannot derive the original
  326. $self->print_menu("1" . "Back to the original page",
  327. "$original/menu") if $original;
  328. $self->print_menu("w" . "Add a comment", "$id/append/text");
  329. } else {
  330. my $comments = $CommentsPrefix . $id;
  331. $self->print_menu("1" . "Comments on this page", "$comments/menu");
  332. }
  333. }
  334. }
  335. sub serve_page_history_link {
  336. my $self = shift;
  337. my $id = shift;
  338. my $revision = shift;
  339. if (not $revision) {
  340. $self->print_menu("1" . "Page History", "$id/history");
  341. }
  342. }
  343. sub serve_file_page_menu {
  344. my $self = shift;
  345. my $id = shift;
  346. my $type = shift;
  347. my $revision = shift;
  348. my $code = substr($type, 0, 6) eq 'image/' ? 'I' : '9';
  349. $self->log(3, "Serving file page menu for " . UrlEncode($id));
  350. $self->print_menu($code . normal_to_free($id)
  351. . ($revision ? "/$revision" : ""), $id);
  352. $self->serve_page_comment_link($id, $revision);
  353. $self->serve_page_history_link($id, $revision);
  354. }
  355. sub serve_text_page_menu {
  356. my $self = shift;
  357. my $id = shift;
  358. my $page = shift;
  359. my $revision = shift;
  360. $self->log(3, "Serving text page menu for " . UrlEncode($id)
  361. . ($revision ? "/$revision" : ""));
  362. $self->print_info("The text of this page:");
  363. $self->print_menu("0" . normal_to_free($id),
  364. $id . ($revision ? "/$revision" : ""));
  365. $self->print_menu("h" . normal_to_free($id),
  366. $id . ($revision ? "/$revision" : "") . "/html");
  367. $self->print_menu("w" . "Replace " . normal_to_free($id),
  368. $id . "/write/text");
  369. $self->serve_page_comment_link($id, $revision);
  370. $self->serve_page_history_link($id, $revision);
  371. my $first = 1;
  372. while ($page->{text} =~ /\[\[([^\]|]*)(?:\|([^\]]*))?\]\]|\[(https?:\/\/\S+)\s+([^\]]*)\]|\[gopher:\/\/([^:\/]*)(?::(\d+))?(?:\/(\d)(\S+))?\s+([^\]]+)\]/g) {
  373. my ($title, $text, $url, $hostname, $port, $type, $selector)
  374. = ($1, $2||$4||$9, $3, $5, $6||70, $7||1, $8);
  375. if ($first) {
  376. $self->print_info("");
  377. $self->print_info("Links leaving " . normal_to_free($id) . ":");
  378. $first = 0;
  379. }
  380. if ($hostname) {
  381. $self->print_text(join("\t", $type . $text, $selector, $hostname, $port) . "\r\n");
  382. } elsif ($url) {
  383. $self->print_menu("h$text", "URL:" . $url, undef, undef, 1);
  384. } elsif ($title and substr($title, 0, 4) eq 'tag:') {
  385. $self->print_menu("1" . ($text||substr($title, 4)),
  386. substr($title, 4) . "/tag");
  387. } elsif ($title =~ s!^image[/a-z]* external:!pics/!) {
  388. $self->print_menu("I" . $text||$title, $title);
  389. } elsif ($title) {
  390. $title =~ s!^image[/a-z]*:!!i;
  391. $self->print_menu("1" . ($text||$title), $title . "/menu");
  392. }
  393. }
  394. $first = 1;
  395. while ($page->{text} =~ /\[https?:\/\/gopher\.floodgap\.com\/gopher\/gw\?a=gopher%3a%2f%2f(.*?)(?:%3a(\d+))?%2f(.)(\S+)\s+([^\]]+)\]/gi) {
  396. my ($hostname, $port, $type, $selector, $text) = ($1, $2||"70", $3, $4, $5);
  397. if ($first) {
  398. $self->print_info("");
  399. $self->print_info("Gopher links (via Floodgap):");
  400. $first = 0;
  401. }
  402. $selector =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/eig; # url unescape
  403. $self->print_text(join("\t", $type . $text, $selector, $hostname, $port)
  404. . "\r\n");
  405. }
  406. if ($page->{text} =~ m/<journal search tag:(\S+)>\s*/) {
  407. my $tag = $1;
  408. $self->print_info("");
  409. $self->serve_tag_list($tag);
  410. }
  411. }
  412. sub serve_page_history {
  413. my $self = shift;
  414. my $id = shift;
  415. $self->log(3, "Serving history of " . UrlEncode($id));
  416. OpenPage($id);
  417. $self->print_menu("1" . normal_to_free($id) . " (current)", "$id/menu");
  418. $self->print_info(CalcTime($Page{ts})
  419. . " by " . GetAuthor($Page{username})
  420. . ($Page{summary} ? ": $Page{summary}" : "")
  421. . ($Page{minor} ? " (minor)" : ""));
  422. foreach my $revision (GetKeepRevisions($OpenPageName)) {
  423. my $keep = GetKeptRevision($revision);
  424. $self->print_menu("1" . normal_to_free($id) . " ($keep->{revision})",
  425. "$id/$keep->{revision}/menu");
  426. $self->print_info(CalcTime($keep->{ts})
  427. . " by " . GetAuthor($keep->{username})
  428. . ($keep->{summary} ? ": $keep->{summary}" : "")
  429. . ($keep->{minor} ? " (minor)" : ""));
  430. }
  431. }
  432. sub get_page {
  433. my $id = shift;
  434. my $revision = shift;
  435. my $page;
  436. if ($revision) {
  437. $OpenPageName = $id;
  438. $page = GetKeptRevision($revision);
  439. } else {
  440. OpenPage($id);
  441. $page = \%Page;
  442. }
  443. return $page;
  444. }
  445. sub serve_page_menu {
  446. my $self = shift;
  447. my $id = shift;
  448. my $revision = shift;
  449. my $page = get_page($id, $revision);
  450. if (my ($type) = TextIsFile($page->{text})) {
  451. $self->serve_file_page_menu($id, $type, $revision);
  452. } else {
  453. $self->serve_text_page_menu($id, $page, $revision);
  454. }
  455. }
  456. sub serve_file_page {
  457. my $self = shift;
  458. my $id = shift;
  459. my $page = shift;
  460. $self->log(3, "Serving " . UrlEncode($id) . " as file");
  461. my ($encoded) = $page->{text} =~ /^[^\n]*\n(.*)/s;
  462. $self->log(4, UrlEncode($id) . " has " . length($encoded)
  463. . " bytes of MIME encoded data");
  464. my $data = decode_base64($encoded);
  465. $self->log(4, UrlEncode($id) . " has " . length($data)
  466. . " bytes of binary data");
  467. binmode(STDOUT, ":raw");
  468. print($data);
  469. }
  470. sub serve_text_page {
  471. my $self = shift;
  472. my $id = shift;
  473. my $page = shift;
  474. my $text = $page->{text};
  475. $self->log(3, "Serving " . UrlEncode($id) . " as " . length($text)
  476. . " bytes of text");
  477. $text =~ s/^\./../mg;
  478. $self->print_text($text);
  479. }
  480. sub serve_page {
  481. my $self = shift;
  482. my $id = shift;
  483. my $revision = shift;
  484. my $page = get_page($id, $revision);
  485. if (my ($type) = TextIsFile($page->{text})) {
  486. $self->serve_file_page($id, $page);
  487. } else {
  488. $self->serve_text_page($id, $page);
  489. }
  490. }
  491. sub serve_page_html {
  492. my $self = shift;
  493. my $id = shift;
  494. my $revision = shift;
  495. my $page = get_page($id, $revision);
  496. $self->log(3, "Serving " . UrlEncode($id) . " as HTML");
  497. my $title = normal_to_free($id);
  498. print GetHtmlHeader(Ts('%s:', $SiteName) . ' ' . UnWiki($title), $id);
  499. print GetHeaderDiv($id, $title);
  500. print $q->start_div({-class=>'wrapper'});
  501. if ($revision) {
  502. # no locking of the file, no updating of the cache
  503. PrintWikiToHTML($page->{text});
  504. } else {
  505. PrintPageHtml();
  506. }
  507. PrintFooter($id, $revision);
  508. }
  509. sub serve_redirect {
  510. my $self = shift;
  511. my $url = shift;
  512. print qq{<!DOCTYPE HTML>
  513. <html lang="en-US">
  514. <head>
  515. <meta http-equiv="refresh" content="0; url=$url">
  516. <title>Redirection</title>
  517. </head>
  518. <body>
  519. If you are not redirected automatically, follow this <a href='$url'>link</a>.
  520. </body>
  521. </html>
  522. };
  523. }
  524. sub serve_image {
  525. my $self = shift;
  526. my $pic = shift;
  527. my $file = $external_image_path . $pic;
  528. # no tricks
  529. if ($file !~ /\.\./ and $file !~ /\/\//
  530. and -f $file and open(my $fh, "<", $file)) {
  531. local $/ = undef;
  532. my $data = <$fh>;
  533. $self->log(4, $pic . " has " . length($data)
  534. . " bytes of binary data");
  535. binmode(STDOUT, ":raw");
  536. print($data);
  537. } else {
  538. $self->log(1, "Error reading $file: $!");
  539. }
  540. }
  541. sub newest_first {
  542. my ($A, $B) = ($a, $b);
  543. if ($A =~ /^\d\d\d\d-\d\d-\d\d/ and $B =~ /^\d\d\d\d-\d\d-\d\d/) {
  544. return $B cmp $A;
  545. }
  546. $A cmp $B;
  547. }
  548. sub serve_tag_list {
  549. my $self = shift;
  550. my $tag = shift;
  551. $self->print_info("Search result for tag $tag:");
  552. for my $id (sort newest_first TagFind($tag)) {
  553. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  554. }
  555. }
  556. sub serve_tag {
  557. my $self = shift;
  558. my $tag = shift;
  559. $self->log(3, "Serving tag " . UrlEncode($tag));
  560. if ($IndexHash{$tag}) {
  561. $self->print_info("This page is about the tag $tag.");
  562. $self->print_menu("1" . normal_to_free($tag), "$tag/menu");
  563. $self->print_info("");
  564. }
  565. $self->serve_tag_list($tag);
  566. }
  567. sub serve_error {
  568. my $self = shift;
  569. my $id = shift;
  570. my $error = shift;
  571. $self->log(3, "Error ('" . UrlEncode($id) . "'): $error");
  572. $self->print_error("Error ('" . UrlEncode($id) . "'): $error");
  573. }
  574. sub write_help {
  575. my $self = shift;
  576. my @lines = split(/\n/, <<"EOF");
  577. This is how your document should start:
  578. ```
  579. username: Alex Schroeder
  580. summary: typo fixed
  581. ```
  582. This is the text of your document.
  583. Just write whatever.
  584. Note the space after the colon for metadata fields.
  585. More metadata fields are allowed:
  586. `minor` is 1 if this is a minor edit. The default is 0.
  587. EOF
  588. for my $line (@lines) {
  589. $self->print_info($line);
  590. }
  591. }
  592. sub write_page_ok {
  593. my $self = shift;
  594. my $id = shift;
  595. $self->print_info("Page was saved.");
  596. $self->print_menu("1" . normal_to_free($id), "$id/menu");
  597. }
  598. sub write_page_error {
  599. my $self = shift;
  600. my $error = shift;
  601. $self->log(4, "Not saved: $error");
  602. $self->print_error("Page was not saved: $error");
  603. map { ReleaseLockDir($_); } keys %Locks;
  604. }
  605. sub write_data {
  606. my $self = shift;
  607. my $id = shift;
  608. my $data = shift;
  609. my $param = shift||'text';
  610. SetParam($param, $data);
  611. my $error;
  612. eval {
  613. local *ReBrowsePage = sub {};
  614. local *ReportError = sub { $error = shift };
  615. DoPost($id);
  616. };
  617. if ($error) {
  618. $self->write_page_error($error);
  619. } else {
  620. $self->write_page_ok($id);
  621. }
  622. }
  623. sub write_file_page {
  624. my $self = shift;
  625. my $id = shift;
  626. my $data = shift;
  627. my $type = shift || 'application/octet-stream';
  628. $self->write_page_error("page title is missing") unless $id;
  629. $self->log(3, "Posting " . length($data) . " bytes of $type to page "
  630. . UrlEncode($id));
  631. # no metadata
  632. $self->write_data($id, "#FILE $type\n" . encode_base64($data));
  633. }
  634. sub write_text {
  635. my $self = shift;
  636. my $id = shift;
  637. my $data = shift;
  638. my $param = shift;
  639. utf8::decode($data);
  640. my ($lead, $meta, $text) = split(/^```\s*(?:meta)?\n/m, $data, 3);
  641. if (not $lead and $meta) {
  642. while ($meta =~ /^([a-z-]+): (.*)/mg) {
  643. if ($1 eq 'minor' and $2) {
  644. SetParam('recent_edit', 'on'); # legacy UseMod parameter name
  645. } else {
  646. SetParam($1, $2);
  647. if ($1 eq "title") {
  648. $id = $2;
  649. }
  650. }
  651. }
  652. $self->log(3, ($param eq 'text' ? "Posting" : "Appending")
  653. . " " . length($text) . " characters (with metadata) to page $id");
  654. $self->write_data($id, $text, $param);
  655. } else {
  656. # no meta data
  657. $self->log(3, ($param eq 'text' ? "Posting" : "Appending")
  658. . " " . length($data) . " characters to page $id") if $id;
  659. $self->write_data($id, $data, $param);
  660. }
  661. }
  662. sub write_text_page {
  663. my $self = shift;
  664. $self->write_text(@_, 'text');
  665. }
  666. sub append_text_page {
  667. my $self = shift;
  668. $self->write_text(@_, 'aftertext');
  669. }
  670. sub read_file {
  671. my $self = shift;
  672. my $length = shift;
  673. $length = $MaxPost if $length > $MaxPost;
  674. local $/ = \$length;
  675. my $buf .= <STDIN>;
  676. $self->log(4, "Received " . length($buf) . " bytes (max is $MaxPost)");
  677. return $buf;
  678. }
  679. sub read_text {
  680. my $self = shift;
  681. my $buf;
  682. while (1) {
  683. my $line = <STDIN>;
  684. if (length($line) == 0) {
  685. sleep(1); # wait for input
  686. next;
  687. }
  688. last if $line =~ /^.\r?\n/m;
  689. $buf .= $line;
  690. if (length($buf) > $MaxPost) {
  691. $buf = substr($buf, 0, $MaxPost);
  692. last;
  693. }
  694. }
  695. $self->log(4, "Received " . length($buf) . " bytes (max is $MaxPost)");
  696. utf8::decode($buf);
  697. $self->log(4, "Received " . length($buf) . " characters");
  698. return $buf;
  699. }
  700. sub process_request {
  701. my $self = shift;
  702. # clear cookie and all that
  703. $q = undef;
  704. Init();
  705. # refresh list of pages
  706. if (IsFile($IndexFile) and ReadIndex()) {
  707. # we're good
  708. } else {
  709. RefreshIndex();
  710. }
  711. eval {
  712. local $SIG{'ALRM'} = sub {
  713. $self->log(1, "Timeout!");
  714. die "Timed Out!\n";
  715. };
  716. alarm(10); # timeout
  717. my $selector = <STDIN>; # no loop
  718. $selector = UrlDecode($selector); # assuming URL-encoded UTF-8
  719. $selector =~ s/\s+$//g; # no trailing whitespace
  720. if (not $selector or $selector eq "/") {
  721. $self->serve_main_menu();
  722. } elsif ($selector eq "do/more") {
  723. $self->serve_phlog_archive();
  724. } elsif ($selector eq "do/index") {
  725. $self->serve_index();
  726. } elsif (substr($selector, 0, 9) eq "do/match\t") {
  727. $self->serve_match(substr($selector, 9));
  728. } elsif (substr($selector, 0, 10) eq "do/search\t") {
  729. $self->serve_search(substr($selector, 10));
  730. } elsif ($selector eq "do/tags") {
  731. $self->serve_tags();
  732. } elsif ($selector eq "do/rc") {
  733. $self->serve_rc(0);
  734. } elsif ($selector eq "do/rss") {
  735. $self->serve_rss(0);
  736. } elsif ($selector eq "do/rc/showedits") {
  737. $self->serve_rc(1);
  738. } elsif ($selector eq "do/new") {
  739. my $data = $self->read_text();
  740. $self->write_text_page(undef, $data);
  741. } elsif ($selector =~ m!^([^/]*)/(\d+)/menu$!) {
  742. $self->serve_page_menu($1, $2);
  743. } elsif ($selector =~ m!^map/(.*)!) {
  744. $self->serve_map($1);
  745. } elsif (substr($selector, -5) eq '/menu') {
  746. $self->serve_page_menu(substr($selector, 0, -5));
  747. } elsif ($selector =~ m!^([^/]*)/tag$!) {
  748. $self->serve_tag($1);
  749. } elsif ($selector =~ m!^([^/]*)(?:/(\d+))?/html!) {
  750. $self->serve_page_html($1, $2);
  751. } elsif ($selector =~ m!^([^/]*)/history$!) {
  752. $self->serve_page_history($1);
  753. } elsif ($selector =~ m!^([^/]*)/write/text$!) {
  754. my $data = $self->read_text();
  755. $self->write_text_page($1, $data);
  756. } elsif ($selector =~ m!^([^/]*)/append/text$!) {
  757. my $data = $self->read_text();
  758. $self->append_text_page($1, $data);
  759. } elsif ($selector =~ m!^([^/]*)(?:/([a-z]+/[-a-z]+))?/write/file(?:\t(\d+))?$!) {
  760. my $data = $self->read_file($3);
  761. $self->write_file_page($1, $data, $2);
  762. } elsif ($selector =~ m!^([^/]*)(?:/(\d+))?(?:/text)?$!) {
  763. $self->serve_page($1, $2);
  764. } elsif ($selector =~ m!^URL:(.*)!i) {
  765. $self->serve_redirect(UrlDecode($1));
  766. } elsif ($selector =~ m!^pics/(.*)!i) {
  767. $self->serve_image(UrlDecode($1));
  768. } else {
  769. $self->serve_error($selector, ValidId($selector)||'Cause unknown');
  770. }
  771. $self->log(4, "Done");
  772. }
  773. }