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