3 * Backend for uploading files from a HTTP resource.
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.
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.
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
25 * Implements uploading from a HTTP resource.
28 * @author Bryan Tong Minh
29 * @author Michael Dale
31 class UploadFromUrl
extends UploadBase
{
32 protected $mAsync, $mUrl;
33 protected $mIgnoreWarnings = true;
35 protected $mTempPath, $mTmpHandle;
37 protected static $allowedUrls = array();
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.
48 public static function isAllowed( $user ) {
49 if ( !$user->isAllowed( 'upload_by_url' ) ) {
50 return 'upload_by_url';
53 return parent
::isAllowed( $user );
57 * Checks if the upload from URL feature is enabled
60 public static function isEnabled() {
61 global $wgAllowCopyUploads;
63 return $wgAllowCopyUploads && parent
::isEnabled();
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'.
74 public static function isAllowedHost( $url ) {
75 global $wgCopyUploadsDomains;
76 if ( !count( $wgCopyUploadsDomains ) ) {
79 $parsedUrl = wfParseUrl( $url );
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 ) ) {
90 // See if all the pieces match or not (excluding wildcards)
91 foreach ( $whitelistedDomainPieces as $index => $piece ) {
92 if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
97 // We found a match, so quit comparing against the list
102 if ( $parsedUrl['host'] === $domain ) {
113 * Checks whether the URL is not allowed.
118 public static function isAllowedUrl( $url ) {
119 if ( !isset( self
::$allowedUrls[$url] ) ) {
121 Hooks
::run( 'IsUploadAllowedFromUrl', array( $url, &$allowed ) );
122 self
::$allowedUrls[$url] = $allowed;
125 return self
::$allowedUrls[$url];
129 * Entry point for API upload
131 * @param string $name
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
138 public function initialize( $name, $url, $async = false ) {
139 global $wgAllowAsyncCopyUploads;
142 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
144 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
147 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
148 # File size and removeTempFile will be filled in later
149 $this->initializePathInfo( $name, $tempPath, 0, false );
153 * Entry point for SpecialUpload
154 * @param WebRequest $request
156 public function initializeFromRequest( &$request ) {
157 $desiredDestName = $request->getText( 'wpDestFile' );
158 if ( !$desiredDestName ) {
159 $desiredDestName = $request->getText( 'wpUploadFileURL' );
163 trim( $request->getVal( 'wpUploadFileURL' ) ),
169 * @param WebRequest $request
172 public static function isValidRequest( $request ) {
175 $url = $request->getVal( 'wpUploadFileURL' );
177 return !empty( $url )
178 && Http
::isValidURI( $url )
179 && $wgUser->isAllowed( 'upload_by_url' );
185 public function getSourceType() {
190 * Download the file (if not async)
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.
196 public function fetchFile( $httpOptions = array() ) {
197 if ( !Http
::isValidURI( $this->mUrl
) ) {
198 return Status
::newFatal( 'http-invalid-url' );
201 if ( !self
::isAllowedHost( $this->mUrl
) ) {
202 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
204 if ( !self
::isAllowedUrl( $this->mUrl
) ) {
205 return Status
::newFatal( 'upload-copy-upload-invalid-url' );
207 if ( !$this->mAsync
) {
208 return $this->reallyFetchFile( $httpOptions );
211 return Status
::newGood();
215 * Create a new temporary file in the URL subdirectory of wfTempDir().
217 * @return string Path to the file
219 protected function makeTemporaryFile() {
220 $tmpFile = TempFSFile
::factory( 'URL' );
221 $tmpFile->bind( $this );
223 return $tmpFile->getPath();
227 * Callback: save a chunk of the result of a HTTP request to the temporary file
230 * @param string $buffer
231 * @return int Number of bytes handled
233 public function saveTempFileChunk( $req, $buffer ) {
234 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
236 if ( $nbytes == strlen( $buffer ) ) {
237 $this->mFileSize +
= $nbytes;
239 // Well... that's not good!
240 fclose( $this->mTmpHandle
);
241 $this->mTmpHandle
= false;
248 * Download the file, save it to the temporary file and update the file
249 * size and set $mRemoveTempFile to true.
251 * @param array $httpOptions Array of options for MWHttpRequest
254 protected function reallyFetchFile( $httpOptions = array() ) {
255 global $wgCopyUploadProxy, $wgCopyUploadTimeout;
256 if ( $this->mTempPath
=== false ) {
257 return Status
::newFatal( 'tmp-create-error' );
260 // Note the temporary file should already be created by makeTemporaryFile()
261 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
262 if ( !$this->mTmpHandle
) {
263 return Status
::newFatal( 'tmp-create-error' );
266 $this->mRemoveTempFile
= true;
267 $this->mFileSize
= 0;
269 $options = $httpOptions +
array( 'followRedirects' => true );
271 if ( $wgCopyUploadProxy !== false ) {
272 $options['proxy'] = $wgCopyUploadProxy;
275 if ( $wgCopyUploadTimeout && !isset( $options['timeout'] ) ) {
276 $options['timeout'] = $wgCopyUploadTimeout;
278 $req = MWHttpRequest
::factory( $this->mUrl
, $options );
279 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
280 $status = $req->execute();
282 if ( $this->mTmpHandle
) {
283 // File got written ok...
284 fclose( $this->mTmpHandle
);
285 $this->mTmpHandle
= null;
287 // We encountered a write error during the download...
288 return Status
::newFatal( 'tmp-write-error' );
291 if ( !$status->isOk() ) {
299 * Wrapper around the parent function in order to defer verifying the
300 * upload until the file really has been fetched.
301 * @return array|mixed
303 public function verifyUpload() {
304 if ( $this->mAsync
) {
305 return array( 'status' => UploadBase
::OK
);
308 return parent
::verifyUpload();
312 * Wrapper around the parent function in order to defer checking warnings
313 * until the file really has been fetched.
316 public function checkWarnings() {
317 if ( $this->mAsync
) {
318 $this->mIgnoreWarnings
= false;
323 return parent
::checkWarnings();
327 * Wrapper around the parent function in order to defer checking protection
328 * until we are sure that the file can actually be uploaded
332 public function verifyTitlePermissions( $user ) {
333 if ( $this->mAsync
) {
337 return parent
::verifyTitlePermissions( $user );
341 * Wrapper around the parent function in order to defer uploading to the
342 * job queue for asynchronous uploads
343 * @param string $comment
344 * @param string $pageText
349 public function performUpload( $comment, $pageText, $watch, $user ) {
350 if ( $this->mAsync
) {
351 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
353 return Status
::newFatal( 'async', $sessionKey );
356 return parent
::performUpload( $comment, $pageText, $watch, $user );
360 * @param string $comment
361 * @param string $pageText
366 protected function insertJob( $comment, $pageText, $watch, $user ) {
367 $sessionKey = $this->stashSession();
368 $job = new UploadFromUrlJob( $this->getTitle(), array(
369 'url' => $this->mUrl
,
370 'comment' => $comment,
371 'pageText' => $pageText,
373 'userName' => $user->getName(),
374 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
375 'ignoreWarnings' => $this->mIgnoreWarnings
,
376 'sessionId' => session_id(),
377 'sessionKey' => $sessionKey,
379 $job->initializeSessionData();
380 JobQueueGroup
::singleton()->push( $job );