Remove two unused $wgUsers.
[mediawiki.git] / includes / UploadFromUrl.php
blob1fb0f383c53c11d570f69cca789ac849f334278a
1 <?php
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 );
23 /**
24 * Do the real fetching stuff
26 function fetchFile() {
27 if( stripos($this->mUrl, 'http://') !== 0 && stripos($this->mUrl, 'ftp://') !== 0 ) {
28 return array(
29 'status' => self::BEFORE_PROCESSING,
30 'error' => 'upload-proto-error',
33 $res = $this->curlCopy();
34 if( $res !== true ) {
35 return array(
36 'status' => self::BEFORE_PROCESSING,
37 'error' => $res,
40 return self::OK;
43 /**
44 * Safe copy from URL
45 * Returns true if there was an error, false otherwise
47 private function curlCopy() {
48 global $wgOut;
50 # Open temporary file
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 $ch = curl_init();
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' ) );
63 curl_exec( $ch );
64 $error = curl_errno( $ch );
65 curl_close( $ch );
67 fclose( $this->mCurlDestHandle );
68 unset( $this->mCurlDestHandle );
70 if( $error )
71 return "upload-curl-error$errornum";
73 return true;
76 /**
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.
80 * @access private
82 function uploadCurlCallback( $ch, $data ) {
83 global $wgMaxUploadSize;
84 $length = strlen( $data );
85 $this->mFileSize += $length;
86 if( $this->mFileSize > $wgMaxUploadSize ) {
87 return 0;
89 fwrite( $this->mCurlDestHandle, $data );
90 return $length;