Bump PHP version requirement to 7.0.0+
[lhc/web/wiklou.git] / includes / http / PhpHttpRequest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 class PhpHttpRequest extends MWHttpRequest {
22
23 private $fopenErrors = [];
24
25 /**
26 * @param string $url
27 * @return string
28 */
29 protected function urlToTcp( $url ) {
30 $parsedUrl = parse_url( $url );
31
32 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
33 }
34
35 /**
36 * Returns an array with a 'capath' or 'cafile' key
37 * that is suitable to be merged into the 'ssl' sub-array of
38 * a stream context options array.
39 * Uses the 'caInfo' option of the class if it is provided, otherwise uses the system
40 * default CA bundle if PHP supports that, or searches a few standard locations.
41 * @return array
42 * @throws DomainException
43 */
44 protected function getCertOptions() {
45 $certOptions = [];
46 $certLocations = [];
47 if ( $this->caInfo ) {
48 $certLocations = [ 'manual' => $this->caInfo ];
49 }
50
51 foreach ( $certLocations as $key => $cert ) {
52 if ( is_dir( $cert ) ) {
53 $certOptions['capath'] = $cert;
54 break;
55 } elseif ( is_file( $cert ) ) {
56 $certOptions['cafile'] = $cert;
57 break;
58 } elseif ( $key === 'manual' ) {
59 // fail more loudly if a cert path was manually configured and it is not valid
60 throw new DomainException( "Invalid CA info passed: $cert" );
61 }
62 }
63
64 return $certOptions;
65 }
66
67 /**
68 * Custom error handler for dealing with fopen() errors.
69 * fopen() tends to fire multiple errors in succession, and the last one
70 * is completely useless (something like "fopen: failed to open stream")
71 * so normal methods of handling errors programmatically
72 * like get_last_error() don't work.
73 * @internal
74 * @param int $errno
75 * @param string $errstr
76 */
77 public function errorHandler( $errno, $errstr ) {
78 $n = count( $this->fopenErrors ) + 1;
79 $this->fopenErrors += [ "errno$n" => $errno, "errstr$n" => $errstr ];
80 }
81
82 /**
83 * @see MWHttpRequest::execute
84 *
85 * @return Status
86 */
87 public function execute() {
88 $this->prepare();
89
90 if ( is_array( $this->postData ) ) {
91 $this->postData = wfArrayToCgi( $this->postData );
92 }
93
94 if ( $this->parsedUrl['scheme'] != 'http'
95 && $this->parsedUrl['scheme'] != 'https' ) {
96 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
97 }
98
99 $this->reqHeaders['Accept'] = "*/*";
100 $this->reqHeaders['Connection'] = 'Close';
101 if ( $this->method == 'POST' ) {
102 // Required for HTTP 1.0 POSTs
103 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
104 if ( !isset( $this->reqHeaders['Content-Type'] ) ) {
105 $this->reqHeaders['Content-Type'] = "application/x-www-form-urlencoded";
106 }
107 }
108
109 // Set up PHP stream context
110 $options = [
111 'http' => [
112 'method' => $this->method,
113 'header' => implode( "\r\n", $this->getHeaderList() ),
114 'protocol_version' => '1.1',
115 'max_redirects' => $this->followRedirects ? $this->maxRedirects : 0,
116 'ignore_errors' => true,
117 'timeout' => $this->timeout,
118 // Curl options in case curlwrappers are installed
119 'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
120 'curl_verify_ssl_peer' => $this->sslVerifyCert,
121 ],
122 'ssl' => [
123 'verify_peer' => $this->sslVerifyCert,
124 'SNI_enabled' => true,
125 'ciphers' => 'HIGH:!SSLv2:!SSLv3:-ADH:-kDH:-kECDH:-DSS',
126 'disable_compression' => true,
127 ],
128 ];
129
130 if ( $this->proxy ) {
131 $options['http']['proxy'] = $this->urlToTcp( $this->proxy );
132 $options['http']['request_fulluri'] = true;
133 }
134
135 if ( $this->postData ) {
136 $options['http']['content'] = $this->postData;
137 }
138
139 if ( $this->sslVerifyHost ) {
140 // PHP 5.6.0 deprecates CN_match, in favour of peer_name which
141 // actually checks SubjectAltName properly.
142 if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) {
143 $options['ssl']['peer_name'] = $this->parsedUrl['host'];
144 } else {
145 $options['ssl']['CN_match'] = $this->parsedUrl['host'];
146 }
147 }
148
149 $options['ssl'] += $this->getCertOptions();
150
151 $context = stream_context_create( $options );
152
153 $this->headerList = [];
154 $reqCount = 0;
155 $url = $this->url;
156
157 $result = [];
158
159 if ( $this->profiler ) {
160 $profileSection = $this->profiler->scopedProfileIn(
161 __METHOD__ . '-' . $this->profileName
162 );
163 }
164 do {
165 $reqCount++;
166 $this->fopenErrors = [];
167 set_error_handler( [ $this, 'errorHandler' ] );
168 $fh = fopen( $url, "r", false, $context );
169 restore_error_handler();
170
171 if ( !$fh ) {
172 // HACK for instant commons.
173 // If we are contacting (commons|upload).wikimedia.org
174 // try again with CN_match for en.wikipedia.org
175 // as php does not handle SubjectAltName properly
176 // prior to "peer_name" option in php 5.6
177 if ( isset( $options['ssl']['CN_match'] )
178 && ( $options['ssl']['CN_match'] === 'commons.wikimedia.org'
179 || $options['ssl']['CN_match'] === 'upload.wikimedia.org' )
180 ) {
181 $options['ssl']['CN_match'] = 'en.wikipedia.org';
182 $context = stream_context_create( $options );
183 continue;
184 }
185 break;
186 }
187
188 $result = stream_get_meta_data( $fh );
189 $this->headerList = $result['wrapper_data'];
190 $this->parseHeader();
191
192 if ( !$this->followRedirects ) {
193 break;
194 }
195
196 # Handle manual redirection
197 if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
198 break;
199 }
200 # Check security of URL
201 $url = $this->getResponseHeader( "Location" );
202
203 if ( !Http::isValidURI( $url ) ) {
204 $this->logger->debug( __METHOD__ . ": insecure redirection\n" );
205 break;
206 }
207 } while ( true );
208 if ( $this->profiler ) {
209 $this->profiler->scopedProfileOut( $profileSection );
210 }
211
212 $this->setStatus();
213
214 if ( $fh === false ) {
215 if ( $this->fopenErrors ) {
216 $this->logger->warning( __CLASS__
217 . ': error opening connection: {errstr1}', $this->fopenErrors );
218 }
219 $this->status->fatal( 'http-request-error' );
220 return Status::wrap( $this->status ); // TODO B/C; move this to callers
221 }
222
223 if ( $result['timed_out'] ) {
224 $this->status->fatal( 'http-timed-out', $this->url );
225 return Status::wrap( $this->status ); // TODO B/C; move this to callers
226 }
227
228 // If everything went OK, or we received some error code
229 // get the response body content.
230 if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
231 while ( !feof( $fh ) ) {
232 $buf = fread( $fh, 8192 );
233
234 if ( $buf === false ) {
235 $this->status->fatal( 'http-read-error' );
236 break;
237 }
238
239 if ( strlen( $buf ) ) {
240 call_user_func( $this->callback, $fh, $buf );
241 }
242 }
243 }
244 fclose( $fh );
245
246 return Status::wrap( $this->status ); // TODO B/C; move this to callers
247 }
248 }