fixes bug 19736
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * HTTP handling class
4 * @defgroup HTTP HTTP
5 * @file
6 * @ingroup HTTP
7 */
8
9 class Http {
10 const SYNC_DOWNLOAD = 1; // syncronys upload (in a single request)
11 const ASYNC_DOWNLOAD = 2; // asynchronous upload we should spawn out another process and monitor progress if possible)
12
13 var $body = '';
14 public static function request($method, $url, $opts = Array() ){
15 $opts['method'] = ( strtoupper( $method ) == 'GET' || strtoupper( $method ) == 'POST' ) ? strtoupper( $method ) : null;
16 $req = new HttpRequest( $url, $opts );
17 $status = $req->doRequest();
18 if( $status->isOK() ){
19 return $status->value;
20 } else {
21 wfDebug( 'http error: ' . $status->getWikiText() );
22 return false;
23 }
24 }
25 /**
26 * Simple wrapper for Http::request( 'GET' )
27 */
28 public static function get( $url, $timeout = false ) {
29 $opts = Array();
30 if( $timeout )
31 $opts['timeout'] = $timeout;
32 return Http::request( 'GET', $url, $opts );
33 }
34
35 /**
36 * Simple wrapper for Http::request( 'POST' )
37 */
38 public static function post( $url, $opts = array() ) {
39 return Http::request( 'POST', $url, $opts );
40 }
41
42 public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD , $redirectCount = 0 ){
43 global $wgPhpCli, $wgMaxUploadSize, $wgMaxRedirects;
44 // do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize
45 $headRequest = new HttpRequest( $url, array( 'headers_only' => true ) );
46 $headResponse = $headRequest->doRequest();
47 if( !$headResponse->isOK() ){
48 return $headResponse;
49 }
50 $head = $headResponse->value;
51
52 // check for redirects:
53 if( isset( $head['Location'] ) && strrpos( $head[0], '302' ) !== false ){
54 if( $redirectCount < $wgMaxRedirects ){
55 if( UploadFromUrl::isValidURI( $head['Location'] ) ){
56 return self::doDownload( $head['Location'], $target_file_path , $dl_mode, $redirectCount++ );
57 } else {
58 return Status::newFatal( 'upload-proto-error' );
59 }
60 } else {
61 return Status::newFatal( 'upload-too-many-redirects' );
62 }
63 }
64 // we did not get a 200 ok response:
65 if( strrpos( $head[0], '200 OK' ) === false ){
66 return Status::newFatal( 'upload-http-error', htmlspecialchars( $head[0] ) );
67 }
68
69 $content_length = ( isset( $head['Content-Length'] ) ) ? $head['Content-Length'] : null;
70 if( $content_length ){
71 if( $content_length > $wgMaxUploadSize ){
72 return Status::newFatal( 'requested file length ' . $content_length . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize );
73 }
74 }
75
76 // check if we can find phpCliPath (for doing a background shell request to php to do the download:
77 if( $wgPhpCli && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD ){
78 wfDebug( __METHOD__ . "\ASYNC_DOWNLOAD\n" );
79 // setup session and shell call:
80 return self::initBackgroundDownload( $url, $target_file_path, $content_length );
81 } else if( $dl_mode == self::SYNC_DOWNLOAD ){
82 wfDebug( __METHOD__ . "\nSYNC_DOWNLOAD\n" );
83 // SYNC_DOWNLOAD download as much as we can in the time we have to execute
84 $opts['method'] = 'GET';
85 $opts['target_file_path'] = $target_file_path;
86 $req = new HttpRequest( $url, $opts );
87 return $req->doRequest();
88 }
89 }
90
91 /**
92 * a non blocking request (generally an exit point in the application)
93 * should write to a file location and give updates
94 *
95 */
96 private function initBackgroundDownload( $url, $target_file_path, $content_length = null ){
97 global $wgMaxUploadSize, $IP, $wgPhpCli, $wgServer;
98 $status = Status::newGood();
99
100 // generate a session id with all the details for the download (pid, target_file_path )
101 $upload_session_key = self::getUploadSessionKey();
102 $session_id = session_id();
103
104 // store the url and target path:
105 $_SESSION['wsDownload'][$upload_session_key]['url'] = $url;
106 $_SESSION['wsDownload'][$upload_session_key]['target_file_path'] = $target_file_path;
107 //since we request from the cmd line we lose the original host name pass in the session:
108 $_SESSION['wsDownload'][$upload_session_key]['orgServer'] = $wgServer;
109
110 if( $content_length )
111 $_SESSION['wsDownload'][$upload_session_key]['content_length'] = $content_length;
112
113 // set initial loaded bytes:
114 $_SESSION['wsDownload'][$upload_session_key]['loaded'] = 0;
115
116 // run the background download request:
117 $cmd = $wgPhpCli . ' ' . $IP . "/maintenance/http_session_download.php --sid {$session_id} --usk {$upload_session_key}";
118 $pid = wfShellBackgroundExec( $cmd, $retval );
119 // the pid is not of much use since we won't be visiting this same apache any-time soon.
120 if( !$pid )
121 return Status::newFatal( 'could not run background shell exec' );
122
123 // update the status value with the $upload_session_key (for the user to check on the status of the upload)
124 $status->value = $upload_session_key;
125
126 // return good status
127 return $status;
128 }
129
130 function getUploadSessionKey(){
131 $key = mt_rand( 0, 0x7fffffff );
132 $_SESSION['wsUploadData'][$key] = array();
133 return $key;
134 }
135
136 /**
137 * used to run a session based download. Is initiated via the shell.
138 *
139 * @param $session_id String: the session id to grab download details from
140 * @param $upload_session_key String: the key of the given upload session
141 * (a given client could have started a few http uploads at once)
142 */
143 public static function doSessionIdDownload( $session_id, $upload_session_key ){
144 global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout, $wgServer;
145 wfDebug( __METHOD__ . "\n\ndoSessionIdDownload:\n\n" );
146 // set session to the provided key:
147 session_id( $session_id );
148 // start the session
149 if( session_start() === false ){
150 wfDebug( __METHOD__ . ' could not start session' );
151 }
152 //get all the vars we need from session_id
153 if(!isset($_SESSION[ 'wsDownload' ][$upload_session_key])){
154 wfDebug( __METHOD__ .' Error:could not find upload session');
155 exit();
156 }
157 // setup the global user from the session key we just inherited
158 $wgUser = User::newFromSession();
159
160 // grab the session data to setup the request:
161 $sd =& $_SESSION['wsDownload'][$upload_session_key];
162
163 //update the wgServer var ( since cmd line thinks we are localhost when we are really orgServer)
164 if(isset($sd['orgServer']) && $sd['orgServer']){
165 $wgServer = $sd['orgServer'];
166 }
167 // close down the session so we can other http queries can get session updates:
168 session_write_close();
169
170 $req = new HttpRequest( $sd['url'], array(
171 'target_file_path' => $sd['target_file_path'],
172 'upload_session_key'=> $upload_session_key,
173 'timeout' => $wgAsyncHTTPTimeout
174 ) );
175 // run the actual request .. (this can take some time)
176 wfDebug( __METHOD__ . "do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] );
177 $status = $req->doRequest();
178 //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n");
179
180 // start up the session again:
181 if( session_start() === false ){
182 wfDebug( __METHOD__ . ' ERROR:: Could not start session');
183 }
184 // grab the updated session data pointer
185 $sd =& $_SESSION['wsDownload'][$upload_session_key];
186 // if error update status:
187 if( !$status->isOK() ){
188 $sd['apiUploadResult'] = ApiFormatJson::getJsonEncode(
189 array( 'error' => $status->getWikiText() )
190 );
191 }
192 // if status okay process upload using fauxReq to api:
193 if( $status->isOK() ){
194 // setup the FauxRequest
195 $fauxReqData = $sd['mParams'];
196 $fauxReqData['action'] = 'upload';
197 $fauxReqData['format'] = 'json';
198 $fauxReqData['internalhttpsession'] = $upload_session_key;
199
200 // evil but no other clean way about it:
201 $faxReq = new FauxRequest( $fauxReqData, true );
202 $processor = new ApiMain( $faxReq, $wgEnableWriteAPI );
203
204 //init the mUpload var for the $processor
205 $processor->execute();
206 $processor->getResult()->cleanUpUTF8();
207 $printer = $processor->createPrinterByName( 'json' );
208 $printer->initPrinter( false );
209 ob_start();
210 $printer->execute();
211 $apiUploadResult = ob_get_clean();
212
213 wfDebug( __METHOD__ . "\n\n got api result:: $apiUploadResult \n" );
214 // the status updates runner will grab the result form the session:
215 $sd['apiUploadResult'] = $apiUploadResult;
216 }
217 // close the session:
218 session_write_close();
219 }
220
221 /**
222 * Check if the URL can be served by localhost
223 * @param $url string Full url to check
224 * @return bool
225 */
226 public static function isLocalURL( $url ) {
227 global $wgCommandLineMode, $wgConf;
228 if ( $wgCommandLineMode ) {
229 return false;
230 }
231
232 // Extract host part
233 $matches = array();
234 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
235 $host = $matches[1];
236 // Split up dotwise
237 $domainParts = explode( '.', $host );
238 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
239 $domainParts = array_reverse( $domainParts );
240 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
241 $domainPart = $domainParts[$i];
242 if ( $i == 0 ) {
243 $domain = $domainPart;
244 } else {
245 $domain = $domainPart . '.' . $domain;
246 }
247 if ( $wgConf->isLocalVHost( $domain ) ) {
248 return true;
249 }
250 }
251 }
252 return false;
253 }
254
255 /**
256 * Return a standard user-agent we can use for external requests.
257 */
258 public static function userAgent() {
259 global $wgVersion;
260 return "MediaWiki/$wgVersion";
261 }
262 }
263 class HttpRequest {
264 var $target_file_path;
265 var $upload_session_key;
266
267 function __construct( $url, $opt ){
268 global $wgSyncHTTPTimeout;
269 //double check its a valid url:
270 $this->url = $url;
271
272 // set the timeout to default sync timeout (unless the timeout option is provided)
273 $this->timeout = ( isset( $opt['timeout'] ) ) ? $opt['timeout'] : $wgSyncHTTPTimeout;
274 $this->method = ( isset( $opt['method'] ) ) ? $opt['method'] : 'GET';
275 $this->target_file_path = ( isset( $opt['target_file_path'] ) ) ? $opt['target_file_path'] : false;
276 $this->upload_session_key = ( isset( $opt['upload_session_key'] ) ) ? $opt['upload_session_key'] : false;
277 $this->headers_only = ( isset( $opt['headers_only'] ) ) ? $opt['headers_only'] : false;
278 }
279
280 /**
281 * Get the contents of a file by HTTP
282 * @param $url string Full URL to act on
283 * @param $Opt associative array Optional array of options:
284 * 'method' => 'GET', 'POST' etc.
285 * 'target_file_path' => if curl should output to a target file
286 * 'adapter' => 'curl', 'soket'
287 */
288 public function doRequest() {
289
290 #make sure we have a valid url
291 if( !UploadFromUrl::isValidURI( $this->url ) )
292 return Status::newFatal('bad-url');
293
294 # Use curl if available
295 if ( function_exists( 'curl_init' ) ) {
296 return $this->doCurlReq();
297 } else {
298 return $this->doPhpReq();
299 }
300 }
301
302 private function doCurlReq(){
303 global $wgHTTPProxy, $wgTitle;
304
305 $status = Status::newGood();
306 $c = curl_init( $this->url );
307
308 // proxy setup:
309 if ( Http::isLocalURL( $this->url ) ) {
310 curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' );
311 } else if ( $wgHTTPProxy ) {
312 curl_setopt( $c, CURLOPT_PROXY, $wgHTTPProxy );
313 }
314
315 curl_setopt( $c, CURLOPT_TIMEOUT, $this->timeout );
316 curl_setopt( $c, CURLOPT_USERAGENT, Http::userAgent() );
317
318 if ( $this->headers_only ) {
319 curl_setopt( $c, CURLOPT_NOBODY, true );
320 curl_setopt( $c, CURLOPT_HEADER, true );
321 } elseif ( $this->method == 'POST' ) {
322 curl_setopt( $c, CURLOPT_POST, true );
323 curl_setopt( $c, CURLOPT_POSTFIELDS, '' );
324 } else {
325 curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $this->method );
326 }
327
328 # Set the referer to $wgTitle, even in command-line mode
329 # This is useful for interwiki transclusion, where the foreign
330 # server wants to know what the referring page is.
331 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
332 # referring page.
333 if ( is_object( $wgTitle ) ) {
334 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
335 }
336
337 // set the write back function (if we are writing to a file)
338 if( $this->target_file_path ){
339 $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key );
340 if( !$cwrite->status->isOK() ){
341 wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" );
342 $status = $cwrite->status;
343 return $status;
344 }
345 curl_setopt( $c, CURLOPT_WRITEFUNCTION, array( $cwrite, 'callbackWriteBody' ) );
346 }
347
348 // start output grabber:
349 if( !$this->target_file_path )
350 ob_start();
351
352 //run the actual curl_exec:
353 try {
354 if ( false === curl_exec( $c ) ) {
355 $error_txt ='Error sending request: #' . curl_errno( $c ) .' '. curl_error( $c );
356 wfDebug( __METHOD__ . $error_txt . "\n" );
357 $status = Status::newFatal( $error_txt );
358 }
359 } catch ( Exception $e ) {
360 // do something with curl exec error?
361 }
362 // if direct request output the results to the stats value:
363 if( !$this->target_file_path && $status->isOK() ){
364 $status->value = ob_get_contents();
365 ob_end_clean();
366 }
367 // if we wrote to a target file close up or return error
368 if( $this->target_file_path ){
369 $cwrite->close();
370 if( !$cwrite->status->isOK() ){
371 return $cwrite->status;
372 }
373 }
374
375 if ( $this->headers_only ) {
376 $headers = explode( "\n", $status->value );
377 $headerArray = array();
378 foreach ( $headers as $header ) {
379 if ( !strlen( trim( $header ) ) )
380 continue;
381 $headerParts = explode( ':', $header, 2 );
382 if ( count( $headerParts ) == 1 ) {
383 $headerArray[] = trim( $header );
384 } else {
385 list( $key, $val ) = $headerParts;
386 $headerArray[trim( $key )] = trim( $val );
387 }
388 }
389 $status->value = $headerArray;
390 } else {
391 # Don't return the text of error messages, return false on error
392 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE );
393 if ( $retcode != 200 ) {
394 wfDebug( __METHOD__ . ": HTTP return code $retcode\n" );
395 $status = Status::newFatal( "HTTP return code $retcode\n" );
396 }
397 # Don't return truncated output
398 $errno = curl_errno( $c );
399 if ( $errno != CURLE_OK ) {
400 $errstr = curl_error( $c );
401 wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" );
402 $status = Status::newFatal( " CURL error code $errno: $errstr\n" );
403 }
404 }
405
406 curl_close( $c );
407
408 // return the result obj
409 return $status;
410 }
411
412 public function doPhpReq(){
413 global $wgTitle, $wgHTTPProxy;
414
415 #check for php.ini allow_url_fopen
416 if( !ini_get( 'allow_url_fopen' ) ){
417 return Status::newFatal( 'allow_url_fopen needs to be enabled for http copy to work' );
418 }
419
420 //start with good status:
421 $status = Status::newGood();
422
423 if ( $this->headers_only ) {
424 $status->value = get_headers( $this->url, 1 );
425 return $status;
426 }
427
428 //setup the headers
429 $headers = array( "User-Agent: " . Http :: userAgent() );
430 if ( is_object( $wgTitle ) ) {
431 $headers[] = "Referer: ". $wgTitle->getFullURL();
432 }
433
434 if( strcasecmp( $this->method, 'post' ) == 0 ) {
435 // Required for HTTP 1.0 POSTs
436 $headers[] = "Content-Length: 0";
437 }
438 $fcontext = stream_context_create ( array(
439 'http' => array(
440 'method' => $this->method,
441 'header' => implode( "\r\n", $headers ),
442 'timeout' => $this->timeout )
443 )
444 );
445
446 $fh = fopen( $this->url, "r", false, $fcontext);
447
448 // set the write back function (if we are writing to a file)
449 if( $this->target_file_path ){
450 $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key );
451 if( !$cwrite->status->isOK() ){
452 wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" );
453 $status = $cwrite->status;
454 return $status;
455 }
456 //read $fh into the simpleFileWriter (grab in 64K chunks since its likely a media file)
457 while ( !feof( $fh )) {
458 $contents = fread($fh, 65536);
459 $cwrite->callbackWriteBody($fh, $contents );
460 }
461
462 $cwrite->close();
463 //check for simpleFileWriter error:
464 if( !$cwrite->status->isOK() ){
465 return $cwrite->status;
466 }
467 } else {
468 //read $fh into status->value
469 $status->value = @stream_get_contents( $fh );
470 }
471 //close the url file wrapper
472 fclose( $fh );
473
474 //check for "false"
475 if( $status->value === false ){
476 $status->error( 'file_get_contents-failed' );
477 }
478 return $status;
479 }
480
481 }
482
483 /**
484 * a simpleFileWriter with session id updates
485 */
486 class simpleFileWriter {
487 var $target_file_path;
488 var $status = null;
489 var $session_id = null;
490 var $session_update_interval = 0; // how often to update the session while downloading
491
492 function simpleFileWriter( $target_file_path, $upload_session_key ){
493 $this->target_file_path = $target_file_path;
494 $this->upload_session_key = $upload_session_key;
495 $this->status = Status::newGood();
496 // open the file:
497 $this->fp = fopen( $this->target_file_path, 'w' );
498 if( $this->fp === false ){
499 $this->status = Status::newFatal( 'HTTP::could-not-open-file-for-writing' );
500 }
501 // true start time
502 $this->prevTime = time();
503 }
504
505 public function callbackWriteBody($ch, $data_packet){
506 global $wgMaxUploadSize;
507
508 // write out the content
509 if( fwrite( $this->fp, $data_packet ) === false ){
510 wfDebug( __METHOD__ ." ::could-not-write-to-file\n" );
511 $this->status = Status::newFatal( 'HTTP::could-not-write-to-file' );
512 return 0;
513 }
514
515 // check file size:
516 clearstatcache();
517 $this->current_fsize = filesize( $this->target_file_path );
518
519 if( $this->current_fsize > $wgMaxUploadSize ){
520 wfDebug( __METHOD__ . " ::http download too large\n" );
521 $this->status = Status::newFatal( 'HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' .
522 Language::formatSize( $wgMaxUploadSize ) . ' ' );
523 return 0;
524 }
525
526 // if more than session_update_interval second have passed update_session_progress
527 if( $this->upload_session_key && ( ( time() - $this->prevTime ) > $this->session_update_interval ) ) {
528 $this->prevTime = time();
529 $session_status = $this->update_session_progress();
530 if( !$session_status->isOK() ){
531 $this->status = $session_status;
532 wfDebug( __METHOD__ . ' update session failed or was canceled');
533 return 0;
534 }
535 }
536 return strlen( $data_packet );
537 }
538
539 public function update_session_progress(){
540 $status = Status::newGood();
541 // start the session
542 if( session_start() === false){
543 wfDebug( __METHOD__ . ' could not start session' );
544 exit( 0 );
545 }
546 $sd =& $_SESSION['wsDownload'][$this->upload_session_key];
547 // check if the user canceled the request:
548 if( isset( $sd['user_cancel'] ) && $sd['user_cancel'] == true ){
549 // kill the download
550 return Status::newFatal( 'user-canceled-request' );
551 }
552 // update the progress bytes download so far:
553 $sd['loaded'] = $this->current_fsize;
554 wfDebug( __METHOD__ . ': set session loaded amount to: ' . $sd['loaded'] . "\n");
555 // close down the session so we can other http queries can get session updates:
556 session_write_close();
557 return $status;
558 }
559
560 public function close(){
561 // do a final session update:
562 $this->update_session_progress();
563 // close up the file handle:
564 if( false === fclose( $this->fp ) ){
565 $this->status = Status::newFatal( 'HTTP::could-not-close-file' );
566 }
567 }
568
569 }