1 |
# -*- perl -*- |
2 |
use strict; |
3 |
|
4 |
package wiki; |
5 |
our $VERSION = do{my @r=(q$Revision: 1.12 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
6 |
package main; |
7 |
binmode STDOUT; binmode STDIN; |
8 |
use Fcntl; |
9 |
require SuikaWiki::Plugin; |
10 |
our %embed_command = ( |
11 |
form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/, |
12 |
); |
13 |
our ($modifier_dbtype,%uri,%PathTo,%PageName,$kanjicode); |
14 |
|
15 |
our %form; |
16 |
our %database; |
17 |
our $database = bless {}, 'wiki::dummy'; |
18 |
my %command_do = ( |
19 |
default => \&do_view, |
20 |
adminchangepassword => \&do_adminchangepassword, |
21 |
write => \&do_write, |
22 |
comment => \&do_comment, |
23 |
RandomJump => sub { |
24 |
my @list = keys %main::database; |
25 |
&main::_http_see_other (page => $list[rand @list]); |
26 |
}, |
27 |
wikiform => \&do_wikiform, |
28 |
); |
29 |
our $UA = ''; ## User agent name |
30 |
$| = 1; |
31 |
require SuikaWiki::Name::Space; |
32 |
my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml'; |
33 |
|
34 |
sub _wiki_exit () { |
35 |
&close_db; |
36 |
exit; |
37 |
} |
38 |
|
39 |
sub do_view { |
40 |
require SuikaWiki::View; |
41 |
my $content = $main::database{$main::form{mypage}}; |
42 |
my $lm = SuikaWiki::Plugin->_database->mtime ($main::form{mypage}); |
43 |
## Determine mode |
44 |
my $view = $form{mycmd}; |
45 |
if (!$view || $view eq 'default' || $view =~ /[^0-9A-Za-z_]/) { |
46 |
## BUG: this code is not strict |
47 |
if ($main::ENV{HTTP_COOKIE} =~ /SelectedMode=([0-9A-Za-z_-]+)/) { |
48 |
$view = $1; $view =~ tr/-/_/; |
49 |
} else { |
50 |
$view = 'read'; |
51 |
} |
52 |
} |
53 |
## Get content and its meta info |
54 |
my ($magic, $content) = SuikaWiki::Plugin->magic_and_content ($content); |
55 |
$magic ||= '#?SuikaWiki/0.9'; |
56 |
my $o = bless {param => \%main::form, page => $main::form{mypage}, toc => [], |
57 |
magic => $magic, content => $content, |
58 |
&main::_compatible_options ()}, 'SuikaWiki::Plugin'; |
59 |
my $view_def = SuikaWiki::View->definition ($view); |
60 |
if (!$view_def->check ($o)) { |
61 |
print "Status: 406 Unsupported Media Type\n"; |
62 |
$view = '-UnsupportedMediaType'; |
63 |
$view_def = SuikaWiki::View->definition ($view); |
64 |
} |
65 |
my $prop = $view_def->properties; |
66 |
my $media = $prop->{media}; |
67 |
if ($prop->{xmedia} && $main::UA =~ /Gecko/) { ## TODO: conneg |
68 |
$media = $prop->{xmedia}; |
69 |
$o->{media} = $media; |
70 |
} elsif ($main::UA =~ m#Mozilla/0\..+Windows#) { |
71 |
$main::kanjicode = 'shift_jis'; |
72 |
} |
73 |
|
74 |
if ($prop->{preprocess}) { |
75 |
_wiki_exit () unless &{$prop->{preprocess}} (o => $o); |
76 |
} |
77 |
|
78 |
## Output CGI/HTTP headers |
79 |
if ($magic =~ m!^\#\?SuikaWiki/0.9!) { |
80 |
&main::print_header ($main::form{mypage}, |
81 |
-last_modified => ($magic =~ /interactive="yes"/ ? time : $lm), |
82 |
-expires => ($magic =~ /interactive="yes"/ ? 1 : undef), o => $o, |
83 |
-media => $media, -view => $view_def, -magic => $magic, content => $content); |
84 |
} else { |
85 |
&main::print_header($main::form{mypage}, -media => $media, -view => $view_def, |
86 |
-magic => $magic, -last_modified => $lm, o => $o); |
87 |
} |
88 |
## Output HTTP message body |
89 |
my $fmt = SuikaWiki::Plugin->formatter ('view'); |
90 |
my $s = $fmt->replace ($view_def->as_string => $o, {formatter => $fmt}); |
91 |
if ($main::kanjicode eq 'euc') { |
92 |
#require Compress::Zlib; |
93 |
#print scalar Compress::Zlib::memGzip (''.$s); |
94 |
print $s; |
95 |
} else { |
96 |
$s .= ''; |
97 |
print &main::code_convert (\$s => $main::kanjicode); |
98 |
} |
99 |
} |
100 |
|
101 |
sub _do_view_msg (%) { |
102 |
require SuikaWiki::View; |
103 |
my %option = @_; |
104 |
my $o = $option{-o} || bless {param => \%form, page => $option{-page}, |
105 |
&_compatible_options ()}, 'SuikaWiki::Plugin'; |
106 |
$o->{toc} = []; |
107 |
$o->{condition} = \%option; ## This parameter really used?? |
108 |
my $view_def = SuikaWiki::View->definition ($option{-view}); |
109 |
unless ($view_def->check ($o)) { |
110 |
print "Status: 406 Unsupported Media Type\n"; |
111 |
$option{-view} = '-UnsupportedMediaType'; |
112 |
$view_def = SuikaWiki::View->definition ($option{-view}); |
113 |
} |
114 |
my $prop = $view_def->properties; |
115 |
my $media = $prop->{media}; |
116 |
if ($prop->{xmedia} && $UA =~ /Gecko/) { |
117 |
$media = $prop->{xmedia}; |
118 |
$o->{media} = $media; |
119 |
} |
120 |
&print_header($option{-page}, -media => $media, -view => $view_def, o => $o, -goto => $option{-goto}); |
121 |
## Output HTTP message body |
122 |
my $fmt = SuikaWiki::Plugin->formatter ('view'); |
123 |
my $s = $fmt->replace ($view_def->as_string => $o, {formatter => $fmt}); |
124 |
if ($main::kanjicode eq 'euc') { |
125 |
print $s; |
126 |
} else { |
127 |
print &main::code_convert (\$s => $main::kanjicode); |
128 |
} |
129 |
} |
130 |
|
131 |
# [move to SuikaWiki::Plugin::WikiAdmin] |
132 |
sub do_adminchangepassword { |
133 |
if ($form{mynewpassword} ne $form{mynewpassword2}) { |
134 |
&_do_view_msg (-view => '-error', -page => $form{mypage}, |
135 |
error_message => &Resource ('Error:PasswordMismatch')); |
136 |
return; |
137 |
} |
138 |
my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage}); |
139 |
if ($validpassword_crypt) { |
140 |
if (not &valid_password($form{myoldpassword})) { |
141 |
&_do_view_msg (-view => '-error', -page => $form{mypage}, |
142 |
error_message => &Resource ('Error:PasswordIsIncorrect')); |
143 |
return; |
144 |
} |
145 |
} |
146 |
my ($sec, $min, $hour, $day, $mon, $year, $weekday) = localtime(time); |
147 |
my (@token) = ('0'..'9', 'A'..'Z', 'a'..'z'); |
148 |
my $salt1 = $token[(time | $$) % scalar(@token)]; |
149 |
my $salt2 = $token[($sec + $min*60 + $hour*60*60) % scalar(@token)]; |
150 |
my $crypted = crypt($form{mynewpassword}, "$salt1$salt2"); |
151 |
$database->meta (AdminPassword => $PageName{AdminSpecialPage} => $crypted); |
152 |
|
153 |
&_do_view_msg (-view => '-wrote', -page => $form{mypage}); |
154 |
} |
155 |
|
156 |
# [move to SuikaWiki::WikiDB] |
157 |
sub valid_password ($) { |
158 |
my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage}); |
159 |
return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0; |
160 |
} |
161 |
|
162 |
# [move to SuikaWiki::Plugin::WikiEdit] |
163 |
sub do_write { |
164 |
if (&frozen_reject()) { |
165 |
return; |
166 |
} |
167 |
|
168 |
if (not &is_editable($form{mypage})) { |
169 |
&_do_view_msg (-view => '-error', -page => $form{mypage}, |
170 |
error_message => &Resource ('Error:ThisPageIsUneditable')); |
171 |
return; |
172 |
} |
173 |
|
174 |
## Check confliction |
175 |
if ($form{myLastModified} ne $database->mtime ($form{mypage})) { |
176 |
&_do_view_msg (-view => '-conflict', -page => $form{mypage}); |
177 |
return; |
178 |
} |
179 |
|
180 |
if ($form{mymsg}) { |
181 |
if ($form{mytouch} || !ref $database) { |
182 |
$database{$form{mypage}} = $form{mymsg}; |
183 |
} else { |
184 |
$database->STORE ($form{mypage} => $form{mymsg}, -touch => 0); |
185 |
} |
186 |
$database->meta (IsFrozen => $form{mypage} => 0 + $form{myfrozen}); |
187 |
my $uri = SuikaWiki::Plugin->_uri_wiki_page ($form{mypage}, mode => ($form{after_edit_cmd}||'default'), with_lm => 1, absolute => 1); |
188 |
$uri .= qq(;after_edit_cmd=@{[SuikaWiki::Plugin->encode($form{after_edit_cmd})]}) if $form{after_edit_cmd}; |
189 |
if ($form{__comment_anchor_index}) { |
190 |
$uri .= qq(#anchor-$form{__comment_anchor_index}); |
191 |
} elsif ($form{__wikiform_anchor_index}) { |
192 |
$uri .= qq(#wikiform-$form{__wikiform_anchor_index}); |
193 |
} |
194 |
&_http_see_other (uri => $uri, page => $form{mypage}, alternate_view => '-wrote'); |
195 |
} else { |
196 |
delete $database{$form{mypage}}; |
197 |
&_do_view_msg (-view => '-deleted', -page => $form{mypage}); |
198 |
} |
199 |
} |
200 |
|
201 |
sub _http_see_other (%) { |
202 |
my %o = @_; |
203 |
$o{uri} ||= SuikaWiki::Plugin->_uri_wiki_page ($o{page}, absolute => 1); |
204 |
if ($o{alternate_view} && ($main::ENV{SERVER_PROTOCOL} eq 'HTTP/0.9' |
205 |
|| $main::ENV{SERVER_PROTOCOL} eq 'HTTP/1.0') |
206 |
&& !($main::UA =~ m#Microsoft Internet Explorer|Mozilla#)) { |
207 |
&_do_view_msg (-view => $o{alternate_view}, -page => $o{page}, -goto => $o{uri}, -o => $o{o}); |
208 |
} else { |
209 |
my $status = q(303 See Other); |
210 |
if ($main::UA =~ m!Mozilla/[0-4]\.|Microsoft Internet Explorer!) { |
211 |
$status = q(302 See Other); |
212 |
} |
213 |
my $euri = SuikaWiki::Plugin->escape ($o{uri}); |
214 |
print qq(Status: $status |
215 |
Location: $o{uri} |
216 |
Content-Type: text/html |
217 |
Content-Language: en |
218 |
|
219 |
<!DOCTYPE p SYSTEM> |
220 |
<p>See <<a href="$euri">$euri</a>>.</p>); |
221 |
} |
222 |
_wiki_exit (); |
223 |
} |
224 |
|
225 |
sub _compatible_options () { |
226 |
(use_anchor_name => ($main::UA =~ m#Mozilla/[1-4]\.|Microsoft Internet Explorer# ? 1 : 0)); |
227 |
} |
228 |
|
229 |
|
230 |
|
231 |
sub print_header ($;%) { |
232 |
my ($page, %option) = @_; |
233 |
if ($main::ENV{HTTP_IF_MODIFIED_SINCE} && $option{-last_modified}) { |
234 |
## TODO: use Message::Field::Date |
235 |
if ($option{-view}->properties->{if}->{modified_since} |
236 |
&& $main::ENV{HTTP_IF_MODIFIED_SINCE} =~ /([0-9]{1,2})\s*([A-Za-z]{3})\s*([0-9]{2,4})\s*([0-9]{2}):([0-9]{2}):([0-9]{2})\s*[Gg][Mm][Tt]/) { |
237 |
require Time::Local; |
238 |
my ($d, $M, $y, $h, $m, $s) = ($1, $2, $3, $4, $5, $6); |
239 |
$M = {jan=>0,feb=>1,mar=>2,apr=>3,may=>4,jun=>5,jul=>6,aug=>7,sep=>8,oct=>9,nov=>10,dec=>11}->{lc $M}; |
240 |
#$y += 1900 if $y < 100; ## BUG: don't conform HTTP spec |
241 |
my $t = Time::Local::timegm_nocheck ($s, $m, $h, $d, $M, $y); |
242 |
if ($option{-last_modified} <= $t) { |
243 |
print "Status: 304 Not Modified\n\n"; |
244 |
_wiki_exit (); |
245 |
} |
246 |
} |
247 |
} |
248 |
my $UA = SuikaWiki::Plugin->user_agent_names; |
249 |
$option{o}->{-header}->{class}->{frozen} = 1 if &main::is_frozen ($page); |
250 |
$option{o}->{-header}->{class}->{'wiki-page-obsoleted'} = 1 if $option{-magic} =~ /obsoleted="yes"/; |
251 |
$option{o}->{-header}->{additional_html_element} ||= SuikaWiki::Markup::XML->new (type => '#fragment'); |
252 |
print "Vary: Negotiate,User-Agent,Accept-Language,Accept\n"; |
253 |
if ($option{-goto}) { |
254 |
if ($UA =~ m#Opera|MSIE 2\.#) { |
255 |
## WARNING: This code may output unsafe HTML document if $option{-goto} is unclean. |
256 |
$option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#; |
257 |
print qq{Refresh: 0; url=$option{-goto}\n}; |
258 |
for ($option{o}->{-header}->{additional_html_element}->append_new_node |
259 |
(namespace_uri => $NS_XHTML1, local_name => 'meta')) { |
260 |
$_->set_attribute ('http-equiv' => 'refresh'); |
261 |
$_->set_attribute (content => "0; url=$option{-goto}"); |
262 |
$_->option (use_EmptyElemTag => 1); |
263 |
} |
264 |
} else { |
265 |
$option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#; |
266 |
print qq{Refresh: 0; url="$option{-goto}"\n}; |
267 |
for ($option{o}->{-header}->{additional_html_element}->append_new_node |
268 |
(namespace_uri => $NS_XHTML1, local_name => 'meta')) { |
269 |
$_->set_attribute ('http-equiv' => 'refresh'); |
270 |
$_->set_attribute (content => qq(0; url="$option{-goto}")); |
271 |
$_->option (use_EmptyElemTag => 1); |
272 |
} |
273 |
} |
274 |
} |
275 |
print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} |
276 |
if $option{-last_modified}; |
277 |
if ($option{-expires} != -1) { |
278 |
if (defined $option{-expires}) { ## TODO: Don't use asctime |
279 |
print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n}; |
280 |
} elsif ($option{-media}->{expires} != -1) { |
281 |
print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n}; |
282 |
} |
283 |
} |
284 |
if ($option{-media}->{charset} && $UA =~ m#Mozilla/[12]\.#) { |
285 |
## UAs don't support official charset names but do non-official names |
286 |
my $ct = qq{$option{-media}->{type}; charset=@{[ &main::get_charset_name ($main::kanjicode, compatible => 1) ]}}; |
287 |
print qq{Content-Type: $ct\n}; |
288 |
for ($option{o}->{-header}->{additional_html_element}->append_new_node |
289 |
(namespace_uri => $NS_XHTML1, local_name => 'meta')) { |
290 |
$_->set_attribute ('http-equiv' => 'content-type'); |
291 |
$_->set_attribute (content => $ct); |
292 |
} |
293 |
} elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic|Mozilla/0\.#) { |
294 |
## Media types or UAs don't support charset parameter in HTTP header |
295 |
print qq{Content-Type: $option{-media}->{type}\n}; |
296 |
if ($option{-media}->{charset}) { |
297 |
for ($option{o}->{-header}->{additional_html_element}->append_new_node |
298 |
(namespace_uri => $NS_XHTML1, local_name => 'meta')) { |
299 |
$_->set_attribute ('http-equiv' => 'content-type'); |
300 |
$_->set_attribute (content => qq($option{-media}->{type}; charset=).main::get_charset_name ($main::kanjicode, compatible => 1)); |
301 |
} |
302 |
} |
303 |
} else { |
304 |
## Modern UAs and Media types with charset parameter |
305 |
my $type = $option{-media}->{type}; |
306 |
$type = 'application/xml' if ($type =~ m!^application/r(?:df|ss)\+xml$!) && ($UA =~ m#Gecko#); |
307 |
print qq{Content-Type: $type; charset=@{[&main::get_charset_name ($main::kanjicode)]}\n}; |
308 |
## meta element is not needed |
309 |
} |
310 |
|
311 |
print <<"EOD"; |
312 |
Content-Style-Type: text/css |
313 |
|
314 |
EOD |
315 |
} |
316 |
|
317 |
sub get_charset_name ($;%) { |
318 |
my ($charset, %option) = (lc shift, @_); |
319 |
if ($charset =~ 'euc') { |
320 |
$charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp'; |
321 |
} elsif ($charset =~ 'sjis' || $charset =~ 'shift') { |
322 |
$charset = $option{compatible} ? 'x-sjis' : 'shift_jis'; |
323 |
} elsif ($charset =~ 'jis') { |
324 |
$charset = 'iso-2022-jp'; |
325 |
} |
326 |
$charset; |
327 |
} |
328 |
|
329 |
=pod |
330 |
|
331 |
sub _decode_argv () { |
332 |
my $QS = $main::ENV{QUERY_STRING}; |
333 |
if ($main::ENV{PATH_INFO}) { |
334 |
die; |
335 |
# new format: not implemented yet |
336 |
} else { |
337 |
my %argv; |
338 |
if ($QS =~ /[&;=]/) { ## ?FOO=foo;BAR=bar;BAZ=baz |
339 |
for (split /[;&]/, $QS) { |
340 |
if (my ($n, $v) = split /=/, $_, 2) { |
341 |
for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'HH', $1/ge}; |
342 |
$argv{$n} = $v; |
343 |
} |
344 |
} |
345 |
} else { ## ?FOO-BAR |
346 |
$argv{page} = $QS; |
347 |
$argv{page} =~ tr/+/ /; |
348 |
$argv{page} =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'HH', $1/ge; |
349 |
} |
350 |
my $ie = $argv{ie}; ## Input coding system |
351 |
for ([qw/mypage page/], [qw/mycmd mode/]) { |
352 |
$argv{$_->[1]} ||= $argv{$_->[0]}; |
353 |
delete $argv{$_->[0]}; |
354 |
} |
355 |
for (keys %argv) { |
356 |
$argv{$_} = main::code_convert ($argv{$_}, $main::kanjicode, $ie); |
357 |
} |
358 |
for ([qw/mypage page/], [qw/mycmd mode/]) { |
359 |
$argv{$_->[0]} = $argv{$_->[1]}; |
360 |
} |
361 |
} |
362 |
} |
363 |
|
364 |
=cut |
365 |
|
366 |
sub init_form { |
367 |
## TODO: Support multipart/form-data |
368 |
my $query = ''; |
369 |
if (uc $main::ENV{REQUEST_METHOD} eq 'POST') { |
370 |
if ($main::ENV{CONTENT_TYPE}=~ m!^application/(?:x-www|sgml)-form-urlencoded(?:$|\s*;)!) { |
371 |
read STDIN, $query, $main::ENV{CONTENT_LENGTH}; |
372 |
} else { |
373 |
$main::form{mycmd} = '___unsupported_media_type___'; |
374 |
$main::form{mypage} = $main::PageName{FrontPage}; |
375 |
return; |
376 |
} |
377 |
} |
378 |
$query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING}; |
379 |
if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) { |
380 |
my $query = SuikaWiki::Plugin->decode ($main::ENV{QUERY_STRING}); |
381 |
$query = &main::code_convert (\$query, $main::kanjicode); |
382 |
$main::form{mypage} = $query; |
383 |
$main::form{mycmd} = 'default'; |
384 |
} else { |
385 |
for (split /[;&]/, $query) { |
386 |
if (my ($n, $v) = split /=/, $_, 2) { |
387 |
for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge}; |
388 |
$main::form{$n} = $v; |
389 |
} |
390 |
} |
391 |
unless (defined $form{mypage}) { |
392 |
$form{mypage} = $form{epage}; |
393 |
$form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g; |
394 |
} |
395 |
$form{mypage} = &main::code_convert (\$form{mypage}, $kanjicode); |
396 |
} |
397 |
$form{mypage} =~ tr/\x00-\x20\x7F//d; |
398 |
$form{mypage} = SuikaWiki::Name::Space::normalize_name ($form{mypage}) || $PageName{FrontPage}; |
399 |
$form{mycmd} ||= $form{mode} || 'default'; |
400 |
$form{mycmd} =~ tr/-/_/; |
401 |
|
402 |
for ('mymsg', 'myname', grep /^(?:wikiform__|pi_)/, keys %form) { |
403 |
$form{$_} = &main::code_convert (\$form{$_}, $kanjicode); |
404 |
} |
405 |
} |
406 |
|
407 |
# [move to SuikaWiki::WikiDB] |
408 |
sub open_db { |
409 |
if ($main::modifier_dbtype eq 'dbmopen') { |
410 |
dbmopen(%main::database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $main::PathTo{WikiDataBase}"; |
411 |
} elsif ($main::modifier_dbtype eq 'AnyDBM_File') { |
412 |
eval q{use AnyDBM_File}; |
413 |
tie(%main::database, "AnyDBM_File", $main::PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $main::PathTo{WikiDataBase}"); |
414 |
} elsif ($main::modifier_dbtype eq 'Yuki::YukiWikiDB') { |
415 |
eval q{use Yuki::YukiWikiDB}; |
416 |
tie(%main::database, "Yuki::YukiWikiDB", $main::PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $main::PathTo{WikiDataBase}"); |
417 |
} else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta |
418 |
eval qq{use $modifier_dbtype}; |
419 |
$database = tie (%database, $modifier_dbtype => $PathTo{WikiDataBase}, |
420 |
-lock => 0, -backup => $wiki::diff::UseDiff, ## TODO: new diff i/f |
421 |
-logfile => $main::PathTo{WikiDatabaseErrorLog}) |
422 |
or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}"); |
423 |
} |
424 |
} |
425 |
|
426 |
# [move to SuikaWiki::WikiDB] |
427 |
sub close_db { |
428 |
if ($modifier_dbtype eq 'dbmopen') { |
429 |
dbmclose(%database); |
430 |
} else { |
431 |
untie(%database); |
432 |
} |
433 |
} |
434 |
|
435 |
# [move to SuikaWiki::Plugin::WikiEdit] |
436 |
sub editform (@) { |
437 |
my %option = @_; |
438 |
my $frozen = &is_frozen ($option{page}); |
439 |
$option{content} = $database{$option{page}} unless defined $option{content}; |
440 |
$option{content} = $database{NewPageTemplate} unless length $option{content}; |
441 |
$option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified}; |
442 |
my $magic = ''; |
443 |
$magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s; |
444 |
|
445 |
my $selected = 'default'; |
446 |
if ($form{after_edit_cmd}) { |
447 |
$selected = $form{after_edit_cmd}; |
448 |
} elsif ($magic =~ /Const|Config|CSS/) { |
449 |
$selected = 'edit'; |
450 |
} |
451 |
my $afteredit = <<EOH; |
452 |
<select name="after_edit_cmd"> |
453 |
<option value="default" label="@{[&Resource('Edit:SaveAndDefault',escape=>1)]}"@{[$selected eq 'default' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndDefault',escape=>1)]}</option> |
454 |
<option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option> |
455 |
<option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option> |
456 |
</select> |
457 |
EOH |
458 |
|
459 |
=pod |
460 |
|
461 |
my $f = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'form'); |
462 |
$f->set_attribute (action => SuikaWiki::Plugin->uri ('wiki'); |
463 |
$f->set_attribute (method => 'post'); |
464 |
if (!$option{conflict}) { |
465 |
for ($f->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'label')) { |
466 |
for ($_->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'input')) { |
467 |
$f->set_attribute (type => 'submit'); |
468 |
$f->set_attribute (value => SuikaWiki::Plugin->resource ('Edit:Save')); |
469 |
} |
470 |
#<input type=hidden name=mycmd value=write/> |
471 |
$_->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'kbd', value => 'S'); |
472 |
} |
473 |
} |
474 |
|
475 |
=cut |
476 |
|
477 |
my $f = <<"EOD"; |
478 |
<form action="$uri{wiki}" method="post"> |
479 |
@{[ $option{conflict} ? '' : qq(<label><input type="submit" value="@{[SuikaWiki::Plugin->resource('Edit:Save',escape=>1)]}" /><kbd>S</kbd></label>) ]} |
480 |
@{[ $option{admin} ? qq(<label>@{[SuikaWiki::Plugin->resource('Edit:Password=',escape=>1)]}<input type="password" name="mypassword" value="" size="10" /></label>) : "" ]} [@{[&get_new_anchor_index($option{content})]}]<br /> |
481 |
<input type="hidden" name="myLastModified" value="$option{last_modified}" /> |
482 |
<input type="hidden" name="mycmd" value="write" /> |
483 |
<input type="hidden" name="mypage" value="@{[SuikaWiki::Plugin->escape($form{mypage})]}" /> |
484 |
<textarea cols="@{[SuikaWiki::Plugin->resource('Edit:Form:Cols')+0||35]}" rows="@{[SuikaWiki::Plugin->resource('Edit:Form:Rows')+0||15]}" name="mymsg" tabindex="1"@{[$main::UA =~ m{Mozilla/[0-4]\.} ? ' wrap="virtual"':'']}>@{[SuikaWiki::Plugin->escape($option{content})]}</textarea><br /> |
485 |
@{[ |
486 |
$option{admin} ? |
487 |
qq( |
488 |
<label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]} />@{[SuikaWiki::Plugin->resource('Edit:Freeze',escape=>1)]}</label> |
489 |
<label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]} />@{[SuikaWiki::Plugin->resource('Edit:DontFreeze',escape=>1)]}</label><br />) |
490 |
: "" |
491 |
]} |
492 |
@{[ |
493 |
$option{conflict} ? "" : |
494 |
qq( |
495 |
<label><input type="checkbox" name="mytouch" value="on" checked="checked" />@{[SuikaWiki::Plugin->resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br /> |
496 |
<label><input type="submit" value="@{[SuikaWiki::Plugin->resource('Edit:Save',escape=>1)]}" accesskey="S" /><kbd>S</kbd></label> |
497 |
$afteredit |
498 |
) |
499 |
]} |
500 |
</form> |
501 |
EOD |
502 |
$f; |
503 |
} |
504 |
|
505 |
# [move to SuikaWiki::WikiDB] |
506 |
sub is_editable { |
507 |
my ($page) = @_; |
508 |
return 0 unless SuikaWiki::Name::Space::validate_name ($page); |
509 |
return 0 if $page =~ /[\x00-\x20\[\]\x7F]/; |
510 |
1; |
511 |
} |
512 |
|
513 |
# [move to SuikaWiki::WikiDB] |
514 |
sub frozen_reject { |
515 |
my ($isfrozen) = $database->meta (IsFrozen => $form{mypage}); |
516 |
my ($willbefrozen) = $form{myfrozen}; |
517 |
if (not $isfrozen and not $willbefrozen) { |
518 |
# You need no check. |
519 |
return 0; |
520 |
} elsif (valid_password($form{mypassword})) { |
521 |
# You are admin. |
522 |
return 0; |
523 |
} else { |
524 |
&_do_view_msg (-view => '-error', -page => $form{mypage}, |
525 |
error_message => SuikaWiki::Plugin->resource ('Error:PasswordIsIncorrect')); |
526 |
exit; |
527 |
} |
528 |
} |
529 |
|
530 |
# [move to SuikaWiki::WikiDB] |
531 |
sub is_frozen ($) { SuikaWiki::Plugin->_database->meta (IsFrozen => $_[0]) ? 1 : 0 } |
532 |
|
533 |
# [to be obsolete] |
534 |
sub do_comment { |
535 |
my ($content) = $database{$form{mypage}}; |
536 |
my $default_name; ## this code is not strict. |
537 |
$default_name = $1 if $content =~ /default-name="([^"]+)"/; |
538 |
my @time = gmtime (time); |
539 |
my $datestr = sprintf '[WEAK[%04d-%02d-%02d %02d:%02d:%02d +00:00]]', $time[5]+1900,$time[4]+1,@time[3,2,1,0]; |
540 |
my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName'); |
541 |
($namestr = '', $datestr = '') if $form{myname} eq 'nodate'; |
542 |
if ($namestr =~ /^(?:>>)?[0-9]/) { |
543 |
$namestr = qq( ''$namestr'': ); |
544 |
} elsif (length $namestr) { |
545 |
$namestr = qq( ''[[$namestr]]'': ); |
546 |
} |
547 |
my $anchor = &get_new_anchor_index ($content); |
548 |
my $i = 1; my $o = 0; |
549 |
$content =~ s{(\[\[\#r?comment\]\])}{ |
550 |
my $embed = $1; |
551 |
if ($i == $form{comment_index}) { |
552 |
if ($embed ne '[[#rcomment]]') { |
553 |
$embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1; |
554 |
} else { |
555 |
$embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1; |
556 |
} |
557 |
} |
558 |
$i++; $embed; |
559 |
}ge; |
560 |
unless ($o) { |
561 |
$content = "#?SuikaWiki/0.9\n\n" unless $content; |
562 |
$content .= "\n" unless $content =~ /\n$/s; |
563 |
$content .= "- [$anchor] $datestr$namestr$form{mymsg}\n"; |
564 |
} |
565 |
$form{__comment_anchor_index} = $anchor; |
566 |
if ($form{mymsg} || $form{myname}) { |
567 |
$form{mymsg} = $content; |
568 |
$form{mytouch} = 'on'; |
569 |
&do_write; |
570 |
} else { ## Don't write |
571 |
$form{mycmd} = 'default'; |
572 |
&do_view; |
573 |
} |
574 |
} |
575 |
|
576 |
# [move to SuikaWiki::Plugin::WikiForm] |
577 |
sub get_new_anchor_index ($) { |
578 |
my $content = shift; |
579 |
my $anchor = 0; |
580 |
$content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge; |
581 |
$anchor + 1; |
582 |
} |
583 |
|
584 |
# [move to SuikaWiki::Plugin::WikiForm] |
585 |
sub do_wikiform { |
586 |
my $content = $database{$form{mypage}}; |
587 |
my $anchor = &get_new_anchor_index ($content); |
588 |
my $write = 0; |
589 |
my $i = 1; |
590 |
$content =~ s{$embed_command{form}}{ |
591 |
my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4); |
592 |
if (($wfname && $wfname eq $form{wikiform_targetform}) |
593 |
|| $i == $form{wikiform_index}) { |
594 |
$template =~ s/\\([\\'])/$1/g; |
595 |
$option =~ s/\\([\\'])/$1/g; |
596 |
my $param = bless {depth=>10}, 'SuikaWiki::Plugin'; |
597 |
$param->{page} = $form{mypage}; |
598 |
$param->{form_index} = $i; |
599 |
$param->{form_name} = $wfname; |
600 |
$param->{anchor_index} = $anchor; |
601 |
$param->{argv} = \%form; |
602 |
$param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/; |
603 |
$param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName'); |
604 |
SuikaWiki::Plugin->formatter ('form_option')->replace ($option, $param); |
605 |
my $t = 1; |
606 |
for (keys %{$param->{require}||{}}) { |
607 |
(undef $t, last) unless length $param->{argv}->{'wikiform__'.$_}; |
608 |
} |
609 |
$t = SuikaWiki::Plugin->formatter ('form_template')->replace ($template, $param) if $t; |
610 |
if (length $t) { |
611 |
if ($param->{output}->{reverse}) { |
612 |
$embed .= "\n" . $t; |
613 |
} else { |
614 |
$embed = $t . "\n" . $embed; |
615 |
} |
616 |
$write = 1; |
617 |
$form{__comment_anchor_index} = $anchor |
618 |
if $param->{anchor_index_}; ## $anchor is used! |
619 |
} |
620 |
$form{__wikiform_anchor_index} = $i; |
621 |
undef $form{wikiform_targetform}; ## Make sure never to match |
622 |
undef $form{wikiform_index}; ## with WikiForm in rest of page! |
623 |
} |
624 |
$i++; $embed; |
625 |
}ge; |
626 |
unless ($write) { |
627 |
#$content = "#?SuikaWiki/0.9\n\n" unless $content; |
628 |
#$content .= "\n" unless $content =~ /\n$/s; |
629 |
# |
630 |
} |
631 |
if ($write) { |
632 |
$form{mymsg} = $content; |
633 |
$form{mytouch} = 'on'; |
634 |
&do_write; |
635 |
} else { ## Don't write! |
636 |
$form{mycmd} = 'default'; |
637 |
&do_view; |
638 |
} |
639 |
} |
640 |
|
641 |
# [to be obsolete] ->Message::MIME::Charset |
642 |
sub code_convert { |
643 |
require Jcode; |
644 |
my ($contentref, $code, $srccode) = (shift, shift || $kanjicode, shift || undef); |
645 |
if ($code =~ /euc/) { $code = 'euc' } |
646 |
elsif ($code =~ /iso/) { $code = 'jis' } |
647 |
elsif ($code =~ /shi/) { $code = 'sjis' } |
648 |
elsif ($code =~ /utf/) { $code = 'utf8' } |
649 |
$$contentref = Jcode->new ($contentref, $srccode)->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; |
650 |
return $$contentref; |
651 |
} |
652 |
|
653 |
# [to be obsolete] ->Message::Field::Date |
654 |
sub _rfc3339_date ($) { |
655 |
my @time = gmtime (shift); |
656 |
sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0]; |
657 |
} |
658 |
|
659 |
# [obsolete] ->SuikaWiki::SrcFormat |
660 |
sub convert_format ($$$;%) { |
661 |
my ($content, $d => $t, %option) = @_; |
662 |
my $f = SuikaWiki::Plugin->format_converter ($d => $t); |
663 |
if (ref $f) { |
664 |
$option{content} = $content; |
665 |
$option{from} = $d; |
666 |
$option{to} = $t; |
667 |
&$f ({}, bless (\%option, 'SuikaWiki::Plugin')); |
668 |
} elsif ($option{-error_no_return}) { |
669 |
return undef; |
670 |
} elsif ($t =~ /HTML|xml/) { |
671 |
if (length $content) { |
672 |
my $r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'pre'); |
673 |
$r->append_text ($content); |
674 |
return $r; |
675 |
} else { |
676 |
return ''; |
677 |
} |
678 |
} else { |
679 |
$content; |
680 |
} |
681 |
} |
682 |
|
683 |
|
684 |
|
685 |
# [obsolete] SuikaWiki::WikiDB |
686 |
package wiki::dummy; |
687 |
sub mtime (@) {undef} |
688 |
sub meta (@) {undef} |
689 |
sub Yuki::YukiWikiDB2::meta (@) {undef} |
690 |
|
691 |
package main; |
692 |
SuikaWiki::Plugin->import_plugins (); |
693 |
$main::UA = $main::ENV{HTTP_USER_AGENT}; |
694 |
&open_db; |
695 |
&init_form; |
696 |
for (@{$SuikaWiki::Plugin::On{WikiDatabaseLoaded}||[]}) { &{$_} } |
697 |
if ($command_do{$form{mycmd}}) { |
698 |
&{$command_do{$form{mycmd}}}; # [to be obsolete] |
699 |
} else { |
700 |
&{$command_do{default}}; |
701 |
} |
702 |
_wiki_exit (); |
703 |
|
704 |
=head1 NAME |
705 |
|
706 |
lib/suikawiki.pl --- SuikaWiki transitional library |
707 |
|
708 |
=head1 AUTHOR |
709 |
|
710 |
Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/> (YukiWiki) |
711 |
|
712 |
Makio Tsukamoto <http://digit.que.ne.jp/> (WalWiki) |
713 |
|
714 |
Wakaba <w@suika.fam.cx> |
715 |
|
716 |
=head1 LICENSE |
717 |
|
718 |
Copyright AUTHORS 2000-2003 |
719 |
|
720 |
This program is free software; you can redistribute it and/or |
721 |
modify it under the same terms as Perl itself. |
722 |
|
723 |
=cut |
724 |
|
725 |
1; # $Date: 2003/09/16 05:16:31 $ |