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