<?php //IAN BUGEJA 2005
    
require_once 'HTML_Parser.php';

    
$url $_REQUEST["ahnarurl"] or die("Missing url...please specify ?ahnarurl= parameter with URL to script.");
    if ((
stripos($url'http://') === false) && (stripos($url'https://') === false)) $url 'http://' $url;
    
$url str_replace('*amp;''&'$url);
    
$url trim($url);
    
$urlparsed parse_url($url);
    
    if (
stripos($url'https://') !== false)
    {
        die(
"Cannot open https site");
    }
    
    if (
$url == 'http://')
    {
      
$url 'http://ahnar.dyndns.org/notfound.php';
    }
    else
    {
        
$path $urlparsed['path'];
        
$fnd strpos($path'.');
        if (
$fnd !== false
        {
            
$fnd strrpos($path'/');
            
$path substr($path0$fnd);
        }
        
$nurl 'http://'.$urlparsed['host'].$urlparsed['port'].$path;
    }

    if (
$url[0] == 'H') { $url[0] = 'h'$url[1]='t'$url[2]='t'$url[3]='p'; }//fopen is case sensitive for porto
    
$handle fopen($url"rb");
    
$contents '';
    if (
$handle === false
        echo 
"ERROR NOT FOUND";
    else
    {
        
$status stream_get_meta_data($handle);
        
//print_r($status);
        
$stat $status['wrapper_data'];
        foreach(
$stat as $st//browse through http header
        
{
            if (
stripos($st'content-type') !== false
            {
                
$contyp stripos($st'text/html');
                if (
$contyp !== false
                    
$contentflag true;
                else
                    
$wcontyp $st;
            }
            elseif (
stripos($st'302 Object moved') !== false//found
            
{
                
$objectmovedflag true;
            }
            elseif ((
$objectmovedflag == true) && (stripos($st'location') !== false))
            {
                
$objectmovedloc substr($ststrpos($st' '));
            }
        }
        
        if (
$contentflag != true
        {
            
fclose($handle);
            
$newcontents 'Incorrect Content Type '.$wcontyp.'<br>';
            
$newcontents .= 'Please open the link in another window:<br>';
            
$newcontents .= "<a href='$url' target='_blank'>$url</a><br><br>";
            
$newcontents .= "Contents of the URL are below:<br>";
            
$newcontents .= "<iframe src='$url' width='80%' height='80%'>IFrame not supported</iframe>";
        }
        elseif (
$objectmovedflag == true)
        {
            echo 
"AHNAR <br> Object has been moved<br>Click the below link for correct location...<br>";
            echo 
"<a href='ahnar.php?ahnarurl=$objectmovedloc'>$objectmovedloc</a><br>";
            die(
" Error");
        }
        else
        {
            while (!
feof($handle)) {
                    
$contents .= fread($handle8192);
            }
            
fclose($handle);
            
//ADAPTIVE HYPERTEXT SYSTEM-------------------------------------------
            
$htmlparser = new HTML_Parser(''$nurl);
            
$newcontents $htmlparser->html_parse($contents);
        }
    }
 
?>

<!doctype html public "-//W3C//DTD HTML 4.01//EN">
<!-- Page Generated on:<?php echo date("l dS of F Y h:i:s A"); ?>-->
<!-- URL Retrieved: <?php echo $url?>-->
<html>

   <head>
      <title>AHNAR</title>
      <meta http-equiv="generator" content="AHNAR" />
      <?php if ($htmlparser->stshlink != '') { echo $htmlparser->stshlink; }
      if (
$contentflag != true) echo "<link rel='stylesheet' href='ahnarstyle.css'>";
      
?>
   </head>
   
<?php
   
if ($htmlparser->conframes == false) {
   if (
$flag != true) echo "<body class='ahnarbody'>"; else echo "<body style='text-align: left'>"?>
        <table width="1024"  style="text-align: center">
        <tr>
            <td>
                AHNAR --- <?php echo $nurl?> --- Ian Bugeja 2005, University of Malta, CSA3004 APT
            </td>
        </tr>
        </table>
        
        <?php  } echo $newcontents?>
   </body>
</html>