blib/lib/MobileDetect.pm

filter: Statementstmt 28.3% 38 / 134 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 57.1% 12 / 21 Podpod 0.0% 0 / 9 totaltotal 22.4% 50 / 223 CC 25 SCAR 36.5
CC
25
Cov
20%
new
132.0
is_phone
20.0
detect_phone
12.0
CRAP 38.3
↑ Summary
1 # Copyright: 2014 by Sebastian Enger
2 # Email: sebastian.enger at gmail - com
3 # Web: Buzz News on http://www.buzzerstar.com/
4 # Licence: Perl
5 # All rights released.
6 package MobileDetect;
7 #
8 # MobileDetect.pm - Perl detection for mobile phone and tablet devices
9 #
10 # Thanks to:
11 # https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php
12 # https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.json
13 #
14 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶no strict "subs";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶no warnings qw/misc/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '1.21';
25 1 ▶use constant JSON_REMOTE_FILE => 'https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant JSON_LOCAL_FILE => '/var/tmp/Mobile_Detect.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant HASH_LOCAL_FILE => '/var/tmp/Mobile_Detect.db';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our @EXPORT = qw(is_phone is_tablet is_mobile_os is_mobile_ua detect_phone detect_tablet detect_mobile_os detect_mobile_ua);
30  
31 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod undocumented
32 0 my($class, %args) = @_;
33 0 my $self = bless({}, $class);
34 0 my $json = JSON->new();
35 0 my $filestamp = 0;
36 0 my $filesize = 0;
37 0 my %hashfile;
38 my $hash;
39
40 0 ▶ if (-e HASH_LOCAL_FILE && -f HASH_LOCAL_FILE){
Branch: 1 branch
BranchTrueFalseTotal
if (-e "/var/tmp/Mobile_Detect.db" and -f "/var/tmp/Mobile_Detect.db") 0 0 0
Condition0%0/3
-e "/var/tmp/Mobile_Detect.db" and -f "/var/tmp/Mobile_Detect.db"
!ll&&!rl&&r
000
MC/DC0%0/2
-e "/var/tmp/Mobile_Detect.db" and -f "/var/tmp/Mobile_Detect.db"
-e "/var/tmp/Mobile_Detect.db" -f "/var/tmp/Mobile_Detect.db"
Truth table0%0/3
A and B
A= -e "/var/tmp/Mobile_Detect.db"B= -f "/var/tmp/Mobile_Detect.db"
ABresult
0X0
100
111
41 0 $filestamp = -M HASH_LOCAL_FILE;
42 0 $filestamp = int($filestamp);
43 0 $filesize = -s HASH_LOCAL_FILE;
44 }
45
46 # if we have a filestamp a file that has been created 31 days before
47 # AND we have a filesize of the file of lower than 1000 bytes we have to download and parse the json file
48 0 ▶ if ( $filestamp > 31 && $filesize < 1000 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($filestamp > 31 and $filesize < 1000) { } 0 0 0
Condition0%0/3
$filestamp > 31 and $filesize < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$filestamp > 31 and $filesize < 1000
$filestamp > 31 $filesize < 1000
Truth table0%0/3
A and B
A= $filestamp > 31B= $filesize < 1000
ABresult
0X0
100
111
49 # print "just downloading json content and fix it to hash content\n";
50
51 0 unlink JSON_LOCAL_FILE;
52 0 unlink HASH_LOCAL_FILE;
53
54 0 my $ua = LWP::UserAgent->new();
55 0 my $res = $ua->get(JSON_REMOTE_FILE);
56 0 my $json_content = "";
57 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
58 0 $json_content = $res->content;
59 } else {
60 0 die "Cannot download JSON RAW File From Github - exiting." . $res->status_line;
61 }
62 0 my $json_text = $json->allow_nonref->utf8->relaxed->decode($json_content);
63
64 0 ▶ while (my($k, $v) = each (%{$json_text->{uaMatch}->{tablets}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$json_text{"uaMatch"}{"tablets"};}) 0 0 0
65 0 $hash->{tablets}->{$k} = $v;
66 }
67 0 ▶ while (my($k, $v) = each (%{$json_text->{uaMatch}->{phones}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$json_text{"uaMatch"}{"phones"};}) 0 0 0
68 0 $hash->{phones}->{$k} = $v;
69 }
70 0 ▶ while (my($k, $v) = each (%{$json_text->{uaMatch}->{browsers}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$json_text{"uaMatch"}{"browsers"};}) 0 0 0
71 0 $hash->{browsers}->{$k} = $v;
72 }
73 0 ▶ while (my($k, $v) = each (%{$json_text->{uaMatch}->{os}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$json_text{"uaMatch"}{"os"};}) 0 0 0
74 0 $hash->{os}->{$k} = $v;
75 }
76 0 $self->{json}=$hash;
77 0 my %hashfile = %{$hash};
78 0 ▶ Storable::store(\%hashfile, HASH_LOCAL_FILE) or die "Can't store %hash in ".HASH_LOCAL_FILE." !\n";
Branch: 1 branch
BranchTrueFalseTotal
Storable::store(\%hashfile, "/var/tmp/Mobile_Detect.db") or die "Can't store %hash in /var/tmp/Mobile_Detect.db !\n" 0 0 0
79 } else {
80 # print "just reading hash content\n";
81 0 $hash = Storable::retrieve(HASH_LOCAL_FILE);
82 0 $self->{json}=$hash;
83 }
84 0 return $self;
85 }
86  
87 0 ▶sub detect_phone(){
Subroutine detect_phone: not called CC=3 SCAR=24.8
Pod undocumented
88 0 my $self = shift;
89 0 my $str = shift;
90 0 my $retVal = 0;
91 0 ▶ while (my($k1, $v1) = each (%{$self->{json}->{phones}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"json"}{"phones"};}) 0 0 0
92 0 ▶ if ($str =~ m/$v1/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$v1/sig) 0 0 0
93 0 $retVal = $k1;
94 }
95 }
96 0 return $retVal;
97 }
98 0 ▶sub detect_tablet(){
Subroutine detect_tablet: not called CC=3 SCAR=24.8
Pod undocumented
99 0 my $self = shift;
100 0 my $str = shift;
101
102 0 my $retVal = 0;
103 0 ▶ while (my($k2, $v2) = each (%{$self->{json}->{tablets}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"json"}{"tablets"};}) 0 0 0
104 0 ▶ if ($str =~ m/$v2/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$v2/sig) 0 0 0
105 0 $retVal = $k2;
106 }
107 }
108 0 return $retVal;
109 }
110 0 ▶sub detect_mobile_os(){
Subroutine detect_mobile_os: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my $self = shift;
112 0 my $str = shift;
113
114 0 my $retVal = 0;
115 0 ▶ while (my($k3, $v3) = each (%{$self->{json}->{os}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"json"}{"os"};}) 0 0 0
116 0 ▶ if ($str =~ m/$v3/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$v3/sig) 0 0 0
117 0 $retVal = $k3;
118 }
119 }
120 0 return $retVal;
121 }
122 0 ▶sub detect_mobile_ua(){
Subroutine detect_mobile_ua: not called CC=3 SCAR=24.8
Pod undocumented
123 0 my $self = shift;
124 0 my $str = shift;
125  
126 0 my $retVal = 0;
127 0 ▶ while (my($k4, $v4) = each (%{$self->{json}->{browsers}})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"json"}{"browsers"};}) 0 0 0
128 0 ▶ if ($str =~ m/$v4/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$v4/sig) 0 0 0
129 0 $retVal = $k4;
130 }
131 }
132 0 return $retVal;
133 }
134  
135 0 ▶sub is_phone(){
Subroutine is_phone: not called CC=4 SCAR=30.0
Pod undocumented
136 0 my $self = shift;
137 0 my $str = shift;
138
139 0 my $val1 = $self->detect_phone($str);
140 0 my $val2 = $self->detect_mobile_os($str);
141 0 my $val3 = $self->detect_mobile_ua($str);
142 0 ▶ if ( $val1 =~ /[a-zA-Z]/igs || $val2 =~ /[a-zA-Z]/igs || $val3 =~ /[a-zA-Z]/igs ){
Branch: 1 branch
BranchTrueFalseTotal
if ($val1 =~ /[a-zA-Z]/sig or $val2 =~ /[a-zA-Z]/sig or $val3 =~ /[a-zA-Z]/sig) 0 0 0
Condition0%0/6
$val1 =~ /[a-zA-Z]/sig or $val2 =~ /[a-zA-Z]/sig or $val3 =~ /[a-zA-Z]/sig
l!l&&r!l&&!r
000
$val1 =~ /[a-zA-Z]/sig or $val2 =~ /[a-zA-Z]/sig
l!l&&r!l&&!r
000
MC/DC0%0/3
$val1 =~ /[a-zA-Z]/sig or $val2 =~ /[a-zA-Z]/sig or $val3 =~ /[a-zA-Z]/sig
$val1 =~ /[a-zA-Z]/sig $val2 =~ /[a-zA-Z]/sig $val3 =~ /[a-zA-Z]/sig
Truth table0%0/4
A or B or C
A= $val1 =~ /[a-zA-Z]/sigB= $val2 =~ /[a-zA-Z]/sigC= $val3 =~ /[a-zA-Z]/sig
ABCresult
1XX1
01X1
0011
0000
143 0 return 1;
144 }
145 0 return 0;
146 }
147 0 ▶sub is_tablet(){
Subroutine is_tablet: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my $self = shift;
149 0 my $str = shift;
150 0 my $val = $self->detect_tablet($str);
151 0 ▶ if ($val =~ /[a-zA-Z]/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[a-zA-Z]/sig) 0 0 0
152 0 return 1;
153 }
154 0 return 0;
155 }
156 0 ▶sub is_mobile_os(){
Subroutine is_mobile_os: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my $self = shift;
158 0 my $str = shift;
159 0 my $val = $self->detect_mobile_os($str);
160 0 ▶ if ($val =~ /[a-zA-Z]/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[a-zA-Z]/sig) 0 0 0
161 0 return 1;
162 }
163 0 return 0;
164 }
165 0 ▶sub is_mobile_ua(){
Subroutine is_mobile_ua: not called CC=2 SCAR=17.9
Pod undocumented
166 0 my $self = shift;
167 0 my $str = shift;
168 0 my $val = $self->detect_mobile_ua($str);
169 0 ▶ if ($val =~ /[a-zA-Z]/igs){
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[a-zA-Z]/sig) 0 0 0
170 0 return 1;
171 }
172 }
173  
174 =pod
175 =head1 NAME
176  
177 MobileDetect - The great new MobileDetect Library for Perl is finally available!
178 Perl Module for the PHP Toolchain Mobile Detect from https://github.com/serbanghita/Mobile-Detect .
179 More Information and development Tools can be found here https://www.buzzerstar.com/development/ and https://devop.tools/
180  
181 Feel free to download, modify or change to code to fullfill your needs.
182  
183 =head1 VERSION
184
185 1.16
186
187 =head1 DEPENDENCIE
188  
189 use strict;
190 use JSON;
191 use LWP::Protocol::https;
192 use LWP::UserAgent;
193 use Storable;
194  
195 =head1 SYNOPSIS
196  
197 #!/usr/bin/perl
198  
199 use MobileDetect;
200  
201 my $obj = MobileDetect->new();
202 my $check = "Mozilla/5.0 (Linux; U; Android 4.1.2; nl-nl; SAMSUNG GT-I8190/I8190XXAME1 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"; # Samsung Galaxy S3 Mini
203  
204 print "is_phone: ".$obj->is_phone($check); print "\n";
205 print "detect_phone: ".$obj->detect_phone($check); print "\n";
206 print "is_tablet: ".$obj->is_tablet($check);print "\n";
207 print "detect_tablet: ".$obj->detect_tablet($check);print "\n";
208  
209 print "is_mobile_os: ".$obj->is_mobile_os($check);print "\n";
210 print "detect_mobile_os: ".$obj->detect_mobile_os($check);print "\n";
211 print "is_mobile_ua: ".$obj->is_mobile_ua($check);print "\n";
212 print "detect_mobile_ua: ".$obj->detect_mobile_ua($check)."\n";
213  
214 =head1 DESCRIPTION
215  
216 Check a given string against the Mobile Detect Library that can be found here: https://github.com/serbanghita/Mobile-Detect
217 I have prepared a Perl Version, because there is no such thing in perl and i also want to show my support for Mr. Șerban Ghiță
218 and his fine piece of PHP Software.
219  
220 The newest Version 1.15 has build in flat file database support, where the content of the downloaded json file is being preparsed
221 and can be read into memory with the needed hash structure for perl to process.
222  
223 This is the Perl Version. You need to setup LWP with HTTPS Support before (needed to regulary update the Mobile_Detect.json file
224 from github).
225  
226 Install needed modules example:
227 From the bash call :"cpan"
228 cpan [1] Promt: call "install JSON"
229 cpan [2] Promt: call "install JSON::XS"
230 cpan [3] Promt: call "install LWP::Protocol"
231 cpan [4] Promt: call "install LWP::Protocol::https"
232 cpan [5] Promt: call "install Storable"
233
234 =head1 AUTHOR
235  
236 Sebastian Enger, C<< <sebastian _._ enger (at) gmail _._ com> >>
237 L<Buzzerstar Development|https://www.buzzerstar.com/development/>
238 L<Buzz Trending News auf BuzzerStar|https://www.buzzerstar.com/trending/>
239 L<Tech Startup News|https://blog.onetopp.com/>
240 L<Innovation Consulting|https://www.onetopp.com/>
241 L<Devops|https://devop.tools/>
242  
243 =head1 BUGS
244  
245 Please report any bugs or feature requests to C<bug-mobiledetect-pp at rt.cpan.org>, or through
246 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MobileDetect>.
247 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
248  
249 =head1 SUPPORT
250  
251 You can find documentation for this module with the perldoc command.
252  
253 perldoc MobileDetect
254  
255 You can also look for information at:
256  
257 L<https://code.google.com/p/mobiledetect/>
258  
259 Or write the author an bug request email:
260 Sebastian Enger, C<< <sebastian.enger at gmail.com> >>
261  
262 =head1 ACKNOWLEDGEMENTS
263  
264  
265 =head1 LICENSE AND COPYRIGHT
266  
267 Copyright 2014 Sebastian Enger.
268  
269 This program is free software; you can redistribute it and/or modify it
270 under the terms of the the Artistic License (2.0). You may obtain a
271 copy of the full license at:
272  
273 L<http://www.perlfoundation.org/artistic_license_2_0>
274  
275 Any use, modification, and distribution of the Standard or Modified
276 Versions is governed by this Artistic License. By using, modifying or
277 distributing the Package, you accept this license. Do not use, modify,
278 or distribute the Package, if you do not accept this license.
279  
280 If your Modified Version has been derived from a Modified Version made
281 by someone other than you, you are nevertheless required to ensure that
282 your Modified Version complies with the requirements of this license.
283  
284 This license does not grant you the right to use any trademark, service
285 mark, tradename, or logo of the Copyright Holder.
286  
287 This license includes the non-exclusive, worldwide, free-of-charge
288 patent license to make, have made, use, offer to sell, sell, import and
289 otherwise transfer the Package with respect to any patent claims
290 licensable by the Copyright Holder that are necessarily infringed by the
291 Package. If you institute patent litigation (including a cross-claim or
292 counterclaim) against any party alleging that the Package constitutes
293 direct or contributory patent infringement, then this Artistic License
294 to you shall terminate on the date that such litigation is filed.
295  
296 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
297 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
298 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
299 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
300 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
301 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
302 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
303 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
304 =cut
305  
306 1 1; # End of MobileDetect
↑ Summary