-
Notifications
You must be signed in to change notification settings - Fork 38
/
RestSave.pm
136 lines (105 loc) · 3.66 KB
/
RestSave.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
# Copyright (C) 2013-2017 Michael Daum http://michaeldaumconsulting.com
#
# 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.
package Foswiki::Plugins::NatEditPlugin::RestSave;
use strict;
use warnings;
use Foswiki::UI::Save ();
use Foswiki::OopsException ();
use Foswiki::Validation ();
use Encode ();
use Error qw( :try );
sub handle {
my ( $session, $plugin, $verb, $response ) = @_;
my $request = $session->{request};
foreach my $key ( $request->multi_param() ) {
my @val = $request->multi_param($key);
# hack to prevent redirecting
if ( $key eq 'redirectto' && @val && $val[0] eq '' ) {
#print STDERR "deleting bogus redirectto\n";
$request->delete($key);
next;
}
if ( ref $val[0] eq 'ARRAY' ) {
$request->param( $key, [ map( toSiteCharSet($_), @{ $val[0] } ) ] );
}
else {
$request->param( $key, [ map( toSiteCharSet($_), @val ) ] );
}
}
# do a normal save
my $error;
my $status = 200;
try {
Foswiki::UI::Save::save($session);
# get a new lease
my $topicObject =
Foswiki::Meta->new( $session, $session->{webName},
$session->{topicName} );
$topicObject->setLease( $Foswiki::cfg{LeaseLength} );
}
catch Foswiki::OopsException with {
$error = shift;
$status = 419;
};
# clear redirect enforced by a checkpoint action
$response->deleteHeader( "Location", "Status" );
$response->status($status);
# add validation key to HTTP header, if required
unless ( $response->getHeader('X-Foswiki-Validation') ) {
my $cgis = $session->getCGISession();
my $context =
$request->url( -full => 1, -path => 1, -query => 1 ) . time();
my $usingStrikeOne = $Foswiki::cfg{Validation}{Method} eq 'strikeone';
$response->pushHeader( 'X-Foswiki-Validation',
_generateValidationKey( $cgis, $context, $usingStrikeOne ) );
}
return ( defined $error ) ? stringifyError($error) : '';
}
# compatibility wrapper
sub _generateValidationKey {
my $nonce;
if ( Foswiki::Validation->can("generateValidationKey") ) {
$nonce = Foswiki::Validation::generateValidationKey(@_);
}
else
{ # extract from "<input type='hidden' name='validation_key' value='?$nonce' />";
my $html = Foswiki::Validation::addValidationKey(@_);
if ( $html =~ /value='\?(.*?)'/ ) {
$nonce = $1;
}
}
return $nonce;
}
sub stringifyError {
my $error = shift;
my $s = '';
$s .= $error->{-text} if defined $error->{-text};
$s .= ' ' . join( ',', @{ $error->{params} } )
if defined $error->{params};
return $s;
}
sub toSiteCharSet {
my $string = shift;
return $string unless $string;
return $string if $Foswiki::UNICODE;
return $string
if ( $Foswiki::cfg{Site}{CharSet} =~ /^utf-?8/i );
# If the site charset is not utf-8, need to convert it
# Leave this code using Encode:: - not used on UNICODE core.
require Encode;
return Encode::encode(
$Foswiki::cfg{Site}{CharSet},
Encode::decode_utf8($string),
Encode::FB_PERLQQ
);
}
1;