Merge "Rename $usableSkins to $allowedSkins"
[mediawiki.git] / includes / upload / UploadFromUrl.php
blobc99feefd51c4c0250d3a7b4d880d6547ab58c6a9
1 <?php
2 /**
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
20 * @file
21 * @ingroup Upload
24 /**
25 * Implements uploading from a HTTP resource.
27 * @ingroup Upload
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 /**
38 * Checks if the user is allowed to use the upload-by-URL feature. If the
39 * user is not allowed, return the name of the user right as a string. If
40 * the user is allowed, have the parent do further permissions checking.
42 * @param $user User
44 * @return bool|string
46 public static function isAllowed( $user ) {
47 if ( !$user->isAllowed( 'upload_by_url' ) ) {
48 return 'upload_by_url';
50 return parent::isAllowed( $user );
53 /**
54 * Checks if the upload from URL feature is enabled
55 * @return bool
57 public static function isEnabled() {
58 global $wgAllowCopyUploads;
59 return $wgAllowCopyUploads && parent::isEnabled();
62 /**
63 * Checks whether the URL is for an allowed host
64 * The domains in the whitelist can include wildcard characters (*) in place
65 * of any of the domain levels, e.g. '*.flickr.com' or 'upload.*.gov.uk'.
67 * @param $url string
68 * @return bool
70 public static function isAllowedHost( $url ) {
71 global $wgCopyUploadsDomains;
72 if ( !count( $wgCopyUploadsDomains ) ) {
73 return true;
75 $parsedUrl = wfParseUrl( $url );
76 if ( !$parsedUrl ) {
77 return false;
79 $valid = false;
80 foreach ( $wgCopyUploadsDomains as $domain ) {
81 // See if the domain for the upload matches this whitelisted domain
82 $whitelistedDomainPieces = explode( '.', $domain );
83 $uploadDomainPieces = explode( '.', $parsedUrl['host'] );
84 if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
85 $valid = true;
86 // See if all the pieces match or not (excluding wildcards)
87 foreach ( $whitelistedDomainPieces as $index => $piece ) {
88 if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
89 $valid = false;
92 if ( $valid ) {
93 // We found a match, so quit comparing against the list
94 break;
97 /* Non-wildcard test
98 if ( $parsedUrl['host'] === $domain ) {
99 $valid = true;
100 break;
104 return $valid;
108 * Entry point for API upload
110 * @param $name string
111 * @param $url string
112 * @param $async mixed Whether the download should be performed
113 * asynchronous. False for synchronous, async or async-leavemessage for
114 * asynchronous download.
115 * @throws MWException
117 public function initialize( $name, $url, $async = false ) {
118 global $wgAllowAsyncCopyUploads;
120 $this->mUrl = $url;
121 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
122 if ( $async ) {
123 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
126 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
127 # File size and removeTempFile will be filled in later
128 $this->initializePathInfo( $name, $tempPath, 0, false );
132 * Entry point for SpecialUpload
133 * @param $request WebRequest object
135 public function initializeFromRequest( &$request ) {
136 $desiredDestName = $request->getText( 'wpDestFile' );
137 if ( !$desiredDestName ) {
138 $desiredDestName = $request->getText( 'wpUploadFileURL' );
140 $this->initialize(
141 $desiredDestName,
142 trim( $request->getVal( 'wpUploadFileURL' ) ),
143 false
148 * @param $request WebRequest object
149 * @return bool
151 public static function isValidRequest( $request ) {
152 global $wgUser;
154 $url = $request->getVal( 'wpUploadFileURL' );
155 return !empty( $url )
156 && Http::isValidURI( $url )
157 && $wgUser->isAllowed( 'upload_by_url' );
161 * @return string
163 public function getSourceType() {
164 return 'url';
168 * @return Status
170 public function fetchFile() {
171 if ( !Http::isValidURI( $this->mUrl ) ) {
172 return Status::newFatal( 'http-invalid-url' );
175 if ( !self::isAllowedHost( $this->mUrl ) ) {
176 return Status::newFatal( 'upload-copy-upload-invalid-domain' );
178 if ( !$this->mAsync ) {
179 return $this->reallyFetchFile();
181 return Status::newGood();
184 * Create a new temporary file in the URL subdirectory of wfTempDir().
186 * @return string Path to the file
188 protected function makeTemporaryFile() {
189 return tempnam( wfTempDir(), 'URL' );
193 * Callback: save a chunk of the result of a HTTP request to the temporary file
195 * @param $req mixed
196 * @param $buffer string
197 * @return int number of bytes handled
199 public function saveTempFileChunk( $req, $buffer ) {
200 $nbytes = fwrite( $this->mTmpHandle, $buffer );
202 if ( $nbytes == strlen( $buffer ) ) {
203 $this->mFileSize += $nbytes;
204 } else {
205 // Well... that's not good!
206 fclose( $this->mTmpHandle );
207 $this->mTmpHandle = false;
210 return $nbytes;
214 * Download the file, save it to the temporary file and update the file
215 * size and set $mRemoveTempFile to true.
216 * @return Status
218 protected function reallyFetchFile() {
219 if ( $this->mTempPath === false ) {
220 return Status::newFatal( 'tmp-create-error' );
223 // Note the temporary file should already be created by makeTemporaryFile()
224 $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
225 if ( !$this->mTmpHandle ) {
226 return Status::newFatal( 'tmp-create-error' );
229 $this->mRemoveTempFile = true;
230 $this->mFileSize = 0;
232 $options = array(
233 'followRedirects' => true
235 global $wgCopyUploadProxy;
236 if ( $wgCopyUploadProxy !== false ) {
237 $options['proxy'] = $wgCopyUploadProxy;
239 $req = MWHttpRequest::factory( $this->mUrl, $options );
240 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
241 $status = $req->execute();
243 if ( $this->mTmpHandle ) {
244 // File got written ok...
245 fclose( $this->mTmpHandle );
246 $this->mTmpHandle = null;
247 } else {
248 // We encountered a write error during the download...
249 return Status::newFatal( 'tmp-write-error' );
252 if ( !$status->isOk() ) {
253 return $status;
256 return $status;
260 * Wrapper around the parent function in order to defer verifying the
261 * upload until the file really has been fetched.
262 * @return array|mixed
264 public function verifyUpload() {
265 if ( $this->mAsync ) {
266 return array( 'status' => UploadBase::OK );
268 return parent::verifyUpload();
272 * Wrapper around the parent function in order to defer checking warnings
273 * until the file really has been fetched.
274 * @return Array
276 public function checkWarnings() {
277 if ( $this->mAsync ) {
278 $this->mIgnoreWarnings = false;
279 return array();
281 return parent::checkWarnings();
285 * Wrapper around the parent function in order to defer checking protection
286 * until we are sure that the file can actually be uploaded
287 * @param $user User
288 * @return bool|mixed
290 public function verifyTitlePermissions( $user ) {
291 if ( $this->mAsync ) {
292 return true;
294 return parent::verifyTitlePermissions( $user );
298 * Wrapper around the parent function in order to defer uploading to the
299 * job queue for asynchronous uploads
300 * @param $comment string
301 * @param $pageText string
302 * @param $watch bool
303 * @param $user User
304 * @return Status
306 public function performUpload( $comment, $pageText, $watch, $user ) {
307 if ( $this->mAsync ) {
308 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
310 return Status::newFatal( 'async', $sessionKey );
313 return parent::performUpload( $comment, $pageText, $watch, $user );
317 * @param $comment
318 * @param $pageText
319 * @param $watch
320 * @param $user User
321 * @return String
323 protected function insertJob( $comment, $pageText, $watch, $user ) {
324 $sessionKey = $this->stashSession();
325 $job = new UploadFromUrlJob( $this->getTitle(), array(
326 'url' => $this->mUrl,
327 'comment' => $comment,
328 'pageText' => $pageText,
329 'watch' => $watch,
330 'userName' => $user->getName(),
331 'leaveMessage' => $this->mAsync == 'async-leavemessage',
332 'ignoreWarnings' => $this->mIgnoreWarnings,
333 'sessionId' => session_id(),
334 'sessionKey' => $sessionKey,
335 ) );
336 $job->initializeSessionData();
337 JobQueueGroup::singleton()->push( $job );
338 return $sessionKey;