AMFDeviceMonitor.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 180 CC 16 SCAR 51.2
CC
16
Cov
0%
handler
272.0
BEGIN
2.0
BEGIN
2.0
CRAP 168.2
1 #file:Apache2/AMFDeviceMonitor.pm;
2 #--------------------------------
3  
4 #
5 # Created by Idel Fuschini
6 # Date: 15/12/09
7 # Site: http://www.idelfuschini.it
8 # Mail: idel.fuschini@gmail.com
9  
10 package Apache2::AMFDeviceMonitor;
11
12 use strict;
13 use warnings;
14 use Apache2::AMFCommonLib ();
15
16 use Apache2::Filter ();
17 use Apache2::RequestRec ();
18 use APR::Table ();
19 use Cache::FileBackend;
20
21 use Apache2::Const -compile => qw(OK);
22
23 use constant BUFF_LEN => 1024;
24 use vars qw($VERSION);
25 $VERSION= "2.21";
26 #
27 # Define the global environment
28 #
29 my $CommonLib = new Apache2::AMFCommonLib ();
30 my $cachedirectorystore="notdefined";
31  
32 $CommonLib->printLog("---------------------------------------------------------------------------");
33 $CommonLib->printLog("AMFDeviceMonitor Version $VERSION");
34
35 if ($ENV{CacheDirectoryStore}) {
36 $cachedirectorystore=$ENV{CacheDirectoryStore};
37 $CommonLib->printLog("CacheDirectoryStore is: $cachedirectorystore");
38 } else {
39 $CommonLib->printLog("CacheDirectoryStore not exist. Please set the variable CacheDirectoryStore into httpd.conf, (the directory must be writeable)");
40 ModPerl::Util::exit();
41 }
42 my $cacheSystem = new Cache::FileBackend( $cachedirectorystore, 3, 000 );
43  
44  
45 sub handler {
46 my $f = shift;
47 my $query_string=$f->r->args;
48 my $id;
49 my $ua;
50 my $capab;
51 my %ArrayQuery;
52 my %ArrayForSort;
53 $ArrayQuery{page}=0;
54 if ($query_string) {
55 my @vars = split(/&/, $query_string);
56 foreach my $var (sort @vars){
57 if ($var) {
58 my ($v,$i) = split(/=/, $var);
59 $v =~ tr/+/ /;
60 $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
61 $i =~ tr/+/ /;
62 $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
63 $i =~ s/<!--(.|\n)*-->//g;
64 $ArrayQuery{$v}=$i;
65 }
66 }
67 }
68  
69 unless ($f->ctx) {
70 $f->r->headers_out->unset('Content-Length');
71 $f->ctx(1);
72 }
73  
74 $f->r->content_type('text/html');
75 $f->print("<title>Apache Mobile Filter - Device Monitor System V$VERSION</title>");
76 $f->print('<style type="text/css">body {font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;color: #4f6b72;background: #E6EAE9}a {color: #c75f3e}caption {padding: 0 0 5px 0;width: 700px; font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;text-align: right;}th {font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;color: #4f6b72;border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;border-top: 1px solid #C1DAD7;letter-spacing: 2px;text-transform: uppercase;text-align: left;padding: 6px 6px 6px 12px;background: #CAE8EA url(images/bg_header.jpg) no-repeat;}th.nobg {border-top: 0;border-left: 0;border-right: 1px solid #C1DAD7;background: none;}td {border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;background: #fff;padding: 6px 6px 6px 12px;color: #4f6b72;}td.alt {background: #F5FAFA;color: #797268;}th.spec {border-left: 1px solid #C1DAD7;border-top: 0;background: #fff url(images/bullet1.gif) no-repeat;font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;}th.specalt {border-left: 1px solid #C1DAD7;border-top: 0;background: #f5fafa url(images/bullet2.gif) no-repeat;font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;color: #797268;}</style>');
77 $f->print("<b>Apache Mobile Filter</b><br>Device Monitor System V$VERSION<HR><a href=\"?\">home</a>&nbsp;|&nbsp;<a href=\"?form=1\">detected devices</a>&nbsp;|&nbsp;<a href=\"?form=2\">devices not found</a><hr>");
78 my $dummy="null";
79 my $count=0;
80 my $page_number=30;
81 my $min=$ArrayQuery{page};
82 my $max=$ArrayQuery{page} + $page_number;
83 my $back=$ArrayQuery{page}-$page_number;
84 my $forward=$ArrayQuery{page}+$page_number;
85
86 if ($ArrayQuery{form}) {
87 if ($ArrayQuery{form} eq "3") {
88 my @pairs = split(/&/, $cacheSystem->restore( 'wurfl-id', $ArrayQuery{deviceid}));
89 my $param_tofound;
90 my $string_tofound;
91 $f->print("<table><tr><td>Parameter</td><td>Value</td></tr>");
92 foreach $param_tofound (@pairs) {
93 ($string_tofound,$dummy)=split(/=/, $param_tofound);
94 $f->print("<tr><td>$string_tofound</td><td>$dummy</td></tr>");
95 }
96 $f->print("</table>");
97 } else {
98 if ($ArrayQuery{form} eq "1") {
99 $f->print('<form action="" method=get>Search device id <input type="text" name="search"><input type="hidden" name="form" value="1"><input type=submit></form>');
100 }
101 $f->print("<table>");
102 $f->print("<tr><td>n.</td><td>device id</td><td>User Agent</td></tr>");
103 foreach $ua ( sort $cacheSystem->get_keys( 'wurfl-ua' ) )
104 {
105 $id =$cacheSystem->restore( 'wurfl-ua', $ua );
106 if ($ArrayQuery{form} eq "2" && $id eq 'device_not_found'){
107 $count++;
108 if ($count > $min - 1 && $count < $max + 1) {
109
110 $f->print("<tr><td>$count</td><td>$id</td><td>$ua</td></tr>");
111 }
112  
113 }
114 if ($ArrayQuery{form} eq "1" && $id ne 'device_not_found') {
115 if ($ArrayQuery{search}) {
116 if ($id =~ m/$ArrayQuery{search}/i) {
117 $count++;
118 if ($count > $min && $count < $max + 1) {
119 $f->print("<tr><td>$count</td><td><a href=\"?form=3&deviceid=$id\">$id</a></td><td>$ua</td></tr>");
120 }
121
122 }
123 } else {
124 $count++;
125 if ($count > $min - 1 && $count < $max + 1) {
126 $f->print("<tr><td>$count</td><td><a href=\"?form=3&deviceid=$id\">$id</a></td><td>$ua</td></tr>");
127 }
128 }
129
130 }
131 }
132 }
133 $f->print("</table><center><table><tr>");
134
135 if ( $min > 0) {
136 $f->print("<td><a href=\"?form=$ArrayQuery{form}&page=$back\">back</a></td>");
137 }
138 if ($forward < $count) {
139 $f->print("<td><a href=\"?form=$ArrayQuery{form}&page=$forward\">forward></a></td>");
140 }
141 $f->print("</tr></table></center>");
142 } else {
143 $f->print('<br><br><br><br><center><table><tr><td><H1>Apache Mobile Filter</H1>Open Source Project: <a href="http://www.idelfuschini.it/en/apache-mobile-filter-v2x.html">http://www.idelfuschini.it/en/apache-mobile-filter-v2x.html</a></td></tr></table>');
144 }
145  
146 return Apache2::Const::OK;
147 }
148 1;
149 =head1 NAME
150  
151 Apache2::AMFDeviceMonitor - This module is an admin tool to control the devices access that Apache Mobile Filter has detected.
152  
153  
154 =head1 COREQUISITES
155  
156 Apache2::Filter ();
157  
158 Apache2::RequestRec ();
159  
160 APR::Table ();
161  
162 Cache::FileBackend;
163
164  
165  
166 =head1 DESCRIPTION
167  
168 This module is an admin tool to control the devices access that Apache Mobile Filter has detected.
169  
170  
171 For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html
172  
173 An example of how to set the httpd.conf is below:
174  
175 =over 4
176  
177 =item C<PerlSetEnv CacheDirectoryStore /tmp>
178  
179  
180 =back
181  
182 NOTE: this software need carrier-data.txt you can download it directly from this site: http://www.andymoore.info/carrier-data.txt or you can set the filter to download it directly.
183  
184 =head1 SEE ALSO
185  
186 For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html
187  
188 Mobile Demo page of the filter: http://apachemobilefilter.nogoogle.it (thanks Ivan alias sigmund)
189  
190 Demo page of the filter: http://apachemobilefilter.nogoogle.it/php_test.php (thanks Ivan alias sigmund)
191  
192 =head1 AUTHOR
193  
194 Idel Fuschini (idel.fuschini [at] gmail [dot] com)
195  
196 =cut