插件46:简单的web代理服务器

来源:互联网 发布:淘宝购物比价软件 编辑:程序博客网 时间:2024/05/22 02:28
<?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 itfunction 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 提供web代理的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 itfunction 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;}?>


原创粉丝点击