我根据 PHP Proxy, Dean 1.6, 小改了一下.
实现了php proxy, 增加了Post数据的功能(但对中文支持不好).
// ==== classWebPage.php ======
<?
/*
This program is free software; you can redistribute it and/or modify
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*Class WebPage**********************************\
* Come along with me into the world of ooPHP. *
* *
* WebPage is the main class in this script. It *
* controls getting pages as well as headers and *
* processing those pages. *
\************************************************/
class WebPage {
/*Var Descriptions*******************************\
* $URL The url we are proxying for *
* $pageData The raw data from the file *
* $headers Array containing the headers sent *
* $static Boolean if we are working on only rel *
* links *
* $currentServer String with the script's server *
* $scriptName Name of the main script *
* $varName Name of the var for passed in file *
* $updatedPageData The processed page data *
* $relDir The directory we're in for rel links *
* $fp The file pointer for reading the file *
\************************************************/
var $URL;
var $pageData;
var $headers;
var $static;
var $currentServer;
var $scriptName;
var $varName;
var $updatedPageData;
var $relDir;
var $fp;
/*Class Constructor******************************\
* The main constructor, pass in varible are *
* listed below. *
* *
* $URL The url we are proxying for *
* $static Boolean if we are working on only rel *
* links *
* $currentServer String with the script's server *
* $scriptName Name of the main script *
* $varName Name of the var for passed in file *
* $relDir The directory we're in for rel links *
\************************************************/
function WebPage($URL, $static, $currentServer, $scriptName, $varName, $relDir){
$this->URL = $URL;
$this->currentServer = $currentServer;
$this->static = $static;
$this->relDir = $relDir;
$this->pageData = "";
$this->varName = $varName;
$this->scriptName = $scriptName;
}
/*openLink***************************************\
* Function for connecting to the URL and getting *
* the headers. *
* *
* Note: The connection is closed by *
* getRawPageData or processPage, whichever comes *
* first. *
\************************************************/
function openLink(){
// echo "[".$this->URL."]";
/**********neo**************/
if(eregi("\?",$this->URL)){
$param="&";
}else{
$param="?";
}
foreach ($_POST as $key => $value) {
$param = $param.urlencode($key)."=".urlencode($value)."&";
}
$this->fp = fopen($this->URL.$param, "rb");
array_shift($http_response_header);
$this->headers = $http_response_header;
}
/*getHeaders*************************************\
* Function returns an array containing the *
* headers that resulted from the page. *
\************************************************/
function getHeaders(){
return $this->headers;
}
/*getRawPageData*********************************\
* Prints out the $fp as a stream without *
* processing (for images/such) *
\************************************************/
function getRawPageData(){
fpassthru($this->fp);
// fclose($this->fp);
return;
}
/*getPageData************************************\
* Function returns a string containing the *
* page data. processPageData must be used *
* before this function is called. *
\************************************************/
function getPageData(){
return $this->updatedPageData;
}
/*processPageData********************************\
* Function process the page, fixing links and *
* images to use the proxy as specified by the *
* class. *
* *
* Note: Add items here if you want to add extra *
* prefixes or delims. *
\************************************************/
function processPageData(){
$this->pageData = "";
while( !feof( $this->fp)){
$this->pageData .= fgets($this->fp, 4096);
}
fclose($this->fp);
$delim[]='"';
$delim[]="'";
$delim[]="";
$pre[]="src=";
$pre[]="background=";
$pre[]="href=";
$pre[]="url\(";
$pre[]="codebase=";
$pre[]="url=";
$pre[]="archive=";
$pre[]="action=";
$this->redirect($pre,$delim);
}
/*fileName***************************************\
* Function returns the name of the file that the *
* URL specifies. *
\************************************************/
function fileName(){
return eregi_replace("[#?].*$", "",
eregi_replace("^.*/", "", $this->URL));
}
/*encodeHTML*************************************\
* Fix Me: *
* This is not done yet but the idea is to change *
* all the html charcters to their special char *
* information (to keep people from stealing your *
* source and using it) *
\************************************************/
function encodeHTML(){
// Not Done Yet
}
/*encryptPage************************************\
* Function changes updatedPageData so that the *
* file is encrypted while sending. *
\************************************************/
function encryptPage(){
$data2 = "";
foreach (split("\n",eregi_replace("\r","",$this->updatedPageData)) as $a){
$data = $this->encrypt(chop($a));
$data = str_replace( "\\", "\\\\", $data);
$data = str_replace( "\"", "\\\"", $data);
$data2 .= "document.write( c(\"".$data."\") );\n";
}
$this->updatedPageData = ""
. "<!-- URL Proxy Server\n"
. "Javascript by Bob Matcuk\n"
. "BMatcuk@Users.SourceForge.Net -->\n"
. "<script language=\"Javascript\">\n"
. " function c(s){\n"
. " var fi = \"\";\n"
. " for( var i = 0; i < s.length-1; i += 2 ){\n"
. " fi = fi + s.charAt(i+1) + s.charAt(i);\n"
. " }\n"
. " if( i < s.length ){\n"
. " fi = fi + s.charAt(i);\n"
. " }\n"
. " fi = fi + \"\\n\";\n"
. " return fi;\n"
. " }\n" . " document.open();\n"
. $data2
. " document.close();\n"
. "</script>\n";
}
/*redirect***************************************\
* Private Function, DO NOT USE FROM PUBLIC SCOPE *
* Basically converts the prefixes in *
* $prefixArray and the delim in $delimArray to a *
* string and uses it to fix links within the *
* page. *
\************************************************/
function redirect($prefixArray,$delimArray){
// echo "redirect!";
$a = $this->pageData;
$name = $this->currentServer;
$fileDir = $this->relDir;
foreach($prefixArray as $prefix){
$start2 = stripslashes($prefix);
$start = $prefix . "[ ]*";
foreach($delimArray as $delim){
if(eregi($prefix . "[ ]*" . $delim, $a) && ($delim == "" ? eregi($prefix . "[ ]*" . "[a-z,A-Z,/,0-9]", $a) : 1)){
$a = eregi_replace($start . $delim . "//",
$start2 . '\a' . "//",
$a);
$a = eregi_replace($start . $delim . "/",
$start2 . $delim . $name . "/" . $this->scriptName . "?" . $this->varName . "=/",
$a);
$a = eregi_replace($start . $delim . "http://",
$start2 . '\a' . "http://",
$a);
$a = eregi_replace($start . $delim . "mailto:",
$start2 . '\a' . "mailto:",
$a);
$a = eregi_replace($start . $delim . "#",
$start2 . '\a' . "#",
$a);
$a = eregi_replace($start . $delim . "javascript:",
$start2 . '\a' . "javascript:",
$a);
$a = eregi_replace($start . $delim,
$start2 . $delim . $name . "/" . $this->scriptName . "?" . $this->varName . "=" . $fileDir ,
$a);
$a = eregi_replace($start . '[\]a',
$start2 . $delim,
$a);
}
}
}
$this->updatedPageData = $a;
}
/*encrypt****************************************\
* This function encrypts the page before it is *
* sent out. *
* Returns the file as a string, encrypted *
\************************************************/
function encrypt( $string ){
for( $i = 0; $i < strlen( $string ) - 1; $i += 2 ){
$temp = $string[$i];
$string[$i] = $string[$i+1];
$string[$i+1] = $temp;
}
return $string;
}
}
?>
// ==== URLproxyServer.php ======
<?
/*
This program is free software; you can redistribute it and/or modify
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/************************************************\
* URL proxy v1.3 Pre-Alpha *
* a co-authored script *
* by Scott Atkins <atkinssc@engr.orst.edu> *
* Bob Matcuk <bmatcuk@users.sourceforge.net> *
* Copyright (C) 2002 All rights reserved *
* Released under the GPL, see the README *
* *
* Edit config.php instead of this file please *
* If you change this file and break your proxy *
* don't ask for help, as you were warned *
\************************************************/
// User should not have to edit this file
include("config.php");
include("classWebPage.php");
// Constants
$STATIC = 1;
$DYNAMIC = 2;
// Mode of the script
$proxyMode = ((isset($server) AND isset($redirectIP)) ? $STATIC : $DYNAMIC);
// Version Number
$phase = "pre-alpha-neo-remake";
$version = "1.3 " . $phase;
/*Flags******************************************\
* $isHTML; true if mimetype is html *
* $isImage; true if mimetype is an image *
* $isDown; true if specified mimetype is to be *
* downloaded *
* $isAuth; true if the page is protected by *
* .htaccess *
* $isForm; true if the page contains a form *
\************************************************/
$isHTML = true;
$isImage = false;
$isDown = true;
$isAuth = true;
$isForm = true;
$isError = true;
/*getContentType*********************************\
* Function for finding the mime type of the file *
* Returns the content type *
\************************************************/
function getContentType($headers){
foreach($headers as $h){
if(eregi("^Content-Type: (.*)$",$h,$A1))
return $A1[1];
}
}
/*processHeaders*********************************\
* Function for handling the headers that apache *
* sends out. *
* Returns an array with the headers that should *
* be sent out *
\************************************************/
function processHeaders($headers, $fileName, $mime_dl, &$type,
&$isDown, &$isHTML, &$isImage){
array_shift($headers);
$type = getContentType($headers);
$isDown = (isset($mime_dl[$type]) ? $mime_dl[$type] : true);
if(eregi("image",$type))
$isImage = true;
elseif(eregi("text/html",$type))
$isHTML = true;
if($isDown)
$headers[] = "Content-Disposition: attachment;" .
" filename=$fileName";
return $headers;
}
/************************************************\
* This block of code gets the directory we are *
* currently in, for rel links. *
\************************************************/
if(eregi("http://",$_GET[$fileVar])){
$relDir = eregi_replace("^http://[^/]*", "", $_GET[$fileVar]);
}else{
$relDir = $_GET[$fileVar];
}
//echo "[".$_GET[$fileVar]."]";
$relDir = eregi_replace("/[^/]*$","/",$relDir);
//echo "[reldir=".$relDir."]";
/************************************************\
* We create a new object of type WebPage and *
* pass it the url we are being a proxy for and *
* other information about the current state. *
\************************************************/
$page = new WebPage($redirectIP."/".$_GET[$fileVar],true,$server,"URLproxyServer.php",$fileVar,$relDir);
/************************************************\
* This tells the WebPage object to open up a *
* connection to the URL. *
* *
* Note: *
* This does not actually get the web page, just *
* opens the connection for the headers. *
\************************************************/
$page->openLink();
/************************************************\
* Process the headers so we know what kind of *
* data we have (html/other) *
\************************************************/
$head = processHeaders($page->getHeaders(),$file,$mime_dl,$type,$isDown,$isHtml,$isImage);
/************************************************\
* This code replicates the headers that were *
* sent when the class connected to the url. *
* *
* FIXME: extra headers need to be sent if we are *
* downloading the file. *
* *
* GOTCHA?: need to check if http 1.1 will work *
* correctly *
\************************************************/
//foreach($head as $h) header($h);
/************************************************\
* This block of code displays the page to the *
* user. *
* *
* Note: Both processPageData and getRawPageData *
* close the connection to the URL when they *
* return. You must re-open a connection with *
* openLink to use them again. *
\************************************************/
//if($isHtml){
if($isImage) {
$page->getRawPageData();
} else{
$page->processPageData();
if($encryptPage)
$page->encryptPage();
echo $page->getPageData();
}
//}else{
// if($isImage)
// $page->getRawPageData();
//}
?>
// ==== config.php ======
<?
/*
This program is free software; you can redistribute it and/or modify
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**********************************\
* filename: config.php *
* date: 5/6/2002 *
* Notes: Edit this file, rather *
* than the server script *
\**********************************/
global $mime_dl;
global $server;
global $redirectIP;
global $fileVar;
global $encryptPage;
/*Mime Types***********************\
* These are mime types you don't *
* want to have downloaded by *
* default (false is required) *
\**********************************/
$mime_dl["text/html"]=false;
$mime_dl["application/pdf"]=false;
$mime_dl["application/x-javascript"]=false;
$mime_dl["application/x-shockwave-flash"]=false;
$mime_dl["image/bmp"]=false;
$mime_dl["image/gif"]=false;
$mime_dl["image/jpeg"]=false;
$mime_dl["image/tiff"]=false;
$mime_dl["text/css"]=false;
$mime_dl["text/directory"]=false;
$mime_dl["text/plain"]=false;
/*Server Vars***********************\
* Change these to match your server *
* Comment out redirectIP to have *
* the proxy work on all ips (by *
* absolute URLs) *
\***********************************/
$server = "http://xxx.yyy.com.cn/~xxx/gate";
$redirectIP = "http://172.16.x.xx:8080";
$fileVar="file";
?>
-----
2005年1月4日星期二
eclipse plugin开发中的感觉
如果没有建设性意见请不要和我辩论
我常常想,Eclipse真是一个了不起的软件,尤其是他的插件,更使他生命力旺盛。
我得感谢IBM的开发者。
但是写Eclipse的人真是个接口狂人。他达到了滥用接口的地步。多少次我以为自己
懂了代码。但是最后实现自己的插件的时候却遇到了困难。
你别指望getLocation或者toOSString可以得到地址。他们要么就是没用的路径要么就是
null。和从javadoc看出来的含义永远是不同的。
像IProgressMonitor这样的接口会在函数调用的时候传下去好多层。很难把功能分开。
相同的接口,对同一操作会有不同的操作方法。你的视情况多次试探才行。
但是一旦搞定了还挺有意思的,好像一个RPG游戏通过了一关。
-----
我常常想,Eclipse真是一个了不起的软件,尤其是他的插件,更使他生命力旺盛。
我得感谢IBM的开发者。
但是写Eclipse的人真是个接口狂人。他达到了滥用接口的地步。多少次我以为自己
懂了代码。但是最后实现自己的插件的时候却遇到了困难。
你别指望getLocation或者toOSString可以得到地址。他们要么就是没用的路径要么就是
null。和从javadoc看出来的含义永远是不同的。
像IProgressMonitor这样的接口会在函数调用的时候传下去好多层。很难把功能分开。
相同的接口,对同一操作会有不同的操作方法。你的视情况多次试探才行。
但是一旦搞定了还挺有意思的,好像一个RPG游戏通过了一关。
-----
订阅:
评论 (Atom)