Remove hard coded colon in h2 after loginerror
[mediawiki.git] / includes / UploadFromUrl.php
blobeb74cc8f0b63fc6d7171949410ee8a1fb23b7096
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() && 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 );
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 $wgUser, $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 $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;
69 return true;
72 /**
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.
76 * @access private
78 function uploadCurlCallback( $ch, $data ) {
79 global $wgMaxUploadSize;
80 $length = strlen( $data );
81 $this->mFileSize += $length;
82 if( $this->mFileSize > $wgMaxUploadSize ) {
83 return 0;
85 fwrite( $this->mCurlDestHandle, $data );
86 $this->curlErrno = curl_errno( $ch );
87 return $length;