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