line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#request object |
2
|
|
|
|
|
|
|
package PSGI::Hector::Request; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=pod |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
PSGI::Hector::Request - Form request class |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $r = $hector->getRequest(); |
13
|
|
|
|
|
|
|
my $params = $r->getParameters(); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Class to deal with the current page request |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 METHODS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
6
|
|
|
6
|
|
57738
|
use strict; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
144
|
|
24
|
6
|
|
|
6
|
|
23
|
use warnings; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
146
|
|
25
|
6
|
|
|
6
|
|
533
|
use Data::Dumper; |
|
6
|
|
|
|
|
5639
|
|
|
6
|
|
|
|
|
292
|
|
26
|
6
|
|
|
6
|
|
420
|
use parent qw(Plack::Request); |
|
6
|
|
|
|
|
242
|
|
|
6
|
|
|
|
|
34
|
|
27
|
|
|
|
|
|
|
######################################################### |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=pod |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head2 getParameters() |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $params = $r->getParameters(); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Returns a hash reference of all the GET/POST values from the current request. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Parameters that have multiple values will be returned as an array reference. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
########################################## |
42
|
|
|
|
|
|
|
sub getParameters{ #get POST or GET data |
43
|
2
|
|
|
2
|
1
|
1116
|
my $self = shift; |
44
|
2
|
|
|
|
|
14
|
return $self->parameters->mixed; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
######################################################### |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=pod |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head2 validate() |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $rules = { |
53
|
|
|
|
|
|
|
'age' => { |
54
|
|
|
|
|
|
|
'rule' => '^\d+$', |
55
|
|
|
|
|
|
|
'friendly' => 'Your Age' |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
}; #the form validation rules |
58
|
|
|
|
|
|
|
my($result, $errors) = $r->validate($rules); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Validates all the current form fields against the provided hash reference. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
The hash reference contains akey for every field you are concerned about, |
63
|
|
|
|
|
|
|
which is a reference to another hash containing two elements. The first is the |
64
|
|
|
|
|
|
|
actaul matching rule. The second is the friendly name for the field used |
65
|
|
|
|
|
|
|
in the error message, if a problem with the field is found. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
The method returns two values, first being a 0 or a 1 indicating the success of the form. |
68
|
|
|
|
|
|
|
The second is a reference to a list of errors if any. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=cut |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
########################################## |
73
|
|
|
|
|
|
|
sub validate{ #checks %form againist the hash rules |
74
|
0
|
|
|
0
|
1
|
0
|
my($self, $rules) = @_; |
75
|
0
|
|
|
|
|
0
|
my %params = %{$self->getParameters()}; |
|
0
|
|
|
|
|
0
|
|
76
|
0
|
|
|
|
|
0
|
my @errors; #fields that have a problem |
77
|
0
|
|
|
|
|
0
|
my $result = 0; |
78
|
0
|
0
|
|
|
|
0
|
if($rules){ |
79
|
0
|
|
|
|
|
0
|
foreach my $key (keys %{$rules}){ #check each field |
|
0
|
|
|
|
|
0
|
|
80
|
0
|
0
|
0
|
|
|
0
|
if(!$params{$key} || $params{$key} !~ m/$rules->{$key}->{'rule'}/){ #found an error |
81
|
0
|
|
|
|
|
0
|
push(@errors, $rules->{$key}->{'friendly'}); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
0
|
0
|
|
|
|
0
|
if($#errors == -1){ #no errors |
85
|
0
|
|
|
|
|
0
|
$result = 1; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else{ |
89
|
0
|
|
|
|
|
0
|
die("No rules to validate form"); |
90
|
|
|
|
|
|
|
} |
91
|
0
|
|
|
|
|
0
|
return($result, \@errors); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
######################################### |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=pod |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 getHeader($header) |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$request->getHeader($name) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Returns the value of the specified request header. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
######################################### |
106
|
|
|
|
|
|
|
sub getHeader{ |
107
|
0
|
|
|
0
|
1
|
0
|
my($self, $name) = @_; |
108
|
0
|
|
|
|
|
0
|
my $value = undef; |
109
|
0
|
|
|
|
|
0
|
$name = uc($name); |
110
|
0
|
|
|
|
|
0
|
$name =~ s/\-/_/g; |
111
|
0
|
0
|
|
|
|
0
|
if(defined($ENV{"HTTP_" . $name})){ |
112
|
0
|
|
|
|
|
0
|
$value = $ENV{'HTTP_' . $name}; |
113
|
|
|
|
|
|
|
} |
114
|
0
|
|
|
|
|
0
|
return $value; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
############################################################################################################ |
117
|
|
|
|
|
|
|
sub getCookie{ #returns the value of a cookie |
118
|
2
|
|
|
2
|
0
|
5
|
my($self, $name) = @_; |
119
|
2
|
|
|
|
|
13
|
my $cookies = $self->cookies(); |
120
|
2
|
50
|
|
|
|
30
|
$cookies->{$name} || undef; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
#################################################### |
123
|
|
|
|
|
|
|
sub __stringfy{ |
124
|
0
|
|
|
0
|
|
|
my($self, $item) = @_; |
125
|
0
|
|
|
|
|
|
local $Data::Dumper::Terse = 1; |
126
|
0
|
|
|
|
|
|
local $Data::Dumper::Indent = 0; |
127
|
0
|
|
|
|
|
|
return Dumper($item); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
########################################################### |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=pod |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 Notes |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 Author |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
MacGyveR |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Development questions, bug reports, and patches are welcome to the above address |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 See Also |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 Copyright |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Copyright (c) 2017 MacGyveR. All rights reserved. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
########################################## |
152
|
|
|
|
|
|
|
return 1; |