Demonstration of using PHP to implement proxy function [upgraded to v0.0.2]

Source: Internet
Author: User
Tags foreach define array error handling header php code trim urlencode
Just wrote practicing's work. The feeling is still some practical value. Let's try it out for everyone.
Function: Implement Agent function through PHP
Applicable users: Slow download at home. And too lazy to find proxy. And there are high-speed host, and the host has PHP, and host support socket.
How to use:
1. Upload this program to the mainframe.
2. Browsing this file on the host through the browser
3. Fill in the URL to download the file address (if necessary, you can fill in the Referer)
4. Click Get proxy URL
5. The link in the newly opened page is the link that is downloaded via proxy

Attention:
1. Multi-Threaded Download, breakpoint continued transmission
2. HTTP protocol only supported
3. Server-Side header redirection is not supported
4. HTTP AUTH not supported
5. When downloading a larger file, you can modify the value of the Set_time_limit in the program to reduce the disconnection (default is 180, or 3 minutes)


PHP Code:--------------------------------------------------------------------------------

<?php
/***************************************/
/* */
/* PHP Http Proxy * *
* version:0.0.2 * *
/* Last modify:2005.1.12 * *
* author:q3boy<q3boy@sina.com> * *
/* */
/***************************************/
Set_time_limit (180);

Define (' Pp_error_code ', 1);
Define (' Pp_error_message ', 2);
Define (' Pp_error_all ', 3);
Define (' pp_return_string ', 1);
Define (' Pp_return_array ', 2);
Define (' Pp_return_all ', 3);

