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

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.51 - (hide annotations) (download)
Sun Jan 26 02:30:24 2003 UTC (21 years, 3 months ago) by w
Branch: MAIN
Changes since 1.50: +136 -106 lines
Mosaic Netscape 0.9 support

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24