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