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