annotate doc/www.powerfulproxy.com/public_html/do_it.php @ 501:4d73737f40ff draft

new version of powerfulproxy from atiti
author Nick <nick@somerandomnick.ano>
date Wed, 11 May 2011 05:11:58 +0000
parents 6da2470e59b5
children 60ece78f8b36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
1 <?php
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
2 // Copyright Atiti, 2011
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
3 // Version 0.1-2
501
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
4 // Heavily modified by Ivo <Ivo@UCIS.nl>
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
5
501
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
6 if (!isset($_SERVER['PATH_INFO'])) die('PATH_INFO is not set');
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
7 $pall = explode("/", $_SERVER['PATH_INFO']);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
8 if (count($pall) <= 1) die('Unexpected path format');
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
9 array_shift($pall);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
10 $proto = array_shift($pall);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
11 $host = array_shift($pall);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
12 $path = implode('/', $pall);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
13 array_pop($pall);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
14 $rp = implode('/', $pall);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
15
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
16 /* CONFIGURATION */
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
17 $SERVICEURL = "http://powerfulproxy.com/do_it.php/";
501
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
18
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
19 $REWRITE_CONTENT_TYPES = array('text/html', 'text/xml', 'text/plain');
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
20 $REWRITE_PATTERNS = array(
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
21 /* Rewrite complete http/https URLs, enable one of the tree, and no more! */
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
22 // '@(https?)://(([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@i' => $SERVICEURL.'$1/$2',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
23 // '@(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.'$3/$4$2',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
24 '@(<[^>]*)(src|href|action)\s*=\s*(\'|")(https?)://([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.'$4/$5$3',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
25 /* Rewrite URLs relative to site root, enable one of the tree, and no more! */
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
26 // '@(src|href|action)\s*=\s*(\'|")/([^\'"]*)\2@i' => '$1=$2'.$SERVICEURL.$proto.'/'.$host.'/$3$2',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
27 '@(<[^>]*)(src|href|action)\s*=\s*(\'|")/([^\'"]*)\3@i' => '$1$2=$3'.$SERVICEURL.$proto.'/'.$host.'/$4$3',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
28 );
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
29 $CURL_OPTIONS = array(
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
30 CURLOPT_USERAGENT => "AnoNet proxy",
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
31 CURLOPT_AUTOREFERER => TRUE,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
32 CURLOPT_CONNECTTIMEOUT => 15,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
33 CURLOPT_TIMEOUT => 28,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
34 CURLOPT_MAXREDIRS => 10,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
35 CURLOPT_FAILONERROR => FALSE,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
36 CURLOPT_HEADER => 1,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
37 CURLOPT_FOLLOWLOCATION => FALSE,
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
38 // CURLOPT_INTERFACE => '0.0.0.0',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
39 // CURLOPT_PROXY => "http://b.polipo.srn.ano:8000/",
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
40 // CURLOPT_PROXYUSERPWD => 'username:password',
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
41 );
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
42 /* END OF CONFIGURATION */
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
43
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
44 $url = $proto."://".$host."/".$path;
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
45 if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING'])) $url .= "?".$_SERVER['QUERY_STRING'];
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
46 $ch = curl_init($url);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
47 curl_setopt_array($ch, $CURL_OPTIONS);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
49 //curl_setopt($ch, CURLOPT_HEADER, FALSE);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
50 if (count($_POST)) {
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
51 curl_setopt($ch, CURLOPT_POST, TRUE);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
52 curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
53 }
501
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
54 $response = curl_exec($ch);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
55 list($header, $data) = explode("\r\n\r\n", $response, 2);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
56 if ($error = curl_error($ch)) die('CURL ERROR: '.$error);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
57 $info = curl_getinfo($ch);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
58
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
59 header('Status: '.$info['http_code']);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
60 header('Content-Type: '.$info['content_type']);
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
61
501
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
62 $redirurl = "";
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
63 if ($info['http_code'] === 301) {
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
64 $headers = explode("\r\n", $header);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
65 foreach($headers as $h) {
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
66 $cur_header = explode(": ", $h);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
67 if ($cur_header[0] == "Location") {
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
68 $redirurl = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $SERVICEURL.str_replace("http://", "http/", "$1"), $cur_header[1]);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
69 $redirurl = str_replace(".php/http://", ".php/http/", $redirurl);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
70 header('Location: '.$redirurl);
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
71 }
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
72 }
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
73 } else {
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
74 if (in_array(strtok($info['content_type'], ';'), $REWRITE_CONTENT_TYPES)) $data = preg_replace(array_keys($REWRITE_PATTERNS), array_values($REWRITE_PATTERNS), $data, -1, $count);
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
75 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
76
501
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
77 header('Content-Length: '.strlen($data));
4d73737f40ff new version of powerfulproxy from atiti
Nick <nick@somerandomnick.ano>
parents: 453
diff changeset
78 echo $data;
453
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
79 ?>