3 * Implements uploading from a HTTP resource.
7 * @author Bryan Tong Minh
10 class UploadFromUrl
extends UploadBase
{
11 protected $mAsync, $mUrl;
12 protected $mIgnoreWarnings = true;
14 protected $mTempPath, $mTmpHandle;
17 * Checks if the user is allowed to use the upload-by-URL feature. If the
18 * user is allowed, pass on permissions checking to the parent.
24 public static function isAllowed( $user ) {
25 if ( !$user->isAllowed( 'upload_by_url' ) ) {
26 return 'upload_by_url';
28 return parent
::isAllowed( $user );
32 * Checks if the upload from URL feature is enabled
35 public static function isEnabled() {
36 global $wgAllowCopyUploads;
37 return $wgAllowCopyUploads && parent
::isEnabled();
41 * Checks whether the URL is for an allowed host
46 public static function isAllowedHost( $url ) {
47 global $wgCopyUploadsDomains;
48 if ( !count( $wgCopyUploadsDomains ) ) {
52 foreach( $wgCopyUploadsDomains as $domain ) {
53 if ( strpos( $url, $domain ) !== false ) {
62 * Entry point for API upload
66 * @param $async mixed Whether the download should be performed
67 * asynchronous. False for synchronous, async or async-leavemessage for
68 * asynchronous download.
70 public function initialize( $name, $url, $async = false ) {
71 global $wgAllowAsyncCopyUploads;
74 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
76 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
79 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
80 # File size and removeTempFile will be filled in later
81 $this->initializePathInfo( $name, $tempPath, 0, false );
85 * Entry point for SpecialUpload
86 * @param $request WebRequest object
88 public function initializeFromRequest( &$request ) {
89 $desiredDestName = $request->getText( 'wpDestFile' );
90 if ( !$desiredDestName ) {
91 $desiredDestName = $request->getText( 'wpUploadFileURL' );
93 return $this->initialize(
95 trim( $request->getVal( 'wpUploadFileURL' ) ),
101 * @param $request WebRequest object
104 public static function isValidRequest( $request ) {
107 $url = $request->getVal( 'wpUploadFileURL' );
108 return !empty( $url )
109 && Http
::isValidURI( $url )
110 && $wgUser->isAllowed( 'upload_by_url' );
116 public function getSourceType() { return 'url'; }
121 public function fetchFile() {
122 if ( !Http
::isValidURI( $this->mUrl
) ) {
123 return Status
::newFatal( 'http-invalid-url' );
126 if( !self
::isAllowedHost( $this->mUrl
) ) {
127 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
129 if ( !$this->mAsync
) {
130 return $this->reallyFetchFile();
132 return Status
::newGood();
135 * Create a new temporary file in the URL subdirectory of wfTempDir().
137 * @return string Path to the file
139 protected function makeTemporaryFile() {
140 return tempnam( wfTempDir(), 'URL' );
144 * Callback: save a chunk of the result of a HTTP request to the temporary file
147 * @param $buffer string
148 * @return int number of bytes handled
150 public function saveTempFileChunk( $req, $buffer ) {
151 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
153 if ( $nbytes == strlen( $buffer ) ) {
154 $this->mFileSize +
= $nbytes;
156 // Well... that's not good!
157 fclose( $this->mTmpHandle
);
158 $this->mTmpHandle
= false;
165 * Download the file, save it to the temporary file and update the file
166 * size and set $mRemoveTempFile to true.
169 protected function reallyFetchFile() {
170 if ( $this->mTempPath
=== false ) {
171 return Status
::newFatal( 'tmp-create-error' );
174 // Note the temporary file should already be created by makeTemporaryFile()
175 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
176 if ( !$this->mTmpHandle
) {
177 return Status
::newFatal( 'tmp-create-error' );
180 $this->mRemoveTempFile
= true;
181 $this->mFileSize
= 0;
183 $req = MWHttpRequest
::factory( $this->mUrl
, array(
184 'followRedirects' => true
186 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
187 $status = $req->execute();
189 if ( $this->mTmpHandle
) {
190 // File got written ok...
191 fclose( $this->mTmpHandle
);
192 $this->mTmpHandle
= null;
194 // We encountered a write error during the download...
195 return Status
::newFatal( 'tmp-write-error' );
198 if ( !$status->isOk() ) {
206 * Wrapper around the parent function in order to defer verifying the
207 * upload until the file really has been fetched.
209 public function verifyUpload() {
210 if ( $this->mAsync
) {
211 return array( 'status' => UploadBase
::OK
);
213 return parent
::verifyUpload();
217 * Wrapper around the parent function in order to defer checking warnings
218 * until the file really has been fetched.
220 public function checkWarnings() {
221 if ( $this->mAsync
) {
222 $this->mIgnoreWarnings
= false;
225 return parent
::checkWarnings();
229 * Wrapper around the parent function in order to defer checking protection
230 * until we are sure that the file can actually be uploaded
232 public function verifyTitlePermissions( $user ) {
233 if ( $this->mAsync
) {
236 return parent
::verifyTitlePermissions( $user );
240 * Wrapper around the parent function in order to defer uploading to the
241 * job queue for asynchronous uploads
243 public function performUpload( $comment, $pageText, $watch, $user ) {
244 if ( $this->mAsync
) {
245 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
247 return Status
::newFatal( 'async', $sessionKey );
250 return parent
::performUpload( $comment, $pageText, $watch, $user );
260 protected function insertJob( $comment, $pageText, $watch, $user ) {
261 $sessionKey = $this->stashSession();
262 $job = new UploadFromUrlJob( $this->getTitle(), array(
263 'url' => $this->mUrl
,
264 'comment' => $comment,
265 'pageText' => $pageText,
267 'userName' => $user->getName(),
268 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
269 'ignoreWarnings' => $this->mIgnoreWarnings
,
270 'sessionId' => session_id(),
271 'sessionKey' => $sessionKey,
273 $job->initializeSessionData();