插件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;}?>
- 插件46:简单的web代理服务器
- twisted编写简单的web代理服务器
- Python 简单的代理服务器
- proxy简单的代理服务器
- 简单的HTTP代理服务器实现
- 五大开源Web代理服务器的横向点评
- VC++6实现简单的代理服务器
- VC++6实现简单的代理服务器
- 一个简单的python代理服务器源码分析
- Eclipse EricssonSDS中实现简单的代理服务器
- centOS + squid 配置简单的代理服务器
- Boost实现简单的udp代理服务器
- 编写了一个简单的http代理服务器
- 用nginx搭建一个简单的代理服务器
- 用vlc搭建简单的流媒体代理服务器
- 利用代理服务器的简单爬虫程序
- java实现一个简单的代理服务器
- 通过nginx搭建一个简单的代理服务器
- 在ubuntu下安装sphinx-chinese 用于xml
- hadoop mapred(hive)执行目录 文件权限问题
- rails3 和 thinking_sphinx安装(brown_zhang)
- 如何学VC++?
- 简单命令回顾
- 插件46:简单的web代理服务器
- reids按天分割日志
- hive cli命令行选项
- Jquery调用webservices(各种返回值)
- select、poll与epoll的区别
- 安装pig时遇到的问题
- <sys/stat.h>的含义!
- JPA中关于MS SQL Server数据库的TimeStamp数据类型如何映射
- 【转】经典.net面试题目