플러그인 46: 간단한 웹 프록시 서버

<?php // Plug-in 46: Simple Web Proxy

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$url = $_GET['u'];

// Change webproxy.php below to the name of your PHP proxy

$result = PIPHP_SimpleWebProxy(urldecode($_GET['u']), "webproxy.php");

switch(strtolower(substr($url, -4)))
{
   case ".jpg":
      header("Content-type: image/jpeg");   die($result);
   case ".gif":
      header("Content-type: image/gif");    die($result);
   case ".png":
      header("Content-type: image/png");    die($result);
   case ".ico":
      header("Content-type: image/x-icon"); die($result);
   case ".css":
      header("Content-type: text/css");     die($result);
   case ".xml":
      header("Content-type: text/xml");     die($result);
   case ".htm": case "html": case ".php":
      header("Content-type: text/html");    die($result);
   default:
      if (strtolower(substr($url, -3)) == ".js")
         header("Content-type: application/x-javascript");
      die($result);
}

function PIPHP_SimpleWebProxy($url, $redirect)
{
   // Plug-in 46: Simple Web Proxy
   //
   // This plug-in takes a URL as an argument which it then
   // passes back to the web browser with all links changed
   // to keep the proxy working. The arguments required are:
   //
   //    $url:      URL of a page to display
   //    $redirect: Location of the PHP proxy program

   $contents = @file_get_contents($url);
   if (!$contents) return NULL;
   
   switch(strtolower(substr($url, -4)))
   {
      case ".jpg": case ".gif": case ".png": case ".ico":
      case ".css": case ".js": case ".xml":
         return $contents;
   }

   $contents = str_replace('&', '&',         $contents);
   $contents = str_replace('&',     '!!**1**!!', $contents);
   
   $dom      = new domdocument();
   @$dom     ->loadhtml($contents);
   $xpath    = new domxpath($dom);
   $hrefs    = $xpath->evaluate("/html/body//a");
   $sources  = $xpath->evaluate("/html/body//img");
   $iframes  = $xpath->evaluate("/html/body//iframe");
   $scripts  = $xpath->evaluate("/html//script");
   $css      = $xpath->evaluate("/html/head/link");
   $links    = array();

   for ($j = 0 ; $j < $hrefs->length ; ++$j)
      $links[] = $hrefs->item($j)->getAttribute('href');
    
   for ($j = 0 ; $j < $sources->length ; ++$j)
      $links[] = $sources->item($j)->getAttribute('src');

   for ($j = 0 ; $j < $iframes->length ; ++$j)
      $links[] = $iframes->item($j)->getAttribute('src');

   for ($j = 0 ; $j < $scripts->length ; ++$j)
      $links[] = $scripts->item($j)->getAttribute('src');

   for ($j = 0 ; $j < $css->length ; ++$j)
      $links[] = $css->item($j)->getAttribute('href');

   $links = array_unique($links);
   $to    = array();
   $count = 0;
   sort($links);
   
   foreach ($links as $link)
   {
      if ($link != "")
      {
         $temp = str_replace('!!**1**!!', '&', $link);

         $to[$count] = "/$redirect?u=" .
           urlencode(PIPHP_RelToAbsURL($url, $temp));
         $contents = str_replace("href=\"$link\"",
            "href=\"!!$count!!\"", $contents);
         $contents = str_replace("href='$link'",
            "href='!!$count!!'",   $contents);
         $contents = str_replace("href=$link",
            "href=!!$count!!",     $contents);
         $contents = str_replace("src=\"$link\"",
            "src=\"!!$count!!\"",  $contents);
         $contents = str_replace("src='$link'",
            "src='!!$count!!'",    $contents);
         $contents = str_replace("src=$link",
            "src=!!$count!!",      $contents);
         ++$count;
      }
   }

   for ($j = 0 ; $j < $count ; ++$j)
      $contents = str_replace("!!$j!!", $to[$j],
         $contents);

   return str_replace('!!**1**!!', '&', $contents);
}

// The below function is repeated here to ensure that it's
// available to the main function which relies on it

function PIPHP_RelToAbsURL($page, $url)
{
   // Plug-in 21: Relative To Absolute URL
   //
   // This plug-in accepts the absolute URL of a web page
   // and a link featured within that page. The link is then
   // turned into an absolute URL which can be independently
   // accessed. Only applies to http:// URLs. Arguments are:
   //
   //    $page: The web page containing the URL
   //    $url:  The URL to convert to absolute

   if (substr($page, 0, 7) != "http://") return $url;
   
   $parse = parse_url($page);
   $root  = $parse['scheme'] . "://" . $parse['host'];
   $p     = strrpos(substr($page, 7), '/');
   
   if ($p) $base = substr($page, 0, $p + 8);
   else $base = "$page/";
   
   if (substr($url, 0, 1) == '/')           $url = $root . $url;
   elseif (substr($url, 0, 7) != "http://") $url = $base . $url;

   return $url;
}

?>

