views:

346

answers:

1

Hello, i am dynamically loading a website via file_get_contents with the following script.

<?php 
header('Content-Type: text/html; charset=iso-8859-1');

$url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
$base_url = explode('/', $url);
$base_url = (substr($url, 0, 7) == 'http://') ? $base_url[2] : $base_url[0];

if (file_get_contents($url) != false) {

$content = @file_get_contents($url);

// $search = array('@(<a\s*[^>]*href=[\'"]?(?![\'"]?http))@', '|(<img\s*[^>]*src=[\'"]?)|');
// $replace = array('\1proxy2.php?url=', '\1'.$url.'/');
// $new_content = preg_replace($search, $replace, $content);


function prepend_proxy($matches) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend = $matches[2] ? $matches[2] : $url;
    $prepend = 'http://h899310.devhost.se/proxy/proxy2.php?url='. $prepend .'/';

    return $matches[1] . $prepend . $matches[3];
}

function imgprepend_proxy($matches2) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend2 = $matches2[2] ? $matches2[2] : $url;
    $prepend2 = $prepend2 .'/';

    return $matches2[1] . $prepend2 . $matches2[3];
}


$new_content = preg_replace_callback(
    '|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
    'prepend_proxy',
    preg_replace_callback(
        '|(src=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
        'imgprepend_proxy',
        $content
    )
);

echo "<base href='http://{$base_url}' />";
echo $new_content;


} else {

  echo "Sidan kan inte visas";

}
?>

Now the problem is that some pictures doesn't show in websites. For example those sites who does have CSS links. It is a CSS problem i think.

You can test the script here to see what i mean:

http://h899310.devhost.se/proxy/index.html

How can I fix this?

+2  A: 

It would appear that one of your URL replacement methods is adding a slash too many. Visit one of the pages your proxy provides, and you will see several URLs beginning with:

http:///www.msdn.com

Take for example loading msdn.com; the CSS won't load, because when looking at the source code of the proxy'd page, we see the URL to the CSS is (note the tree forward slashes):

http://h899310.devhost.se/proxy/proxy2.php?url=http:///i3.msdn.microsoft.com/global/global-bn20090721.css

Viewing the URL directly reveals a warning in your script showing that file_get_contents can't load the URL:

Warning: file_get_contents(http:///i3.msdn.microsoft.com/global/global-bn20090721.css) [function.file-get-contents]: failed to open stream: No error in D:\users\u190790\h899310.devhost.se\Wwwroot\proxy\proxy2.php on line 9
Sidan kan inte visas

Briefly look at your code, it seems the problem is with $prepend; it should look like this instead:

<?php
$prepend = $matches2[2] ? $matches2[2] : $url . '/';
$prepend = $prepend;
?>
Paul Lammertsma
Yes, now it shows the pictures of the site. BUT! Now the links on the page DOESN'T go through the script like before: proxy2.php?url=asdsdasd