# -*- perl -*- use strict; package wiki; our $VERSION = do{my @r=(q$Revision: 1.13 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; package main; binmode STDOUT; binmode STDIN; use Fcntl; require SuikaWiki::Plugin; our %embed_command = ( form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/, ); our ($modifier_dbtype,%uri,%PathTo,%PageName,$kanjicode); our %form; our %database; our $database = bless {}, 'wiki::dummy'; my %command_do = ( default => \&do_view, adminchangepassword => \&do_adminchangepassword, write => \&do_write, comment => \&do_comment, RandomJump => sub { my @list = keys %main::database; &main::_http_see_other (page => $list[rand @list]); }, wikiform => \&do_wikiform, ); our $UA = ''; ## User agent name $| = 1; require SuikaWiki::Name::Space; my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml'; sub _wiki_exit () { &close_db; exit; } sub do_view { require SuikaWiki::View; my $content = $main::database{$main::form{mypage}}; my $lm = SuikaWiki::Plugin->_database->mtime ($main::form{mypage}); ## Determine mode my $view = $form{mycmd}; if (!$view || $view eq 'default' || $view =~ /[^0-9A-Za-z_]/) { ## BUG: this code is not strict if ($main::ENV{HTTP_COOKIE} =~ /SelectedMode=([0-9A-Za-z_-]+)/) { $view = $1; $view =~ tr/-/_/; } else { $view = 'read'; } } ## Get content and its meta info my ($magic, $content) = SuikaWiki::Plugin->magic_and_content ($content); $magic ||= '#?SuikaWiki/0.9'; my $o = bless {param => \%main::form, page => $main::form{mypage}, toc => [], magic => $magic, content => $content, &main::_compatible_options ()}, 'SuikaWiki::Plugin'; my $view_def = SuikaWiki::View->definition ($view); if (!$view_def->check ($o)) { print "Status: 406 Unsupported Media Type\n"; $view = '-UnsupportedMediaType'; $view_def = SuikaWiki::View->definition ($view); } my $prop = $view_def->properties; my $media = $prop->{media}; if ($prop->{xmedia} && $main::UA =~ /Gecko/) { ## TODO: conneg $media = $prop->{xmedia}; $o->{media} = $media; } elsif ($main::UA =~ m#Mozilla/0\..+Windows#) { $main::kanjicode = 'shift_jis'; } if ($prop->{preprocess}) { _wiki_exit () unless &{$prop->{preprocess}} (o => $o); } ## Output CGI/HTTP headers if ($magic =~ m!^\#\?SuikaWiki/0.9!) { &main::print_header ($main::form{mypage}, -last_modified => ($magic =~ /interactive="yes"/ ? time : $lm), -expires => ($magic =~ /interactive="yes"/ ? 1 : undef), o => $o, -media => $media, -view => $view_def, -magic => $magic, content => $content); } else { &main::print_header($main::form{mypage}, -media => $media, -view => $view_def, -magic => $magic, -last_modified => $lm, o => $o); } ## Output HTTP message body my $fmt = SuikaWiki::Plugin->formatter ('view'); my $s = $fmt->replace ($view_def->as_string => $o, {formatter => $fmt}); if ($main::kanjicode eq 'euc') { #require Compress::Zlib; #print scalar Compress::Zlib::memGzip (''.$s); print $s; } else { $s .= ''; print &main::code_convert (\$s => $main::kanjicode); } } sub _do_view_msg (%) { require SuikaWiki::View; my %option = @_; my $o = $option{-o} || bless {param => \%form, page => $option{-page}, &_compatible_options ()}, 'SuikaWiki::Plugin'; $o->{toc} = []; $o->{condition} = \%option; ## This parameter really used?? my $view_def = SuikaWiki::View->definition ($option{-view}); unless ($view_def->check ($o)) { print "Status: 406 Unsupported Media Type\n"; $option{-view} = '-UnsupportedMediaType'; $view_def = SuikaWiki::View->definition ($option{-view}); } my $prop = $view_def->properties; my $media = $prop->{media}; if ($prop->{xmedia} && $UA =~ /Gecko/) { $media = $prop->{xmedia}; $o->{media} = $media; } &print_header($option{-page}, -media => $media, -view => $view_def, o => $o, -goto => $option{-goto}); ## Output HTTP message body my $fmt = SuikaWiki::Plugin->formatter ('view'); my $s = $fmt->replace ($view_def->as_string => $o, {formatter => $fmt}); if ($main::kanjicode eq 'euc') { print $s; } else { print &main::code_convert (\$s => $main::kanjicode); } } # [move to SuikaWiki::Plugin::WikiAdmin] sub do_adminchangepassword { if ($form{mynewpassword} ne $form{mynewpassword2}) { &_do_view_msg (-view => '-error', -page => $form{mypage}, error_message => &Resource ('Error:PasswordMismatch')); return; } my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage}); if ($validpassword_crypt) { if (not &valid_password($form{myoldpassword})) { &_do_view_msg (-view => '-error', -page => $form{mypage}, error_message => &Resource ('Error:PasswordIsIncorrect')); return; } } my ($sec, $min, $hour, $day, $mon, $year, $weekday) = localtime(time); my (@token) = ('0'..'9', 'A'..'Z', 'a'..'z'); my $salt1 = $token[(time | $$) % scalar(@token)]; my $salt2 = $token[($sec + $min*60 + $hour*60*60) % scalar(@token)]; my $crypted = crypt($form{mynewpassword}, "$salt1$salt2"); $database->meta (AdminPassword => $PageName{AdminSpecialPage} => $crypted); &_do_view_msg (-view => '-wrote', -page => $form{mypage}); } # [move to SuikaWiki::WikiDB] sub valid_password ($) { my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage}); return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0; } # [move to SuikaWiki::Plugin::WikiEdit] sub do_write { if (&frozen_reject()) { return; } if (not &is_editable($form{mypage})) { &_do_view_msg (-view => '-error', -page => $form{mypage}, error_message => &Resource ('Error:ThisPageIsUneditable')); return; } ## Check confliction if ($form{myLastModified} ne $database->mtime ($form{mypage})) { &_do_view_msg (-view => '-conflict', -page => $form{mypage}); return; } if ($form{mymsg}) { if ($form{mytouch} || !ref $database) { $database{$form{mypage}} = $form{mymsg}; } else { $database->STORE ($form{mypage} => $form{mymsg}, -touch => 0); } $database->meta (IsFrozen => $form{mypage} => 0 + $form{myfrozen}); my $uri = SuikaWiki::Plugin->_uri_wiki_page ($form{mypage}, mode => ($form{after_edit_cmd}||'default'), with_lm => 1, absolute => 1); $uri .= qq(;after_edit_cmd=@{[SuikaWiki::Plugin->encode($form{after_edit_cmd})]}) if $form{after_edit_cmd}; if ($form{__comment_anchor_index}) { $uri .= qq(#anchor-$form{__comment_anchor_index}); } elsif ($form{__wikiform_anchor_index}) { $uri .= qq(#wikiform-$form{__wikiform_anchor_index}); } &_http_see_other (uri => $uri, page => $form{mypage}, alternate_view => '-wrote'); } else { delete $database{$form{mypage}}; &_do_view_msg (-view => '-deleted', -page => $form{mypage}); } } sub _http_see_other (%) { my %o = @_; $o{uri} ||= SuikaWiki::Plugin->_uri_wiki_page ($o{page}, absolute => 1); if ($o{alternate_view} && ($main::ENV{SERVER_PROTOCOL} eq 'HTTP/0.9' || $main::ENV{SERVER_PROTOCOL} eq 'HTTP/1.0') && !($main::UA =~ m#Microsoft Internet Explorer|Mozilla#)) { &_do_view_msg (-view => $o{alternate_view}, -page => $o{page}, -goto => $o{uri}, -o => $o{o}); } else { my $status = q(303 See Other); if ($main::UA =~ m!Mozilla/[0-4]\.|Microsoft Internet Explorer!) { $status = q(302 See Other); } my $euri = SuikaWiki::Plugin->escape ($o{uri}); print qq(Status: $status Location: $o{uri} Content-Type: text/html Content-Language: en
See <$euri>.
); } _wiki_exit (); } sub _compatible_options () { (use_anchor_name => ($main::UA =~ m#Mozilla/[1-4]\.|Microsoft Internet Explorer# ? 1 : 0)); } sub print_header ($;%) { my ($page, %option) = @_; if ($main::ENV{HTTP_IF_MODIFIED_SINCE} && $option{-last_modified}) { ## TODO: use Message::Field::Date if ($option{-view}->properties->{if}->{modified_since} && $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]/) { require Time::Local; my ($d, $M, $y, $h, $m, $s) = ($1, $2, $3, $4, $5, $6); $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}; #$y += 1900 if $y < 100; ## BUG: don't conform HTTP spec my $t = Time::Local::timegm_nocheck ($s, $m, $h, $d, $M, $y); if ($option{-last_modified} <= $t) { print "Status: 304 Not Modified\n\n"; _wiki_exit (); } } } my $UA = SuikaWiki::Plugin->user_agent_names; $option{o}->{-header}->{class}->{frozen} = 1 if &main::is_frozen ($page); $option{o}->{-header}->{class}->{'wiki-page-obsoleted'} = 1 if $option{-magic} =~ /obsoleted="yes"/; $option{o}->{-header}->{additional_html_element} ||= SuikaWiki::Markup::XML->new (type => '#fragment'); print "Vary: Negotiate,User-Agent,Accept-Language,Accept\n"; if ($option{-goto}) { if ($UA =~ m#Opera|MSIE 2\.#) { ## WARNING: This code may output unsafe HTML document if $option{-goto} is unclean. $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#; print qq{Refresh: 0; url=$option{-goto}\n}; for ($option{o}->{-header}->{additional_html_element}->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'meta')) { $_->set_attribute ('http-equiv' => 'refresh'); $_->set_attribute (content => "0; url=$option{-goto}"); $_->option (use_EmptyElemTag => 1); } } else { $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#; print qq{Refresh: 0; url="$option{-goto}"\n}; for ($option{o}->{-header}->{additional_html_element}->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'meta')) { $_->set_attribute ('http-equiv' => 'refresh'); $_->set_attribute (content => qq(0; url="$option{-goto}")); $_->option (use_EmptyElemTag => 1); } } } print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified}; if ($option{-expires} != -1) { if (defined $option{-expires}) { ## TODO: Don't use asctime print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n}; } elsif ($option{-media}->{expires} != -1) { print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n}; } } if ($option{-media}->{charset} && $UA =~ m#Mozilla/[12]\.#) { ## UAs don't support official charset names but do non-official names my $ct = qq{$option{-media}->{type}; charset=@{[ &main::get_charset_name ($main::kanjicode, compatible => 1) ]}}; print qq{Content-Type: $ct\n}; for ($option{o}->{-header}->{additional_html_element}->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'meta')) { $_->set_attribute ('http-equiv' => 'content-type'); $_->set_attribute (content => $ct); } } elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic|Mozilla/0\.#) { ## Media types or UAs don't support charset parameter in HTTP header print qq{Content-Type: $option{-media}->{type}\n}; if ($option{-media}->{charset}) { for ($option{o}->{-header}->{additional_html_element}->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'meta')) { $_->set_attribute ('http-equiv' => 'content-type'); $_->set_attribute (content => qq($option{-media}->{type}; charset=).main::get_charset_name ($main::kanjicode, compatible => 1)); } } } else { ## Modern UAs and Media types with charset parameter my $type = $option{-media}->{type}; $type = 'application/xml' if ($type =~ m!^application/r(?:df|ss)\+xml$!) && ($UA =~ m#Gecko#); print qq{Content-Type: $type; charset=@{[&main::get_charset_name ($main::kanjicode)]}\n}; ## meta element is not needed } print <<"EOD"; Content-Style-Type: text/css EOD } sub get_charset_name ($;%) { my ($charset, %option) = (lc shift, @_); if ($charset =~ 'euc') { $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp'; } elsif ($charset =~ 'sjis' || $charset =~ 'shift') { $charset = $option{compatible} ? 'x-sjis' : 'shift_jis'; } elsif ($charset =~ 'jis') { $charset = 'iso-2022-jp'; } $charset; } =pod sub _decode_argv () { my $QS = $main::ENV{QUERY_STRING}; if ($main::ENV{PATH_INFO}) { die; # new format: not implemented yet } else { my %argv; if ($QS =~ /[&;=]/) { ## ?FOO=foo;BAR=bar;BAZ=baz for (split /[;&]/, $QS) { if (my ($n, $v) = split /=/, $_, 2) { for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'HH', $1/ge}; $argv{$n} = $v; } } } else { ## ?FOO-BAR $argv{page} = $QS; $argv{page} =~ tr/+/ /; $argv{page} =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'HH', $1/ge; } my $ie = $argv{ie}; ## Input coding system for ([qw/mypage page/], [qw/mycmd mode/]) { $argv{$_->[1]} ||= $argv{$_->[0]}; delete $argv{$_->[0]}; } for (keys %argv) { $argv{$_} = main::code_convert ($argv{$_}, $main::kanjicode, $ie); } for ([qw/mypage page/], [qw/mycmd mode/]) { $argv{$_->[0]} = $argv{$_->[1]}; } } } =cut sub init_form { ## TODO: Support multipart/form-data my $query = ''; if (uc $main::ENV{REQUEST_METHOD} eq 'POST') { if ($main::ENV{CONTENT_TYPE}=~ m!^application/(?:x-www|sgml)-form-urlencoded(?:$|\s*;)!) { read STDIN, $query, $main::ENV{CONTENT_LENGTH}; } else { $main::form{mycmd} = '___unsupported_media_type___'; $main::form{mypage} = $main::PageName{FrontPage}; return; } } $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING}; if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) { my $query = SuikaWiki::Plugin->decode ($main::ENV{QUERY_STRING}); $query = &main::code_convert (\$query, $main::kanjicode); $main::form{mypage} = $query; $main::form{mycmd} = 'default'; } else { for (split /[;&]/, $query) { if (my ($n, $v) = split /=/, $_, 2) { for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge}; $main::form{$n} = $v; } } unless (defined $form{mypage}) { $form{mypage} = $form{epage}; $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g; } $form{mypage} = &main::code_convert (\$form{mypage}, $kanjicode); } $form{mypage} =~ tr/\x00-\x20\x7F//d; $form{mypage} = SuikaWiki::Name::Space::normalize_name ($form{mypage}) || $PageName{FrontPage}; $form{mycmd} ||= $form{mode} || 'default'; $form{mycmd} =~ tr/-/_/; for ('mymsg', 'myname', grep /^(?:wikiform__|pi_)/, keys %form) { $form{$_} = &main::code_convert (\$form{$_}, $kanjicode); } } # [move to SuikaWiki::WikiDB] sub open_db { if ($main::modifier_dbtype eq 'dbmopen') { dbmopen(%main::database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $main::PathTo{WikiDataBase}"; } elsif ($main::modifier_dbtype eq 'AnyDBM_File') { eval q{use AnyDBM_File}; tie(%main::database, "AnyDBM_File", $main::PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $main::PathTo{WikiDataBase}"); } elsif ($main::modifier_dbtype eq 'Yuki::YukiWikiDB') { eval q{use Yuki::YukiWikiDB}; tie(%main::database, "Yuki::YukiWikiDB", $main::PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $main::PathTo{WikiDataBase}"); } else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta eval qq{use $modifier_dbtype}; $database = tie (%database, $modifier_dbtype => $PathTo{WikiDataBase}, -lock => 0, -backup => $wiki::diff::UseDiff, ## TODO: new diff i/f -logfile => $main::PathTo{WikiDatabaseErrorLog}) or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}"); } } # [move to SuikaWiki::WikiDB] sub close_db { if ($modifier_dbtype eq 'dbmopen') { dbmclose(%database); } else { untie(%database); } } # [move to SuikaWiki::Plugin::WikiEdit] sub editform (@) { my %option = @_; my $frozen = &is_frozen ($option{page}); $option{content} = $database{$option{page}} unless defined $option{content}; $option{content} = $database{NewPageTemplate} unless length $option{content}; $option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified}; my $magic = ''; $magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s; my $selected = 'default'; if ($form{after_edit_cmd}) { $selected = $form{after_edit_cmd}; } elsif ($magic =~ /Const|Config|CSS/) { $selected = 'edit'; } my $afteredit = <