/
UI.pm
655 lines (538 loc) · 20.1 KB
/
UI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# See bottom of file for license and copyright information
=begin TML
---+!! package Foswiki::UI
Coordinator of execution flow and service functions used by the UI packages
=cut
package Foswiki::UI;
use strict;
use warnings;
BEGIN {
#Monitor::MARK("Start of BEGIN block in UI.pm");
$Foswiki::cfg{SwitchBoard} ||= {};
# package - perl package that contains the method for this request
# function - name of the function in package
# context - hash of context vars to define
# allow - hash of HTTP methods to allow (all others are denied)
# deny - hash of HTTP methods that are denied (all others are allowed)
# 'deny' is not tested if 'allow' is defined
# The switchboard can contain entries either as hashes or as arrays.
# The array format specifies [0] package, [1] function, [2] context
# and should be used when declaring scripts from plugins that must work
# with Foswiki 1.0.0 and 1.0.4.
$Foswiki::cfg{SwitchBoard}{attach} = {
package => 'Foswiki::UI::Attach',
function => 'attach',
context => { attach => 1 },
};
$Foswiki::cfg{SwitchBoard}{changes} = {
package => 'Foswiki::UI::Changes',
function => 'changes',
context => { changes => 1 },
};
$Foswiki::cfg{SwitchBoard}{edit} = {
package => 'Foswiki::UI::Edit',
function => 'edit',
context => { edit => 1 },
};
$Foswiki::cfg{SwitchBoard}{login} = {
package => undef,
function => 'logon',
context => { ( login => 1, logon => 1 ) },
};
$Foswiki::cfg{SwitchBoard}{logon} = {
package => undef,
function => 'logon',
context => { ( login => 1, logon => 1 ) },
};
$Foswiki::cfg{SwitchBoard}{manage} = {
package => 'Foswiki::UI::Manage',
function => 'manage',
context => { manage => 1 },
allow => { POST => 1 },
};
$Foswiki::cfg{SwitchBoard}{oops} = {
package => 'Foswiki::UI::Oops',
function => 'oops_cgi',
context => { oops => 1 },
};
$Foswiki::cfg{SwitchBoard}{preview} = {
package => 'Foswiki::UI::Preview',
function => 'preview',
context => { preview => 1 },
};
$Foswiki::cfg{SwitchBoard}{previewauth} =
$Foswiki::cfg{SwitchBoard}{preview};
$Foswiki::cfg{SwitchBoard}{rdiff} = {
package => 'Foswiki::UI::RDiff',
function => 'diff',
context => { diff => 1 },
};
$Foswiki::cfg{SwitchBoard}{rdiffauth} = $Foswiki::cfg{SwitchBoard}{rdiff};
$Foswiki::cfg{SwitchBoard}{register} = {
package => 'Foswiki::UI::Register',
function => 'register_cgi',
context => { register => 1 },
# method verify must allow GET; protect in Foswiki::UI::Register
#allow => { POST => 1 },
};
$Foswiki::cfg{SwitchBoard}{rename} = {
package => 'Foswiki::UI::Rename',
function => 'rename',
context => { rename => 1 },
# Rename is 2 stage; protect in Foswiki::UI::Rename
#allow => { POST => 1 },
};
$Foswiki::cfg{SwitchBoard}{resetpasswd} = {
package => 'Foswiki::UI::Passwords',
function => 'resetPassword',
context => { resetpasswd => 1 },
allow => { POST => 1 },
};
$Foswiki::cfg{SwitchBoard}{rest} = {
package => 'Foswiki::UI::Rest',
function => 'rest',
context => { rest => 1 },
};
$Foswiki::cfg{SwitchBoard}{restauth} = $Foswiki::cfg{SwitchBoard}{rest};
$Foswiki::cfg{SwitchBoard}{save} = {
package => 'Foswiki::UI::Save',
function => 'save',
context => { save => 1 },
allow => { POST => 1 },
};
$Foswiki::cfg{SwitchBoard}{search} = {
package => 'Foswiki::UI::Search',
function => 'search',
context => { search => 1 },
};
$Foswiki::cfg{SwitchBoard}{statistics} = {
package => 'Foswiki::UI::Statistics',
function => 'statistics',
context => { statistics => 1 },
};
$Foswiki::cfg{SwitchBoard}{upload} = {
package => 'Foswiki::UI::Upload',
function => 'upload',
context => { upload => 1 },
allow => { POST => 1 },
};
$Foswiki::cfg{SwitchBoard}{viewfile} = {
package => 'Foswiki::UI::Viewfile',
function => 'viewfile',
context => { viewfile => 1 },
};
$Foswiki::cfg{SwitchBoard}{viewfileauth} =
$Foswiki::cfg{SwitchBoard}{viewfile};
$Foswiki::cfg{SwitchBoard}{view} = {
package => 'Foswiki::UI::View',
function => 'view',
context => { view => 1 },
};
$Foswiki::cfg{SwitchBoard}{viewauth} = $Foswiki::cfg{SwitchBoard}{view};
#Monitor::MARK("End of BEGIN block in UI.pm");
}
use Error qw(:try);
use Assert;
use CGI ();
use Foswiki ();
use Foswiki::Request ();
use Foswiki::Response ();
use Foswiki::OopsException ();
use Foswiki::EngineException ();
use Foswiki::ValidationException ();
use Foswiki::AccessControlException ();
use Foswiki::Validation ();
# Used to lazily load UI handler modules
our %isInitialized = ();
use constant TRACE_REQUEST => 0;
=begin TML
---++ StaticMethod handleRequest($req) -> $res
Main coordinator of request-process-response cycle.
=cut
sub handleRequest {
my $req = shift;
my $res;
my $dispatcher = $Foswiki::cfg{SwitchBoard}{ $req->action() };
unless ( defined $dispatcher ) {
$res = new Foswiki::Response();
$res->header( -type => 'text/html', -status => '404' );
my $html = CGI::start_html('404 Not Found');
$html .= CGI::h1( {}, 'Not Found' );
$html .= CGI::p( {},
"The requested URL "
. $req->uri
. " was not found on this server." );
$html .= CGI::end_html();
$res->print($html);
return $res;
}
if ( ref($dispatcher) eq 'ARRAY' ) {
# Old-style array entry in switchboard from a plugin
my @array = @$dispatcher;
$dispatcher = {
package => $array[0],
function => $array[1],
context => $array[2],
};
}
if ( $dispatcher->{package} && !$isInitialized{ $dispatcher->{package} } ) {
eval qq(use $dispatcher->{package});
die $@ if $@;
$isInitialized{ $dispatcher->{package} } = 1;
}
my $sub;
$sub = $dispatcher->{package} . '::' if $dispatcher->{package};
$sub .= $dispatcher->{function};
# Get the params cache from the path
my $cache = $req->param('foswiki_redirect_cache');
if ( defined $cache ) {
$req->delete('foswiki_redirect_cache');
}
# If the path specifies a cache path, use that. It's arbitrary
# as to which takes precedence (param or path) because we should
# never have both at once.
my $path_info = $req->path_info();
if ( $path_info =~ s#/foswiki_redirect_cache/([a-f0-9]{32})## ) {
$cache = $1;
$req->path_info($path_info);
}
if ( defined $cache && $cache =~ /^([a-f0-9]{32})$/ ) {
require Foswiki::Request::Cache;
# implicit untaint required, because $cache may be used in a filename.
# Note that the cache serialises the method and path_info, which
# will be restored.
Foswiki::Request::Cache->new()->load( $1, $req );
}
if (TRACE_REQUEST) {
print STDERR "INCOMING "
. $req->method() . " "
. $req->url . " -> "
. $sub . "\n";
print STDERR "validation_key: "
. ( $req->param('validation_key') || 'no key' ) . "\n";
#require Data::Dumper;
#print STDERR Data::Dumper->Dump([$req]);
}
if ( UNIVERSAL::isa( $Foswiki::engine, 'Foswiki::Engine::CLI' ) ) {
$dispatcher->{context}->{command_line} = 1;
}
elsif (
defined $req->method()
&& (
(
defined $dispatcher->{allow}
&& !$dispatcher->{allow}->{ uc( $req->method() ) }
)
|| ( defined $dispatcher->{deny}
&& $dispatcher->{deny}->{ uc( $req->method() ) } )
)
)
{
$res = new Foswiki::Response();
$res->header( -type => 'text/html', -status => '405' );
$res->print( '<H1>Bad Request:</H1> The request method: '
. uc( $req->method() )
. ' is denied for the '
. $req->action()
. ' action.' );
if ( uc( $req->method() ) eq 'GET' ) {
$res->print( '<br/><br/>'
. 'The <tt><b>'
. $req->action()
. '</b></tt> script can only be called with the <tt>POST</tt> type method'
. '<br/><br/>'
. 'For example:<br/>'
. ' <tt><form method="post" action="%SCRIPTURL{'
. $req->action()
. '}%/%WEB%/%TOPIC%"></tt><br/>'
. '<br/><br/>See <a href="http://foswiki.org/System/CommandAndCGIScripts#A_61'
. $req->action()
. '_61">System.CommandAndCGIScripts</a> for more information.'
);
}
return $res;
}
$res = _execute( $req, \&$sub, %{ $dispatcher->{context} } );
return $res;
}
=begin TML
---++ StaticMethod _execute($req, $sub, %initialContext) -> $res
Creates a Foswiki session object with %initalContext and calls
$sub method. Returns the Foswiki::Response object.
=cut
sub _execute {
my ( $req, $sub, %initialContext ) = @_;
my $session;
my $res;
# If we get a known exception from new Foswiki(), then it must have
# come from one of the plugin handlers, which are initialised at this
# time (initPlugin, earlyInitPlugin for example). In this case the
# setup of the Foswiki object is pretty much complete; we can safely
# recover it from $Foswiki::Plugins::SESSION and clean it up.
# Error::Simple and EngineException indicate something more
# basic, however, that we can't clean up.
try {
# DO NOT pass in $req->remoteUser here (even though it appears to be right)
# because it may occlude the login manager. Exception is when running in
# CLI environment.
$session = new Foswiki(
( defined $ENV{GATEWAY_INTERFACE} || defined $ENV{MOD_PERL} )
? undef
: $req->remoteUser(),
$req, \%initialContext
);
$res = $session->{response};
unless ( defined $res->status() && $res->status() =~ /^\s*3\d\d/ ) {
$session->getLoginManager()->checkAccess();
&$sub($session);
}
}
catch Foswiki::ValidationException with {
my $e = shift;
$session ||= $Foswiki::Plugins::SESSION;
$res = $session->{response} if $session;
$res ||= new Foswiki::Response();
my $query = $session->{request};
# Cache the original query, so we can complete if if it is
# confirmed
require Foswiki::Request::Cache;
my $uid = Foswiki::Request::Cache->new()->save($query);
print STDERR "ValidationException: redirect with $uid\n"
if DEBUG;
# We use the login script for validation because it already
# has the correct criteria in httpd.conf for Apache login.
# URL is absolute as required by
# http://tools.ietf.org/html/rfc2616#section-14.30
my $url = $session->getScriptUrl(
1, 'login',
$session->{webName}, $session->{topicName},
foswikiloginaction => 'validate',
foswikioriginalquery => $uid
);
$session->redirect($url); # no passthrough
}
catch Foswiki::AccessControlException with {
my $e = shift;
$session ||= $Foswiki::Plugins::SESSION;
$res = $session->{response} if $session;
$res ||= new Foswiki::Response();
unless ( $session->getLoginManager()->forceAuthentication() ) {
# Login manager did not want to authenticate, perhaps because
# we are already authenticated.
my $exception = new Foswiki::OopsException(
'accessdenied',
status => 403,
web => $e->{web},
topic => $e->{topic},
def => 'topic_access',
params => [ $e->{mode}, $e->{reason} ]
);
$exception->generate($session);
}
}
catch Foswiki::OopsException with {
my $e = shift;
$session ||= $Foswiki::Plugins::SESSION;
$res = $session->{response} if $session;
$res ||= new Foswiki::Response();
$e->generate($session);
}
catch Foswiki::EngineException with {
my $e = shift;
my $res = $e->{response};
unless ( defined $res ) {
$res = new Foswiki::Response();
$res->header( -type => 'text/html', -status => $e->{status} );
my $html = CGI::start_html( $e->{status} . ' Bad Request' );
$html .= CGI::h1( {}, 'Bad Request' );
$html .= CGI::p( {}, $e->{reason} );
$html .= CGI::end_html();
$res->print($html);
}
$Foswiki::engine->finalizeError( $res, $session->{request} );
return $e->{status};
}
catch Error::Simple with {
# Most usually a 'die'
my $e = shift;
$session ||= $Foswiki::Plugins::SESSION;
$res = $session->{response} if $session;
$res ||= new Foswiki::Response();
$res->header( -type => 'text/plain' )
unless $res->outputHasStarted();
if (DEBUG) {
# output the full message and stacktrace to the browser
$res->print( $e->stringify() );
}
else {
my $mess = $e->stringify();
print STDERR $mess;
$session->logger->log( 'warning', $mess ) if $session;
# tell the browser where to look for more help
my $text =
'Foswiki detected an internal error - please check your Foswiki logs and webserver logs for more information.'
. "\n\n";
$mess =~ s/ at .*$//s;
# cut out pathnames from public announcement
$mess =~ s#/[\w./]+#path#g unless DEBUG;
$text .= $mess;
$res->print($text);
}
}
otherwise {
# Aargh! Should never get here
$res = new Foswiki::Response;
$res->header( -type => 'text/plain' );
$res->print("Unspecified error");
};
$session->finish() if $session;
return $res;
}
=begin TML
---++ StaticMethod logon($session)
Handler for "logon" action.
* =$session= is a Foswiki session object
=cut
sub logon {
my $session = shift;
my $action = $session->{request}->param('foswikiloginaction');
$session->{request}->delete('foswikiloginaction');
if ( defined $action && $action eq 'validate' ) {
Foswiki::Validation::validate($session);
}
else {
$session->getLoginManager()->login( $session->{request}, $session );
}
}
=begin TML
---++ StaticMethod checkWebExists( $session, $web, $op )
Check if the web exists. If it doesn't, will throw an oops exception.
$op is the user operation being performed.
=cut
sub checkWebExists {
my ( $session, $webName, $op ) = @_;
ASSERT( $session->isa('Foswiki') ) if DEBUG;
unless ($webName) {
throw Foswiki::OopsException(
'accessdenied',
status => 404,
def => 'bad_web_name',
web => $webName,
topic => $Foswiki::cfg{WebPrefsTopicName},
params => [$op]
);
}
unless ( $session->webExists($webName) ) {
throw Foswiki::OopsException(
'accessdenied',
status => 404,
def => 'no_such_web',
web => $webName,
topic => $Foswiki::cfg{WebPrefsTopicName},
params => [$op]
);
}
}
=begin TML
---++ StaticMethod topicExists( $session, $web, $topic, $op ) => boolean
Check if the given topic exists, throwing an OopsException
if it doesn't. $op is the user operation being performed.
=cut
sub checkTopicExists {
my ( $session, $web, $topic, $op ) = @_;
ASSERT( $session->isa('Foswiki') ) if DEBUG;
unless ( $session->topicExists( $web, $topic ) ) {
throw Foswiki::OopsException(
'accessdenied',
status => 404,
def => 'no_such_topic',
web => $web,
topic => $topic,
params => [$op]
);
}
}
=begin TML
---++ StaticMethod checkAccess( $session, $mode, $topicObject )
Check if the given mode of access by the given user to the given
web.topic is permissible, throwing a Foswiki::OopsException if not.
=cut
sub checkAccess {
my ( $session, $mode, $topicObject ) = @_;
ASSERT( $session->isa('Foswiki') ) if DEBUG;
unless ( $topicObject->haveAccess($mode) ) {
throw Foswiki::AccessControlException( $mode, $session->{user},
$topicObject->web, $topicObject->topic, $Foswiki::Meta::reason );
}
}
=begin TML
---++ StaticMethod checkValidationKey( $session )
Check the validation key for the given action. Throws an exception
if the validation key isn't valid (handled in _execute(), above)
* =$session= - the current session object
See Foswiki::Validation for more information.
=cut
sub checkValidationKey {
my ($session) = @_;
# If validation is disabled, do nothing
return if ( $Foswiki::cfg{Validation}{Method} eq 'none' );
# No point in command-line mode
return if $session->inContext('command_line');
# Check the nonce before we do anything else
my $nonce = $session->{request}->param('validation_key');
$session->{request}->delete('validation_key');
if ( !defined($nonce)
|| !Foswiki::Validation::isValidNonce( $session->getCGISession(),
$nonce ) )
{
throw Foswiki::ValidationException( $session->{request}->action() );
}
if ( defined($nonce) ) {
# Expire the nonce. If the user tries to use it again, they will
# be prompted.
Foswiki::Validation::expireValidationKeys( $session->getCGISession(),
$Foswiki::cfg{Validation}{ExpireKeyOnUse} ? $nonce : undef );
}
}
=begin TML
---++ StaticMethod run( $method, %context )
Supported for bin scripts that were written for Foswiki < 1.0. The parameters
are a function reference to the UI method to call and initial context.
In Foswiki >= 1.0 it should be replaced by a Config.spec entry such as:
# **PERL H**
# Bin script registration - do not modify
$Foswiki::cfg{SwitchBoard}{publish} = [ "Foswiki::Contrib::Publish", "publish", { publishing => 1 } ];
=cut
sub run {
my ( $method, %context ) = @_;
if ( UNIVERSAL::isa( $Foswiki::engine, 'Foswiki::Engine::CLI' ) ) {
$context{command_line} = 1;
}
_execute( Foswiki::Request->new(), $method, %context );
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
and TWiki Contributors. All Rights Reserved. TWiki Contributors
are listed in the AUTHORS file in the root of this distribution.
Copyright (C) 2005 Martin at Cleaver.org
Copyright (C) 2005-2007 TWiki Contributors
and also based/inspired on Catalyst framework, whose Author is
Sebastian Riedel. Refer to
http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7010/lib/Catalyst.pm
for more credit and liscence details.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.