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