| 1 | #file:Apache2/AMFDetectRightFilter.pm; | ||
| 2 | #-------------------------------- | ||
| 3 | |||
| 4 | # | ||
| 5 | # Created by Idel Fuschini | ||
| 6 | # Date: 15/10/11 | ||
| 7 | # Site: http://www.apachemobilefilter.org | ||
| 8 | # Mail: idel.fuschini@gmail.com | ||
| 9 | |||
| 10 | |||
| 11 | package Apache2::AMFDetectRightFilter; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use Apache2::AMFCommonLib (); | ||
| 16 | |||
| 17 | use Apache2::RequestRec (); | ||
| 18 | use Apache2::RequestUtil (); | ||
| 19 | use Apache2::SubRequest (); | ||
| 20 | use Apache2::Log; | ||
| 21 | use Apache2::Filter (); | ||
| 22 | use APR::Table (); | ||
| 23 | use LWP::Simple qw($ua get); | ||
| 24 | use Apache2::Const -compile => qw(OK REDIRECT DECLINED); | ||
| 25 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; | ||
| 26 | use constant BUFF_LEN => 1024; | ||
| 27 | use Cache::FileBackend; | ||
| 28 | |||
| 29 | |||
| 30 | # | ||
| 31 | # Define the global environment | ||
| 32 | # | ||
| 33 | |||
| 34 | use vars qw($VERSION); | ||
| 35 | $VERSION= "4.33";;; | ||
| 36 | my $CommonLib = new Apache2::AMFCommonLib (); | ||
| 37 | |||
| 38 | my %Capability; | ||
| 39 | my %Array_fb; | ||
| 40 | my %Array_id; | ||
| 41 | my %Array_fullua_id; | ||
| 42 | my %Array_DDRcapability; | ||
| 43 | |||
| 44 | my %MobileArray=$CommonLib->getMobileArray; | ||
| 45 | my %PCArray=$CommonLib->getPCArray; | ||
| 46 | my $mobileversionurl="none"; | ||
| 47 | my $fullbrowserurl="none"; | ||
| 48 | my $redirecttranscoder="true"; | ||
| 49 | my $redirecttranscoderurl="none"; | ||
| 50 | my $resizeimagedirectory="none"; | ||
| 51 | my $DetectRightnetdownload="false"; | ||
| 52 | my $downloadDetectRighturl="false"; | ||
| 53 | my $listall="false"; | ||
| 54 | my $cookiecachesystem="false"; | ||
| 55 | my $DetectRightVersion="unknown"; | ||
| 56 | my $personalDetectRighturl='unknown'; | ||
| 57 | my $cachedirectorystore="/tmp"; | ||
| 58 | my $capabilitylist="none"; | ||
| 59 | my $restmode='false'; | ||
| 60 | my $deepSearch=2; | ||
| 61 | my $checkVersion='false'; | ||
| 62 | my $mobilenable="false"; | ||
| 63 | |||
| 64 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 65 | $CommonLib->printLog("------- APACHE MOBILE FILTER V$VERSION -------"); | ||
| 66 | $CommonLib->printLog("------- support http://groups.google.com/group/amf-device-detection -------"); | ||
| 67 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 68 | $CommonLib->printLog("AMFDetectRightFilter module Version $VERSION"); | ||
| 69 | if (($ENV{AMFSetProxy}) && $ENV{AMFSetProxy} eq 'true' ) { | ||
| 70 | $CommonLib->printLog("AMFSetHttpProxy is ".$ENV{AMFSetHttpProxy}); | ||
| 71 | my $port=3128; | ||
| 72 | my $default=""; | ||
| 73 | if ($ENV{AMFSetHttpProxyPort}) { | ||
| 74 | $port=$ENV{AMFSetHttpProxyPort}; | ||
| 75 | } else { | ||
| 76 | $default=' (default)'; | ||
| 77 | } | ||
| 78 | $CommonLib->printLog("AMFSetHttpProxyPort is ".$port.$default); | ||
| 79 | $ua->proxy('http','http://'.$ENV{AMFSetHttpProxy}.':'.$port); | ||
| 80 | } | ||
| 81 | my $timeoutinsecond=5; | ||
| 82 | my $default=""; | ||
| 83 | if ($ENV{AMFSetGetParameterTimeoOut}) { | ||
| 84 | $timeoutinsecond=$ENV{AMFSetGetParameterTimeoOut}; | ||
| 85 | } else { | ||
| 86 | $default=' (default)'; | ||
| 87 | } | ||
| 88 | $CommonLib->printLog("AMFSetGetParameterTimeoOut is ".$timeoutinsecond.'s'.$default); | ||
| 89 | $ua->timeout($timeoutinsecond); | ||
| 90 | if ($ENV{AMFCheckVersion}) { | ||
| 91 | $checkVersion=$ENV{AMFCheckVersion}; | ||
| 92 | } | ||
| 93 | if ($ENV{AMFCheckVersion}) { | ||
| 94 | $checkVersion=$ENV{AMFCheckVersion}; | ||
| 95 | } | ||
| 96 | if ($checkVersion eq 'true') { | ||
| 97 | $CommonLib->printLog("Check on apchemobilefilter.org if the installed AMF is the last version"); | ||
| 98 | $CommonLib->printLog("Try to download http://www.apachemobilefilter.org/param/amf.config"); | ||
| 99 | my $url="http://www.apachemobilefilter.org/param/amf.config"; | ||
| 100 | my $content = get ($url); | ||
| 101 | $content =~ s/\n//g; | ||
| 102 | my $check_version=0; | ||
| 103 | if ($content) { | ||
| 104 | $check_version=$content; | ||
| 105 | } | ||
| 106 | if ($check_version > $VERSION && $check_version ne 0) { | ||
| 107 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 108 | $CommonLib->printLog("----- There is a new version of AMF V$check_version online ----"); | ||
| 109 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 110 | } else { | ||
| 111 | $CommonLib->printLog("AMF installed is the last version"); | ||
| 112 | } | ||
| 113 | } else { | ||
| 114 | $CommonLib->printLog("AMFCheckVersion is false, AMF don't check the last version."); | ||
| 115 | } | ||
| 116 | if ($ENV{ResizeImageDirectory}) { | ||
| 117 | $Capability{'max_image_width'}="max_image_width"; | ||
| 118 | $Capability{'max_image_height'}="max_image_width"; | ||
| 119 | $resizeimagedirectory=$ENV{ResizeImageDirectory}; | ||
| 120 | } | ||
| 121 | if (($ENV{FullBrowserUrl}) || ($ENV{MobileVersionUrl})) { | ||
| 122 | $Capability{'device_claims_web_support'}="device_claims_web_support"; | ||
| 123 | $Capability{'is_wireless_device'}="is_wireless_device"; | ||
| 124 | $fullbrowserurl=$ENV{FullBrowserUrl} | ||
| 125 | } | ||
| 126 | if ($ENV{RedirectTranscoderUrl}) { | ||
| 127 | $Capability{'is_transcoder'}="is_transcoder"; | ||
| 128 | $redirecttranscoderurl=$ENV{RedirectTranscoderUrl}; | ||
| 129 | } | ||
| 130 | |||
| 131 | # | ||
| 132 | # Check if AMFMobileHome and CacheDirectoryStore is setting in apache httpd.conf file for example: | ||
| 133 | # PerlSetEnv AMFMobileHome <apache_directory>/MobileFilter | ||
| 134 | # | ||
| 135 | if ($ENV{CacheDirectoryStore}) { | ||
| 136 | $cachedirectorystore=$ENV{CacheDirectoryStore}; | ||
| 137 | $CommonLib->printLog("CacheDirectoryStore is: $cachedirectorystore"); | ||
| 138 | } else { | ||
| 139 | $CommonLib->printLog("CacheDirectoryStore not exist. Please set the variable CacheDirectoryStore into httpd.conf, (the directory must be writeable)"); | ||
| 140 | ModPerl::Util::exit(); | ||
| 141 | } | ||
| 142 | # | ||
| 143 | # Define the cache system directory | ||
| 144 | # | ||
| 145 | my $cacheSystem = new Cache::FileBackend( $cachedirectorystore, 3, 000 ); | ||
| 146 | $cacheSystem->store( 'DetectRight-id', 'device_not_found', "id=device_not_found&device=false&device_claims_web_support=true&is_wireless_device=false"); | ||
| 147 | if ($cacheSystem->restore('DetectRight-conf','ver')) { | ||
| 148 | } else { | ||
| 149 | $CommonLib->printLog('Create new wurf-con store'); | ||
| 150 | $cacheSystem->store('DetectRight-conf', 'ver', 'null'); | ||
| 151 | $cacheSystem->store('DetectRight-conf', 'caplist', 'null'); | ||
| 152 | $cacheSystem->store('DetectRight-conf', 'listall', 'null'); | ||
| 153 | $cacheSystem->store('DetectRight-conf', 'RedirectTranscoderUrl','null'); | ||
| 154 | $cacheSystem->store('DetectRight-conf', 'MobileVersionUrl','null'); | ||
| 155 | $cacheSystem->store('DetectRight-conf', 'FullBrowserUrl','null'); | ||
| 156 | $cacheSystem->store('DetectRight-conf', 'ResizeImageDirectory','null'); | ||
| 157 | } | ||
| 158 | if ($ENV{AMFMobileHome}) { | ||
| 159 | &loadConfigFile("$ENV{AMFMobileHome}/DetectRightAMF.xml"); | ||
| 160 | } else { | ||
| 161 | $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf"); | ||
| 162 | ModPerl::Util::exit(); | ||
| 163 | } | ||
| 164 | sub loadConfigFile { | ||
| 165 | my ($fileDetectRight) = @_; | ||
| 166 | my $null=""; | ||
| 167 | my $null2=""; | ||
| 168 | my $null3=""; | ||
| 169 | my $val; | ||
| 170 | my $capability; | ||
| 171 | my $r_id; | ||
| 172 | my $dummy; | ||
| 173 | #The filter | ||
| 174 | $CommonLib->printLog("Start read configuration from httpd.conf"); | ||
| 175 | |||
| 176 | if ($ENV{DetectRightNetDownload}) { | ||
| 177 | if ($ENV{DetectRightNetDownload} eq 'true' || $ENV{DetectRightNetDownload} eq 'false') { | ||
| 178 | $DetectRightnetdownload=$ENV{DetectRightNetDownload}; | ||
| 179 | $CommonLib->printLog("DetectRightNetDownload is: $DetectRightnetdownload"); | ||
| 180 | } else { | ||
| 181 | $CommonLib->printLog("Error DetectRightNetDownload parmeter must set to true or false"); | ||
| 182 | ModPerl::Util::exit(); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | if ($ENV{DownloadDetectRightURL}) { | ||
| 187 | $downloadDetectRighturl=$ENV{DownloadDetectRightURL}; | ||
| 188 | $CommonLib->printLog("DownloadDetectRightURL is: $downloadDetectRighturl"); | ||
| 189 | } | ||
| 190 | if ($ENV{CapabilityList}) { | ||
| 191 | my @dummycapability = split(/,/, $ENV{CapabilityList}); | ||
| 192 | $capabilitylist=$ENV{CapabilityList}; | ||
| 193 | foreach $dummy (@dummycapability) { | ||
| 194 | if ($dummy eq "all") { | ||
| 195 | $listall="true"; | ||
| 196 | } | ||
| 197 | $Capability{$dummy}=$dummy; | ||
| 198 | $CommonLib->printLog("CapabilityList is: $dummy"); | ||
| 199 | } | ||
| 200 | } else { | ||
| 201 | $listall="true"; | ||
| 202 | $CommonLib->printLog('CapabilityList not setted so the default value is "all"'); | ||
| 203 | } | ||
| 204 | if ($ENV{AMFMobileKeys}) { | ||
| 205 | my @dummyMobileKeys = split(/,/, $ENV{AMFMobileKeys}); | ||
| 206 | foreach $dummy (@dummyMobileKeys) { | ||
| 207 | $MobileArray{$dummy}='mobile'; | ||
| 208 | } | ||
| 209 | $CommonLib->printLog("AMFMobileKeys is: $ENV{AMFMobileKeys}"); | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | if ($ENV{AMFProductionMode}) { | ||
| 214 | $cookiecachesystem=$ENV{AMFProductionMode}; | ||
| 215 | $CommonLib->printLog("AMFProductionMode is: $cookiecachesystem"); | ||
| 216 | } else { | ||
| 217 | $CommonLib->printLog("AMFProductionMode (the CookieCacheSystem is deprecated) is not setted the default value is $cookiecachesystem"); | ||
| 218 | } | ||
| 219 | if ($ENV{PersonalDetectRightFileName}) { | ||
| 220 | $personalDetectRighturl=$ENV{AMFMobileHome}."/".$ENV{PersonalDetectRightFileName}; | ||
| 221 | $CommonLib->printLog("PersonalDetectRightFileName is: $ENV{PersonalDetectRightFileName}"); | ||
| 222 | } | ||
| 223 | if ($ENV{RestMode}) { | ||
| 224 | $restmode=$ENV{RestMode}; | ||
| 225 | $CommonLib->printLog("RestMode is: $restmode"); | ||
| 226 | } | ||
| 227 | if ($ENV{AMFDeepParse}) { | ||
| 228 | $deepSearch=$ENV{AMFDeepParse}; | ||
| 229 | $CommonLib->printLog("AMFDeepParse is: $deepSearch"); | ||
| 230 | } else { | ||
| 231 | $CommonLib->printLog("AMFDeepParse is not setted the default value is 3"); | ||
| 232 | } | ||
| 233 | if ($ENV{FullBrowserMobileAccessKey}) { | ||
| 234 | $mobilenable="$ENV{FullBrowserMobileAccessKey}"; | ||
| 235 | $CommonLib->printLog("FullBrowserMobileAccessKey is: $ENV{FullBrowserMobileAccessKey}"); | ||
| 236 | $CommonLib->printLog("For access the device to fullbrowser set the link: <url>?$mobilenable=true"); | ||
| 237 | } | ||
| 238 | $CommonLib->printLog("Finish loading parameter"); | ||
| 239 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 240 | if ($DetectRightnetdownload eq "true") { | ||
| 241 | |||
| 242 | $CommonLib->printLog("Start process downloading DetectRightAMF.xml from $downloadDetectRighturl"); | ||
| 243 | $CommonLib->printLog ("Test the URL"); | ||
| 244 | my ($content_type, $document_length, $modified_time, $expires, $server) = head($downloadDetectRighturl); | ||
| 245 | if ($content_type eq "") { | ||
| 246 | $CommonLib->printLog("Couldn't get $downloadDetectRighturl."); | ||
| 247 | ModPerl::Util::exit(); | ||
| 248 | } else { | ||
| 249 | $CommonLib->printLog("The URL is correct"); | ||
| 250 | $CommonLib->printLog("The size of document wurf file: $document_length bytes"); | ||
| 251 | } | ||
| 252 | |||
| 253 | if ($content_type eq 'application/zip') { | ||
| 254 | $CommonLib->printLog("The file is a zip file."); | ||
| 255 | $CommonLib->printLog ("Start downloading"); | ||
| 256 | my @dummypairs = split(/\//, $downloadDetectRighturl); | ||
| 257 | my ($ext_zip) = $downloadDetectRighturl =~ /\.(\w+)$/; | ||
| 258 | my $filezip=$dummypairs[-1]; | ||
| 259 | my $tmp_dir=$ENV{AMFMobileHome}; | ||
| 260 | $filezip="$tmp_dir/$filezip"; | ||
| 261 | my $status = getstore ($downloadDetectRighturl,$filezip); | ||
| 262 | my $output="$tmp_dir/tmp_DetectRightAMF.xml"; | ||
| 263 | unzip $filezip => $output | ||
| 264 | or die "unzip failed: $UnzipError\n"; | ||
| 265 | # | ||
| 266 | # call parseDetectRightFile | ||
| 267 | # | ||
| 268 | callparseDetectRightFile($output); | ||
| 269 | |||
| 270 | } else { | ||
| 271 | $CommonLib->printLog("The file is a xml file."); | ||
| 272 | my $content = get ($downloadDetectRighturl); | ||
| 273 | $content =~ s/\n//g; | ||
| 274 | $content =~ s/>/>\n/g; | ||
| 275 | |||
| 276 | my @rows = split(/\n/, $content); | ||
| 277 | my $row; | ||
| 278 | my $count=0; | ||
| 279 | my $numberRow=scalar(@rows); | ||
| 280 | my $progress=0; | ||
| 281 | foreach $row (@rows){ | ||
| 282 | $r_id=parseDetectRightFile($row,$r_id); | ||
| 283 | $progress++; | ||
| 284 | my $perc=int(($progress/$numberRow)*100); | ||
| 285 | print "Percent loaded: ".$perc."%\r"; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | $CommonLib->printLog("Finish downloading DetectRight from $downloadDetectRighturl"); | ||
| 289 | |||
| 290 | } else { | ||
| 291 | if (-e "$fileDetectRight") { | ||
| 292 | $CommonLib->printLog("Start loading DetectRightAMF.xml"); | ||
| 293 | if (open (IN,"$fileDetectRight")) { | ||
| 294 | my $filesize= -s $fileDetectRight; | ||
| 295 | my $string_file; | ||
| 296 | read (IN,$string_file,$filesize); | ||
| 297 | close IN; | ||
| 298 | $string_file =~ s/\n//g; | ||
| 299 | $string_file =~ s/>/>\n/g; | ||
| 300 | my @arrayFile=split(/\n/, $string_file); | ||
| 301 | my $numberRow=scalar(@arrayFile); | ||
| 302 | my $progress=0; | ||
| 303 | foreach my $line (@arrayFile) { | ||
| 304 | $r_id=parseDetectRightFile($line,$r_id); | ||
| 305 | $progress++; | ||
| 306 | my $perc=int(($progress/$numberRow)*100); | ||
| 307 | print "Percent loaded: ".$perc."%\r"; | ||
| 308 | } | ||
| 309 | } else { | ||
| 310 | $CommonLib->printLog("Error open file:$fileDetectRight"); | ||
| 311 | ModPerl::Util::exit(); | ||
| 312 | } | ||
| 313 | } else { | ||
| 314 | $CommonLib->printLog("File $fileDetectRight not found"); | ||
| 315 | ModPerl::Util::exit(); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | close IN; | ||
| 319 | my $arrLen = scalar %Array_fb; | ||
| 320 | ($arrLen,$dummy)= split(/\//, $arrLen); | ||
| 321 | if ($arrLen == 0) { | ||
| 322 | $CommonLib->printLog("Error the file probably is not a DetectRight file, control the url or path"); | ||
| 323 | $CommonLib->printLog("Control also if the file is compress file, and DownloadZipFile parameter is seted false"); | ||
| 324 | ModPerl::Util::exit(); | ||
| 325 | } | ||
| 326 | $CommonLib->printLog("DetectRight version: $DetectRightVersion"); | ||
| 327 | if ($cacheSystem->restore('DetectRight-conf', 'amfver') ne $VERSION|| | ||
| 328 | $cacheSystem->restore('DetectRight-conf', 'ResizeImageDirectory') ne $resizeimagedirectory|| | ||
| 329 | $cacheSystem->restore('DetectRight-conf', 'DownloadDetectRightURL') ne $downloadDetectRighturl|| | ||
| 330 | $cacheSystem->restore('DetectRight-conf', 'FullBrowserUrl') ne $fullbrowserurl|| | ||
| 331 | $cacheSystem->restore('DetectRight-conf', 'RedirectTranscoderUrl') ne $redirecttranscoderurl|| | ||
| 332 | $cacheSystem->restore('DetectRight-conf', 'ver') ne $DetectRightVersion || | ||
| 333 | $cacheSystem->restore('DetectRight-conf', 'caplist') ne $capabilitylist|| | ||
| 334 | $cacheSystem->restore('DetectRight-conf', 'listall') ne $listall) { | ||
| 335 | $CommonLib->printLog("********************************************************************************************************"); | ||
| 336 | $CommonLib->printLog("* This is a new version of DetectRight or you change some parameter value or it's a new version of AMF, now the old cache must be deleted *"); | ||
| 337 | $CommonLib->printLog("********************************************************************************************************"); | ||
| 338 | $cacheSystem->store('DetectRight-conf', 'ver', $DetectRightVersion); | ||
| 339 | $cacheSystem->store('DetectRight-conf', 'amfver', $VERSION); | ||
| 340 | $cacheSystem->store('DetectRight-conf', 'caplist', $capabilitylist); | ||
| 341 | $cacheSystem->store('DetectRight-conf', 'listall', $listall); | ||
| 342 | $cacheSystem->store('DetectRight-conf', 'RedirectTranscoderUrl', $redirecttranscoderurl); | ||
| 343 | $cacheSystem->store('DetectRight-conf', 'FullBrowserUrl', $fullbrowserurl); | ||
| 344 | $cacheSystem->store('DetectRight-conf', 'DownloadDetectRightURL', $downloadDetectRighturl); | ||
| 345 | $cacheSystem->store('DetectRight-conf', 'ResizeImageDirectory', $resizeimagedirectory); | ||
| 346 | |||
| 347 | $cacheSystem->delete_namespace( 'DetectRight-id' ); | ||
| 348 | $cacheSystem->delete_namespace( 'DetectRight-ua' ); | ||
| 349 | } | ||
| 350 | $CommonLib->printLog("This version of DetectRight has $arrLen UserAgent"); | ||
| 351 | $CommonLib->printLog("End loading DetectRightAMF.xml"); | ||
| 352 | if ($personalDetectRighturl ne 'unknown') { | ||
| 353 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 354 | if (-e "$personalDetectRighturl") { | ||
| 355 | $CommonLib->printLog("Start loading $ENV{PersonalDetectRightFileName}"); | ||
| 356 | if (open (IN,"$personalDetectRighturl")) { | ||
| 357 | my $filesize= -s $personalDetectRighturl; | ||
| 358 | my $string_file; | ||
| 359 | read (IN,$string_file,$filesize); | ||
| 360 | close IN; | ||
| 361 | $string_file =~ s/\n//g; | ||
| 362 | $string_file =~ s/>/>\n/g; | ||
| 363 | my @arrayFile=split(/\n/, $string_file); | ||
| 364 | foreach my $line (@arrayFile) { | ||
| 365 | #print "$line\n"; | ||
| 366 | $r_id=parseDetectRightFile($line,$r_id); | ||
| 367 | } | ||
| 368 | } else { | ||
| 369 | $CommonLib->printLog("Error open file:$personalDetectRighturl"); | ||
| 370 | ModPerl::Util::exit(); | ||
| 371 | } | ||
| 372 | $CommonLib->printLog("END loading $ENV{PersonalDetectRightFileName}"); | ||
| 373 | close IN; | ||
| 374 | |||
| 375 | } else { | ||
| 376 | $CommonLib->printLog("File $personalDetectRighturl not found"); | ||
| 377 | ModPerl::Util::exit(); | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | } | ||
| 383 | sub callparseDetectRightFile { | ||
| 384 | my ($output) = @_; | ||
| 385 | my $r_id; | ||
| 386 | if (open (IN,"$output")) { | ||
| 387 | my $filesize= -s $output; | ||
| 388 | my $string_file; | ||
| 389 | read (IN,$string_file,$filesize); | ||
| 390 | close IN; | ||
| 391 | $string_file =~ s/\n//g; | ||
| 392 | $string_file =~ s/>/>\n/g; | ||
| 393 | my @arrayFile=split(/\n/, $string_file); | ||
| 394 | foreach my $line (@arrayFile) { | ||
| 395 | $r_id=parseDetectRightFile($line,$r_id); | ||
| 396 | } | ||
| 397 | } else { | ||
| 398 | $CommonLib->printLog("Error open file:$output"); | ||
| 399 | ModPerl::Util::exit(); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | sub parseDetectRightFile { | ||
| 403 | my ($record,$val) = @_; | ||
| 404 | my $null=""; | ||
| 405 | my $null2=""; | ||
| 406 | my $null3=""; | ||
| 407 | my $uagent=""; | ||
| 408 | my $fb=""; | ||
| 409 | my $value=""; | ||
| 410 | my $id; | ||
| 411 | my $name=""; | ||
| 412 | my $version=""; | ||
| 413 | if ($val) { | ||
| 414 | $id="$val"; | ||
| 415 | } | ||
| 416 | if ($record =~ /\<device/o) { | ||
| 417 | if (index($record,'user_agent') > -1 ) { | ||
| 418 | $uagent=lc(substr($record,index($record,'user_agent') + 12,index($record,'"',index($record,'user_agent')+ 13)- index($record,'user_agent') - 12)); | ||
| 419 | |||
| 420 | if (index($uagent,'blackberry') >-1 ) { | ||
| 421 | $uagent=substr($uagent,index($uagent,'blackberry')); | ||
| 422 | } | ||
| 423 | ($uagent,$version)=$CommonLib->androidDetection($uagent); | ||
| 424 | } | ||
| 425 | if (index($record,'id') > -1 ) { | ||
| 426 | $id=substr($record,index($record,'id') + 4,index($record,'"',index($record,'id')+ 5)- index($record,'id') - 4); | ||
| 427 | } | ||
| 428 | if (index($record,'fall_back') > -1 ) { | ||
| 429 | $fb=substr($record,index($record,'fall_back') + 11,index($record,'"',index($record,'fall_back')+ 12)- index($record,'fall_back') - 11); | ||
| 430 | } | ||
| 431 | if (($fb) && ($id)) { | ||
| 432 | $Array_fb{"$id"}=$fb; | ||
| 433 | } | ||
| 434 | if (($uagent) && ($id)) { | ||
| 435 | my %ParseUA=$CommonLib->GetMultipleUa($uagent,$deepSearch); | ||
| 436 | my $pair; | ||
| 437 | my $arrUaLen = scalar %ParseUA; | ||
| 438 | my $contaUA=0; | ||
| 439 | my $Array_fullua_id=$uagent; | ||
| 440 | foreach $pair (reverse sort { $a <=> $b } keys %ParseUA) { | ||
| 441 | my $dummy=$ParseUA{$pair}; | ||
| 442 | if ($Array_id{$dummy}) {} else { | ||
| 443 | $Array_id{$dummy}=$id; | ||
| 444 | } | ||
| 445 | $contaUA=$contaUA-1; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | } | ||
| 450 | if ($record =~ /\<capability/o) { | ||
| 451 | ($null,$name,$null2,$value,$null3,$fb)=split(/\"/, $record); | ||
| 452 | if ($listall eq "true") { | ||
| 453 | $Capability{$name}=$name; | ||
| 454 | } | ||
| 455 | if (($id) && ($Capability{$name}) && ($name) && ($value)) { | ||
| 456 | $Array_DDRcapability{"$val|$name"}=$value; | ||
| 457 | } | ||
| 458 | } | ||
| 459 | if ($record =~ /\/ver>/o) { | ||
| 460 | $DetectRightVersion=substr($record,index($record,'<ver>') + 5,index($record,'</ver>') - 9); | ||
| 461 | } | ||
| 462 | return $id; | ||
| 463 | |||
| 464 | } | ||
| 465 | sub FallBack { | ||
| 466 | my ($idToFind) = @_; | ||
| 467 | my $dummy_id; | ||
| 468 | my $dummy; | ||
| 469 | my $dummy2; | ||
| 470 | my $LOOP; | ||
| 471 | my %ArrayCapFoundToPass; | ||
| 472 | my $capability; | ||
| 473 | foreach $capability (sort keys %Capability) { | ||
| 474 | $dummy_id=$idToFind; | ||
| 475 | $LOOP=0; | ||
| 476 | while ($LOOP<2) { | ||
| 477 | $dummy="$dummy_id|$capability"; | ||
| 478 | if ($Array_DDRcapability{$dummy}) { | ||
| 479 | $LOOP=2; | ||
| 480 | $dummy2="$dummy_id|$capability"; | ||
| 481 | $ArrayCapFoundToPass{$capability}=$Array_DDRcapability{$dummy2}; | ||
| 482 | } else { | ||
| 483 | if ($Array_fb{$dummy_id}) { | ||
| 484 | $dummy_id=$Array_fb{$dummy_id}; | ||
| 485 | } else { | ||
| 486 | $dummy_id="root"; | ||
| 487 | } | ||
| 488 | if ($dummy_id eq "root" || $dummy_id eq "generic") { | ||
| 489 | $LOOP++; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | } | ||
| 495 | return %ArrayCapFoundToPass; | ||
| 496 | } | ||
| 497 | sub IdentifyUAMethod { | ||
| 498 | my ($UserAgent) = @_; | ||
| 499 | my $ind=0; | ||
| 500 | my %ArrayPM; | ||
| 501 | my $pair; | ||
| 502 | my $pair2; | ||
| 503 | my $id_find=""; | ||
| 504 | my $dummy; | ||
| 505 | my $uagent_toMatch; | ||
| 506 | my $near_toFind=100; | ||
| 507 | my $near_toMatch; | ||
| 508 | my %ArrayUAType=$CommonLib->GetMultipleUa(lc($UserAgent),$deepSearch); | ||
| 509 | foreach $pair (reverse sort { $a <=> $b } keys %ArrayUAType) | ||
| 510 | { | ||
| 511 | my $dummy=$ArrayUAType{$pair}; | ||
| 512 | if ($Array_id{$dummy}) { | ||
| 513 | if (!$id_find) { | ||
| 514 | $id_find=$Array_id{$dummy}; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | } | ||
| 518 | return $id_find; | ||
| 519 | } | ||
| 520 | sub IdentifyPCUAMethod { | ||
| 521 | my ($UserAgent) = @_; | ||
| 522 | my $ind=0; | ||
| 523 | my $id_find="none"; | ||
| 524 | my $pair; | ||
| 525 | my $length=0; | ||
| 526 | |||
| 527 | foreach $pair (sort keys %PCArray) { | ||
| 528 | if ($UserAgent =~ m/$pair/ && $id_find eq 'none') { | ||
| 529 | $id_find=$PCArray{$pair}; | ||
| 530 | } | ||
| 531 | } | ||
| 532 | if ($id_find) {}else{$id_find="";}; | ||
| 533 | return $id_find; | ||
| 534 | } | ||
| 535 | |||
| 536 | |||
| 537 | sub handler { | ||
| 538 | my $f = shift; | ||
| 539 | my $capability2; | ||
| 540 | my $variabile=""; | ||
| 541 | my $user_agent=lc($f->headers_in->{'User-Agent'}|| ''); | ||
| 542 | my $x_user_agent=$f->headers_in->{'X-Device-User-Agent'}|| ''; | ||
| 543 | my $x_operamini_phone_ua=$f->headers_in->{'X-OperaMini-Phone-Ua'}|| ''; | ||
| 544 | my $x_operamini_ua=$f->headers_in->{'X-OperaMini-Ua'}|| ''; | ||
| 545 | my $query_string=$f->args; | ||
| 546 | my $docroot = $f->document_root(); | ||
| 547 | my $id=""; | ||
| 548 | my $location="none"; | ||
| 549 | my $width_toSearch; | ||
| 550 | my $type_redirect="internal"; | ||
| 551 | my $return_value; | ||
| 552 | my $dummy=""; | ||
| 553 | my $variabile2=""; | ||
| 554 | my %ArrayCapFound; | ||
| 555 | my $controlCookie; | ||
| 556 | my $query_img=""; | ||
| 557 | $ArrayCapFound{is_transcoder}='false'; | ||
| 558 | $ArrayCapFound{'device_claims_web_support'}='none'; | ||
| 559 | $ArrayCapFound{'is_wireless_device'}='none'; | ||
| 560 | my %ArrayQuery; | ||
| 561 | my $var; | ||
| 562 | my $mobile=0; | ||
| 563 | my $version=""; | ||
| 564 | my $realPCbrowser='none'; | ||
| 565 | if ($user_agent eq "") { | ||
| 566 | $user_agent="no useragent found"; | ||
| 567 | } | ||
| 568 | if ($x_user_agent) { | ||
| 569 | $user_agent=lc($x_user_agent); | ||
| 570 | } | ||
| 571 | if ($x_operamini_phone_ua) { | ||
| 572 | $user_agent=lc($x_operamini_phone_ua); | ||
| 573 | } | ||
| 574 | my $cookie = $f->headers_in->{Cookie} || ''; | ||
| 575 | $id=$CommonLib->readCookie($cookie); | ||
| 576 | my $amfFull=$CommonLib->readCookie_fullB($cookie); | ||
| 577 | if ($query_string) { | ||
| 578 | my @vars = split(/&/, $query_string); | ||
| 579 | foreach $var (sort @vars){ | ||
| 580 | if ($var) { | ||
| 581 | my ($v,$i) = split(/=/, $var); | ||
| 582 | $v =~ tr/+/ /; | ||
| 583 | $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 584 | if ($i) { | ||
| 585 | $i =~ tr/+/ /; | ||
| 586 | $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 587 | $i =~ s/<!--(.|\n)*-->//g; | ||
| 588 | $ArrayQuery{$v}=$i; | ||
| 589 | } | ||
| 590 | } | ||
| 591 | } | ||
| 592 | if (($ArrayQuery{amf}) && $restmode eq 'true') { | ||
| 593 | $user_agent=lc($ArrayQuery{amf}); | ||
| 594 | } | ||
| 595 | if ($ArrayQuery{$mobilenable}) { | ||
| 596 | $f->err_headers_out->set('Set-Cookie' => "amfFull=false; path=/;"); | ||
| 597 | $amfFull='ok'; | ||
| 598 | } | ||
| 599 | |||
| 600 | } | ||
| 601 | $user_agent=lc($user_agent); | ||
| 602 | if ($user_agent =~ m/blackberry/i) { | ||
| 603 | $user_agent=substr($user_agent,index($user_agent,'blackberry')); | ||
| 604 | $mobile=1; | ||
| 605 | } | ||
| 606 | if ($user_agent =~ m/up.link/i ) { | ||
| 607 | $user_agent=substr($user_agent,0,index($user_agent,'up.link') - 1); | ||
| 608 | $mobile=1; | ||
| 609 | } | ||
| 610 | ($user_agent,$version)=$CommonLib->androidDetection($user_agent); | ||
| 611 | |||
| 612 | if ($cacheSystem->restore( 'DetectRight-ua', $user_agent )) { | ||
| 613 | # | ||
| 614 | # cookie is not empty so I try to read in memory cache on my httpd cache | ||
| 615 | # | ||
| 616 | $id=$cacheSystem->restore( 'DetectRight-ua', $user_agent ); | ||
| 617 | if ($cacheSystem->restore( 'DetectRight-id', $id )) { | ||
| 618 | # | ||
| 619 | # I'm here only for old device | ||
| 620 | # | ||
| 621 | my @pairs = split(/&/, $cacheSystem->restore( 'DetectRight-id', $id )); | ||
| 622 | my $param_tofound; | ||
| 623 | my $string_tofound; | ||
| 624 | foreach $param_tofound (@pairs) { | ||
| 625 | ($string_tofound,$dummy)=split(/=/, $param_tofound); | ||
| 626 | if ($dummy) { | ||
| 627 | $ArrayCapFound{$string_tofound}=$dummy; | ||
| 628 | my $upper2=uc($string_tofound); | ||
| 629 | $f->subprocess_env("AMF_$upper2" => $ArrayCapFound{$string_tofound}); | ||
| 630 | $f->pnotes($string_tofound => $ArrayCapFound{$string_tofound}); | ||
| 631 | } | ||
| 632 | } | ||
| 633 | $id=$ArrayCapFound{id}; | ||
| 634 | if ($ArrayCapFound{realpcbrowser} ne 'none') { | ||
| 635 | $ArrayCapFound{'is_wireless_device'}='false'; | ||
| 636 | $ArrayCapFound{'device_claims_web_support'}='true'; | ||
| 637 | } | ||
| 638 | } | ||
| 639 | } else { | ||
| 640 | if ($id eq "") { | ||
| 641 | if ($user_agent) { | ||
| 642 | my $pair; | ||
| 643 | my $lcuser_agent=lc($user_agent); | ||
| 644 | if ($mobile==0) { | ||
| 645 | foreach $pair (%MobileArray) { | ||
| 646 | if ($user_agent =~ m/$pair/i) { | ||
| 647 | $mobile=1; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | if ($mobile==0) { | ||
| 651 | $user_agent=$CommonLib->botDetection($user_agent); | ||
| 652 | $realPCbrowser=IdentifyPCUAMethod($user_agent); | ||
| 653 | if ($realPCbrowser ne "none") { | ||
| 654 | $id="generic_web_browser"; | ||
| 655 | $mobile=-1; | ||
| 656 | } | ||
| 657 | } | ||
| 658 | } | ||
| 659 | if (!$id) {$id="";}; | ||
| 660 | if ($id eq "") { | ||
| 661 | $id=IdentifyUAMethod($user_agent); | ||
| 662 | } | ||
| 663 | if ($id eq "") { | ||
| 664 | $id='generic_web_browser'; | ||
| 665 | } else { | ||
| 666 | #this check the correct version of Android | ||
| 667 | if ($version) { | ||
| 668 | if ($version ne 'nc') { | ||
| 669 | my $lengthId=length($version); | ||
| 670 | my $count=0; | ||
| 671 | while($count<$lengthId) { | ||
| 672 | my $idToCheck=$id."_sub".substr($version,0,length($version)-$count); | ||
| 673 | if ($Array_fb{$idToCheck}) { | ||
| 674 | $id=$idToCheck; | ||
| 675 | $count=$lengthId; | ||
| 676 | } | ||
| 677 | $count++; | ||
| 678 | } | ||
| 679 | } | ||
| 680 | } | ||
| 681 | } | ||
| 682 | $cacheSystem->store( 'DetectRight-ua', $user_agent, $id); | ||
| 683 | } | ||
| 684 | } | ||
| 685 | if ($id ne "") { | ||
| 686 | # | ||
| 687 | # device detected | ||
| 688 | # | ||
| 689 | if ($realPCbrowser ne 'none') { | ||
| 690 | $id=$realPCbrowser; | ||
| 691 | } | ||
| 692 | if ($cacheSystem->restore( 'DetectRight-id', $id )) { | ||
| 693 | # | ||
| 694 | # I'm here only for old device looking in cache | ||
| 695 | # | ||
| 696 | my @pairs = split(/&/, $cacheSystem->restore( 'DetectRight-id', $id )); | ||
| 697 | my $param_tofound; | ||
| 698 | my $string_tofound; | ||
| 699 | foreach $param_tofound (@pairs) { | ||
| 700 | ($string_tofound,$dummy)=split(/=/, $param_tofound); | ||
| 701 | $ArrayCapFound{$string_tofound}=$dummy; | ||
| 702 | my $upper2=uc($string_tofound); | ||
| 703 | $f->subprocess_env("AMF_$upper2" => $ArrayCapFound{$string_tofound}); | ||
| 704 | $f->pnotes("$string_tofound" => $ArrayCapFound{$string_tofound}); | ||
| 705 | } | ||
| 706 | $id=$ArrayCapFound{id}; | ||
| 707 | |||
| 708 | |||
| 709 | } else { | ||
| 710 | if ($mobile == -1) { | ||
| 711 | %ArrayCapFound=FallBack('generic_web_browser'); | ||
| 712 | } else { | ||
| 713 | %ArrayCapFound=FallBack($id); | ||
| 714 | } | ||
| 715 | foreach $capability2 (sort keys %ArrayCapFound) { | ||
| 716 | $variabile2="$variabile2$capability2=$ArrayCapFound{$capability2}&"; | ||
| 717 | my $upper=uc($capability2); | ||
| 718 | $f->subprocess_env("AMF_$upper" => $ArrayCapFound{$capability2}); | ||
| 719 | $f->pnotes("$capability2" => $ArrayCapFound{$capability2}); | ||
| 720 | } | ||
| 721 | if ($realPCbrowser ne 'none') { | ||
| 722 | $id=$realPCbrowser; | ||
| 723 | $ArrayCapFound{'is_wireless_device'}='false'; | ||
| 724 | $ArrayCapFound{'device_claims_web_support'}='true'; | ||
| 725 | } | ||
| 726 | |||
| 727 | $variabile2="id=$id&$variabile2&realpcbrowser=$realPCbrowser"; | ||
| 728 | $f->subprocess_env("AMF_ID" => $id); | ||
| 729 | $f->pnotes('id' => $id); | ||
| 730 | $cacheSystem->store( 'DetectRight-id', $id, $variabile2 ); | ||
| 731 | $cacheSystem->store( 'DetectRight-ua', $user_agent, $id); | ||
| 732 | } | ||
| 733 | if ($cookiecachesystem eq "true") { | ||
| 734 | $f->err_headers_out->set('Set-Cookie' => "amfID=$id; path=/;"); | ||
| 735 | } | ||
| 736 | } | ||
| 737 | } | ||
| 738 | my $amf_device_ismobile = 'true'; | ||
| 739 | if ($ArrayCapFound{'device_claims_web_support'} eq 'true' && $ArrayCapFound{'is_wireless_device'} eq 'false') { | ||
| 740 | $amf_device_ismobile = 'false'; | ||
| 741 | } | ||
| 742 | if ($ArrayCapFound{'is_tablet'}) { | ||
| 743 | $f->subprocess_env("AMF_DEVICE_IS_TABLET" => lc($ArrayCapFound{'is_tablet'})); | ||
| 744 | } | ||
| 745 | |||
| 746 | if ($amfFull ne "") { | ||
| 747 | $f->subprocess_env("AMF_FORCE_TO_DESKTOP" => 'true'); | ||
| 748 | $f->pnotes("amf_force_to_desktop" => 'true'); | ||
| 749 | } | ||
| 750 | $f->pnotes("amf_device_ismobile" => $amf_device_ismobile); | ||
| 751 | $f->subprocess_env("AMF_DEVICE_IS_MOBILE" => $amf_device_ismobile); | ||
| 752 | $f->subprocess_env("AMF_VER" => $VERSION); | ||
| 753 | $f->subprocess_env("AMF_DETECT_RIGHT_VER" => $DetectRightVersion); | ||
| 754 | $f->headers_out->set("AMF-Ver"=> $VERSION); | ||
| 755 | if ($x_operamini_ua) { | ||
| 756 | $f->subprocess_env("AMF_MOBILE_BROWSER" => $x_operamini_ua); | ||
| 757 | $f->pnotes("mobile_browser" => $x_operamini_ua); | ||
| 758 | $f->subprocess_env("AMF_IS_TRANCODER" => 'true'); | ||
| 759 | $f->pnotes("is_transcoder" => 'true'); | ||
| 760 | } | ||
| 761 | return Apache2::Const::DECLINED; | ||
| 762 | } | ||
| 763 | 1; | ||
| 764 | |||
| 765 | |||
| 766 | =head1 NAME | ||
| 767 | |||
| 768 | Apache2::AMFDetectRightFilter - The module detects the mobile device and passes the DetectRight capabilities on to the other web application as environment variables | ||
| 769 | |||
| 770 | =head1 DESCRIPTION | ||
| 771 | |||
| 772 | Module for device detection, the cache is based on file system | ||
| 773 | |||
| 774 | =head1 AMF PROJECT SITE | ||
| 775 | |||
| 776 | http://www.apachemobilefilter.org | ||
| 777 | |||
| 778 | =head1 DOCUMENTATION | ||
| 779 | |||
| 780 | http://wiki.apachemobilefilter.org | ||
| 781 | |||
| 782 | Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFDetectRightFilter | ||
| 783 | |||
| 784 | =head1 AUTHOR | ||
| 785 | |||
| 786 | Idel Fuschini (idel.fuschini [at] gmail [dot] com) | ||
| 787 | |||
| 788 | =head1 COPYRIGHT | ||
| 789 | |||
| 790 | You may distribute under the terms of either the GNU General Public | ||
| 791 | License or the Artistic License, as specified in the Perl README file. | ||
| 792 | |||
| 793 | =cut | ||
| 794 |