6 * Implements uploading from a HTTP resource.
8 * @author Bryan Tong Minh
11 class UploadFromUrl
extends UploadBase
{
12 protected $mTempDownloadPath;
14 // by default do a SYNC_DOWNLOAD
15 protected $dl_mode = Http
::SYNC_DOWNLOAD
;
18 * Checks if the user is allowed to use the upload-by-URL feature. If the
19 * user is allowed, pass on permissions checking to the parent.
21 public static function isAllowed( $user ) {
22 if( !$user->isAllowed( 'upload_by_url' ) )
23 return 'upload_by_url';
24 return parent
::isAllowed( $user );
28 * Checks if the upload from URL feature is enabled
30 public static function isEnabled() {
31 global $wgAllowCopyUploads;
32 return $wgAllowCopyUploads && parent
::isEnabled();
36 * Entry point for API upload:: ASYNC_DOWNLOAD (if possible)
38 public function initialize( $name, $url, $asyncdownload, $na = false ) {
39 global $wgTmpDirectory, $wgPhpCli;
41 // check for $asyncdownload request:
42 if( $asyncdownload !== false){
43 if( $wgPhpCli && wfShellExecEnabled() ){
44 $this->dl_mode
= Http
::ASYNC_DOWNLOAD
;
46 $this->dl_mode
= Http
::SYNC_DOWNLOAD
;
50 $localFile = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
51 parent
::initialize( $name, $localFile, 0, true );
53 $this->mUrl
= trim( $url );
56 public function isAsync(){
57 return $this->dl_mode
== Http
::ASYNC_DOWNLOAD
;
61 * Entry point for SpecialUpload no ASYNC_DOWNLOAD possible
62 * @param $request Object: WebRequest object
64 public function initializeFromRequest( &$request ) {
65 $desiredDestName = $request->getText( 'wpDestFile' );
66 if( !$desiredDestName )
67 $desiredDestName = $request->getText( 'wpUploadFile' );
68 return $this->initialize(
70 $request->getVal( 'wpUploadFileURL' ),
76 * Do the real fetching stuff
78 public function fetchFile() {
79 // Entry point for SpecialUpload
80 if( Http
::isValidURI( $this->mUrl
) === false ) {
81 return Status
::newFatal( 'upload-proto-error' );
84 // Now do the actual download to the target file:
85 $status = Http
::doDownload( $this->mUrl
, $this->mTempPath
, $this->dl_mode
);
87 // Update the local filesize var:
88 $this->mFileSize
= filesize( $this->mTempPath
);
94 * @param $request Object: WebRequest object
96 public static function isValidRequest( $request ){
97 if( !$request->getVal( 'wpUploadFileURL' ) )
99 // check that is a valid url:
100 return Http
::isValidURI( $request->getVal( 'wpUploadFileURL' ) );