Ticket #16236: 16236.6.diff
| File 16236.6.diff, 9.7 KB (added by , 15 years ago) |
|---|
-
wp-includes/class-http.php
227 227 'body' => null, 228 228 'compress' => false, 229 229 'decompress' => true, 230 'sslverify' => true 230 'sslverify' => true, 231 'stream' => false, 232 'filename' => null 231 233 ); 232 234 233 235 $r = wp_parse_args( $args, $defaults ); … … 255 257 $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host']; 256 258 unset( $homeURL ); 257 259 260 if ( $r['stream'] && ! $r['filename'] ) 261 $r['filename'] = get_temp_dir() . basename( $url ); 262 263 if ( $r['stream'] ) { 264 $r['blocking'] = true; 265 if ( ! is_dir( dirname( $r['filename'] ) ) || ! is_writable( dirname( $r['filename'] ) ) ) 266 return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) ); 267 } 268 258 269 if ( is_null( $r['headers'] ) ) 259 270 $r['headers'] = array(); 260 271 … … 748 759 } 749 760 750 761 $strResponse = ''; 751 while ( ! feof($handle) ) 752 $strResponse .= fread($handle, 4096); 762 $bodyStarted = false; 753 763 754 fclose($handle); 764 if ( $r['stream'] ) 765 $stream_handle = fopen( $r['filename'], 'w+' ); 755 766 767 while ( ! feof($handle) ) { 768 if ( $r['stream'] ) { 769 $block = fread( $handle, 4096 ); 770 if ( $bodyStarted ) { 771 fwrite( $stream_handle, $block ); 772 } else { 773 $strResponse .= $block; 774 if ( strpos( $strResponse, "\r\n\r\n" ) ) { 775 $process = WP_Http::processResponse( $strResponse ); 776 $bodyStarted = true; 777 fwrite( $stream_handle, $process['body'] ); 778 unset( $strResponse ); 779 $process['body'] = ''; 780 } 781 } 782 } else { 783 $strResponse .= fread( $handle, 4096 ); 784 } 785 } 786 787 fclose( $handle ); 788 756 789 if ( true === $secure_transport ) 757 790 error_reporting($error_reporting); 758 791 759 $process = WP_Http::processResponse($strResponse); 760 $arrHeaders = WP_Http::processHeaders($process['headers']); 792 if ( $r['stream'] ) { 793 fclose( $stream_handle ); 794 $arrHeaders = WP_Http::processHeaders( $process['headers'] ); 795 } else { 796 $process = WP_Http::processResponse( $strResponse ); 797 $arrHeaders = WP_Http::processHeaders( $process['headers'] ); 798 unset( $strResponse ); 799 } 761 800 762 801 // Is the response code within the 400 range? 763 802 if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 ) … … 779 818 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) ) 780 819 $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); 781 820 782 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'] );821 return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename']); 783 822 } 784 823 785 824 /** … … 1075 1114 return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 1076 1115 } 1077 1116 1078 $strResponse = stream_get_contents($handle); 1079 $meta = stream_get_meta_data($handle); 1117 if ( $r['stream'] ) { 1118 $stream_handle = fopen( $r['filename'], 'w+' ); 1119 stream_copy_to_stream( $handle, $stream_handle ); 1120 fclose( $stream_handle ); 1121 $strResponse = ''; 1122 } else { 1123 $strResponse = stream_get_contents( $handle ); 1124 } 1125 $meta = stream_get_meta_data( $handle ); 1080 1126 1081 fclose( $handle);1127 fclose( $handle ); 1082 1128 1083 1129 $processedHeaders = array(); 1084 1130 if ( isset( $meta['wrapper_data']['headers'] ) ) … … 1092 1138 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) ) 1093 1139 $strResponse = WP_Http_Encoding::decompress( $strResponse ); 1094 1140 1095 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'] );1141 return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename']); 1096 1142 } 1097 1143 1098 1144 /** … … 1291 1337 */ 1292 1338 class WP_Http_Curl { 1293 1339 1340 private $headers; 1341 1294 1342 /** 1295 1343 * Send a HTTP request to a URI using cURL extension. 1296 1344 * … … 1387 1435 else 1388 1436 curl_setopt( $handle, CURLOPT_HEADER, false ); 1389 1437 1438 if ( $r['stream'] ) { 1439 $stream_handle = fopen( $r['filename'], 'w+' ); 1440 curl_setopt( $handle, CURLOPT_FILE, $stream_handle ); 1441 curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) ); 1442 } 1443 1390 1444 // The option doesn't work with safe mode or when open_basedir is set. 1391 1445 // Disable HEAD when making HEAD requests. 1392 1446 if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] ) … … 1420 1474 $theResponse = curl_exec( $handle ); 1421 1475 1422 1476 if ( !empty($theResponse) ) { 1423 $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); 1424 $theHeaders = trim( substr($theResponse, 0, $headerLength) ); 1425 if ( strlen($theResponse) > $headerLength ) 1426 $theBody = substr( $theResponse, $headerLength ); 1427 else 1477 if ( ! empty( $this->headers ) ) { 1478 $theHeaders = $this->headers; 1428 1479 $theBody = ''; 1429 if ( false !== strpos($theHeaders, "\r\n\r\n") ) { 1430 $headerParts = explode("\r\n\r\n", $theHeaders); 1431 $theHeaders = $headerParts[ count($headerParts) -1 ]; 1480 } else { 1481 $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); 1482 $theHeaders = trim( substr($theResponse, 0, $headerLength) ); 1483 if ( strlen($theResponse) > $headerLength ) 1484 $theBody = substr( $theResponse, $headerLength ); 1485 else 1486 $theBody = ''; 1487 if ( false !== strpos($theHeaders, "\r\n\r\n") ) { 1488 $headerParts = explode("\r\n\r\n", $theHeaders); 1489 $theHeaders = $headerParts[ count($headerParts) - 1 ]; 1490 } 1432 1491 } 1433 1492 $theHeaders = WP_Http::processHeaders($theHeaders); 1434 1493 } else { … … 1447 1506 1448 1507 curl_close( $handle ); 1449 1508 1509 if ( $r['stream'] ) 1510 fclose( $stream_handle ); 1511 1450 1512 // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually. 1451 1513 if ( !empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) ) { 1452 1514 if ( $r['redirection']-- > 0 ) { … … 1459 1521 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) ) 1460 1522 $theBody = WP_Http_Encoding::decompress( $theBody ); 1461 1523 1462 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'] );1524 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename']); 1463 1525 } 1464 1526 1527 function stream_headers( $handle, $headers ) { 1528 $this->headers .= $headers; 1529 return strlen($headers); 1530 } 1531 1465 1532 /** 1466 1533 * Whether this class can be used for retrieving an URL. 1467 1534 * -
wp-includes/functions.php
2111 2111 } 2112 2112 2113 2113 /** 2114 * Determines a writable directory for temporary files. 2115 * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/ 2116 * 2117 * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file. 2118 * 2119 * @since 2.5.0 2120 * 2121 * @return string Writable temporary directory 2122 */ 2123 function get_temp_dir() { 2124 static $temp; 2125 if ( defined('WP_TEMP_DIR') ) 2126 return trailingslashit(WP_TEMP_DIR); 2127 2128 if ( $temp ) 2129 return trailingslashit($temp); 2130 2131 $temp = WP_CONTENT_DIR . '/'; 2132 if ( is_dir($temp) && @is_writable($temp) ) 2133 return $temp; 2134 2135 if ( function_exists('sys_get_temp_dir') ) { 2136 $temp = sys_get_temp_dir(); 2137 if ( @is_writable($temp) ) 2138 return trailingslashit($temp); 2139 } 2140 2141 $temp = ini_get('upload_tmp_dir'); 2142 if ( is_dir($temp) && @is_writable($temp) ) 2143 return trailingslashit($temp); 2144 2145 $temp = '/tmp/'; 2146 return $temp; 2147 } 2148 2149 /** 2114 2150 * Get an array containing the current upload directory's path and url. 2115 2151 * 2116 2152 * Checks the 'upload_path' option, which should be from the web root folder, -
wp-admin/includes/file.php
153 153 } 154 154 155 155 /** 156 * Determines a writable directory for temporary files.157 * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/158 *159 * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file.160 *161 * @since 2.5.0162 *163 * @return string Writable temporary directory164 */165 function get_temp_dir() {166 static $temp;167 if ( defined('WP_TEMP_DIR') )168 return trailingslashit(WP_TEMP_DIR);169 170 if ( $temp )171 return trailingslashit($temp);172 173 $temp = WP_CONTENT_DIR . '/';174 if ( is_dir($temp) && @is_writable($temp) )175 return $temp;176 177 if ( function_exists('sys_get_temp_dir') ) {178 $temp = sys_get_temp_dir();179 if ( @is_writable($temp) )180 return trailingslashit($temp);181 }182 183 $temp = ini_get('upload_tmp_dir');184 if ( is_dir($temp) && @is_writable($temp) )185 return trailingslashit($temp);186 187 $temp = '/tmp/';188 return $temp;189 }190 191 /**192 156 * Returns a filename of a Temporary unique file. 193 157 * Please note that the calling function must unlink() this itself. 194 158 *