Index: trunk/phase3/includes/HttpFunctions.php |
— | — | @@ -255,7 +255,9 @@ |
256 | 256 | |
257 | 257 | function __construct( $url, $opt ){ |
258 | 258 | global $wgSyncHTTPTimeout; |
| 259 | + //double check its a valid url: |
259 | 260 | $this->url = $url; |
| 261 | + |
260 | 262 | // set the timeout to default sync timeout (unless the timeout option is provided) |
261 | 263 | $this->timeout = ( isset( $opt['timeout'] ) ) ? $opt['timeout'] : $wgSyncHTTPTimeout; |
262 | 264 | $this->method = ( isset( $opt['method'] ) ) ? $opt['method'] : 'GET'; |
— | — | @@ -272,6 +274,16 @@ |
273 | 275 | * 'adapter' => 'curl', 'soket' |
274 | 276 | */ |
275 | 277 | public function doRequest() { |
| 278 | + |
| 279 | + #make sure we have a valid url |
| 280 | + if( !UploadFromUrl::isValidURI( $this->url ) ) |
| 281 | + return Status::newFatal('bad-url'); |
| 282 | + |
| 283 | + #check for php.ini allow_url_fopen |
| 284 | + if( ini_get('allow_url_fopen') == 0){ |
| 285 | + return Status::newFatal('allow_url_fopen needs to be enabled for http copy to work'); |
| 286 | + } |
| 287 | + |
276 | 288 | # Use curl if available |
277 | 289 | if ( function_exists( 'curl_init' ) ) { |
278 | 290 | return $this->doCurlReq(); |
— | — | @@ -318,6 +330,7 @@ |
319 | 331 | if( !$cwrite->status->isOK() ){ |
320 | 332 | wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" ); |
321 | 333 | $status = $cwrite->status; |
| 334 | + return $status; |
322 | 335 | } |
323 | 336 | curl_setopt( $c, CURLOPT_WRITEFUNCTION, array( $cwrite, 'callbackWriteBody' ) ); |
324 | 337 | } |
— | — | @@ -369,24 +382,58 @@ |
370 | 383 | } |
371 | 384 | |
372 | 385 | public function doPhpReq(){ |
373 | | - #$use file_get_contents... |
374 | | - # This doesn't have local fetch capabilities... |
| 386 | + global $wgTitle, $wgHTTPProxy; |
| 387 | + //start with good status: |
| 388 | + $status = Status::newGood(); |
375 | 389 | |
| 390 | + //setup the headers |
376 | 391 | $headers = array( "User-Agent: " . Http :: userAgent() ); |
377 | | - if( strcasecmp( $method, 'post' ) == 0 ) { |
| 392 | + if ( is_object( $wgTitle ) ) { |
| 393 | + $headers[] = "Referer: ". $wgTitle->getFullURL(); |
| 394 | + } |
| 395 | + |
| 396 | + if( strcasecmp( $this->method, 'post' ) == 0 ) { |
378 | 397 | // Required for HTTP 1.0 POSTs |
379 | 398 | $headers[] = "Content-Length: 0"; |
380 | 399 | } |
381 | | - $opts = array( |
| 400 | + $fcontext = stream_context_create ( array( |
382 | 401 | 'http' => array( |
383 | | - 'method' => $method, |
| 402 | + 'method' => $this->method, |
384 | 403 | 'header' => implode( "\r\n", $headers ), |
385 | | - 'timeout' => $timeout ) ); |
386 | | - $ctx = stream_context_create( $opts ); |
| 404 | + 'timeout' => $this->timeout ) |
| 405 | + ) |
| 406 | + ); |
387 | 407 | |
388 | | - $status = new Status; |
389 | | - $status->value = file_get_contents( $url, false, $ctx ); |
390 | | - if( !$status->value ){ |
| 408 | + $fh = fopen( $this->url, "r", false, $fcontext); |
| 409 | + |
| 410 | + // set the write back function (if we are writing to a file) |
| 411 | + if( $this->target_file_path ){ |
| 412 | + $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key ); |
| 413 | + if( !$cwrite->status->isOK() ){ |
| 414 | + wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" ); |
| 415 | + $status = $cwrite->status; |
| 416 | + return $status; |
| 417 | + } |
| 418 | + //read $fh into the simpleFileWriter (grab in 64K chunks since its likely a media file) |
| 419 | + while ( !feof( $fh )) { |
| 420 | + $contents = fread($fh, 65536); |
| 421 | + $cwrite->callbackWriteBody($fh, $contents ); |
| 422 | + } |
| 423 | + |
| 424 | + $cwrite->close(); |
| 425 | + //check for simpleFileWriter error: |
| 426 | + if( !$cwrite->status->isOK() ){ |
| 427 | + return $cwrite->status; |
| 428 | + } |
| 429 | + }else{ |
| 430 | + //read $fh into status->value |
| 431 | + $status->value = @stream_get_contents( $fh ); |
| 432 | + } |
| 433 | + //close the url file wrapper |
| 434 | + fclose( $fh ); |
| 435 | + |
| 436 | + //check for "false" |
| 437 | + if( $status->value === false ){ |
391 | 438 | $status->error( 'file_get_contents-failed' ); |
392 | 439 | } |
393 | 440 | return $status; |