re: r65152
[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 protected $comment, $watchList;
14
15 /**
16 * Checks if the user is allowed to use the upload-by-URL feature. If the
17 * user is allowed, pass on permissions checking to the parent.
18 */
19 public static function isAllowed( $user ) {
20 if( !$user->isAllowed( 'upload_by_url' ) )
21 return 'upload_by_url';
22 return parent::isAllowed( $user );
23 }
24
25 /**
26 * Checks if the upload from URL feature is enabled
27 * @return bool
28 */
29 public static function isEnabled() {
30 global $wgAllowCopyUploads;
31 return $wgAllowCopyUploads && parent::isEnabled();
32 }
33
34 /**
35 * Entry point for API upload
36 * @return bool true on success
37 */
38 public function initialize( $name, $url, $comment, $watchlist ) {
39 global $wgUser;
40
41 if( !Http::isValidURI( $url ) ) {
42 return Status::newFatal( 'http-invalid-url' );
43 }
44 $params = array(
45 "userName" => $wgUser->getName(),
46 "userID" => $wgUser->getID(),
47 "url" => trim( $url ),
48 "timestamp" => wfTimestampNow(),
49 "comment" => $comment,
50 "watchlist" => $watchlist);
51
52 $title = Title::newFromText( $name );
53 /* // Check whether the user has the appropriate permissions to upload anyway */
54 /* $permission = $this->isAllowed( $wgUser ); */
55
56 /* if ( $permission !== true ) { */
57 /* if ( !$wgUser->isLoggedIn() ) { */
58 /* return Status::newFatal( 'uploadnologintext' ); */
59 /* } else { */
60 /* return Status::newFatal( 'badaccess-groups' ); */
61 /* } */
62 /* } */
63
64 /* $permErrors = $this->verifyPermissions( $wgUser ); */
65 /* if ( $permErrors !== true ) { */
66 /* return Status::newFatal( 'badaccess-groups' ); */
67 /* } */
68
69
70 $job = new UploadFromUrlJob( $title, $params );
71 return $job->insert();
72 }
73
74 /**
75 * Initialize a queued download
76 * @param $job Job
77 */
78 public function initializeFromJob( $job ) {
79 $this->mUrl = $job->params['url'];
80 $this->mTempPath = tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
81 $this->mDesiredDestName = $job->title;
82 $this->comment = $job->params['comment'];
83 $this->watchList = $job->params['watchlist'];
84 $this->getTitle();
85 }
86
87 /**
88 * Entry point for SpecialUpload
89 * @param $request Object: WebRequest object
90 */
91 public function initializeFromRequest( &$request ) {
92 $desiredDestName = $request->getText( 'wpDestFile' );
93 if( !$desiredDestName )
94 $desiredDestName = $request->getText( 'wpUploadFileURL' );
95 return $this->initialize(
96 $desiredDestName,
97 $request->getVal( 'wpUploadFileURL' ),
98 false
99 );
100 }
101
102 /**
103 * @param $request Object: WebRequest object
104 */
105 public static function isValidRequest( $request ){
106 if( !$request->getVal( 'wpUploadFileURL' ) )
107 return false;
108 // check that is a valid url:
109 return Http::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
110 }
111
112 private function saveTempFile( $req ) {
113 $filename = tempnam( wfTempDir(), 'URL' );
114 if ( $filename === false ) {
115 return Status::newFatal( 'tmp-create-error' );
116 }
117 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
118 return Status::newFatal( 'tmp-write-error' );
119 }
120
121 $this->mTempPath = $filename;
122 $this->mFileSize = filesize( $filename );
123
124 return Status::newGood();
125 }
126
127 public function doUpload() {
128 global $wgUser;
129
130 $req = HttpRequest::factory($this->mUrl);
131 $status = $req->execute();
132
133 if( !$status->isOk() ) {
134 return $status;
135 }
136
137 $status = $this->saveTempFile( $req );
138 $this->mRemoveTempFile = true;
139
140 if( !$status->isOk() ) {
141 return $status;
142 }
143
144 $v = $this->verifyUpload();
145 if( $v['status'] !== UploadBase::OK ) {
146 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
147 }
148
149 // This has to come from API
150 /* $warnings = $this->checkForWarnings(); */
151 /* if( isset($warnings) ) return $warnings; */
152
153 // Use comment as initial page text by default
154 if ( is_null( $this->mParams['text'] ) ) {
155 $this->mParams['text'] = $this->mParams['comment'];
156 }
157
158 $file = $this->getLocalFile();
159 // This comes from ApiBase
160 /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
161
162 if ( !$status->isGood() ) {
163 return $status;
164 }
165
166 $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
167 $this->pageText, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
168
169 return $status;
170 }
171 }