1 |
=head1 NAME |
2 |
|
3 |
suikawiki.pl - SuikaWiki Driver as HTTP CGI Script (SWHCS) |
4 |
|
5 |
=head1 DESCRIPTION |
6 |
|
7 |
This script is a WikiDriver for SuikaWiki, working as HTTP CGI script. |
8 |
With this script, SuikaWiki WikiEngine can be controled via remote WWW |
9 |
user agents. |
10 |
|
11 |
This file is part of SuikaWiki. |
12 |
|
13 |
=cut |
14 |
|
15 |
package wiki::driver::http; |
16 |
use strict; |
17 |
our $VERSION = do{my @r=(q$Revision: 1.33 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
18 |
|
19 |
## These lines should be removed after utf8 support |
20 |
BEGIN { |
21 |
$Message::Util::Formatter::Base::Token = qr/[\w._+\x80-\xFF-]+/; |
22 |
require Message::Util::Formatter::Base; |
23 |
} |
24 |
|
25 |
## -- Constructing a new instance of the WikiEngine -- |
26 |
|
27 |
require SuikaWiki::Implementation; |
28 |
our $WIKI = SuikaWiki::Implementation->new; |
29 |
|
30 |
## -- Registering Version of the WikiDriver -- |
31 |
|
32 |
$WIKI->{driver_name} = 'SWHCS'; |
33 |
$WIKI->{driver_version} = $VERSION; |
34 |
$WIKI->{driver_uri_reference} |
35 |
= q<http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SWHCS>; |
36 |
|
37 |
## -- Preparing Dying Message as HTTP Response -- |
38 |
|
39 |
require SuikaWiki::Output::CGICarp; |
40 |
$SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT = 'Internal WikiEngine Error'; |
41 |
CGI::Carp::set_message (sub { |
42 |
my $msg = shift; ## Already escaped |
43 |
my $wiki_name_version = sprintf '%s/%s %s/%s', |
44 |
$WIKI->{driver_name}, $WIKI->{driver_version}, |
45 |
$WIKI->{engine_name}, $WIKI->{engine_version}; |
46 |
my $trace = Carp::longmess (); |
47 |
for ($trace, $wiki_name_version) { |
48 |
s/&/&/g; s/</</g; s/([^\x20-\x7E])/sprintf '&#x%02X;', ord $1/ge; |
49 |
}; |
50 |
print <<" EOH"; |
51 |
<!DOCTYPE html SYSTEM> |
52 |
<title lang="en">500 Internal WikiEngine Error</title> |
53 |
<h1 lang="en">Internal WikiEngine Error</h1> |
54 |
<p>$msg</p> |
55 |
<p>$trace</p> |
56 |
<address>$wiki_name_version</address> |
57 |
EOH |
58 |
}); |
59 |
|
60 |
## -- Loading Configuration File -- |
61 |
|
62 |
$_->($WIKI) for our @Config; |
63 |
|
64 |
## -- Setting Upper Bound for Too Many Accesses at the Same Time -- |
65 |
|
66 |
{ |
67 |
require SuikaWiki::DB::Util::Lock; |
68 |
my $lock = SuikaWiki::DB::Util::Lock |
69 |
->new (-directory => $WIKI->{config}->{path_to}->{db__lock__dir}, |
70 |
-name => 'main', |
71 |
-share => 1, |
72 |
-limit => $WIKI->{config}->{engine_max_access}); |
73 |
$lock->lock or do { |
74 |
require SuikaWiki::Output::HTTP; |
75 |
my $out = SuikaWiki::Output::HTTP->new; |
76 |
$out->{status_code} = 503; |
77 |
$out->{status_phrase} = q<WikiEngine Busy>; |
78 |
$out->add_header_field ('Retry-After' => 120); |
79 |
$out->{entity}->{media_type} = q<text/html>; |
80 |
$out->{entity}->{charset} = q<iso-8859-1>; |
81 |
$out->{entity}->{language} = [q<en>]; |
82 |
$out->{entity}->{body_is_octet_stream} = 1; |
83 |
my $wiki_name_version = sprintf '%s/%s %s/%s', |
84 |
$WIKI->{driver_name}, $WIKI->{driver_version}, |
85 |
$WIKI->{engine_name}, $WIKI->{engine_version}; |
86 |
for ($wiki_name_version) { |
87 |
s/&/&/g; s/</</g; s/([^\x20-\x7E])/sprintf '&#x%02X;', ord $1/ge; |
88 |
}; |
89 |
$out->{entity}->{body} = qq< |
90 |
<!DOCTYPE html SYSTEM> |
91 |
<title>503 WikiEngine Busy</title> |
92 |
<h1>WikiEngine Busy</h1> |
93 |
<p>WikiEngine is now busy and is unable to complete your request. |
94 |
Please retry again after a moment.</p> |
95 |
<address>$wiki_name_version</address> |
96 |
>; |
97 |
$out->output (output => 'http-cgi'); |
98 |
exit; |
99 |
}; |
100 |
END { $lock->unlock } |
101 |
} |
102 |
|
103 |
## -- Loading Modules -- |
104 |
|
105 |
use SuikaWiki::DB::Util::Error; |
106 |
|
107 |
## -- Transitional Functions -- |
108 |
|
109 |
# [to be obsolete] ->Message::MIME::Charset |
110 |
sub main::code_convert { |
111 |
my ($contentref, $code, $srccode) = @_; |
112 |
return $$contentref if $$contentref !~ /[^\x21-\x7E]/; |
113 |
require Jcode; |
114 |
$code ||= $WIKI->{config}->{charset}->{internal}; |
115 |
for ($code, $srccode) { |
116 |
s/[^0-9A-Za-z_.+-]+//g; |
117 |
if ($_ eq 'euc-jp') { $_ = 'euc' } |
118 |
elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' } |
119 |
elsif ($_ eq 'utf-8') { $_ = 'utf8' } |
120 |
elsif ($_ eq 'shift_jis') { $_ = 'sjis' } |
121 |
} |
122 |
if ($code eq 'iso-8859-1') { |
123 |
return $$contentref; ## TODO: |
124 |
} |
125 |
$$contentref = Jcode->new ($contentref, $srccode) |
126 |
## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE |
127 |
->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" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-)) |
128 |
# ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC)) |
129 |
->h2z (1) |
130 |
->$code; |
131 |
return $$contentref; |
132 |
} |
133 |
|
134 |
## -- Initializing WikiPlugin -- |
135 |
|
136 |
$WIKI->init_plugin; ## WikiPlugin manager |
137 |
|
138 |
## -- Initializing WikiView -- |
139 |
|
140 |
$WIKI->init_view; ## WikiView manager |
141 |
$WIKI->{view}->register_common_modes; |
142 |
|
143 |
## WikiView manager error handler |
144 |
push @{$WIKI->{event}->{view_error}}, sub { |
145 |
my ($wiki, $event) = @_; |
146 |
SuikaWiki::Plugin->module_package ('Error') |
147 |
->report_error_simple |
148 |
($wiki, WikiView => $event->{error}->text, |
149 |
-trace => 1) |
150 |
if $event->{error}->{-def}->{level} eq 'fatal' |
151 |
or $wiki->{config}->{debug}->{view}; |
152 |
unless ($event->{error}->{-def}->{level} eq 'fatal' |
153 |
or $event->{error}->{-def}->{level} eq 'stop') { |
154 |
$event->{cancel} = 1; |
155 |
} |
156 |
}; |
157 |
|
158 |
## "view_in_mode" method definition |
159 |
push @{$WIKI->{event}->{view_in_mode}}, sub { |
160 |
my ($wiki, $event) = @_; |
161 |
my $opt = $event->{view_in_mode}; |
162 |
my $arg = {condition => {mode => $opt->{mode} || '-error', |
163 |
output => 'http-cgi', |
164 |
http_method => $opt->{method} || 'GET'}}; |
165 |
my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg); |
166 |
if (ref $viewobj) { |
167 |
$viewobj->main ($arg); |
168 |
} elsif ($opt->{mode} ne '-error') { |
169 |
report SuikaWiki::View::Implementation::error |
170 |
-type => 'WARN_VIEW_NOT_DEFINED', condition => $arg->{condition}, |
171 |
-object => $wiki->{view}, method => 'view_in_mode'; |
172 |
$wiki->view_in_mode (mode => '-wv--no-view-definition', method => 'GET'); |
173 |
## TODO: cache control for non-GET |
174 |
} else { |
175 |
die "Some error occured. Additionally, error reporting mode not defined"; |
176 |
} |
177 |
}; |
178 |
|
179 |
## WikiView formatting template error handler |
180 |
$WIKI->{config}->{catch}->{formatter_view} |
181 |
= catch Message::Util::Formatter::error with { |
182 |
my $err = shift; |
183 |
my $wiki = $err->{option}->{param}->{wiki}; |
184 |
SuikaWiki::Plugin->module_package ('Error') |
185 |
->reporting_formatting_template_error ($err, $wiki, |
186 |
trace => 1); |
187 |
$wiki->view_in_mode (mode => '-error', method => 'GET'); |
188 |
throw SuikaWiki::View::Implementation::error |
189 |
-type => 'ERROR_REPORTED'; |
190 |
}; |
191 |
|
192 |
## WikiView formatting template error handler (occured in "-error" mode) |
193 |
$WIKI->{config}->{catch}->{formatter_view_error} |
194 |
= catch Message::Util::Formatter::error with { |
195 |
my $err = shift; |
196 |
my $wiki = $err->{option}->{param}->{wiki}; |
197 |
SuikaWiki::Plugin->module_package ('Error') |
198 |
->reporting_formatting_template_error ($err, $wiki, |
199 |
trace => 1); |
200 |
$wiki->view_in_mode (mode => '-error-error', method => 'GET'); |
201 |
throw SuikaWiki::View::Implementation::error |
202 |
-type => 'ERROR_REPORTED'; |
203 |
}; |
204 |
|
205 |
## -- Preparing for WikiDatabase Error Reports -- |
206 |
{ |
207 |
my $error_report = sub { |
208 |
my ($wiki, $err) = @_; |
209 |
if ($err->{-def}->{level} eq 'fatal') { |
210 |
$wiki->close_db if $wiki->{db}; |
211 |
} |
212 |
my $report = ($err->{-def}->{level} eq 'fatal' or |
213 |
$err->{-def}->{level} eq 'stop' or |
214 |
$wiki->{config}->{debug}->{db}) ? 1 : 0; |
215 |
if ($report and $wiki->{config}->{path_to}->{db__content__error_log}) { |
216 |
my $err_msg = caller (1).($err->{method}? '->'.$err->{method}: '').': ' |
217 |
.(defined $err->{file}? $err->{file} . ': ' : '') |
218 |
.(defined $err->{prop}? $err->{prop} . ': ' : '') |
219 |
.(defined $err->{key}? join ('//', @{$err->{key}}).': ':'') |
220 |
. $err->text |
221 |
. ($wiki->{config}->{debug}->{db} > 1 ? Carp::longmess () : ''); |
222 |
open LOG, '>>', $wiki->{config}->{path_to}->{db__content__error_log}; |
223 |
print LOG scalar (gmtime), " @{[$$]} {$err->{-def}->{level}}: ", |
224 |
$err_msg, "\n"; |
225 |
close LOG; |
226 |
} |
227 |
SuikaWiki::Plugin->module_package ('WikiDB') |
228 |
->reporting_error ($err, $wiki) if $report; |
229 |
if ($err->{-def}->{level} eq 'fatal' |
230 |
# or $err->{-def}->{level} eq 'stop' ## for debug |
231 |
) { |
232 |
$wiki->view_in_mode (mode => '-wdb--fatal-error'); |
233 |
throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED'; |
234 |
} |
235 |
}; |
236 |
unshift @{$WIKI->{event}->{database_loaded}}, sub { |
237 |
my $wiki = shift; |
238 |
unshift @{$wiki->{db}->{event}->{error}}, sub { |
239 |
my ($db, $event) = @_; |
240 |
$error_report->($wiki, $event->{error}); |
241 |
if ($event->{error}->{-type} eq 'INFO_DB_PROP_OPENED') { |
242 |
unshift @{$db->{prop}->{$event->{error}->{prop}}->{-db} |
243 |
->{event}->{error}}, sub { |
244 |
my ($db, $event) = @_; |
245 |
$error_report->($wiki, $event->{error}); |
246 |
}; |
247 |
} |
248 |
}; # database error |
249 |
}; # database_loaded |
250 |
} |
251 |
|
252 |
## -- Preparing for Misc. Error Reports -- |
253 |
|
254 |
if ($WIKI->{config}->{debug}->{general}) { |
255 |
$main::SIG{__WARN__} = sub { |
256 |
push @{$WIKI->{var}->{error}||=[]}, { |
257 |
description => Message::Markup::XML::Node->new |
258 |
(type => '#text', |
259 |
value => $_[0]), |
260 |
}; |
261 |
}; |
262 |
} |
263 |
|
264 |
## -- (Declaring for) Initializing $wiki->{var} -- |
265 |
|
266 |
push @{$WIKI->{event}->{setting_initial_variables}}, sub { |
267 |
my $wiki = shift; |
268 |
## Database access mode |
269 |
$wiki->{var}->{db}->{read_only}->{'#default'} = 1; |
270 |
$wiki->{var}->{db}->{lock_module}->{'#default'} |
271 |
= q<SuikaWiki::DB::Util::Lock>; |
272 |
$wiki->{var}->{db}->{read_lock_module}->{'#default'} |
273 |
= q<SuikaWiki::DB::Lock::NoLock>; |
274 |
|
275 |
## Input parameter |
276 |
require SuikaWiki::Input::HTTP; |
277 |
$wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki); |
278 |
$wiki->{input}->{decoder}->{'#default'} = sub { |
279 |
my ($http, $s, $temp_params) = @_; |
280 |
return main::code_convert (\$s, $wiki->{config}->{charset}->{internal}, |
281 |
lc (@{$temp_params->{_charset_}||[]}[0]) |
282 |
|| $wiki->{config}->{charset}->{uri_param}); |
283 |
}; |
284 |
|
285 |
## User agent negotiation |
286 |
$wiki->{var}->{client}->{user_agent_name} |
287 |
= $wiki->{input}->meta_variable ('HTTP_USER_AGENT'); |
288 |
$wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent']; |
289 |
try { |
290 |
my $dg = SuikaWiki::Plugin->module_package ('Downgrade'); |
291 |
$dg->set_downgrade_flags ($wiki) if $dg; |
292 |
} catch SuikaWiki::Plugin::error with { |
293 |
my $err = shift; |
294 |
$err->raise unless $err->{-type} eq 'PLUGIN_NOT_FOUND'; |
295 |
}; |
296 |
|
297 |
## TODO: PATH_INFO support |
298 |
|
299 |
## URI query parameter |
300 |
my $page = $wiki->{input}->meta_variable ('QUERY_STRING'); |
301 |
if ($page and not (index ($page, '=') > -1)) { |
302 |
$page =~ tr/+/ /; |
303 |
$page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge; |
304 |
$page = main::code_convert |
305 |
(\$page, $wiki->{config}->{charset}->{internal}, |
306 |
$wiki->{config}->{charset}->{uri_query}); |
307 |
} else { |
308 |
$page = $wiki->{input}->parameter ('mypage'); |
309 |
} |
310 |
|
311 |
## ISSUE: WikiName normalization needed |
312 |
$page =~ s/\s+/\x20/g; |
313 |
$page =~ s/^\x20//; $page =~ s/\x20+$//; |
314 |
$page =~ tr/\x00-\x1F\x7F//d; |
315 |
if ($page) { |
316 |
$wiki->{var}->{page} = $wiki->name ($page); |
317 |
} else { |
318 |
# $wiki->{var}->{page} = $wiki->{config}->{page}->{Default}; |
319 |
$wiki->{var}->{page} = $wiki->name ([]); |
320 |
} |
321 |
|
322 |
## Mode |
323 |
my $mode = $wiki->{input}->parameter ('mode') |
324 |
|| $wiki->{input}->parameter ('mycmd') ## for compatibility with |
325 |
|| 'default'; ## YukiWiki and SuikaWiki 2 |
326 |
$mode =~ tr/-/_/; |
327 |
my $data = $wiki->{input}->parameter ('wikiform__msg'); |
328 |
if (($mode eq 'wikiform' and $page =~ /^http:/) or |
329 |
($mode eq 'wikiform' and ($data =~ s/http:/http:/g) > 20)) { |
330 |
$mode = 'spam'; |
331 |
push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Negotiate'; |
332 |
} |
333 |
if ($mode eq 'default' or $mode =~ /[^0-9A-Za-z_]/) { |
334 |
my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE'); |
335 |
## BUG: this code is not strict |
336 |
if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) { |
337 |
$mode = $1; $mode =~ tr/-/_/; |
338 |
} elsif ($wiki->{var}->{client}->{downgrade}->{is_robot} and |
339 |
$wiki->{plugin}->module_package ('Robot', allow_undef => 1)) { |
340 |
$mode = 'robot--read'; |
341 |
} else { |
342 |
$mode = 'read'; |
343 |
} |
344 |
push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie'; |
345 |
} |
346 |
$wiki->{var}->{mode} = $mode; |
347 |
}; |
348 |
|
349 |
## -- Referer Logging -- |
350 |
push @{$WIKI->{event}->{input_close}}, sub { |
351 |
my ($wiki, $event) = @_; |
352 |
try { |
353 |
SuikaWiki::Plugin->module_package ('Referer') |
354 |
->add_referer (wiki => $wiki, |
355 |
uri => $wiki->{input}->meta_variable |
356 |
('HTTP_REFERER')); |
357 |
} catch SuikaWiki::Plugin::error with { |
358 |
my $err = shift; |
359 |
$err->raise unless $err->{-type} eq 'PLUGIN_NOT_FOUND'; |
360 |
}; |
361 |
}; |
362 |
|
363 |
#### ---- Per-Session ---- |
364 |
|
365 |
## -- Initializing $wiki->{var} (Actual) -- |
366 |
|
367 |
$WIKI->init_variables; ## Per-session variables |
368 |
|
369 |
## -- Instantiating WikiView -- |
370 |
|
371 |
try { |
372 |
$WIKI->view_in_mode |
373 |
(mode => $WIKI->{var}->{mode}, |
374 |
method => $WIKI->{input}->meta_variable ('REQUEST_METHOD')); |
375 |
} catch SuikaWiki::DB::Util::Error with { |
376 |
my $err = shift; |
377 |
unless ($err->{-type} eq 'ERROR_REPORTED') { |
378 |
$WIKI->view_in_mode (mode => '-wdb--fatal-error'); |
379 |
} |
380 |
} catch SuikaWiki::View::Implementation::error with { |
381 |
my $err = shift; |
382 |
$err->throw unless $err->{-type} eq 'ERROR_REPORTED'; |
383 |
} catch SuikaWiki::Format::Definition::error with { |
384 |
$WIKI->view_in_mode (mode => '-wf--converter-not-found'); |
385 |
} finally { |
386 |
$WIKI->close_input; |
387 |
$WIKI->close_db; |
388 |
}; |
389 |
exit; |
390 |
|
391 |
|
392 |
|
393 |
## -- Terminating WikiEngine -- |
394 |
|
395 |
END { |
396 |
$WIKI->exit; |
397 |
} |
398 |
|
399 |
=head1 SYNOPSIS |
400 |
|
401 |
In your C<suikawiki.cgi>, write as: |
402 |
|
403 |
#!/usr/bin/perl |
404 |
BEGIN { $0 = ''.$0 } |
405 |
use strict; |
406 |
use lib qw(lib); |
407 |
use CGI::Carp qw(fatalsToBrowser); |
408 |
require 'suikawiki.pl'; |
409 |
|
410 |
=head1 SEE ALSO |
411 |
|
412 |
<http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SuikaWiki>, |
413 |
<http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SWHCS>, |
414 |
C<wiki.cgi>, C<wikidata/suikawiki-config.ph>, |
415 |
C<SuikaWiki::Implementation> |
416 |
|
417 |
=head1 LICENSE |
418 |
|
419 |
Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al. All rights reserved. |
420 |
|
421 |
This program is free software; you can redistribute it and/or |
422 |
modify it under the same terms as Perl itself. |
423 |
|
424 |
=cut |
425 |
|
426 |
1; # $Date: 2006/05/28 01:59:51 $ |