/[pub]/suikawiki/script/lib/suikawiki.pl
Suika

Contents of /suikawiki/script/lib/suikawiki.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations) (download)
Sun Oct 5 11:50:40 2003 UTC (21 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.13: +70 -98 lines
File MIME type: text/plain
suikawiki2 transitional

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24