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)
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
;
21 wfDebug( 'http error: ' . $status->getWikiText() );
26 * Simple wrapper for Http::request( 'GET' )
28 public static function get( $url, $timeout = false ) {
31 $opts['timeout'] = $timeout;
32 return Http
::request( 'GET', $url, $opts );
36 * Simple wrapper for Http::request( 'POST' )
38 public static function post( $url, $opts = array() ) {
39 return Http
::request( 'POST', $url, $opts );
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() ){
50 $head = $headResponse->value
;
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++
);
58 return Status
::newFatal( 'upload-proto-error' );
61 return Status
::newFatal( 'upload-too-many-redirects' );
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] ) );
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 );
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();
92 * a non blocking request (generally an exit point in the application)
93 * should write to a file location and give updates
96 private function initBackgroundDownload( $url, $target_file_path, $content_length = null ){
97 global $wgMaxUploadSize, $IP, $wgPhpCli;
98 $status = Status
::newGood();
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();
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;
108 if( $content_length )
109 $_SESSION['wsDownload'][$upload_session_key]['content_length'] = $content_length;
111 // set initial loaded bytes:
112 $_SESSION['wsDownload'][$upload_session_key]['loaded'] = 0;
114 // run the background download request:
115 $cmd = $wgPhpCli . ' ' . $IP . "/maintenance/http_session_download.php --sid {$session_id} --usk {$upload_session_key}";
116 $pid = wfShellBackgroundExec( $cmd, $retval );
117 // the pid is not of much use since we won't be visiting this same apache any-time soon.
119 return Status
::newFatal( 'could not run background shell exec' );
121 // update the status value with the $upload_session_key (for the user to check on the status of the upload)
122 $status->value
= $upload_session_key;
124 // return good status
128 function getUploadSessionKey(){
129 $key = mt_rand( 0, 0x7fffffff );
130 $_SESSION['wsUploadData'][$key] = array();
135 * used to run a session based download. Is initiated via the shell.
137 * @param $session_id String: the session id to grab download details from
138 * @param $upload_session_key String: the key of the given upload session
139 * (a given client could have started a few http uploads at once)
141 public static function doSessionIdDownload( $session_id, $upload_session_key ){
142 global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout;
143 wfDebug( __METHOD__
. "\n\ndoSessionIdDownload\n\n" );
144 // set session to the provided key:
145 session_id( $session_id );
147 if( session_start() === false ){
148 wfDebug( __METHOD__
. ' could not start session' );
150 //get all the vars we need from session_id
151 if(!isset($_SESSION[ 'wsDownload' ][$upload_session_key])){
152 wfDebug( __METHOD__
.' Error:could not find upload session');
155 // setup the global user from the session key we just inherited
156 $wgUser = User
::newFromSession();
158 // grab the session data to setup the request:
159 $sd =& $_SESSION['wsDownload'][$upload_session_key];
160 // close down the session so we can other http queries can get session updates:
161 session_write_close();
163 $req = new HttpRequest( $sd['url'], array(
164 'target_file_path' => $sd['target_file_path'],
165 'upload_session_key'=> $upload_session_key,
166 'timeout' => $wgAsyncHTTPTimeout
168 // run the actual request .. (this can take some time)
169 wfDebug( __METHOD__
. "do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] );
170 $status = $req->doRequest();
171 //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n");
173 // start up the session again:
174 if( session_start() === false ){
175 wfDebug( __METHOD__
. ' ERROR:: Could not start session');
177 // grab the updated session data pointer
178 $sd =& $_SESSION['wsDownload'][$upload_session_key];
179 // if error update status:
180 if( !$status->isOK() ){
181 $sd['apiUploadResult'] = ApiFormatJson
::getJsonEncode(
182 array( 'error' => $status->getWikiText() )
185 // if status okay process upload using fauxReq to api:
186 if( $status->isOK() ){
187 // setup the FauxRequest
188 $fauxReqData = $sd['mParams'];
189 $fauxReqData['action'] = 'upload';
190 $fauxReqData['format'] = 'json';
191 $fauxReqData['internalhttpsession'] = $upload_session_key;
193 // evil but no other clean way about it:
194 $faxReq = new FauxRequest( $fauxReqData, true );
195 $processor = new ApiMain( $faxReq, $wgEnableWriteAPI );
197 //init the mUpload var for the $processor
198 $processor->execute();
199 $processor->getResult()->cleanUpUTF8();
200 $printer = $processor->createPrinterByName( 'json' );
201 $printer->initPrinter( false );
204 $apiUploadResult = ob_get_clean();
206 wfDebug( __METHOD__
. "\n\n got api result:: $apiUploadResult \n" );
207 // the status updates runner will grab the result form the session:
208 $sd['apiUploadResult'] = $apiUploadResult;
210 // close the session:
211 session_write_close();
215 * Check if the URL can be served by localhost
216 * @param $url string Full url to check
219 public static function isLocalURL( $url ) {
220 global $wgCommandLineMode, $wgConf;
221 if ( $wgCommandLineMode ) {
227 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
230 $domainParts = explode( '.', $host );
231 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
232 $domainParts = array_reverse( $domainParts );
233 for ( $i = 0; $i < count( $domainParts ); $i++
) {
234 $domainPart = $domainParts[$i];
236 $domain = $domainPart;
238 $domain = $domainPart . '.' . $domain;
240 if ( $wgConf->isLocalVHost( $domain ) ) {
249 * Return a standard user-agent we can use for external requests.
251 public static function userAgent() {
253 return "MediaWiki/$wgVersion";
257 var $target_file_path;
258 var $upload_session_key;
260 function __construct( $url, $opt ){
261 global $wgSyncHTTPTimeout;
262 //double check its a valid url:
265 // set the timeout to default sync timeout (unless the timeout option is provided)
266 $this->timeout
= ( isset( $opt['timeout'] ) ) ?
$opt['timeout'] : $wgSyncHTTPTimeout;
267 $this->method
= ( isset( $opt['method'] ) ) ?
$opt['method'] : 'GET';
268 $this->target_file_path
= ( isset( $opt['target_file_path'] ) ) ?
$opt['target_file_path'] : false;
269 $this->upload_session_key
= ( isset( $opt['upload_session_key'] ) ) ?
$opt['upload_session_key'] : false;
270 $this->headers_only
= ( isset( $opt['headers_only'] ) ) ?
$opt['headers_only'] : false;
274 * Get the contents of a file by HTTP
275 * @param $url string Full URL to act on
276 * @param $Opt associative array Optional array of options:
277 * 'method' => 'GET', 'POST' etc.
278 * 'target_file_path' => if curl should output to a target file
279 * 'adapter' => 'curl', 'soket'
281 public function doRequest() {
283 #make sure we have a valid url
284 if( !UploadFromUrl
::isValidURI( $this->url
) )
285 return Status
::newFatal('bad-url');
287 # Use curl if available
288 if ( function_exists( 'curl_init' ) ) {
289 return $this->doCurlReq();
291 return $this->doPhpReq();
295 private function doCurlReq(){
296 global $wgHTTPProxy, $wgTitle;
298 $status = Status
::newGood();
299 $c = curl_init( $this->url
);
302 if ( Http
::isLocalURL( $this->url
) ) {
303 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
304 } else if ( $wgHTTPProxy ) {
305 curl_setopt( $c, CURLOPT_PROXY
, $wgHTTPProxy );
308 curl_setopt( $c, CURLOPT_TIMEOUT
, $this->timeout
);
309 curl_setopt( $c, CURLOPT_USERAGENT
, Http
::userAgent() );
311 if ( $this->headers_only
) {
312 curl_setopt( $c, CURLOPT_NOBODY
, true );
313 curl_setopt( $c, CURLOPT_HEADER
, true );
314 } elseif ( $this->method
== 'POST' ) {
315 curl_setopt( $c, CURLOPT_POST
, true );
316 curl_setopt( $c, CURLOPT_POSTFIELDS
, '' );
318 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $this->method
);
321 # Set the referer to $wgTitle, even in command-line mode
322 # This is useful for interwiki transclusion, where the foreign
323 # server wants to know what the referring page is.
324 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
326 if ( is_object( $wgTitle ) ) {
327 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
330 // set the write back function (if we are writing to a file)
331 if( $this->target_file_path
){
332 $cwrite = new simpleFileWriter( $this->target_file_path
, $this->upload_session_key
);
333 if( !$cwrite->status
->isOK() ){
334 wfDebug( __METHOD__
. "ERROR in setting up simpleFileWriter\n" );
335 $status = $cwrite->status
;
338 curl_setopt( $c, CURLOPT_WRITEFUNCTION
, array( $cwrite, 'callbackWriteBody' ) );
341 // start output grabber:
342 if( !$this->target_file_path
)
345 //run the actual curl_exec:
347 if ( false === curl_exec( $c ) ) {
348 $error_txt ='Error sending request: #' . curl_errno( $c ) .' '. curl_error( $c );
349 wfDebug( __METHOD__
. $error_txt . "\n" );
350 $status = Status
::newFatal( $error_txt );
352 } catch ( Exception
$e ) {
353 // do something with curl exec error?
355 // if direct request output the results to the stats value:
356 if( !$this->target_file_path
&& $status->isOK() ){
357 $status->value
= ob_get_contents();
360 // if we wrote to a target file close up or return error
361 if( $this->target_file_path
){
363 if( !$cwrite->status
->isOK() ){
364 return $cwrite->status
;
368 if ( $this->headers_only
) {
369 $headers = explode( "\n", $status->value
);
370 $headerArray = array();
371 foreach ( $headers as $header ) {
372 if ( !strlen( trim( $header ) ) )
374 $headerParts = explode( ':', $header, 2 );
375 if ( count( $headerParts ) == 1 ) {
376 $headerArray[] = trim( $header );
378 list( $key, $val ) = $headerParts;
379 $headerArray[trim( $key )] = trim( $val );
382 $status->value
= $headerArray;
384 # Don't return the text of error messages, return false on error
385 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE
);
386 if ( $retcode != 200 ) {
387 wfDebug( __METHOD__
. ": HTTP return code $retcode\n" );
388 $status = Status
::newFatal( "HTTP return code $retcode\n" );
390 # Don't return truncated output
391 $errno = curl_errno( $c );
392 if ( $errno != CURLE_OK
) {
393 $errstr = curl_error( $c );
394 wfDebug( __METHOD__
. ": CURL error code $errno: $errstr\n" );
395 $status = Status
::newFatal( " CURL error code $errno: $errstr\n" );
401 // return the result obj
405 public function doPhpReq(){
406 global $wgTitle, $wgHTTPProxy;
408 ini_set( 'allow_url_fopen',1 );
409 #check for php.ini allow_url_fopen
410 if( !ini_get( 'allow_url_fopen' ) ){
411 return Status
::newFatal( 'allow_url_fopen needs to be enabled for http copy to work' );
414 //start with good status:
415 $status = Status
::newGood();
417 if ( $this->headers_only
) {
418 $status->value
= get_headers( $this->url
, 1 );
423 $headers = array( "User-Agent: " . Http
:: userAgent() );
424 if ( is_object( $wgTitle ) ) {
425 $headers[] = "Referer: ". $wgTitle->getFullURL();
428 if( strcasecmp( $this->method
, 'post' ) == 0 ) {
429 // Required for HTTP 1.0 POSTs
430 $headers[] = "Content-Length: 0";
432 $fcontext = stream_context_create ( array(
434 'method' => $this->method
,
435 'header' => implode( "\r\n", $headers ),
436 'timeout' => $this->timeout
)
440 $fh = fopen( $this->url
, "r", false, $fcontext);
442 // set the write back function (if we are writing to a file)
443 if( $this->target_file_path
){
444 $cwrite = new simpleFileWriter( $this->target_file_path
, $this->upload_session_key
);
445 if( !$cwrite->status
->isOK() ){
446 wfDebug( __METHOD__
. "ERROR in setting up simpleFileWriter\n" );
447 $status = $cwrite->status
;
450 //read $fh into the simpleFileWriter (grab in 64K chunks since its likely a media file)
451 while ( !feof( $fh )) {
452 $contents = fread($fh, 65536);
453 $cwrite->callbackWriteBody($fh, $contents );
457 //check for simpleFileWriter error:
458 if( !$cwrite->status
->isOK() ){
459 return $cwrite->status
;
462 //read $fh into status->value
463 $status->value
= @stream_get_contents
( $fh );
465 //close the url file wrapper
469 if( $status->value
=== false ){
470 $status->error( 'file_get_contents-failed' );
478 * a simpleFileWriter with session id updates
480 class simpleFileWriter
{
481 var $target_file_path;
483 var $session_id = null;
484 var $session_update_interval = 0; // how often to update the session while downloading
486 function simpleFileWriter( $target_file_path, $upload_session_key ){
487 $this->target_file_path
= $target_file_path;
488 $this->upload_session_key
= $upload_session_key;
489 $this->status
= Status
::newGood();
491 $this->fp
= fopen( $this->target_file_path
, 'w' );
492 if( $this->fp
=== false ){
493 $this->status
= Status
::newFatal( 'HTTP::could-not-open-file-for-writing' );
496 $this->prevTime
= time();
499 public function callbackWriteBody($ch, $data_packet){
500 global $wgMaxUploadSize;
502 // write out the content
503 if( fwrite( $this->fp
, $data_packet ) === false ){
504 wfDebug( __METHOD__
." ::could-not-write-to-file\n" );
505 $this->status
= Status
::newFatal( 'HTTP::could-not-write-to-file' );
511 $this->current_fsize
= filesize( $this->target_file_path
);
513 if( $this->current_fsize
> $wgMaxUploadSize ){
514 wfDebug( __METHOD__
. " ::http download too large\n" );
515 $this->status
= Status
::newFatal( 'HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' .
516 Language
::formatSize( $wgMaxUploadSize ) . ' ' );
520 // if more than session_update_interval second have passed update_session_progress
521 if( $this->upload_session_key
&& ( ( time() - $this->prevTime
) > $this->session_update_interval
) ) {
522 $this->prevTime
= time();
523 $session_status = $this->update_session_progress();
524 if( !$session_status->isOK() ){
525 $this->status
= $session_status;
526 wfDebug( __METHOD__
. ' update session failed or was canceled');
530 return strlen( $data_packet );
533 public function update_session_progress(){
534 $status = Status
::newGood();
536 if( session_start() === false){
537 wfDebug( __METHOD__
. ' could not start session' );
540 $sd =& $_SESSION['wsDownload'][$this->upload_session_key
];
541 // check if the user canceled the request:
542 if( isset( $sd['user_cancel'] ) && $sd['user_cancel'] == true ){
544 return Status
::newFatal( 'user-canceled-request' );
546 // update the progress bytes download so far:
547 $sd['loaded'] = $this->current_fsize
;
548 wfDebug( __METHOD__
. ': set session loaded amount to: ' . $sd['loaded'] . "\n");
549 // close down the session so we can other http queries can get session updates:
550 session_write_close();
554 public function close(){
555 // do a final session update:
556 $this->update_session_progress();
557 // close up the file handle:
558 if( false === fclose( $this->fp
) ){
559 $this->status
= Status
::newFatal( 'HTTP::could-not-close-file' );