AMFSwitcher.pm untested

filter: Statementstmt 0.0% 0 / 212 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 337 CC 46 SCAR 62.0
CC
46
Cov
0%
handler
812.0
loadConfigFile
380.0
BEGIN
2.0
CRAP 491.5
1 #file:Apache2/AMFSwitcher.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  
11  
12 package Apache2::AMFSwitcher;
13
14 use strict;
15 use warnings;
16 use Apache2::AMFCommonLib ();
17
18 use Apache2::RequestRec ();
19 use Apache2::RequestUtil ();
20 use Apache2::SubRequest ();
21 use Apache2::Log;
22 use Apache2::Filter ();
23 use APR::Table ();
24 use LWP::Simple;
25 use Apache2::Const -compile => qw(REDIRECT DECLINED HTTP_TEMPORARY_REDIRECT HTTP_MOVED_PERMANENTLY HTTP_MOVED_TEMPORARILY HTTP_SEE_OTHER HTTP_NOT_MODIFIED HTTP_USE_PROXY);
26 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
27 use constant BUFF_LEN => 1024;
28 use vars qw($VERSION);
29 $VERSION= "4.33";;;
30 #
31 # Define the global environment
32 #
33 my $CommonLib = new Apache2::AMFCommonLib ();
34 my $mobileversionurl="none";
35 my $fullbrowserurl="none";
36 my $redirecttranscoderurl="none";
37 my $redirecttranscoder="false";
38 my $wildcardredirect="false";
39 my $mobileversionurl_ck="/";
40 my $fullbrowserurl_ck="/";
41 my $redirecttranscoderurl_ck="/";
42 my @IncludeString;
43 my @ExcludeString;
44 my $mobilenable="false";
45 my $mobileDomain="none";
46 my $fullbrowserDomain="none";
47 my $transcoderDomain="none";
48 my $forcetablet="false";
49 my $return_http_switch=Apache2::Const::REDIRECT;
50
51 my %ArrayPath;
52 $ArrayPath{1}='none';
53 $ArrayPath{2}='none';
54 $ArrayPath{3}='none';
55 $CommonLib->printLog("---------------------------------------------------------------------------");
56 $CommonLib->printLog("AMFSwitcher Version $VERSION");
57 if ($ENV{AMFMobileHome}) {
58 } else {
59 $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf");
60 $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated");
61 ModPerl::Util::exit();
62 }
63 $CommonLib->printLog("If you use AMFWURFLFilter is better to use WebPatch LoadWebPatch not exist.");
64 $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated");
65 &loadConfigFile();
66 sub loadConfigFile {
67 my $null="";
68 my $null2="";
69 my $null3="";
70 my $val;
71 my $capability;
72 my $r_id;
73 my $dummy;
74 $CommonLib->printLog("AMFSwitcher: Start read configuration from httpd.conf");
75 if ($ENV{TypeRedirect}) {
76 if ($ENV{TypeRedirect} eq "301") {
77 $return_http_switch=Apache2::Const::HTTP_MOVED_PERMANENTLY;
78 } elsif ($ENV{TypeRedirect} eq "302") {
79 $return_http_switch=Apache2::Const::HTTP_MOVED_TEMPORARILY;
80 } elsif ($ENV{TypeRedirect} eq "303") {
81 $return_http_switch=Apache2::Const::HTTP_SEE_OTHER;
82 } elsif ($ENV{TypeRedirect} eq "304") {
83 $return_http_switch=Apache2::Const::HTTP_NOT_MODIFIED;
84 } elsif ($ENV{TypeRedirect} eq "305") {
85 $return_http_switch=Apache2::Const::HTTP_USE_PROXY;
86 } elsif ($ENV{TypeRedirect} eq "307") {
87 $return_http_switch=Apache2::Const::HTTP_TEMPORARY_REDIRECT;
88 }
89 }
90  
91 if ($ENV{FullBrowserUrl}) {
92 $fullbrowserurl=$ENV{FullBrowserUrl};
93 $ArrayPath{2}=$ENV{FullBrowserUrl};
94 $CommonLib->printLog("FullBrowserUrl is: $fullbrowserurl");
95 $fullbrowserurl_ck=$ENV{FullBrowserUrl};
96 if (substr ($fullbrowserurl,0,5) eq "http:") {
97 my ($dummy,$dummy2,$url_domain,$dummy3)=split(/\//, $fullbrowserurl);
98 $fullbrowserDomain=$url_domain;
99
100 }
101 }
102 if ($ENV{RedirectTranscoderUrl}) {
103 $redirecttranscoderurl=$ENV{RedirectTranscoderUrl};
104 $ArrayPath{3}=$ENV{RedirectTranscoderUrl};
105 $redirecttranscoder="true";
106 $redirecttranscoderurl_ck=$ENV{RedirectTranscoderUrl};
107 $CommonLib->printLog("RedirectTranscoderUrl is: $redirecttranscoderurl");
108 if (substr ($redirecttranscoderurl,0,5) eq "http:") {
109 my ($dummy,$dummy2,$url_domain,$dummy3)=split(/\//, $redirecttranscoderurl);
110 $transcoderDomain=$url_domain;
111
112 }
113 }
114 if ($ENV{"AMFSwitcherExclude"}){
115 @ExcludeString=split(/,/, $ENV{AMFSwitcherExclude});
116 $CommonLib->printLog("SwitcherExclude is: $ENV{AMFSwitcherExclude}");
117 }
118 if ($ENV{WildCardRedirect}) {
119 if ($ENV{WildCardRedirect} eq 'true') {
120 $wildcardredirect="true";
121 } else {
122 $wildcardredirect="false";
123 }
124 $CommonLib->printLog("WildCardRedirect is: $wildcardredirect");
125 }
126 if ($ENV{ForceTabletAsFullBrowser}) {
127 if ($ENV{ForceTabletAsFullBrowser} eq 'true') {
128 $forcetablet="true";
129 } else {
130 $forcetablet="false";
131 }
132 $CommonLib->printLog("ForceTabletAsFullBrowser is: $forcetablet");
133 }
134 if ($ENV{MobileVersionUrl}) {
135 $mobileversionurl=$ENV{MobileVersionUrl};
136 $ArrayPath{1}=$ENV{MobileVersionUrl};
137 $CommonLib->printLog("MobileVersionUrl is: $mobileversionurl");
138 $mobileversionurl_ck=$ENV{MobileVersionUrl};
139 push(@ExcludeString,$ENV{MobileVersionUrl});
140 if (substr ($mobileversionurl,0,5) eq "http:") {
141 my ($dummy,$dummy2,$url_domain,$dummy3)=split(/\//, $mobileversionurl);
142 $mobileDomain=$url_domain;
143
144 }
145 }
146 $CommonLib->printLog("Finish loading parameter");
147 }
148 sub handler {
149 my $f = shift;
150 my $capability2;
151 my $query_string=$f->args;
152 my $device_claims_web_support="null";
153 my $is_wireless_device="null";
154 my $is_transcoder="null";
155 my $location="none";
156 my $return_value=Apache2::Const::DECLINED;
157 my $device_type=1;
158 my $no_redirect=1;
159 my $uri=$f->unparsed_uri();
160 my $servername=$f->get_server_name();
161 my $uriAppend="";
162 my $filter="true";
163 my %ArrayQuery;
164 my $isTablet="null";
165 my $amf_device_ismobile = "true";
166 my $amf_force_to_mobile = "false";
167  
168 if ($query_string) {
169 my @vars = split(/&/, $query_string);
170 foreach my $var (sort @vars){
171 if ($var) {
172 my ($v,$i) = split(/=/, $var);
173 $v =~ tr/+/ /;
174 $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
175 if ($i) {
176 $i =~ tr/+/ /;
177 $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
178 $i =~ s/<!--(.|\n)*-->//g;
179 }
180 $ArrayQuery{$v}="ok";
181 }
182 }
183 }
184 if ($f->pnotes('is_tablet')) {
185 $isTablet=$f->pnotes('is_tablet')
186 }
187 if ($f->pnotes('is_transcoder')) {
188 $is_transcoder=$f->pnotes('is_transcoder');
189 }
190 if ($f->pnotes('amf_force_to_desktop')) {
191 $amf_force_to_mobile=$f->pnotes('amf_force_to_desktop');
192 }
193 if ($f->pnotes('amf_device_ismobile')) {
194 $amf_device_ismobile=$f->pnotes('amf_device_ismobile');
195 }
196 foreach my $string (@ExcludeString) {
197 if (index($uri,$string) > -1) {
198 $filter="false";
199 }
200 }
201 if ($filter eq "true"){
202 if ($amf_device_ismobile eq 'false'|| ($isTablet eq "true" && $forcetablet eq "true")) {
203 if ($fullbrowserDomain ne $servername) {
204 if ($fullbrowserurl ne 'none') {
205 if ($wildcardredirect eq 'true'){
206 $location=$uri;
207 if ($location =~ /$mobileversionurl_ck/o) {
208 $location =~ s/$mobileversionurl_ck/$fullbrowserurl/;
209 } else {
210 $location = $fullbrowserurl;
211 }
212 } else {
213 $location = $fullbrowserurl;
214 }
215 }
216 $device_type=2;
217 }
218 } else {
219 if ($mobileDomain ne $servername) {
220 if ($wildcardredirect eq 'true'){
221 $location=$uri;
222 if ($location =~ /$fullbrowserurl_ck/o) {
223 $location =~ s/$fullbrowserurl_ck/$mobileversionurl/;
224 } else {
225 $location = $mobileversionurl;
226 }
227 } else {
228 $location = $mobileversionurl;
229 }
230 } else {
231 if (substr ($mobileversionurl,0,4) eq "http") {
232 $location = $mobileversionurl;
233 }
234 }
235 $device_type=1;
236 }
237 if ($is_transcoder eq 'true') {
238 if ($transcoderDomain ne $servername) {
239 if ($redirecttranscoderurl ne 'none') {
240 if ($wildcardredirect eq 'true'){
241 $location=$uri;
242 if ($location =~ /$fullbrowserurl_ck/o) {
243 $location =~ s/$fullbrowserurl_ck/$redirecttranscoderurl/;
244 } else {
245 $location = $redirecttranscoderurl;
246 }
247 }
248 }
249 $device_type=3;
250 }
251 }
252  
253 if ($ArrayPath{$device_type} eq substr($uri,0,length($ArrayPath{$device_type}))) {
254 $no_redirect=0;
255 }
256 if ($location ne "none" && $amf_force_to_mobile eq 'false') {
257 if (substr ($location,0,4) eq "http") {
258 $f->headers_out->set(Location => $location);
259 $f->status($return_http_switch);
260 $return_value=$return_http_switch;
261 } else {
262 if ($no_redirect==1) {
263 $f->headers_out->set(Location => $location);
264 $f->status($return_http_switch);
265 $return_value=$return_http_switch;
266 }
267 }
268 }
269
270 }
271 return $return_value;
272 }
273  
274 1;
275  
276  
277 =head1 NAME
278  
279 Apache2::AMFSwitcher - Used to switch the device to the apropriate content (mobile, fullbrowser or for transcoder)
280  
281  
282 =head1 DESCRIPTION
283  
284 This module has the scope to manage with WURFLFilter.pm module the group of device (MobileDevice, PC and transcoder).
285  
286 =head1 AMF PROJECT SITE
287  
288 http://www.apachemobilefilter.org
289  
290 =head1 DOCUMENTATION
291  
292 http://wiki.apachemobilefilter.org
293  
294 Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFSwitcher
295  
296 =head1 AUTHOR
297  
298 Idel Fuschini (idel.fuschini [at] gmail [dot] com)
299  
300 =head1 COPYRIGHT
301  
302 You may distribute under the terms of either the GNU General Public
303 License or the Artistic License, as specified in the Perl README file.
304  
305 =cut