Make WordPress Core

Changeset 9303


Ignore:
Timestamp:
10/23/2008 09:08:09 PM (18 years ago)
Author:
ryan
Message:

Snoopy 1.2.4

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-snoopy.php

    r8082 r9303  
    11<?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  *
     2
     3/*************************************************
     4
     5Snoopy - the PHP net client
     6Author: Monte Ohrt <monte@ispi.net>
     7Copyright (c): 1999-2008 New Digital Group, all rights reserved
     8Version: 1.2.4
     9
    2010 * This library is free software; you can redistribute it and/or
    2111 * modify it under the terms of the GNU Lesser General Public
     
    3121 * License along with this library; if not, write to the Free Software
    3222 * 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  */
     23
     24You may contact the author of Snoopy by e-mail at:
     25monte@ohrt.com
     26
     27The latest version of Snoopy can be obtained from:
     28http://snoopy.sourceforge.net/
     29
     30*************************************************/
     31
    4632class Snoopy
    4733{
    4834    /**** Public variables ****/
    49 
     35   
    5036    /* user definable vars */
    5137
     
    5642    var $proxy_user     =   "";                 // proxy user to use
    5743    var $proxy_pass     =   "";                 // proxy password to use
    58 
    59     var $agent          =   "Snoopy v1.2.3";    // agent we masquerade as
     44   
     45    var $agent          =   "Snoopy v1.2.4";    // agent we masquerade as
    6046    var $referer        =   "";                 // referer info to pass
    6147    var $cookies        =   array();            // array of cookies to pass
     
    7460                                                // NOTE: this currently does not respect
    7561                                                // dates, domains or paths.
    76 
     62   
    7763    var $user           =   "";                 // user for http authentication
    7864    var $pass           =   "";                 // password for http authentication
    79 
     65   
    8066    // http accept types
    8167    var $accept         =   "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
    82 
     68   
    8369    var $results        =   "";                 // where the content is put
    84 
     70       
    8571    var $error          =   "";                 // error messages sent here
    8672    var $response_code  =   "";                 // response code returned from server
    8773    var $headers        =   array();            // headers returned from server sent here
    88     var $maxlength      =   8192;               // max return data length (body)
     74    var $maxlength      =   500000;             // max return data length (body)
    8975    var $read_timeout   =   0;                  // timeout on read operations, in seconds
    9076                                                // supported only since PHP 4 Beta 4
     
    10894                                                // as these functions are not stable
    10995                                                // as of this Snoopy release.
    110 
    111     /**** Private variables ****/
    112 
     96   
     97    /**** Private variables ****/   
     98   
    11399    var $_maxlinelen    =   4096;               // max line length (headers)
    114 
     100   
    115101    var $_httpmethod    =   "GET";              // default http request method
    116102    var $_httpversion   =   "HTTP/1.0";         // default http request version
     
    122108    var $_frameurls     =   array();            // frame src urls
    123109    var $_framedepth    =   0;                  // increments on frame depth
    124 
     110   
    125111    var $_isproxy       =   false;              // set if using a proxy server
    126112    var $_fp_timeout    =   30;                 // timeout for socket connection
     
    137123    function fetch($URI)
    138124    {
    139 
     125   
    140126        //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
    141127        $URI_PARTS = parse_url($URI);
     
    148134        if (empty($URI_PARTS["path"]))
    149135            $URI_PARTS["path"] = '';
    150 
     136               
    151137        switch(strtolower($URI_PARTS["scheme"]))
    152138        {
     
    168154                        $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
    169155                    }
    170 
     156                   
    171157                    $this->_disconnect($fp);
    172158
     
    191177                        $frameurls = $this->_frameurls;
    192178                        $this->_frameurls = array();
    193 
     179                       
    194180                        while(list(,$frameurl) = each($frameurls))
    195181                        {
     
    202188                                break;
    203189                        }
    204                     }
     190                    }                   
    205191                }
    206192                else
     
    208194                    return false;
    209195                }
    210                 return true;
     196                return true;                   
    211197                break;
    212198            case "https":
     
    262248                            break;
    263249                    }
    264                 }
    265                 return true;
     250                }                   
     251                return true;                   
    266252                break;
    267253            default:
     
    270256                return false;
    271257                break;
    272         }
     258        }       
    273259        return true;
    274260    }
     
    288274    {
    289275        unset($postdata);
    290 
     276       
    291277        $postdata = $this->_prepare_post_body($formvars, $formfiles);
    292 
     278           
    293279        $URI_PARTS = parse_url($URI);
    294280        if (!empty($URI_PARTS["user"]))
     
    320306                        $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
    321307                    }
    322 
     308                   
    323309                    $this->_disconnect($fp);
    324310
     
    327313                        /* url was redirected, check if we've hit the max depth */
    328314                        if($this->maxredirs > $this->_redirectdepth)
    329                         {
     315                        {                       
    330316                            if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
    331                                 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
    332 
     317                                $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);                     
     318                           
    333319                            // only follow redirect if it's on this site, or offsiteok is true
    334320                            if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
     
    349335                        $frameurls = $this->_frameurls;
    350336                        $this->_frameurls = array();
    351 
     337                       
    352338                        while(list(,$frameurl) = each($frameurls))
    353                         {
     339                        {                                                       
    354340                            if($this->_framedepth < $this->maxframes)
    355341                            {
     
    360346                                break;
    361347                        }
    362                     }
    363 
     348                    }                   
     349                   
    364350                }
    365351                else
     
    367353                    return false;
    368354                }
    369                 return true;
     355                return true;                   
    370356                break;
    371357            case "https":
     
    394380                    /* url was redirected, check if we've hit the max depth */
    395381                    if($this->maxredirs > $this->_redirectdepth)
    396                     {
     382                    {                       
    397383                        if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
    398                             $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
     384                            $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);                     
    399385
    400386                        // only follow redirect if it's on this site, or offsiteok is true
     
    418404
    419405                    while(list(,$frameurl) = each($frameurls))
    420                     {
     406                    {                                                       
    421407                        if($this->_framedepth < $this->maxframes)
    422408                        {
     
    427413                            break;
    428414                    }
    429                 }
    430                 return true;
     415                }                   
     416                return true;                   
    431417                break;
    432 
     418               
    433419            default:
    434420                // not a valid protocol
     
    436422                return false;
    437423                break;
    438         }
     424        }       
    439425        return true;
    440426    }
     
    450436    {
    451437        if ($this->fetch($URI))
    452         {
     438        {           
    453439            if($this->lastredirectaddr)
    454440                $URI = $this->lastredirectaddr;
     
    478464    function fetchform($URI)
    479465    {
    480 
     466       
    481467        if ($this->fetch($URI))
    482         {
     468        {           
    483469
    484470            if(is_array($this->results))
     
    489475            else
    490476                $this->results = $this->_stripform($this->results);
    491 
     477           
    492478            return true;
    493479        }
     
    495481            return false;
    496482    }
    497 
    498 
     483   
     484   
    499485/*======================================================================*\
    500486    Function:   fetchtext
     
    507493    {
    508494        if($this->fetch($URI))
    509         {
     495        {           
    510496            if(is_array($this->results))
    511497            {
     
    531517    {
    532518        if($this->submit($URI,$formvars, $formfiles))
    533         {
     519        {           
    534520            if($this->lastredirectaddr)
    535521                $URI = $this->lastredirectaddr;
     
    565551    {
    566552        if($this->submit($URI,$formvars, $formfiles))
    567         {
     553        {           
    568554            if($this->lastredirectaddr)
    569555                $URI = $this->lastredirectaddr;
     
    589575    }
    590576
    591 
     577   
    592578
    593579/*======================================================================*\
     
    601587    }
    602588
    603 
     589   
    604590/*======================================================================*\
    605591    Function:   set_submit_normal
     
    612598    }
    613599
    614 
    615 
     600   
     601   
    616602
    617603/*======================================================================*\
    618604    Private functions
    619605\*======================================================================*/
    620 
    621 
     606   
     607   
    622608/*======================================================================*\
    623609    Function:   _striplinks
     
    628614
    629615    function _striplinks($document)
    630     {
     616    {   
    631617        preg_match_all("'<\s*a\s.*?href\s*=\s*          # find <a href=
    632618                        ([\"\'])?                   # find single or double quote
     
    634620                                                    # quote, otherwise match up to next space
    635621                        'isx",$document,$links);
    636 
     622                       
    637623
    638624        // catenate the non-empty matches from the conditional subpattern
     
    642628            if(!empty($val))
    643629                $match[] = $val;
    644         }
    645 
     630        }               
     631       
    646632        while(list($key,$val) = each($links[3]))
    647633        {
    648634            if(!empty($val))
    649635                $match[] = $val;
    650         }
    651 
     636        }       
     637       
    652638        // return the links
    653639        return $match;
     
    662648
    663649    function _stripform($document)
    664     {
     650    {   
    665651        preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
    666 
     652       
    667653        // catenate the matches
    668654        $match = implode("\r\n",$elements[0]);
    669 
     655               
    670656        // return the links
    671657        return $match;
    672658    }
    673659
    674 
    675 
     660   
     661   
    676662/*======================================================================*\
    677663    Function:   _striptext
     
    683669    function _striptext($document)
    684670    {
    685 
     671       
    686672        // I didn't use preg eval (//e) since that is only available in PHP 4.0.
    687673        // so, list your entities one by one here. I included some of the
    688674        // more common ones.
    689 
     675                               
    690676        $search = array("'<script[^>]*?>.*?</script>'si",   // strip out javascript
    691677                        "'<[\/\!]*?[^<>]*?>'si",            // strip out html tags
     
    736722                            "ß",
    737723                        );
    738 
     724                   
    739725        $text = preg_replace($search,$replace,$document);
    740 
     726                               
    741727        return $text;
    742728    }
     
    752738    function _expandlinks($links,$URI)
    753739    {
    754 
     740       
    755741        preg_match("/^[^\?]+/",$URI,$match);
    756742
     
    760746        $match_root =
    761747        $match_part["scheme"]."://".$match_part["host"];
    762 
     748               
    763749        $search = array(    "|^http://".preg_quote($this->host)."|i",
    764750                            "|^(\/)|i",
     
    767753                            "|/[^\/]+/\.\./|"
    768754                        );
    769 
     755                       
    770756        $replace = array(   "",
    771757                            $match_root."/",
     
    773759                            "/",
    774760                            "/"
    775                         );
    776 
     761                        );         
     762               
    777763        $expandedLinks = preg_replace($search,$replace,$links);
    778764
     
    787773                $URI        the full URI
    788774                $body       body contents to send if any (POST)
    789     Output:
    790 \*======================================================================*/
    791 
     775    Output:     
     776\*======================================================================*/
     777   
    792778    function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
    793779    {
     
    795781        if($this->passcookies && $this->_redirectaddr)
    796782            $this->setcookies();
    797 
     783           
    798784        $URI_PARTS = parse_url($URI);
    799785        if(empty($url))
    800786            $url = "/";
    801         $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
     787        $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";       
    802788        if(!empty($this->agent))
    803789            $headers .= "User-Agent: ".$this->agent."\r\n";
    804790        if(!empty($this->host) && !isset($this->rawheaders['Host'])) {
    805791            $headers .= "Host: ".$this->host;
    806             if(!empty($this->port) && $this->port != 80)
     792            if(!empty($this->port))
    807793                $headers .= ":".$this->port;
    808794            $headers .= "\r\n";
     
    813799            $headers .= "Referer: ".$this->referer."\r\n";
    814800        if(!empty($this->cookies))
    815         {
     801        {           
    816802            if(!is_array($this->cookies))
    817803                $this->cookies = (array)$this->cookies;
    818 
     804   
    819805            reset($this->cookies);
    820806            if ( count($this->cookies) > 0 ) {
     
    824810                }
    825811                $headers .= substr($cookie_headers,0,-2) . "\r\n";
    826             }
     812            } 
    827813        }
    828814        if(!empty($this->rawheaders))
     
    839825            $headers .= "\r\n";
    840826        }
    841         if(!empty($body))
     827        if(!empty($body))   
    842828            $headers .= "Content-length: ".strlen($body)."\r\n";
    843         if(!empty($this->user) || !empty($this->pass))
     829        if(!empty($this->user) || !empty($this->pass)) 
    844830            $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n";
    845 
     831       
    846832        //add proxy auth headers
    847         if(!empty($this->proxy_user))
     833        if(!empty($this->proxy_user))   
    848834            $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n";
    849835
    850836
    851837        $headers .= "\r\n";
    852 
     838       
    853839        // set the read timeout if needed
    854840        if ($this->read_timeout > 0)
    855841            socket_set_timeout($fp, $this->read_timeout);
    856842        $this->timed_out = false;
    857 
     843       
    858844        fwrite($fp,$headers.$body,strlen($headers.$body));
    859 
     845       
    860846        $this->_redirectaddr = false;
    861847        unset($this->headers);
    862 
     848                       
    863849        while($currentHeader = fgets($fp,$this->_maxlinelen))
    864850        {
     
    868854                return false;
    869855            }
    870 
     856               
    871857            if($currentHeader == "\r\n")
    872858                break;
    873 
     859                       
    874860            // if a header begins with Location: or URI:, set the redirect
    875861            if(preg_match("/^(Location:|URI:)/i",$currentHeader))
     
    891877                    $this->_redirectaddr = $matches[2];
    892878            }
    893 
     879       
    894880            if(preg_match("|^HTTP/|",$currentHeader))
    895881            {
     
    897883                {
    898884                    $this->status= $status[1];
    899                 }
     885                }               
    900886                $this->response_code = $currentHeader;
    901887            }
    902 
     888               
    903889            $this->headers[] = $currentHeader;
    904890        }
     
    918904            return false;
    919905        }
    920 
     906       
    921907        // check if there is a a redirect meta tag
    922 
     908       
    923909        if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
    924910
    925911        {
    926             $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
     912            $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 
    927913        }
    928914
     
    940926        else
    941927            $this->results = $results;
    942 
     928       
    943929        return true;
    944930    }
     
    950936                $URI        the full URI
    951937                $body       body contents to send if any (POST)
    952     Output:
    953 \*======================================================================*/
    954 
     938    Output:     
     939\*======================================================================*/
     940   
    955941    function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
    956     {
     942    { 
    957943        if($this->passcookies && $this->_redirectaddr)
    958944            $this->setcookies();
    959945
    960         $headers = array();
    961 
     946        $headers = array();     
     947                   
    962948        $URI_PARTS = parse_url($URI);
    963949        if(empty($url))
    964950            $url = "/";
    965951        // GET ... header not needed for curl
    966         //$headers[] = $http_method." ".$url." ".$this->_httpversion;
     952        //$headers[] = $http_method." ".$url." ".$this->_httpversion;       
    967953        if(!empty($this->agent))
    968954            $headers[] = "User-Agent: ".$this->agent;
     
    977963            $headers[] = "Referer: ".$this->referer;
    978964        if(!empty($this->cookies))
    979         {
     965        {           
    980966            if(!is_array($this->cookies))
    981967                $this->cookies = (array)$this->cookies;
    982 
     968   
    983969            reset($this->cookies);
    984970            if ( count($this->cookies) > 0 ) {
     
    1003989                $headers[] = "Content-type: $content_type";
    1004990        }
    1005         if(!empty($body))
     991        if(!empty($body))   
    1006992            $headers[] = "Content-length: ".strlen($body);
    1007         if(!empty($this->user) || !empty($this->pass))
     993        if(!empty($this->user) || !empty($this->pass)) 
    1008994            $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
    1009 
     995           
    1010996        for($curr_header = 0; $curr_header < count($headers); $curr_header++) {
    1011997            $safer_header = strtr( $headers[$curr_header], "\"", " " );
    1012998            $cmdline_params .= " -H \"".$safer_header."\"";
    1013999        }
    1014 
     1000       
    10151001        if(!empty($body))
    10161002            $cmdline_params .= " -d \"$body\"";
    1017 
     1003       
    10181004        if($this->read_timeout > 0)
    10191005            $cmdline_params .= " -m ".$this->read_timeout;
    1020 
     1006       
    10211007        $headerfile = tempnam($temp_dir, "sno");
    10221008
    1023         $safer_URI = strtr( $URI, "\"", " " ); // strip quotes from the URI to avoid shell access
    1024         exec(escapeshellcmd($this->curl_path." -D \"$headerfile\"".$cmdline_params." \"".$safer_URI."\""),$results,$return);
    1025 
     1009        exec($this->curl_path." -k -D \"$headerfile\"".$cmdline_params." \"".escapeshellcmd($URI)."\"",$results,$return);
     1010       
    10261011        if($return)
    10271012        {
     
    10291014            return false;
    10301015        }
    1031 
    1032 
     1016           
     1017           
    10331018        $results = implode("\r\n",$results);
    1034 
     1019       
    10351020        $result_headers = file("$headerfile");
    1036 
     1021                       
    10371022        $this->_redirectaddr = false;
    10381023        unset($this->headers);
    1039 
     1024                       
    10401025        for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
    10411026        {
    1042 
     1027           
    10431028            // if a header begins with Location: or URI:, set the redirect
    10441029            if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
     
    10601045                    $this->_redirectaddr = $matches[2];
    10611046            }
    1062 
     1047       
    10631048            if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
    10641049                $this->response_code = $result_headers[$currentHeader];
     
    10681053
    10691054        // check if there is a a redirect meta tag
    1070 
     1055       
    10711056        if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
    10721057        {
    1073             $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
     1058            $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 
    10741059        }
    10751060
     
    10891074
    10901075        unlink("$headerfile");
    1091 
     1076       
    10921077        return true;
    10931078    }
     
    10971082    Purpose:    set cookies for a redirection
    10981083\*======================================================================*/
    1099 
     1084   
    11001085    function setcookies()
    11011086    {
     
    11071092    }
    11081093
    1109 
     1094   
    11101095/*======================================================================*\
    11111096    Function:   _check_timeout
     
    11311116    Input:      $fp file pointer
    11321117\*======================================================================*/
    1133 
     1118   
    11341119    function _connect(&$fp)
    11351120    {
     
    11371122            {
    11381123                $this->_isproxy = true;
    1139 
     1124               
    11401125                $host = $this->proxy_host;
    11411126                $port = $this->proxy_port;
     
    11461131            $port = $this->port;
    11471132        }
    1148 
     1133   
    11491134        $this->status = 0;
    1150 
     1135       
    11511136        if($fp = fsockopen(
    11521137                    $host,
     
    11841169    Input:      $fp file pointer
    11851170\*======================================================================*/
    1186 
     1171   
    11871172    function _disconnect($fp)
    11881173    {
     
    11901175    }
    11911176
    1192 
     1177   
    11931178/*======================================================================*\
    11941179    Function:   _prepare_post_body
     
    11981183    Output:     post body
    11991184\*======================================================================*/
    1200 
     1185   
    12011186    function _prepare_post_body($formvars, $formfiles)
    12021187    {
     
    12071192        if (count($formvars) == 0 && count($formfiles) == 0)
    12081193            return;
    1209 
     1194       
    12101195        switch ($this->_submit_type) {
    12111196            case "application/x-www-form-urlencoded":
     
    12231208            case "multipart/form-data":
    12241209                $this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
    1225 
     1210               
    12261211                reset($formvars);
    12271212                while(list($key,$val) = each($formvars)) {
     
    12381223                    }
    12391224                }
    1240 
     1225               
    12411226                reset($formfiles);
    12421227                while (list($field_name, $file_names) = each($formfiles)) {
     
    12461231
    12471232                        $fp = fopen($file_name, "r");
    1248                         while (!feof($fp)) {
    1249                             $file_content .= fread($fp, filesize($file_name));
    1250                         }
     1233                        $file_content = fread($fp, filesize($file_name));
    12511234                        fclose($fp);
    12521235                        $base_name = basename($file_name);
     
    12641247    }
    12651248}
    1266 endif;
    12671249
    12681250?>
Note: See TracChangeset for help on using the changeset viewer.