Index: wp-includes/functions.php =================================================================== --- wp-includes/functions.php (revision 6145) +++ wp-includes/functions.php (working copy) @@ -690,8 +690,39 @@ return $array; } -function wp_remote_fopen( $uri ) { - $timeout = 10; +function wp_limited_curl($url, $bytes_limit = 30720, $timeout = 10) { + $ch = curl_init($url); + + $GLOBALS['__temp'] = array('total' => 0, 'output' => '', 'bytes_limit' => $bytes_limit); + + function read_body($ch, $string) { + $length = strlen($string); + + $GLOBALS['__temp']['total'] += $length; + $GLOBALS['__temp']['output'] .= $string; + + if ( $GLOBALS['__temp']['total'] > $GLOBALS['__temp']['bytes_limit'] ) + return -1; + + return $length; + } + + curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body'); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); + curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096); + curl_setopt($ch, CURLOPT_RANGE, "0-$bytes_limit"); + curl_exec($ch); + curl_close($ch); + + $output = $GLOBALS['__temp']['output']; + unset($GLOBALS['__temp']); + + return $output; +} + +function wp_remote_fopen( $uri, $bytes_limit = 102400 ) { // 100 KB + $timeout = 10; $parsed_url = @parse_url($uri); if ( !$parsed_url || !is_array($parsed_url) ) @@ -702,24 +733,21 @@ if ( ini_get('allow_url_fopen') ) { $fp = @fopen( $uri, 'r' ); - if ( !$fp ) + if ( !is_resource($fp) ) return false; - //stream_set_timeout($fp, $timeout); // Requires php 4.3 + if ( function_exists('stream_set_timeout') ) + stream_set_timeout($fp, $timeout); // Requires php 4.3 $linea = ''; - while( $remote_read = fread($fp, 4096) ) + $bytes = 0; + while( ( $remote_read = fread($fp, 4096) ) && $bytes < $bytes_limit ) { + $bytes = $bytes + 4096; $linea .= $remote_read; + } fclose($fp); return $linea; } else if ( function_exists('curl_init') ) { - $handle = curl_init(); - curl_setopt ($handle, CURLOPT_URL, $uri); - curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1); - curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1); - curl_setopt ($handle, CURLOPT_TIMEOUT, $timeout); - $buffer = curl_exec($handle); - curl_close($handle); - return $buffer; + return wp_limited_curl($uri, $bytes_limit, $timeout); } else { return false; }