* ( bug 19930 ) default to SYNC_DOWNLOAD
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2
3 class UploadFromUrl extends UploadBase {
4 protected $mTempDownloadPath;
5
6 // by default do a SYNC_DOWNLOAD
7 protected $dl_mode = Http::SYNC_DOWNLOAD;
8
9 /**
10 * Checks if the user is allowed to use the upload-by-URL feature
11 */
12 static function isAllowed( $user ) {
13 if( !$user->isAllowed( 'upload_by_url' ) )
14 return 'upload_by_url';
15 return parent::isAllowed( $user );
16 }
17
18 /**
19 * Checks if the upload from URL feature is enabled
20 */
21 static function isEnabled() {
22 global $wgAllowCopyUploads;
23 return $wgAllowCopyUploads && parent::isEnabled();
24 }
25
26 /* entry point for API upload:: ASYNC_DOWNLOAD (if possible) */
27 function initialize( $name, $url, $asyncdownload, $na = false ) {
28 global $wgTmpDirectory, $wgPhpCli;
29
30 // check for $asyncdownload request:
31 if( $asyncdownload !== false){
32 if( $wgPhpCli && wfShellExecEnabled() ){
33 $this->dl_mode = Http::ASYNC_DOWNLOAD;
34 } else {
35 $this->dl_mode = Http::SYNC_DOWNLOAD;
36 }
37 }
38
39 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
40 parent::initialize( $name, $local_file, 0, true );
41
42 $this->mUrl = trim( $url );
43 }
44
45 public function isAsync(){
46 return $this->dl_mode == Http::ASYNC_DOWNLOAD;
47 }
48
49 /**
50 * Entry point for SpecialUpload no ASYNC_DOWNLOAD possible
51 * @param $request Object: WebRequest object
52 */
53 function initializeFromRequest( &$request ) {
54
55 // set dl mode if not set:
56 if( !$this->dl_mode )
57 $this->dl_mode = Http::SYNC_DOWNLOAD;
58
59 $desiredDestName = $request->getText( 'wpDestFile' );
60 if( !$desiredDestName )
61 $desiredDestName = $request->getText( 'wpUploadFile' );
62 return $this->initialize(
63 $desiredDestName,
64 $request->getVal( 'wpUploadFileURL' ),
65 false
66 );
67 }
68
69 /**
70 * Do the real fetching stuff
71 */
72 function fetchFile() {
73 // entry point for SpecialUplaod
74 if( self::isValidURI( $this->mUrl ) === false ) {
75 return Status::newFatal( 'upload-proto-error' );
76 }
77
78 // now do the actual download to the target file:
79 $status = Http::doDownload( $this->mUrl, $this->mTempPath, $this->dl_mode );
80
81 // update the local filesize var:
82 $this->mFileSize = filesize( $this->mTempPath );
83
84 return $status;
85 }
86
87 /**
88 * @param $request Object: WebRequest object
89 */
90 static function isValidRequest( $request ){
91 if( !$request->getVal( 'wpUploadFileURL' ) )
92 return false;
93 // check that is a valid url:
94 return self::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
95 }
96
97 /**
98 * Checks that the given URI is a valid one
99 * @param $uri Mixed: URI to check for validity
100 */
101 static function isValidURI( $uri ){
102 return preg_match(
103 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
104 $uri,
105 $matches
106 );
107 }
108
109 }