Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Backend for uploading files from a HTTP resource.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Upload
25 */
26
27 /**
28 * Implements uploading from a HTTP resource.
29 *
30 * @ingroup Upload
31 * @author Bryan Tong Minh
32 * @author Michael Dale
33 */
34 class UploadFromUrl extends UploadBase {
35 protected $mUrl;
36
37 protected $mTempPath, $mTmpHandle;
38
39 protected static $allowedUrls = [];
40
41 /**
42 * Checks if the user is allowed to use the upload-by-URL feature. If the
43 * user is not allowed, return the name of the user right as a string. If
44 * the user is allowed, have the parent do further permissions checking.
45 *
46 * @param User $user
47 *
48 * @return bool|string
49 */
50 public static function isAllowed( $user ) {
51 if ( !$user->isAllowed( 'upload_by_url' ) ) {
52 return 'upload_by_url';
53 }
54
55 return parent::isAllowed( $user );
56 }
57
58 /**
59 * Checks if the upload from URL feature is enabled
60 * @return bool
61 */
62 public static function isEnabled() {
63 global $wgAllowCopyUploads;
64
65 return $wgAllowCopyUploads && parent::isEnabled();
66 }
67
68 /**
69 * Checks whether the URL is for an allowed host
70 * The domains in the whitelist can include wildcard characters (*) in place
71 * of any of the domain levels, e.g. '*.flickr.com' or 'upload.*.gov.uk'.
72 *
73 * @param string $url
74 * @return bool
75 */
76 public static function isAllowedHost( $url ) {
77 global $wgCopyUploadsDomains;
78 if ( !count( $wgCopyUploadsDomains ) ) {
79 return true;
80 }
81 $parsedUrl = wfParseUrl( $url );
82 if ( !$parsedUrl ) {
83 return false;
84 }
85 $valid = false;
86 foreach ( $wgCopyUploadsDomains as $domain ) {
87 // See if the domain for the upload matches this whitelisted domain
88 $whitelistedDomainPieces = explode( '.', $domain );
89 $uploadDomainPieces = explode( '.', $parsedUrl['host'] );
90 if ( count( $whitelistedDomainPieces ) === count( $uploadDomainPieces ) ) {
91 $valid = true;
92 // See if all the pieces match or not (excluding wildcards)
93 foreach ( $whitelistedDomainPieces as $index => $piece ) {
94 if ( $piece !== '*' && $piece !== $uploadDomainPieces[$index] ) {
95 $valid = false;
96 }
97 }
98 if ( $valid ) {
99 // We found a match, so quit comparing against the list
100 break;
101 }
102 }
103 /* Non-wildcard test
104 if ( $parsedUrl['host'] === $domain ) {
105 $valid = true;
106 break;
107 }
108 */
109 }
110
111 return $valid;
112 }
113
114 /**
115 * Checks whether the URL is not allowed.
116 *
117 * @param string $url
118 * @return bool
119 */
120 public static function isAllowedUrl( $url ) {
121 if ( !isset( self::$allowedUrls[$url] ) ) {
122 $allowed = true;
123 Hooks::run( 'IsUploadAllowedFromUrl', [ $url, &$allowed ] );
124 self::$allowedUrls[$url] = $allowed;
125 }
126
127 return self::$allowedUrls[$url];
128 }
129
130 /**
131 * Entry point for API upload
132 *
133 * @param string $name
134 * @param string $url
135 * @throws MWException
136 */
137 public function initialize( $name, $url ) {
138 $this->mUrl = $url;
139
140 $tempPath = $this->makeTemporaryFile();
141 # File size and removeTempFile will be filled in later
142 $this->initializePathInfo( $name, $tempPath, 0, false );
143 }
144
145 /**
146 * Entry point for SpecialUpload
147 * @param WebRequest &$request
148 */
149 public function initializeFromRequest( &$request ) {
150 $desiredDestName = $request->getText( 'wpDestFile' );
151 if ( !$desiredDestName ) {
152 $desiredDestName = $request->getText( 'wpUploadFileURL' );
153 }
154 $this->initialize(
155 $desiredDestName,
156 trim( $request->getVal( 'wpUploadFileURL' ) )
157 );
158 }
159
160 /**
161 * @param WebRequest $request
162 * @return bool
163 */
164 public static function isValidRequest( $request ) {
165 global $wgUser;
166
167 $url = $request->getVal( 'wpUploadFileURL' );
168
169 return !empty( $url )
170 && $wgUser->isAllowed( 'upload_by_url' );
171 }
172
173 /**
174 * @return string
175 */
176 public function getSourceType() {
177 return 'url';
178 }
179
180 /**
181 * Download the file
182 *
183 * @param array $httpOptions Array of options for MWHttpRequest.
184 * This could be used to override the timeout on the http request.
185 * @return Status
186 */
187 public function fetchFile( $httpOptions = [] ) {
188 if ( !Http::isValidURI( $this->mUrl ) ) {
189 return Status::newFatal( 'http-invalid-url', $this->mUrl );
190 }
191
192 if ( !self::isAllowedHost( $this->mUrl ) ) {
193 return Status::newFatal( 'upload-copy-upload-invalid-domain' );
194 }
195 if ( !self::isAllowedUrl( $this->mUrl ) ) {
196 return Status::newFatal( 'upload-copy-upload-invalid-url' );
197 }
198 return $this->reallyFetchFile( $httpOptions );
199 }
200
201 /**
202 * Create a new temporary file in the URL subdirectory of wfTempDir().
203 *
204 * @return string Path to the file
205 */
206 protected function makeTemporaryFile() {
207 $tmpFile = MediaWikiServices::getInstance()->getTempFSFileFactory()
208 ->newTempFSFile( 'URL', 'urlupload_' );
209 $tmpFile->bind( $this );
210
211 return $tmpFile->getPath();
212 }
213
214 /**
215 * Callback: save a chunk of the result of a HTTP request to the temporary file
216 *
217 * @param mixed $req
218 * @param string $buffer
219 * @return int Number of bytes handled
220 */
221 public function saveTempFileChunk( $req, $buffer ) {
222 wfDebugLog( 'fileupload', 'Received chunk of ' . strlen( $buffer ) . ' bytes' );
223 $nbytes = fwrite( $this->mTmpHandle, $buffer );
224
225 if ( $nbytes == strlen( $buffer ) ) {
226 $this->mFileSize += $nbytes;
227 } else {
228 // Well... that's not good!
229 wfDebugLog(
230 'fileupload',
231 'Short write ' . $nbytes . '/' . strlen( $buffer ) .
232 ' bytes, aborting with ' . $this->mFileSize . ' uploaded so far'
233 );
234 fclose( $this->mTmpHandle );
235 $this->mTmpHandle = false;
236 }
237
238 return $nbytes;
239 }
240
241 /**
242 * Download the file, save it to the temporary file and update the file
243 * size and set $mRemoveTempFile to true.
244 *
245 * @param array $httpOptions Array of options for MWHttpRequest
246 * @return Status
247 */
248 protected function reallyFetchFile( $httpOptions = [] ) {
249 global $wgCopyUploadProxy, $wgCopyUploadTimeout;
250 if ( $this->mTempPath === false ) {
251 return Status::newFatal( 'tmp-create-error' );
252 }
253
254 // Note the temporary file should already be created by makeTemporaryFile()
255 $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
256 if ( !$this->mTmpHandle ) {
257 return Status::newFatal( 'tmp-create-error' );
258 }
259 wfDebugLog( 'fileupload', 'Temporary file created "' . $this->mTempPath . '"' );
260
261 $this->mRemoveTempFile = true;
262 $this->mFileSize = 0;
263
264 $options = $httpOptions + [ 'followRedirects' => true ];
265
266 if ( $wgCopyUploadProxy !== false ) {
267 $options['proxy'] = $wgCopyUploadProxy;
268 }
269
270 if ( $wgCopyUploadTimeout && !isset( $options['timeout'] ) ) {
271 $options['timeout'] = $wgCopyUploadTimeout;
272 }
273 wfDebugLog(
274 'fileupload',
275 'Starting download from "' . $this->mUrl . '" ' .
276 '<' . implode( ',', array_keys( array_filter( $options ) ) ) . '>'
277 );
278 $req = MWHttpRequest::factory( $this->mUrl, $options, __METHOD__ );
279 $req->setCallback( [ $this, 'saveTempFileChunk' ] );
280 $status = $req->execute();
281
282 if ( $this->mTmpHandle ) {
283 // File got written ok...
284 fclose( $this->mTmpHandle );
285 $this->mTmpHandle = null;
286 } else {
287 // We encountered a write error during the download...
288 return Status::newFatal( 'tmp-write-error' );
289 }
290
291 wfDebugLog( 'fileupload', $status );
292 if ( $status->isOK() ) {
293 wfDebugLog( 'fileupload', 'Download by URL completed successfully.' );
294 } else {
295 wfDebugLog(
296 'fileupload',
297 'Download by URL completed with HTTP status ' . $req->getStatus()
298 );
299 }
300
301 return $status;
302 }
303 }