r110979: removed implode() call and format the message as plaintext, as any HTML...
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 /**
3 * Implements uploading from a HTTP resource.
4 *
5 * @ingroup Upload
6 * @author Bryan Tong Minh
7 * @author Michael Dale
8 */
9 class UploadFromUrl extends UploadBase {
10 protected $mAsync, $mUrl;
11 protected $mIgnoreWarnings = true;
12
13 protected $mTempPath, $mTmpHandle;
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 * @param $user User
20 *
21 * @return bool
22 */
23 public static function isAllowed( $user ) {
24 if ( !$user->isAllowed( 'upload_by_url' ) ) {
25 return 'upload_by_url';
26 }
27 return parent::isAllowed( $user );
28 }
29
30 /**
31 * Checks if the upload from URL feature is enabled
32 * @return bool
33 */
34 public static function isEnabled() {
35 global $wgAllowCopyUploads;
36 return $wgAllowCopyUploads && parent::isEnabled();
37 }
38
39 /**
40 * Entry point for API upload
41 *
42 * @param $name string
43 * @param $url string
44 * @param $async mixed Whether the download should be performed
45 * asynchronous. False for synchronous, async or async-leavemessage for
46 * asynchronous download.
47 */
48 public function initialize( $name, $url, $async = false ) {
49 global $wgAllowAsyncCopyUploads;
50
51 $this->mUrl = $url;
52 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
53 if ( $async ) {
54 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
55 }
56
57 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
58 # File size and removeTempFile will be filled in later
59 $this->initializePathInfo( $name, $tempPath, 0, false );
60 }
61
62 /**
63 * Entry point for SpecialUpload
64 * @param $request WebRequest object
65 */
66 public function initializeFromRequest( &$request ) {
67 $desiredDestName = $request->getText( 'wpDestFile' );
68 if ( !$desiredDestName ) {
69 $desiredDestName = $request->getText( 'wpUploadFileURL' );
70 }
71 return $this->initialize(
72 $desiredDestName,
73 trim( $request->getVal( 'wpUploadFileURL' ) ),
74 false
75 );
76 }
77
78 /**
79 * @param $request WebRequest object
80 * @return bool
81 */
82 public static function isValidRequest( $request ) {
83 global $wgUser;
84
85 $url = $request->getVal( 'wpUploadFileURL' );
86 return !empty( $url )
87 && Http::isValidURI( $url )
88 && $wgUser->isAllowed( 'upload_by_url' );
89 }
90
91 /**
92 * @return string
93 */
94 public function getSourceType() { return 'url'; }
95
96 /**
97 * @return Status
98 */
99 public function fetchFile() {
100 if ( !Http::isValidURI( $this->mUrl ) ) {
101 return Status::newFatal( 'http-invalid-url' );
102 }
103
104 if ( !$this->mAsync ) {
105 return $this->reallyFetchFile();
106 }
107 return Status::newGood();
108 }
109 /**
110 * Create a new temporary file in the URL subdirectory of wfTempDir().
111 *
112 * @return string Path to the file
113 */
114 protected function makeTemporaryFile() {
115 return tempnam( wfTempDir(), 'URL' );
116 }
117
118 /**
119 * Callback: save a chunk of the result of a HTTP request to the temporary file
120 *
121 * @param $req mixed
122 * @param $buffer string
123 * @return int number of bytes handled
124 */
125 public function saveTempFileChunk( $req, $buffer ) {
126 $nbytes = fwrite( $this->mTmpHandle, $buffer );
127
128 if ( $nbytes == strlen( $buffer ) ) {
129 $this->mFileSize += $nbytes;
130 } else {
131 // Well... that's not good!
132 fclose( $this->mTmpHandle );
133 $this->mTmpHandle = false;
134 }
135
136 return $nbytes;
137 }
138
139 /**
140 * Download the file, save it to the temporary file and update the file
141 * size and set $mRemoveTempFile to true.
142 * @return Status
143 */
144 protected function reallyFetchFile() {
145 if ( $this->mTempPath === false ) {
146 return Status::newFatal( 'tmp-create-error' );
147 }
148
149 // Note the temporary file should already be created by makeTemporaryFile()
150 $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
151 if ( !$this->mTmpHandle ) {
152 return Status::newFatal( 'tmp-create-error' );
153 }
154
155 $this->mRemoveTempFile = true;
156 $this->mFileSize = 0;
157
158 $req = MWHttpRequest::factory( $this->mUrl, array(
159 'followRedirects' => true
160 ) );
161 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
162 $status = $req->execute();
163
164 if ( $this->mTmpHandle ) {
165 // File got written ok...
166 fclose( $this->mTmpHandle );
167 $this->mTmpHandle = null;
168 } else {
169 // We encountered a write error during the download...
170 return Status::newFatal( 'tmp-write-error' );
171 }
172
173 if ( !$status->isOk() ) {
174 return $status;
175 }
176
177 return $status;
178 }
179
180 /**
181 * Wrapper around the parent function in order to defer verifying the
182 * upload until the file really has been fetched.
183 */
184 public function verifyUpload() {
185 if ( $this->mAsync ) {
186 return array( 'status' => UploadBase::OK );
187 }
188 return parent::verifyUpload();
189 }
190
191 /**
192 * Wrapper around the parent function in order to defer checking warnings
193 * until the file really has been fetched.
194 */
195 public function checkWarnings() {
196 if ( $this->mAsync ) {
197 $this->mIgnoreWarnings = false;
198 return array();
199 }
200 return parent::checkWarnings();
201 }
202
203 /**
204 * Wrapper around the parent function in order to defer checking protection
205 * until we are sure that the file can actually be uploaded
206 */
207 public function verifyTitlePermissions( $user ) {
208 if ( $this->mAsync ) {
209 return true;
210 }
211 return parent::verifyTitlePermissions( $user );
212 }
213
214 /**
215 * Wrapper around the parent function in order to defer uploading to the
216 * job queue for asynchronous uploads
217 */
218 public function performUpload( $comment, $pageText, $watch, $user ) {
219 if ( $this->mAsync ) {
220 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
221
222 return Status::newFatal( 'async', $sessionKey );
223 }
224
225 return parent::performUpload( $comment, $pageText, $watch, $user );
226 }
227
228 /**
229 * @param $comment
230 * @param $pageText
231 * @param $watch
232 * @param $user User
233 * @return
234 */
235 protected function insertJob( $comment, $pageText, $watch, $user ) {
236 $sessionKey = $this->stashSession();
237 $job = new UploadFromUrlJob( $this->getTitle(), array(
238 'url' => $this->mUrl,
239 'comment' => $comment,
240 'pageText' => $pageText,
241 'watch' => $watch,
242 'userName' => $user->getName(),
243 'leaveMessage' => $this->mAsync == 'async-leavemessage',
244 'ignoreWarnings' => $this->mIgnoreWarnings,
245 'sessionId' => session_id(),
246 'sessionKey' => $sessionKey,
247 ) );
248 $job->initializeSessionData();
249 $job->insert();
250 return $sessionKey;
251 }
252
253 }