4 class UploadFromUrl
extends UploadBase
{
5 static function isAllowed( $user ) {
6 if( !$user->isAllowed( 'upload_by_url' ) )
7 return 'upload_by_url';
8 return parent
::isAllowed( $user );
10 static function isEnabled() {
11 global $wgAllowCopyUploads;
12 return $wgAllowCopyUploads && parent
::isEnabled();
15 function initialize( $name, $url ) {
16 global $wgTmpDirectory;
17 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
18 $this-initialize( $name, $local_file, 0, true );
20 $this->mUrl
= trim( $url );
24 * Do the real fetching stuff
26 function fetchFile() {
27 if( stripos($this->mUrl
, 'http://') !== 0 && stripos($this->mUrl
, 'ftp://') !== 0 ) {
29 'status' => self
::BEFORE_PROCESSING
,
30 'error' => 'upload-proto-error',
33 $res = $this->curlCopy();
36 'status' => self
::BEFORE_PROCESSING
,
45 * Returns true if there was an error, false otherwise
47 private function curlCopy() {
48 global $wgUser, $wgOut;
51 $this->mCurlDestHandle
= @fopen
( $this->mTempPath
, "wb" );
52 if( $this->mCurlDestHandle
=== false ) {
53 # Could not open temporary file to write in
54 return 'upload-file-error';
58 curl_setopt( $ch, CURLOPT_HTTP_VERSION
, 1.0); # Probably not needed, but apparently can work around some bug
59 curl_setopt( $ch, CURLOPT_TIMEOUT
, 10); # 10 seconds timeout
60 curl_setopt( $ch, CURLOPT_LOW_SPEED_LIMIT
, 512); # 0.5KB per second minimum transfer speed
61 curl_setopt( $ch, CURLOPT_URL
, $this->mUrl
);
62 curl_setopt( $ch, CURLOPT_WRITEFUNCTION
, array( $this, 'uploadCurlCallback' ) );
64 $error = curl_errno( $ch );
67 fclose( $this->mCurlDestHandle
);
68 unset( $this->mCurlDestHandle
);
71 return "upload-curl-error$errornum";
77 * Callback function for CURL-based web transfer
78 * Write data to file unless we've passed the length limit;
79 * if so, abort immediately.
82 function uploadCurlCallback( $ch, $data ) {
83 global $wgMaxUploadSize;
84 $length = strlen( $data );
85 $this->mFileSize +
= $length;
86 if( $this->mFileSize
> $wgMaxUploadSize ) {
89 fwrite( $this->mCurlDestHandle
, $data );