Make WordPress Core


Ignore:
Timestamp:
02/12/2006 07:53:23 AM (19 years ago)
Author:
ryan
Message:

Death to trailing tabs. Props Mark J. fixes #2405

File:
1 edited

Legend:

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

    r3235 r3517  
    4040{
    4141    /**** Public variables ****/
    42    
     42
    4343    /* user definable vars */
    4444
     
    4949    var $proxy_user     =   "";                 // proxy user to use
    5050    var $proxy_pass     =   "";                 // proxy password to use
    51    
     51
    5252    var $agent          =   "Snoopy v1.2.3";    // agent we masquerade as
    5353    var $referer        =   "";                 // referer info to pass
     
    6767                                                // NOTE: this currently does not respect
    6868                                                // dates, domains or paths.
    69    
     69
    7070    var $user           =   "";                 // user for http authentication
    7171    var $pass           =   "";                 // password for http authentication
    72    
     72
    7373    // http accept types
    7474    var $accept         =   "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
    75    
     75
    7676    var $results        =   "";                 // where the content is put
    77        
     77
    7878    var $error          =   "";                 // error messages sent here
    7979    var $response_code  =   "";                 // response code returned from server
     
    101101                                                // as these functions are not stable
    102102                                                // as of this Snoopy release.
    103    
    104     /**** Private variables ****/   
    105    
     103
     104    /**** Private variables ****/
     105
    106106    var $_maxlinelen    =   4096;               // max line length (headers)
    107    
     107
    108108    var $_httpmethod    =   "GET";              // default http request method
    109109    var $_httpversion   =   "HTTP/1.0";         // default http request version
     
    115115    var $_frameurls     =   array();            // frame src urls
    116116    var $_framedepth    =   0;                  // increments on frame depth
    117    
     117
    118118    var $_isproxy       =   false;              // set if using a proxy server
    119119    var $_fp_timeout    =   30;                 // timeout for socket connection
     
    130130    function fetch($URI)
    131131    {
    132    
     132
    133133        //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
    134134        $URI_PARTS = parse_url($URI);
     
    141141        if (empty($URI_PARTS["path"]))
    142142            $URI_PARTS["path"] = '';
    143                
     143
    144144        switch(strtolower($URI_PARTS["scheme"]))
    145145        {
     
    161161                        $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
    162162                    }
    163                    
     163
    164164                    $this->_disconnect($fp);
    165165
     
    184184                        $frameurls = $this->_frameurls;
    185185                        $this->_frameurls = array();
    186                        
     186
    187187                        while(list(,$frameurl) = each($frameurls))
    188188                        {
     
    195195                                break;
    196196                        }
    197                     }                   
     197                    }
    198198                }
    199199                else
     
    201201                    return false;
    202202                }
    203                 return true;                   
     203                return true;
    204204                break;
    205205            case "https":
     
    255255                            break;
    256256                    }
    257                 }                   
    258                 return true;                   
     257                }
     258                return true;
    259259                break;
    260260            default:
     
    263263                return false;
    264264                break;
    265         }       
     265        }
    266266        return true;
    267267    }
     
    281281    {
    282282        unset($postdata);
    283        
     283
    284284        $postdata = $this->_prepare_post_body($formvars, $formfiles);
    285            
     285
    286286        $URI_PARTS = parse_url($URI);
    287287        if (!empty($URI_PARTS["user"]))
     
    313313                        $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
    314314                    }
    315                    
     315
    316316                    $this->_disconnect($fp);
    317317
     
    320320                        /* url was redirected, check if we've hit the max depth */
    321321                        if($this->maxredirs > $this->_redirectdepth)
    322                         {                       
     322                        {
    323323                            if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
    324                                 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);                     
    325                            
     324                                $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
     325
    326326                            // only follow redirect if it's on this site, or offsiteok is true
    327327                            if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
     
    342342                        $frameurls = $this->_frameurls;
    343343                        $this->_frameurls = array();
    344                        
     344
    345345                        while(list(,$frameurl) = each($frameurls))
    346                         {                                                       
     346                        {
    347347                            if($this->_framedepth < $this->maxframes)
    348348                            {
     
    353353                                break;
    354354                        }
    355                     }                   
    356                    
     355                    }
     356
    357357                }
    358358                else
     
    360360                    return false;
    361361                }
    362                 return true;                   
     362                return true;
    363363                break;
    364364            case "https":
     
    387387                    /* url was redirected, check if we've hit the max depth */
    388388                    if($this->maxredirs > $this->_redirectdepth)
    389                     {                       
     389                    {
    390390                        if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
    391                             $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);                     
     391                            $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
    392392
    393393                        // only follow redirect if it's on this site, or offsiteok is true
     
    411411
    412412                    while(list(,$frameurl) = each($frameurls))
    413                     {                                                       
     413                    {
    414414                        if($this->_framedepth < $this->maxframes)
    415415                        {
     
    420420                            break;
    421421                    }
    422                 }                   
    423                 return true;                   
     422                }
     423                return true;
    424424                break;
    425                
     425
    426426            default:
    427427                // not a valid protocol
     
    429429                return false;
    430430                break;
    431         }       
     431        }
    432432        return true;
    433433    }
     
    443443    {
    444444        if ($this->fetch($URI))
    445         {           
     445        {
    446446            if($this->lastredirectaddr)
    447447                $URI = $this->lastredirectaddr;
     
    471471    function fetchform($URI)
    472472    {
    473        
     473
    474474        if ($this->fetch($URI))
    475         {           
     475        {
    476476
    477477            if(is_array($this->results))
     
    482482            else
    483483                $this->results = $this->_stripform($this->results);
    484            
     484
    485485            return true;
    486486        }
     
    488488            return false;
    489489    }
    490    
    491    
     490
     491
    492492/*======================================================================*\
    493493    Function:   fetchtext
     
    500500    {
    501501        if($this->fetch($URI))
    502         {           
     502        {
    503503            if(is_array($this->results))
    504504            {
     
    524524    {
    525525        if($this->submit($URI,$formvars, $formfiles))
    526         {           
     526        {
    527527            if($this->lastredirectaddr)
    528528                $URI = $this->lastredirectaddr;
     
    558558    {
    559559        if($this->submit($URI,$formvars, $formfiles))
    560         {           
     560        {
    561561            if($this->lastredirectaddr)
    562562                $URI = $this->lastredirectaddr;
     
    582582    }
    583583
    584    
     584
    585585
    586586/*======================================================================*\
     
    594594    }
    595595
    596    
     596
    597597/*======================================================================*\
    598598    Function:   set_submit_normal
     
    605605    }
    606606
    607    
    608    
     607
     608
    609609
    610610/*======================================================================*\
    611611    Private functions
    612612\*======================================================================*/
    613    
    614    
     613
     614
    615615/*======================================================================*\
    616616    Function:   _striplinks
     
    621621
    622622    function _striplinks($document)
    623     {   
     623    {
    624624        preg_match_all("'<\s*a\s.*?href\s*=\s*          # find <a href=
    625625                        ([\"\'])?                   # find single or double quote
     
    627627                                                    # quote, otherwise match up to next space
    628628                        'isx",$document,$links);
    629                        
     629
    630630
    631631        // catenate the non-empty matches from the conditional subpattern
     
    635635            if(!empty($val))
    636636                $match[] = $val;
    637         }               
    638        
     637        }
     638
    639639        while(list($key,$val) = each($links[3]))
    640640        {
    641641            if(!empty($val))
    642642                $match[] = $val;
    643         }       
    644        
     643        }
     644
    645645        // return the links
    646646        return $match;
     
    655655
    656656    function _stripform($document)
    657     {   
     657    {
    658658        preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
    659        
     659
    660660        // catenate the matches
    661661        $match = implode("\r\n",$elements[0]);
    662                
     662
    663663        // return the links
    664664        return $match;
    665665    }
    666666
    667    
    668    
     667
     668
    669669/*======================================================================*\
    670670    Function:   _striptext
     
    676676    function _striptext($document)
    677677    {
    678        
     678
    679679        // I didn't use preg eval (//e) since that is only available in PHP 4.0.
    680680        // so, list your entities one by one here. I included some of the
    681681        // more common ones.
    682                                
     682
    683683        $search = array("'<script[^>]*?>.*?</script>'si",   // strip out javascript
    684684                        "'<[\/\!]*?[^<>]*?>'si",            // strip out html tags
     
    729729                            "ß",
    730730                        );
    731                    
     731
    732732        $text = preg_replace($search,$replace,$document);
    733                                
     733
    734734        return $text;
    735735    }
     
    745745    function _expandlinks($links,$URI)
    746746    {
    747        
     747
    748748        preg_match("/^[^\?]+/",$URI,$match);
    749749
     
    753753        $match_root =
    754754        $match_part["scheme"]."://".$match_part["host"];
    755                
     755
    756756        $search = array(    "|^http://".preg_quote($this->host)."|i",
    757757                            "|^(\/)|i",
     
    760760                            "|/[^\/]+/\.\./|"
    761761                        );
    762                        
     762
    763763        $replace = array(   "",
    764764                            $match_root."/",
     
    766766                            "/",
    767767                            "/"
    768                         );         
    769                
     768                        );
     769
    770770        $expandedLinks = preg_replace($search,$replace,$links);
    771771
     
    780780                $URI        the full URI
    781781                $body       body contents to send if any (POST)
    782     Output:     
    783 \*======================================================================*/
    784    
     782    Output:
     783\*======================================================================*/
     784
    785785    function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
    786786    {
     
    788788        if($this->passcookies && $this->_redirectaddr)
    789789            $this->setcookies();
    790            
     790
    791791        $URI_PARTS = parse_url($URI);
    792792        if(empty($url))
    793793            $url = "/";
    794         $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";       
     794        $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
    795795        if(!empty($this->agent))
    796796            $headers .= "User-Agent: ".$this->agent."\r\n";
     
    806806            $headers .= "Referer: ".$this->referer."\r\n";
    807807        if(!empty($this->cookies))
    808         {           
     808        {
    809809            if(!is_array($this->cookies))
    810810                $this->cookies = (array)$this->cookies;
    811    
     811
    812812            reset($this->cookies);
    813813            if ( count($this->cookies) > 0 ) {
     
    832832            $headers .= "\r\n";
    833833        }
    834         if(!empty($body))   
     834        if(!empty($body))
    835835            $headers .= "Content-length: ".strlen($body)."\r\n";
    836         if(!empty($this->user) || !empty($this->pass)) 
     836        if(!empty($this->user) || !empty($this->pass))
    837837            $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n";
    838        
     838
    839839        //add proxy auth headers
    840         if(!empty($this->proxy_user))   
     840        if(!empty($this->proxy_user))
    841841            $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n";
    842842
    843843
    844844        $headers .= "\r\n";
    845        
     845
    846846        // set the read timeout if needed
    847847        if ($this->read_timeout > 0)
    848848            socket_set_timeout($fp, $this->read_timeout);
    849849        $this->timed_out = false;
    850        
     850
    851851        fwrite($fp,$headers.$body,strlen($headers.$body));
    852        
     852
    853853        $this->_redirectaddr = false;
    854854        unset($this->headers);
    855                        
     855
    856856        while($currentHeader = fgets($fp,$this->_maxlinelen))
    857857        {
     
    861861                return false;
    862862            }
    863                
     863
    864864            if($currentHeader == "\r\n")
    865865                break;
    866                        
     866
    867867            // if a header begins with Location: or URI:, set the redirect
    868868            if(preg_match("/^(Location:|URI:)/i",$currentHeader))
     
    884884                    $this->_redirectaddr = $matches[2];
    885885            }
    886        
     886
    887887            if(preg_match("|^HTTP/|",$currentHeader))
    888888            {
     
    890890                {
    891891                    $this->status= $status[1];
    892                 }               
     892                }
    893893                $this->response_code = $currentHeader;
    894894            }
    895                
     895
    896896            $this->headers[] = $currentHeader;
    897897        }
     
    911911            return false;
    912912        }
    913        
     913
    914914        // check if there is a a redirect meta tag
    915        
     915
    916916        if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
    917917
    918918        {
    919             $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 
     919            $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
    920920        }
    921921
     
    933933        else
    934934            $this->results = $results;
    935        
     935
    936936        return true;
    937937    }
     
    943943                $URI        the full URI
    944944                $body       body contents to send if any (POST)
    945     Output:     
    946 \*======================================================================*/
    947    
     945    Output:
     946\*======================================================================*/
     947
    948948    function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
    949949    {
     
    951951            $this->setcookies();
    952952
    953         $headers = array();     
    954                    
     953        $headers = array();
     954
    955955        $URI_PARTS = parse_url($URI);
    956956        if(empty($url))
    957957            $url = "/";
    958958        // GET ... header not needed for curl
    959         //$headers[] = $http_method." ".$url." ".$this->_httpversion;       
     959        //$headers[] = $http_method." ".$url." ".$this->_httpversion;
    960960        if(!empty($this->agent))
    961961            $headers[] = "User-Agent: ".$this->agent;
     
    970970            $headers[] = "Referer: ".$this->referer;
    971971        if(!empty($this->cookies))
    972         {           
     972        {
    973973            if(!is_array($this->cookies))
    974974                $this->cookies = (array)$this->cookies;
    975    
     975
    976976            reset($this->cookies);
    977977            if ( count($this->cookies) > 0 ) {
     
    996996                $headers[] = "Content-type: $content_type";
    997997        }
    998         if(!empty($body))   
     998        if(!empty($body))
    999999            $headers[] = "Content-length: ".strlen($body);
    1000         if(!empty($this->user) || !empty($this->pass)) 
     1000        if(!empty($this->user) || !empty($this->pass))
    10011001            $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
    1002            
     1002
    10031003        for($curr_header = 0; $curr_header < count($headers); $curr_header++) {
    10041004            $safer_header = strtr( $headers[$curr_header], "\"", " " );
    10051005            $cmdline_params .= " -H \"".$safer_header."\"";
    10061006        }
    1007        
     1007
    10081008        if(!empty($body))
    10091009            $cmdline_params .= " -d \"$body\"";
    1010        
     1010
    10111011        if($this->read_timeout > 0)
    10121012            $cmdline_params .= " -m ".$this->read_timeout;
    1013        
     1013
    10141014        $headerfile = tempnam($temp_dir, "sno");
    10151015
    10161016        $safer_URI = strtr( $URI, "\"", " " ); // strip quotes from the URI to avoid shell access
    10171017        exec(escapeshellcmd($this->curl_path." -D \"$headerfile\"".$cmdline_params." \"".$safer_URI."\""),$results,$return);
    1018        
     1018
    10191019        if($return)
    10201020        {
     
    10221022            return false;
    10231023        }
    1024            
    1025            
     1024
     1025
    10261026        $results = implode("\r\n",$results);
    1027        
     1027
    10281028        $result_headers = file("$headerfile");
    1029                        
     1029
    10301030        $this->_redirectaddr = false;
    10311031        unset($this->headers);
    1032                        
     1032
    10331033        for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
    10341034        {
    1035            
     1035
    10361036            // if a header begins with Location: or URI:, set the redirect
    10371037            if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
     
    10531053                    $this->_redirectaddr = $matches[2];
    10541054            }
    1055        
     1055
    10561056            if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
    10571057                $this->response_code = $result_headers[$currentHeader];
     
    10611061
    10621062        // check if there is a a redirect meta tag
    1063        
     1063
    10641064        if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
    10651065        {
    1066             $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 
     1066            $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
    10671067        }
    10681068
     
    10821082
    10831083        unlink("$headerfile");
    1084        
     1084
    10851085        return true;
    10861086    }
     
    10901090    Purpose:    set cookies for a redirection
    10911091\*======================================================================*/
    1092    
     1092
    10931093    function setcookies()
    10941094    {
     
    11001100    }
    11011101
    1102    
     1102
    11031103/*======================================================================*\
    11041104    Function:   _check_timeout
     
    11241124    Input:      $fp file pointer
    11251125\*======================================================================*/
    1126    
     1126
    11271127    function _connect(&$fp)
    11281128    {
     
    11301130            {
    11311131                $this->_isproxy = true;
    1132                
     1132
    11331133                $host = $this->proxy_host;
    11341134                $port = $this->proxy_port;
     
    11391139            $port = $this->port;
    11401140        }
    1141    
     1141
    11421142        $this->status = 0;
    1143        
     1143
    11441144        if($fp = fsockopen(
    11451145                    $host,
     
    11771177    Input:      $fp file pointer
    11781178\*======================================================================*/
    1179    
     1179
    11801180    function _disconnect($fp)
    11811181    {
     
    11831183    }
    11841184
    1185    
     1185
    11861186/*======================================================================*\
    11871187    Function:   _prepare_post_body
     
    11911191    Output:     post body
    11921192\*======================================================================*/
    1193    
     1193
    11941194    function _prepare_post_body($formvars, $formfiles)
    11951195    {
     
    12001200        if (count($formvars) == 0 && count($formfiles) == 0)
    12011201            return;
    1202        
     1202
    12031203        switch ($this->_submit_type) {
    12041204            case "application/x-www-form-urlencoded":
     
    12161216            case "multipart/form-data":
    12171217                $this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
    1218                
     1218
    12191219                reset($formvars);
    12201220                while(list($key,$val) = each($formvars)) {
     
    12311231                    }
    12321232                }
    1233                
     1233
    12341234                reset($formfiles);
    12351235                while (list($field_name, $file_names) = each($formfiles)) {
Note: See TracChangeset for help on using the changeset viewer.