Permalink
Browse files

Updated comments

  • Loading branch information...
1 parent 2d4a670 commit 8bc3f4e2bcb6af2d4dfad86777abf42304930f54 @inspectorfegter committed Mar 8, 2012
Showing with 64 additions and 6 deletions.
  1. +64 −6 proxy-builder.php
View
@@ -8,56 +8,115 @@
Author URI: http://coderrr.com
License: GPLv2
*/
+
+/** Proxy Builder Class
+ * This class allows you to commandere a URI segment and turn it into a full-fleged HTTP proxy
+*/
class ProxyBuilder{
protected $proxies = array();
protected $host;
+ /** Instantiate Object
+ * @param array $proxies Key/Value pair of URI segment => Proxy URL. Multiple proxies may be passed.
+ * @return void
+ */
function __construct($proxies){
$this->proxies = $proxies;
$this->host = $_SERVER['SERVER_NAME'];
+ $this->register_hooks();
+ }
+
+ /** Register WP Actions and Filters
+ * @return void
+ */
+ protected function register_hooks(){
if(!is_admin())
add_action('init', array(&$this, 'add_proxy'), 0);
}
+ /** Check for the specified URI segments and add the proxy functionality
+ * @return void
+ */
public function add_proxy(){
- foreach($this->proxies as $uri => $url){
+
+ # Iterate through all supplied proxies
+ foreach($this->proxies as $uri => $url){
+
+ # Check for URI match at the begining of the request URI line
if(preg_match("~^/$uri~", $_SERVER['REQUEST_URI'])){
- $uri_parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
- $replace_url = "http://$this->host/$uri";
+
+ # Set an alternate content URL for possible modification
$content_url = $url;
+
+ # Generate an array of URI segments
+ $uri_parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
+
+ # If request is not the proxy root
if(count($uri_parts) > 1){
+
+ # Trash our URI segment since we already know it
unset($uri_parts[0]);
+
+ # Check for WWW requests if supplied proxy domain is not WWW
if($uri_parts[1] == 'www' && !preg_match('/www/', $replace_url)){
+
+ # Traxh the WWW segment as we don't need it anymore
unset($uri_parts[1]);
- $content_url .= '/'. implode('/', $uri_parts);
+
+ # Build the new content URL to retrieve
+ $content_url .= '/'. implode('/', $uri_parts);
+
+ # Inject WWW into the content URL
$content_url = str_replace('http://', 'http://www.', $content_url);
}
else
+ # Build the new content URL to retrieve
$content_url .= '/'. implode('/', $uri_parts);
}
+ # Check for a query string and append it to the content URL
$query = $_SERVER['QUERY_STRING'] ? '?'.esc_attr($_SERVER['QUERY_STRING']) : '';
$content_url .= $query;
+ # Retrieve the remote content
$response = wp_remote_get($content_url);
$content = wp_remote_retrieve_body($response);
+ # Create replacement URL to make our proxy legit when others view our source code
+ $replace_url = "http://$this->host/$uri";
+ $content = str_replace($url, $replace_url, $content);
+
+ # Create a WWW replacement URL in case of mismatch of URLs in the content
$www_url = str_replace('http://', 'http://www.', $url);
$content = str_replace($www_url, $replace_url.'/www', $content);
- $content = str_replace($url, $replace_url, $content);
+
+ # Add our proxy URL to IMG, A, LINK, SCRIPT, and FORM tags
$content = str_replace('src="/', 'src="'.$replace_url.'/', $content);
$content = str_replace('href="/', 'href="'.$replace_url.'/', $content);
$content = str_replace('action="/', 'action="'.$replace_url.'/', $content);
+ # Send file headers if retrieving single files
$this->set_file_headers($content_url);
+
+ # Send the response body to the browser
echo $content;
+
+ # Die WordPress, Die!
exit;
}
}
}
+ /** Set file headers for single files.
+ * @param string $content_url The URL from which to extract the file extension
+ * @return void
+ */
protected function set_file_headers($content_url){
+
+ # Find the file extension
$ext = substr(strrchr($content_url,'.'),1);
+
+ # Set the proper header
switch ($ext){
case 'jpg':
header('Content-Type: image/jpeg');
@@ -91,6 +150,5 @@ protected function set_file_headers($content_url){
break;
default:
}
-
}
}

0 comments on commit 8bc3f4e

Please sign in to comment.