Two places were modified
1 adds a limit for getting the length
2 The head sends the data to add some parameter, lest by the opposite party server thinks is collects and directly rejects.
File Address: d:\website\***.com\www\include\dedehttpdown.class.php
<?php if (!defined (' Dedeinc ')) exit ("Request error!"); /** * Dream HTTP Download class * * @version $Id: dedehttpdown.class.php 1 11:42 July 6, 2010 z Tianya $ * @package dedecms.
Libraries * @copyright Copyright (c) 2007-2010, Desdev, Inc. * @license http://help.dedecms.com/usersguide/license.html * @link http://www.dedecms.com * * * @set_time_
Limit (0);
Class Dedehttpdown {var $m _url = ';
var $m _urlpath = ';
var $m _scheme = ' http ';
var $m _host = ';
var $m _port = ' 80 ';
var $m _user = ';
var $m _pass = ';
var $m _path = '/';
var $m _query = ';
var $m _fp = ';
var $m _error = ';
var $m _httphead = ';
var $m _html = ';
var $dataLimit = 0;
var $m _puthead = ';
var $BaseUrlPath = ';
var $HomeUrl = ';
var $reTry = 0;
var $JumpCount = 0;
/** * Initialization System * @access public * @param string $url address to download * @return string * * Function PrivateInit ($url) {if ($url = = ") {return;
} $urls = ';
$urls = @parse_url ($url);
$this->m_url = $url;
if (Is_array ($urls)) {$this->m_host = $urls ["Host"];
if (!empty ($urls ["scheme"]) {$this->m_scheme = $urls ["scheme"];
} if (!empty ($urls ["user"]) {$this->m_user = $urls ["User"];
} if (!empty ($urls ["Pass"]) {$this->m_pass = $urls [' Pass '];
} if (!empty ($urls ["Port"]) {$this->m_port = $urls ["Port"];
} if (!empty ($urls ["path"])) {$this->m_path = $urls ["Path"];
} $this->m_urlpath = $this->m_path;
if (!empty ($urls ["Query"]) {$this->m_query = $urls ["Query"];$this->m_urlpath. = "?".
$this->m_query;
$this->homeurl = $urls ["Host"];
$this->baseurlpath = $this->homeurl. $urls ["Path"]; $this->baseurlpath = preg_replace ("/\/" ([^\/]*) \. (.
*) $/","/", $this->baseurlpath);
$this->baseurlpath = preg_replace ("/\/$/", "", $this->baseurlpath);
}/** * Reset Parameters * * @access public * @return void */function Resetany ()
{$this->m_url = "";
$this->m_urlpath = "";
$this->m_scheme = "http";
$this->m_host = "";
$this->m_port = "80";
$this->m_user = "";
$this->m_pass = "";
$this->m_path = "/";
$this->m_query = "";
$this->m_error = ""; /** * Open the specified URL * * @access public * @param string $url address * @param string $requestType Request Type * @return String */FUnction OpenURL ($url, $requestType = "Get") {$this->resetany ();
$this->jumpcount = 0;
$this->m_httphead = Array ();
$this->m_html = ';
$this->datalimit = 0;
$this->retry = 0;
$this->close ();
Initialization system $this->privateinit ($url);
$this->privatestartsession ($requestType); /** * Go to 303 redirect URL * * @access public * @param string $url address * @return Strin
G */function Jumpopenurl ($url) {$this->resetany ();
$this->jumpcount++;
$this->m_httphead = Array ();
$this->m_html = "";
$this->close ();
Initialization system $this->privateinit ($url);
$this->privatestartsession (' get ');
/** * The reason for an operation error * * @access public * @return void/function Printerror ()
{echo error message: ". $this->m_error; echo "<br/> Specific return head:<br/> ";
foreach ($this->m_httphead as $k => $v) {echo "$k => $v <br/>\r\n";} /** * To determine if the answer result of a header sent with a GET method is correct * @access public * @return BOOL/function Isget
OK () {if (Preg_match ("/^2/", $this->gethead ("Http-state")) {return TRUE; else {$this->m_error. = $this->gethead ("Http-state"). "-". $this->gethead ("Http-de scribe ")."
<br/> ";
return FALSE; }/** * See if the returned page is a text type * * @access public * @return BOOL/function IsTe XT () {if (Preg_match ("/^2/", $this->gethead ("Http-state")) && Preg_match ("/text|xml/i", $this->ge
THead ("Content-type")) {return TRUE;
else {$this->m_error. = "Content is not text type or URL redirect <br/>";
return FALSE; }}/** * judgmentWhether the returned page is a specific type * * @access public * @param string $ctype Content Type * @return String * * function Iscontenttype ($ctype) {if (Preg_match ("/^2/", $this->gethead ("Http-state")) && $t
His->gethead ("Content-type") ==strtolower ($ctype)) {return TRUE;} else {$this->m_error. = "wrong type". $this->gethead ("Content-type"). "
<br/> ";
return FALSE;
/** * Download file with HTTP protocol * * @access public * @param string $savefilename Save file name
* @return string/function Savetobin ($savefilename) {if (! $this->isgetok ()) {
return FALSE; } if (@feof ($this->m_fp)) {$this->m_error = "The connection has been closed. ";
return FALSE;
$fp = fopen ($savefilename, "w");
while (!feof ($this->m_fp)) {fwrite ($fp, Fread ($this->m_fp, 1024)); } fclose ($this->m_fp);
Fclose ($FP);
return TRUE; /** * Save Web page content as text file * * @access public * @param string $savefilename save file name *
@return String */function Savetotext ($savefilename) {if ($this->istext ()) {
$this->savebinfile ($savefilename);
else {return ""; /** * Using HTTP protocol to obtain content of a Web page * * @access public * @return string/function Ge
Thtml () {if (! $this->istext ()) {return ';
} if ($this->m_html!= ') {return $this->m_html; } if (! $this->m_fp| |
@feof ($this->m_fp)) {return ';
while (!feof ($this->m_fp)) {$this->m_html. = fgets ($this->m_fp,256); if ($this->datalimit && strlen ($this->m_html) > $this->datalimit) break;
@fclose ($this->m_fp);
return $this->m_html; /** * Start HTTP Session * * @access public * @param string $requestType request type * @retur
n String */function privatestartsession ($requestType = "Get") {if (! $this->privateopenhost ())
{$this->m_error. = "Open remote host error!";
return FALSE;
} $this->retry++;
if ($this->gethead ("http-edition") = = "http/1.1") {$HTTPV = "http/1.1";
else {$HTTPV = "http/1.0";
$ps = Explode ('? ', $this->m_urlpath);
$headString = '; Sends a fixed start request header get, host information if ($requestType = = "Get") {$headString. = ' Get '. $this->m_urlpath. "$
Httpv\r\n ";
else {$headString. = "POST". $ps [0]. "$HTTPV \ r \ n"; } $this->m_puthead["Host" = $this->m_host; Send user-defined request headers if (!isset ($this->m_puthead["User-agent"])) {$this->m_puthead["user-agent" ] = "mozilla/4.0" (compatible; MSIE 6.0;
Windows NT 5.2) "; } if (!isset ($this->m_puthead["refer"])) {$this->m_puthead["refer"] = "http://". $this-&G
t;m_puthead["Host"];
}/* Add on 2012-12-19 */$headString. = "connection:keep-alive\r\n";
$headString. = "accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n"; # $headString. = "Accept-encoding:gzip, deflate\r\n";
Comment out $headString. = "accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
foreach ($this->m_puthead as $k => $v) {$k = Trim ($k);
$v = Trim ($v);
if ($k!= "" && $v!= "") {$headString. = "$k: $v \ r \ n";
} fputs ($this->m_fp, $headString); if ($requesttype== "POST") {$postdata = ""; if (count ($ps) >1) {for ($i =1; $i <count ($ps); $i + +) {$p
Ostdata. = $ps [$i];
} else {$postdata = "OK";
} $plen = strlen ($postdata);
Fputs ($this->m_fp, "content-type:application/x-www-form-urlencoded\r\n");
Fputs ($this->m_fp, "content-length: $plen \ r \ n");
//Send fixed end request Header//http1.1 protocol must specify close link after end of document, otherwise feof can not be used when reading document ($HTTPV = = "http/1.1") {
Fputs ($this->m_fp, "connection:close\r\n\r\n");
else {fputs ($this->m_fp, "\ r \ n");
} if ($requestType = = "POST") {fputs ($this->m_fp, $postdata);
//Get the response header status information $httpstas = Explode ("", Fgets ($this->m_fp,256)); $this->m_httphead["Http-eDition "] = Trim ($httpstas [0]);
$this->m_httphead["http-state"] = Trim ($httpstas [1]);
$this->m_httphead["http-describe"] = ""; for ($i =2; $i <count ($httpstas); $i + +) {$this->m_httphead["http-describe"]. Trim ($httpstas [$i
]);
//Get the detailed answer head while (!feof ($this->m_fp)) {$line = Trim (fgets ($this->m_fp,256));
if ($line = = "") {break;
} $hkey = "";
$hvalue = "";
$v = 0; For ($i =0 $i <strlen ($line), $i + +) {if ($v ==1) {$hvalue.
= $line [$i];
} if ($line [$i]== ":") {$v = 1;
} if ($v ==0) {$hkey. = $line [$i];
}} $hkey = Trim ($hkey);
if ($hkey!= "") {$this->m_httphead[strtolower ($hkey)] = Trim ($hvalue);
}//If the connection is not shut down properly, retry if (feof ($this->m_fp)) {if ($this->retry > 10)
{return FALSE;
$this->privatestartsession ($requestType); }//Determine if the answer is 3xx (Preg_match ("/^3/", $this->m_httphead["Http-state")) {if ($thi
S->jumpcount > 3) {return; } if (Isset ($this->m_httphead["Location")) {$newurl = $this->m_httphead["Lo
cation "];
if (Preg_match ("/^http/i", $newurl)) {$this->jumpopenurl ($newurl);
else {$newurl = $this->fillurl ($newurl);
$this->jumpopenurl ($newurl);
}
} else {$this->m_error = ' unrecognized reply.
"; }}/** * Gets the value of an HTTP header * * @access public * @param string $headname header file
Name * @return String */function GetHead ($headname) {$headname = Strtolower ($headname); return Isset ($this->m_httphead[$headname])?
$this->m_httphead[$headname]: '; /** * Sets the value of HTTP header * * @access public * @param string $skey key * @param string $svalue value * @return string/function Sethead ($skey, $svalue) {$this->m_puthead[$skey]
= $svalue;
/** * Open Connection * * @access public * @return bool/function Privateopenhost ()
{if ($this->m_host== "") {return FALSE;
} $errno = "";
$errstr = ""; $this->m_fp = @fsockopen ($this->m_host, $this->m_port, $errnO, $errstr, 10);
if (! $this->m_fp) {$this->m_error = $errstr;
return FALSE;
else {return TRUE;
}/** * Shut down connection * * @access public * @return void */function close () {
@fclose ($this->m_fp); /** * Complement relative URL * * @access public * @param string $surl requires an incomplete address * @return Stri
NG */function Fillurl ($surl) {$i = 0;
$dstr = "";
$pstr = "";
$okurl = "";
$pathStep = 0;
$surl = Trim ($surl);
if ($surl = = "") {return "";
$pos = Strpos ($surl, "#");
if ($pos >0) {$surl = substr ($surl, 0, $pos);
} if ($surl [0]== "/") {$okurl = "http://". $this->homeurl. $surl;
else if ($surl [0]== ".") {if (strlen ($surl) <=1) {return ""; else if ($surl [1]== "/") {$okurl = "http://". $this->baseurlpath. "
/". substr ($surl, 2,strlen ($surl)-2);
else {$urls = explode ("/", $surl);
foreach ($urls as $u) {if ($u = = "...")
{$pathStep + +; else if ($i <count ($urls)-1) {$dstr. = $urls [$i].
/";
else {$dstr. = $urls [$i];
} $i + +;
$urls = Explode ("/", $this->baseurlpath);
if (count ($urls) <= $pathStep) {return ""; } else {$pstr ="http://"; For ($i =0 $i <count ($urls)-$pathStep; $i + +) {$pstr. = $urls [$i]. "
/";
$okurl = $pstr. $dstr; }} else {if (strlen ($surl) <7) {$okurl = "http://". $this->baseurlpath. "
/". $surl;
else if (Strtolower (substr ($surl, 0,7)) = = "http://") {$okurl = $surl; else {$okurl = "http://". $this->baseurlpath. "
/". $surl;
} $okurl = Preg_replace ("/^ (http:\/\/)/I", "", $okurl);
$okurl = Preg_replace ("/\/{1,}/", "/", $okurl);
Return "http://". $okurl; }}//end Class