public static function run($config) { // detect request method and url $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; $path = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; if (isset($config['domain_url_start_path'])) { $path = $config['domain_url_start_path']; } // parse the proxy url, and grab the directory $proxyPath = parse_url($config['proxy_url'], PHP_URL_PATH); // replace the proxy directory from the $path $path = preg_replace('/^' . preg_quote($proxyPath, '/') . '/', '', $path); // make request $req = new Request($method, $config['domain_url']); $res = $req->send($path); // setup response // give same http status self::setHttpStatus($res->getStatusCode()); // content-type self::setHttpContentType($res->getHeader('content-type')); $content = (string) $res->getBody(); // Hijack all ajax requests if (stristr($res->getHeader('content-type'), 'html')) { // Idea from: http://verboselogging.com/2010/02/20/hijack-ajax-requests-like-a-terrorist $script_include = "\n <script>\n (function(open) {\n // set our start path\n \tvar ourSuperHackyProxyPath = '" . str_replace('\'', '', $config['proxy_url']) . "/';\n // hijack the XMLHttpRequest open method\n XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {\n // force internal links to use our proxy\n if ( ! url.match(/^https?:\\/\\//)) {\n url = ourSuperHackyProxyPath + url.replace(/^\\//, '');\n }\n open.call(this, method, url, async, user, pass);\n };\n })(XMLHttpRequest.prototype.open);\n </script>"; $content = str_replace('<head>', '<head>' . $script_include, $content); } // Handle additional HTML content if (isset($config['append_html_content']) && stristr($res->getHeader('content-type'), 'html')) { $content .= $config['append_html_content']; } // run replacements over content $res = new Response($content); $res->setProxyPath($config['proxy_url']); $res->replaceDomainLinks($config['domain_url'] . '/'); $res->replaceInternalHtmlLinks(); if (stristr($path, '.css')) { $res->replaceInternalCssLinks(); } $content = $res->getHtml(); // and finaly return the output return $content; }
public static function run($config) { // detect request method and url $method = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != '' ? $_SERVER['REQUEST_METHOD'] : 'GET'; $usingUrlParam = false; if (isset($config['url_param_access'])) { // use a url param to get url we're mimic'ing? $url = isset($_GET[$config['url_param_access']]) && $_GET[$config['url_param_access']] != '' ? $_GET[$config['url_param_access']] : '/'; $parsedDoamin = parse_url($url); $domain = (isset($parsedDoamin['protocol']) ? $parsedDoamin['protocol'] : 'http') . '://'; $domain .= isset($parsedDoamin['host']) ? $parsedDoamin['host'] : ''; $path = isset($parsedDoamin['path']) ? $parsedDoamin['path'] : '/'; $path .= isset($parsedDoamin['query']) ? '?' . $parsedDoamin['query'] : ''; $usingUrlParam = true; } else { // fallback to given request :) $domain = $config['domain_url']; $path = isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '' ? $_SERVER['REQUEST_URI'] : '/'; } if ($path == '/' && isset($config['start_path']) && $config['start_path'] != '') { $path = $config['start_path']; } // parse the proxy url, and grab the directory $proxyPath = parse_url($config['proxy_url'], PHP_URL_PATH); // replace the proxy directory from the $path $path = preg_replace('/^' . preg_quote($proxyPath, '/') . '/', '', $path); // TODO: throw an error if ($domain == 'http://' || $domain == '') { return ''; } // make request $req = new Request($method, $domain); $res = $req->send($path); // setup response // give same http status self::setHttpStatus($res->getStatusCode()); // content-type self::setHttpContentType($res->getHeader('content-type')); $content = (string) $res->getBody(); // Hijack all ajax requests if (stristr($res->getHeader('content-type'), 'html')) { // Idea from: http://verboselogging.com/2010/02/20/hijack-ajax-requests-like-a-terrorist $script_include = "\n <script>\n (function(open) {\n // set our start path\n \tvar ourSuperHackyProxyPath = '" . str_replace('\'', '', $domain) . "/';\n // hijack the XMLHttpRequest open method\n XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {\n // force internal links to use our proxy\n if ( ! url.match(/^https?:\\/\\//)) {\n \t" . ($usingUrlParam ? " url = ourSuperHackyProxyPath + encodeURIComponent(url.replace(/^\\//, '')); " : " url = ourSuperHackyProxyPath + url.replace(/^\\//, ''); ") . "\n }\n open.call(this, method, url, async, user, pass);\n };\n })(XMLHttpRequest.prototype.open);\n </script>"; $content = str_replace('<head>', '<head>' . $script_include, $content); } // Handle additional HTML content if (isset($config['append_html_content']) && stristr($res->getHeader('content-type'), 'html')) { $content .= $config['append_html_content']; } // run replacements over content $res = new Response($content); $res->setProxyPath($config['proxy_url'] . ($usingUrlParam ? '?' . $config['url_param_access'] . '=' . urlencode($domain) : '')); $res->replaceDomainLinks($domain); $res->replaceInternalHtmlLinks($usingUrlParam); if (stristr($path, '.css')) { $res->replaceInternalCssLinks($usingUrlParam); } $content = $res->getHtml(); // and finaly return the output return $content; }