Follow-up r81612, disable $wgAllowAsyncCopyUploads
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 /**
3 * Implements uploading from a HTTP resource.
4 *
5 * @file
6 * @ingroup upload
7 * @author Bryan Tong Minh
8 * @author Michael Dale
9 */
10
11 class UploadFromUrl extends UploadBase {
12 protected $mAsync, $mUrl;
13 protected $mIgnoreWarnings = true;
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 *
37 * @param $name string
38 * @param $url string
39 * @param $async mixed Whether the download should be performed
40 * asynchronous. False for synchronous, async or async-leavemessage for
41 * asynchronous download.
42 */
43 public function initialize( $name, $url, $async = false ) {
44 global $wgAllowAsyncCopyUploads;
45
46 $this->mUrl = $url;
47 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
48 if ( $async ) {
49 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
50 }
51
52 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
53 # File size and removeTempFile will be filled in later
54 $this->initializePathInfo( $name, $tempPath, 0, false );
55 }
56
57 /**
58 * Entry point for SpecialUpload
59 * @param $request Object: WebRequest object
60 */
61 public function initializeFromRequest( &$request ) {
62 $desiredDestName = $request->getText( 'wpDestFile' );
63 if ( !$desiredDestName )
64 $desiredDestName = $request->getText( 'wpUploadFileURL' );
65 return $this->initialize(
66 $desiredDestName,
67 $request->getVal( 'wpUploadFileURL' ),
68 false
69 );
70 }
71
72 /**
73 * @param $request Object: WebRequest object
74 */
75 public static function isValidRequest( $request ) {
76 global $wgUser;
77
78 $url = $request->getVal( 'wpUploadFileURL' );
79 return !empty( $url )
80 && Http::isValidURI( $url )
81 && $wgUser->isAllowed( 'upload_by_url' );
82 }
83
84 public function getSourceType() { return 'url'; }
85
86 public function fetchFile() {
87 if ( !Http::isValidURI( $this->mUrl ) ) {
88 return Status::newFatal( 'http-invalid-url' );
89 }
90
91 if ( !$this->mAsync ) {
92 return $this->reallyFetchFile();
93 }
94 return Status::newGood();
95 }
96 /**
97 * Create a new temporary file in the URL subdirectory of wfTempDir().
98 *
99 * @return string Path to the file
100 */
101 protected function makeTemporaryFile() {
102 return tempnam( wfTempDir(), 'URL' );
103 }
104 /**
105 * Save the result of a HTTP request to the temporary file
106 *
107 * @param $req MWHttpRequest
108 * @return Status
109 */
110 private function saveTempFile( $req ) {
111 if ( $this->mTempPath === false ) {
112 return Status::newFatal( 'tmp-create-error' );
113 }
114 if ( file_put_contents( $this->mTempPath, $req->getContent() ) === false ) {
115 return Status::newFatal( 'tmp-write-error' );
116 }
117
118 $this->mFileSize = filesize( $this->mTempPath );
119
120 return Status::newGood();
121 }
122 /**
123 * Download the file, save it to the temporary file and update the file
124 * size and set $mRemoveTempFile to true.
125 */
126 protected function reallyFetchFile() {
127 $req = MWHttpRequest::factory( $this->mUrl );
128 $status = $req->execute();
129
130 if ( !$status->isOk() ) {
131 return $status;
132 }
133
134 $status = $this->saveTempFile( $req );
135 if ( !$status->isGood() ) {
136 return $status;
137 }
138 $this->mRemoveTempFile = true;
139
140 return $status;
141 }
142
143 /**
144 * Wrapper around the parent function in order to defer verifying the
145 * upload until the file really has been fetched.
146 */
147 public function verifyUpload() {
148 if ( $this->mAsync ) {
149 return array( 'status' => UploadBase::OK );
150 }
151 return parent::verifyUpload();
152 }
153
154 /**
155 * Wrapper around the parent function in order to defer checking warnings
156 * until the file really has been fetched.
157 */
158 public function checkWarnings() {
159 if ( $this->mAsync ) {
160 $this->mIgnoreWarnings = false;
161 return array();
162 }
163 return parent::checkWarnings();
164 }
165
166 /**
167 * Wrapper around the parent function in order to defer checking protection
168 * until we are sure that the file can actually be uploaded
169 */
170 public function verifyPermissions( $user ) {
171 if ( $this->mAsync ) {
172 return true;
173 }
174 return parent::verifyPermissions( $user );
175 }
176
177 /**
178 * Wrapper around the parent function in order to defer uploading to the
179 * job queue for asynchronous uploads
180 */
181 public function performUpload( $comment, $pageText, $watch, $user ) {
182 if ( $this->mAsync ) {
183 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
184
185 $status = new Status;
186 $status->error( 'async', $sessionKey );
187 return $status;
188 }
189
190 return parent::performUpload( $comment, $pageText, $watch, $user );
191 }
192
193 /**
194 * @param $comment
195 * @param $pageText
196 * @param $watch
197 * @param $user User
198 * @return
199 */
200 protected function insertJob( $comment, $pageText, $watch, $user ) {
201 $sessionKey = $this->stashSession();
202 $job = new UploadFromUrlJob( $this->getTitle(), array(
203 'url' => $this->mUrl,
204 'comment' => $comment,
205 'pageText' => $pageText,
206 'watch' => $watch,
207 'userName' => $user->getName(),
208 'leaveMessage' => $this->mAsync == 'async-leavemessage',
209 'ignoreWarnings' => $this->mIgnoreWarnings,
210 'sessionId' => session_id(),
211 'sessionKey' => $sessionKey,
212 ) );
213 $job->initializeSessionData();
214 $job->insert();
215 return $sessionKey;
216 }
217
218
219 }