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