check that $wgArticle is an instance of the Article class in Skin::pageStats() per...
[lhc/web/wiklou.git] / includes / UploadFromUrl.php
1 <?php
2
3
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 );
9 }
10 static function isEnabled() {
11 global $wgAllowCopyUploads;
12 return $wgAllowCopyUploads && parent::isEnabled();
13 }
14
15 function initialize( $name, $url ) {
16 global $wgTmpDirectory;
17 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
18 $this-initialize( $name, $local_file, 0, true );
19
20 $this->mUrl = trim( $url );
21 }
22
23 /**
24 * Do the real fetching stuff
25 */
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',
31 );
32 }
33 $res = $this->curlCopy();
34 if( $res !== true ) {
35 return array(
36 'status' => self::BEFORE_PROCESSING,
37 'error' => $res,
38 );
39 }
40 return self::OK;
41 }
42
43 /**
44 * Safe copy from URL
45 * Returns true if there was an error, false otherwise
46 */
47 private function curlCopy() {
48 global $wgUser, $wgOut;
49
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';
55 }
56
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 );
66
67 fclose( $this->mCurlDestHandle );
68 unset( $this->mCurlDestHandle );
69
70 if( $error )
71 return "upload-curl-error$errornum";
72
73 return true;
74 }
75
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
81 */
82 function uploadCurlCallback( $ch, $data ) {
83 global $wgMaxUploadSize;
84 $length = strlen( $data );
85 $this->mFileSize += $length;
86 if( $this->mFileSize > $wgMaxUploadSize ) {
87 return 0;
88 }
89 fwrite( $this->mCurlDestHandle, $data );
90 return $length;
91 }
92 }