/
View.pm
573 lines (489 loc) · 19.2 KB
/
View.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
# See bottom of file for license and copyright information
=begin TML
---+ package Foswiki::UI::View
UI delegate for view function
=cut
package Foswiki::UI::View;
use strict;
use integer;
use Monitor;
use Assert;
require Foswiki;
require Foswiki::UI;
require Foswiki::Sandbox;
require Foswiki::OopsException;
=begin TML
---++ StaticMethod view( $session )
=view= command handler.
This method is designed to be
invoked via the =UI::run= method.
Generate a complete HTML page that represents the viewed topics.
The view is controlled by CGI parameters as follows:
| =rev= | topic revision to view |
| =section= | restrict view to a named section |
| =raw= | no format body text if set |
| =skin= | comma-separated list of skin(s) to use |
| =contenttype= | Allows you to specify an alternate content type |
=cut
sub view {
my $session = shift;
my $query = $session->{request};
my $webName = $session->{webName};
my $topicName = $session->{topicName};
my $raw = $query->param('raw') || '';
my $contentType = $query->param('contenttype');
my $showRev = 1;
my $logEntry = '';
my $revdate = '';
my $revuser = '';
my $store = $session->{store};
# is this view indexable by search engines? Default yes.
my $indexableView = 1;
Foswiki::UI::checkWebExists( $session, $webName, $topicName, 'view' );
my $skin = $session->getSkin();
my $rev = $store->cleanUpRevID( $query->param('rev') );
my $topicExists = $store->topicExists( $webName, $topicName );
my $map = $Foswiki::cfg{Plugins}{TWikiCompatibilityPlugin}{WebSearchPath};
if (!$topicExists
&& $Foswiki::cfg{Plugins}{TWikiCompatibilityPlugin}{Enabled}
&& defined($map)
&& defined($map->{$webName})
) {
#try the other web (TWikiCompatibility)
if ($topicExists = $session->{store}->topicExists( $map->{$webName}, $topicName )) {
$session->{webName} = $webName = $map->{$webName};
}
}
# text and meta of the _latest_ rev of the topic
my ( $currText, $currMeta );
# text and meta of the chosen rev of the topic
my ( $meta, $text );
my $viewTemplate;
if ($topicExists) {
require Foswiki::Time;
( $currMeta, $currText ) =
$store->readTopic( $session->{user}, $webName, $topicName, undef );
Foswiki::UI::checkAccess( $session, $webName, $topicName, 'VIEW',
$session->{user}, $currText );
( $revdate, $revuser, $showRev ) = $currMeta->getRevisionInfo();
$revdate = Foswiki::Time::formatTime($revdate);
if ( !$rev || $rev > $showRev ) {
$rev = $showRev;
}
if ( $rev < $showRev ) {
( $meta, $text ) =
$store->readTopic( $session->{user}, $webName, $topicName, $rev );
( $revdate, $revuser ) = $meta->getRevisionInfo();
$revdate = Foswiki::Time::formatTime($revdate);
$logEntry .= 'r' . $rev;
}
else {
# viewing the most recent rev
( $text, $meta ) = ( $currText, $currMeta );
}
# So we're reading an existing topic here. It is about time
# to apply the 'section' selection (and maybe others in the
# future as well). $text is cleared unless a named section
# matching the 'section' URL parameter is found.
if ( my $section = $query->param('section') ) {
my ( $ntext, $sections ) = Foswiki::parseSections($text);
$text = ''; # in the beginning, there was ... NO section
FINDSECTION:
for my $s (@$sections) {
if ( $s->{type} eq 'section' && $s->{name} eq $section ) {
$text =
substr( $ntext, $s->{start}, $s->{end} - $s->{start} );
last FINDSECTION;
}
}
}
}
else { # Topic does not exist yet
$indexableView = 0;
$session->enterContext('new_topic');
$rev = 1;
$viewTemplate = 'TopicDoesNotExistView';
$logEntry .= ' (not exist)';
}
if ($raw) {
$indexableView = 0;
$logEntry .= ' raw=' . $raw;
if ( $raw eq 'debug' || $raw eq 'all' ) {
$text = $store->getDebugText( $meta, $text );
}
}
if ( $Foswiki::cfg{Log}{view} ) {
$session->writeLog( 'view', $webName . '.' . $topicName, $logEntry );
}
# Note; must enter all contexts before the template is read, as
# TMPL:P is expanded on the fly in the template reader. :-(
my ( $revTitle, $revArg ) = ( '', '' );
if ( $rev < $showRev ) {
$session->enterContext('inactive');
# disable edit of previous revisions
$revTitle = '(r' . $rev . ')';
$revArg = '&rev=' . $rev;
}
my $template =
$viewTemplate || $query->param('template')
|| $session->{prefs}->getPreferencesValue('VIEW_TEMPLATE')
|| 'view';
# Always use default view template for raw=debug, raw=all and raw=on
if ( $raw =~ /^(debug|all|on)$/ ) {
$template = 'view';
}
my $tmpl = $session->templates->readTemplate( $template, $skin );
if ( !$tmpl && $template ne 'view' ) {
$tmpl = $session->templates->readTemplate( 'view', $skin );
}
if ( !$tmpl ) {
throw Foswiki::OopsException(
'attention',
def => 'no_such_template',
web => $webName,
topic => $topicName,
params => [ $template, 'VIEW_TEMPLATE' ]
);
}
$tmpl =~ s/%REVTITLE%/$revTitle/g;
$tmpl =~ s/%REVARG%/$revArg/g;
if ( $indexableView
&& $Foswiki::cfg{AntiSpam}{RobotsAreWelcome}
&& !$query->param() )
{
# it's an indexable view type, there are no parameters
# on the url, and robots are welcome. Remove the NOINDEX meta tag
$tmpl =~ s/<meta name="robots"[^>]*>//goi;
}
# Show revisions around the one being displayed
# we start at $showRev then possibly jump near $rev if too distant
my $revsToShow = $Foswiki::cfg{NumberOfRevisions} + 1;
$revsToShow = $showRev if $showRev < $revsToShow;
my $doingRev = $showRev;
my $revs = '';
while ( $revsToShow > 0 ) {
$revsToShow--;
if ( $doingRev == $rev ) {
$revs .= 'r' . $rev;
}
else {
$revs .= CGI::a(
{
href => $session->getScriptUrl(
0, 'view', $webName, $topicName, rev => $doingRev
),
rel => 'nofollow'
},
"r$doingRev"
);
}
if ( $doingRev - $rev >= $Foswiki::cfg{NumberOfRevisions} ) {
# we started too far away, need to jump closer to $rev
use integer;
$doingRev = $rev + $revsToShow / 2;
$doingRev = $revsToShow if $revsToShow > $doingRev;
$revs .= ' | ';
next;
}
if ($revsToShow) {
$revs .= ' '
. CGI::a(
{
href => $session->getScriptUrl(
0, 'rdiff', $webName, $topicName,
rev1 => $doingRev,
rev2 => $doingRev - 1
),
rel => 'nofollow'
},
'<'
) . ' ';
}
$doingRev--;
}
$tmpl =~ s/%REVISIONS%/$revs/go;
## SMELL: This is also used in Foswiki::_TOC. Could insert a tag in
## TOC and remove all those here, finding the parameters only once
my @qparams = ();
foreach my $name ( $query->param ) {
next if ( $name eq 'keywords' );
next if ( $name eq 'topic' );
push @qparams, $name => $query->param($name);
}
$tmpl =~ s/%QUERYPARAMSTRING%/Foswiki::_make_params(1,@qparams)/geo;
# extract header and footer from the template, if there is a
# %TEXT% tag marking the split point. The topic text is inserted
# in place of the %TEXT% tag. The text before this tag is inserted
# as header, the text after is inserted as footer. If there is a
# %STARTTEXT% tag present, the header text between %STARTTEXT% and
# %TEXT is rendered together, as is the footer text between %TEXT%
# and %ENDTEXT%, if present. This allows correct handling of Foswiki
# markup in header or footer if those do require examination of the
# topic text to work correctly (e.g., %TOC%).
# Note: This feature is experimental and may be replaced by an
# alternative solution not requiring additional tags.
my ( $start, $end );
# SMELL: unchecked implicit untaint of data that *may* be coming from
# a topic (topics can be templates)
if ( $tmpl =~ m/^(.*)%TEXT%(.*)$/s ) {
my @starts = split( /%STARTTEXT%/, $1 );
if ( $#starts > 0 ) {
# we know that there is something before %STARTTEXT%
$start = $starts[0];
$text = $starts[1] . $text;
}
else {
$start = $1;
}
my @ends = split( /%ENDTEXT%/, $2 );
if ( $#ends > 0 ) {
# we know that there is something after %ENDTEXT%
$text .= $ends[0];
$end = $ends[1];
}
else {
$end = $2;
}
}
else {
my @starts = split( /%STARTTEXT%/, $tmpl );
if ( $#starts > 0 ) {
# we know that there is something before %STARTTEXT%
$start = $starts[0];
$text = $starts[1];
}
else {
$start = $tmpl;
$text = '';
}
$end = '';
}
# If minimalist is set, images and anchors will be stripped from text
my $minimalist = 0;
if ($contentType) {
$minimalist = ( $skin =~ /\brss/ );
}
elsif ( $skin =~ /\brss/ ) {
$contentType = 'text/xml';
$minimalist = 1;
}
elsif ( $skin =~ /\bxml/ ) {
$contentType = 'text/xml';
$minimalist = 1;
}
elsif ( $raw eq 'text' || $raw eq 'all' ) {
$contentType = 'text/plain';
}
else {
$contentType = 'text/html';
}
$session->{SESSION_TAGS}{MAXREV} = $showRev;
$session->{SESSION_TAGS}{CURRREV} = $rev;
# Set page generation mode to RSS if using an RSS skin
$session->enterContext('rss') if $skin =~ /\brss/;
# Set the meta-object that contains the rendering info
# SMELL: hack to get around not having a proper topic object model
$session->enterContext( 'can_render_meta', $meta ) if $meta;
my $page;
# Legacy: If the _only_ skin is 'text' it is used like this:
# http://.../view/Codev/MyTopic?skin=text&contenttype=text/plain&raw=on
# which shows the topic as plain text; useful for those who want
# to download plain text for the topic. So when the skin is 'text'
# we do _not_ want to create a textarea.
# raw=on&skin=text is deprecated; use raw=text instead.
Monitor::MARK('Ready to render');
if ( $raw eq 'text' || $raw eq 'all' || ( $raw && $skin eq 'text' ) ) {
# use raw text
$page = $text;
}
else {
my @args = ( $session, $webName, $topicName, $meta, $minimalist );
$session->enterContext('header_text');
$page = _prepare( $start, @args );
$session->leaveContext('header_text');
Monitor::MARK('Rendered header');
if ($raw) {
if ($text) {
my $p = $session->{prefs};
$page .= CGI::textarea(
-readonly => 'readonly',
-rows => $p->getPreferencesValue('EDITBOXHEIGHT'),
-cols => $p->getPreferencesValue('EDITBOXWIDTH'),
-style => $p->getPreferencesValue('EDITBOXSTYLE'),
-class => 'foswikiTextarea foswikiTextareaRawView',
-id => 'topic',
-default => $text
);
}
}
else {
$session->enterContext('body_text');
$page .= _prepare( $text, @args );
$session->leaveContext('body_text');
}
Monitor::MARK('Rendered body');
$session->enterContext('footer_text');
$page .= _prepare( $end, @args );
$session->leaveContext('footer_text');
Monitor::MARK('Rendered footer');
}
# Output has to be done in one go, because if we generate the header and
# then redirect because of some later constraint, some browsers fall over
$session->writeCompletePage( $page, 'view', $contentType );
Monitor::MARK('Wrote HTML');
}
sub _prepare {
my ( $text, $session, $webName, $topicName, $meta, $minimalist ) = @_;
$text = $session->handleCommonTags( $text, $webName, $topicName, $meta );
$text =
$session->renderer->getRenderedVersion( $text, $webName, $topicName );
$text =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois;
if ($minimalist) {
$text =~ s/<img [^>]*>//gi; # remove image tags
$text =~ s/<a [^>]*>//gi; # remove anchor tags
$text =~ s/<\/a>//gi; # remove anchor tags
}
return $text;
}
=begin TML
---++ StaticMethod viewfile( $session, $web, $topic, $query )
=viewfile= command handler.
This method is designed to be
invoked via the =UI::run= method.
Command handler for viewfile. View a file in the browser.
Some parameters are passed in CGI query:
| =filename= | Attachment to view |
| =rev= | Revision to view |
=cut
sub viewfile {
my $session = shift;
my $query = $session->{request};
my $topic = Foswiki::Sandbox::untaint($session->{topicName}, \&Foswiki::Sandbox::validateTopicName);
my $webName = Foswiki::Sandbox::untaint($session->{webName}, \&Foswiki::Sandbox::validateWebName);
my $fileName;
unless (defined($ENV{REDIRECT_STATUS}) && defined($ENV{REQUEST_URI})) {
if ( defined( $query->param('filename') ) ) {
$fileName = Foswiki::Sandbox::untaint($query->param('filename'), \&Foswiki::Sandbox::validateWebName);
} else {
my $pathInfo = Foswiki::Sandbox::normalizeFileName($query->path_info());
$pathInfo =~ s|//*|/|g; #stop the simplistic parsing from barfing on //
my @path = split( '/', $pathInfo );
shift(@path) unless ($path[0]); #remove leading empty string
#work out the web, topic and filename
$webName = shift(@path);
while (($path[0]) && (TWiki::Func::webExists("$webName/".$path[0]))) {
$webName .= '/'.shift(@path);
}
$topic = shift(@path);
$fileName = join('/', @path);
}
} else {
#this is a redirect - can be used to make 404,401 etc URL's more foswiki tailored
#and is also used in TWikiCompatibility
my $pathInfo = Foswiki::Sandbox::normalizeFileName($ENV{REQUEST_URI});
$pathInfo =~ s|$Foswiki::cfg{PubUrlPath}||; #remove pubUrlPath
$pathInfo =~ s|//*|/|g; #stop the simplistic parsing from barfing on //
my @path = split( '/', $pathInfo );
shift(@path) unless ($path[0]); #remove leading empty string
#work out the web, topic and filename
$webName = shift(@path);
while (($path[0]) && (TWiki::Func::webExists("$webName/".$path[0]))) {
$webName .= '/'.shift(@path);
}
$topic = shift(@path);
$fileName = join('/', @path);
}
if ( !$fileName ) {
throw Foswiki::OopsException(
'attention',
def => 'no_such_attachment',
web => 'Unknown',
topic => 'Unknown',
status => 404,
params => [ 'viewfile', '?' ]
);
}
#print STDERR "VIEWFILE: web($webName), topic($topic), file($fileName)\n";
#you can't remove the /'s from the filename, as there are directories below the pub/web/topic
#$fileName = Foswiki::Sandbox::sanitizeAttachmentName($fileName);
$fileName = Foswiki::Sandbox::normalizeFileName($fileName);
my $rev = $session->{store}->cleanUpRevID( $query->param('rev') );
unless ( $fileName
&& $session->{store}->attachmentExists( $webName, $topic, $fileName ) )
{
#try the other web (TWikiCompatibility)
my $map = $Foswiki::cfg{Plugins}{TWikiCompatibilityPlugin}{WebSearchPath};
if ($Foswiki::cfg{Plugins}{TWikiCompatibilityPlugin}{Enabled}
&& defined ($map)
&& $session->{store}->attachmentExists( $map->{$webName}, $topic, $fileName )) {
$webName = $map->{$webName};
$session->{response}->status(200); #OK, but, um non-authorative?
} else {
throw Foswiki::OopsException(
'attention',
def => 'no_such_attachment',
web => $webName,
topic => $topic,
status => 404,
params => [ 'viewfile', $fileName || '?' ]
);
}
}
ASSERT(UNTAINTED($webName)) if DEBUG;
ASSERT(UNTAINTED($topic)) if DEBUG;
ASSERT(UNTAINTED($fileName)) if DEBUG;
ASSERT(UNTAINTED($rev)) if DEBUG;
# TSA SMELL: Maybe could be less memory hungry if get a file handle
# and set response body to it. This way engines could send data the
# best way possible to each one
my $fileContent =
$session->{store}
->readAttachment( $session->{user}, $webName, $topic, $fileName, $rev );
my $type = _suffixToMimeType( $session, $fileName );
my $length = length($fileContent);
my $dispo = 'inline;filename=' . $fileName;
$session->{response}
->header( -type => $type, qq(Content-Disposition="$dispo") );
$session->{response}->print($fileContent);
}
sub _suffixToMimeType {
my ( $session, $theFilename ) = @_;
my $mimeType = 'text/plain';
if ( $theFilename =~ /\.([^.]+)$/ ) {
my $suffix = $1;
my @types = grep { s/^\s*([^\s]+).*?\s$suffix\s.*$/$1/i }
map { $_ . ' ' }
split( /[\n\r]/, Foswiki::readFile( $Foswiki::cfg{MimeTypesFileName} ) );
$mimeType = $types[0] if (@types);
}
return $mimeType;
}
1;
__DATA__
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2008 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.
# Based on parts of Ward Cunninghams original Wiki and JosWiki.
# Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
# Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
#
# 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.