blib/lib/CGI/Untaint/Facebook.pm

filter: Statementstmt 84.5% 71 / 84 Branchbran 68.7% 22 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 81.6% 111 / 136 CC 19 SCAR 28.6
CC
19
Cov
80%
is_valid
25.2
BEGIN
1.0
BEGIN
1.0
CRAP 17.4
↑ 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.16
22  
23 =cut
24  
25 3 our $VERSION = '0.16';
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 13 ▶sub is_valid {
Subroutine is_valid: called CC=19 SCAR=32.3
Pod documented
54 13 my $self = shift;
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/]) { } 5 4 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 5 $url = URI::Heuristic::uf_uristr("https://www.facebook.com/$value");
90 5 $self->value($url);
91 } else {
92 4 ▶ if(!$self->SUPER::is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SUPER::is_valid) 0 4 4
93 0 return 0;
94 }
95 4 $url = $value;
96 }
97  
98 11 my $request = HTTP::Request->new('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 ▶ if(!$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
Condition83%5/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
231
MC/DC66%2/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 table60%3/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\/\?next=\Q$e\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ m[^https?://(www|m).facebook.com/login/\?next=\Q$e\E]) 3 2 5
122 3 return 1;
123 }
124 2 ▶ 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 2 2
125 0 return 1;
126 }
127 }
128 2 carp "redirect from $url to $location";
129 2 return 1;
130 } elsif($error_code != 404) {
131 # Probably the certs file is wrong, or there
132 # was a timeout
133 0 carp "$url: ", $webdoc->status_line();
134 }
135 1 return 0;
136 }
137 5 my $response = $browser->decoded_content();
138 0 ▶ if($response =~ /This content isn't available at the moment/mis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response =~ /This content isn't available at the moment/ism) 0 0 0
139 0 return 0;
140 }
141 0 return 1;
142 }
143  
144 =head1 AUTHOR
145  
146 Nigel Horne, C<< <njh at bandsman.co.uk> >>
147  
148 =head1 BUGS
149  
150 Please report any bugs or feature requests to C<bug-cgi-untaint-url-facebook at rt.cpan.org>, or through
151 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Untaint-Twitter>. I will be notified, and then you'll
152 automatically be notified of progress on your bug as I make changes.
153  
154 =head1 SEE ALSO
155  
156 CGI::Untaint::url
157  
158 =head1 SUPPORT
159  
160 You can find documentation for this module with the perldoc command.
161  
162 perldoc CGI::Untaint::Facebook
163  
164 You can also look for information at:
165  
166 =over 4
167  
168 =item * RT: CPAN's request tracker
169  
170 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Untaint-Facebook>
171  
172 =item * Search CPAN
173  
174 L<http://search.cpan.org/dist/CGI-Untaint-Facebook>
175  
176 =back
177  
178 =head1 ACKNOWLEDGEMENTS
179  
180 =head1 LICENSE AND COPYRIGHT
181  
182 Copyright 2012-2023 Nigel Horne.
183  
184 This program is released under the following licence: GPL2
185  
186 =cut
187  
188 3 1; # End of CGI::Untaint::Facebook
↑ Summary