/[pub]/suikawiki/script/wiki.cgi
Suika

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (hide annotations) (download)
Sun Jan 12 09:15:06 2003 UTC (21 years, 10 months ago) by w
Branch: MAIN
Changes since 1.49: +131 -261 lines
Most of do_* are integred

1 wakaba 1.9 #!/usr/bin/perl
2 wakaba 1.16 # wiki.cgi - This is YukiWiki, yet another Wiki clone.
3 wakaba 1.9 #
4 wakaba 1.16 # This program is free software; you can redistribute it and/or
5     # modify it under the same terms as Perl itself.
6    
7     use strict;
8 w 1.44 use lib qw(./lib);
9 wakaba 1.16 use CGI::Carp qw(fatalsToBrowser);
10 w 1.50 our $VERSION = do{my @r=(q$Revision: 1.49 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
11     binmode STDOUT; binmode STDIN;
12 w 1.43 require 'wikidata/suikawiki-config.ph';
13 wakaba 1.16 use Fcntl;
14     ##############################
15 w 1.49 our %fmt; ## formatter objects
16 w 1.50 our %embed_command = (
17 wakaba 1.22 searched => '^\[\[#searched:([^\]]+)\]\]$',
18 w 1.43 form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/,
19 wakaba 1.22 );
20 w 1.43 our ($modifier_dbtype,$url_cgi,%uri,%PathTo,$use_exists);
21 w 1.48 our (%PageName,$kanjicode,$lang,%ViewDefinition);
22 w 1.43
23 wakaba 1.16 ##############################
24     my %form;
25 w 1.46 our %database;
26 w 1.47 our $database = bless {}, 'wiki::dummy';
27 wakaba 1.16 my %interwiki;
28     ##############################
29     my %command_do = (
30 w 1.50 read => \&do_view,
31 wakaba 1.16 adminchangepassword => \&do_adminchangepassword,
32     write => \&do_write,
33     searchform => \&do_searchform,
34     comment => \&do_comment,
35 wakaba 1.28 RandomJump => \&do_random_jump,
36 wakaba 1.41 wikiform => \&do_wikiform,
37 wakaba 1.16 );
38 wakaba 1.33 my $UA = ''; ## User agent name
39 wakaba 1.41 $| = 1;
40 wakaba 1.16 ##############################
41 wakaba 1.9
42     sub main {
43 wakaba 1.32 $UA = $main::ENV{HTTP_USER_AGENT};
44 wakaba 1.16 &open_db;
45     &init_form;
46     if ($command_do{$form{mycmd}}) {
47     &{$command_do{$form{mycmd}}};
48 wakaba 1.9 } else {
49 wakaba 1.42 &{$command_do{read}};
50 wakaba 1.9 }
51 wakaba 1.16 &close_db;
52 wakaba 1.9 }
53    
54 w 1.50 sub do_view {
55 wakaba 1.26 my $content = $database{$form{mypage}};
56 w 1.47 my $lm = $database->mtime ($form{mypage});
57 wakaba 1.27 wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
58 wakaba 1.32 wiki::useragent::add ($ENV{HTTP_USER_AGENT});
59 w 1.50 &load_formatter ('view');
60     my $view = $form{mycmd};
61     if ($view eq 'edit') {
62     $view = 'adminedit' if $form{admin};
63     } elsif ($view =~ /[^0-9A-Za-z]/) {
64     $view = 'view'
65     }
66     my ($magic, $content) = &SuikaWiki::Plugin::magic_and_content (undef, $content);
67     $magic ||= '#?SuikaWiki/0.9';
68     my $o = bless {param => \%form, page => $form{mypage}, toc => [],
69     magic => $magic, content => $content,
70     formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
71     if (!ref $ViewDefinition{$view} || !&{$ViewDefinition{$view}->{check}} ($o)) {
72     print "Status: 406 Unsupported Media Type\n";
73     $view = '-UnsupportedMediaType';
74     }
75     my $media = $ViewDefinition{$view}->{media};
76     if ($ViewDefinition{$view}->{xmedia} && $UA =~ /Gecko/) {
77     $media = $ViewDefinition{$view}->{xmedia};
78     $o->{media} = $media;
79     }
80 w 1.49 if ($magic =~ m!^\#\?SuikaWiki/0.9!) {
81 w 1.50 &print_header ($form{mypage}, -last_modified => ($magic =~ /interactive="yes"/ ? $lm : time),
82     -expires => ($magic =~ /interactive="yes"/ ? 1 : undef), o => $o,
83     -media => $media, -magic => $magic, content => $content);
84 wakaba 1.26 } else {
85 w 1.50 &print_header($form{mypage}, -media => $media,
86     -magic => $magic, -last_modified => $lm, o => $o);
87 wakaba 1.26 }
88 w 1.50 print $fmt{view}->replace ($ViewDefinition{$view}->{template} => $o);
89 wakaba 1.9 }
90    
91 w 1.50 sub _do_view_msg (%) {
92     my %option = @_;
93     &load_formatter ('view');
94     my $o = bless {param => \%form, page => $option{-page}, toc => [], condition => \%option,
95     formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
96     unless (&{$ViewDefinition{$option{-view}}->{check}} ($o)) {
97 wakaba 1.25 print "Status: 406 Unsupported Media Type\n";
98 w 1.50 $option{-view} = '-UnsupportedMediaType';
99 wakaba 1.25 }
100 w 1.50 my $media = $ViewDefinition{$option{-view}}->{media};
101     if ($ViewDefinition{$option{-view}}->{xmedia} && $UA =~ /Gecko/) {
102     $media = $ViewDefinition{$option{-view}}->{xmedia};
103     $o->{media} = $media;
104     }
105     &print_header($option{-page}, -media => $media, o => $o, -goto => $option{-goto});
106     print $fmt{view}->replace ($ViewDefinition{$option{-view}}->{template} => $o);
107 wakaba 1.25 }
108    
109 wakaba 1.32 sub id_and_name ($) {
110     my $name = shift;
111 wakaba 1.36 if ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer#) {
112 wakaba 1.32 qq{id="$name"><a name="$name"></a};
113     } else {
114     qq{id="$name"};
115     }
116     }
117    
118 wakaba 1.16 sub do_adminchangepassword {
119     if ($form{mynewpassword} ne $form{mynewpassword2}) {
120 w 1.50 &_do_view_msg (-view => '-error', -page => $form{mypage},
121     error_message => &Resource ('Error:PasswordMismatch'));
122     return;
123 wakaba 1.16 }
124 w 1.47 my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
125 wakaba 1.16 if ($validpassword_crypt) {
126     if (not &valid_password($form{myoldpassword})) {
127 w 1.50 &_do_view_msg (-view => '-error', -page => $form{mypage},
128     error_message => &Resource ('Error:PasswordIsIncorrect'));
129     return;
130 wakaba 1.9 }
131 wakaba 1.16 }
132     my ($sec, $min, $hour, $day, $mon, $year, $weekday) = localtime(time);
133     my (@token) = ('0'..'9', 'A'..'Z', 'a'..'z');
134     my $salt1 = $token[(time | $$) % scalar(@token)];
135     my $salt2 = $token[($sec + $min*60 + $hour*60*60) % scalar(@token)];
136     my $crypted = crypt($form{mynewpassword}, "$salt1$salt2");
137 w 1.47 $database->meta (AdminPassword => $PageName{AdminSpecialPage} => $crypted);
138 w 1.50
139     &_do_view_msg (-view => '-wrote', -page => $form{mypage});
140 wakaba 1.16 }
141    
142 w 1.45 sub valid_password ($) {
143 w 1.47 my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
144 w 1.45 return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0;
145     }
146    
147 wakaba 1.16 sub do_write {
148     if (&frozen_reject()) {
149     return;
150 wakaba 1.9 }
151    
152     if (not &is_editable($form{mypage})) {
153 w 1.50 &_do_view_msg (-view => '-error', -page => $form{mypage},
154     error_message => &Resource ('Error:ThisPageIsUneditable'));
155 wakaba 1.9 return;
156     }
157    
158 w 1.48 ## Check confliction
159 w 1.49 if ($form{myLastModified} ne $database->mtime ($form{mypage})) {
160 w 1.50 &_do_view_msg (-view => '-conflict', -page => $form{mypage});
161 w 1.48 return;
162 wakaba 1.9 }
163    
164     if ($form{mymsg}) {
165 w 1.46 if ($form{mytouch} || !ref $database) {
166     $database{$form{mypage}} = $form{mymsg};
167     } else {
168     $database->STORE ($form{mypage} => $form{mymsg}, -touch => 0);
169 wakaba 1.16 }
170 w 1.47 $database->meta (IsFrozen => $form{mypage} => 0 + $form{myfrozen});
171 wakaba 1.36 my $fragment = '';
172     $fragment .= qq(;after_edit_cmd=@{[&encode($form{after_edit_cmd})]}) if $form{after_edit_cmd};
173 wakaba 1.33 if ($form{__comment_anchor_index}) {
174 wakaba 1.36 $fragment .= qq(#anchor-$form{__comment_anchor_index});
175 wakaba 1.33 } elsif ($form{__wikiform_anchor_index}) {
176 wakaba 1.36 $fragment .= qq(#wikiform-$form{__wikiform_anchor_index});
177 wakaba 1.33 }
178 w 1.50 &_do_view_msg (-view => '-wrote', -page => $form{mypage}, -goto => $url_cgi.'?mycmd='.&encode($form{after_edit_cmd}||'read').';mypage='.&encode($form{mypage}).qq(;x-param=@{[time.[0..9]->[rand 10]]}$fragment));
179 wakaba 1.16 } else {
180     delete $database{$form{mypage}};
181 w 1.50 &_do_view_msg (-view => '-deleted', -page => $form{mypage});
182 wakaba 1.9 }
183     }
184    
185 w 1.48 sub _compatible_options () {
186     (use_anchor_name => ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer# ? 1 : 0));
187     }
188    
189 wakaba 1.22 sub get_search_result ($;%) {
190 wakaba 1.32 my $word = lc shift;
191 wakaba 1.22 my %option = @_;
192 wakaba 1.30 my @r;
193     foreach my $page (keys %database) {
194 wakaba 1.41 next if !$option{-match_myself} && ($page eq $word);
195     my $content = lc $database{$page};
196 wakaba 1.32 if (index (lc $page, $word) > -1) {
197 wakaba 1.41 my $c = $content =~ s/\Q$word\E//g;
198 wakaba 1.30 push @r, [$page, $c+20];
199 wakaba 1.32 } elsif (index ($word, lc $page) > -1) {
200 wakaba 1.41 my $c = $content =~ s/\Q$word\E//g;
201 wakaba 1.30 push @r, [$page, $c+10];
202 wakaba 1.41 } elsif (my $c = $content =~ s/\Q$word\E//g) {
203 wakaba 1.30 push @r, [$page, $c];
204 wakaba 1.22 }
205     }
206 wakaba 1.41 #my $em = sub { my $s = shift; $s =~ s#(\Q$word\E)#<em>$1</em>#gi; $s };
207     my $r = join "\n", map {qq(<li>[$_->[1]] <a href ="$url_cgi?@{[&encode($_->[0])]}" class="wiki">@{[&escape($_->[0])]}</a> <span class="wikipage-summary">@{[&escape(&get_subjectline($_->[0]))]}</span></li>)} sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @r;
208 wakaba 1.30 $r = qq|<ul class="search-result">$r</ul>| if $r;
209     wantarray? ($r, scalar @r): $r;
210 wakaba 1.22 }
211    
212 wakaba 1.28 sub do_random_jump {
213     my @list = keys %database;
214     my $name = &encode ($list[rand @list]);
215 w 1.50 print "Location: $uri{wiki}?$name\n";
216 wakaba 1.28 print "\n";
217     }
218    
219 wakaba 1.27 sub print_header ($;%) {
220     my ($page, %option) = @_;
221 wakaba 1.41 my @head;
222 w 1.50 $option{o}->{-header}->{class} = &is_frozen($page) ? 'frozen' : '';
223     $option{o}->{-header}->{class} .= " wiki-page-obsoleted" if $option{-magic} =~ /obsoleted="yes"/;
224 wakaba 1.32 if ($option{-goto}) {
225 wakaba 1.36 if ($UA =~ m#Opera|MSIE 2\.#) {
226 wakaba 1.41 ## WARNING: This code may output unsafe HTML document if
227     ## $option{-goto} is not clean.
228 wakaba 1.36 $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#;
229 wakaba 1.33 print qq{Refresh: 0; url=$option{-goto}\n};
230 wakaba 1.41 push @head, qq(<meta http-equiv="refresh" content="0; url=$option{-goto}">);
231 w 1.50 } elsif ($UA =~ /Gecko/) {
232     print qq{Refresh: 0; url="$option{-goto}"\n};
233     push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;" />);
234 wakaba 1.32 } else {
235 wakaba 1.36 $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#;
236 wakaba 1.33 print qq{Refresh: 0; url="$option{-goto}"\n};
237 wakaba 1.41 push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;">);
238 wakaba 1.32 }
239     }
240 wakaba 1.41 print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified};
241 w 1.50 if ($option{-expires} != -1) {
242     if (defined $option{-expires}) { ## TODO: Don't use asctime
243     print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n};
244     } elsif ($option{-media}->{expires} != -1) {
245     print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n};
246     }
247 wakaba 1.42 }
248 w 1.50 if ($option{-media}->{charset} && $UA =~ m#Mozilla/2#) {
249     my $ct = qq{$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}};
250 wakaba 1.41 print qq{Content-Type: $ct\n};
251 w 1.50 $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$ct">\n};
252     } elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic#) {
253     print qq{Content-Type: $option{-media}->{type}\n};
254 wakaba 1.32 } else {
255 w 1.50 my $type = $option{-media}->{type};
256     $type = 'application/xml' if $type eq 'application/rss+xml';
257     print qq{Content-Type: $type; charset=@{[&get_charset_name($kanjicode)]}\n};
258 wakaba 1.32 }
259 w 1.50 print <<"EOD"; ## TODO:
260 wakaba 1.18 Content-Language: $lang
261     Content-Style-Type: text/css
262 wakaba 1.9
263 wakaba 1.22 EOD
264 w 1.50 $option{o}->{-header}->{links} = join "\n", (@head);
265 wakaba 1.22 }
266    
267 wakaba 1.41 sub get_charset_name ($;%) {
268     my ($charset, %option) = (lc shift, @_);
269     if ($charset =~ 'euc') {
270     $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp';
271     } elsif ($charset =~ 'sjis' || $charset =~ 'shift') {
272     $charset = $option{compatible} ? 'x-sjis' : 'shift_jis';
273     } elsif ($charset =~ 'jis') {
274     $charset = 'iso-2022-jp';
275 wakaba 1.32 }
276     $charset;
277     }
278    
279 wakaba 1.16 sub escape {
280     my $s = shift;
281 w 1.48 $s =~ s|\x0D\x0A|\x0A|g;
282 wakaba 1.23 $s =~ s|&|&amp;|g;
283 wakaba 1.16 $s =~ s|<|&lt;|g;
284     $s =~ s|>|&gt;|g;
285     $s =~ s|"|&quot;|g;
286     return $s;
287     }
288    
289     sub unescape {
290     my $s = shift;
291     # $s =~ s|\n|\r\n|g;
292 wakaba 1.23 $s =~ s|&lt;|<|g;
293     $s =~ s|&gt;|>|g;
294     $s =~ s|&quot;|"|g;
295     $s =~ s|&amp;|&|g;
296 wakaba 1.16 return $s;
297     }
298    
299 w 1.49 sub convert_format ($$$;%) {
300     my ($content, $d => $t, %option) = @_;
301     &load_formatter ('format');
302 w 1.50 my $f = SuikaWiki::Plugin->format_converter ($d => $t);
303 w 1.49 if (ref $f) {
304     $option{content} = $content;
305 w 1.50 $option{from} = $d;
306     $option{to} = $t;
307 w 1.49 &$f ({}, bless (\%option, 'SuikaWiki::Plugin'));
308     } elsif ($t =~ /HTML|xml/) {
309     length $content ? '<pre>'.&escape($content).'</pre>' : '';
310     } else {
311     $content;
312     }
313 wakaba 1.9 }
314    
315 wakaba 1.16 sub text_to_html {
316     my ($txt, %option) = @_;
317 w 1.49 my $toc = $option{-toc} || (ref $option{toc} ? $option{toc} : []);
318 wakaba 1.16 my $tocnum = 0;
319 wakaba 1.41
320     ## Load constants
321     my %const;
322     if ($option{content_format} =~ /import="([^"]+)"/) {
323     for (split /\s*,\s*/, $1) {
324     my $wp = $database{$_};
325     if ($wp =~ m!^\#\?SuikaWikiConst/1.0!) {
326     wiki::suikawikiconst::to_hash ($wp => \%const);
327     }
328     }
329     }
330    
331     $txt =~ s{__&&([^&]+)&&__}{defined $const{$1}?$const{$1}:qq(__&&$1&&__)}ge;
332     my (@txt) = split(/\n/, $txt);
333 wakaba 1.16 my (@saved, @result);
334     unshift(@saved, "</p>");
335     push(@result, "<p>");
336 wakaba 1.9 foreach (@txt) {
337     chomp;
338 wakaba 1.25 if (/^\*\*\*\*\*([^\x0D\x0A]*)/) {
339 w 1.48 push @$toc, [5, "i$tocnum" => ($1 || $tocnum)];
340 wakaba 1.41 push(@result, splice(@saved), qq(<h6 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h6>');
341 wakaba 1.18 $tocnum++;
342 wakaba 1.25 } elsif (/^\*\*\*\*([^\x0D\x0A]*)/) {
343 w 1.48 push @$toc, [4, "i$tocnum" => ($1 || $tocnum)];
344 wakaba 1.41 push(@result, splice(@saved), qq(<h5 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h5>');
345 wakaba 1.18 $tocnum++;
346 wakaba 1.25 } elsif (/^\*\*\*([^\x0D\x0A]*)/) {
347 w 1.48 push @$toc, [3, "i$tocnum" => ($1 || $tocnum)];
348 wakaba 1.41 push(@result, splice(@saved), qq(<h4 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h4>');
349 wakaba 1.18 $tocnum++;
350 wakaba 1.25 } elsif (/^\*\*([^\x0D\x0A]*)/) {
351 wakaba 1.16 # if (/^\*\*(.*)/) {
352     # Walrus mod (6) end
353 w 1.48 push @$toc, [2, "i$tocnum" => ($1 || $tocnum)];
354 wakaba 1.41 push(@result, splice(@saved), qq(<h3 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h3>');
355 wakaba 1.16 $tocnum++;
356 wakaba 1.25 } elsif (/^\*([^\x0D\x0A]*)/) {
357 w 1.48 push @$toc, [1, "i$tocnum" => ($1 || $tocnum)];
358 wakaba 1.41 push(@result, splice(@saved), qq(<h2 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h2>');
359 wakaba 1.16 $tocnum++;
360 wakaba 1.25 } elsif (/^(={1,6})(.*)/) {
361 wakaba 1.18 &back_push('ol', length($1), \@saved, \@result);
362 wakaba 1.41 push(@result, '<li>' . &inline($2, const => \%const) . '</li>');
363 wakaba 1.25 } elsif (/^(-{1,6})(.*)/) {
364 wakaba 1.28 &back_push('ul', length($1), \@saved, \@result);
365     my ($pf, $l) = ('', $2);
366     if (!$main::_EMBEDED && $l =~ s/^\s*\[([0-9]+)\]//) {
367     my $num = 0+$1;
368     $pf = qq(<a name="anchor-$num" id="anchor-$num" class="anchor">[$num]</a>);
369     }
370 wakaba 1.41 push(@result, '<li>' . $pf . &inline ($l, const => \%const) . '</li>');
371 wakaba 1.9 } elsif (/^:([^:]+):(.*)/) {
372 wakaba 1.16 &back_push('dl', 1, \@saved, \@result);
373 wakaba 1.41 push(@result, '<dt>' . &inline($1, const => \%const) . '</dt>', '<dd>' . &inline($2, const => \%const) . '</dd>');
374 wakaba 1.30 } elsif (/^(?!>>\d)(>{1,5})(.*)/) {
375 wakaba 1.16 &back_push('blockquote', length($1), \@saved, \@result);
376 wakaba 1.30 push @result, "<p>";
377 wakaba 1.41 push(@result, &inline($2, const => \%const));
378 wakaba 1.30 unshift @saved, "</p>";
379 wakaba 1.9 } elsif (/^\s*$/) {
380     push(@result, splice(@saved));
381 wakaba 1.30 push(@result, "<p>");
382 wakaba 1.9 unshift(@saved, "</p>");
383     } elsif (/^(\s+.*)$/) {
384 wakaba 1.16 &back_push('pre', 1, \@saved, \@result);
385 wakaba 1.41 push(@result, &inline($1, const => \%const));
386     } elsif (/^\,(.*?)[\x0D\x0A]*$/) {
387     &back_push('table', 1, \@saved, \@result);
388 wakaba 1.16 #######
389     # This part is taken from Mr. Ohzaki's Perl Memo and Makio Tsukamoto's WalWiki.
390     # XXXXX
391     my $tmp = "$1,";
392     my @value = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_} ($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
393     my @align = map {(s/^\s+//) ? ((s/\s+$//) ? ' align="center"' : ' align="right"') : ''} @value;
394     my @colspan = map {($_ eq '==') ? 0 : 1} @value;
395     for (my $i = 0; $i < @value; $i++) {
396     if ($colspan[$i]) {
397     while ($i + $colspan[$i] < @value and $value[$i + $colspan[$i]] eq '==') {
398     $colspan[$i]++;
399     }
400     $colspan[$i] = ($colspan[$i] > 1) ? sprintf(' colspan="%d"', $colspan[$i]) : '';
401 wakaba 1.41 $value[$i] = sprintf('<td%s%s>%s</td>', $align[$i], $colspan[$i], &inline($value[$i], const => \%const));
402 wakaba 1.16 } else {
403     $value[$i] = '';
404     }
405     }
406     push(@result, join('', '<tr>', @value, '</tr>'));
407     # XXXXX
408     #######
409 wakaba 1.29 } elsif (/^\[(INS|DEL|PRE)\[\s*$/) {
410 wakaba 1.28 push @result, splice (@saved), '<'.lc($1).'>';
411     unshift @saved, "</p>";
412     push @result, "<p>";
413 wakaba 1.29 } elsif (/^\](INS|DEL|PRE)\]\s*$/) {
414 wakaba 1.28 push @result, splice (@saved), '</'.lc($1).'>';
415     } elsif (/^\[([0-9]+)\](.*)$/ && !$main::_EMBEDED) {
416     my $num = 0+$1;
417     push @result, qq(<a name="anchor-$num" id="anchor-$num" class="anchor">[$num]</a>);
418 wakaba 1.41 push @result, &inline ($2, const => \%const);
419 wakaba 1.9 } else {
420 wakaba 1.41 push(@result, &inline($_, const => \%const));
421 wakaba 1.9 }
422     }
423     push(@result, splice(@saved));
424 wakaba 1.27
425 w 1.48 my $r = join("\n", @result);
426     $r =~ s#<p>\x0D?\x0A</p>##g;
427     $r =~ s#[\x0D\x0A]+</#</#g;
428     $r =~ s#<pre>\x0D?\x0A#<pre>#g;
429     $r;
430 wakaba 1.9 }
431    
432     sub back_push {
433 wakaba 1.16 my ($tag, $level, $savedref, $resultref, $attr) = @_;
434     while (@$savedref > $level) {
435     push(@$resultref, shift(@$savedref));
436     }
437     if ($savedref->[0] ne "</$tag>") {
438     push(@$resultref, splice(@$savedref));
439     }
440     while (@$savedref < $level) {
441     unshift(@$savedref, "</$tag>");
442     push(@$resultref, "<$tag$attr>");
443 wakaba 1.9 }
444     }
445    
446 wakaba 1.41 sub inline ($;%) {
447     my ($line, %option) = @_;
448 wakaba 1.16 $line = &escape($line);
449 wakaba 1.34 $line =~ s{$embed_command{form}}{&make_custom_form ($1, $2, $3, $4)}ge;
450 wakaba 1.32 $line =~ s{\[(INS|DEL|SUP|SUB|VAR|CODE|KBD|SAMP|DFN)(?:\(([A-Za-z0-9\x20-]+)\))?\[(.+?)\]\]}{<@{[lc $1]}@{[$2 ? qq( class="$2") : '']}>$3</@{[lc $1]}>}g;
451 wakaba 1.30 $line =~ s:\[(WEAK)\[(.+?)\]\]:<span class="@{[lc $1]}">$2</span>:g;
452     $line =~ s:\[ABBR\[([^]]+)\] \[([^]]+)\]\]:<acronym title="$2">$1</acronym>:g;
453     $line =~ s:\[RUBYB\[([^]]+)\] \[([^]]+)\] \[([^]]+)\]\]:<span class="ruby"><ruby class="rb"><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby><span class="rp"> (</span><span class="rt-below">$3</span><span class="rp">) </span></span>:g;
454     $line =~ s:\[RUBY\[([^]]+)\] \[([^]]+)\]\]:<ruby><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby>:g;
455     $line =~ s:\[RUBYB\[([^]]+)\] \[([^]]+)\]\]:<span class="ruby"><span class="rb">$1</span><span class="rp"> (</span><span class="rt-below">$2</span><span class="rp">) </span></span>:g;
456     $line =~ s|'''([^']+)'''|<strong>$1</strong>|g;
457     $line =~ s|''([^']+)''|<em>$1</em>|g;
458 wakaba 1.31 $line =~ s{
459 wakaba 1.41 (\[\[(\#\S+?)\]\])
460     |\[\[([^[]+?)](?:&gt;&gt;([0-9]+))?]
461 wakaba 1.28 |&gt;&gt;([0-9]+)
462 wakaba 1.31 |&lt;([A-Za-z0-9%]+:(?:(?!&gt;).)+)&gt;
463     }{
464     my ($l, $page,$anchor, $anum, $uri) = ($1, $3,$4, 0+$5, $6);
465 wakaba 1.28 if ($l) {
466 w 1.46 &embedded_to_html($1);
467 wakaba 1.28 } elsif (defined $page) {
468     &make_wikilink ($page, anchor => 0+$anchor);
469     } elsif ($anum) {
470     qq(<a href="#anchor-$anum" class="wiki-anchor">&gt;&gt;$anum</a>);
471 wakaba 1.31 } elsif ($uri) {
472     &make_urilink ($uri);
473 wakaba 1.28 }
474 wakaba 1.31 }gex;
475 wakaba 1.16 return $line;
476     }
477    
478 wakaba 1.28 sub make_wikilink ($%) {
479     my ($ename, %option) = @_;
480     my $name = &unescape ($ename);
481 w 1.49 $option{latest} = $option{latest} ? qq(mycmd=read;x-param=@{[time.[0..9]->[rand 10]]};mypage=) : '';
482 wakaba 1.28 if ($database{$name}) {
483     my $subject = &escape (&get_subjectline ($name, delimiter => ''));
484     if ($option{anchor}) {
485 w 1.49 return qq(<a title="$subject" href="$uri{wiki}?$option{latest}@{[&encode($name)]}#anchor-$option{anchor}" class="wiki">$ename&gt;&gt;$option{anchor}</a>);
486 wakaba 1.28 } else {
487 w 1.49 return qq(<a title="$subject" href="$uri{wiki}?$option{latest}@{[&encode($name)]}" class="wiki">$ename</a>);
488 wakaba 1.28 }
489     } else {
490 w 1.49 return qq(<a title="@{[&Resource('JumpAndEditWikiPage',escape=>1)]}" href="$uri{wiki}?$option{latest}@{[&escape($name)]}" class="wiki not-exist">$ename<span class="mark">@{[&Resource('JumpAndEditWikiPageMark',escape=>1)]}</span></a>);
491 wakaba 1.28 }
492     }
493    
494 wakaba 1.31 sub make_urilink ($;%) {
495     require URI;
496     my $uri = shift;
497     if ($uri =~ s/^IW://) { ## InterWiki (not URI)
498     $uri = &unescape ($uri);
499     if ($uri =~ /^([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+"):([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+")$/) {
500     my ($site, $name) = ($1, $2);
501     for ($site, $name) {
502     if (s/^"//) { s/"$//; s/\\(.)/$1/g }
503     }
504 wakaba 1.41 &init_InterWikiName () unless $interwiki{'[[]]'};
505 wakaba 1.31 if ($interwiki{$site}) {
506 wakaba 1.41 &load_formatter ('interwiki');
507 wakaba 1.31 my $uri = &escape ($fmt{interwiki}->replace ($interwiki{$site} => {site => $site, name => $name}));
508     $site = &escape ($site); $name = &escape ($name);
509 wakaba 1.32 qq(&lt;<a href="$uri" class="out-of-wiki interwiki" title="$name ($site); URI: &lt;$uri&gt;"><span class="interwiki-site">$site:</span><span class="interwiki-name">$name</span></a>&gt;);
510 wakaba 1.31 } else {
511 wakaba 1.41 qq(&lt;@{[&Resource('Error:UnknownInterWikiName=',escape=>1)]}@{[&escape ($site)]}&gt;);
512 wakaba 1.31 }
513     } else {
514 wakaba 1.41 qq(&lt;@{[&Resource('Error:InvalidInterWiki=',escape=>1)]}@{[&escape($uri)]}&gt;);
515 wakaba 1.31 }
516     } elsif ($uri =~ /^urn:/) { ## URN
517     my $uri2 = &escape (URI->new ('/uri-res/N2L?'.&unescape ($uri), 'http')->canonical);
518     qq(&lt;<a href="$uri2" title="URI: &lt;$uri&gt; (via &lt;$uri2&gt;)" class="out-of-wiki urn">$uri</a>&gt;);
519     } elsif ($uri =~ s/^MAIL://) { ## mail address (not URI)
520     my $uri2 = &escape (URI->new ('mailto:'.&unescape ($uri))->canonical);
521     qq(&lt;<a href="$uri2" class="out-of-wiki mail">$uri</a>&gt;);
522     } elsif ($uri =~ s/^IMG(?:\([^)]+\))?://) { ## image (not URI itself)
523     my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
524     qq(<img src="$uri2" alt="" title="URI: &lt;$uri2&gt;" class="out-of-wiki">);
525     } else { ## misc. URI
526     CGI::Carp::warningsToBrowser (0);
527     my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
528     CGI::Carp::warningsToBrowser (1);
529     qq(&lt;<a href="$uri2" title="URI: &lt;$uri2&gt;" class="out-of-wiki">$uri</a>&gt;);
530     }
531     }
532    
533 wakaba 1.41 {my $FormIndex = 0;
534 wakaba 1.35 sub make_custom_form ($$$$) {
535 wakaba 1.34 my ($wfname, $definition, $template, $option) = @_;
536 wakaba 1.33 ## $template and $option is currently not used in this procedure.
537     unless ($main::_EMBEDED) {
538     $FormIndex++;
539 wakaba 1.34 if (length $definition) {
540     my $param = bless {}, 'SuikaWiki::Plugin';
541 w 1.47 my $lastmodified = $database->mtime ($form{mypage});
542 wakaba 1.35 &load_formatter (qw/form_input form_option/);
543     $definition = &unescape ($definition);
544 wakaba 1.34 $definition =~ s/\\(.)/$1/g;
545     $option = &unescape ($option);
546     $option =~ s/\\(.)/$1/g;
547     $fmt{form_option}->replace ($option, $param);
548 wakaba 1.42 $param->{output}->{form} = 1 unless defined $param->{output}->{form};
549     $definition .= ' %submit;' if $definition !~ /%submit/ && !$param->{output}->{nosubmit} && $param->{output}->{form};
550 w 1.50 $param->{output}->{page} ||= $form{mypage};
551 w 1.48 $param->{form_disabled} = 1 if $database->meta (IsFrozen => $form{mypage});
552 wakaba 1.34 my $target_form = $param->{output}->{id};
553 wakaba 1.42 my $r = '';
554     $r = <<EOH if $param->{output}->{form};
555 wakaba 1.33 <form method="post" action="$url_cgi" id="wikiform-$FormIndex" class="wikiform">
556 w 1.50 <input type="hidden" name="mycmd" value="@{[$param->{form_disabled}?'read':'wikiform']}" />
557     <input type="hidden" name="mypage" value="@{[&escape($param->{output}->{page})]}" />
558     <input type="hidden" name="myLastModified" value="$lastmodified" />
559     <input type="hidden" name="mytouch" value="on" />
560     <input type="hidden" name="@{[$target_form? qq(wikiform_targetform" value="@{[&escape($target_form)]}) : qq(wikiform_index" value="$FormIndex)]}" />
561 wakaba 1.33 EOH
562 wakaba 1.34 $r .= qq(<a name="wikiform-$FormIndex"></a>) if $UA =~ m#Mozilla/[12]\.#;
563 wakaba 1.35 $r .= $fmt{form_input}->replace ($definition, $param);
564 wakaba 1.42 $r .= "</form>\n" if $param->{output}->{form};
565 wakaba 1.34 $r;
566     } else { ## No input-interface WikiForm
567     qq(<a id="wikiform-$FormIndex" name="wikiform-$FormIndex"><!-- #form --></a>);
568     }
569 wakaba 1.33 } else {
570 wakaba 1.41 qq(<ins class="wiki-error">@{[&Resource('Error:WikiForm:EmbedIsNotSupported',escape=>1)]}</ins>);
571 wakaba 1.9 }
572 wakaba 1.41 }}
573 wakaba 1.9
574 wakaba 1.16 sub init_form {
575 wakaba 1.41 ## TODO: Support multipart/form-data
576     my $query = '';
577     if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
578     read STDIN, $query, $main::ENV{CONTENT_LENGTH};
579 wakaba 1.16 }
580 wakaba 1.41 $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
581 wakaba 1.42 if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
582 wakaba 1.30 my $query = &decode($main::ENV{QUERY_STRING});
583 wakaba 1.32 $query = &code_convert(\$query, $kanjicode);
584 wakaba 1.16 $form{mypage} = $query;
585 wakaba 1.30 $form{mycmd} = $database{$form{mypage}} ? 'read' : 'edit';
586 wakaba 1.41 } else {
587     for (split /[;&]/, $query) {
588     if (my ($n, $v) = split /=/, $_, 2) {
589     for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
590     $form{$n} = $v;
591     }
592     }
593 w 1.43 unless (defined $form{mypage}) {
594     $form{mypage} = $form{epage};
595     $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
596     }
597 wakaba 1.16 }
598 w 1.50 $form{mypage} ||= $PageName{FrontPage};
599 w 1.48 $form{mypage} =~ tr/\x00-\x1F\x7F//d;
600 wakaba 1.32 $form{mycmd} ||= 'read';
601 wakaba 1.16
602     # mypreview_edit -> do_edit, with preview.
603     # mypreview_adminedit -> do_adminedit, with preview.
604     # mypreview_write -> do_write, without preview.
605     foreach (keys %form) {
606     if (/^mypreview_(.*)$/) {
607     $form{mycmd} = $1;
608     $form{mypreview} = 1;
609     }
610     }
611 wakaba 1.9
612 wakaba 1.30 #
613 wakaba 1.16 # $form{mycmd} is frozen here.
614 wakaba 1.30 #
615 wakaba 1.16
616 wakaba 1.33 for (grep /^wikiform__/, keys %form) {
617     $form{$_} = &code_convert (\$form{$_}, $kanjicode);
618     }
619 wakaba 1.16 $form{mymsg} = &code_convert(\$form{mymsg}, $kanjicode);
620     $form{myname} = &code_convert(\$form{myname}, $kanjicode);
621     }
622    
623 wakaba 1.41 {my %SubjectLine;
624 wakaba 1.16 sub get_subjectline {
625     my ($page, %option) = @_;
626 wakaba 1.41 unless (defined $SubjectLine{$page}) {
627     if (not &is_editable($page)) {
628     $SubjectLine{$page} = "";
629     } else {
630     $SubjectLine{$page} = $database{$page};
631     $SubjectLine{$page} =~ s!^\#\?[^\x0A\x0D]+[\x0A\x0D]*!!s;
632     $SubjectLine{$page} =~ s/\x0D?\x0A.*//s;
633     }
634     }
635     if (length $SubjectLine{$page}) {
636     $option{delimiter} = defined $option{delimiter} ? $option{delimiter} : &Resource('Title-Summary Delimiter');
637     $option{delimiter}.$SubjectLine{$page}.$option{tail};
638 wakaba 1.16 } else {
639 wakaba 1.41 '';
640 wakaba 1.16 }
641 wakaba 1.41 }}
642 wakaba 1.9
643 wakaba 1.16 sub open_db {
644     if ($modifier_dbtype eq 'dbmopen') {
645 w 1.50 dbmopen(%database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $PathTo{WikiDataBase}";
646 wakaba 1.16 } elsif ($modifier_dbtype eq 'AnyDBM_File') {
647 wakaba 1.41 eval q{use AnyDBM_File};
648 w 1.50 tie(%database, "AnyDBM_File", $PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $PathTo{WikiDataBase}");
649 w 1.47 } elsif ($modifier_dbtype eq 'Yuki::YukiWikiDB') {
650 wakaba 1.41 eval q{use Yuki::YukiWikiDB};
651 w 1.50 tie(%database, "Yuki::YukiWikiDB", $PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $PathTo{WikiDataBase}");
652 w 1.47 } else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta
653 w 1.46 eval qq{use $modifier_dbtype};
654 w 1.50 $database = tie(%database, $modifier_dbtype => $PathTo{WikiDataBase}, -lock => 2, -backup => $wiki::diff::UseDiff) or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}");
655 wakaba 1.9 }
656     }
657    
658 wakaba 1.16 sub close_db {
659     if ($modifier_dbtype eq 'dbmopen') {
660     dbmclose(%database);
661     } else {
662     untie(%database);
663     }
664     }
665 wakaba 1.9
666 w 1.48 sub editform (@) {
667     my %option = @_;
668     my $frozen = &is_frozen ($option{page});
669     $option{content} = $database{$option{page}} unless defined $option{content};
670     $option{content} = $database{NewPageTemplate} unless length $option{content};
671     $option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified};
672     my $f = '';
673     my $magic = '';
674     $magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s;
675    
676     my $selected = 'read';
677     if ($form{after_edit_cmd}) {
678     $selected = $form{after_edit_cmd};
679     } elsif ($magic =~ /Const|Config|CSS/) {
680     $selected = 'edit';
681     }
682     my $afteredit = <<EOH;
683 wakaba 1.36 <select name="after_edit_cmd">
684 wakaba 1.41 <option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option>
685     <option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option>
686 wakaba 1.36 </select>
687     EOH
688 w 1.48 $f .= <<"EOD";
689     <form action="$uri{wiki}" method="post">
690 w 1.50 @{[ $option{conflict} ? '' : qq(<label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" /><kbd>S</kbd></label>) ]}
691     @{[ $option{admin} ? qq(<label>@{[&Resource('Edit:Password=',escape=>1)]}<input type="password" name="mypassword" value="" size="10" /></label>) : "" ]} [@{[&get_new_anchor_index($option{content})]}]<br />
692     <input type="hidden" name="myLastModified" value="$option{last_modified}" />
693     <input type="hidden" name="mypage" value="@{[&escape($form{mypage})]}" />
694     <textarea cols="@{[&Resource('Edit:Form:Cols')+0||80]}" rows="@{[&Resource('Edit:Form:Rows')+0||20]}" name="mymsg" tabindex="1">@{[&escape($option{content})]}</textarea><br />
695 wakaba 1.16 @{[
696 w 1.48 $option{admin} ?
697 wakaba 1.16 qq(
698 w 1.50 <label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]} />@{[&Resource('Edit:Freeze',escape=>1)]}</label>
699     <label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]} />@{[&Resource('Edit:DontFreeze',escape=>1)]}</label><br />)
700 wakaba 1.16 : ""
701     ]}
702     @{[
703 w 1.48 $option{conflict} ? "" :
704 wakaba 1.16 qq(
705 w 1.50 <label><input type="checkbox" name="mytouch" value="on" checked="checked" />@{[&Resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br />
706     <label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" accesskey="S" /><kbd>S</kbd></label>
707 wakaba 1.36 $afteredit
708 wakaba 1.16 )
709     ]}
710     </form>
711     EOD
712 w 1.48 $f;
713 wakaba 1.9 }
714    
715 wakaba 1.16 sub is_editable {
716     my ($page) = @_;
717 w 1.50 $page =~ /[\x00-\x20\x7F]/ ? 0 : 1;
718 wakaba 1.9 }
719    
720 wakaba 1.16 sub decode {
721     my ($s) = @_;
722     $s =~ tr/+/ /;
723     $s =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
724     return $s;
725     }
726 wakaba 1.9
727 wakaba 1.16 sub encode {
728 w 1.49 my $s = shift;
729 w 1.50 $s =~ s/([^0-9A-Za-z_-])/sprintf '%%%02X', ord $1/ge;
730 w 1.49 $s;
731 wakaba 1.16 }
732 wakaba 1.9
733 wakaba 1.16 sub get_now {
734 wakaba 1.41 my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
735 wakaba 1.16 $year += 1900;
736     $mon++;
737     $mon = "0$mon" if $mon < 10;
738     $day = "0$day" if $day < 10;
739     $hour = "0$hour" if $hour < 10;
740     $min = "0$min" if $min < 10;
741 wakaba 1.28 #$sec = "0$sec" if $sec < 10;
742 wakaba 1.41 return "$year-$mon-$day $hour:$min";
743 wakaba 1.16 }
744    
745     sub init_InterWikiName {
746 wakaba 1.41 my @content = split /\n/, $database{InterWikiName};
747 wakaba 1.31 for (@content) {
748     if (/^([^#]\S*)\s+(\S[^\x0A\x0D]+)/) {
749     $interwiki{$1} = $2;
750 wakaba 1.16 }
751 wakaba 1.31 }
752 wakaba 1.41 $interwiki{'[[]]'} = 1; ## dummy
753 wakaba 1.16 }
754 wakaba 1.9
755 wakaba 1.16 sub frozen_reject {
756 w 1.47 my ($isfrozen) = $database->meta (IsFrozen => $form{mypage});
757 wakaba 1.16 my ($willbefrozen) = $form{myfrozen};
758     if (not $isfrozen and not $willbefrozen) {
759     # You need no check.
760     return 0;
761     } elsif (valid_password($form{mypassword})) {
762     # You are admin.
763     return 0;
764     } else {
765 w 1.50 &_do_view_msg (-view => '-error', -page => $form{mypage},
766     error_message => &Resource ('Error:PasswordIsIncorrect'));
767     exit;
768 wakaba 1.16 }
769     }
770 wakaba 1.9
771 w 1.47 sub is_frozen ($) { $database->meta (IsFrozen => $_[0]) ? 1 : 0 }
772 wakaba 1.9
773 wakaba 1.16 sub do_comment {
774     my ($content) = $database{$form{mypage}};
775 w 1.49 my $default_name; ## this code is not strict.
776 wakaba 1.30 $default_name = $1 if $content =~ /default-name="([^"]+)"/;
777     my $datestr = '[WEAK['.&get_now.']]';
778 wakaba 1.41 my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
779 wakaba 1.30 ($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
780 wakaba 1.29 if ($namestr =~ /^(?:>>)?[0-9]/) {
781     $namestr = qq( ''$namestr'': );
782 wakaba 1.30 } elsif (length $namestr) {
783 wakaba 1.29 $namestr = qq( ''[[$namestr]]'': );
784     }
785 wakaba 1.33 my $anchor = &get_new_anchor_index ($content);
786 wakaba 1.27 my $i = 1; my $o = 0;
787 wakaba 1.41 $content =~ s{(\[\[\#r?comment\]\])}{
788 wakaba 1.25 my $embed = $1;
789     if ($i == $form{comment_index}) {
790 wakaba 1.41 if ($embed ne '[[#rcomment]]') {
791 wakaba 1.28 $embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
792 wakaba 1.25 } else {
793 wakaba 1.28 $embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
794 wakaba 1.25 }
795     }
796     $i++; $embed;
797     }ge;
798     unless ($o) {
799 wakaba 1.30 $content = "#?SuikaWiki/0.9\n\n" unless $content;
800     $content .= "\n" unless $content =~ /\n$/s;
801 wakaba 1.28 $content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
802 wakaba 1.16 }
803 wakaba 1.29 $form{__comment_anchor_index} = $anchor;
804 wakaba 1.30 if ($form{mymsg} || $form{myname}) {
805 wakaba 1.16 $form{mymsg} = $content;
806     $form{mytouch} = 'on';
807     &do_write;
808     } else {
809     $form{mycmd} = 'read';
810     &do_read;
811     }
812     }
813 wakaba 1.9
814 wakaba 1.33 sub get_new_anchor_index ($) {
815     my $content = shift;
816     my $anchor = 0;
817     $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
818     $anchor + 1;
819     }
820    
821 wakaba 1.27 my $CommentIndex = 0;
822 wakaba 1.16 sub embedded_to_html {
823     my ($embedded) = @_;
824 wakaba 1.41 if ($embedded eq '[[#comment]]' or $embedded eq '[[#rcomment]]') {
825 wakaba 1.28 unless ($main::_EMBEDED) {
826 w 1.47 my $lastmodified = $database->mtime ($form{mypage});
827 wakaba 1.16 return <<"EOD";
828 wakaba 1.30 <form action="$url_cgi" method="post" id="x-comment-@{[++$CommentIndex]}" class="comment"><p>
829 w 1.50 <input type="hidden" name="mycmd" value="comment" />
830     <input type="hidden" name="mypage" value="$form{mypage}" />
831     <input type="hidden" name="myLastModified" value="$lastmodified" />
832     <input type="hidden" name="mytouch" value="on" />
833     <input type="hidden" name="comment_index" value="$CommentIndex" />
834 wakaba 1.41 @{[&Resource('WikiForm:WikiComment:Name=',escape=>1)]}
835 w 1.50 <input type="text" name="myname" value="" size="10" class="comment-name" />
836     <input type="text" name="mymsg" value="" size="60" class="comment-msg" />
837     <input type="submit" value="@{[&Resource('WikiForm:Add',escape=>1)]}" title="@{[&Resource('WikiForm:AddLong',escape=>1)]}" class="comment-submit" />
838 wakaba 1.30 </p></form>
839 wakaba 1.16 EOD
840 wakaba 1.28 } else {
841     return <<"EOD";
842     <del><form action="$url_cgi" method="get">
843 w 1.50 <input type="hidden" name="mycmd" value="read" />
844     <input type="hidden" name="mypage" value="$form{mypage}" />
845 wakaba 1.41 @{[&Resource('WikiForm:WikiComment:Name=',escape=>1)]}
846 w 1.50 <input type="text" name="myname" value="" size="10" disabled="disabled" />
847     <input type="text" name="mymsg" value="" size="60" disabled="disabled" />
848 wakaba 1.28 </form></del>
849     EOD
850     }
851 wakaba 1.22 } elsif ($embedded =~ /$embed_command{searched}/) {
852 wakaba 1.30 return get_search_result ($1, -match_myself => 1);
853 wakaba 1.28 } elsif ($embedded =~ /^\[\[\#embed:(.+)\]\]$/) {
854     my ($name, $r) = ($1, '');
855     if ($main::_EMBEDED != 1) {
856     my ($content, $cf) = ($database{$name}, 'SuikaWiki/0.9');
857     $cf = $1 if $content =~ s!^(?:[\#<]\?|/\*\s*)?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.][^\x0D\x0A]*)?)[\x0D\x0A]+!!s;
858     if ($cf =~ m!^(?:\#\?)?SuikaWiki/0.9(?:$|\s)!) {
859     $main::_EMBEDED = 1;
860     $r = &text_to_html ($content, content_format => $cf);
861     $main::_EMBEDED = 0;
862     } elsif (length $content) {
863     $r = "<pre>@{[&escape ($content)]}</pre>";
864     } else {
865 wakaba 1.41 $r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:PageNotFound')]}\n]INS]\n", content_format => 'SuikaWiki/0.9');
866 wakaba 1.28 }
867     } else { ## nested #EMBED
868 wakaba 1.41 $r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:Nested',escape=>1)]}\n]INS]\n", content_format => 'SuikaWiki/0.9');
869 wakaba 1.16 }
870 wakaba 1.31 return qq(<blockquote title="@{[&escape($name)]}" class="wiki-embed">$r<div class="cite-note">¡Ø<cite><a href="$url_cgi?@{[&encode($name)]}" class="wiki">@{[&escape($name)]}</a></cite>¡Ù</div></blockquote>);
871 wakaba 1.28 } elsif ($embedded =~ /^\[\[\#randomlink:(.+)\]\]$/) {
872     return qq(<a href="$url_cgi?mycmd=RandomJump;x-param=@{[time.[0..9]->[rand 10]]}" class="wiki randomlink">$1</a>);
873     } else {
874     return $embedded;
875     }
876 wakaba 1.16 }
877 wakaba 1.9
878 wakaba 1.35 sub load_formatter (@) {
879     for my $t (@_) {
880 wakaba 1.33 unless ($fmt{$t}) {
881 wakaba 1.41 require Message::Util::Formatter;
882 wakaba 1.33 $fmt{$t} = Message::Util::Formatter->new;
883     for (@{$SuikaWiki::Plugin::List{'wiki'.$t}||[]}) {
884     $_->load_formatter ($fmt{$t}, type => 'wiki'.$t);
885     }
886     }
887     }
888 wakaba 1.35 }
889    
890     sub do_wikiform {
891     my $content = $database{$form{mypage}};
892     my $anchor = &get_new_anchor_index ($content);
893     &load_formatter (qw/form_template form_option/);
894 wakaba 1.33 my $write = 0;
895     my $i = 1;
896     $content =~ s{$embed_command{form}}{
897 wakaba 1.34 my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
898     if (($wfname && $wfname eq $form{wikiform_targetform})
899     || $i == $form{wikiform_index}) {
900     $template =~ s/\\(.)/$1/g;
901     $option =~ s/\\(.)/$1/g;
902 wakaba 1.33 my $param = bless {}, 'SuikaWiki::Plugin';
903     $param->{page} = $form{mypage};
904     $param->{form_index} = $i;
905 wakaba 1.34 $param->{form_name} = $wfname;
906 wakaba 1.33 $param->{anchor_index} = $anchor;
907     $param->{argv} = \%form;
908     $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
909 wakaba 1.41 $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
910 wakaba 1.33 $fmt{form_option}->replace ($option, $param);
911 wakaba 1.34 my $t = 1;
912     for (@{$param->{require}||[]}) {
913 wakaba 1.36 (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
914 wakaba 1.34 }
915     $t = $fmt{form_template}->replace ($template, $param) if $t;
916 wakaba 1.33 if (length $t) {
917     if ($param->{output}->{reverse}) {
918 wakaba 1.34 $embed .= "\n" . $t;
919     } else {
920 wakaba 1.33 $embed = $t . "\n" . $embed;
921     }
922     $write = 1;
923     $form{__comment_anchor_index} = $anchor
924 wakaba 1.34 if $param->{anchor_index_}; ## $anchor is used!
925 wakaba 1.33 }
926 wakaba 1.34 $form{__wikiform_anchor_index} = $i;
927     undef $form{wikiform_targetform}; ## make sure never to match
928     undef $form{wikiform_index}; ## with WikiForm in rest of page
929 wakaba 1.33 }
930     $i++; $embed;
931     }ge;
932     unless ($write) {
933     #$content = "#?SuikaWiki/0.9\n\n" unless $content;
934     #$content .= "\n" unless $content =~ /\n$/s;
935     #
936     }
937     if ($write) {
938     $form{mymsg} = $content;
939     $form{mytouch} = 'on';
940     &do_write;
941     } else {
942     $form{mycmd} = 'read';
943     &do_read;
944     }
945     }
946    
947 wakaba 1.16 sub code_convert {
948 wakaba 1.41 require Jcode;
949 wakaba 1.27 my ($contentref, $code) = (shift, shift || $kanjicode);
950 w 1.49 if ($code =~ /euc/) { $code = 'euc' }
951     elsif ($code =~ /iso/) { $code = 'jis' }
952     elsif ($code =~ /shi/) { $code = 'sjis' }
953     elsif ($code =~ /utf/) { $code = 'utf8' }
954 w 1.43 $$contentref = Jcode->new ($contentref)->tr ("\xA3\xB0-\xA3\xB9\xA3\xC1-\xA3\xDA\xA3\xE1-\xA3\xFA\xA1\xF5\xA1\xA4\xA1\xA5\xA1\xA7\xA1\xA8\xA1\xA9\xA1\xAA\xA1\xAE\xA1\xB0\xA1\xB2\xA1\xBF\xA1\xC3\xA1\xCA\xA1\xCB\xA1\xCE\xA1\xCF\xA1\xD0\xA1\xD1\xA1\xDC\xA1\xF0\xA1\xF3\xA1\xF4\xA1\xF6\xA1\xF7\xA1\xE1\xA2\xAF\xA2\xB0\xA2\xB2\xA2\xB1\xA1\xE4\xA1\xE3\xA1\xC0\xA1\xA1" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@='"~-><\ ))->$code;
955 wakaba 1.16 return $$contentref;
956     }
957    
958 wakaba 1.41 sub _rfc3339_date ($) {
959     my @time = gmtime (shift);
960     sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
961     }
962    
963 w 1.49 sub is_exist_page { $use_exists ? exists ($database{$_[0]}) : $database{$_[0]} }
964 wakaba 1.27 sub __get_database ($) { $database{ $_[0] } }
965    
966 wakaba 1.41 my %_Resource;
967     sub Resource ($;%) {
968     my ($s, %o) = @_;
969     unless (defined $_Resource{$s}) {
970     $_Resource{$_[0]} = &wiki::resource::get ($s, $_Resource{__option});
971     }
972     $o{escape} ? &escape ($_Resource{$s}) : $_Resource{$s};
973     }
974    
975 wakaba 1.27 package wiki::referer;
976     sub add ($$) {
977     my $page = shift;
978     my $uri = shift;
979     unless (ref $uri) {
980     require URI;
981     $uri = URI->new ($uri);
982     ## Some schemes do not have query part.
983     eval q{ $uri->query (undef) if $uri->query =~ /^[0-9]{6,8}$/ };
984     $uri->fragment (undef);
985     }
986     $uri = $uri->canonical;
987     return unless $uri;
988     for my $regex (&get_dont_record) {
989     return if $uri =~ /$regex/;
990     }
991     my %list = get ($page);
992     $list{ $uri }++;
993     set ($page, \%list);
994     }
995 w 1.47 sub get ($) { split /"/, $main::database->meta (Referer => $_[0]) }
996 wakaba 1.27 sub set ($%) {
997     my $page = shift;
998     my $list = shift;
999 w 1.47 $main::database->meta (Referer => $page => join '"', %$list);
1000 wakaba 1.27 }
1001    
1002     sub get_dont_record () {
1003     map {s/\$/\\\$/g; s/\@/\\\@/g; $_}
1004     grep !/^#/,
1005 w 1.49 split /[\x0D\x0A]+/, $main::database{RefererDontRecord};
1006 wakaba 1.27 }
1007     sub get_site_name () {
1008 w 1.49 my @lines = grep /[^#]/, split /[\x0D\x0A]+/, $main::database{RefererSiteName};
1009 wakaba 1.28 my @item;
1010 wakaba 1.27 for (@lines) {
1011     next if /^#/;
1012     my ($uri, $name) = split /\s+/, $_, 2;
1013     $uri =~ s/\$/\\\$/g; $uri =~ s/\@/\\\@/g; $uri =~ s/\//\\\//g;
1014 wakaba 1.28 $name =~ s!([()/\\])!\\$1!g; $name =~ s/\$([0-9]+)/).__decode (\${$1}).q(/g;
1015     push @item, [$uri, qq(q($name))];
1016 wakaba 1.27 }
1017 wakaba 1.28 @item;
1018 wakaba 1.27 }
1019    
1020     sub list_html ($) {
1021     my $page = shift;
1022     my %list = get ($page);
1023     my $r = '';
1024 wakaba 1.28 my @name = get_site_name;
1025 wakaba 1.27 for my $uri (sort keys %list) {
1026     my $title;
1027 wakaba 1.28 for my $item (@name) {
1028     if ($uri =~ /$item->[0]/) {
1029 wakaba 1.27 $title = $uri;
1030 wakaba 1.28 eval qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e}
1031     or die $@ ;#. qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e};
1032 wakaba 1.27 last;
1033     }
1034     }
1035     my $euri = main::escape ($uri);
1036     if ($title) {
1037 wakaba 1.36 $r .= qq(<li>[$list{$uri}] <a href="$euri" title="URI: &lt;$euri&gt;">@{[main::escape ($title)]}</a></li>\n);
1038 wakaba 1.27 } else {
1039     $r .= qq(<li>[$list{$uri}] &lt;<a href="$euri">$euri</a>&gt;</li>\n);
1040     }
1041     }
1042     $r ? qq(<ul>$r</ul>\n) : '';
1043     }
1044    
1045     sub __decode ($) {
1046     my $s = shift;
1047     $s =~ tr/+/ /;
1048     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
1049     main::code_convert (\$s);
1050 wakaba 1.32 }
1051    
1052     package wiki::useragent;
1053 w 1.45 our $UseLog;
1054 wakaba 1.32
1055     sub add ($) {
1056     my $s = shift;
1057     return unless length $s;
1058 w 1.45 return unless $UseLog;
1059 wakaba 1.39 $s =~ s/([\x00-\x08\x0A-\x1F\x25\x7F-\xFF])/sprintf '%%%02X', unpack 'C', $1/ge;
1060 wakaba 1.32 my %ua;
1061 w 1.45 for (split /\n/, &main::__get_database($main::PageName{UserAgentList})) {
1062 wakaba 1.32 if (/^-\[(\d+)\] (.+)$/) {
1063     my ($t, $n) = ($1, $2);
1064     $n =~ tr/\x0A\x0D//d;
1065     $ua{$n} = $t;
1066     }
1067     }
1068     $ua{$s}++;
1069     my $s = qq(#?SuikaWiki/0.9\n);
1070     for (sort {$ua{$a} <=> $ua{$b}} keys %ua) {
1071     $s .= sprintf qq(-[%d] %s\n), $ua{$_}, $_;
1072     }
1073 w 1.46 $main::database->STORE ($main::PageName{UserAgentList} => $s, -touch => 0);
1074 wakaba 1.16 }
1075 wakaba 1.33
1076 wakaba 1.41 package wiki::suikawikiconst;
1077    
1078     sub to_hash ($;$) {
1079     my $page = shift;
1080     my $h = shift || {};
1081     my $val;
1082     for my $line (split /\n/, $page) {
1083     $line =~ tr/\x0A\x0D//d;
1084     if ($val && $line =~ s/^\s+//) {
1085     $h->{$val} .= length $h->{$val} ? "\n" . $line : $line;
1086     } elsif ($line =~ /^(.+):/) {
1087     $val = $1; $h->{$val} = '';
1088     }
1089     }
1090     $h;
1091     }
1092 w 1.47
1093     package wiki::dummy;
1094     sub mtime (@) {undef}
1095     sub meta (@) {undef}
1096     sub Yuki::YukiWikiDB2::meta (@) {undef}
1097 wakaba 1.41
1098 wakaba 1.33 package SuikaWiki::Plugin;
1099 w 1.49 our $plugin_directory;
1100 wakaba 1.33 our %List;
1101 w 1.48 our %Index;
1102 w 1.50 push @main::INC, $plugin_directory.'/../..';
1103 wakaba 1.33
1104     sub escape ($$) { main::escape ($_[1]) }
1105     sub unescape ($$) { main::unescape ($_[1]) }
1106     sub encode ($$) { main::encode ($_[1]) }
1107     sub decode ($$) { main::decode ($_[1]) }
1108     sub __get_datetime ($) { main::get_now () }
1109 wakaba 1.42 sub resource ($$;%) { shift; &main::Resource (@_) }
1110     sub uri ($$) { $main::uri{$_[1]} }
1111 w 1.48 sub new_index ($$) { ++$Index{$_[1]} }
1112 w 1.50 sub user_agent_names ($) { $main::UA }
1113 w 1.49 sub magic_and_content ($$) {
1114 w 1.50 my ($magic, $page) = ('', $_[1]);
1115     $magic = $1 if $page =~ s!^((?:\#\?|/\*|<\?)[^\x02\x0A\x0D]+)[\x02\x0A\x0D]+!!s;
1116     ($magic, $page);
1117 w 1.49 }
1118     sub formatter ($$) {
1119     &main::load_formatter ($_[1]);
1120     $main::fmt{$_[1]};
1121 w 1.50 }
1122     sub format_converter ($$$) {
1123     &main::load_formatter ('format');
1124     $main::fmt{format}->{($_[1]=~/([A-Za-z0-9]\S+)/?$1:'SuikaWiki/0.9').'_to_'.$_[2]}
1125     || $main::fmt{format}->{($_[1]=~/([A-Za-z0-9](?:(?!\/)\S)+)/?$1:'SuikaWiki').'_to_'.$_[2]};
1126 w 1.49 }
1127 wakaba 1.33
1128     sub regist ($@) {
1129     my $pack = shift;
1130     for (@_) {
1131     push @{$List{$_}}, $pack;
1132     }
1133     }
1134    
1135 wakaba 1.35 sub import_plugins () {
1136     opendir PDIR, $plugin_directory;
1137     my @plugin = grep {s/\.pm$//} readdir (PDIR);
1138     closedir PDIR;
1139     for (@plugin) {
1140     eval qq{ use SuikaWiki::Plugin::$_ } unless /[^A-Za-z0-9_]/;
1141     push @{$List{_all}}, qq(SuikaWiki::Plugin::$_);
1142     }
1143     }
1144    
1145     &import_plugins ();
1146    
1147 wakaba 1.41 package wiki::conneg;
1148    
1149     ## BUG: this parser isn't strict.
1150     sub get_accept_lang (;$) {
1151     my $alang = shift || $main::ENV{HTTP_ACCEPT_LANGUAGE};
1152     my %alang = (ja => 0.0002, en => 0.0001);
1153     my $i = 0.1;
1154     for (split /\s*,\s*/, $alang) {
1155     tr/\x09\x0A\x0D\x20//d;
1156     if (/((?:(?!;q=).)+)(?:;q="?([0-9.]+)"?)?/) {
1157     my $l = lc $1; $l =~ tr/\x22\x5C//d;
1158     $alang{$l} = (defined $2 ? $2 : 1.000)*1000;
1159     $alang{$l} += $i unless $alang{$l} == 0;
1160     $i -= 0.001;
1161     }
1162     }
1163     \%alang;
1164     }
1165    
1166     package wiki::resource;
1167    
1168     sub get ($;\%) {
1169     my ($resname, $option) = @_;
1170     $option->{accept_language} ||= &wiki::conneg::get_accept_lang ();
1171     $option->{resource} ||= {};
1172     my $v;
1173     for my $lang (sort {$option->{accept_language}->{$b} <=> $option->{accept_language}->{$a}} grep {$option->{accept_language}->{$_}!=0} keys %{$option->{accept_language}}) {
1174     while (length $lang) {
1175     unless ($option->{accept_language}->{defined $option->{accept_language}->{$lang} ? $lang : '*'} == 0) {
1176 w 1.49 $option->{resource}->{$lang} ||= &wiki::suikawikiconst::to_hash ($main::database{'WikiResource:'.$lang});
1177 wakaba 1.41 $v = $option->{resource}->{$lang}->{$resname};
1178     last if defined $v;
1179     }
1180     $lang =~ s/[^+-]*$//; $lang =~ s/[+-]$//;
1181     }
1182     last if defined $v;
1183     }
1184     defined $v ? $v : $resname;
1185     }
1186    
1187 wakaba 1.35 package main;
1188     &main;
1189     exit 0;
1190 wakaba 1.9
1191 wakaba 1.16 1;
1192     __END__
1193     =head1 NAME
1194 wakaba 1.9
1195 w 1.46 wiki.cgi --- SuikaWiki: Yet yet another Wiki engine
1196 w 1.49
1197     =head1 SEE ALSO
1198    
1199     <IW:SuikaWiki:SuikaWiki>
1200 wakaba 1.9
1201 w 1.46 =head1 AUTHORS
1202 wakaba 1.9
1203 w 1.46 Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/>
1204 wakaba 1.9
1205 w 1.46 Makio Tsukamoto <http://digit.que.ne.jp/>
1206 wakaba 1.9
1207 w 1.46 Wakaba <w@suika.fam.cx>
1208 wakaba 1.9
1209 wakaba 1.16 =head1 LICENSE
1210 wakaba 1.9
1211 w 1.46 Copyright (C) 2000-2003 AUTHORS
1212 wakaba 1.9
1213 wakaba 1.16 This program is free software; you can redistribute it and/or
1214     modify it under the same terms as Perl itself.
1215 wakaba 1.9
1216     =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24