Class Cphpproxy {

var $_strurl;
var $_arrargv;
var $_strref;
var $_arrurl;
var $_ressocket;
var $_interror;
var $_strerror;
var $_arrdefaultarrayurl;
var $_arrdefaultport;

function Cphpproxy ($STRURL = null, $ARRARGV = Array ()) {
$this->init ();
Is_null ($STRURL) ": $this->seturl ($STRURL);
Is_null ($arrArgv [' refer '])? ': $this->setref ($arrArgv [' refer ']);
$this->proxy ($STRURL, $ARRARGV);
}
function SetError ($intError, $mixArgv =null) {
$this->_interror = $intError;
if ($intError ==1001) {
$this->_strerror = "parameter pass error while calling $MIXARGV method, enumeration value does not exist";
}elseif ($intError ==1) {
$this->_strerror = "url is incorrect, URL is malformed. \ n". $mixArgvs;
}elseif ($intError = = 2) {
$this->_strerror = "URL error, this protocol is not currently supported. \ n". $mixArgvs;
}elseif ($intError = = 3) {
$this->_strerror = "Cannot open". $this->_arrurl[' host '. ': '. $this->_arrurl[' Port '. " \ n ". $mixArgv [0]. ': '. $mixArgv [1];
}

return false;
}

function GetError ($enumReturnType =pp_error_code) {
if ($enumReturnType ==pp_error_all) {
Return Array (Str_pad ($this->_interror,5, ' 0 ', str_pad_left), $this->_strerror);
}elseif ($enumReturnType ==pp_error_message) {
return $this->_strerror;
}elseif ($enumReturnType ==pp_error_code) {
return $this->_interror;
}else {
return false;
}
}

function init () {
$this->_strurl = null;
$this->_arrargv = Array ();
$this->_strref = null;
$this->_arrurl = Array ();
$this->_ressocket = null;
$this->_interror = 0;
$this->_strerror = ' There is no error ';
Arrurl Default Value
$this->_arrdefaultarrayurl = Array (' protocol ' => ' http ', ' file ' => ' index.htm ');
Default port
$this->_arrdefaultport =array (' http ' => ' 80 ');
return true;
}
function SetUrl ($STRURL) {
$STRURL = Trim ($STRURL);
Regular
$reg = "/^ ([\w]+): \/\/)?" ([^:|\/|@]*:)? ([^:|\/|@]*@)? ([\w|\-|\.] +) (: \d+)? (\/[^\?| #]*)? (\? [^#]*)? (#.*) $/is ";
if (!preg_match ($reg, $STRURL, $arrUrl)) {
Return $this->seterror (1, $STRURL);
}else {
To disassemble a matching array
List ($tmp, $tmp, $arr [' Protocol '], $arr [' User '], $arr [' Pass '], $arr [' Host '], $arr [' Port '], $arr [' Path '], $arr [' query '] , $arr [' anchor ']) = $ARRURL;
Default protocol
if (! $arr [' protocol ']) {
$ARRURL [1] = $this->_arrdefaultarrayurl[' protocol ']. ':/ /';
}
Set filename
$arr [' file '] = basename ($arr [' path ']);
Default value
foreach ($this->_arrdefaultarrayurl as $key => $val) {
if ($arr [$key] = = ') {
$arr [$key] = $val;
}
}
Default port
if (Is_null ($this->_arrdefaultport[$arr [' Protocol ']]) {
Return $this->seterror (2, $arr [' Protocol ']);
}elseif (! $arr [' Port ']) {
$arr [' port '] = $this->_arrdefaultport[$arr [' Protocol ']];
}

Set URI
$arr [' uri '] = ($arr [' path ']? $arr [' Path ']: '/'. ($arr [' query ']? '? '. $arr [' query ']: '. ($arr [' Anchor ']? # '. $arr [' anchor ']: ';
Set URL
unset ($arrUrl [0]);
Unset ($ARRURL [2]);
$this->_strurl = Implode (", $ARRURL);
Set Arrurl
$this->_arrurl = $arr;
return true;
}
}
function GetUrl ($enumReturnType = Pp_return_array) {
if ($enumReturnType ==pp_return_string) {
return $this->_strurl;
}elseif ($enumReturnType ==pp_return_array) {
return $this->_arrurl;
}if ($enumReturnType ==pp_return_all) {
Return Array ($this->_strurl, $this->_arrurl);
}else {
return $this->seterror (1001);
}

}
function Setrefer ($STRREF) {
$this->_strref = Trim ($STRREF);
return true;
}
function Getrefer () {
return $this->_strref;
}

function Getproxyurl () {
Global $_server;
List ($strProcotol) = explode ('/', Strtolower (Trim ($_server[' server_protocol ')));
$str = $strProcotol. ':/ /'. $_server[' http_host '].$_server[' php_self '.
' u= '. UrlEncode ($this->geturl (pp_return_string)).
($this->getrefer ()? (' &r= '. UrlEncode ($this->getrefer ()): ");
return $str;
}
function Opensocket () {
$arr = $this->geturl ();
$this->_ressocket = @fsockopen ($arr [' Host '], $arr [' Port '], $intError, $strError, 30);
if (! $this->_ressocket) {
$this->_ressocket = null;
return $this->seterror (3,array ($intError, $strError));
}else {
return true;
}
}
function Getrequest () {
Return $this the->{' get '. Ucfirst ($this->_arrurl[' Protocol ')). ' Request '} ();
}
function SendRequest () {
Return fwrite ($this->_ressocket, $this->getrequest ());
}
function Flushresponse () {
return $this->{' flush '. Ucfirst ($this->_arrurl[' Protocol ')). ' Response '} ();
}
function Gethttprequest () {
$arr = $this->geturl ();
$arrRequest = Getallheaders ();
$arrRequest [' host '] = $arr [' Host '];
$strRequest = "Get". $arr [' URI ']. " Http/1.1\r\n ";
foreach ($arrRequest as $key => $val) {
$strRequest. = "$key: $val \ r \ n";
}
$strRequest. = "\ r \ n";
return $strRequest;
}
function Flushhttpresponse () {
$bolHeader =true;
while (!feof ($this->_ressocket)) {
$str = fgets ($this->_ressocket, 4096);
if ($bolHeader) {
@header ($STR);
}else {
Echo ($STR);
}
if ($bolHeader && $str = = "\ r \ n") {
$bolHeader =false;
@header ("Content-type:application/gzip");
@header (' Content-disposition:attachment filename= "'. $this->_arrurl[' file ');
}
}
return true;
}
function Close () {
Fclose ($this->_ressocket);
$this->_ressocket = null;
return true;
}
function proxy ($strUrl = null, $ARRARGV = Array ()) {
if (!is_null ($this->geturl (pp_return_string)) && $this->opensocket () && $this->sendrequest () && $this->flushresponse ()) {
Echo (123);
$this->close ();
}else {
return false;
}
}
}
if (sizeof ($_get)) {
$strGetArrayName = ' _get ';
}elseif (sizeof ($HTTP _get_vars)) {
$strGetArrayName = ' Http_get_vars ';
}else {
Die (' <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html" >
<title>PhpProxy</title>
<style type= "Text/css" >
<!--body,td {font-family:verdana; font-size:12px} th,input {Font-family:verdana;} -->
</style>
<body>
<form name= "Form1" method= "Get" action= "'. $_server[' php_self '". >
<table width= "border=" 1 "align=" Center "cellpadding=" 5 "cellspacing=" 1 ">
<tr>
<th colspan= "2" >PhpProxy</th>
</tr>
<tr>
&LT;TD width= "The Nowrap>url:</td>"
&LT;TD width= "432" ><input name= "U" type= "text" id= "U" size= "></td>"
</tr>
<tr>
&LT;TD nowrap>referer:</td>
<td><input name= "R" type= "text" id= "R" size= "></td>"
</tr>
<tr>
&LT;TD colspan= "2" align= "center" ><input name= "Act" value= "make" type= "hidden" >
<input type= "Submit" value= "Get Proxy Url" >
<input type= "reset" value= "reset" ></td>
</tr>
</table>
</form>
</body>
}
$STRURL = Trim (${$strGetArrayName}[' u '));
if ($strUrl = = ") {
Die (' Please enter URL address. ');
}
Get Referer
$STRREFTMP = Trim (${$strGetArrayName}[' R '));
Initialize proxy class
$objProxy = new Cphpproxy ();
Setting URLs and refer
$objProxy->seturl ($STRURL);
$objProxy->setrefer ($STRREF);
Error output
if ($objProxy->geterror ()) {
Die ($objProxy->geterror (pp_error_message));
}

echo URL for download
if (${$strGetArrayName}[' act ']== ' make ') {
Die ("<center>}
Fetch file
$objProxy->proxy ();
Error handling
if ($objProxy->geterror ()) {
Die ($objProxy->geterror (pp_error_message));
}
?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.