here it is ... the upload-api, script-server, js2 (javascript phase2) branch merge...
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 class UploadFromUrl extends UploadBase {
3 protected $mTempDownloadPath;
4
5 //by default do a SYNC_DOWNLOAD
6 protected $dl_mode = null;
7
8 static function isAllowed( $user ) {
9 if( !$user->isAllowed( 'upload_by_url' ) )
10 return 'upload_by_url';
11 return parent::isAllowed( $user );
12 }
13 static function isEnabled() {
14 global $wgAllowCopyUploads;
15 return $wgAllowCopyUploads && parent::isEnabled();
16 }
17 /*entry point for Api upload:: ASYNC_DOWNLOAD (if possible) */
18 function initialize( $name, $url, $asyncdownload = false) {
19 global $wgTmpDirectory, $wgPhpCliPath;
20
21 //check for $asyncdownload request:
22 if($asyncdownload !== false){
23 if($wgPhpCliPath && wfShellExecEnabled() ){
24 $this->dl_mode = Http::ASYNC_DOWNLOAD;
25 }else{
26 $this->dl_mode = Http::SYNC_DOWNLOAD;
27 }
28 }
29
30 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
31 parent::initialize( $name, $local_file, 0, true );
32
33 $this->mUrl = trim( $url );
34 }
35 public function isAsync(){
36 return $this->dl_mode == Http::ASYNC_DOWNLOAD;
37 }
38 /*entry point for SpecialUpload no ASYNC_DOWNLOAD possible: */
39 function initializeFromRequest( &$request ) {
40
41 //set dl mode if not set:
42 if(!$this->dl_mode)
43 $this->dl_mode = Http::SYNC_DOWNLOAD;
44
45 $desiredDestName = $request->getText( 'wpDestFile' );
46 if( !$desiredDestName )
47 $desiredDestName = $request->getText( 'wpUploadFile' );
48 return $this->initialize(
49 $desiredDestName,
50 $request->getVal('wpUploadFileURL')
51 );
52 }
53 /**
54 * Do the real fetching stuff
55 */
56 function fetchFile( ) {
57 //entry point for SpecialUplaod
58 if( self::isValidURI($this->mUrl) === false) {
59 return Status::newFatal('upload-proto-error');
60 }
61 //now do the actual download to the target file:
62 $status = Http::doDownload ( $this->mUrl, $this->mTempPath, $this->dl_mode );
63
64 //update the local filesize var:
65 $this->mFileSize = filesize( $this->mTempPath );
66
67 return $status;
68 }
69
70 static function isValidRequest( $request ){
71 if( !$request->getVal('wpUploadFileURL') )
72 return false;
73 //check that is a valid url:
74 return self::isValidURI( $request->getVal('wpUploadFileURL') );
75 }
76 static function isValidURI( $uri ){
77 return preg_match('/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
78 $uri, $matches);
79 }
80 }