File Coverage

blib/lib/MobileDetect.pm
Criterion Covered Total %
statement 27 115 23.4
branch 0 22 0.0
condition 0 12 0.0
subroutine 9 18 50.0
pod 0 9 0.0
total 36 176 20.4


line stmt bran cond sub pod time code
1             # Copyleft 2014 Sebastian Enger
2             # sebastian.enger at gmail - com
3             # All rights released.
4             package MobileDetect;
5             #
6             # MobileDetect - Perl detection mobile phone and tablet devices
7             #
8             # Thanks to:
9             # https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php
10             # https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.json
11              
12 1     1   17030 use 5.006;
  1         3  
  1         32  
13 1     1   3 use strict;
  1         2  
  1         38  
14 1     1   6 use warnings FATAL => 'all';
  1         6  
  1         39  
15 1     1   661 use JSON;
  1         17314  
  1         5  
16 1     1   1728 use Data::Dumper;
  1         6307  
  1         81  
17 1     1   438 use LWP::Protocol::https;
  1         138881  
  1         44  
18 1     1   2941 use LWP::UserAgent;
  1         13850  
  1         50  
19              
20             =head1 NAME
21              
22             MobileDetect::PP - The great new MobileDetect::PP is finally available!
23              
24             Perl Module for the PHP Toolchain Mobile Detect from https://github.com/serbanghita/Mobile-Detect
25              
26             =head1 VERSION
27              
28             Version 1.01
29              
30             =cut
31              
32             our $VERSION = '1.12';
33 1     1   9 use constant JSON_REMOTE_FILE => 'https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.json';
  1         1  
  1         71  
34 1     1   6 use constant JSON_LOCAL_FILE => '/var/tmp/Mobile_Detect.json';
  1         2  
  1         1144  
