Changeset 17555
- Timestamp:
- 03/25/2011 02:42:20 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/file.php
r17525 r17555 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. … … 520 484 return new WP_Error('http_no_file', __('Could not create Temporary file.')); 521 485 522 $handle = @fopen($tmpfname, 'wb'); 523 if ( ! $handle ) 524 return new WP_Error('http_no_file', __('Could not create Temporary file.')); 525 526 $response = wp_remote_get($url, array('timeout' => $timeout)); 527 528 if ( is_wp_error($response) ) { 529 fclose($handle); 530 unlink($tmpfname); 486 $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); 487 488 if ( is_wp_error( $response ) ) { 489 unlink( $tmpfname ); 531 490 return $response; 532 491 } 533 492 534 493 if ( $response['response']['code'] != '200' ){ 535 fclose($handle); 536 unlink($tmpfname); 537 return new WP_Error('http_404', trim($response['response']['message'])); 538 } 539 540 fwrite($handle, $response['body']); 541 fclose($handle); 494 unlink( $tmpfname ); 495 return new WP_Error( 'http_404', trim( $response['response']['message'] ) ); 496 } 542 497 543 498 return $tmpfname; -
trunk/wp-includes/class-http.php
r17551 r17555 98 98 'compress' => false, 99 99 'decompress' => true, 100 'sslverify' => true 100 'sslverify' => true, 101 'stream' => false, 102 'filename' => null 101 103 ); 102 104 … … 136 138 $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host']; 137 139 unset( $homeURL ); 140 141 // If we are streaming to a file but no filename was given drop it in the WP temp dir 142 // and pick it's name using the basename of the $url 143 if ( $r['stream'] && empty( $r['filename'] ) ) 144 $r['filename'] = get_temp_dir() . basename( $url ); 145 146 // Force some settings if we are streaming to a file and check for existence and perms of destination directory 147 if ( $r['stream'] ) { 148 $r['blocking'] = true; 149 if ( ! is_writable( dirname( $r['filename'] ) ) ) 150 return new WP_Error( 'http_request_failed', __( 'Destination directory for file streaming does not exist or is not writable.' ) ); 151 } 138 152 139 153 if ( is_null( $r['headers'] ) ) … … 660 674 661 675 $strResponse = ''; 662 while ( ! feof($handle) ) 663 $strResponse .= fread($handle, 4096); 664 665 fclose($handle); 676 $bodyStarted = false; 677 678 // If streaming to a file setup the file handle 679 if ( $r['stream'] ) { 680 if ( ! WP_DEBUG ) 681 $stream_handle = @fopen( $r['filename'], 'w+' ); 682 else 683 $stream_handle = fopen( $r['filename'], 'w+' ); 684 if ( ! $stream_handle ) 685 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) ); 686 687 while ( ! feof($handle) ) { 688 $block = fread( $handle, 4096 ); 689 if ( $bodyStarted ) { 690 fwrite( $stream_handle, $block ); 691 } else { 692 $strResponse .= $block; 693 if ( strpos( $strResponse, "\r\n\r\n" ) ) { 694 $process = WP_Http::processResponse( $strResponse ); 695 $bodyStarted = true; 696 fwrite( $stream_handle, $process['body'] ); 697 unset( $strResponse ); 698 $process['body'] = ''; 699 } 700 } 701 } 702 703 fclose( $stream_handle ); 704 705 } else { 706 while ( ! feof($handle) ) 707 $strResponse .= fread( $handle, 4096 ); 708 709 $process = WP_Http::processResponse( $strResponse ); 710 unset( $strResponse ); 711 } 712 713 fclose( $handle ); 666 714 667 715 if ( true === $secure_transport ) 668 716 error_reporting($error_reporting); 669 717 670 $process = WP_Http::processResponse($strResponse); 671 $arrHeaders = WP_Http::processHeaders($process['headers']); 718 $arrHeaders = WP_Http::processHeaders( $process['headers'] ); 672 719 673 720 // Is the response code within the 400 range? … … 691 738 $process['body'] = WP_Http_Encoding::decompress( $process['body'] ); 692 739 693 return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']);740 return array( 'headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename'] ); 694 741 } 695 742 … … 835 882 } 836 883 837 $strResponse = stream_get_contents($handle); 838 $meta = stream_get_meta_data($handle); 839 840 fclose($handle); 884 if ( $r['stream'] ) { 885 if ( ! WP_DEBUG ) 886 $stream_handle = @fopen( $r['filename'], 'w+' ); 887 else 888 $stream_handle = fopen( $r['filename'], 'w+' ); 889 890 if ( ! $stream_handle ) 891 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) ); 892 893 stream_copy_to_stream( $handle, $stream_handle ); 894 895 fclose( $stream_handle ); 896 $strResponse = ''; 897 } else { 898 $strResponse = stream_get_contents( $handle ); 899 } 900 901 $meta = stream_get_meta_data( $handle ); 902 903 fclose( $handle ); 841 904 842 905 $processedHeaders = array(); … … 857 920 $strResponse = WP_Http_Encoding::decompress( $strResponse ); 858 921 859 return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);922 return array( 'headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies'], 'filename' => $r['filename'] ); 860 923 } 861 924 … … 1007 1070 $theBody = http_inflate( $theBody ); 1008 1071 1072 if ( $r['stream'] ) { 1073 if ( !WP_DEBUG ) 1074 $stream_handle = @fopen( $r['filename'], 'w+' ); 1075 else 1076 $stream_handle = fopen( $r['filename'], 'w+' ); 1077 1078 if ( ! $stream_handle ) 1079 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) ); 1080 1081 fwrite( $stream_handle, $theBody ); 1082 fclose( $stream_handle ); 1083 $theBody = ''; 1084 } 1085 1009 1086 $theResponse = array(); 1010 1087 $theResponse['code'] = $info['response_code']; 1011 1088 $theResponse['message'] = get_status_header_desc($info['response_code']); 1012 1089 1013 return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse, 'cookies' => $theHeaders['cookies']);1090 return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theResponse, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] ); 1014 1091 } 1015 1092 … … 1037 1114 */ 1038 1115 class WP_Http_Curl { 1116 1117 /** 1118 * Temporary header storage for use with streaming to a file. 1119 * 1120 * @since 3.2.0 1121 * @access private 1122 * @var string 1123 */ 1124 private $headers; 1039 1125 1040 1126 /** … … 1122 1208 1123 1209 if ( true === $r['blocking'] ) 1124 curl_setopt( $handle, CURLOPT_HEADER, true ); 1125 else 1126 curl_setopt( $handle, CURLOPT_HEADER, false ); 1210 curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) ); 1211 1212 curl_setopt( $handle, CURLOPT_HEADER, false ); 1213 1214 // If streaming to a file open a file handle, and setup our curl streaming handler 1215 if ( $r['stream'] ) { 1216 if ( ! WP_DEBUG ) 1217 $stream_handle = @fopen( $r['filename'], 'w+' ); 1218 else 1219 $stream_handle = fopen( $r['filename'], 'w+' ); 1220 if ( ! $stream_handle ) 1221 return new WP_Error( 'http_request_failed', sprintf( __( 'Could not open handle for fopen() to %s' ), $r['filename'] ) ); 1222 curl_setopt( $handle, CURLOPT_FILE, $stream_handle ); 1223 } 1127 1224 1128 1225 // The option doesn't work with safe mode or when open_basedir is set. … … 1156 1253 1157 1254 $theResponse = curl_exec( $handle ); 1158 1159 if ( !empty($theResponse) ) { 1160 $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); 1161 $theHeaders = trim( substr($theResponse, 0, $headerLength) ); 1162 if ( strlen($theResponse) > $headerLength ) 1163 $theBody = substr( $theResponse, $headerLength ); 1164 else 1165 $theBody = ''; 1166 if ( false !== strpos($theHeaders, "\r\n\r\n") ) { 1167 $headerParts = explode("\r\n\r\n", $theHeaders); 1168 $theHeaders = $headerParts[ count($headerParts) -1 ]; 1169 } 1170 $theHeaders = WP_Http::processHeaders($theHeaders); 1171 } else { 1255 $theBody = ''; 1256 $theHeaders = WP_Http::processHeaders( $this->headers ); 1257 1258 if ( ! empty($theResponse) && ! is_bool( $theResponse ) ) // is_bool: when using $args['stream'], curl_exec will return (bool)true 1259 $theBody = $theResponse; 1260 1261 // If no response, and It's not a HEAD request with valid headers returned 1262 if ( empty($theResponse) && 'HEAD' != $args['method'] && ! empty($this->headers) ) { 1172 1263 if ( $curl_error = curl_error($handle) ) 1173 1264 return new WP_Error('http_request_failed', $curl_error); 1174 1265 if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) ) 1175 1266 return new WP_Error('http_request_failed', __('Too many redirects.')); 1176 1177 $theHeaders = array( 'headers' => array(), 'cookies' => array() ); 1178 $theBody = ''; 1179 } 1267 } 1268 1269 unset( $this->headers ); 1180 1270 1181 1271 $response = array(); … … 1185 1275 curl_close( $handle ); 1186 1276 1277 if ( $r['stream'] ) 1278 fclose( $stream_handle ); 1279 1187 1280 // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually. 1188 if ( ! empty($theHeaders['headers']['location']) && (ini_get('safe_mode') || ini_get('open_basedir')) && 0 !== $r['_redirection'] ) {1281 if ( ! empty( $theHeaders['headers']['location'] ) && ( ini_get( 'safe_mode' ) || ini_get( 'open_basedir' ) ) && 0 !== $r['_redirection'] ) { 1189 1282 if ( $r['redirection']-- > 0 ) { 1190 return $this->request( $theHeaders['headers']['location'], $r);1283 return $this->request( $theHeaders['headers']['location'], $r ); 1191 1284 } else { 1192 return new WP_Error( 'http_request_failed', __('Too many redirects.'));1285 return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) ); 1193 1286 } 1194 1287 } … … 1197 1290 $theBody = WP_Http_Encoding::decompress( $theBody ); 1198 1291 1199 return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']); 1292 return array( 'headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename'] ); 1293 } 1294 1295 /** 1296 * Grab the headers of the cURL request 1297 * 1298 * Each header is sent individually to this callback, so we append to the $header property for temporary storage 1299 * 1300 * @since 3.2.0 1301 * @access private 1302 * @return int 1303 */ 1304 private function stream_headers( $handle, $headers ) { 1305 $this->headers .= $headers; 1306 return strlen( $headers ); 1200 1307 } 1201 1308 -
trunk/wp-includes/functions.php
r17516 r17555 2109 2109 2110 2110 return rtrim($base, '/') . '/' . ltrim($path, '/'); 2111 } 2112 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; 2111 2147 } 2112 2148
Note: See TracChangeset
for help on using the changeset viewer.