AMFCommonLib.pm untested

filter: Statementstmt 0.0% 0 / 208 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 308 CC 28 SCAR 35.3
CC
28
Cov
0%
androidDetection
90.0
CleanUa
42.0
GetMultipleUa
42.0
CRAP 34.2
1 #file:Apache2/AMFCommonLib.pm;
2 #--------------------------------
3  
4 #
5 # Created by Idel Fuschini
6 # Date: 01/08/10
7 # Site: http://www.apachemobilefilter.org
8 # Mail: idel.fuschini@gmail.com
9  
10 package Apache2::AMFCommonLib;
11 use strict;
12 use warnings;
13 use vars qw($VERSION);
14 $VERSION= "4.33";;;
15  
16  
17 sub new {
18 my $package = shift;
19 return bless({}, $package);
20 }
21  
22 sub getMobileArray {
23 my %MobileArray;
24 my $mobileParam="android,bolt,brew,docomo,foma,hiptop,htc,ipod,ipad,kddi,kindle,lge,maemo,midp,mobi,netfront,nintendo,nokia,novarra,openweb,palm,phone,playstation,psp,samsung,sanyo,softbank,sony,symbian,up.browser,up.link,wap,webos,windows ce,wireless,xv6875.1,mini,mobi,symbos,touchpad,rim,arm,zune,spv,blackberry,mitsu,siem,sama,sch-,moto,ipaq,sec-,sgh-,gradiente,alcat,mot-,sagem,ericsson,lg-,lg/,nec-,philips,panasonic,kwc-,portalm,telit,ericy,zte,hutc,qc-,sharp,vodafone,compal,dbtel,sendo,benq,bird,amoi,becker,lenovo,tsm";
25 my @dummyMobileKeys = split(/,/, $mobileParam);
26 foreach my $dummy (@dummyMobileKeys) {
27 $MobileArray{$dummy}='mobile';
28 }
29 return %MobileArray;
30 }
31 sub getPCArray {
32 my %PCArray;
33 $PCArray{'chrome'}='google_chrome';
34 my $i=0;
35 while ($i < 28) {
36 $PCArray{"chrome/$i"}="google_chrome_$i";
37 $i++;
38 }
39 $i=4;
40 $PCArray{'firefox'}='firefox';
41 $PCArray{'firefox/1.0'}='firefox_1';
42 $PCArray{'firefox/2.0'}='firefox_2';
43 $PCArray{'firefox/3.0'}='firefox_3';
44 $PCArray{'firefox/3.5'}='firefox_3_5';
45 while ($i < 12) {
46 $PCArray{"firefox/$i.0"}="firefox_".$i."_0";
47 $i++;
48 }
49 $PCArray{'chrome/'}='google_chrome_';
50 $PCArray{'msie'}='msie';
51 $PCArray{'msie 5'}='msie_5';
52 $PCArray{'msie 6'}='msie_6';
53 $PCArray{'msie 7'}='msie_7';
54 $PCArray{'msie 8'}='msie_8';
55 $PCArray{'msie 9'}='msie_9';
56 $PCArray{'opera'}='opera';
57 $PCArray{'konqueror'}='konqueror';
58 return %PCArray;
59 }
60 sub getMD5 {
61 my $self = shift;
62 my $file;
63 if (@_) {
64 $file = shift;
65 }
66 open(FILE, $file) or die "Can't open '$file': $!";
67 binmode(FILE);
68 my $returnMD5=Digest::MD5->new->addfile(*FILE)->hexdigest;
69 return $returnMD5;
70 }
71 sub Data {
72 my $_sec;
73 my $_min;
74 my $_hour;
75 my $_mday;
76 my $_day;
77 my $_mon;
78 my $_year;
79 my $_wday;
80 my $_yday;
81 my $_isdst;
82 my $_data;
83 ($_sec,$_min,$_hour,$_mday,$_mon,$_year,$_wday,$_yday,$_isdst) = localtime(time);
84 $_mon=$_mon+1;
85 $_year=substr($_year,1);
86 $_mon=&correct_number($_mon);
87 $_mday=&correct_number($_mday);
88 $_hour=&correct_number($_hour);
89 $_min=&correct_number($_min);
90 $_sec=&correct_number($_sec);
91 $_data="$_mday/$_mon/$_year - $_hour:$_min:$_sec";
92 return $_data;
93 }
94 sub correct_number {
95 my ($number) = @_;
96 if ($number < 10) {
97 $number="0$number";
98 }
99 return $number;
100 }
101 sub printLog {
102 my $self = shift;
103 if (@_) {
104 $self->{'printLog'} = shift;
105 }
106 my $data=Data();
107 print "$data - $self->{'printLog'}\n";
108 }
109 sub CleanUa {
110 my $self = shift;
111 my $UserAgent;
112 if (@_) {
113 $UserAgent = shift;
114 }
115 my $string="";
116 $UserAgent =~ s/\ //g;
117 #$UserAgent =~ s/([0-9\\.]+).*?//g;
118 $UserAgent =~ s/iemobile \/([0-9\\.]+).*?/iemobile /g;
119 $UserAgent =~ s/series40\/([0-9\\.]+)...(!?abc)*?/series40/g;
120 $UserAgent =~ s/series60\/([0-9\\.]+)...(!?abc)*?/series60/g;
121  
122 if ( $UserAgent =~ m/^outlook/i ) {
123 $UserAgent=substr($UserAgent,index($UserAgent,'(') + 1,length($UserAgent) - index($UserAgent,'(') -2);
124 }
125  
126 if ( $UserAgent =~ m/windows nt/i) {
127 my $first=substr($UserAgent,0,index($UserAgent,'windows nt') + 12);
128 my $second="";
129 if (length($UserAgent) > index($UserAgent,'windows nt') + 14) {
130 $second=substr($UserAgent,index($UserAgent,'windows nt') + 14);
131 }
132 $UserAgent=$first.$second;
133 }
134 my @arrayFile=split(/\ /, $UserAgent);
135 foreach my $field (@arrayFile) {
136 if ($field =~ m/applewebkit/i || $field =~ m/chrome/i || $field =~ m/safari/i) {
137 my ($first,$second)=split(/\//, $field);
138  
139 $string=$string." ".$first;
140 } else {
141 $string=$string." ".$field;
142 }
143 }
144 $string=substr($string,1);
145 return $string;
146 }
147 sub GetMultipleUa {
148 my $self = shift;
149 my $UserAgent;
150 my $deep;
151 my $count=0;
152 if (@_) {
153 $UserAgent = shift;
154 $deep = shift;
155 }
156 my $length=length($UserAgent);
157 my %ArrayUAparse;
158 if (substr($UserAgent,$length-1,1) eq ')') {
159 $UserAgent=substr($UserAgent,0,$length-1);
160 }
161 $UserAgent =~ s/\ /|/g;
162 $UserAgent =~ s/\//|/g;
163 $UserAgent =~ s/\-/|/g;
164 $UserAgent =~ s/\_/|/g;
165 $UserAgent =~ s/\./|/g;
166 my @pairs = split(/\|/, $UserAgent);
167 my $deep_to_verify=scalar(@pairs) - $deep - 1;
168 my $ind=0;
169 my $string="";
170 if ($deep > scalar(@pairs)) {
171 $deep=scalar(@pairs) - 1;
172 }
173 foreach my $key (@pairs) {
174 if ($ind==0) {
175 $string=$key;
176 } else {
177 $string=$string." ".$key;
178 }
179 if ($ind > $deep - 1) {
180 $ArrayUAparse{$ind}=$string;
181 }
182 $ind++;
183 }
184 return %ArrayUAparse;
185
186 }
187  
188 sub androidDetection {
189 my $self = shift;
190 my $ua="";
191 if (@_) {
192 $ua = shift;
193 }
194 #print "$ua----------\n";
195 my $version='nc';
196 my $os='nc';
197 if (index($ua,'android') > -1 ) {
198 #my $string_to_parse=substr($ua,index($ua,'(') + 1,index($ua,')'));
199 my @param=split(/\;/,$ua);
200 #my ($dummy1,$dummy2,$vers,$lan,$dummy5)=split(/\;/,$string_to_parse);
201 my $element=scalar @param;
202 my $count=0;
203 my $count_add=0;
204 my @param_ua;
205 if ($element > 0) {
206 while ($count<$element) {
207 if (index($param[$count],'-')>-1 && length($param[$count])==6) {
208 } elsif (length($param[$count])==2) {
209 } elsif (index($param[$count],'android')>-1) {
210 ($os,$version)=split(/ /,$param[$count]);
211 if ($version) {
212 if (index($version,'.') > -1) {
213 $version =~ s/\.//g;
214 }
215 }
216 $param_ua[$count_add]="android xx";
217 $count_add++;
218 } else {
219 $param_ua[$count_add]=$param[$count];
220 $count_add++;
221 }
222 $count++;
223 }
224 $count=0;
225 $element=scalar @param_ua;
226 $ua = "";
227 while ($count < $element) {
228 $ua=$ua." ".$param_ua[$count];
229 $count++;
230 }
231 $ua=substr($ua,1);
232 }
233 #print $ua."\n";
234 }
235 return ($ua,$version);
236  
237 }
238 sub botDetection {
239 my $self = shift;
240 my $ua="";
241 my @arrayBot = ('googlebot','google web preview','msnbot','google.com/bot','ia_archiver','yahoo!','webalta crawler','flickysearchbot','yanga worldsearch','stackrambler','mail.ru','yandex');
242 if (@_) {
243 $ua = shift;
244 }
245 foreach my $pair (@arrayBot) {
246 if (index($ua,$pair) > -1 ) {
247 $ua='It is a bot';
248 }
249 }
250 return $ua;
251  
252 }
253 sub readCookie {
254 my $self = shift;
255 my $cookie_search;
256 if (@_) {
257 $cookie_search = shift;
258 }
259 my $param_tofound;
260 my $string_tofound;
261 my $value="";
262 my $id_return="";
263 my @pairs = split(/;/, $cookie_search);
264 my $name;
265 foreach $param_tofound (@pairs) {
266 ($string_tofound,$value)=split(/=/, $param_tofound);
267 if ($string_tofound =~ "amfID") {
268 $id_return=$value;
269 }
270 }
271 return $id_return;
272 }
273 sub readCookie_fullB {
274 my $self = shift;
275 my $cookie_search;
276 if (@_) {
277 $cookie_search = shift;
278 }
279 my $param_tofound;
280 my $string_tofound;
281 my $value="";
282 my $id_return="";
283 my @pairs = split(/;/, $cookie_search);
284 my $name;
285 foreach $param_tofound (@pairs) {
286 ($string_tofound,$value)=split(/=/, $param_tofound);
287 if ($string_tofound =~ "amfFull") {
288 $id_return=$value;
289 }
290 }
291 return $id_return;
292 }
293  
294 1;
295  
296  
297 =head1 NAME
298  
299 Apache2::AMFCommonLib - Common Library That AMF uses.
300  
301 =head1 DESCRIPTION
302  
303 Is a simple Common Library for AMF
304  
305 =head1 AMF PROJECT SITE
306  
307 http://www.apachemobilefilter.org
308  
309 =head1 DOCUMENTATION
310  
311 http://wiki.apachemobilefilter.org
312  
313 =head1 AUTHOR
314  
315 Idel Fuschini (idel.fuschini [at] gmail [dot] com)
316  
317 =head1 COPYRIGHT
318  
319 You may distribute under the terms of either the GNU General Public
320 License or the Artistic License, as specified in the Perl README file.
321  
322 =cut