Ticket #16236: 16236.diff

File 16236.diff, 5.5 KB (added by sivel, 2 years ago)

Some early code to support stream to file using cURL

Line 
1Index: wp-includes/class-http.php
2===================================================================
3--- wp-includes/class-http.php  (revision 17444)
4+++ wp-includes/class-http.php  (working copy)
5@@ -227,7 +227,9 @@
6                        'body' => null,
7                        'compress' => false,
8                        'decompress' => true,
9-                       'sslverify' => true
10+                       'sslverify' => true,
11+                       'stream' => false,
12+                       'filename' => false
13                );
14 
15                $r = wp_parse_args( $args, $defaults );
16@@ -255,6 +257,14 @@
17                $r['local'] = $homeURL['host'] == $arrURL['host'] || 'localhost' == $arrURL['host'];
18                unset( $homeURL );
19 
20+               if ( $r['stream']  && ! $r['filename'] )
21+                       $r['filename'] = basename( $url );
22+
23+               if ( $r['stream'] ) {
24+                       $r['blocking'] = true;
25+                       $r['method'] = 'GET';
26+               }
27+
28                if ( is_null( $r['headers'] ) )
29                        $r['headers'] = array();
30 
31@@ -1291,6 +1301,8 @@
32  */
33 class WP_Http_Curl {
34 
35+       private $headers;
36+
37        /**
38         * Send a HTTP request to a URI using cURL extension.
39         *
40@@ -1387,6 +1399,12 @@
41                else
42                        curl_setopt( $handle, CURLOPT_HEADER, false );
43 
44+               if ( $r['stream'] ) {
45+                       $stream_handle = fopen( get_temp_dir() . $r['filename'], 'w+' );
46+                       curl_setopt( $handle, CURLOPT_FILE, $stream_handle );
47+                       curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers' ) );
48+               }
49+
50                // The option doesn't work with safe mode or when open_basedir is set.
51                // Disable HEAD when making HEAD requests.
52                if ( !ini_get('safe_mode') && !ini_get('open_basedir') && 'HEAD' != $r['method'] )
53@@ -1421,14 +1439,19 @@
54 
55                if ( !empty($theResponse) ) {
56                        $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
57-                       $theHeaders = trim( substr($theResponse, 0, $headerLength) );
58-                       if ( strlen($theResponse) > $headerLength )
59-                               $theBody = substr( $theResponse, $headerLength );
60-                       else
61+                       if ( ! empty( $this->headers ) ) {
62+                               $theHeaders = $this->headers;
63                                $theBody = '';
64-                       if ( false !== strpos($theHeaders, "\r\n\r\n") ) {
65-                               $headerParts = explode("\r\n\r\n", $theHeaders);
66-                               $theHeaders = $headerParts[ count($headerParts) -1 ];
67+                       } else {
68+                               $theHeaders = trim( substr($theResponse, 0, $headerLength) );
69+                               if ( strlen($theResponse) > $headerLength )
70+                                       $theBody = substr( $theResponse, $headerLength );
71+                               else
72+                                       $theBody = '';
73+                               if ( false !== strpos($theHeaders, "\r\n\r\n") ) {
74+                                       $headerParts = explode("\r\n\r\n", $theHeaders);
75+                                       $theHeaders = $headerParts[ count($headerParts) -1 ];
76+                               }
77                        }
78                        $theHeaders = WP_Http::processHeaders($theHeaders);
79                } else {
80@@ -1462,6 +1485,11 @@
81                return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']);
82        }
83 
84+       function stream_headers( $handle, $headers ) {
85+               $this->headers .= $headers;
86+               return strlen($headers);
87+       }
88+
89        /**
90         * Whether this class can be used for retrieving an URL.
91         *
92Index: wp-includes/functions.php
93===================================================================
94--- wp-includes/functions.php   (revision 17444)
95+++ wp-includes/functions.php   (working copy)
96@@ -2111,6 +2111,42 @@
97 }
98 
99 /**
100+ * Determines a writable directory for temporary files.
101+ * 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/
102+ *
103+ * 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.
104+ *
105+ * @since 2.5.0
106+ *
107+ * @return string Writable temporary directory
108+ */
109+function get_temp_dir() {
110+       static $temp;
111+       if ( defined('WP_TEMP_DIR') )
112+               return trailingslashit(WP_TEMP_DIR);
113+
114+       if ( $temp )
115+               return trailingslashit($temp);
116+
117+       $temp = WP_CONTENT_DIR . '/';
118+       if ( is_dir($temp) && @is_writable($temp) )
119+               return $temp;
120+
121+       if  ( function_exists('sys_get_temp_dir') ) {
122+               $temp = sys_get_temp_dir();
123+               if ( @is_writable($temp) )
124+                       return trailingslashit($temp);
125+       }
126+
127+       $temp = ini_get('upload_tmp_dir');
128+       if ( is_dir($temp) && @is_writable($temp) )
129+               return trailingslashit($temp);
130+
131+       $temp = '/tmp/';
132+       return $temp;
133+}
134+
135+/**
136  * Get an array containing the current upload directory's path and url.
137  *
138  * Checks the 'upload_path' option, which should be from the web root folder,
139Index: wp-admin/includes/file.php
140===================================================================
141--- wp-admin/includes/file.php  (revision 17444)
142+++ wp-admin/includes/file.php  (working copy)
143@@ -153,42 +153,6 @@
144 }
145 
146 /**
147- * Determines a writable directory for temporary files.
148- * 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/
149- *
150- * 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.
151- *
152- * @since 2.5.0
153- *
154- * @return string Writable temporary directory
155- */
156-function get_temp_dir() {
157-       static $temp;
158-       if ( defined('WP_TEMP_DIR') )
159-               return trailingslashit(WP_TEMP_DIR);
160-
161-       if ( $temp )
162-               return trailingslashit($temp);
163-
164-       $temp = WP_CONTENT_DIR . '/';
165-       if ( is_dir($temp) && @is_writable($temp) )
166-               return $temp;
167-
168-       if  ( function_exists('sys_get_temp_dir') ) {
169-               $temp = sys_get_temp_dir();
170-               if ( @is_writable($temp) )
171-                       return trailingslashit($temp);
172-       }
173-
174-       $temp = ini_get('upload_tmp_dir');
175-       if ( is_dir($temp) && @is_writable($temp) )
176-               return trailingslashit($temp);
177-
178-       $temp = '/tmp/';
179-       return $temp;
180-}
181-
182-/**
183  * Returns a filename of a Temporary unique file.
184  * Please note that the calling function must unlink() this itself.
185  *