Merge "Fix IE9's lack of support for console.warn.apply"
[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 return parent::isAllowed( $user );
53 }
54
55 /**
56 * Checks if the upload from URL feature is enabled
57 * @return bool
58 */
59 public static function isEnabled() {
60 global $wgAllowCopyUploads;
61 return $wgAllowCopyUploads && parent::isEnabled();
62 }
63
64 /**
65 * Checks whether the URL is for an allowed host
66 * The domains in the whitelist can include wildcard characters (*) in place
67 * of any of the domain levels, e.g. '*.flickr.com' or 'upload.*.gov.uk'.
68 *
69 * @param string $url
70 * @return bool
71 */
72 public static function isAllowedHost( $url ) {
73 global $wgCopyUploadsDomains;
74 if ( !count( $wgCopyUploadsDomains ) ) {
75 return true;
76 }
77 $parsedUrl = wfParseUrl( $url );
78 if ( !$parsedUrl ) {
79 return false;
80 }
81 $valid = false;
82 foreach ( $wgCopyUploadsDomains as $domain ) {
83 // See if the domain for the upload matches this whitelisted domain
84 $whitelistedDomainPieces = explode( '.', $domain );
85 $uploadDomainPieces = explode( '.', $parsedUrl['host'] );
86 if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
87 $valid = true;
88 // See if all the pieces match or not (excluding wildcards)
89 foreach ( $whitelistedDomainPieces as $index => $piece ) {
90 if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
91 $valid = false;
92 }
93 }
94 if ( $valid ) {
95 // We found a match, so quit comparing against the list
96 break;
97 }
98 }
99 /* Non-wildcard test
100 if ( $parsedUrl['host'] === $domain ) {
101 $valid = true;
102 break;
103 }
104 */
105 }
106 return $valid;
107 }
108
109 /**
110 * Checks whether the URL is not allowed.
111 *
112 * @param string $url
113 * @return bool
114 */
115 public static function isAllowedUrl( $url ) {
116 if ( !isset( self::$allowedUrls[$url] ) ) {
117 $allowed = true;
118 wfRunHooks( 'IsUploadAllowedFromUrl', array( $url, &$allowed ) );
119 self::$allowedUrls[$url] = $allowed;
120 }
121 return self::$allowedUrls[$url];
122 }
123
124 /**
125 * Entry point for API upload
126 *
127 * @param string $name
128 * @param string $url
129 * @param bool|string $async Whether the download should be performed
130 * asynchronous. False for synchronous, async or async-leavemessage for
131 * asynchronous download.
132 * @throws MWException
133 */
134 public function initialize( $name, $url, $async = false ) {
135 global $wgAllowAsyncCopyUploads;
136
137 $this->mUrl = $url;
138 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
139 if ( $async ) {
140 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
141 }
142
143 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
144 # File size and removeTempFile will be filled in later
145 $this->initializePathInfo( $name, $tempPath, 0, false );
146 }
147
148 /**
149 * Entry point for SpecialUpload
150 * @param WebRequest $request
151 */
152 public function initializeFromRequest( &$request ) {
153 $desiredDestName = $request->getText( 'wpDestFile' );
154 if ( !$desiredDestName ) {
155 $desiredDestName = $request->getText( 'wpUploadFileURL' );
156 }
157 $this->initialize(
158 $desiredDestName,
159 trim( $request->getVal( 'wpUploadFileURL' ) ),
160 false
161 );
162 }
163
164 /**
165 * @param WebRequest $request
166 * @return bool
167 */
168 public static function isValidRequest( $request ) {
169 global $wgUser;
170
171 $url = $request->getVal( 'wpUploadFileURL' );
172 return !empty( $url )
173 && Http::isValidURI( $url )
174 && $wgUser->isAllowed( 'upload_by_url' );
175 }
176
177 /**
178 * @return string
179 */
180 public function getSourceType() {
181 return 'url';
182 }
183
184 /**
185 * Download the file (if not async)
186 *
187 * @param array $httpOptions Array of options for MWHttpRequest. Ignored if async.
188 * This could be used to override the timeout on the http request.
189 * @return Status
190 */
191 public function fetchFile( $httpOptions = array() ) {
192 if ( !Http::isValidURI( $this->mUrl ) ) {
193 return Status::newFatal( 'http-invalid-url' );
194 }
195
196 if ( !self::isAllowedHost( $this->mUrl ) ) {
197 return Status::newFatal( 'upload-copy-upload-invalid-domain' );
198 }
199 if ( !self::isAllowedUrl( $this->mUrl ) ) {
200 return Status::newFatal( 'upload-copy-upload-invalid-url' );
201 }
202 if ( !$this->mAsync ) {
203 return $this->reallyFetchFile( $httpOptions );
204 }
205 return Status::newGood();
206 }
207
208 /**
209 * Create a new temporary file in the URL subdirectory of wfTempDir().
210 *
211 * @return string Path to the file
212 */
213 protected function makeTemporaryFile() {
214 $tmpFile = TempFSFile::factory( 'URL' );
215 $tmpFile->bind( $this );
216 return $tmpFile->getPath();
217 }
218
219 /**
220 * Callback: save a chunk of the result of a HTTP request to the temporary file
221 *
222 * @param mixed $req
223 * @param string $buffer
224 * @return int Number of bytes handled
225 */
226 public function saveTempFileChunk( $req, $buffer ) {
227 $nbytes = fwrite( $this->mTmpHandle, $buffer );
228
229 if ( $nbytes == strlen( $buffer ) ) {
230 $this->mFileSize += $nbytes;
231 } else {
232 // Well... that's not good!
233 fclose( $this->mTmpHandle );
234 $this->mTmpHandle = false;
235 }
236
237 return $nbytes;
238 }
239
240 /**
241 * Download the file, save it to the temporary file and update the file
242 * size and set $mRemoveTempFile to true.
243 *
244 * @param array $httpOptions Array of options for MWHttpRequest
245 * @return Status
246 */
247 protected function reallyFetchFile( $httpOptions = array() ) {
248 global $wgCopyUploadProxy, $wgCopyUploadTimeout;
249 if ( $this->mTempPath === false ) {
250 return Status::newFatal( 'tmp-create-error' );
251 }
252
253 // Note the temporary file should already be created by makeTemporaryFile()
254 $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
255 if ( !$this->mTmpHandle ) {
256 return Status::newFatal( 'tmp-create-error' );
257 }
258
259 $this->mRemoveTempFile = true;
260 $this->mFileSize = 0;
261
262 $options = $httpOptions + array(
263 'followRedirects' => true,
264 );
265 if ( $wgCopyUploadProxy !== false ) {
266 $options['proxy'] = $wgCopyUploadProxy;
267 }
268 if ( $wgCopyUploadTimeout && !isset( $options['timeout'] ) ) {
269 $options['timeout'] = $wgCopyUploadTimeout;
270 }
271 $req = MWHttpRequest::factory( $this->mUrl, $options );
272 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
273 $status = $req->execute();
274
275 if ( $this->mTmpHandle ) {
276 // File got written ok...
277 fclose( $this->mTmpHandle );
278 $this->mTmpHandle = null;
279 } else {
280 // We encountered a write error during the download...
281 return Status::newFatal( 'tmp-write-error' );
282 }
283
284 if ( !$status->isOk() ) {
285 return $status;
286 }
287
288 return $status;
289 }
290
291 /**
292 * Wrapper around the parent function in order to defer verifying the
293 * upload until the file really has been fetched.
294 * @return array|mixed
295 */
296 public function verifyUpload() {
297 if ( $this->mAsync ) {
298 return array( 'status' => UploadBase::OK );
299 }
300 return parent::verifyUpload();
301 }
302
303 /**
304 * Wrapper around the parent function in order to defer checking warnings
305 * until the file really has been fetched.
306 * @return array
307 */
308 public function checkWarnings() {
309 if ( $this->mAsync ) {
310 $this->mIgnoreWarnings = false;
311 return array();
312 }
313 return parent::checkWarnings();
314 }
315
316 /**
317 * Wrapper around the parent function in order to defer checking protection
318 * until we are sure that the file can actually be uploaded
319 * @param User $user
320 * @return bool|mixed
321 */
322 public function verifyTitlePermissions( $user ) {
323 if ( $this->mAsync ) {
324 return true;
325 }
326 return parent::verifyTitlePermissions( $user );
327 }
328
329 /**
330 * Wrapper around the parent function in order to defer uploading to the
331 * job queue for asynchronous uploads
332 * @param string $comment
333 * @param string $pageText
334 * @param bool $watch
335 * @param User $user
336 * @return Status
337 */
338 public function performUpload( $comment, $pageText, $watch, $user ) {
339 if ( $this->mAsync ) {
340 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
341
342 return Status::newFatal( 'async', $sessionKey );
343 }
344
345 return parent::performUpload( $comment, $pageText, $watch, $user );
346 }
347
348 /**
349 * @param string $comment
350 * @param string $pageText
351 * @param bool $watch
352 * @param User $user
353 * @return string
354 */
355 protected function insertJob( $comment, $pageText, $watch, $user ) {
356 $sessionKey = $this->stashSession();
357 $job = new UploadFromUrlJob( $this->getTitle(), array(
358 'url' => $this->mUrl,
359 'comment' => $comment,
360 'pageText' => $pageText,
361 'watch' => $watch,
362 'userName' => $user->getName(),
363 'leaveMessage' => $this->mAsync == 'async-leavemessage',
364 'ignoreWarnings' => $this->mIgnoreWarnings,
365 'sessionId' => session_id(),
366 'sessionKey' => $sessionKey,
367 ) );
368 $job->initializeSessionData();
369 JobQueueGroup::singleton()->push( $job );
370 return $sessionKey;
371 }
372
373 }