3 * Backend for uploading files from a HTTP resource.
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.
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.
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
25 * Implements uploading from a HTTP resource.
28 * @author Bryan Tong Minh
29 * @author Michael Dale
31 class UploadFromUrl
extends UploadBase
{
32 protected $mAsync, $mUrl;
33 protected $mIgnoreWarnings = true;
35 protected $mTempPath, $mTmpHandle;
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.
45 public static function isAllowed( $user ) {
46 if ( !$user->isAllowed( 'upload_by_url' ) ) {
47 return 'upload_by_url';
49 return parent
::isAllowed( $user );
53 * Checks if the upload from URL feature is enabled
56 public static function isEnabled() {
57 global $wgAllowCopyUploads;
58 return $wgAllowCopyUploads && parent
::isEnabled();
62 * Checks whether the URL is for an allowed host
67 public static function isAllowedHost( $url ) {
68 global $wgCopyUploadsDomains;
69 if ( !count( $wgCopyUploadsDomains ) ) {
72 $parsedUrl = wfParseUrl( $url );
77 foreach( $wgCopyUploadsDomains as $domain ) {
78 if ( $parsedUrl['host'] === $domain ) {
87 * Entry point for API upload
91 * @param $async mixed Whether the download should be performed
92 * asynchronous. False for synchronous, async or async-leavemessage for
93 * asynchronous download.
95 public function initialize( $name, $url, $async = false ) {
96 global $wgAllowAsyncCopyUploads;
99 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
101 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
104 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
105 # File size and removeTempFile will be filled in later
106 $this->initializePathInfo( $name, $tempPath, 0, false );
110 * Entry point for SpecialUpload
111 * @param $request WebRequest object
113 public function initializeFromRequest( &$request ) {
114 $desiredDestName = $request->getText( 'wpDestFile' );
115 if ( !$desiredDestName ) {
116 $desiredDestName = $request->getText( 'wpUploadFileURL' );
120 trim( $request->getVal( 'wpUploadFileURL' ) ),
126 * @param $request WebRequest object
129 public static function isValidRequest( $request ) {
132 $url = $request->getVal( 'wpUploadFileURL' );
133 return !empty( $url )
134 && Http
::isValidURI( $url )
135 && $wgUser->isAllowed( 'upload_by_url' );
141 public function getSourceType() { return 'url'; }
146 public function fetchFile() {
147 if ( !Http
::isValidURI( $this->mUrl
) ) {
148 return Status
::newFatal( 'http-invalid-url' );
151 if( !self
::isAllowedHost( $this->mUrl
) ) {
152 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
154 if ( !$this->mAsync
) {
155 return $this->reallyFetchFile();
157 return Status
::newGood();
160 * Create a new temporary file in the URL subdirectory of wfTempDir().
162 * @return string Path to the file
164 protected function makeTemporaryFile() {
165 return tempnam( wfTempDir(), 'URL' );
169 * Callback: save a chunk of the result of a HTTP request to the temporary file
172 * @param $buffer string
173 * @return int number of bytes handled
175 public function saveTempFileChunk( $req, $buffer ) {
176 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
178 if ( $nbytes == strlen( $buffer ) ) {
179 $this->mFileSize +
= $nbytes;
181 // Well... that's not good!
182 fclose( $this->mTmpHandle
);
183 $this->mTmpHandle
= false;
190 * Download the file, save it to the temporary file and update the file
191 * size and set $mRemoveTempFile to true.
194 protected function reallyFetchFile() {
195 if ( $this->mTempPath
=== false ) {
196 return Status
::newFatal( 'tmp-create-error' );
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' );
205 $this->mRemoveTempFile
= true;
206 $this->mFileSize
= 0;
208 $req = MWHttpRequest
::factory( $this->mUrl
, array(
209 'followRedirects' => true
211 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
212 $status = $req->execute();
214 if ( $this->mTmpHandle
) {
215 // File got written ok...
216 fclose( $this->mTmpHandle
);
217 $this->mTmpHandle
= null;
219 // We encountered a write error during the download...
220 return Status
::newFatal( 'tmp-write-error' );
223 if ( !$status->isOk() ) {
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
235 public function verifyUpload() {
236 if ( $this->mAsync
) {
237 return array( 'status' => UploadBase
::OK
);
239 return parent
::verifyUpload();
243 * Wrapper around the parent function in order to defer checking warnings
244 * until the file really has been fetched.
247 public function checkWarnings() {
248 if ( $this->mAsync
) {
249 $this->mIgnoreWarnings
= false;
252 return parent
::checkWarnings();
256 * Wrapper around the parent function in order to defer checking protection
257 * until we are sure that the file can actually be uploaded
260 public function verifyTitlePermissions( $user ) {
261 if ( $this->mAsync
) {
264 return parent
::verifyTitlePermissions( $user );
268 * Wrapper around the parent function in order to defer uploading to the
269 * job queue for asynchronous uploads
272 public function performUpload( $comment, $pageText, $watch, $user ) {
273 if ( $this->mAsync
) {
274 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
276 return Status
::newFatal( 'async', $sessionKey );
279 return parent
::performUpload( $comment, $pageText, $watch, $user );
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,
296 'userName' => $user->getName(),
297 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
298 'ignoreWarnings' => $this->mIgnoreWarnings
,
299 'sessionId' => session_id(),
300 'sessionKey' => $sessionKey,
302 $job->initializeSessionData();