Ticket #16236: 16236.7.diff
| File 16236.7.diff, 12.3 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 we are streaming to a file but no filename was given drop it in the WP temp dir 261 // and pick it's name using the basename of the $url 262 if ( $r['stream'] && ! $r['filename'] ) 263 $r['filename'] = get_temp_dir() . basename( $url ); 264 265 // Force some settings if we are streaming to a file and check for existence and perms of destination directory 266 if ( $r['stream'] ) { 267 $r['blocking'] = true; 268 if ( ! is_dir( dirname( $r['filename'] ) ) || ! is_writable( dirname( $r['filename'] ) ) ) 269 return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) ); 270 } 271 258 272 if ( is_null( $r['headers'] ) ) 259 273 $r['headers'] = array(); 260 274 … … 748 762 } 749 763 750 764 $strResponse = ''; 751 while ( ! feof($handle) ) 752 $strResponse .= fread($handle, 4096); 765 $bodyStarted = false; 753 766 754 fclose($handle); 767 // If streaming to a file setup the file handle 768 if ( $r['stream'] ) 769 $stream_handle = fopen( $r['filename'], 'w+' ); 755 770 771 while ( ! feof($handle) ) { 772 if ( $r['stream'] ) { 773 $block = fread( $handle, 4096 ); 774 if ( $bodyStarted ) { 775 fwrite( $stream_handle, $block ); 776 } else { 777 $strResponse .= $block; 778 if ( strpos( $strResponse, "\r\n\r\n" ) ) { 779 $process = WP_Http::processResponse( $strResponse ); 780 $bodyStarted = true; 781 fwrite( $stream_handle, $process['body'] ); 782 unset( $strResponse ); 783 $process['body'] = ''; 784 } 785 } 786 } else { 787 $strResponse .= fread( $handle, 4096 ); 788 } 789 } 790 791 fclose( $handle ); 792 756 793 if ( true === $secure_transport ) 757 794 error_reporting($error_reporting); 758 795 759 $process = WP_Http::processResponse($strResponse); 760 $arrHeaders = WP_Http::processHeaders($process['headers']); 796 if ( $r['stream'] ) { 797 fclose( $stream_handle ); 798 $arrHeaders = WP_Http::processHeaders( $process['headers'] ); 799 } else { 800 $process = WP_Http::processResponse( $strResponse ); 801 $arrHeaders = WP_Http::processHeaders( $process['headers'] ); 802 unset( $strResponse ); 803 } 761 804 762 805 // Is the response code within the 400 range? 763 806 if ( (int) $arrHeaders['response']['code'] >= 400 && (int) $arrHeaders['response']['code'] < 500 ) … … 779 822 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) ) 780 823 $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); 781 824 782 return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']);825 return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename'] ); 783 826 } 784 827 785 828 /** … … 1075 1118 return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() ); 1076 1119 } 1077 1120 1078 $strResponse = stream_get_contents($handle); 1079 $meta = stream_get_meta_data($handle); 1121 if ( $r['stream'] ) { 1122 $stream_handle = fopen( $r['filename'], 'w+' ); 1123 stream_copy_to_stream( $handle, $stream_handle ); 1124 fclose( $stream_handle ); 1125 $strResponse = ''; 1126 } else { 1127 $strResponse = stream_get_contents( $handle ); 1128 } 1080 1129 1081 fclose($handle);1130 $meta = stream_get_meta_data( $handle ); 1082 1131 1132 fclose( $handle ); 1133 1083 1134 $processedHeaders = array(); 1084 1135 if ( isset( $meta['wrapper_data']['headers'] ) ) 1085 1136 $processedHeaders = WP_Http::processHeaders($meta['wrapper_data']['headers']); … … 1092 1143 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($processedHeaders['headers']) ) 1093 1144 $strResponse = WP_Http_Encoding::decompress( $strResponse ); 1094 1145 1095 return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);1146 return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename'] ); 1096 1147 } 1097 1148 1098 1149 /** … … 1275 1326 * 1276 1327 * @return boolean False means this class can not be used, true means it can. 1277 1328 */ 1278 function test($args = array()) { 1279 return apply_filters('use_http_extension_transport', function_exists('http_request'), $args ); 1329 function test( $args = array() ) { 1330 $use = true; 1331 1332 if ( ! function_exists( 'http_request' ) ) 1333 $use = false; 1334 1335 $stream = isset( $args['stream'] ) && $args['stream']; 1336 1337 if ( $stream ) 1338 $use = false; 1339 1340 return apply_filters( 'use_http_extension_transport', $use, $args ); 1280 1341 } 1281 1342 } 1282 1343 … … 1292 1353 class WP_Http_Curl { 1293 1354 1294 1355 /** 1356 * Temporary header storage for use with streaming to a file. 1357 * 1358 * @since 3.2.0 1359 * @access private 1360 * @var string 1361 */ 1362 private $headers; 1363 1364 /** 1295 1365 * Send a HTTP request to a URI using cURL extension. 1296 1366 * 1297 1367 * @access public … … 1387 1457 else 1388 1458 curl_setopt( $handle, CURLOPT_HEADER, false ); 1389 1459 1460 // If streaming to a file open a file handle, and setup our callback for grabbing the headers 1461 if ( $r['stream'] ) { 1462 $stream_handle = fopen( $r['filename'], 'w+' ); 1463 curl_setopt( $handle, CURLOPT_FILE, $stream_handle ); 1464 curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) ); 1465 } 1466 1390 1467 // The option doesn't work with safe mode or when open_basedir is set. 1391 1468 // Disable HEAD when making HEAD requests. 1392 1469 if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] ) … … 1419 1496 1420 1497 $theResponse = curl_exec( $handle ); 1421 1498 1422 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 1499 if ( ! empty( $theResponse ) ) { 1500 // If we are streaming to a file the $headers property will hold our headers 1501 if ( ! empty( $this->headers ) ) { 1502 $theHeaders = $this->headers; 1503 unset( $this->headers ); 1428 1504 $theBody = ''; 1429 if ( false !== strpos($theHeaders, "\r\n\r\n") ) { 1430 $headerParts = explode("\r\n\r\n", $theHeaders); 1431 $theHeaders = $headerParts[ count($headerParts) -1 ]; 1505 } else { 1506 $headerLength = curl_getinfo( $handle, CURLINFO_HEADER_SIZE ); 1507 $theHeaders = trim( substr( $theResponse, 0, $headerLength ) ); 1508 if ( strlen( $theResponse ) > $headerLength ) 1509 $theBody = substr( $theResponse, $headerLength ); 1510 else 1511 $theBody = ''; 1512 if ( false !== strpos( $theHeaders, "\r\n\r\n" ) ) { 1513 $headerParts = explode( "\r\n\r\n", $theHeaders ); 1514 $theHeaders = $headerParts[ count( $headerParts ) - 1 ]; 1515 } 1432 1516 } 1433 $theHeaders = WP_Http::processHeaders( $theHeaders);1517 $theHeaders = WP_Http::processHeaders( $theHeaders ); 1434 1518 } else { 1435 if ( $curl_error = curl_error( $handle) )1436 return new WP_Error( 'http_request_failed', $curl_error);1437 if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302) ) )1438 return new WP_Error( 'http_request_failed', __('Too many redirects.'));1519 if ( $curl_error = curl_error( $handle ) ) 1520 return new WP_Error( 'http_request_failed', $curl_error ); 1521 if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) 1522 return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) ); 1439 1523 1440 1524 $theHeaders = array( 'headers' => array(), 'cookies' => array() ); 1441 1525 $theBody = ''; … … 1447 1531 1448 1532 curl_close( $handle ); 1449 1533 1534 if ( $r['stream'] ) 1535 fclose( $stream_handle ); 1536 1450 1537 // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually. 1451 1538 if ( !empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) ) { 1452 1539 if ( $r['redirection']-- > 0 ) { … … 1459 1546 if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers']) ) 1460 1547 $theBody = WP_Http_Encoding::decompress( $theBody ); 1461 1548 1462 return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']);1549 return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] ); 1463 1550 } 1464 1551 1465 1552 /** 1553 * Grab the headers of the cURL request when steraming to a file 1554 * 1555 * Each header is sent individually to this callback, so we append to the $header property for temporary storage 1556 * 1557 * @since 3.2.0 1558 * @access private 1559 * @return int 1560 */ 1561 private function stream_headers( $handle, $headers ) { 1562 $this->headers .= $headers; 1563 return strlen( $headers ); 1564 } 1565 1566 /** 1466 1567 * Whether this class can be used for retrieving an URL. 1467 1568 * 1468 1569 * @static … … 1470 1571 * 1471 1572 * @return boolean False means this class can not be used, true means it can. 1472 1573 */ 1473 function test( $args = array()) {1574 function test( $args = array() ) { 1474 1575 if ( function_exists('curl_init') && function_exists('curl_exec') ) 1475 1576 return apply_filters('use_curl_transport', true, $args); 1476 1577 -
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 *