3 class UploadFromUrl
extends UploadBase
{
4 protected $mTempDownloadPath;
6 // by default do a SYNC_DOWNLOAD
7 protected $dl_mode = Http
::SYNC_DOWNLOAD
;
10 * Checks if the user is allowed to use the upload-by-URL feature
12 static function isAllowed( $user ) {
13 if( !$user->isAllowed( 'upload_by_url' ) )
14 return 'upload_by_url';
15 return parent
::isAllowed( $user );
19 * Checks if the upload from URL feature is enabled
21 static function isEnabled() {
22 global $wgAllowCopyUploads;
23 return $wgAllowCopyUploads && parent
::isEnabled();
26 /* entry point for API upload:: ASYNC_DOWNLOAD (if possible) */
27 function initialize( $name, $url, $asyncdownload, $na = false ) {
28 global $wgTmpDirectory, $wgPhpCli;
30 // check for $asyncdownload request:
31 if( $asyncdownload !== false){
32 if( $wgPhpCli && wfShellExecEnabled() ){
33 $this->dl_mode
= Http
::ASYNC_DOWNLOAD
;
35 $this->dl_mode
= Http
::SYNC_DOWNLOAD
;
39 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
40 parent
::initialize( $name, $local_file, 0, true );
42 $this->mUrl
= trim( $url );
45 public function isAsync(){
46 return $this->dl_mode
== Http
::ASYNC_DOWNLOAD
;
50 * Entry point for SpecialUpload no ASYNC_DOWNLOAD possible
51 * @param $request Object: WebRequest object
53 function initializeFromRequest( &$request ) {
55 // set dl mode if not set:
57 $this->dl_mode
= Http
::SYNC_DOWNLOAD
;
59 $desiredDestName = $request->getText( 'wpDestFile' );
60 if( !$desiredDestName )
61 $desiredDestName = $request->getText( 'wpUploadFile' );
62 return $this->initialize(
64 $request->getVal( 'wpUploadFileURL' ),
70 * Do the real fetching stuff
72 function fetchFile() {
73 // entry point for SpecialUplaod
74 if( self
::isValidURI( $this->mUrl
) === false ) {
75 return Status
::newFatal( 'upload-proto-error' );
78 // now do the actual download to the target file:
79 $status = Http
::doDownload( $this->mUrl
, $this->mTempPath
, $this->dl_mode
);
81 // update the local filesize var:
82 $this->mFileSize
= filesize( $this->mTempPath
);
88 * @param $request Object: WebRequest object
90 static function isValidRequest( $request ){
91 if( !$request->getVal( 'wpUploadFileURL' ) )
93 // check that is a valid url:
94 return self
::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
98 * Checks that the given URI is a valid one
99 * @param $uri Mixed: URI to check for validity
101 static function isValidURI( $uri ){
103 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',