root/trunk/wp-includes/class-snoopy.php

Revision 8082, 37.0 kB (checked in by westi, 3 weeks ago)

Updated phpdoc for external libs. See #7038 props jacobsantos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * Snoopy - the PHP net client
4  * @author Monte Ohrt <monte@ispi.net>
5  * @copyright 1999-2000 ispi, all rights reserved
6  * @version 1.01
7  * @license GNU Lesser GPL
8  * @link http://snoopy.sourceforge.net/
9  * @package Snoopy
10  */
11
12 if ( !in_array('Snoopy', get_declared_classes() ) ) :
13 /**
14  * Snoopy - the PHP net client
15  *
16  * @author Monte Ohrt <monte@ispi.net>
17  * @copyright (c): 1999-2000 ispi, all rights reserved
18  * @version 1.01
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33  *
34  * You may contact the author of Snoopy by e-mail at:
35  * monte@ispi.net
36  *
37  * Or, write to:
38  * Monte Ohrt
39  * CTO, ispi
40  * 237 S. 70th suite 220
41  * Lincoln, NE 68510
42  *
43  * @link http://snoopy.sourceforge.net/ The latest version of Snoopy can be
44  *        obtained
45  */
46 class Snoopy
47 {
48     /**** Public variables ****/
49
50     /* user definable vars */
51
52     var $host            =    "www.php.net";        // host name we are connecting to
53     var $port            =    80;                    // port we are connecting to
54     var $proxy_host        =    "";                    // proxy host to use
55     var $proxy_port        =    "";                    // proxy port to use
56     var $proxy_user        =    "";                    // proxy user to use
57     var $proxy_pass        =    "";                    // proxy password to use
58
59     var $agent            =    "Snoopy v1.2.3";    // agent we masquerade as
60     var    $referer        =    "";                    // referer info to pass
61     var $cookies        =    array();            // array of cookies to pass
62                                                 // $cookies["username"]="joe";
63     var    $rawheaders        =    array();            // array of raw headers to send
64                                                 // $rawheaders["Content-type"]="text/html";
65
66     var $maxredirs        =    5;                    // http redirection depth maximum. 0 = disallow
67     var $lastredirectaddr    =    "";                // contains address of last redirected address
68     var    $offsiteok        =    true;                // allows redirection off-site
69     var $maxframes        =    0;                    // frame content depth maximum. 0 = disallow
70     var $expandlinks    =    true;                // expand links to fully qualified URLs.
71                                                 // this only applies to fetchlinks()
72                                                 // submitlinks(), and submittext()
73     var $passcookies    =    true;                // pass set cookies back through redirects
74                                                 // NOTE: this currently does not respect
75                                                 // dates, domains or paths.
76
77     var    $user            =    "";                    // user for http authentication
78     var    $pass            =    "";                    // password for http authentication
79
80     // http accept types
81     var $accept            =    "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
82
83     var $results        =    "";                    // where the content is put
84
85     var $error            =    "";                    // error messages sent here
86     var    $response_code    =    "";                    // response code returned from server
87     var    $headers        =    array();            // headers returned from server sent here
88     var    $maxlength        =    8192;                // max return data length (body)
89     var $read_timeout    =    0;                    // timeout on read operations, in seconds
90                                                 // supported only since PHP 4 Beta 4
91                                                 // set to 0 to disallow timeouts
92     var $timed_out        =    false;                // if a read operation timed out
93     var    $status            =    0;                    // http request status
94
95     var $temp_dir        =    "/tmp";                // temporary directory that the webserver
96                                                 // has permission to write to.
97                                                 // under Windows, this should be C:\temp
98
99     var    $curl_path        =    "/usr/local/bin/curl";
100                                                 // Snoopy will use cURL for fetching
101                                                 // SSL content if a full system path to
102                                                 // the cURL binary is supplied here.
103                                                 // set to false if you do not have
104                                                 // cURL installed. See http://curl.haxx.se
105                                                 // for details on installing cURL.
106                                                 // Snoopy does *not* use the cURL
107                                                 // library functions built into php,
108                                                 // as these functions are not stable
109                                                 // as of this Snoopy release.
110
111     /**** Private variables ****/
112
113     var    $_maxlinelen    =    4096;                // max line length (headers)
114
115     var $_httpmethod    =    "GET";                // default http request method
116     var $_httpversion    =    "HTTP/1.0";            // default http request version
117     var $_submit_method    =    "POST";                // default submit method
118     var $_submit_type    =    "application/x-www-form-urlencoded";    // default submit type
119     var $_mime_boundary    =   "";                    // MIME boundary for multipart/form-data submit type
120     var $_redirectaddr    =    false;                // will be set if page fetched is a redirect
121     var $_redirectdepth    =    0;                    // increments on an http redirect
122     var $_frameurls        =     array();            // frame src urls
123     var $_framedepth    =    0;                    // increments on frame depth
124
125     var $_isproxy        =    false;                // set if using a proxy server
126     var $_fp_timeout    =    30;                    // timeout for socket connection
127
128 /*======================================================================*\
129     Function:    fetch
130     Purpose:    fetch the contents of a web page
131                 (and possibly other protocols in the
132                 future like ftp, nntp, gopher, etc.)
133     Input:        $URI    the location of the page to fetch
134     Output:        $this->results    the output text from the fetch
135 \*======================================================================*/
136
137     function fetch($URI)
138     {
139
140         //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
141         $URI_PARTS = parse_url($URI);
142         if (!empty($URI_PARTS["user"]))
143             $this->user = $URI_PARTS["user"];
144         if (!empty($URI_PARTS["pass"]))
145             $this->pass = $URI_PARTS["pass"];
146         if (empty($URI_PARTS["query"]))
147             $URI_PARTS["query"] = '';
148         if (empty($URI_PARTS["path"]))
149             $URI_PARTS["path"] = '';
150
151         switch(strtolower($URI_PARTS["scheme"]))
152         {
153             case "http":
154                 $this->host = $URI_PARTS["host"];
155                 if(!empty($URI_PARTS["port"]))
156                     $this->port = $URI_PARTS["port"];
157                 if($this->_connect($fp))
158                 {
159                     if($this->_isproxy)
160                     {
161                         // using proxy, send entire URI
162                         $this->_httprequest($URI,$fp,$URI,$this->_httpmethod);
163                     }
164                     else
165                     {
166                         $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
167                         // no proxy, send only the path
168                         $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
169                     }
170
171                     $this->_disconnect($fp);
172
173                     if($this->_redirectaddr)
174                     {
175                         /* url was redirected, check if we've hit the max depth */
176                         if($this->maxredirs > $this->_redirectdepth)
177                         {
178                             // only follow redirect if it's on this site, or offsiteok is true
179                             if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
180                             {
181                                 /* follow the redirect */
182                                 $this->_redirectdepth++;
183                                 $this->lastredirectaddr=$this->_redirectaddr;
184                                 $this->fetch($this->_redirectaddr);
185                             }
186                         }
187                     }
188
189                     if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
190                     {
191                         $frameurls = $this->_frameurls;
192                         $this->_frameurls = array();
193
194                         while(list(,$frameurl) = each($frameurls))
195                         {
196                             if($this->_framedepth < $this->maxframes)
197                             {
198                                 $this->fetch($frameurl);
199                                 $this->_framedepth++;
200                             }
201                             else
202                                 break;
203                         }
204                     }
205                 }
206                 else
207                 {
208                     return false;
209                 }
210                 return true;
211                 break;
212             case "https":
213                 if(!$this->curl_path)
214                     return false;
215                 if(function_exists("is_executable"))
216                     if (!is_executable($this->curl_path))
217                         return false;
218                 $this->host = $URI_PARTS["host"];
219                 if(!empty($URI_PARTS["port"]))
220                     $this->port = $URI_PARTS["port"];
221                 if($this->_isproxy)
222                 {
223                     // using proxy, send entire URI
224                     $this->_httpsrequest($URI,$URI,$this->_httpmethod);
225                 }
226                 else
227                 {
228                     $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
229                     // no proxy, send only the path
230                     $this->_httpsrequest($path, $URI, $this->_httpmethod);
231                 }
232
233                 if($this->_redirectaddr)
234                 {
235                     /* url was redirected, check if we've hit the max depth */
236                     if($this->maxredirs > $this->_redirectdepth)
237                     {
238                         // only follow redirect if it's on this site, or offsiteok is true
239                         if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
240                         {
241                             /* follow the redirect */
242                             $this->_redirectdepth++;
243                             $this->lastredirectaddr=$this->_redirectaddr;
244                             $this->fetch($this->_redirectaddr);
245                         }
246                     }
247                 }
248
249                 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
250                 {
251                     $frameurls = $this->_frameurls;
252                     $this->_frameurls = array();
253
254                     while(list(,$frameurl) = each($frameurls))
255                     {
256                         if($this->_framedepth < $this->maxframes)
257                         {
258                             $this->fetch($frameurl);
259                             $this->_framedepth++;
260                         }
261                         else
262                             break;
263                     }
264                 }
265                 return true;
266                 break;
267             default:
268                 // not a valid protocol
269                 $this->error    =    'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
270                 return false;
271                 break;
272         }
273         return true;
274     }
275
276 /*======================================================================*\
277     Function:    submit
278     Purpose:    submit an http form
279     Input:        $URI    the location to post the data
280                 $formvars    the formvars to use.
281                     format: $formvars["var"] = "val";
282                 $formfiles  an array of files to submit
283                     format: $formfiles["var"] = "/dir/filename.ext";
284     Output:        $this->results    the text output from the post
285 \*======================================================================*/
286
287     function submit($URI, $formvars="", $formfiles="")
288     {
289         unset($postdata);
290
291         $postdata = $this->_prepare_post_body($formvars, $formfiles);
292
293         $URI_PARTS = parse_url($URI);
294         if (!empty($URI_PARTS["user"]))
295             $this->user = $URI_PARTS["user"];
296         if (!empty($URI_PARTS["pass"]))
297             $this->pass = $URI_PARTS["pass"];
298         if (empty($URI_PARTS["query"]))
299             $URI_PARTS["query"] = '';
300         if (empty($URI_PARTS["path"]))
301             $URI_PARTS["path"] = '';
302
303         switch(strtolower($URI_PARTS["scheme"]))
304         {
305             case "http":
306                 $this->host = $URI_PARTS["host"];
307                 if(!empty($URI_PARTS["port"]))
308                     $this->port = $URI_PARTS["port"];
309                 if($this->_connect($fp))
310                 {
311                     if($this->_isproxy)
312                     {
313                         // using proxy, send entire URI
314                         $this->_httprequest($URI,$fp,$URI,$this->_submit_method,$this->_submit_type,$postdata);
315                     }
316                     else
317                     {
318                         $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
319                         // no proxy, send only the path
320                         $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
321                     }
322
323                     $this->_disconnect($fp);
324
325                     if($this->_redirectaddr)
326                     {
327                         /* url was redirected, check if we've hit the max depth */
328                         if($this->maxredirs > $this->_redirectdepth)
329                         {
330                             if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
331                                 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
332
333                             // only follow redirect if it's on this site, or offsiteok is true
334                             if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
335                             {
336                                 /* follow the redirect */
337                                 $this->_redirectdepth++;
338                                 $this->lastredirectaddr=$this->_redirectaddr;
339                                 if( strpos( $this->_redirectaddr, "?" ) > 0 )
340                                     $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
341                                 else
342                                     $this->submit($this->_redirectaddr,$formvars, $formfiles);
343                             }
344                         }
345                     }
346
347                     if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
348                     {
349                         $frameurls = $this->_frameurls;
350                         $this->_frameurls = array();
351
352                         while(list(,$frameurl) = each($frameurls))
353                         {
354                             if($this->_framedepth < $this->maxframes)
355                             {
356                                 $this->fetch($frameurl);
357                                 $this->_framedepth++;
358                             }
359                             else
360                                 break;
361                         }
362                     }
363
364                 }
365                 else
366                 {
367                     return false;
368                 }
369                 return true;
370                 break;
371             case "https":
372                 if(!$this->curl_path)
373                     return false;
374                 if(function_exists("is_executable"))
375                     if (!is_executable($this->curl_path))
376                         return false;
377                 $this->host = $URI_PARTS["host"];
378                 if(!empty($URI_PARTS["port"]))
379                     $this->port = $URI_PARTS["port"];
380                 if($this->_isproxy)
381                 {
382                     // using proxy, send entire URI
383                     $this->_httpsrequest($URI, $URI, $this->_submit_method, $this->_submit_type, $postdata);
384                 }
385                 else
386                 {
387                     $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
388                     // no proxy, send only the path
389                     $this->_httpsrequest($path, $URI, $this->_submit_method, $this->_submit_type, $postdata);
390                 }
391
392                 if($this->_redirectaddr)
393                 {
394                     /* url was redirected, check if we've hit the max depth */
395                     if($this->maxredirs > $this->_redirectdepth)
396                     {
397                         if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
398                             $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
399
400                         // only follow redirect if it's on this site, or offsiteok is true
401                         if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
402                         {
403                             /* follow the redirect */
404                             $this->_redirectdepth++;
405                             $this->lastredirectaddr=$this->_redirectaddr;
406                             if( strpos( $this->_redirectaddr, "?" ) > 0 )
407                                 $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
408                             else
409                                 $this->submit($this->_redirectaddr,$formvars, $formfiles);
410                         }
411                     }
412                 }
413
414                 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
415                 {
416                     $frameurls = $this->_frameurls;
417                     $this->_frameurls = array();
418
419                     while(list(,$frameurl) = each($frameurls))
420                     {
421                         if($this->_framedepth < $this->maxframes)
422                         {
423                             $this->fetch($frameurl);
424                             $this->_framedepth++;
425                         }
426                         else
427                             break;
428                     }
429                 }
430                 return true;
431                 break;
432
433             defau