Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Better forms support, iframe/frame support, port agnosticism

  • Loading branch information...
commit b6b1c8f2bc45ed3a955af45ccf897ce9c46b39dd 1 parent 223a54f
@joshdick authored
Showing with 38 additions and 10 deletions.
  1. +38 −10 miniProxy.php
View
48 miniProxy.php
@@ -2,7 +2,8 @@
//miniProxy - a barebones web proxy written in PHP. <https://github.com/joshdick/miniProxy>
-define("PROXY_PREFIX", "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . "?url=");
+define("URL_PARAM", "___mp_url");
+define("PROXY_PREFIX", "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "?" . URL_PARAM . "=");
//Retrieves contents and HTTP headers of a URL with cURL.
function getFile($fileLoc)
@@ -13,20 +14,29 @@ function getFile($fileLoc)
if (empty($user_agent)) {
$user_agent = "Mozilla/5.0 (compatible; miniProxy)";
}
- $ch = curl_init($fileLoc);
+ $ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_FAILONERROR, true);
- if ($_POST) { //If data was POSTed to the proxy, re-POST the data to the requested URL
- $postData = array();
- foreach ($_POST as $key => $value) {
- $postData[] = $key . "=" . $value;
- }
+ //If data was POSTed to the proxy, re-POST the data to the requested URL
+ $postData = array();
+ foreach ($_POST as $key => $value) {
+ $postData[] = $key . "=" . $value;
+ }
+ if (count($postData) > 0) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode("&", $postData));
}
+ //If data was GETed to the proxy, re-GET the data to the requested URL
+ $getData = array();
+ foreach ($_GET as $key => $value) {
+ if ($key == URL_PARAM) continue; //Strip out any data added by proxifyGETForms()
+ $getData[] = $key . "=" . $value;
+ }
+ if (count($getData) > 0) $fileLoc .= "?" . implode("&", $getData);
+ curl_setopt($ch, CURLOPT_URL, $fileLoc);
$data = curl_exec($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
@@ -78,7 +88,24 @@ function proxifyTags(&$doc, $baseURL, $tags, $attrName) {
}
}
-$url = empty($_GET['url']) ? null : $_GET['url'];
+//Rewrite GET forms so that their actions point back to the proxy, and add a hidden form field containing the original action.
+function proxifyGETForms(&$doc, $baseURL) {
+ foreach($doc->getElementsByTagName("form") as $form) {
+ $method = $form->getAttribute("method");
+ if (empty($method) || strtolower($method) != "post") { //Only modify GET forms
+ $action = rel2abs($form->getAttribute("action"), $baseURL); //Store an absolute version of the form action
+ $form->setAttribute("action", ""); //Wipe out the action, forcing The form to submit back to the proxy
+ //Add a hidden form field containing the original form action
+ $proxyField = $doc->createElement("input");
+ $proxyField->setAttribute("type", "hidden");
+ $proxyField->setAttribute("name", URL_PARAM);
+ $proxyField->setAttribute("value", $action);
+ $form->appendChild($proxyField);
+ }
+ }
+}
+
+$url = empty($_GET[URL_PARAM]) ? null : $_GET[URL_PARAM];
if (empty($url)) die("No URL was specified.<br /><br />miniProxy should be invoked like this:<br /><br /><a href=\"" . PROXY_PREFIX . "http://en.wikipedia.org/\">" . PROXY_PREFIX . "http://en.wikipedia.org/");
if (!preg_match("@^.*://@", $url)) $url = "http://" . $url; //Assume that any supplied URLS without a scheme are HTTP URLs.
@@ -88,9 +115,10 @@ function proxifyTags(&$doc, $baseURL, $tags, $attrName) {
if ($isHTML) { //This is a web page, so modify the DOM to make things point back to the proxy
$doc = new DomDocument();
@$doc->loadHTML($file["data"]);
+ proxifyGETForms($doc, $url);
proxifyTags($doc, $url, array("a", "link"), "href");
- proxifyTags($doc, $url, array("img", "script"), "src");
- proxifyTags($doc, $url, array("form"), "action");
+ proxifyTags($doc, $url, array("img", "script", "iframe", "frame"), "src");
+ proxifyTags($doc, $url, array("form"), "action"); //This will only affect POST forms; GET form actions were blanked by proxifyGETForms() above
echo "<!-- Proxified page constructed by miniProxy -->\n" . $doc->saveHTML();
} else { //This isn't a web page, so serve unmodified through the proxy with the correct headers (images, CSS, JavaScript, etc.)
header("Content-Type: " . $file["contentType"]);
Please sign in to comment.
Something went wrong with that request. Please try again.