2 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
0 package Apache::AdBlocker;
1
2 use strict;
3 use vars qw(@ISA $VERSION);
4 use Apache::Constants qw(:common);
5 use GD ();
6 use Image::Size qw(imgsize);
7 use LWP::UserAgent ();
8
9 @ISA = qw(LWP::UserAgent);
10 $VERSION = '1.00';
11
12 my $UA = ->new;
13 $UA->agent(join "/", , $VERSION);
14 my $Ad = join "|", qw{ads? advertisements? banners? adv promotions?};
15
16 sub handler {
17 Â my($r) = @_;
18 Â return DECLINED unless $r->proxyreq;
19 Â $r->handler("perl-script"); # Okay, let&âs do it
20 Â $r->push_handlers(PerlHandler => \&proxy_handler);
21 Â return OK;
22 }
23
24 sub proxy_handler {
25 Â my ($r) = @_;
26
27 Â my $request = HTTP::Request->new($r->method => $r->uri);
28 Â my %headers_in = $r->headers_in;
29
30 Â while (my($key,$val) = each %headers_in) {
31 Â Â $request->header($key,$val);
32 Â }
33
34 Â if($r->method eq 'POST') {
35 Â Â my $len = $r->header_in('Content-length');
36 Â Â my $buf;
37 Â Â $r->read($buf, $len);
38 Â Â $request->content($buf);
39 Â }
40
41 Â my $response = $UA->request($request);
42 Â $r->content_type($response->header('Content-type'));
43
44 Â # Feed response back into our request
45 Â $r->status($response->code);
46 Â $r->status_line(join " ", $response->code, $response->message);
47 Â $response->scan(sub {
48 Â Â $r->header_out(@_);
49 Â });
50
51 Â $r->send_http_header();
52 Â my $content = \$response->content;
53
54 Â if ($r->content_type =~ /^image/ && $r->uri =~ /\b($Ad)\b/i) {
55 Â Â $r->content_type("image/gif");
56 Â Â block_ad($content);
57 Â }
58
59 Â $r->print($$content);
60
61 Â return OK;
62 }
63
64 sub block_ad {
65 Â my $data = shift;
66 Â my($x, $y) = imgsize($data);
67
68 Â my $im = GD::Image->new($x,$y);
69
70 Â my $white = $im->colorAllocate(255, 255, 255);
71 Â my $black = $im->colorAllocate(0, 0, 0 );
72 Â my $red = $im->colorAllocate(255, 0, 0 );
73
74 Â $im->transparent($white);
75 Â $im->string(GD::gdLargeFont(), 5, 5, "Blocked Ad", $red);
76 Â $im->rectangle(0, 0, $x-1, $y-1, $black);
77
78 Â $$data = $im->gif;
79 }
80
81 1;
82
83
2 Einträge, 1 Seite |