* Updated upload hooks documentation
[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 $desiredDestName = $request->getText( 'wpDestFile' );
66 if( !$desiredDestName )
67 $desiredDestName = $request->getText( 'wpUploadFileURL' );
68 return $this->initialize(
69 $desiredDestName,
70 $request->getVal( 'wpUploadFileURL' ),
71 false
72 );
73 }
74
75 /**
76 * Do the real fetching stuff
77 */
78 public function fetchFile() {
79 // Entry point for SpecialUpload
80 if( Http::isValidURI( $this->mUrl ) === false ) {
81 return Status::newFatal( 'upload-proto-error' );
82 }
83
84 // Now do the actual download to the target file:
85 $status = Http::doDownload( $this->mUrl, $this->mTempPath, $this->dl_mode );
86
87 // Update the local filesize var:
88 $this->mFileSize = filesize( $this->mTempPath );
89
90 return $status;
91 }
92
93 /**
94 * @param $request Object: WebRequest object
95 */
96 public static function isValidRequest( $request ){
97 if( !$request->getVal( 'wpUploadFileURL' ) )
98 return false;
99 // check that is a valid url:
100 return Http::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
101 }
102
103
104
105 }