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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12.2.2 - (hide annotations) (download)
Thu Oct 9 09:47:45 2003 UTC (21 years, 1 month ago) by wakaba
Branch: branch-suikawiki-1
Changes since 1.12.2.1: +30 -10 lines
File MIME type: text/plain
Use appropriate status code

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24