Function DownImageKeep ($ gurl, $ rfurl, $ filename, $ gcookie = "", $ JumpCount = 0, $ maxtime = 30) { $ Urlinfos = GetHostInfo ($ gurl ); $ Ghost = trim ($ urlinfos ['host']); If ($ ghost = ") { Return FALSE; } $ Gquery = $ urlinfos ['query']; If ($ gcookie = ""&&! Empty ($ rfurl )) { $ Gcookie = RefurlCookie ($ rfurl ); } $ SessionQuery = "GET $ gquery HTTP/1.1rn "; $ SessionQuery. = "Host: $ ghostrn "; $ SessionQuery. = "Referer: $ rfurlrn "; $ SessionQuery. = "Accept: */* rn "; $ SessionQuery. = "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98) rn "; If ($ gcookie! = ""&&! Preg_match ("/[rn]/", $ gcookie )) { $ SessionQuery. = $ gcookie. "rn "; } $ SessionQuery. = "Connection: Keep-Alivernrn "; $ Errno = ""; $ Errstr = ""; $ M_fp = fsockopen ($ ghost, 80, $ errno, $ errstr, 10 ); Fwrite ($ m_fp, $ sessionQuery ); $ Lnum = 0; // Obtain the response header $ M_httphead = Array (); $ Httpstas = explode ("", fgets ($ m_fp, 256 )); $ M_httphead ["http-edition"] = trim ($ httpstas [0]); $ M_httphead ["http-state"] = trim ($ httpstas [1]); While (! Feof ($ m_fp )) { $ Line = trim (fgets ($ m_fp, 256 )); If ($ line = "" ||$ lnum> 100) { Break; } $ Hkey = ""; $ Hvalue = ""; $ V = 0; For ($ I = 0; $ I { If ($ v = 1) { $ Hvalue. = $ line [$ I]; } If ($ line [$ I] = ":") { $ V = 1; } If ($ v = 0) { $ Hkey. = $ line [$ I]; } } $ Hkey = trim ($ hkey ); If ($ hkey! = "") { $ M_httphead [strtolower ($ hkey)] = trim ($ hvalue ); } } If (preg_match ("/^ 3/", $ m_httphead ["http-state"]) { If (isset ($ m_httphead ["location"]) & $ JumpCount <3) {$ JumpCount ++; DownImageKeep ($ gurl, $ rfurl, $ filename, $ gcookie, $ JumpCount) ;}else {return FALSE ;}} if (! Preg_match ("/^ 2/", $ m_httphead ["http-state"]) {return FALSE;} if (! Isset ($ m_httphead) {return FALSE;} $ contentLength = $ m_httphead ['content-length']; // save the image $ fp = fopen ($ filename, "w ") or die ("writing File: {$ filename} failed! "); $ I = 0; $ okdata = ""; $ starttime = time (); while (! Feof ($ m_fp) {$ okdata. = fgetc ($ m_fp); $ I ++; // time-out exit if (time ()-$ starttime> $ maxtime) { Break; } // End with the specified size If ($ I >=$ contentLength) { Break; } } If ($ okdata! = "") { Fwrite ($ fp, $ okdata ); } Fclose ($ fp ); If ($ okdata = "") { @ Unlink ($ filename ); Fclose ($ m_fp ); Return FALSE; } Fclose ($ m_fp ); Return TRUE; } // Obtain the host and query parts of the website Function GetHostInfo ($ gurl) { $ Gurl = preg_replace ("/^ http: // I", "", trim ($ gurl )); $ Garr ['host'] = preg_replace ("// (. *) $/I", "", $ gurl ); $ Garr ['query'] = "/". preg_replace ("/^ ([^/] *) // I", "", $ gurl ); Return $ garr; } // Obtain the Cookie information returned by the page Function RefurlCookie ($ gurl) { Global $ gcookie, $ lastRfurl; $ Gurl = trim ($ gurl ); If (! Empty ($ gcookie) & $ lastRfurl ==$ gurl) { Return $ gcookie; } Else { $ LastRfurl = $ gurl; } If (trim ($ gurl) = ") { Return "; } $ Urlinfos = GetHostInfo ($ gurl ); $ Ghost = $ urlinfos ['host']; $ Gquery = $ urlinfos ['query']; $ SessionQuery = "GET $ gquery HTTP/1.1rn "; $ SessionQuery. = "Host: $ ghostrn "; $ SessionQuery. = "Accept: */* rn "; $ SessionQuery. = "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98) rn "; $ SessionQuery. = "Connection: Closernrn "; $ Errno = ""; $ Errstr = ""; $ M_fp = fsockopen ($ ghost, 80, $ errno, $ errstr, 10) or die ($ ghost .' '); Fwrite ($ m_fp, $ sessionQuery ); $ Lnum = 0; // Obtain the detailed response header $ Gcookie = ""; While (! Feof ($ m_fp )) { $ Line = trim (fgets ($ m_fp, 256 )); If ($ line = "" ||$ lnum> 100) { Break; } Else { If (preg_match ("/^ cookie/I", $ line )) { $ Gcookie = $ line; Break; } } } Fclose ($ m_fp ); Return $ gcookie; } |