/[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.3 - (hide annotations) (download)
Sun Nov 9 02:31:08 2003 UTC (21 years ago) by wakaba
Branch: branch-suikawiki-1
Changes since 1.12.2.2: +5 -3 lines
File MIME type: text/plain
Buggy Gecko support

1 wakaba 1.1 # -*- perl -*-
2     use strict;
3    
4 wakaba 1.12 package wiki;
5 wakaba 1.12.2.3 our $VERSION = do{my @r=(q$Revision: 1.12.2.2 $=~/\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 wakaba 1.12.2.3 if ($main::UA =~ m!Gecko/([0-9]+)!) {
230     $status = q(302 See Other) if $1 > 20030331;
231     } elsif ($main::UA =~ m!Mozilla/[0-4]\.|Microsoft Internet Explorer!) {
232 wakaba 1.10 $status = q(302 See Other);
233     }
234 w 1.9 my $euri = SuikaWiki::Plugin->escape ($o{uri});
235 wakaba 1.10 print qq(Status: $status
236 w 1.9 Location: $o{uri}
237     Content-Type: text/html
238     Content-Language: en
239    
240     <!DOCTYPE p SYSTEM>
241 wakaba 1.10 <p>See &lt;<a href="$euri">$euri</a>&gt;.</p>);
242 w 1.9 }
243     _wiki_exit ();
244     }
245    
246 wakaba 1.1 sub _compatible_options () {
247 wakaba 1.10 (use_anchor_name => ($main::UA =~ m#Mozilla/[1-4]\.|Microsoft Internet Explorer# ? 1 : 0));
248 wakaba 1.1 }
249    
250 w 1.9
251 wakaba 1.1
252     sub print_header ($;%) {
253     my ($page, %option) = @_;
254 w 1.9 if ($main::ENV{HTTP_IF_MODIFIED_SINCE} && $option{-last_modified}) {
255     ## TODO: use Message::Field::Date
256     if ($option{-view}->properties->{if}->{modified_since}
257     && $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]/) {
258     require Time::Local;
259     my ($d, $M, $y, $h, $m, $s) = ($1, $2, $3, $4, $5, $6);
260     $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};
261     #$y += 1900 if $y < 100; ## BUG: don't conform HTTP spec
262     my $t = Time::Local::timegm_nocheck ($s, $m, $h, $d, $M, $y);
263     if ($option{-last_modified} <= $t) {
264     print "Status: 304 Not Modified\n\n";
265     _wiki_exit ();
266     }
267     }
268     }
269 wakaba 1.8 my $UA = SuikaWiki::Plugin->user_agent_names;
270     $option{o}->{-header}->{class}->{frozen} = 1 if &main::is_frozen ($page);
271 w 1.9 $option{o}->{-header}->{class}->{'wiki-page-obsoleted'} = 1 if $option{-magic} =~ /obsoleted="yes"/;
272 wakaba 1.8 $option{o}->{-header}->{additional_html_element} ||= SuikaWiki::Markup::XML->new (type => '#fragment');
273 w 1.9 print "Vary: Negotiate,User-Agent,Accept-Language,Accept\n";
274 wakaba 1.1 if ($option{-goto}) {
275     if ($UA =~ m#Opera|MSIE 2\.#) {
276 wakaba 1.8 ## WARNING: This code may output unsafe HTML document if $option{-goto} is unclean.
277     $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#;
278     print qq{Refresh: 0; url=$option{-goto}\n};
279     for ($option{o}->{-header}->{additional_html_element}->append_new_node
280     (namespace_uri => $NS_XHTML1, local_name => 'meta')) {
281     $_->set_attribute ('http-equiv' => 'refresh');
282     $_->set_attribute (content => "0; url=$option{-goto}");
283 wakaba 1.10 $_->option (use_EmptyElemTag => 1);
284 wakaba 1.8 }
285 wakaba 1.1 } else {
286 wakaba 1.8 $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#;
287     print qq{Refresh: 0; url="$option{-goto}"\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' => 'refresh');
291     $_->set_attribute (content => qq(0; url="$option{-goto}"));
292 wakaba 1.10 $_->option (use_EmptyElemTag => 1);
293 wakaba 1.8 }
294 wakaba 1.1 }
295     }
296 wakaba 1.8 print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n}
297     if $option{-last_modified};
298 wakaba 1.1 if ($option{-expires} != -1) {
299     if (defined $option{-expires}) { ## TODO: Don't use asctime
300     print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n};
301     } elsif ($option{-media}->{expires} != -1) {
302     print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n};
303     }
304     }
305     if ($option{-media}->{charset} && $UA =~ m#Mozilla/[12]\.#) {
306 wakaba 1.8 ## UAs don't support official charset names but do non-official names
307     my $ct = qq{$option{-media}->{type}; charset=@{[ &main::get_charset_name ($main::kanjicode, compatible => 1) ]}};
308     print qq{Content-Type: $ct\n};
309     for ($option{o}->{-header}->{additional_html_element}->append_new_node
310     (namespace_uri => $NS_XHTML1, local_name => 'meta')) {
311     $_->set_attribute ('http-equiv' => 'content-type');
312     $_->set_attribute (content => $ct);
313     }
314 wakaba 1.1 } elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic|Mozilla/0\.#) {
315 wakaba 1.8 ## Media types or UAs don't support charset parameter in HTTP header
316     print qq{Content-Type: $option{-media}->{type}\n};
317     if ($option{-media}->{charset}) {
318     for ($option{o}->{-header}->{additional_html_element}->append_new_node
319     (namespace_uri => $NS_XHTML1, local_name => 'meta')) {
320     $_->set_attribute ('http-equiv' => 'content-type');
321     $_->set_attribute (content => qq($option{-media}->{type}; charset=).main::get_charset_name ($main::kanjicode, compatible => 1));
322     }
323     }
324 wakaba 1.1 } else {
325 wakaba 1.8 ## Modern UAs and Media types with charset parameter
326     my $type = $option{-media}->{type};
327 w 1.9 $type = 'application/xml' if ($type =~ m!^application/r(?:df|ss)\+xml$!) && ($UA =~ m#Gecko#);
328 wakaba 1.8 print qq{Content-Type: $type; charset=@{[&main::get_charset_name ($main::kanjicode)]}\n};
329     ## meta element is not needed
330 wakaba 1.1 }
331 wakaba 1.5
332 wakaba 1.4 print <<"EOD";
333 wakaba 1.1 Content-Style-Type: text/css
334    
335     EOD
336     }
337    
338     sub get_charset_name ($;%) {
339     my ($charset, %option) = (lc shift, @_);
340     if ($charset =~ 'euc') {
341     $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp';
342     } elsif ($charset =~ 'sjis' || $charset =~ 'shift') {
343     $charset = $option{compatible} ? 'x-sjis' : 'shift_jis';
344     } elsif ($charset =~ 'jis') {
345     $charset = 'iso-2022-jp';
346     }
347     $charset;
348     }
349    
350 w 1.9 =pod
351 wakaba 1.1
352 w 1.9 sub _decode_argv () {
353     my $QS = $main::ENV{QUERY_STRING};
354     if ($main::ENV{PATH_INFO}) {
355     die;
356     # new format: not implemented yet
357     } else {
358     my %argv;
359     if ($QS =~ /[&;=]/) { ## ?FOO=foo;BAR=bar;BAZ=baz
360     for (split /[;&]/, $QS) {
361     if (my ($n, $v) = split /=/, $_, 2) {
362     for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'HH', $1/ge};
363     $argv{$n} = $v;
364     }
365     }
366     } else { ## ?FOO-BAR
367     $argv{page} = $QS;
368     $argv{page} =~ tr/+/ /;
369     $argv{page} =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'HH', $1/ge;
370     }
371     my $ie = $argv{ie}; ## Input coding system
372     for ([qw/mypage page/], [qw/mycmd mode/]) {
373     $argv{$_->[1]} ||= $argv{$_->[0]};
374     delete $argv{$_->[0]};
375     }
376     for (keys %argv) {
377     $argv{$_} = main::code_convert ($argv{$_}, $main::kanjicode, $ie);
378     }
379     for ([qw/mypage page/], [qw/mycmd mode/]) {
380     $argv{$_->[0]} = $argv{$_->[1]};
381 wakaba 1.8 }
382 wakaba 1.1 }
383     }
384    
385 w 1.9 =cut
386 wakaba 1.1
387     sub init_form {
388     ## TODO: Support multipart/form-data
389     my $query = '';
390     if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
391 wakaba 1.12.2.1 if ($main::ENV{CONTENT_TYPE}=~ m!^application/(?:x-www|sgml)-form-urlencoded(?:$|\s*;)!) {
392 wakaba 1.5 read STDIN, $query, $main::ENV{CONTENT_LENGTH};
393     } else {
394 w 1.9 $main::form{mycmd} = '___unsupported_media_type___';
395     $main::form{mypage} = $main::PageName{FrontPage};
396 wakaba 1.5 return;
397     }
398 wakaba 1.1 }
399     $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
400     if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
401 w 1.9 my $query = SuikaWiki::Plugin->decode ($main::ENV{QUERY_STRING});
402     $query = &main::code_convert (\$query, $main::kanjicode);
403     $main::form{mypage} = $query;
404     $main::form{mycmd} = 'default';
405 wakaba 1.1 } else {
406     for (split /[;&]/, $query) {
407     if (my ($n, $v) = split /=/, $_, 2) {
408     for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
409 w 1.9 $main::form{$n} = $v;
410 wakaba 1.1 }
411     }
412     unless (defined $form{mypage}) {
413     $form{mypage} = $form{epage};
414     $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
415     }
416 w 1.9 $form{mypage} = &main::code_convert (\$form{mypage}, $kanjicode);
417 wakaba 1.1 }
418 w 1.9 $form{mypage} =~ tr/\x00-\x20\x7F//d;
419     $form{mypage} = SuikaWiki::Name::Space::normalize_name ($form{mypage}) || $PageName{FrontPage};
420     $form{mycmd} ||= $form{mode} || 'default';
421 wakaba 1.4 $form{mycmd} =~ tr/-/_/;
422 wakaba 1.1
423 w 1.9 for ('mymsg', 'myname', grep /^(?:wikiform__|pi_)/, keys %form) {
424     $form{$_} = &main::code_convert (\$form{$_}, $kanjicode);
425 wakaba 1.1 }
426     }
427    
428 w 1.9 # [move to SuikaWiki::WikiDB]
429 wakaba 1.1 sub open_db {
430 w 1.9 if ($main::modifier_dbtype eq 'dbmopen') {
431     dbmopen(%main::database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $main::PathTo{WikiDataBase}";
432     } elsif ($main::modifier_dbtype eq 'AnyDBM_File') {
433 wakaba 1.1 eval q{use AnyDBM_File};
434 w 1.9 tie(%main::database, "AnyDBM_File", $main::PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $main::PathTo{WikiDataBase}");
435     } elsif ($main::modifier_dbtype eq 'Yuki::YukiWikiDB') {
436 wakaba 1.1 eval q{use Yuki::YukiWikiDB};
437 w 1.9 tie(%main::database, "Yuki::YukiWikiDB", $main::PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $main::PathTo{WikiDataBase}");
438 wakaba 1.1 } else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta
439     eval qq{use $modifier_dbtype};
440 w 1.9 $database = tie (%database, $modifier_dbtype => $PathTo{WikiDataBase},
441 wakaba 1.11 -lock => 0, -backup => $wiki::diff::UseDiff, ## TODO: new diff i/f
442 w 1.9 -logfile => $main::PathTo{WikiDatabaseErrorLog})
443     or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}");
444 wakaba 1.1 }
445     }
446    
447 w 1.9 # [move to SuikaWiki::WikiDB]
448 wakaba 1.1 sub close_db {
449     if ($modifier_dbtype eq 'dbmopen') {
450     dbmclose(%database);
451     } else {
452     untie(%database);
453     }
454     }
455    
456 w 1.9 # [move to SuikaWiki::Plugin::WikiEdit]
457 wakaba 1.1 sub editform (@) {
458     my %option = @_;
459     my $frozen = &is_frozen ($option{page});
460     $option{content} = $database{$option{page}} unless defined $option{content};
461     $option{content} = $database{NewPageTemplate} unless length $option{content};
462     $option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified};
463     my $magic = '';
464     $magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s;
465    
466     my $selected = 'default';
467     if ($form{after_edit_cmd}) {
468     $selected = $form{after_edit_cmd};
469     } elsif ($magic =~ /Const|Config|CSS/) {
470     $selected = 'edit';
471     }
472     my $afteredit = <<EOH;
473     <select name="after_edit_cmd">
474     <option value="default" label="@{[&Resource('Edit:SaveAndDefault',escape=>1)]}"@{[$selected eq 'default' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndDefault',escape=>1)]}</option>
475     <option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option>
476     <option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option>
477     </select>
478     EOH
479 wakaba 1.8
480     =pod
481    
482     my $f = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'form');
483     $f->set_attribute (action => SuikaWiki::Plugin->uri ('wiki');
484     $f->set_attribute (method => 'post');
485 w 1.9 if (!$option{conflict}) {
486 wakaba 1.8 for ($f->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'label')) {
487     for ($_->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'input')) {
488     $f->set_attribute (type => 'submit');
489     $f->set_attribute (value => SuikaWiki::Plugin->resource ('Edit:Save'));
490     }
491 w 1.9 #<input type=hidden name=mycmd value=write/>
492 wakaba 1.8 $_->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'kbd', value => 'S');
493     }
494     }
495    
496     =cut
497    
498     my $f = <<"EOD";
499 wakaba 1.1 <form action="$uri{wiki}" method="post">
500 w 1.9 @{[ $option{conflict} ? '' : qq(<label><input type="submit" value="@{[SuikaWiki::Plugin->resource('Edit:Save',escape=>1)]}" /><kbd>S</kbd></label>) ]}
501     @{[ $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 />
502 wakaba 1.1 <input type="hidden" name="myLastModified" value="$option{last_modified}" />
503 w 1.9 <input type="hidden" name="mycmd" value="write" />
504     <input type="hidden" name="mypage" value="@{[SuikaWiki::Plugin->escape($form{mypage})]}" />
505 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 />
506 wakaba 1.1 @{[
507     $option{admin} ?
508     qq(
509 w 1.9 <label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]} />@{[SuikaWiki::Plugin->resource('Edit:Freeze',escape=>1)]}</label>
510     <label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]} />@{[SuikaWiki::Plugin->resource('Edit:DontFreeze',escape=>1)]}</label><br />)
511 wakaba 1.1 : ""
512     ]}
513     @{[
514     $option{conflict} ? "" :
515     qq(
516 w 1.9 <label><input type="checkbox" name="mytouch" value="on" checked="checked" />@{[SuikaWiki::Plugin->resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br />
517     <label><input type="submit" value="@{[SuikaWiki::Plugin->resource('Edit:Save',escape=>1)]}" accesskey="S" /><kbd>S</kbd></label>
518 wakaba 1.1 $afteredit
519     )
520     ]}
521     </form>
522     EOD
523     $f;
524     }
525    
526 w 1.9 # [move to SuikaWiki::WikiDB]
527 wakaba 1.1 sub is_editable {
528     my ($page) = @_;
529 wakaba 1.4 return 0 unless SuikaWiki::Name::Space::validate_name ($page);
530     return 0 if $page =~ /[\x00-\x20\[\]\x7F]/;
531 wakaba 1.2 1;
532 wakaba 1.1 }
533    
534 w 1.9 # [move to SuikaWiki::WikiDB]
535 wakaba 1.1 sub frozen_reject {
536     my ($isfrozen) = $database->meta (IsFrozen => $form{mypage});
537     my ($willbefrozen) = $form{myfrozen};
538     if (not $isfrozen and not $willbefrozen) {
539     # You need no check.
540     return 0;
541     } elsif (valid_password($form{mypassword})) {
542     # You are admin.
543     return 0;
544     } else {
545 wakaba 1.12.2.2 print "Status: 403 Unauthorized\n";
546     &_do_view_msg (-view => '-error', -page => $form{mypage},
547     error_message => SuikaWiki::Plugin->resource ('Error:PasswordIsIncorrect'));
548 wakaba 1.1 exit;
549     }
550     }
551    
552 w 1.9 # [move to SuikaWiki::WikiDB]
553 wakaba 1.8 sub is_frozen ($) { SuikaWiki::Plugin->_database->meta (IsFrozen => $_[0]) ? 1 : 0 }
554 wakaba 1.1
555 w 1.9 # [to be obsolete]
556 wakaba 1.1 sub do_comment {
557     my ($content) = $database{$form{mypage}};
558     my $default_name; ## this code is not strict.
559     $default_name = $1 if $content =~ /default-name="([^"]+)"/;
560 w 1.9 my @time = gmtime (time);
561     my $datestr = sprintf '[WEAK[%04d-%02d-%02d %02d:%02d:%02d +00:00]]', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
562 wakaba 1.1 my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
563     ($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
564     if ($namestr =~ /^(?:>>)?[0-9]/) {
565     $namestr = qq( ''$namestr'': );
566     } elsif (length $namestr) {
567     $namestr = qq( ''[[$namestr]]'': );
568     }
569     my $anchor = &get_new_anchor_index ($content);
570     my $i = 1; my $o = 0;
571     $content =~ s{(\[\[\#r?comment\]\])}{
572     my $embed = $1;
573     if ($i == $form{comment_index}) {
574     if ($embed ne '[[#rcomment]]') {
575     $embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
576     } else {
577     $embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
578     }
579     }
580     $i++; $embed;
581     }ge;
582     unless ($o) {
583     $content = "#?SuikaWiki/0.9\n\n" unless $content;
584     $content .= "\n" unless $content =~ /\n$/s;
585     $content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
586     }
587     $form{__comment_anchor_index} = $anchor;
588     if ($form{mymsg} || $form{myname}) {
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 w 1.9 # [move to SuikaWiki::Plugin::WikiForm]
599 wakaba 1.1 sub get_new_anchor_index ($) {
600     my $content = shift;
601     my $anchor = 0;
602     $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
603     $anchor + 1;
604     }
605    
606 w 1.9 # [move to SuikaWiki::Plugin::WikiForm]
607 wakaba 1.1 sub do_wikiform {
608     my $content = $database{$form{mypage}};
609     my $anchor = &get_new_anchor_index ($content);
610     my $write = 0;
611     my $i = 1;
612     $content =~ s{$embed_command{form}}{
613     my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
614     if (($wfname && $wfname eq $form{wikiform_targetform})
615     || $i == $form{wikiform_index}) {
616     $template =~ s/\\([\\'])/$1/g;
617     $option =~ s/\\([\\'])/$1/g;
618     my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
619     $param->{page} = $form{mypage};
620     $param->{form_index} = $i;
621     $param->{form_name} = $wfname;
622     $param->{anchor_index} = $anchor;
623     $param->{argv} = \%form;
624     $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
625     $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
626 w 1.9 SuikaWiki::Plugin->formatter ('form_option')->replace ($option, $param);
627 wakaba 1.1 my $t = 1;
628     for (keys %{$param->{require}||{}}) {
629     (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
630     }
631 w 1.9 $t = SuikaWiki::Plugin->formatter ('form_template')->replace ($template, $param) if $t;
632 wakaba 1.1 if (length $t) {
633     if ($param->{output}->{reverse}) {
634     $embed .= "\n" . $t;
635     } else {
636     $embed = $t . "\n" . $embed;
637     }
638     $write = 1;
639     $form{__comment_anchor_index} = $anchor
640     if $param->{anchor_index_}; ## $anchor is used!
641     }
642     $form{__wikiform_anchor_index} = $i;
643     undef $form{wikiform_targetform}; ## Make sure never to match
644     undef $form{wikiform_index}; ## with WikiForm in rest of page!
645     }
646     $i++; $embed;
647     }ge;
648     unless ($write) {
649     #$content = "#?SuikaWiki/0.9\n\n" unless $content;
650     #$content .= "\n" unless $content =~ /\n$/s;
651     #
652     }
653     if ($write) {
654     $form{mymsg} = $content;
655     $form{mytouch} = 'on';
656     &do_write;
657     } else { ## Don't write!
658     $form{mycmd} = 'default';
659     &do_view;
660     }
661     }
662    
663 w 1.9 # [to be obsolete] ->Message::MIME::Charset
664 wakaba 1.1 sub code_convert {
665     require Jcode;
666 w 1.9 my ($contentref, $code, $srccode) = (shift, shift || $kanjicode, shift || undef);
667 wakaba 1.1 if ($code =~ /euc/) { $code = 'euc' }
668     elsif ($code =~ /iso/) { $code = 'jis' }
669     elsif ($code =~ /shi/) { $code = 'sjis' }
670     elsif ($code =~ /utf/) { $code = 'utf8' }
671 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;
672 wakaba 1.1 return $$contentref;
673     }
674    
675 w 1.9 # [to be obsolete] ->Message::Field::Date
676 wakaba 1.1 sub _rfc3339_date ($) {
677     my @time = gmtime (shift);
678     sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
679     }
680    
681 w 1.9 # [obsolete] ->SuikaWiki::SrcFormat
682     sub convert_format ($$$;%) {
683     my ($content, $d => $t, %option) = @_;
684     my $f = SuikaWiki::Plugin->format_converter ($d => $t);
685     if (ref $f) {
686     $option{content} = $content;
687     $option{from} = $d;
688     $option{to} = $t;
689     &$f ({}, bless (\%option, 'SuikaWiki::Plugin'));
690     } elsif ($option{-error_no_return}) {
691     return undef;
692     } elsif ($t =~ /HTML|xml/) {
693     if (length $content) {
694     my $r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'pre');
695     $r->append_text ($content);
696     return $r;
697     } else {
698     return '';
699     }
700     } else {
701     $content;
702     }
703     }
704    
705    
706 wakaba 1.1
707 w 1.9 # [obsolete] SuikaWiki::WikiDB
708 wakaba 1.1 package wiki::dummy;
709     sub mtime (@) {undef}
710     sub meta (@) {undef}
711     sub Yuki::YukiWikiDB2::meta (@) {undef}
712    
713     package main;
714 w 1.9 SuikaWiki::Plugin->import_plugins ();
715     $main::UA = $main::ENV{HTTP_USER_AGENT};
716     &open_db;
717     &init_form;
718     for (@{$SuikaWiki::Plugin::On{WikiDatabaseLoaded}||[]}) { &{$_} }
719     if ($command_do{$form{mycmd}}) {
720     &{$command_do{$form{mycmd}}}; # [to be obsolete]
721     } else {
722     &{$command_do{default}};
723     }
724     _wiki_exit ();
725 wakaba 1.1
726     =head1 NAME
727    
728 wakaba 1.4 lib/suikawiki.pl --- SuikaWiki transitional library
729 wakaba 1.1
730     =head1 AUTHOR
731    
732 wakaba 1.4 Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/> (YukiWiki)
733 wakaba 1.1
734 wakaba 1.4 Makio Tsukamoto <http://digit.que.ne.jp/> (WalWiki)
735 wakaba 1.1
736     Wakaba <w@suika.fam.cx>
737    
738     =head1 LICENSE
739    
740 wakaba 1.4 Copyright AUTHORS 2000-2003
741 wakaba 1.1
742     This program is free software; you can redistribute it and/or
743     modify it under the same terms as Perl itself.
744    
745     =cut
746    
747 wakaba 1.12.2.3 1; # $Date: 2003/10/09 09:47:45 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24