blib/lib/CGI/Untaint/Facebook.pm

filter: Statementstmt 87.1% 68 / 78 Branchbran 71.4% 20 / 28 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 82.5% 104 / 126 SCAR 29.3
CC
17
Cov
82%
is_valid
21.0
BEGIN
1.0
BEGIN
1.0
CRAP 18.6
↑ Summary
1 package CGI::Untaint::Facebook;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # use base 'CGI::Untaint::object';
8 3 ▶use base 'CGI::Untaint::url';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI::Heuristic;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 CGI::Untaint::Facebook - Validate a string is a valid Facebook URL or ID
18  
19 =head1 VERSION
20  
21 Version 0.14
22  
23 =cut
24  
25 3 our $VERSION = '0.14';
26  
27 =head1 SYNOPSIS
28  
29 CGI::Untaint::Facebook validate if a given ID in a form is a valid Facebook ID.
30 The ID can be either a full Facebook URL, or a page on facebook, so
31 'http://www.facebook.com/nigelhorne' and 'nigelhorne' will both return true.
32  
33 use CGI::Info;
34 use CGI::Untaint;
35 use CGI::Untaint::Facebook;
36 # ...
37 my $info = CGI::Info->new();
38 my $params = $info->params();
39 # ...
40 my $u = CGI::Untaint->new($params);
41 my $tid = $u->extract(-as_Facebook => 'web_address');
42 # $tid will be lower case
43  
44 =head1 SUBROUTINES/METHODS
45  
46 =head2 is_valid
47  
48 Validates the data.
49 Returns a boolean if $self->value is a valid Facebook URL.
50  
51 =cut
52  
53 sub is_valid {
54 13 ▶ my $self = shift;
Subroutine is_valid: called CC=17 SCAR=30.5
Pod documented
55  
56 13 my $value = $self->value;
57  
58 13 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 13 13
59 0 return 0;
60 }
61  
62 # Ignore leading and trailing spaces
63 13 $value =~ s/\s+$//;
64 13 $value =~ s/^\s+//;
65  
66 13 ▶ if(length($value) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value == 0) 1 12 13
67 1 return 0;
68 }
69  
70 12 ▶ if($value =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\s/) 1 11 12
71 1 return 0;
72 }
73  
74 # Allow URLs such as https://m.facebook.com/#!/groups/6000106799?ref=bookmark&__user=764645045)
75 11 ▶ if($value =~ /([a-zA-Z0-9\-\/\.:\?&_=#!]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[([a-zA-Z0-9\-/\.:\?&_=#!]+)]) { } 11 0 11
76 11 $value = $1;
77 } else {
78 0 return 0;
79 }
80  
81 11 my $url;
82 11 ▶ if($value =~ /^http:\/\/www.facebook.com\/(.+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ m[^http://www.facebook.com/(.+)]) { } 2 9 11
elsif ($value =~ /^www\.facebook\.com/) { } 0 9 9
elsif (not $value =~ m[^https://(www|m).facebook.com/]) { } 6 3 9
83 2 $url = "https://www.facebook.com/$1";
84 2 $self->value($url);
85 } elsif($value =~ /^www\.facebook\.com/) {
86 0 $url = "https://$value";
87 0 $self->value($url);
88 } elsif($value !~ /^https:\/\/(www|m).facebook.com\//) {
89 6 $url = URI::Heuristic::uf_uristr("https://www.facebook.com/$value");
90 6 $self->value($url);
91 } else {
92 3 ▶ if(!$self->SUPER::is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SUPER::is_valid) 0 3 3
93 0 return 0;
94 }
95 3 $url = $value;
96 }
97  
98 11 my $request = new HTTP::Request('HEAD' => $url);
99 11 $request->header('Accept' => 'text/html');
100 11 ▶ if($ENV{'HTTP_ACCEPT_LANGUAGE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_ACCEPT_LANGUAGE"}) 0 11 11
101 0 $request->header('Accept-Language' => $ENV{'HTTP_ACCEPT_LANGUAGE'});
102 }
103 11 my $browser = LWP::UserAgent->new();
104 11 $browser->ssl_opts(verify_hostname => 1, SSL_ca_file => Mozilla::CA::SSL_ca_file());
105 11 $browser->agent(ref($self)); # Should be CGI::Untaint::Facebook
106 11 $browser->timeout(10);
107 11 $browser->max_size(128);
108 11 $browser->env_proxy(1);
109  
110 11 my $webdoc = $browser->simple_request($request);
111 11 my $error_code = $webdoc->code;
112 11 ▶ unless($webdoc->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($webdoc->is_success) 6 5 11
113 6 ▶ if((($error_code == 301) || ($error_code == 302)) &&
Branch: 2 branches
BranchTrueFalseTotal
if ($error_code == 301 || $error_code == 302 and $webdoc->as_string =~ /^location: (.+)$/im) { } 5 1 6
elsif ($error_code != 404) { } 0 1 1
Condition66%4/6
$error_code == 301 || $error_code == 302 and $webdoc->as_string =~ /^location: (.+)$/im
!ll&&!rl&&r
105
$error_code == 301 or $error_code == 302
l!l&&r!l&&!r
051
MC/DC33%1/3
$error_code == 301 || $error_code == 302 and $webdoc->as_string =~ /^location: (.+)$/im
$error_code == 301 $error_code == 302 $webdoc->as_string =~ /^location: (.+)$/im
Truth table40%2/5
A or B and C
A= $error_code == 301B= $error_code == 302C= $webdoc->as_string =~ /^location: (.+)$/im
ABCresult
00X0
1X00
0100
1X11
0111
114 ($webdoc->as_string =~ /^location: (.+)$/im)) {
115 5 my $location = $1;
116 5 ▶ if($location =~ /^https?:\/\/(www|m).facebook.com\/pages\/.+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ m[^https?://(www|m).facebook.com/pages/.+]) { } 0 5 5
117 0 $self->value($location);
118 0 return 1;
119 } else {
120 5 my $e = uri_escape($url);
121 5 ▶ if($location =~ /^https?:\/\/(www|m).facebook.com\/login.php\?next=\Q$e\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ m[^https?://(www|m).facebook.com/login.php\?next=\Q$e\E]) 0 5 5
122 0 return 1;
123 }
124 }
125 5 carp "redirect from $url to $location";
126 } elsif($error_code != 404) {
127 # Probably the certs file is wrong, or there
128 # was a timeout
129 0 carp "$url: " . $webdoc->status_line;
130 }
131 6 return 0;
132 }
133 5 return 1;
134 }
135  
136 =head1 AUTHOR
137  
138 Nigel Horne, C<< <njh at bandsman.co.uk> >>
139  
140 =head1 BUGS
141  
142 Please report any bugs or feature requests to C<bug-cgi-untaint-url-facebook at rt.cpan.org>, or through
143 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Untaint-Twitter>. I will be notified, and then you'll
144 automatically be notified of progress on your bug as I make changes.
145  
146  
147 =head1 SEE ALSO
148  
149 CGI::Untaint::url
150  
151  
152 =head1 SUPPORT
153  
154 You can find documentation for this module with the perldoc command.
155  
156 perldoc CGI::Untaint::Facebook
157  
158  
159 You can also look for information at:
160  
161 =over 4
162  
163 =item * RT: CPAN's request tracker
164  
165 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Untaint-Facebook>
166  
167 =item * AnnoCPAN: Annotated CPAN documentation
168  
169 L<http://annocpan.org/dist/CGI-Untaint-Facebook>
170  
171 =item * CPAN Ratings
172  
173 L<http://cpanratings.perl.org/d/CGI-Untaint-Facebook>
174  
175 =item * Search CPAN
176  
177 L<http://search.cpan.org/dist/CGI-Untaint-Facebook>
178  
179 =back
180  
181 =head1 ACKNOWLEDGEMENTS
182  
183  
184 =head1 LICENSE AND COPYRIGHT
185  
186 Copyright 2012-2017 Nigel Horne.
187  
188 This program is released under the following licence: GPL
189  
190  
191 =cut
192  
193 3 1; # End of CGI::Untaint::Facebook
↑ Summary