annotate doc/www.powerfulproxy.com/public_html/do_it.php @ 453:6da2470e59b5 draft

powerfulproxy
author Nick <nick@somerandomnick.ano>
date Mon, 28 Mar 2011 17:35:47 +0000
parents
children 4d73737f40ff
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
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
4
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
5 // Where are we at?
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
6 $SERVICEURL = "http://powerfulproxy.com/do_it.php/";
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
7 // Do da request
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
8 function get_url($url, $data) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
9 if (!$url) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
10 echo "Invalid use!";
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
11 die;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
12 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
13 $options = array(
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
14 CURLOPT_RETURNTRANSFER => true, // return web page
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
15 CURLOPT_HEADER => false, // don't return headers
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
16 CURLOPT_FOLLOWLOCATION => true, // follow redirects
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
17 CURLOPT_ENCODING => "", // handle all encodings
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
18 CURLOPT_USERAGENT => "AnoNet proxy", // who am i
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
19 CURLOPT_AUTOREFERER => true, // set referer on redirect
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
20 CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
21 CURLOPT_TIMEOUT => 28, // timeout on response
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
22 CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
23 CURLOPT_FAILONERROR => true,
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
24 // CURLOPT_PROXY => "http://b.polipo.srn.ano:8000/",
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
25 );
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
26 $ch = curl_init ($url);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
27 curl_setopt_array( $ch, $options );
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
28 $fields_string = "";
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
29 if (count($data)) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
30 foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
31 rtrim($fields_string,'&');
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
32 curl_setopt($ch, CURLOPT_POST, count($data));
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
33 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
34 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
35 $ret = curl_exec ($ch);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
36 if ($error = curl_error($ch))
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
37 echo 'ERROR: ',$error;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
38 $info = curl_getinfo($ch);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
39 return array("data"=>$ret,"info"=>$info);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
40 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
41 // Rewrite relative paths
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
42 function rewriteRelative($html, $base) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
43 $server = preg_replace('@^([^\:]*)://([^/*]*)(/|$).*@', '\1://\2/', $base);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
44 $html = preg_replace('@\<([^>]*) (href|src)="/([^"]*)"@i', '<\1 \2="' . $server . '\3"', $html);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
45 $html = preg_replace('@\<([^>]*) (href|src)="(([^\:"])*|([^"]*:[^/"].*))"@i', '<\1 \2="' . $base . '\3"', $html);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
46 return $html;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
47 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
48 if (isset($_SERVER["PATH_INFO"]))
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
49 $p = $_SERVER["PATH_INFO"];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
50 if (isset($_SERVER["QUERY_STRING"]))
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
51 $q = $_SERVER["QUERY_STRING"];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
52 $postdata = $_POST;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
53 $pall = explode("/", $p);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
54 if (count($pall) <= 1) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
55 echo "Wrong host format? or smtg.";
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
56 die;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
57 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
58 $proto = $pall[1];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
59 $host = $pall[2];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
60 unset($pall[0]);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
61 unset($pall[1]);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
62 unset($pall[2]);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
63 $path = implode("/", $pall);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
64 // Figure out relative paths
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
65 $pi = pathinfo($path);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
66 if ($pi) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
67 $rp = @$pi["dirname"];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
68 } else
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
69 $rp = "";
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
70 if (!$rp)
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
71 $rp = $path;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
72 // Construct request url
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
73 $geturl = $proto."://".$host."/".$path;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
74 if ($q)
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
75 $geturl .= "?".$q; // Append query string
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
76
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
77 $d = get_url($geturl, $postdata);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
78 $data = $d["data"];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
79 $ct = $d["info"]["content_type"];
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
80 $ct_s = explode(";", $ct);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
81 $found = false;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
82 $match_ct = array("text/html", "text/xml", "text/plain");
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
83 foreach($match_ct as $m) {
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
84 if ($ct_s[0] == $m)
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
85 $found = true;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
86 }
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
87 if ($found) { // Only rewrite for proper content
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
88 $ret = rewriteRelative($data, $proto."://".$host."/".$rp."/");
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
89 $ret = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $SERVICEURL.str_replace("http://", "http/", "$1"), $ret);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
90 $ret = str_replace(".php/http://", ".php/http/", $ret);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
91 $ret = str_replace(".php/https://", ".php/https/", $ret);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
92
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
93 $ret = str_replace("../", "", $ret);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
94 $items = Array("/src='\/(.*)'/", "/src=\"\/(.*)\"/", "/href='\/(.*)'/", "/href=\"\/(.*)\"/");
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
95 $ret = preg_replace($items, "src='".$SERVICEURL.$proto."/".$host."/$1'", $ret);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
96 $ret = preg_replace("/action=\"\/\"/i", "action=\"".$SERVICEURL.$proto."/".$host."/\"", $ret);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
97
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
98 } else
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
99 $ret = "";
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
100 // Output da shit
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
101 header("Content-Type: ".$ct);
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
102 if (strlen($ret) == 0)
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
103 echo $data;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
104 else
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
105 echo $ret;
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
106
6da2470e59b5 powerfulproxy
Nick <nick@somerandomnick.ano>
parents:
diff changeset
107 ?>