1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
<?php
/**
* Backend for uploading files from a HTTP resource.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Upload
*/
/**
* Implements uploading from a HTTP resource.
*
* @ingroup Upload
* @author Bryan Tong Minh
* @author Michael Dale
*/
class UploadFromUrl extends UploadBase {
protected $mAsync, $mUrl;
protected $mIgnoreWarnings = true;
protected $mTempPath, $mTmpHandle;
protected static $allowedUrls = array();
/**
* Checks if the user is allowed to use the upload-by-URL feature. If the
* user is not allowed, return the name of the user right as a string. If
* the user is allowed, have the parent do further permissions checking.
*
* @param $user User
*
* @return bool|string
*/
public static function isAllowed( $user ) {
if ( !$user->isAllowed( 'upload_by_url' ) ) {
return 'upload_by_url';
}
return parent::isAllowed( $user );
}
/**
* Checks if the upload from URL feature is enabled
* @return bool
*/
public static function isEnabled() {
global $wgAllowCopyUploads;
return $wgAllowCopyUploads && parent::isEnabled();
}
/**
* Checks whether the URL is for an allowed host
* The domains in the whitelist can include wildcard characters (*) in place
* of any of the domain levels, e.g. '*.flickr.com' or 'upload.*.gov.uk'.
*
* @param $url string
* @return bool
*/
public static function isAllowedHost( $url ) {
global $wgCopyUploadsDomains;
if ( !count( $wgCopyUploadsDomains ) ) {
return true;
}
$parsedUrl = wfParseUrl( $url );
if ( !$parsedUrl ) {
return false;
}
$valid = false;
foreach ( $wgCopyUploadsDomains as $domain ) {
// See if the domain for the upload matches this whitelisted domain
$whitelistedDomainPieces = explode( '.', $domain );
$uploadDomainPieces = explode( '.', $parsedUrl['host'] );
if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
$valid = true;
// See if all the pieces match or not (excluding wildcards)
foreach ( $whitelistedDomainPieces as $index => $piece ) {
if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
$valid = false;
}
}
if ( $valid ) {
// We found a match, so quit comparing against the list
break;
}
}
/* Non-wildcard test
if ( $parsedUrl['host'] === $domain ) {
$valid = true;
break;
}
*/
}
return $valid;
}
/**
* Checks whether the URL is not allowed.
*
* @param $url string
* @return bool
*/
public static function isAllowedUrl( $url ) {
if ( !isset( self::$allowedUrls[$url] ) ) {
$allowed = true;
wfRunHooks( 'IsUploadAllowedFromUrl', array( $url, &$allowed ) );
self::$allowedUrls[$url] = $allowed;
}
return self::$allowedUrls[$url];
}
/**
* Entry point for API upload
*
* @param $name string
* @param $url string
* @param $async mixed Whether the download should be performed
* asynchronous. False for synchronous, async or async-leavemessage for
* asynchronous download.
* @throws MWException
*/
public function initialize( $name, $url, $async = false ) {
global $wgAllowAsyncCopyUploads;
$this->mUrl = $url;
$this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
if ( $async ) {
throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
}
$tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
# File size and removeTempFile will be filled in later
$this->initializePathInfo( $name, $tempPath, 0, false );
}
/**
* Entry point for SpecialUpload
* @param $request WebRequest object
*/
public function initializeFromRequest( &$request ) {
$desiredDestName = $request->getText( 'wpDestFile' );
if ( !$desiredDestName ) {
$desiredDestName = $request->getText( 'wpUploadFileURL' );
}
$this->initialize(
$desiredDestName,
trim( $request->getVal( 'wpUploadFileURL' ) ),
false
);
}
/**
* @param $request WebRequest object
* @return bool
*/
public static function isValidRequest( $request ) {
global $wgUser;
$url = $request->getVal( 'wpUploadFileURL' );
return !empty( $url )
&& Http::isValidURI( $url )
&& $wgUser->isAllowed( 'upload_by_url' );
}
/**
* @return string
*/
public function getSourceType() {
return 'url';
}
/**
* Download the file (if not async)
*
* @param Array $httpOptions Array of options for MWHttpRequest. Ignored if async.
* This could be used to override the timeout on the http request.
* @return Status
*/
public function fetchFile( $httpOptions = array() ) {
if ( !Http::isValidURI( $this->mUrl ) ) {
return Status::newFatal( 'http-invalid-url' );
}
if ( !self::isAllowedHost( $this->mUrl ) ) {
return Status::newFatal( 'upload-copy-upload-invalid-domain' );
}
if ( !self::isAllowedUrl( $this->mUrl ) ) {
return Status::newFatal( 'upload-copy-upload-invalid-url' );
}
if ( !$this->mAsync ) {
return $this->reallyFetchFile( $httpOptions );
}
return Status::newGood();
}
/**
* Create a new temporary file in the URL subdirectory of wfTempDir().
*
* @return string Path to the file
*/
protected function makeTemporaryFile() {
return tempnam( wfTempDir(), 'URL' );
}
/**
* Callback: save a chunk of the result of a HTTP request to the temporary file
*
* @param $req mixed
* @param $buffer string
* @return int number of bytes handled
*/
public function saveTempFileChunk( $req, $buffer ) {
$nbytes = fwrite( $this->mTmpHandle, $buffer );
if ( $nbytes == strlen( $buffer ) ) {
$this->mFileSize += $nbytes;
} else {
// Well... that's not good!
fclose( $this->mTmpHandle );
$this->mTmpHandle = false;
}
return $nbytes;
}
/**
* Download the file, save it to the temporary file and update the file
* size and set $mRemoveTempFile to true.
*
* @param Array $httpOptions Array of options for MWHttpRequest
* @return Status
*/
protected function reallyFetchFile( $httpOptions = array() ) {
global $wgCopyUploadProxy, $wgCopyUploadTimeout;
if ( $this->mTempPath === false ) {
return Status::newFatal( 'tmp-create-error' );
}
// Note the temporary file should already be created by makeTemporaryFile()
$this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
if ( !$this->mTmpHandle ) {
return Status::newFatal( 'tmp-create-error' );
}
$this->mRemoveTempFile = true;
$this->mFileSize = 0;
$options = $httpOptions + array(
'followRedirects' => true,
);
if ( $wgCopyUploadProxy !== false ) {
$options['proxy'] = $wgCopyUploadProxy;
}
if ( $wgCopyUploadTimeout && !isset( $options['timeout'] ) ) {
$options['timeout'] = $wgCopyUploadTimeout;
}
$req = MWHttpRequest::factory( $this->mUrl, $options );
$req->setCallback( array( $this, 'saveTempFileChunk' ) );
$status = $req->execute();
if ( $this->mTmpHandle ) {
// File got written ok...
fclose( $this->mTmpHandle );
$this->mTmpHandle = null;
} else {
// We encountered a write error during the download...
return Status::newFatal( 'tmp-write-error' );
}
if ( !$status->isOk() ) {
return $status;
}
return $status;
}
/**
* Wrapper around the parent function in order to defer verifying the
* upload until the file really has been fetched.
* @return array|mixed
*/
public function verifyUpload() {
if ( $this->mAsync ) {
return array( 'status' => UploadBase::OK );
}
return parent::verifyUpload();
}
/**
* Wrapper around the parent function in order to defer checking warnings
* until the file really has been fetched.
* @return Array
*/
public function checkWarnings() {
if ( $this->mAsync ) {
$this->mIgnoreWarnings = false;
return array();
}
return parent::checkWarnings();
}
/**
* Wrapper around the parent function in order to defer checking protection
* until we are sure that the file can actually be uploaded
* @param $user User
* @return bool|mixed
*/
public function verifyTitlePermissions( $user ) {
if ( $this->mAsync ) {
return true;
}
return parent::verifyTitlePermissions( $user );
}
/**
* Wrapper around the parent function in order to defer uploading to the
* job queue for asynchronous uploads
* @param $comment string
* @param $pageText string
* @param $watch bool
* @param $user User
* @return Status
*/
public function performUpload( $comment, $pageText, $watch, $user ) {
if ( $this->mAsync ) {
$sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
return Status::newFatal( 'async', $sessionKey );
}
return parent::performUpload( $comment, $pageText, $watch, $user );
}
/**
* @param $comment
* @param $pageText
* @param $watch
* @param $user User
* @return String
*/
protected function insertJob( $comment, $pageText, $watch, $user ) {
$sessionKey = $this->stashSession();
$job = new UploadFromUrlJob( $this->getTitle(), array(
'url' => $this->mUrl,
'comment' => $comment,
'pageText' => $pageText,
'watch' => $watch,
'userName' => $user->getName(),
'leaveMessage' => $this->mAsync == 'async-leavemessage',
'ignoreWarnings' => $this->mIgnoreWarnings,
'sessionId' => session_id(),
'sessionKey' => $sessionKey,
) );
$job->initializeSessionData();
JobQueueGroup::singleton()->push( $job );
return $sessionKey;
}
}
|