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