| 1 |
wakaba |
1.22 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
suikawiki.pl - SuikaWiki Driver for HTTP CGI Script |
| 4 |
|
|
|
| 5 |
|
|
=cut |
| 6 |
|
|
|
| 7 |
wakaba |
1.1 |
use strict; |
| 8 |
|
|
package main; |
| 9 |
wakaba |
1.18 |
our $WIKI; |
| 10 |
wakaba |
1.22 |
package wiki::driver::http; |
| 11 |
|
|
## -- Version of WikiDriver -- |
| 12 |
wakaba |
1.23 |
our $VERSION = do{my @r=(q$Revision: 1.22 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
| 13 |
wakaba |
1.22 |
$WIKI->{implementation_version} = 'hcs'.$VERSION; |
| 14 |
|
|
|
| 15 |
|
|
## -- Dying Message as HTTP Response -- |
| 16 |
|
|
require SuikaWiki::Output::CGICarp; |
| 17 |
|
|
$SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT |
| 18 |
|
|
= 'Internal WikiEngine Error'; |
| 19 |
|
|
CGI::Carp::set_message (sub { |
| 20 |
|
|
my $msg = shift; |
| 21 |
|
|
#$msg =~ s/&/&/g; |
| 22 |
|
|
#$msg =~ s/</</g; |
| 23 |
|
|
my $wiki_name_version = $WIKI->{implementation_name} .'/'. $WIKI->version; |
| 24 |
|
|
my $trace = Carp::longmess (); |
| 25 |
|
|
for ($trace, $wiki_name_version) |
| 26 |
|
|
{ s/&/&/g; s/</</g; |
| 27 |
|
|
s/([^\x20-\x7E])/sprintf '&#x%02X;', ord $1/ge; }; |
| 28 |
|
|
print STDOUT <<EOH; |
| 29 |
|
|
<!DOCTYPE html SYSTEM> |
| 30 |
|
|
<title>500 Internal WikiEngine Error</title> |
| 31 |
|
|
<h1>Internal WikiEngine Error</h1> |
| 32 |
|
|
<p>$msg</p> |
| 33 |
|
|
<p>$trace</p> |
| 34 |
|
|
<address>$wiki_name_version</address> |
| 35 |
|
|
EOH |
| 36 |
|
|
}); |
| 37 |
|
|
|
| 38 |
|
|
## -- Required Modules -- |
| 39 |
|
|
use SuikaWiki::DB::Util::Error; |
| 40 |
wakaba |
1.1 |
require SuikaWiki::Plugin; |
| 41 |
w |
1.9 |
require SuikaWiki::Name::Space; |
| 42 |
wakaba |
1.18 |
require SuikaWiki::SrcFormat; |
| 43 |
wakaba |
1.1 |
|
| 44 |
wakaba |
1.23 |
|
| 45 |
wakaba |
1.22 |
## -- Transitional Functions -- |
| 46 |
wakaba |
1.1 |
|
| 47 |
w |
1.9 |
# [to be obsolete] ->Message::MIME::Charset |
| 48 |
wakaba |
1.22 |
sub main::code_convert { |
| 49 |
wakaba |
1.1 |
require Jcode; |
| 50 |
wakaba |
1.15 |
my ($contentref, $code, $srccode) = @_; |
| 51 |
|
|
$code ||= $WIKI->{config}->{charset}->{internal}; |
| 52 |
|
|
for ($code, $srccode) { |
| 53 |
wakaba |
1.23 |
s/[^0-9A-Za-z_.+-]+//g; |
| 54 |
wakaba |
1.15 |
if ($_ eq 'euc-jp') { $_ = 'euc' } |
| 55 |
|
|
elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' } |
| 56 |
|
|
elsif ($_ eq 'utf-8') { $_ = 'utf8' } |
| 57 |
|
|
elsif ($_ eq 'shift_jis') { $_ = 'sjis' } |
| 58 |
|
|
} |
| 59 |
wakaba |
1.19 |
if ($code eq 'iso-8859-1') { |
| 60 |
|
|
return $$contentref; ## TODO: |
| 61 |
|
|
} |
| 62 |
wakaba |
1.15 |
$$contentref = Jcode->new ($contentref, $srccode) |
| 63 |
|
|
## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE |
| 64 |
|
|
->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\xA1\xE4\xA1\xE3\xA1\xA1\xA2\xAF\xA2\xB0\xA2\xB2\xA2\xB1\xA1\xA1\xC0" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-)) |
| 65 |
wakaba |
1.23 |
# ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC)) |
| 66 |
|
|
->h2z (1) |
| 67 |
wakaba |
1.15 |
->$code; |
| 68 |
|
|
return $$contentref; |
| 69 |
wakaba |
1.1 |
} |
| 70 |
|
|
|
| 71 |
wakaba |
1.18 |
# [to be obsolete] ->Message::Field::Date : Map |
| 72 |
wakaba |
1.22 |
sub main::_rfc3339_date ($) { |
| 73 |
wakaba |
1.1 |
my @time = gmtime (shift); |
| 74 |
|
|
sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0]; |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
wakaba |
1.20 |
## Obsolete |
| 78 |
wakaba |
1.22 |
sub SuikaWiki::Plugin::get_data ($$$$;%) { |
| 79 |
wakaba |
1.20 |
my ($self, $prop, $key, %opt) = @_; |
| 80 |
|
|
## TODO: common interface to WikiDB |
| 81 |
|
|
## TODO: error recovering |
| 82 |
|
|
$main::WIKI->{db}->get ($prop => $key); |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
wakaba |
1.22 |
## -- Initializing WikiPlugin -- |
| 86 |
wakaba |
1.15 |
|
| 87 |
wakaba |
1.22 |
$WIKI->init_plugin; ## WikiPlugin manager |
| 88 |
wakaba |
1.15 |
|
| 89 |
wakaba |
1.22 |
## -- Initializing WikiView -- |
| 90 |
wakaba |
1.15 |
|
| 91 |
wakaba |
1.22 |
$WIKI->init_view; ## WikiView manager |
| 92 |
|
|
$WIKI->{view}->register_common_modes; |
| 93 |
|
|
|
| 94 |
|
|
## WikiView manager error handler |
| 95 |
|
|
push @{$WIKI->{event}->{view_error}}, sub { |
| 96 |
|
|
my ($wiki, $event) = @_; |
| 97 |
|
|
SuikaWiki::Plugin->module_package ('Error') |
| 98 |
|
|
->report_error_simple |
| 99 |
|
|
($wiki, WikiView => $event->{error}->text, |
| 100 |
|
|
-trace => 1) |
| 101 |
|
|
if $event->{error}->{-def}->{level} eq 'fatal' |
| 102 |
|
|
or $wiki->{config}->{debug}->{view}; |
| 103 |
|
|
unless ($event->{error}->{-def}->{level} eq 'fatal' |
| 104 |
|
|
or $event->{error}->{-def}->{level} eq 'stop') { |
| 105 |
|
|
$event->{cancel} = 1; |
| 106 |
|
|
} |
| 107 |
|
|
}; |
| 108 |
|
|
|
| 109 |
|
|
## "view_in_mode" method definition |
| 110 |
|
|
push @{$WIKI->{event}->{view_in_mode}}, sub { |
| 111 |
|
|
my ($wiki, $opt) = @_; |
| 112 |
|
|
my $arg = {condition => {mode => $opt->{mode} || '-error', |
| 113 |
|
|
output => 'http-cgi', |
| 114 |
|
|
http_method => $opt->{method} || 'GET'}}; |
| 115 |
|
|
my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg); |
| 116 |
|
|
if (ref $viewobj) { |
| 117 |
|
|
$viewobj->main ($arg); |
| 118 |
|
|
} elsif ($opt->{mode} ne '-error') { |
| 119 |
|
|
report SuikaWiki::View::Implementation::error |
| 120 |
|
|
-type => 'WARN_VIEW_NOT_DEFINED', condition => $arg->{condition}, |
| 121 |
|
|
-object => $wiki->{view}, method => 'view_in_mode'; |
| 122 |
|
|
$wiki->view_in_mode (mode => '-error', method => 'GET'); |
| 123 |
|
|
## TODO: cache control for non-GET |
| 124 |
|
|
} else { |
| 125 |
|
|
die "Some error occured. Additionally, error reporting mode not defined"; |
| 126 |
|
|
} |
| 127 |
|
|
}; |
| 128 |
|
|
|
| 129 |
|
|
## WikiView formatting template error handler |
| 130 |
|
|
$WIKI->{config}->{catch}->{formatter_view} |
| 131 |
|
|
= catch Message::Util::Formatter::error with { |
| 132 |
|
|
my $err = shift; |
| 133 |
wakaba |
1.23 |
my $wiki = $err->{option}->{param}->{wiki}; |
| 134 |
wakaba |
1.22 |
SuikaWiki::Plugin->module_package ('Error') |
| 135 |
wakaba |
1.23 |
->reporting_formatting_template_error ($err, $wiki, |
| 136 |
|
|
trace => 1); |
| 137 |
wakaba |
1.22 |
$wiki->view_in_mode (mode => '-error', method => 'GET'); |
| 138 |
|
|
throw SuikaWiki::View::Implementation::error |
| 139 |
|
|
-type => 'ERROR_REPORTED'; |
| 140 |
|
|
}; |
| 141 |
|
|
|
| 142 |
|
|
## WikiView formatting template error handler (occured in "-error" mode) |
| 143 |
|
|
$WIKI->{config}->{catch}->{formatter_view_error} |
| 144 |
|
|
= catch Message::Util::Formatter::error with { |
| 145 |
|
|
my $err = shift; |
| 146 |
wakaba |
1.23 |
my $wiki = $err->{option}->{param}->{wiki}; |
| 147 |
wakaba |
1.22 |
SuikaWiki::Plugin->module_package ('Error') |
| 148 |
wakaba |
1.23 |
->reporting_formatting_template_error ($err, $wiki, |
| 149 |
|
|
trace => 1); |
| 150 |
wakaba |
1.22 |
$wiki->view_in_mode (mode => '-error-error', method => 'GET'); |
| 151 |
|
|
throw SuikaWiki::View::Implementation::error |
| 152 |
|
|
-type => 'ERROR_REPORTED'; |
| 153 |
|
|
}; |
| 154 |
wakaba |
1.15 |
|
| 155 |
wakaba |
1.22 |
## -- WikiDatabase Error Reporting -- |
| 156 |
|
|
{ |
| 157 |
|
|
my $error_report = sub { |
| 158 |
|
|
my ($wiki, $err) = @_; |
| 159 |
|
|
my $report = ($err->{-def}->{level} eq 'fatal' or |
| 160 |
|
|
$err->{-def}->{level} eq 'stop' or |
| 161 |
|
|
$wiki->{config}->{debug}->{db}) ? 1 : 0; |
| 162 |
|
|
if ($report and $wiki->{config}->{path_to}->{db__content__error_log}) { |
| 163 |
wakaba |
1.23 |
my $err_msg = caller (1).($err->{method}? '->'.$err->{method}: '').': ' |
| 164 |
|
|
.(defined $err->{file}? $err->{file} . ': ' : '') |
| 165 |
|
|
.(defined $err->{prop}? $err->{prop} . ': ' : '') |
| 166 |
|
|
.(defined $err->{key}? join ('//', @{$err->{key}}).': ':'') |
| 167 |
wakaba |
1.22 |
. $err->text; |
| 168 |
|
|
open LOG, '>>', $wiki->{config}->{path_to}->{db__content__error_log}; |
| 169 |
|
|
print LOG scalar (gmtime), " @{[$$]} {$err->{-def}->{level}}: ", |
| 170 |
|
|
$err_msg, "\n"; |
| 171 |
|
|
close LOG; |
| 172 |
|
|
} |
| 173 |
|
|
SuikaWiki::Plugin->module_package ('WikiDB') |
| 174 |
|
|
->reporting_error ($err, $wiki) if $report; |
| 175 |
|
|
if ($err->{-def}->{level} eq 'fatal' or $err->{-def}->{level} eq 'stop') { |
| 176 |
|
|
$wiki->view_in_mode (mode => '-wdb--fatal-error'); |
| 177 |
|
|
throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED'; |
| 178 |
|
|
} |
| 179 |
|
|
}; |
| 180 |
|
|
unshift @{$WIKI->{event}->{database_loaded}}, sub { |
| 181 |
|
|
my $wiki = shift; |
| 182 |
|
|
unshift @{$wiki->{db}->{event}->{error}}, sub { |
| 183 |
|
|
my ($db, $event) = @_; |
| 184 |
|
|
$error_report->($wiki, $event->{error}); |
| 185 |
|
|
if ($event->{error}->{-type} eq 'INFO_DB_PROP_OPENED') { |
| 186 |
|
|
unshift @{$db->{prop}->{$event->{error}->{prop}}->{-db} |
| 187 |
|
|
->{event}->{error}}, sub { |
| 188 |
|
|
my ($db, $event) = @_; |
| 189 |
|
|
$error_report->($wiki, $event->{error}); |
| 190 |
|
|
}; |
| 191 |
|
|
} |
| 192 |
|
|
}; # database error |
| 193 |
|
|
}; # database_loaded |
| 194 |
wakaba |
1.15 |
} |
| 195 |
wakaba |
1.22 |
|
| 196 |
|
|
## -- Misc. Error Reporting -- |
| 197 |
wakaba |
1.15 |
|
| 198 |
wakaba |
1.22 |
if ($WIKI->{config}->{debug}->{general}) { |
| 199 |
|
|
$main::SIG{__WARN__} = sub { |
| 200 |
|
|
push @{$WIKI->{var}->{error}||=[]}, { |
| 201 |
|
|
description => Message::Markup::XML::Node->new |
| 202 |
|
|
(type => '#text', |
| 203 |
|
|
value => $_[0]), |
| 204 |
|
|
}; |
| 205 |
|
|
}; |
| 206 |
|
|
} |
| 207 |
w |
1.9 |
|
| 208 |
wakaba |
1.22 |
## -- Initializing $wiki->{var} (Declaration) -- |
| 209 |
wakaba |
1.21 |
|
| 210 |
|
|
push @{$WIKI->{event}->{setting_initial_variables}}, sub { |
| 211 |
|
|
my $wiki = shift; |
| 212 |
wakaba |
1.15 |
$wiki->{var}->{db}->{read_only}->{'#default'} = 1; |
| 213 |
|
|
|
| 214 |
|
|
require SuikaWiki::Input::HTTP; |
| 215 |
wakaba |
1.21 |
$wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki); |
| 216 |
wakaba |
1.15 |
$wiki->{input}->{decoder}->{'#default'} = sub { |
| 217 |
|
|
my ($http, $s, $temp_params) = @_; |
| 218 |
wakaba |
1.18 |
return main::code_convert (\$s, $wiki->{config}->{charset}->{internal}, |
| 219 |
|
|
lc (@{$temp_params->{_charset_}||[]}[0]) |
| 220 |
wakaba |
1.15 |
|| $wiki->{config}->{charset}->{uri_param}); |
| 221 |
|
|
}; |
| 222 |
|
|
$wiki->{var}->{client}->{user_agent_name} |
| 223 |
|
|
= $wiki->{input}->meta_variable ('HTTP_USER_AGENT'); |
| 224 |
|
|
$wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent']; |
| 225 |
wakaba |
1.21 |
my $dg = SuikaWiki::Plugin->module_package ('Downgrade'); |
| 226 |
|
|
$dg->set_downgrade_flags ($wiki) if $dg; |
| 227 |
wakaba |
1.15 |
|
| 228 |
|
|
## TODO: PATH_INFO support |
| 229 |
|
|
my $page = $wiki->{input}->meta_variable ('QUERY_STRING'); |
| 230 |
|
|
if ($page && !(index ($page, '=') > -1)) { |
| 231 |
|
|
$page =~ tr/+/ /; |
| 232 |
|
|
$page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge; |
| 233 |
|
|
$page = main::code_convert |
| 234 |
wakaba |
1.18 |
(\$page, $wiki->{config}->{charset}->{internal}, |
| 235 |
wakaba |
1.15 |
$wiki->{config}->{charset}->{uri_query}); |
| 236 |
|
|
} else { |
| 237 |
|
|
$page = $wiki->{input}->parameter ('mypage'); |
| 238 |
|
|
} |
| 239 |
|
|
|
| 240 |
|
|
|
| 241 |
|
|
## TODO: SuikaWiki 3 WikiName |
| 242 |
|
|
$page =~ tr/\x00-\x20\x7F//d; |
| 243 |
|
|
$page = SuikaWiki::Name::Space::normalize_name ($page); |
| 244 |
|
|
if ($page) { |
| 245 |
|
|
$wiki->{var}->{page} = [split '//', $page]; |
| 246 |
|
|
} else { |
| 247 |
|
|
$wiki->{var}->{page} = $wiki->{config}->{page}->{Default}; |
| 248 |
|
|
} |
| 249 |
|
|
|
| 250 |
|
|
## Mode |
| 251 |
|
|
my $mode = $wiki->{input}->parameter ('mode') |
| 252 |
|
|
|| $wiki->{input}->parameter ('mycmd') ## for compatibility with |
| 253 |
|
|
|| 'default'; ## YukiWiki and SuikaWiki 2 |
| 254 |
|
|
$mode =~ tr/-/_/; |
| 255 |
|
|
if ($mode eq 'default' || $mode =~ /[^0-9A-Za-z_]/) { |
| 256 |
|
|
## BUG: this code is not strict |
| 257 |
|
|
my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE'); |
| 258 |
|
|
if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) { |
| 259 |
|
|
$mode = $1; $mode =~ tr/-/_/; |
| 260 |
|
|
} else { |
| 261 |
|
|
$mode = 'read'; |
| 262 |
|
|
} |
| 263 |
|
|
push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie'; |
| 264 |
|
|
} |
| 265 |
|
|
$wiki->{var}->{mode} = $mode; |
| 266 |
|
|
}; |
| 267 |
wakaba |
1.21 |
|
| 268 |
wakaba |
1.22 |
## -- Initializing $wiki->{var} (Actual) -- |
| 269 |
|
|
|
| 270 |
|
|
$WIKI->init_variables; ## Per-session variables |
| 271 |
wakaba |
1.15 |
|
| 272 |
wakaba |
1.22 |
## -- Instantiating WikiView -- |
| 273 |
|
|
|
| 274 |
|
|
try { |
| 275 |
|
|
$WIKI->view_in_mode |
| 276 |
|
|
(mode => $WIKI->{var}->{mode}, |
| 277 |
|
|
method => $WIKI->{input}->meta_variable ('REQUEST_METHOD')); |
| 278 |
|
|
} catch SuikaWiki::DB::Util::Error with { |
| 279 |
|
|
my $err = shift; |
| 280 |
|
|
$err->throw unless $err->{-type} eq 'ERROR_REPORTED'; |
| 281 |
|
|
} catch SuikaWiki::View::Implementation::error with { |
| 282 |
|
|
my $err = shift; |
| 283 |
|
|
$err->throw unless $err->{-type} eq 'ERROR_REPORTED'; |
| 284 |
|
|
} finally { |
| 285 |
|
|
$WIKI->close_input; |
| 286 |
|
|
$WIKI->close_db; |
| 287 |
wakaba |
1.18 |
}; |
| 288 |
wakaba |
1.22 |
exit; |
| 289 |
wakaba |
1.18 |
|
| 290 |
wakaba |
1.22 |
## -- Terminating WikiEngine -- |
| 291 |
wakaba |
1.14 |
END { |
| 292 |
wakaba |
1.22 |
$WIKI->exit; |
| 293 |
|
|
} |
| 294 |
wakaba |
1.1 |
|
| 295 |
|
|
=head1 LICENSE |
| 296 |
|
|
|
| 297 |
wakaba |
1.23 |
Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al |
| 298 |
wakaba |
1.1 |
|
| 299 |
|
|
This program is free software; you can redistribute it and/or |
| 300 |
|
|
modify it under the same terms as Perl itself. |
| 301 |
|
|
|
| 302 |
|
|
=cut |
| 303 |
|
|
|
| 304 |
wakaba |
1.23 |
1; # $Date: 2003/12/26 09:40:28 $ |