Make WordPress Core

Ticket #4137: 4137-functions-curl.patch

File 4137-functions-curl.patch, 2.2 KB (added by pishmishy, 16 years ago)

potential fix for issue when curl is used?

  • wp-includes/functions.php

     
    661661        return $array;
    662662}
    663663
     664function wp_limited_curl($url) {
     665        /* This function is a wrapper for curl
     666         * that limits the amount of data we
     667         * fetch from a URI to avoid DOS problems
     668         * with wp_remote_fopen()
     669         */
     670
     671        $ch = curl_init($url);
     672        global $total;
     673        global $output;
     674        $total = 0;
     675        $output = "";
     676
     677        function read_body($ch, $string) {
     678                $length = strlen($string);
     679                global $total;
     680                global $output;
     681                $total += $length;
     682                $output .= $string;
     683                if ($total > 30720) return -1;
     684                return $length;
     685        }
     686
     687        curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
     688        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
     689        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
     690        curl_exec($ch);
     691        curl_close($ch);
     692
     693        return $output;
     694}
     695
    664696function wp_remote_fopen( $uri ) {
    665         $timeout = 10;
     697        $bytes_limit = 30720;  /* limit on size of source documen bytes, see
     698                                * Errata for pingback specification.
     699                                * http://www.hixie.ch/specs/pingback/pingback
     700                                */
     701        $timeout = 10;
    666702        $parsed_url = @parse_url($uri);
    667703
    668704        if ( !$parsed_url || !is_array($parsed_url) )
     
    678714
    679715                //stream_set_timeout($fp, $timeout); // Requires php 4.3
    680716                $linea = '';
    681                 while( $remote_read = fread($fp, 4096) )
     717                $bytes = 0;
     718                while( $remote_read = fread($fp, 4096) && $bytes < $bytes_limit )
     719                        $bytes = $bytes + 4096;
    682720                        $linea .= $remote_read;
    683721                fclose($fp);
    684722                return $linea;
    685723        } else if ( function_exists('curl_init') ) {
    686                 $handle = curl_init();
    687                 curl_setopt ($handle, CURLOPT_URL, $uri);
    688                 curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
    689                 curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
    690                 curl_setopt ($handle, CURLOPT_TIMEOUT, $timeout);
    691                 $buffer = curl_exec($handle);
    692                 curl_close($handle);
    693                 return $buffer;
     724                return wp_limited_curl($uri);
    694725        } else {
    695726                return false;
    696727        }