35              
36             our @EXPORT = qw(is_phone is_tablet is_mobile_os is_mobile_ua detect_phone detect_tablet detect_mobile_os detect_mobile_ua);
37              
38             =head1 SYNOPSIS
39              
40             Check a given string against the Mobile Detect Library that can be found here: https://github.com/serbanghita/Mobile-Detect
41             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ță
42             and his fine piece of PHP Software.
43              
44             This is the Perl Version. You need to setup LWP with HTTPS Support before (needed to regulary update the Mobile_Detect.json file
45             from github).
46              
47             Install needed modules example:
48             From the bash call :"cpan"
49             cpan [1] Promt: call "install JSON"
50             cpan [2] Promt: call "install JSON::XS"
51             cpan [3] Promt: call "install LWP::Protocol"
52             cpan [4] Promt: call "install LWP::Protocol::https"
53              
54             Usage Example:
55              
56             #!/usr/bin/perl
57              
58             use MobileDetect::PP;
59              
60             my $obj = MobileDetect::PP->new();
61             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
62              
63             print "is_phone: ".$obj->is_phone($check); print "\n";
64             print "detect_phone: ".$obj->detect_phone($check); print "\n";
65             print "is_tablet: ".$obj->is_tablet($check);print "\n";
66             print "detect_tablet: ".$obj->detect_tablet($check);print "\n";
67              
68             print "is_mobile_os: ".$obj->is_mobile_os($check);print "\n";
69             print "detect_mobile_os: ".$obj->detect_mobile_os($check);print "\n";
70             print "is_mobile_ua: ".$obj->is_mobile_ua($check);print "\n";
71             print "detect_mobile_ua: ".$obj->detect_mobile_ua($check)."\n";
72              
73             exit;
74             =cut
75              
76             sub new {
77 0     0 0   my($class, %args) = @_;
78 0           my $self = bless({}, $class);
79 0           my $json = JSON->new();
80 0           my $content = "";
81 0           my $filestamp = -M JSON_LOCAL_FILE;
82            
83 0 0 0       if ( (defined($filestamp) && $filestamp > 31) || !-e JSON_LOCAL_FILE ){
      0        
84 0           unlink JSON_LOCAL_FILE;
85            
86 0           my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
87 0           my $res = $ua->get(JSON_REMOTE_FILE);
88              
89 0 0         open my $handle, '>:encoding(UTF-8)', JSON_LOCAL_FILE or die "Can't open ".JSON_LOCAL_FILE." for reading: $!";
90 0           print $handle $res->content;
91 0           close $handle;
92 0           $content = $res->content;
93             } else {
94 0 0         open my $handle, '<:encoding(UTF-8)', JSON_LOCAL_FILE or die "Can't open ".JSON_LOCAL_FILE." for reading: $!";
95 0           local $/ = undef;
96 0           $content = <$handle>;
97 0           close $handle;
98             }
99 0           my $json_text = $json->allow_nonref->utf8->relaxed->decode($content);
100            
101 0           while (my($k, $v) = each (%{$json_text->{uaMatch}->{tablets}})){
  0            
102 0           $self->{tablets}->{$k} = $v;
103             }
104 0           while (my($k, $v) = each (%{$json_text->{uaMatch}->{phones}})){
  0            
105 0           $self->{phones}->{$k} = $v;
106             }
107 0           while (my($k, $v) = each (%{$json_text->{uaMatch}->{browsers}})){
  0            
108 0           $self->{browsers}->{$k} = $v;
109             }
110 0           while (my($k, $v) = each (%{$json_text->{uaMatch}->{os}})){
  0            
111 0           $self->{os}->{$k} = $v;
112             }
113 0           return $self;
114             }
115              
116             sub detect_phone(){
117 0     0 0   my $self = shift;
118 0           my $str = shift;
119             #print "check string: $str\n";
120 0           my $retVal = 0;
121 0           while (my($k1, $v1) = each (%{$self->{phones}})){
  0            
122 0 0         if ($str =~ m/$v1/igs){
123             # print "have match: $k1\n";
124 0           $retVal = $k1;
125             } else {
126             # print "no match: $k1\n";
127             }
128             }
129 0           return $retVal;
130             }
131             sub detect_tablet(){
132 0     0 0   my $self = shift;
133 0           my $str = shift;
134            
135 0           my $retVal = 0;
136 0           while (my($k2, $v2) = each (%{$self->{tablets}})){
  0            
137 0 0         if ($str =~ m/$v2/igs){
138             # print "have match: $k2\n";
139 0           $retVal = $k2;
140             } else {
141             # print "no match: $k2\n";
142             }
143             }
144 0           return $retVal;
145             }
146             sub detect_mobile_os(){
147 0     0 0   my $self = shift;
148 0           my $str = shift;
149            
150 0           my $retVal = 0;
151 0           while (my($k3, $v3) = each (%{$self->{os}})){
  0            
152 0 0         if ($str =~ m/$v3/igs){
153             # print "have match: $k2\n";
154 0           $retVal = $k3;
155             } else {
156             # print "no match: $k2\n";
157             }
158             }
159 0           return $retVal;
160             }
161             sub detect_mobile_ua(){
162 0     0 0   my $self = shift;
163 0           my $str = shift;
164              
165 0           my $retVal = 0;
166 0           while (my($k4, $v4) = each (%{$self->{browsers}})){
  0            
167 0 0         if ($str =~ m/$v4/igs){
168             # print "have match: $k2\n";
169 0           $retVal = $k4;
170             } else {
171             # print "no match: $k2\n";
172             }
173             }
174 0           return $retVal;
175             }
176              
177             sub is_phone(){
178 0     0 0   my $self = shift;
179 0           my $str = shift;
180            
181 0           my $val1 = $self->detect_phone($str);
182 0           my $val2 = $self->detect_mobile_os($str);
183 0           my $val3 = $self->detect_mobile_ua($str);
184             #print "DEBUG: $val1 -$val2-$val3\n";
185 0 0 0       if ( $val1 =~ /[a-zA-Z]/igs || $val2 =~ /[a-zA-Z]/igs || $val3 =~ /[a-zA-Z]/igs ){
      0        
186 0           return 1;
187             }
188 0           return 0;
189             }
190             sub is_tablet(){
191 0     0 0   my $self = shift;
192 0           my $str = shift;
193 0           my $val = $self->detect_tablet($str);
194            
195 0 0         if ($val =~ /[a-zA-Z]/igs){
196 0           return 1;
197             }
198 0           return 0;
199             }
200             sub is_mobile_os(){
201 0     0 0   my $self = shift;
202 0           my $str = shift;
203 0           my $val = $self->detect_mobile_os($str);
204            
205 0 0         if ($val =~ /[a-zA-Z]/igs){
206 0           return 1;
207             }
208 0           return 0;
209             }
210             sub is_mobile_ua(){
211 0     0 0   my $self = shift;
212 0           my $str = shift;
213 0           my $val = $self->detect_mobile_ua($str);
214            
215 0 0         if ($val =~ /[a-zA-Z]/igs){
216 0           return 1;
217             }
218             }
219              
220              
221             =head1 AUTHOR
222              
223             Sebastian Enger, C<< >>
224             Web News auf Deutsch L.
225             Trending News auf Deutsch L.
226             Newsticker auf Deutsch L.
227             =head1 BUGS
228              
229             Please report any bugs or feature requests to C, or through
230             the web interface at L. I will be notified, and then you'll
231             automatically be notified of progress on your bug as I make changes.
232              
233             =head1 SUPPORT
234              
235             You can find documentation for this module with the perldoc command.
236              
237             perldoc MobileDetect::PP
238              
239              
240             You can also look for information at:
241              
242             L
243              
244             Or write the author an bug request email:
245             Sebastian Enger, C<< >>
246              
247             =over 4
248              
249             =item * RT: CPAN's request tracker (report bugs here)
250              
251             L
252              
253             =item * AnnoCPAN: Annotated CPAN documentation
254              
255             L
256              
257             =item * CPAN Ratings
258              
259             L
260              
261             =item * Search CPAN
262              
263             L
264              
265             =back
266              
267              
268             =head1 ACKNOWLEDGEMENTS
269              
270              
271             =head1 LICENSE AND COPYRIGHT
272              
273             Copyright 2014 Sebastian Enger.
274              
275             This program is free software; you can redistribute it and/or modify it
276             under the terms of the the Artistic License (2.0). You may obtain a
277             copy of the full license at:
278              
279             L
280              
281             Any use, modification, and distribution of the Standard or Modified
282             Versions is governed by this Artistic License. By using, modifying or
283             distributing the Package, you accept this license. Do not use, modify,
284             or distribute the Package, if you do not accept this license.
285              
286             If your Modified Version has been derived from a Modified Version made
287             by someone other than you, you are nevertheless required to ensure that
288             your Modified Version complies with the requirements of this license.
289              
290             This license does not grant you the right to use any trademark, service
291             mark, tradename, or logo of the Copyright Holder.
292              
293             This license includes the non-exclusive, worldwide, free-of-charge
294             patent license to make, have made, use, offer to sell, sell, import and
295             otherwise transfer the Package with respect to any patent claims
296             licensable by the Copyright Holder that are necessarily infringed by the
297             Package. If you institute patent litigation (including a cross-claim or
298             counterclaim) against any party alleging that the Package constitutes
299             direct or contributory patent infringement, then this Artistic License
300             to you shall terminate on the date that such litigation is filed.
301              
302             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
303             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
304             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
305             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
306             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
307             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
308             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
309             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310              
311              
312             =cut
313              
314             1; # End of MobileDetect::PP