3 * Implements uploading from a HTTP resource.
6 * @author Bryan Tong Minh
9 class UploadFromUrl
extends UploadBase
{
10 protected $mAsync, $mUrl;
11 protected $mIgnoreWarnings = true;
13 protected $mTempPath, $mTmpHandle;
16 * Checks if the user is allowed to use the upload-by-URL feature. If the
17 * user is allowed, pass on permissions checking to the parent.
23 public static function isAllowed( $user ) {
24 if ( !$user->isAllowed( 'upload_by_url' ) ) {
25 return 'upload_by_url';
27 return parent
::isAllowed( $user );
31 * Checks if the upload from URL feature is enabled
34 public static function isEnabled() {
35 global $wgAllowCopyUploads;
36 return $wgAllowCopyUploads && parent
::isEnabled();
40 * Checks whether the URL is for an allowed host
45 public static function isAllowedHost( $url ) {
46 global $wgCopyUploadsDomains;
47 if ( !count( $wgCopyUploadsDomains ) ) {
50 $parsedUrl = wfParseUrl( $url );
55 foreach( $wgCopyUploadsDomains as $domain ) {
56 if ( $parsedUrl['host'] === $domain ) {
65 * Entry point for API upload
69 * @param $async mixed Whether the download should be performed
70 * asynchronous. False for synchronous, async or async-leavemessage for
71 * asynchronous download.
73 public function initialize( $name, $url, $async = false ) {
74 global $wgAllowAsyncCopyUploads;
77 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
79 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
82 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
83 # File size and removeTempFile will be filled in later
84 $this->initializePathInfo( $name, $tempPath, 0, false );
88 * Entry point for SpecialUpload
89 * @param $request WebRequest object
91 public function initializeFromRequest( &$request ) {
92 $desiredDestName = $request->getText( 'wpDestFile' );
93 if ( !$desiredDestName ) {
94 $desiredDestName = $request->getText( 'wpUploadFileURL' );
96 return $this->initialize(
98 trim( $request->getVal( 'wpUploadFileURL' ) ),
104 * @param $request WebRequest object
107 public static function isValidRequest( $request ) {
110 $url = $request->getVal( 'wpUploadFileURL' );
111 return !empty( $url )
112 && Http
::isValidURI( $url )
113 && $wgUser->isAllowed( 'upload_by_url' );
119 public function getSourceType() { return 'url'; }
124 public function fetchFile() {
125 if ( !Http
::isValidURI( $this->mUrl
) ) {
126 return Status
::newFatal( 'http-invalid-url' );
129 if( !self
::isAllowedHost( $this->mUrl
) ) {
130 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
132 if ( !$this->mAsync
) {
133 return $this->reallyFetchFile();
135 return Status
::newGood();
138 * Create a new temporary file in the URL subdirectory of wfTempDir().
140 * @return string Path to the file
142 protected function makeTemporaryFile() {
143 return tempnam( wfTempDir(), 'URL' );
147 * Callback: save a chunk of the result of a HTTP request to the temporary file
150 * @param $buffer string
151 * @return int number of bytes handled
153 public function saveTempFileChunk( $req, $buffer ) {
154 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
156 if ( $nbytes == strlen( $buffer ) ) {
157 $this->mFileSize +
= $nbytes;
159 // Well... that's not good!
160 fclose( $this->mTmpHandle
);
161 $this->mTmpHandle
= false;
168 * Download the file, save it to the temporary file and update the file
169 * size and set $mRemoveTempFile to true.
172 protected function reallyFetchFile() {
173 if ( $this->mTempPath
=== false ) {
174 return Status
::newFatal( 'tmp-create-error' );
177 // Note the temporary file should already be created by makeTemporaryFile()
178 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
179 if ( !$this->mTmpHandle
) {
180 return Status
::newFatal( 'tmp-create-error' );
183 $this->mRemoveTempFile
= true;
184 $this->mFileSize
= 0;
186 $req = MWHttpRequest
::factory( $this->mUrl
, array(
187 'followRedirects' => true
189 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
190 $status = $req->execute();
192 if ( $this->mTmpHandle
) {
193 // File got written ok...
194 fclose( $this->mTmpHandle
);
195 $this->mTmpHandle
= null;
197 // We encountered a write error during the download...
198 return Status
::newFatal( 'tmp-write-error' );
201 if ( !$status->isOk() ) {
209 * Wrapper around the parent function in order to defer verifying the
210 * upload until the file really has been fetched.
211 * @return array|mixed
213 public function verifyUpload() {
214 if ( $this->mAsync
) {
215 return array( 'status' => UploadBase
::OK
);
217 return parent
::verifyUpload();
221 * Wrapper around the parent function in order to defer checking warnings
222 * until the file really has been fetched.
225 public function checkWarnings() {
226 if ( $this->mAsync
) {
227 $this->mIgnoreWarnings
= false;
230 return parent
::checkWarnings();
234 * Wrapper around the parent function in order to defer checking protection
235 * until we are sure that the file can actually be uploaded
238 public function verifyTitlePermissions( $user ) {
239 if ( $this->mAsync
) {
242 return parent
::verifyTitlePermissions( $user );
246 * Wrapper around the parent function in order to defer uploading to the
247 * job queue for asynchronous uploads
250 public function performUpload( $comment, $pageText, $watch, $user ) {
251 if ( $this->mAsync
) {
252 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
254 return Status
::newFatal( 'async', $sessionKey );
257 return parent
::performUpload( $comment, $pageText, $watch, $user );
267 protected function insertJob( $comment, $pageText, $watch, $user ) {
268 $sessionKey = $this->stashSession();
269 $job = new UploadFromUrlJob( $this->getTitle(), array(
270 'url' => $this->mUrl
,
271 'comment' => $comment,
272 'pageText' => $pageText,
274 'userName' => $user->getName(),
275 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
276 'ignoreWarnings' => $this->mIgnoreWarnings
,
277 'sessionId' => session_id(),
278 'sessionKey' => $sessionKey,
280 $job->initializeSessionData();