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