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() && function_exists( 'curl_init' );
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';
57 $opts = array( CURLOPT_HTTP_VERSION
=> 1.0,
58 CURLOPT_LOW_SPEED_LIMIT
=> 512,
59 CURLOPT_WRITEFUNCTION
=> array( $this, 'uploadCurlCallback' )
61 Http
::get( $this->mUrl
, 10, $opts );
63 fclose( $this->mCurlDestHandle
);
64 unset( $this->mCurlDestHandle
);
66 if( $this->curlErrno
!== CURLE_OK
)
67 return "upload-curl-error" . $this->curlErrno
;
73 * Callback function for CURL-based web transfer
74 * Write data to file unless we've passed the length limit;
75 * if so, abort immediately.
78 function uploadCurlCallback( $ch, $data ) {
79 global $wgMaxUploadSize;
80 $length = strlen( $data );
81 $this->mFileSize +
= $length;
82 if( $this->mFileSize
> $wgMaxUploadSize ) {
85 fwrite( $this->mCurlDestHandle
, $data );
86 $this->curlErrno
= curl_errno( $ch );