Request.pm
14.9 KB
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
# --
# Copyright (C) 2001-2017 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
#
# Custom version for CAS authentication - rodrigo@goncalves.pro.br
#
# Version 2016-01-18 - RG - Version for OTRS 5.0.6
# Version 2017-12-07 - RG - Version for OTRS 6.0.1
#
# --
package Kernel::System::Web::Request;
use strict;
use warnings;
use CGI ();
use CGI::Carp;
use File::Path qw();
use Kernel::System::VariableCheck qw(:all);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::CheckItem',
'Kernel::System::Encode',
'Kernel::System::Web::UploadCache',
'Kernel::System::FormDraft',
);
=head1 NAME
Kernel::System::Web::Request - global CGI interface
=head1 DESCRIPTION
All cgi param functions.
=head1 PUBLIC INTERFACE
=head2 new()
create param object. Do not use it directly, instead use:
use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Web::Request' => {
WebRequest => CGI::Fast->new(), # optional, e. g. if fast cgi is used
}
);
my $ParamObject = $Kernel::OM->Get('Kernel::System::Web::Request');
If Kernel::System::Web::Request is instantiated several times, they will share the
same CGI data (this can be helpful in filters which do not have access to the
ParamObject, for example.
If you need to reset the CGI data before creating a new instance, use
CGI::initialize_globals();
before calling Kernel::System::Web::Request->new();
=cut
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
# get config object
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# max 5 MB posts
$CGI::POST_MAX = $ConfigObject->Get('WebMaxFileUpload') || 1024 * 1024 * 5; ## no critic
# query object (in case use already existing WebRequest, e. g. fast cgi)
$Self->{Query} = $Param{WebRequest} || CGI->new();
return $Self;
}
=head2 Error()
to get the error back
if ( $ParamObject->Error() ) {
print STDERR $ParamObject->Error() . "\n";
}
=cut
sub Error {
my ( $Self, %Param ) = @_;
# Workaround, do not check cgi_error() with perlex, CGI module is not
# working with perlex.
if ( $ENV{'GATEWAY_INTERFACE'} && $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-PerlEx/ ) {
return;
}
return if !$Self->{Query}->cgi_error();
## no critic
return $Self->{Query}->cgi_error() . ' - POST_MAX=' . ( $CGI::POST_MAX / 1024 ) . 'KB';
## use critic
}
=head2 GetParam()
to get single request parameters. By default, trimming is performed on the data.
my $Param = $ParamObject->GetParam(
Param => 'ID',
Raw => 1, # optional, input data is not changed
);
=cut
sub GetParam {
my ( $Self, %Param ) = @_;
my $Value = $Self->{Query}->param( $Param{Param} );
# Fallback to query string for mixed requests.
my $RequestMethod = $Self->{Query}->request_method() // '';
if ( $RequestMethod eq 'POST' && !defined $Value ) {
$Value = $Self->{Query}->url_param( $Param{Param} );
}
$Kernel::OM->Get('Kernel::System::Encode')->EncodeInput( \$Value );
my $Raw = defined $Param{Raw} ? $Param{Raw} : 0;
if ( !$Raw ) {
# If it is a plain string, perform trimming
if ( ref \$Value eq 'SCALAR' ) {
$Kernel::OM->Get('Kernel::System::CheckItem')->StringClean(
StringRef => \$Value,
TrimLeft => 1,
TrimRight => 1,
);
}
}
return $Value;
}
=head2 GetParamNames()
to get names of all parameters passed to the script.
my @ParamNames = $ParamObject->GetParamNames();
Example:
Called URL: index.pl?Action=AdminSystemConfiguration;Subaction=Save;Name=Config::Option::Valid
my @ParamNames = $ParamObject->GetParamNames();
print join " :: ", @ParamNames;
#prints Action :: Subaction :: Name
=cut
sub GetParamNames {
my $Self = shift;
# fetch all names
my @ParamNames = $Self->{Query}->param();
# Fallback to query string for mixed requests.
my $RequestMethod = $Self->{Query}->request_method() // '';
if ( $RequestMethod eq 'POST' ) {
my %POSTNames;
@POSTNames{@ParamNames} = @ParamNames;
my @GetNames = $Self->{Query}->url_param();
GETNAME:
for my $GetName (@GetNames) {
next GETNAME if !defined $GetName;
push @ParamNames, $GetName if !exists $POSTNames{$GetName};
}
}
for my $Name (@ParamNames) {
$Kernel::OM->Get('Kernel::System::Encode')->EncodeInput( \$Name );
}
return @ParamNames;
}
=head2 GetArray()
to get array request parameters.
By default, trimming is performed on the data.
my @Param = $ParamObject->GetArray(
Param => 'ID',
Raw => 1, # optional, input data is not changed
);
=cut
sub GetArray {
my ( $Self, %Param ) = @_;
my @Values = $Self->{Query}->multi_param( $Param{Param} );
# Fallback to query string for mixed requests.
my $RequestMethod = $Self->{Query}->request_method() // '';
if ( $RequestMethod eq 'POST' && !@Values ) {
@Values = $Self->{Query}->url_param( $Param{Param} );
}
$Kernel::OM->Get('Kernel::System::Encode')->EncodeInput( \@Values );
my $Raw = defined $Param{Raw} ? $Param{Raw} : 0;
if ( !$Raw ) {
# get check item object
my $CheckItemObject = $Kernel::OM->Get('Kernel::System::CheckItem');
VALUE:
for my $Value (@Values) {
# don't validate CGI::File::Temp objects from file uploads
next VALUE if !$Value || ref \$Value ne 'SCALAR';
$CheckItemObject->StringClean(
StringRef => \$Value,
TrimLeft => 1,
TrimRight => 1,
);
}
}
return @Values;
}
=head2 GetUploadAll()
gets file upload data.
my %File = $ParamObject->GetUploadAll(
Param => 'FileParam', # the name of the request parameter containing the file data
);
returns (
Filename => 'abc.txt',
ContentType => 'text/plain',
Content => 'Some text',
);
=cut
sub GetUploadAll {
my ( $Self, %Param ) = @_;
# get upload
my $Upload = $Self->{Query}->upload( $Param{Param} );
return if !$Upload;
# get real file name
my $UploadFilenameOrig = $Self->GetParam( Param => $Param{Param} ) || 'unknown';
my $NewFileName = "$UploadFilenameOrig"; # use "" to get filename of anony. object
$Kernel::OM->Get('Kernel::System::Encode')->EncodeInput( \$NewFileName );
# replace all devices like c: or d: and dirs for IE!
$NewFileName =~ s/.:\\(.*)/$1/g;
$NewFileName =~ s/.*\\(.+?)/$1/g;
# return a string
my $Content = '';
while (<$Upload>) {
$Content .= $_;
}
close $Upload;
my $ContentType = $Self->_GetUploadInfo(
Filename => $UploadFilenameOrig,
Header => 'Content-Type',
);
return (
Filename => $NewFileName,
Content => $Content,
ContentType => $ContentType,
);
}
sub _GetUploadInfo {
my ( $Self, %Param ) = @_;
# get file upload info
my $FileInfo = $Self->{Query}->uploadInfo( $Param{Filename} );
# return if no upload info exists
return 'application/octet-stream' if !$FileInfo;
# return if no content type of upload info exists
return 'application/octet-stream' if !$FileInfo->{ $Param{Header} };
# return content type of upload info
return $FileInfo->{ $Param{Header} };
}
=head2 SetCookie()
set a cookie
$ParamObject->SetCookie(
Key => ID,
Value => 123456,
Expires => '+3660s',
Path => 'otrs/', # optional, only allow cookie for given path
Secure => 1, # optional, set secure attribute to disable cookie on HTTP (HTTPS only)
HTTPOnly => 1, # optional, sets HttpOnly attribute of cookie to prevent access via JavaScript
);
=cut
sub SetCookie {
my ( $Self, %Param ) = @_;
$Param{Path} ||= '';
return $Self->{Query}->cookie(
-name => $Param{Key},
-value => $Param{Value},
-expires => $Param{Expires},
-secure => $Param{Secure} || '',
-httponly => $Param{HTTPOnly} || '',
-path => '/' . $Param{Path},
);
}
=head2 GetCookie()
get a cookie
my $String = $ParamObject->GetCookie(
Key => ID,
);
=cut
sub GetCookie {
my ( $Self, %Param ) = @_;
return $Self->{Query}->cookie( $Param{Key} );
}
=head2 IsAJAXRequest()
checks if the current request was sent by AJAX
my $IsAJAXRequest = $ParamObject->IsAJAXRequest();
=cut
sub IsAJAXRequest {
my ( $Self, %Param ) = @_;
return ( $Self->{Query}->http('X-Requested-With') // '' ) eq 'XMLHttpRequest' ? 1 : 0;
}
=head2 LoadFormDraft()
Load specified draft.
This will read stored draft data and inject it into the param object
for transparent use by frontend module.
my $FormDraftID = $ParamObject->LoadFormDraft(
FormDraftID => 123,
UserID => 1,
);
=cut
sub LoadFormDraft {
my ( $Self, %Param ) = @_;
return if !$Param{FormDraftID} || !$Param{UserID};
# get draft data
my $FormDraft = $Kernel::OM->Get('Kernel::System::FormDraft')->FormDraftGet(
FormDraftID => $Param{FormDraftID},
UserID => $Param{UserID},
);
return if !IsHashRefWithData($FormDraft);
# Verify action.
my $Action = $Self->GetParam( Param => 'Action' );
return if $FormDraft->{Action} ne $Action;
# add draft name to form data
$FormDraft->{FormData}->{FormDraftTitle} = $FormDraft->{Title};
# create FormID and add to form data
my $FormID = $Kernel::OM->Get('Kernel::System::Web::UploadCache')->FormIDCreate();
$FormDraft->{FormData}->{FormID} = $FormID;
# set form data to param object, depending on type
KEY:
for my $Key ( sort keys %{ $FormDraft->{FormData} } ) {
my $Value = $FormDraft->{FormData}->{$Key} // '';
# array value
if ( IsArrayRefWithData($Value) ) {
$Self->{Query}->param(
-name => $Key,
-values => $Value,
);
next KEY;
}
# scalar value
$Self->{Query}->param(
-name => $Key,
-value => $Value,
);
}
# add UploadCache data
my $UploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
for my $File ( @{ $FormDraft->{FileData} } ) {
return if !$UploadCacheObject->FormIDAddFile(
%{$File},
FormID => $FormID,
);
}
return $Param{FormDraftID};
}
=head2 SaveFormDraft()
Create or replace draft using data from param object and upload cache.
Specified params can be overwritten if necessary.
my $FormDraftID = $ParamObject->SaveFormDraft(
UserID => 1
ObjectType => 'Ticket',
ObjectID => 123,
OverrideParams => { # optional, can contain strings and array references
Subaction => undef,
UserID => 1,
CustomParam => [ 1, 2, 3, ],
...
},
);
=cut
sub SaveFormDraft {
my ( $Self, %Param ) = @_;
# check params
return if !$Param{UserID} || !$Param{ObjectType} || !IsInteger( $Param{ObjectID} );
# gather necessary data for backend
my %MetaParams;
for my $Param (qw(Action FormDraftID FormDraftTitle FormID)) {
$MetaParams{$Param} = $Self->GetParam(
Param => $Param,
);
}
return if !$MetaParams{Action};
# determine session name param (SessionUseCookie = 0) for exclusion
my $SessionName = $Kernel::OM->Get('Kernel::Config')->Get('SessionName') || 'SessionID';
# compile override list
my %OverrideParams = IsHashRefWithData( $Param{OverrideParams} ) ? %{ $Param{OverrideParams} } : ();
# these params must always be excluded for safety, they take precedence
for my $Name (
qw(Action ChallengeToken FormID FormDraftID FormDraftTitle FormDraftAction LoadFormDraftID),
$SessionName
)
{
$OverrideParams{$Name} = undef;
}
# Gather all params.
# Exclude, add or override by using OverrideParams if necessary.
my @ParamNames = $Self->GetParamNames();
my %ParamSeen;
my %FormData;
PARAM:
for my $Param ( @ParamNames, sort keys %OverrideParams ) {
next PARAM if $ParamSeen{$Param}++;
my $Value;
# check for overrides first
if ( exists $OverrideParams{$Param} ) {
# allow only strings and array references as value
if (
IsStringWithData( $OverrideParams{$Param} )
|| IsArrayRefWithData( $OverrideParams{$Param} )
)
{
$Value = $OverrideParams{$Param};
}
# skip all other parameters (including those specified to be excluded by using 'undef')
else {
next PARAM;
}
}
# get other values from param object
if ( !defined $Value ) {
my @Values = $Self->GetArray( Param => $Param );
next PARAM if !IsArrayRefWithData( \@Values );
# store single occurances as string
if ( scalar @Values == 1 ) {
$Value = $Values[0];
}
# store multiple occurances as array reference
else {
$Value = \@Values;
}
}
$FormData{$Param} = $Value;
}
# get files from upload cache
my @FileData = $Kernel::OM->Get('Kernel::System::Web::UploadCache')->FormIDGetAllFilesData(
FormID => $MetaParams{FormID},
);
# prepare data to add or update draft
my %FormDraft = (
FormData => \%FormData,
FileData => \@FileData,
FormDraftID => $MetaParams{FormDraftID},
ObjectType => $Param{ObjectType},
ObjectID => $Param{ObjectID},
Action => $MetaParams{Action},
Title => $MetaParams{FormDraftTitle},
UserID => $Param{UserID},
);
# update draft
if ( $MetaParams{FormDraftID} ) {
return if !$Kernel::OM->Get('Kernel::System::FormDraft')->FormDraftUpdate(%FormDraft);
return 1;
}
# create new draft
return if !$Kernel::OM->Get('Kernel::System::FormDraft')->FormDraftAdd(%FormDraft);
return 1;
}
# CAS Custom
sub GetMethod {
my ( $Self, %Param ) = @_;
return $Self->{Query}->request_method();
}
# CAS Custom
1;
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<http://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=cut