플러그인 설명:
플러그인은 URL 주소를 받아들여 모든 링크를 프록시를 통해 접근하는 것으로 바꾸고 수정된 URL 주소를 되돌려줍니다. 다음 인자를 받아들여야 합니다.
$url 변환할 URL
$redirect에서 웹 프록시를 제공하는 PHP 프로그램의 파일 이름
webproxy.php 내용은 다음과 같습니다.
<?php // Plug-in 46: Simple Web Proxy

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$url = $_GET['u'];

// Change webproxy.php below to the name of your PHP proxy

$result = PIPHP_SimpleWebProxy(urldecode($_GET['u']), "webproxy.php");

switch(strtolower(substr($url, -4)))
{
   case ".jpg":
      header("Content-type: image/jpeg");   die($result);
   case ".gif":
      header("Content-type: image/gif");    die($result);
   case ".png":
      header("Content-type: image/png");    die($result);
   case ".ico":
      header("Content-type: image/x-icon"); die($result);
   case ".css":
      header("Content-type: text/css");     die($result);
   case ".xml":
      header("Content-type: text/xml");     die($result);
   case ".htm": case "html": case ".php":
      header("Content-type: text/html");    die($result);
   default:
      if (strtolower(substr($url, -3)) == ".js")
         header("Content-type: application/x-javascript");
      die($result);
}

function PIPHP_SimpleWebProxy($url, $redirect)
{
   // Plug-in 46: Simple Web Proxy
   //
   // This plug-in takes a URL as an argument which it then
   // passes back to the web browser with all links changed
   // to keep the proxy working. The arguments required are:
   //
   //    $url:      URL of a page to display
   //    $redirect: Location of the PHP proxy program

   $contents = @file_get_contents($url);
   if (!$contents) return NULL;
   
   switch(strtolower(substr($url, -4)))
   {
      case ".jpg": case ".gif": case ".png": case ".ico":
      case ".css": case ".js": case ".xml":
         return $contents;
   }

   $contents = str_replace('&', '&',         $contents);
   $contents = str_replace('&',     '!!**1**!!', $contents);
   
   $dom      = new domdocument();
   @$dom     ->loadhtml($contents);
   $xpath    = new domxpath($dom);
   $hrefs    = $xpath->evaluate("/html/body//a");
   $sources  = $xpath->evaluate("/html/body//img");
   $iframes  = $xpath->evaluate("/html/body//iframe");
   $scripts  = $xpath->evaluate("/html//script");
   $css      = $xpath->evaluate("/html/head/link");
   $links    = array();

   for ($j = 0 ; $j < $hrefs->length ; ++$j)
      $links[] = $hrefs->item($j)->getAttribute('href');
    
   for ($j = 0 ; $j < $sources->length ; ++$j)
      $links[] = $sources->item($j)->getAttribute('src');

   for ($j = 0 ; $j < $iframes->length ; ++$j)
      $links[] = $iframes->item($j)->getAttribute('src');

   for ($j = 0 ; $j < $scripts->length ; ++$j)
      $links[] = $scripts->item($j)->getAttribute('src');

   for ($j = 0 ; $j < $css->length ; ++$j)
      $links[] = $css->item($j)->getAttribute('href');

   $links = array_unique($links);
   $to    = array();
   $count = 0;
   sort($links);
   
   foreach ($links as $link)
   {
      if ($link != "")
      {
         $temp = str_replace('!!**1**!!', '&', $link);

         $to[$count] = "/$redirect?u=" .
           urlencode(PIPHP_RelToAbsURL($url, $temp));
         $contents = str_replace("href=\"$link\"",
            "href=\"!!$count!!\"", $contents);
         $contents = str_replace("href='$link'",
            "href='!!$count!!'",   $contents);
         $contents = str_replace("href=$link",
            "href=!!$count!!",     $contents);
         $contents = str_replace("src=\"$link\"",
            "src=\"!!$count!!\"",  $contents);
         $contents = str_replace("src='$link'",
            "src='!!$count!!'",    $contents);
         $contents = str_replace("src=$link",
            "src=!!$count!!",      $contents);
         ++$count;
      }
   }

   for ($j = 0 ; $j < $count ; ++$j)
      $contents = str_replace("!!$j!!", $to[$j],
         $contents);

   return str_replace('!!**1**!!', '&', $contents);
}

// The below function is repeated here to ensure that it's
// available to the main function which relies on it

function PIPHP_RelToAbsURL($page, $url)
{
   // Plug-in 21: Relative To Absolute URL
   //
   // This plug-in accepts the absolute URL of a web page
   // and a link featured within that page. The link is then
   // turned into an absolute URL which can be independently
   // accessed. Only applies to http:// URLs. Arguments are:
   //
   //    $page: The web page containing the URL
   //    $url:  The URL to convert to absolute

   if (substr($page, 0, 7) != "http://") return $url;
   
   $parse = parse_url($page);
   $root  = $parse['scheme'] . "://" . $parse['host'];
   $p     = strrpos(substr($page, 7), '/');
   
   if ($p) $base = substr($page, 0, $p + 8);
   else $base = "$page/";
   
   if (substr($url, 0, 1) == '/')           $url = $root . $url;
   elseif (substr($url, 0, 7) != "http://") $url = $base . $url;

   return $url;
}

?>

좋은 웹페이지 즐겨찾기