a10f0d4a33074dd7907f405c6b93467454defee1
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( $url, $opts = array() ) {
15 $req = new HttpRequest( $url, $opts );
16 $status = $req->doRequest();
17 if( $status->isOK() ){
18 return $status->value
;
25 * Simple wrapper for Http::request( 'GET' )
27 public static function get( $url, $opts = array() ) {
28 $opt['method'] = 'GET';
29 return Http
::request( $url, $opts );
33 * Simple wrapper for Http::request( 'POST' )
35 public static function post( $url, $opts = array() ) {
36 $opts['method'] = 'POST';
37 return Http
::request( $url, $opts );
40 public static function doDownload( $url, $target_file_path , $dl_mode = self
::SYNC_DOWNLOAD
, $redirectCount = 0 ){
41 global $wgPhpCliPath, $wgMaxUploadSize, $wgMaxRedirects;
42 // do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize
43 $head = get_headers( $url, 1 );
45 // check for redirects:
46 if( isset( $head['Location'] ) && strrpos( $head[0], '302' ) !== false ){
47 if( $redirectCount < $wgMaxRedirects ){
48 if( UploadFromUrl
::isValidURI( $head['Location'] ) ){
49 return self
::doDownload( $head['Location'], $target_file_path , $dl_mode, $redirectCount++
);
51 return Status
::newFatal( 'upload-proto-error' );
54 return Status
::newFatal( 'upload-too-many-redirects' );
57 // we did not get a 200 ok response:
58 if( strrpos( $head[0], '200 OK' ) === false ){
59 return Status
::newFatal( 'upload-http-error', htmlspecialchars( $head[0] ) );
62 $content_length = ( isset( $head['Content-Length'] ) ) ?
$head['Content-Length'] : null;
63 if( $content_length ){
64 if( $content_length > $wgMaxUploadSize ){
65 return Status
::newFatal( 'requested file length ' . $content_length . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize );
69 // check if we can find phpCliPath (for doing a background shell request to php to do the download:
70 if( $wgPhpCliPath && wfShellExecEnabled() && $dl_mode == self
::ASYNC_DOWNLOAD
){
71 wfDebug( __METHOD__
. "\ASYNC_DOWNLOAD\n" );
72 // setup session and shell call:
73 return self
::initBackgroundDownload( $url, $target_file_path, $content_length );
74 } else if( $dl_mode == self
::SYNC_DOWNLOAD
){
75 wfDebug( __METHOD__
. "\nSYNC_DOWNLOAD\n" );
76 // SYNC_DOWNLOAD download as much as we can in the time we have to execute
77 $opts['method'] = 'GET';
78 $opts['target_file_path'] = $target_file_path;
79 $req = new HttpRequest( $url, $opts );
80 return $req->doRequest();
85 * a non blocking request (generally an exit point in the application)
86 * should write to a file location and give updates
89 private function initBackgroundDownload( $url, $target_file_path, $content_length = null ){
90 global $wgMaxUploadSize, $IP, $wgPhpCliPath;
91 $status = Status
::newGood();
93 // generate a session id with all the details for the download (pid, target_file_path )
94 $upload_session_key = self
::getUploadSessionKey();
95 $session_id = session_id();
97 // store the url and target path:
98 $_SESSION['wsDownload'][$upload_session_key]['url'] = $url;
99 $_SESSION['wsDownload'][$upload_session_key]['target_file_path'] = $target_file_path;
101 if( $content_length )
102 $_SESSION['wsDownload'][$upload_session_key]['content_length'] = $content_length;
104 // set initial loaded bytes:
105 $_SESSION['wsDownload'][$upload_session_key]['loaded'] = 0;
107 // run the background download request:
108 $cmd = $wgPhpCliPath . ' ' . $IP . "/maintenance/http_session_download.php --sid {$session_id} --usk {$upload_session_key}";
109 $pid = wfShellBackgroundExec( $cmd, $retval );
110 // the pid is not of much use since we won't be visiting this same apache any-time soon.
112 return Status
::newFatal( 'could not run background shell exec' );
114 // update the status value with the $upload_session_key (for the user to check on the status of the upload)
115 $status->value
= $upload_session_key;
117 // return good status
121 function getUploadSessionKey(){
122 $key = mt_rand( 0, 0x7fffffff );
123 $_SESSION['wsUploadData'][$key] = array();
128 * used to run a session based download. Is initiated via the shell.
130 * @param $session_id String: the session id to grab download details from
131 * @param $upload_session_key String: the key of the given upload session
132 * (a given client could have started a few http uploads at once)
134 public static function doSessionIdDownload( $session_id, $upload_session_key ){
135 global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout;
136 wfDebug( __METHOD__
. "\n\ndoSessionIdDownload\n\n" );
137 // set session to the provided key:
138 session_id( $session_id );
140 if( session_start() === false ){
141 wfDebug( __METHOD__
. ' could not start session' );
143 //get all the vars we need from session_id
144 if(!isset($_SESSION[ 'wsDownload' ][$upload_session_key])){
145 wfDebug( __METHOD__
.' Error:could not find upload session');
148 // setup the global user from the session key we just inherited
149 $wgUser = User
::newFromSession();
151 // grab the session data to setup the request:
152 $sd =& $_SESSION['wsDownload'][$upload_session_key];
153 // close down the session so we can other http queries can get session updates:
154 session_write_close();
156 $req = new HttpRequest( $sd['url'], array(
157 'target_file_path' => $sd['target_file_path'],
158 'upload_session_key'=> $upload_session_key,
159 'timeout' => $wgAsyncHTTPTimeout
161 // run the actual request .. (this can take some time)
162 wfDebug( __METHOD__
. "do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] );
163 $status = $req->doRequest();
164 //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n");
166 // start up the session again:
167 if( session_start() === false ){
168 wfDebug( __METHOD__
. ' ERROR:: Could not start session');
170 // grab the updated session data pointer
171 $sd =& $_SESSION['wsDownload'][$upload_session_key];
172 // if error update status:
173 if( !$status->isOK() ){
174 $sd['apiUploadResult'] = ApiFormatJson
::getJsonEncode(
175 array( 'error' => $status->getWikiText() )
178 // if status okay process upload using fauxReq to api:
179 if( $status->isOK() ){
180 // setup the FauxRequest
181 $fauxReqData = $sd['mParams'];
182 $fauxReqData['action'] = 'upload';
183 $fauxReqData['format'] = 'json';
184 $fauxReqData['internalhttpsession'] = $upload_session_key;
186 // evil but no other clean way about it:
187 $faxReq = new FauxRequest( $fauxReqData, true );
188 $processor = new ApiMain( $faxReq, $wgEnableWriteAPI );
190 //init the mUpload var for the $processor
191 $processor->execute();
192 $processor->getResult()->cleanUpUTF8();
193 $printer = $processor->createPrinterByName( 'json' );
194 $printer->initPrinter( false );
197 $apiUploadResult = ob_get_clean();
199 wfDebug( __METHOD__
. "\n\n got api result:: $apiUploadResult \n" );
200 // the status updates runner will grab the result form the session:
201 $sd['apiUploadResult'] = $apiUploadResult;
203 // close the session:
204 session_write_close();
208 * Check if the URL can be served by localhost
209 * @param $url string Full url to check
212 public static function isLocalURL( $url ) {
213 global $wgCommandLineMode, $wgConf;
214 if ( $wgCommandLineMode ) {
220 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
223 $domainParts = explode( '.', $host );
224 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
225 $domainParts = array_reverse( $domainParts );
226 for ( $i = 0; $i < count( $domainParts ); $i++
) {
227 $domainPart = $domainParts[$i];
229 $domain = $domainPart;
231 $domain = $domainPart . '.' . $domain;
233 if ( $wgConf->isLocalVHost( $domain ) ) {
242 * Return a standard user-agent we can use for external requests.
244 public static function userAgent() {
246 return "MediaWiki/$wgVersion";
250 var $target_file_path;
251 var $upload_session_key;
253 function __construct( $url, $opt ){
254 global $wgSyncHTTPTimeout;
256 // set the timeout to default sync timeout (unless the timeout option is provided)
257 $this->timeout
= ( isset( $opt['timeout'] ) ) ?
$opt['timeout'] : $wgSyncHTTPTimeout;
258 $this->method
= ( isset( $opt['method'] ) ) ?
$opt['method'] : 'GET';
259 $this->target_file_path
= ( isset( $opt['target_file_path'] ) ) ?
$opt['target_file_path'] : false;
260 $this->upload_session_key
= ( isset( $opt['upload_session_key'] ) ) ?
$opt['upload_session_key'] : false;
264 * Get the contents of a file by HTTP
265 * @param $url string Full URL to act on
266 * @param $Opt associative array Optional array of options:
267 * 'method' => 'GET', 'POST' etc.
268 * 'target_file_path' => if curl should output to a target file
269 * 'adapter' => 'curl', 'soket'
271 public function doRequest() {
272 # Use curl if available
273 if ( function_exists( 'curl_init' ) ) {
274 return $this->doCurlReq();
276 return $this->doPhpReq();
280 private function doCurlReq(){
281 global $wgHTTPProxy, $wgTitle;
283 $status = Status
::newGood();
284 $c = curl_init( $this->url
);
287 if ( Http
::isLocalURL( $this->url
) ) {
288 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
289 } else if ( $wgHTTPProxy ) {
290 curl_setopt( $c, CURLOPT_PROXY
, $wgHTTPProxy );
293 curl_setopt( $c, CURLOPT_TIMEOUT
, $this->timeout
);
294 curl_setopt( $c, CURLOPT_USERAGENT
, Http
::userAgent() );
296 if ( $this->method
== 'POST' ) {
297 curl_setopt( $c, CURLOPT_POST
, true );
298 curl_setopt( $c, CURLOPT_POSTFIELDS
, '' );
300 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $this->method
);
303 # Set the referer to $wgTitle, even in command-line mode
304 # This is useful for interwiki transclusion, where the foreign
305 # server wants to know what the referring page is.
306 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
308 if ( is_object( $wgTitle ) ) {
309 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
312 // set the write back function (if we are writing to a file)
313 if( $this->target_file_path
){
314 $cwrite = new simpleFileWriter( $this->target_file_path
, $this->upload_session_key
);
315 if( !$cwrite->status
->isOK() ){
316 wfDebug( __METHOD__
. "ERROR in setting up simpleFileWriter\n" );
317 $status = $cwrite->status
;
319 curl_setopt( $c, CURLOPT_WRITEFUNCTION
, array( $cwrite, 'callbackWriteBody' ) );
322 // start output grabber:
323 if( !$this->target_file_path
)
326 //run the actual curl_exec:
328 if ( false === curl_exec( $c ) ) {
329 $error_txt ='Error sending request: #' . curl_errno( $c ) .' '. curl_error( $c );
330 wfDebug( __METHOD__
. $error_txt . "\n" );
331 $status = Status
::newFatal( $error_txt );
333 } catch ( Exception
$e ) {
334 // do something with curl exec error?
336 // if direct request output the results to the stats value:
337 if( !$this->target_file_path
&& $status->isOK() ){
338 $status->value
= ob_get_contents();
341 // if we wrote to a target file close up or return error
342 if( $this->target_file_path
){
344 if( !$cwrite->status
->isOK() ){
345 return $cwrite->status
;
349 # Don't return the text of error messages, return false on error
350 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE
);
351 if ( $retcode != 200 ) {
352 wfDebug( __METHOD__
. ": HTTP return code $retcode\n" );
353 $status = Status
::newFatal( "HTTP return code $retcode\n" );
355 # Don't return truncated output
356 $errno = curl_errno( $c );
357 if ( $errno != CURLE_OK
) {
358 $errstr = curl_error( $c );
359 wfDebug( __METHOD__
. ": CURL error code $errno: $errstr\n" );
360 $status = Status
::newFatal( " CURL error code $errno: $errstr\n" );
364 // return the result obj
368 public function doPhpReq(){
369 #$use file_get_contents...
370 # This doesn't have local fetch capabilities...
372 $headers = array( "User-Agent: " . self
:: userAgent() );
373 if( strcasecmp( $method, 'post' ) == 0 ) {
374 // Required for HTTP 1.0 POSTs
375 $headers[] = "Content-Length: 0";
380 'header' => implode( "\r\n", $headers ),
381 'timeout' => $timeout ) );
382 $ctx = stream_context_create( $opts );
384 $status->value
= file_get_contents( $url, false, $ctx );
385 if( !$status->value
){
386 $status->error( 'file_get_contents-failed' );
394 * a simpleFileWriter with session id updates
396 class simpleFileWriter
{
397 var $target_file_path;
399 var $session_id = null;
400 var $session_update_interval = 0; // how often to update the session while downloading
402 function simpleFileWriter( $target_file_path, $upload_session_key ){
403 $this->target_file_path
= $target_file_path;
404 $this->upload_session_key
= $upload_session_key;
405 $this->status
= Status
::newGood();
407 $this->fp
= fopen( $this->target_file_path
, 'w' );
408 if( $this->fp
=== false ){
409 $this->status
= Status
::newFatal( 'HTTP::could-not-open-file-for-writing' );
412 $this->prevTime
= time();
415 public function callbackWriteBody($ch, $data_packet){
416 global $wgMaxUploadSize;
418 // write out the content
419 if( fwrite( $this->fp
, $data_packet ) === false ){
420 wfDebug( __METHOD__
." ::could-not-write-to-file\n" );
421 $this->status
= Status
::newFatal( 'HTTP::could-not-write-to-file' );
427 $this->current_fsize
= filesize( $this->target_file_path
);
429 if( $this->current_fsize
> $wgMaxUploadSize ){
430 wfDebug( __METHOD__
. " ::http download too large\n" );
431 $this->status
= Status
::newFatal( 'HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' .
432 Language
::formatSize( $wgMaxUploadSize ) . ' ' );
436 // if more than session_update_interval second have passed update_session_progress
437 if( $this->upload_session_key
&& ( ( time() - $this->prevTime
) > $this->session_update_interval
) ) {
438 $this->prevTime
= time();
439 $session_status = $this->update_session_progress();
440 if( !$session_status->isOK() ){
441 $this->status
= $session_status;
442 wfDebug( __METHOD__
. ' update session failed or was canceled');
446 return strlen( $data_packet );
449 public function update_session_progress(){
450 $status = Status
::newGood();
452 if( session_start() === false){
453 wfDebug( __METHOD__
. ' could not start session' );
456 $sd =& $_SESSION['wsDownload'][$this->upload_session_key
];
457 // check if the user canceled the request:
458 if( $sd['user_cancel'] == true ){
460 return Status
::newFatal( 'user-canceled-request' );
462 // update the progress bytes download so far:
463 $sd['loaded'] = $this->current_fsize
;
464 wfDebug( __METHOD__
. ': set session loaded amount to: ' . $sd['loaded'] . "\n");
465 // close down the session so we can other http queries can get session updates:
466 session_write_close();
470 public function close(){
471 // do a final session update:
472 $this->update_session_progress();
473 // close up the file handle:
474 if( false === fclose( $this->fp
) ){
475 $this->status
= Status
::newFatal( 'HTTP::could-not-close-file' );