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 $parsedUrl = wfParseUrl( $url );
53 foreach( $wgCopyUploadsDomains as $domain ) {
54 if ( $parsedUrl['host'] === $domain ) {
63 * Entry point for API upload
67 * @param $async mixed Whether the download should be performed
68 * asynchronous. False for synchronous, async or async-leavemessage for
69 * asynchronous download.
71 public function initialize( $name, $url, $async = false ) {
72 global $wgAllowAsyncCopyUploads;
75 $this->mAsync
= $wgAllowAsyncCopyUploads ?
$async : false;
77 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
80 $tempPath = $this->mAsync ?
null : $this->makeTemporaryFile();
81 # File size and removeTempFile will be filled in later
82 $this->initializePathInfo( $name, $tempPath, 0, false );
86 * Entry point for SpecialUpload
87 * @param $request WebRequest object
89 public function initializeFromRequest( &$request ) {
90 $desiredDestName = $request->getText( 'wpDestFile' );
91 if ( !$desiredDestName ) {
92 $desiredDestName = $request->getText( 'wpUploadFileURL' );
94 return $this->initialize(
96 trim( $request->getVal( 'wpUploadFileURL' ) ),
102 * @param $request WebRequest object
105 public static function isValidRequest( $request ) {
108 $url = $request->getVal( 'wpUploadFileURL' );
109 return !empty( $url )
110 && Http
::isValidURI( $url )
111 && $wgUser->isAllowed( 'upload_by_url' );
117 public function getSourceType() { return 'url'; }
122 public function fetchFile() {
123 if ( !Http
::isValidURI( $this->mUrl
) ) {
124 return Status
::newFatal( 'http-invalid-url' );
127 if( !self
::isAllowedHost( $this->mUrl
) ) {
128 return Status
::newFatal( 'upload-copy-upload-invalid-domain' );
130 if ( !$this->mAsync
) {
131 return $this->reallyFetchFile();
133 return Status
::newGood();
136 * Create a new temporary file in the URL subdirectory of wfTempDir().
138 * @return string Path to the file
140 protected function makeTemporaryFile() {
141 return tempnam( wfTempDir(), 'URL' );
145 * Callback: save a chunk of the result of a HTTP request to the temporary file
148 * @param $buffer string
149 * @return int number of bytes handled
151 public function saveTempFileChunk( $req, $buffer ) {
152 $nbytes = fwrite( $this->mTmpHandle
, $buffer );
154 if ( $nbytes == strlen( $buffer ) ) {
155 $this->mFileSize +
= $nbytes;
157 // Well... that's not good!
158 fclose( $this->mTmpHandle
);
159 $this->mTmpHandle
= false;
166 * Download the file, save it to the temporary file and update the file
167 * size and set $mRemoveTempFile to true.
170 protected function reallyFetchFile() {
171 if ( $this->mTempPath
=== false ) {
172 return Status
::newFatal( 'tmp-create-error' );
175 // Note the temporary file should already be created by makeTemporaryFile()
176 $this->mTmpHandle
= fopen( $this->mTempPath
, 'wb' );
177 if ( !$this->mTmpHandle
) {
178 return Status
::newFatal( 'tmp-create-error' );
181 $this->mRemoveTempFile
= true;
182 $this->mFileSize
= 0;
184 $req = MWHttpRequest
::factory( $this->mUrl
, array(
185 'followRedirects' => true
187 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
188 $status = $req->execute();
190 if ( $this->mTmpHandle
) {
191 // File got written ok...
192 fclose( $this->mTmpHandle
);
193 $this->mTmpHandle
= null;
195 // We encountered a write error during the download...
196 return Status
::newFatal( 'tmp-write-error' );
199 if ( !$status->isOk() ) {
207 * Wrapper around the parent function in order to defer verifying the
208 * upload until the file really has been fetched.
210 public function verifyUpload() {
211 if ( $this->mAsync
) {
212 return array( 'status' => UploadBase
::OK
);
214 return parent
::verifyUpload();
218 * Wrapper around the parent function in order to defer checking warnings
219 * until the file really has been fetched.
221 public function checkWarnings() {
222 if ( $this->mAsync
) {
223 $this->mIgnoreWarnings
= false;
226 return parent
::checkWarnings();
230 * Wrapper around the parent function in order to defer checking protection
231 * until we are sure that the file can actually be uploaded
233 public function verifyTitlePermissions( $user ) {
234 if ( $this->mAsync
) {
237 return parent
::verifyTitlePermissions( $user );
241 * Wrapper around the parent function in order to defer uploading to the
242 * job queue for asynchronous uploads
244 public function performUpload( $comment, $pageText, $watch, $user ) {
245 if ( $this->mAsync
) {
246 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
248 return Status
::newFatal( 'async', $sessionKey );
251 return parent
::performUpload( $comment, $pageText, $watch, $user );
261 protected function insertJob( $comment, $pageText, $watch, $user ) {
262 $sessionKey = $this->stashSession();
263 $job = new UploadFromUrlJob( $this->getTitle(), array(
264 'url' => $this->mUrl
,
265 'comment' => $comment,
266 'pageText' => $pageText,
268 'userName' => $user->getName(),
269 'leaveMessage' => $this->mAsync
== 'async-leavemessage',
270 'ignoreWarnings' => $this->mIgnoreWarnings
,
271 'sessionId' => session_id(),
272 'sessionKey' => $sessionKey,
274 $job->initializeSessionData();