Skip to content

Instantly share code, notes, and snippets.

@gelendir
Created March 31, 2012 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gelendir/2267118 to your computer and use it in GitHub Desktop.
Save gelendir/2267118 to your computer and use it in GitHub Desktop.
<?php
$BASE_URL = "http://131.137.250.85/cgi-bin/qweb.dll";
$SCRIPT_NAME = basename(__FILE__);
function send_request($url, $body=null) {
$query = parse_url($url);
$path = $query['path'];
if( isset( $query['query'] ) ) {
$path .= "?" . $query['query'];
}
$method = "GET";
if( $body != null ) {
$method = "POST";
}
$request = "$method $path HTTP/1.1\n"
. "Host: ${query['host']}\n";
$handle = fsockopen($query['host'], 80);
fwrite($handle, $request);
foreach( getallheaders() as $key => $value ) {
if( $key != "Host" && $key != "Origin" ) {
fwrite($handle, "$key: $value\n");
}
}
fwrite($handle, "\n");
if( $body != null ) {
fwrite($handle, $body);
}
return $handle;
}
function substitute_urls($content) {
global $BASE_URL;
global $SCRIPT_NAME;
$content = str_replace( $BASE_URL, $SCRIPT_NAME, $content );
return $content;
}
function rearrange_page($url, $body=null) {
$headers = array();
$content = "";
$handle = send_request($url, $body);
$line = fgets($handle);
$headers[] = $line;
$continue = true;
while( $continue ) {
$line = fgets($handle);
$continue = (
!feof($handle) &&
substr( $line, 0, 1 ) != "<"
);
if( substr( $line, 0, 1 ) != "<" && trim($line) != "" ) {
$headers[] = $line;
}
}
$content .= $line;
while( !feof($handle) ) {
$line = fgets($handle);
$content .= $line;
}
fclose($handle);
$content = substitute_urls($content);
return array(
'headers' => $headers,
'content' => $content,
);
}
if( count( $_REQUEST ) == 0 ) {
$query = $BASE_URL . "?6GQHKN2";
$page = rearrange_page($query);
} else if ( count( $_GET ) > 0 ) {
$parts = array();
foreach( $_GET as $key => $value ) {
$parts[] = "$key=$value";
}
$query = $BASE_URL . "?" . implode( "&", $parts );
$page = rearrange_page($query);
} else if ( count( $_POST ) > 0 ) {
$query = $BASE_URL;
$body = file_get_contents("php://input");
$page = rearrange_page($query, $body);
}
foreach( $page['headers'] as $header ) {
header($header);
}
echo $page['content'];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment