2 class UploadFromUrl
extends UploadBase
{
3 protected $mTempDownloadPath;
5 //by default do a SYNC_DOWNLOAD
6 protected $dl_mode = null;
8 static function isAllowed( $user ) {
9 if( !$user->isAllowed( 'upload_by_url' ) )
10 return 'upload_by_url';
11 return parent
::isAllowed( $user );
13 static function isEnabled() {
14 global $wgAllowCopyUploads;
15 return $wgAllowCopyUploads && parent
::isEnabled();
17 /*entry point for Api upload:: ASYNC_DOWNLOAD (if possible) */
18 function initialize( $name, $url, $asyncdownload = false) {
19 global $wgTmpDirectory, $wgPhpCliPath;
21 //check for $asyncdownload request:
22 if($asyncdownload !== false){
23 if($wgPhpCliPath && wfShellExecEnabled() ){
24 $this->dl_mode
= Http
::ASYNC_DOWNLOAD
;
26 $this->dl_mode
= Http
::SYNC_DOWNLOAD
;
30 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
31 parent
::initialize( $name, $local_file, 0, true );
33 $this->mUrl
= trim( $url );
35 public function isAsync(){
36 return $this->dl_mode
== Http
::ASYNC_DOWNLOAD
;
38 /*entry point for SpecialUpload no ASYNC_DOWNLOAD possible: */
39 function initializeFromRequest( &$request ) {
41 //set dl mode if not set:
43 $this->dl_mode
= Http
::SYNC_DOWNLOAD
;
45 $desiredDestName = $request->getText( 'wpDestFile' );
46 if( !$desiredDestName )
47 $desiredDestName = $request->getText( 'wpUploadFile' );
48 return $this->initialize(
50 $request->getVal('wpUploadFileURL')
54 * Do the real fetching stuff
56 function fetchFile( ) {
57 //entry point for SpecialUplaod
58 if( self
::isValidURI($this->mUrl
) === false) {
59 return Status
::newFatal('upload-proto-error');
61 //now do the actual download to the target file:
62 $status = Http
::doDownload ( $this->mUrl
, $this->mTempPath
, $this->dl_mode
);
64 //update the local filesize var:
65 $this->mFileSize
= filesize( $this->mTempPath
);
70 static function isValidRequest( $request ){
71 if( !$request->getVal('wpUploadFileURL') )
73 //check that is a valid url:
74 return self
::isValidURI( $request->getVal('wpUploadFileURL') );
76 static function isValidURI( $uri ){
77 return preg_match('/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',