10 const SYNC_DOWNLOAD
= 1; // syncronous upload (in a single request)
11 const ASYNC_DOWNLOAD
= 2; // asynchronous upload
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
;
23 wfDebug( 'http error: ' . $status->getWikiText() );
29 * Simple wrapper for Http::request( 'GET' )
31 public static function get( $url, $timeout = false, $opts = array() ) {
32 global $wgSyncHTTPTimeout;
34 $opts['timeout'] = $timeout;
35 return Http
::request( 'GET', $url, $opts );
39 * Simple wrapper for Http::request( 'POST' )
41 public static function post( $url, $opts = array() ) {
42 return Http
::request( 'POST', $url, $opts );
45 public static function doDownload( $url, $target_file_path, $dl_mode = self
::SYNC_DOWNLOAD
,
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() ) {
55 $head = $headResponse->value
;
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++
);
64 return Status
::newFatal( 'upload-proto-error' );
67 return Status
::newFatal( 'upload-too-many-redirects' );
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] ) );
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 );
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 );
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();
100 * a non blocking request (generally an exit point in the application)
101 * should write to a file location and give updates
104 private static function initBackgroundDownload( $url, $target_file_path,
105 $content_length = null )
107 global $IP, $wgPhpCli, $wgServer;
108 $status = Status
::newGood();
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();
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;
120 if( $content_length )
121 $_SESSION['wsDownload'][$upload_session_key]['content_length'] = $content_length;
123 // set initial loaded bytes:
124 $_SESSION['wsDownload'][$upload_session_key]['loaded'] = 0;
126 // run the background download request:
127 $cmd = $wgPhpCli . ' ' . $IP . "/maintenance/http_session_download.php " .
128 "--sid {$session_id} --usk {$upload_session_key} --wiki " . wfWikiId();
129 $pid = wfShellBackgroundExec( $cmd );
130 // the pid is not of much use since we won't be visiting this same apache any-time soon.
132 return Status
::newFatal( 'could not run background shell exec' );
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;
138 // return good status
142 static function getUploadSessionKey() {
143 $key = mt_rand( 0, 0x7fffffff );
144 $_SESSION['wsUploadData'][$key] = array();
149 * used to run a session based download. Is initiated via the shell.
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)
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:
165 if( session_start() === false ) {
166 wfDebug( __METHOD__
. ' could not start session' );
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');
173 // setup the global user from the session key we just inherited
174 $wgUser = User
::newFromSession();
176 // grab the session data to setup the request:
177 $sd =& $_SESSION['wsDownload'][$upload_session_key];
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'];
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();
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
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");
201 // start up the session again:
202 if( session_start() === false ) {
203 wfDebug( __METHOD__
. ' ERROR:: Could not start session');
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'] = FormatJson
::encode(
210 array( 'error' => $status->getWikiText() )
213 // if status okay process upload using fauxReq to api:
214 if( $status->isOK() ){
215 // setup the FauxRequest
216 $fauxReqData = $sd['mParams'];
218 // Fix boolean parameters
219 foreach( $fauxReqData as $k => $v ) {
221 unset( $fauxReqData[$k] );
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 );
231 //init the mUpload var for the $processor
232 $processor->execute();
233 $processor->getResult()->cleanUpUTF8();
234 $printer = $processor->createPrinterByName( 'json' );
235 $printer->initPrinter( false );
238 $apiUploadResult = ob_get_clean();
240 // the status updates runner will grab the result form the session:
241 $sd['apiUploadResult'] = $apiUploadResult;
243 // close the session:
244 session_write_close();
248 * Check if the URL can be served by localhost
249 * @param $url string Full url to check
252 public static function isLocalURL( $url ) {
253 global $wgCommandLineMode, $wgConf;
254 if ( $wgCommandLineMode ) {
260 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
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];
269 $domain = $domainPart;
271 $domain = $domainPart . '.' . $domain;
273 if ( $wgConf->isLocalVHost( $domain ) ) {
282 * Return a standard user-agent we can use for external requests.
284 public static function userAgent() {
286 return "MediaWiki/$wgVersion";
290 * Checks that the given URI is a valid one
291 * @param $uri Mixed: URI to check for validity
293 public static function isValidURI( $uri ){
295 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
303 var $target_file_path;
304 var $upload_session_key;
305 function __construct( $url, $opt ){
307 global $wgSyncHTTPTimeout;
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;
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'] : '';
325 $this->ssl_verifyhost
= (isset( $opt['ssl_verifyhost'] ))?
$opt['ssl_verifyhost']: false;
327 $this->cainfo
= (isset( $opt['cainfo'] ))?
$op['cainfo']: false;
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);
336 return new phpHttpRequest($url, $opt);
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'
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();
356 class curlHttpRequest
extends HttpRequest
{
357 public function doReq(){
358 global $wgHTTPProxy, $wgTitle;
360 $status = Status
::newGood();
361 $c = curl_init( $this->url
);
364 if ( Http
::isLocalURL( $this->url
) ) {
365 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
366 } elseif ( $wgHTTPProxy ) {
367 curl_setopt( $c, CURLOPT_PROXY
, $wgHTTPProxy );
370 curl_setopt( $c, CURLOPT_TIMEOUT
, $this->timeout
);
371 curl_setopt( $c, CURLOPT_USERAGENT
, Http
::userAgent() );
373 if($this->ssl_verifyhost
)
374 curl_setopt( $c, CURLOPT_SSL_VERIFYHOST
, $this->ssl_verifyhost
);
377 curl_setopt( $c, CURLOPT_CAINFO
, $this->cainfo
);
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:' ) );
390 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $this->method
);
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
398 if ( is_object( $wgTitle ) ) {
399 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
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
408 if( !$cwrite->status
->isOK() ) {
409 wfDebug( __METHOD__
. "ERROR in setting up simpleFileWriter\n" );
410 $status = $cwrite->status
;
413 curl_setopt( $c, CURLOPT_WRITEFUNCTION
, array( $cwrite, 'callbackWriteBody' ) );
416 // start output grabber:
417 if( !$this->target_file_path
)
420 //run the actual curl_exec:
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 );
427 } catch ( Exception
$e ) {
428 // do something with curl exec error?
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();
435 // if we wrote to a target file close up or return error
436 if( $this->target_file_path
) {
438 if( !$cwrite->status
->isOK() ) {
439 return $cwrite->status
;
443 if ( $this->headers_only
) {
444 $headers = explode( "\n", $status->value
);
445 $headerArray = array();
446 foreach ( $headers as $header ) {
447 if ( !strlen( trim( $header ) ) )
449 $headerParts = explode( ':', $header, 2 );
450 if ( count( $headerParts ) == 1 ) {
451 $headerArray[] = trim( $header );
453 list( $key, $val ) = $headerParts;
454 $headerArray[trim( $key )] = trim( $val );
457 $status->value
= $headerArray;
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" );
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" );
475 // return the result obj
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' );
487 // start with good status:
488 $status = Status
::newGood();
490 if ( $this->headers_only
) {
491 $status->value
= get_headers( $this->url
, 1 );
496 $headers = array( "User-Agent: " . Http
::userAgent() );
497 if ( is_object( $wgTitle ) ) {
498 $headers[] = "Referer: ". $wgTitle->getFullURL();
501 if( strcasecmp( $this->method
, 'post' ) == 0 ) {
502 // Required for HTTP 1.0 POSTs
503 $headers[] = "Content-Length: 0";
505 $fcontext = stream_context_create ( array(
507 'method' => $this->method
,
508 'header' => implode( "\r\n", $headers ),
509 'timeout' => $this->timeout
)
512 $fh = fopen( $this->url
, "r", false, $fcontext);
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
;
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 );
531 // check for simpleFileWriter error:
532 if( !$cwrite->status
->isOK() ) {
533 return $cwrite->status
;
536 // read $fh into status->value
537 $status->value
= @stream_get_contents
( $fh );
539 //close the url file wrapper
543 if( $status->value
=== false ) {
544 $status->error( 'file_get_contents-failed' );
552 * a simpleFileWriter with session id updates
554 class simpleFileWriter
{
555 var $target_file_path;
557 var $session_id = null;
558 var $session_update_interval = 0; // how often to update the session while downloading
560 function simpleFileWriter( $target_file_path, $upload_session_key,
561 $do_close_session_update = false )
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;
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' );
573 $this->prevTime
= time();
576 public function callbackWriteBody( $ch, $data_packet ) {
577 global $wgMaxUploadSize, $wgLang;
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' );
588 $this->current_fsize
= filesize( $this->target_file_path
);
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 ) . ' ' );
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');
608 return strlen( $data_packet );
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' );
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' );
629 // update the progress bytes download so far:
630 $sd['loaded'] = $this->current_fsize
;
632 // close down the session so we can other http queries can get session updates:
633 if( !$wgSessionsInMemcached )
634 session_write_close();
639 public function close() {
640 // do a final session update:
641 if( $this->do_close_session_update
) {
642 $this->update_session_progress();
644 // close up the file handle:
645 if( false === fclose( $this->fp
) ) {
646 $this->status
= Status
::newFatal( 'HTTP::could-not-close-file' );