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 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.
46 public static function isAllowed( $user ) {
47 if ( !$user->isAllowed( 'upload_by_url' ) ) {
48 return 'upload_by_url';
50 return parent
::isAllowed( $user );
54 * Checks if the upload from URL feature is enabled
57 public static function isEnabled() {
58 global $wgAllowCopyUploads;
59 return $wgAllowCopyUploads && parent
::isEnabled();
63 * Checks whether the URL is for an allowed host
68 public static function isAllowedHost( $url ) {
69 global $wgCopyUploadsDomains;
70 if ( !count( $wgCopyUploadsDomains ) ) {
73 $uri = new Uri( $url );
74 $parsedDomain = $uri->getHost();
75 if ( $parsedDomain === null ) {
79 foreach( $wgCopyUploadsDomains as $domain ) {
80 if ( $parsedDomain === $domain ) {
89 * Entry point for API upload
93 * @param $async mixed Whether the download should be performed
94 * asynchronous. False for synchronous, async or async-leavemessage for
95 * asynchronous download.
98 public function initialize( $name, $url, $async = false ) {
99 global $wgAllowAsyncCopyUploads;
102 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
104 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
107 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
108 # File size and removeTempFile will be filled in later
109 $this->initializePathInfo( $name, $tempPath, 0, false );
113 * Entry point for SpecialUpload
114 * @param $request WebRequest object
116 public function initializeFromRequest( &$request ) {
117 $desiredDestName = $request->getText( 'wpDestFile' );
118 if ( !$desiredDestName ) {
119 $desiredDestName = $request->getText( 'wpUploadFileURL' );
123 trim( $request->getVal( 'wpUploadFileURL' ) ),
129 * @param $request WebRequest object
132 public static function isValidRequest( $request ) {
135 $url = $request->getVal( 'wpUploadFileURL' );
136 return !empty( $url )
137 && Http
::isValidURI( $url )
138 && $wgUser->isAllowed( 'upload_by_url' );
144 public function getSourceType() { return 'url'; }
149 public function fetchFile() {
150 if ( !Http
::isValidURI( $this->mUrl
) ) {
151 return Status
::newFatal( 'http-invalid-url' );
154 if( !self
::isAllowedHost( $this->mUrl
) ) {
155 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
157 if ( !$this->mAsync
) {
158 return $this->reallyFetchFile();
160 return Status
::newGood();
163 * Create a new temporary file in the URL subdirectory of wfTempDir().
165 * @return string Path to the file
167 protected function makeTemporaryFile() {
168 return tempnam( wfTempDir(), 'URL' );
172 * Callback: save a chunk of the result of a HTTP request to the temporary file
175 * @param $buffer string
176 * @return int number of bytes handled
178 public function saveTempFileChunk( $req, $buffer ) {
179 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
181 if ( $nbytes == strlen( $buffer ) ) {
182 $this->mFileSize +
= $nbytes;
184 // Well... that's not good!
185 fclose( $this->mTmpHandle
);
186 $this->mTmpHandle
= false;
193 * Download the file, save it to the temporary file and update the file
194 * size and set $mRemoveTempFile to true.
197 protected function reallyFetchFile() {
198 if ( $this->mTempPath
=== false ) {
199 return Status
::newFatal( 'tmp-create-error' );
202 // Note the temporary file should already be created by makeTemporaryFile()
203 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
204 if ( !$this->mTmpHandle
) {
205 return Status
::newFatal( 'tmp-create-error' );
208 $this->mRemoveTempFile
= true;
209 $this->mFileSize
= 0;
211 $req = MWHttpRequest
::factory( $this->mUrl
, array(
212 'followRedirects' => true
214 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
215 $status = $req->execute();
217 if ( $this->mTmpHandle
) {
218 // File got written ok...
219 fclose( $this->mTmpHandle
);
220 $this->mTmpHandle
= null;
222 // We encountered a write error during the download...
223 return Status
::newFatal( 'tmp-write-error' );
226 if ( !$status->isOk() ) {
234 * Wrapper around the parent function in order to defer verifying the
235 * upload until the file really has been fetched.
236 * @return array|mixed
238 public function verifyUpload() {
239 if ( $this->mAsync
) {
240 return array( 'status' => UploadBase
::OK
);
242 return parent
::verifyUpload();
246 * Wrapper around the parent function in order to defer checking warnings
247 * until the file really has been fetched.
250 public function checkWarnings() {
251 if ( $this->mAsync
) {
252 $this->mIgnoreWarnings
= false;
255 return parent
::checkWarnings();
259 * Wrapper around the parent function in order to defer checking protection
260 * until we are sure that the file can actually be uploaded
264 public function verifyTitlePermissions( $user ) {
265 if ( $this->mAsync
) {
268 return parent
::verifyTitlePermissions( $user );
272 * Wrapper around the parent function in order to defer uploading to the
273 * job queue for asynchronous uploads
274 * @param $comment string
275 * @param $pageText string
280 public function performUpload( $comment, $pageText, $watch, $user ) {
281 if ( $this->mAsync
) {
282 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
284 return Status
::newFatal( 'async', $sessionKey );
287 return parent
::performUpload( $comment, $pageText, $watch, $user );
297 protected function insertJob( $comment, $pageText, $watch, $user ) {
298 $sessionKey = $this->stashSession();
299 $job = new UploadFromUrlJob( $this->getTitle(), array(
300 'url' => $this->mUrl
,
301 'comment' => $comment,
302 'pageText' => $pageText,
304 'userName' => $user->getName(),
305 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
306 'ignoreWarnings' => $this->mIgnoreWarnings
,
307 'sessionId' => session_id(),
308 'sessionKey' => $sessionKey,
310 $job->initializeSessionData();