1 |
wakaba |
1.22 |
=head1 NAME |
2 |
|
|
|
3 |
wakaba |
1.24 |
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 |
wakaba |
1.22 |
|
13 |
|
|
=cut |
14 |
|
|
|
15 |
wakaba |
1.24 |
package wiki::driver::http; |
16 |
wakaba |
1.1 |
use strict; |
17 |
wakaba |
1.25 |
our $VERSION = do{my @r=(q$Revision: 1.24 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
18 |
wakaba |
1.24 |
|
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 |
wakaba |
1.22 |
|
37 |
wakaba |
1.24 |
## -- Preparing Dying Message as HTTP Response -- |
38 |
|
|
|
39 |
wakaba |
1.22 |
require SuikaWiki::Output::CGICarp; |
40 |
wakaba |
1.24 |
$SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT = 'Internal WikiEngine Error'; |
41 |
wakaba |
1.22 |
CGI::Carp::set_message (sub { |
42 |
wakaba |
1.24 |
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 |
wakaba |
1.22 |
}); |
59 |
|
|
|
60 |
wakaba |
1.24 |
## -- Loading Configuration File -- |
61 |
|
|
|
62 |
|
|
require 'wikidata/suikawiki-config.ph'; |
63 |
|
|
config ($WIKI); |
64 |
|
|
|
65 |
|
|
## -- Loading Modules -- |
66 |
wakaba |
1.1 |
|
67 |
wakaba |
1.24 |
use SuikaWiki::DB::Util::Error; |
68 |
|
|
require SuikaWiki::Name::Space; |
69 |
wakaba |
1.23 |
|
70 |
wakaba |
1.22 |
## -- Transitional Functions -- |
71 |
wakaba |
1.1 |
|
72 |
w |
1.9 |
# [to be obsolete] ->Message::MIME::Charset |
73 |
wakaba |
1.22 |
sub main::code_convert { |
74 |
wakaba |
1.24 |
my ($contentref, $code, $srccode) = @_; |
75 |
|
|
return $$contentref if $$contentref !~ /[^\x21-\x7E]/; |
76 |
wakaba |
1.1 |
require Jcode; |
77 |
wakaba |
1.15 |
$code ||= $WIKI->{config}->{charset}->{internal}; |
78 |
|
|
for ($code, $srccode) { |
79 |
wakaba |
1.23 |
s/[^0-9A-Za-z_.+-]+//g; |
80 |
wakaba |
1.15 |
if ($_ eq 'euc-jp') { $_ = 'euc' } |
81 |
|
|
elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' } |
82 |
|
|
elsif ($_ eq 'utf-8') { $_ = 'utf8' } |
83 |
|
|
elsif ($_ eq 'shift_jis') { $_ = 'sjis' } |
84 |
|
|
} |
85 |
wakaba |
1.19 |
if ($code eq 'iso-8859-1') { |
86 |
|
|
return $$contentref; ## TODO: |
87 |
|
|
} |
88 |
wakaba |
1.15 |
$$contentref = Jcode->new ($contentref, $srccode) |
89 |
|
|
## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE |
90 |
wakaba |
1.25 |
->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&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-)) |
91 |
wakaba |
1.23 |
# ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC)) |
92 |
|
|
->h2z (1) |
93 |
wakaba |
1.15 |
->$code; |
94 |
|
|
return $$contentref; |
95 |
wakaba |
1.1 |
} |
96 |
|
|
|
97 |
wakaba |
1.22 |
## -- Initializing WikiPlugin -- |
98 |
wakaba |
1.15 |
|
99 |
wakaba |
1.22 |
$WIKI->init_plugin; ## WikiPlugin manager |
100 |
wakaba |
1.15 |
|
101 |
wakaba |
1.22 |
## -- Initializing WikiView -- |
102 |
wakaba |
1.15 |
|
103 |
wakaba |
1.22 |
$WIKI->init_view; ## WikiView manager |
104 |
|
|
$WIKI->{view}->register_common_modes; |
105 |
|
|
|
106 |
|
|
## WikiView manager error handler |
107 |
|
|
push @{$WIKI->{event}->{view_error}}, sub { |
108 |
|
|
my ($wiki, $event) = @_; |
109 |
|
|
SuikaWiki::Plugin->module_package ('Error') |
110 |
|
|
->report_error_simple |
111 |
|
|
($wiki, WikiView => $event->{error}->text, |
112 |
|
|
-trace => 1) |
113 |
|
|
if $event->{error}->{-def}->{level} eq 'fatal' |
114 |
|
|
or $wiki->{config}->{debug}->{view}; |
115 |
|
|
unless ($event->{error}->{-def}->{level} eq 'fatal' |
116 |
|
|
or $event->{error}->{-def}->{level} eq 'stop') { |
117 |
|
|
$event->{cancel} = 1; |
118 |
|
|
} |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
## "view_in_mode" method definition |
122 |
|
|
push @{$WIKI->{event}->{view_in_mode}}, sub { |
123 |
|
|
my ($wiki, $opt) = @_; |
124 |
|
|
my $arg = {condition => {mode => $opt->{mode} || '-error', |
125 |
|
|
output => 'http-cgi', |
126 |
|
|
http_method => $opt->{method} || 'GET'}}; |
127 |
|
|
my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg); |
128 |
|
|
if (ref $viewobj) { |
129 |
|
|
$viewobj->main ($arg); |
130 |
|
|
} elsif ($opt->{mode} ne '-error') { |
131 |
|
|
report SuikaWiki::View::Implementation::error |
132 |
|
|
-type => 'WARN_VIEW_NOT_DEFINED', condition => $arg->{condition}, |
133 |
|
|
-object => $wiki->{view}, method => 'view_in_mode'; |
134 |
wakaba |
1.25 |
$wiki->view_in_mode (mode => '-wv--no-view-definition', method => 'GET'); |
135 |
wakaba |
1.22 |
## TODO: cache control for non-GET |
136 |
|
|
} else { |
137 |
|
|
die "Some error occured. Additionally, error reporting mode not defined"; |
138 |
|
|
} |
139 |
|
|
}; |
140 |
|
|
|
141 |
|
|
## WikiView formatting template error handler |
142 |
|
|
$WIKI->{config}->{catch}->{formatter_view} |
143 |
|
|
= catch Message::Util::Formatter::error with { |
144 |
|
|
my $err = shift; |
145 |
wakaba |
1.23 |
my $wiki = $err->{option}->{param}->{wiki}; |
146 |
wakaba |
1.22 |
SuikaWiki::Plugin->module_package ('Error') |
147 |
wakaba |
1.23 |
->reporting_formatting_template_error ($err, $wiki, |
148 |
|
|
trace => 1); |
149 |
wakaba |
1.22 |
$wiki->view_in_mode (mode => '-error', method => 'GET'); |
150 |
|
|
throw SuikaWiki::View::Implementation::error |
151 |
|
|
-type => 'ERROR_REPORTED'; |
152 |
|
|
}; |
153 |
|
|
|
154 |
|
|
## WikiView formatting template error handler (occured in "-error" mode) |
155 |
|
|
$WIKI->{config}->{catch}->{formatter_view_error} |
156 |
|
|
= catch Message::Util::Formatter::error with { |
157 |
|
|
my $err = shift; |
158 |
wakaba |
1.23 |
my $wiki = $err->{option}->{param}->{wiki}; |
159 |
wakaba |
1.22 |
SuikaWiki::Plugin->module_package ('Error') |
160 |
wakaba |
1.23 |
->reporting_formatting_template_error ($err, $wiki, |
161 |
|
|
trace => 1); |
162 |
wakaba |
1.22 |
$wiki->view_in_mode (mode => '-error-error', method => 'GET'); |
163 |
|
|
throw SuikaWiki::View::Implementation::error |
164 |
|
|
-type => 'ERROR_REPORTED'; |
165 |
|
|
}; |
166 |
wakaba |
1.15 |
|
167 |
wakaba |
1.24 |
## -- Preparing for WikiDatabase Error Reports -- |
168 |
wakaba |
1.22 |
{ |
169 |
|
|
my $error_report = sub { |
170 |
|
|
my ($wiki, $err) = @_; |
171 |
|
|
my $report = ($err->{-def}->{level} eq 'fatal' or |
172 |
|
|
$err->{-def}->{level} eq 'stop' or |
173 |
|
|
$wiki->{config}->{debug}->{db}) ? 1 : 0; |
174 |
|
|
if ($report and $wiki->{config}->{path_to}->{db__content__error_log}) { |
175 |
wakaba |
1.23 |
my $err_msg = caller (1).($err->{method}? '->'.$err->{method}: '').': ' |
176 |
|
|
.(defined $err->{file}? $err->{file} . ': ' : '') |
177 |
|
|
.(defined $err->{prop}? $err->{prop} . ': ' : '') |
178 |
|
|
.(defined $err->{key}? join ('//', @{$err->{key}}).': ':'') |
179 |
wakaba |
1.22 |
. $err->text; |
180 |
|
|
open LOG, '>>', $wiki->{config}->{path_to}->{db__content__error_log}; |
181 |
|
|
print LOG scalar (gmtime), " @{[$$]} {$err->{-def}->{level}}: ", |
182 |
|
|
$err_msg, "\n"; |
183 |
|
|
close LOG; |
184 |
|
|
} |
185 |
|
|
SuikaWiki::Plugin->module_package ('WikiDB') |
186 |
|
|
->reporting_error ($err, $wiki) if $report; |
187 |
wakaba |
1.24 |
if ($err->{-def}->{level} eq 'fatal' |
188 |
|
|
# or $err->{-def}->{level} eq 'stop' ## for debug |
189 |
|
|
) { |
190 |
wakaba |
1.22 |
$wiki->view_in_mode (mode => '-wdb--fatal-error'); |
191 |
|
|
throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED'; |
192 |
|
|
} |
193 |
|
|
}; |
194 |
|
|
unshift @{$WIKI->{event}->{database_loaded}}, sub { |
195 |
|
|
my $wiki = shift; |
196 |
|
|
unshift @{$wiki->{db}->{event}->{error}}, sub { |
197 |
|
|
my ($db, $event) = @_; |
198 |
|
|
$error_report->($wiki, $event->{error}); |
199 |
|
|
if ($event->{error}->{-type} eq 'INFO_DB_PROP_OPENED') { |
200 |
|
|
unshift @{$db->{prop}->{$event->{error}->{prop}}->{-db} |
201 |
|
|
->{event}->{error}}, sub { |
202 |
|
|
my ($db, $event) = @_; |
203 |
|
|
$error_report->($wiki, $event->{error}); |
204 |
|
|
}; |
205 |
|
|
} |
206 |
|
|
}; # database error |
207 |
|
|
}; # database_loaded |
208 |
wakaba |
1.15 |
} |
209 |
wakaba |
1.22 |
|
210 |
wakaba |
1.24 |
## -- Preparing for Misc. Error Reports -- |
211 |
wakaba |
1.15 |
|
212 |
wakaba |
1.22 |
if ($WIKI->{config}->{debug}->{general}) { |
213 |
|
|
$main::SIG{__WARN__} = sub { |
214 |
|
|
push @{$WIKI->{var}->{error}||=[]}, { |
215 |
|
|
description => Message::Markup::XML::Node->new |
216 |
|
|
(type => '#text', |
217 |
|
|
value => $_[0]), |
218 |
|
|
}; |
219 |
|
|
}; |
220 |
|
|
} |
221 |
w |
1.9 |
|
222 |
wakaba |
1.24 |
## -- (Declaring for) Initializing $wiki->{var} -- |
223 |
wakaba |
1.21 |
|
224 |
|
|
push @{$WIKI->{event}->{setting_initial_variables}}, sub { |
225 |
|
|
my $wiki = shift; |
226 |
wakaba |
1.24 |
## Database access mode |
227 |
wakaba |
1.15 |
$wiki->{var}->{db}->{read_only}->{'#default'} = 1; |
228 |
|
|
|
229 |
wakaba |
1.24 |
## Input parameter |
230 |
wakaba |
1.15 |
require SuikaWiki::Input::HTTP; |
231 |
wakaba |
1.21 |
$wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki); |
232 |
wakaba |
1.15 |
$wiki->{input}->{decoder}->{'#default'} = sub { |
233 |
|
|
my ($http, $s, $temp_params) = @_; |
234 |
wakaba |
1.18 |
return main::code_convert (\$s, $wiki->{config}->{charset}->{internal}, |
235 |
|
|
lc (@{$temp_params->{_charset_}||[]}[0]) |
236 |
wakaba |
1.15 |
|| $wiki->{config}->{charset}->{uri_param}); |
237 |
|
|
}; |
238 |
wakaba |
1.24 |
|
239 |
|
|
## User agent negotiation |
240 |
wakaba |
1.15 |
$wiki->{var}->{client}->{user_agent_name} |
241 |
|
|
= $wiki->{input}->meta_variable ('HTTP_USER_AGENT'); |
242 |
|
|
$wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent']; |
243 |
wakaba |
1.21 |
my $dg = SuikaWiki::Plugin->module_package ('Downgrade'); |
244 |
|
|
$dg->set_downgrade_flags ($wiki) if $dg; |
245 |
wakaba |
1.15 |
|
246 |
|
|
## TODO: PATH_INFO support |
247 |
wakaba |
1.24 |
|
248 |
|
|
## URI query parameter |
249 |
wakaba |
1.15 |
my $page = $wiki->{input}->meta_variable ('QUERY_STRING'); |
250 |
wakaba |
1.24 |
if ($page and not (index ($page, '=') > -1)) { |
251 |
wakaba |
1.15 |
$page =~ tr/+/ /; |
252 |
|
|
$page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge; |
253 |
|
|
$page = main::code_convert |
254 |
wakaba |
1.18 |
(\$page, $wiki->{config}->{charset}->{internal}, |
255 |
wakaba |
1.15 |
$wiki->{config}->{charset}->{uri_query}); |
256 |
|
|
} else { |
257 |
|
|
$page = $wiki->{input}->parameter ('mypage'); |
258 |
|
|
} |
259 |
wakaba |
1.25 |
|
260 |
|
|
## ISSUE: WikiName normalization needed |
261 |
|
|
$page =~ s/\s+/\x20/g; |
262 |
|
|
$page =~ s/^\x20//; $page =~ s/\x20+$//; |
263 |
|
|
$page =~ tr/\x00-\x1F\x7F//d; |
264 |
wakaba |
1.15 |
if ($page) { |
265 |
wakaba |
1.25 |
$wiki->{var}->{page} = $wiki->name ($page); |
266 |
wakaba |
1.15 |
} else { |
267 |
|
|
$wiki->{var}->{page} = $wiki->{config}->{page}->{Default}; |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
## Mode |
271 |
|
|
my $mode = $wiki->{input}->parameter ('mode') |
272 |
|
|
|| $wiki->{input}->parameter ('mycmd') ## for compatibility with |
273 |
|
|
|| 'default'; ## YukiWiki and SuikaWiki 2 |
274 |
|
|
$mode =~ tr/-/_/; |
275 |
wakaba |
1.24 |
if ($mode eq 'default' or $mode =~ /[^0-9A-Za-z_]/) { |
276 |
|
|
my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE'); |
277 |
wakaba |
1.15 |
## BUG: this code is not strict |
278 |
|
|
if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) { |
279 |
|
|
$mode = $1; $mode =~ tr/-/_/; |
280 |
|
|
} else { |
281 |
|
|
$mode = 'read'; |
282 |
|
|
} |
283 |
|
|
push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie'; |
284 |
|
|
} |
285 |
|
|
$wiki->{var}->{mode} = $mode; |
286 |
|
|
}; |
287 |
wakaba |
1.21 |
|
288 |
wakaba |
1.24 |
|
289 |
|
|
#### ---- Per-Session ---- |
290 |
|
|
|
291 |
wakaba |
1.22 |
## -- Initializing $wiki->{var} (Actual) -- |
292 |
|
|
|
293 |
|
|
$WIKI->init_variables; ## Per-session variables |
294 |
wakaba |
1.15 |
|
295 |
wakaba |
1.22 |
## -- Instantiating WikiView -- |
296 |
|
|
|
297 |
|
|
try { |
298 |
|
|
$WIKI->view_in_mode |
299 |
|
|
(mode => $WIKI->{var}->{mode}, |
300 |
|
|
method => $WIKI->{input}->meta_variable ('REQUEST_METHOD')); |
301 |
|
|
} catch SuikaWiki::DB::Util::Error with { |
302 |
|
|
my $err = shift; |
303 |
wakaba |
1.24 |
unless ($err->{-type} eq 'ERROR_REPORTED') { |
304 |
|
|
$WIKI->view_in_mode (mode => '-wdb--fatal-error'); |
305 |
|
|
} |
306 |
wakaba |
1.22 |
} catch SuikaWiki::View::Implementation::error with { |
307 |
|
|
my $err = shift; |
308 |
|
|
$err->throw unless $err->{-type} eq 'ERROR_REPORTED'; |
309 |
wakaba |
1.25 |
} catch SuikaWiki::Format::Definition::error with { |
310 |
|
|
$WIKI->view_in_mode (mode => '-wf--converter-not-found'); |
311 |
wakaba |
1.22 |
} finally { |
312 |
|
|
$WIKI->close_input; |
313 |
|
|
$WIKI->close_db; |
314 |
wakaba |
1.18 |
}; |
315 |
wakaba |
1.22 |
exit; |
316 |
wakaba |
1.18 |
|
317 |
wakaba |
1.24 |
|
318 |
|
|
|
319 |
wakaba |
1.22 |
## -- Terminating WikiEngine -- |
320 |
wakaba |
1.24 |
|
321 |
wakaba |
1.14 |
END { |
322 |
wakaba |
1.22 |
$WIKI->exit; |
323 |
|
|
} |
324 |
wakaba |
1.1 |
|
325 |
wakaba |
1.24 |
=head1 SYNOPSIS |
326 |
|
|
|
327 |
|
|
In your C<suikawiki.cgi>, write as: |
328 |
|
|
|
329 |
|
|
#!/usr/bin/perl |
330 |
|
|
BEGIN { $0 = ''.$0 } |
331 |
|
|
use strict; |
332 |
|
|
use lib qw(lib); |
333 |
|
|
use CGI::Carp qw(fatalsToBrowser); |
334 |
|
|
require 'suikawiki.pl'; |
335 |
|
|
|
336 |
|
|
=head1 SEE ALSO |
337 |
|
|
|
338 |
|
|
<http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SuikaWiki>, |
339 |
|
|
<http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SWHCS>, |
340 |
|
|
C<wiki.cgi>, C<wikidata/suikawiki-config.ph>, |
341 |
|
|
C<SuikaWiki::Implementation> |
342 |
|
|
|
343 |
wakaba |
1.1 |
=head1 LICENSE |
344 |
|
|
|
345 |
wakaba |
1.24 |
Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al. All rights reserved. |
346 |
wakaba |
1.1 |
|
347 |
|
|
This program is free software; you can redistribute it and/or |
348 |
|
|
modify it under the same terms as Perl itself. |
349 |
|
|
|
350 |
|
|
=cut |
351 |
|
|
|
352 |
wakaba |
1.25 |
1; # $Date: 2004/02/01 12:25:26 $ |