* bug 21063 http copy upload and upload from stash give different file path types...
[lhc/web/wiklou.git] / includes / upload / UploadFromChunks.php
1 <?php
2 /**
3 * @file
4 * @ingroup upload
5 *
6 * @author Michael Dale
7 *
8 * first destination checks are made (if ignorewarnings is not checked) errors / warning is returned.
9 *
10 * we return the uploadUrl
11 * we then accept chunk uploads from the client.
12 * return chunk id on each POSTED chunk
13 * once the client posts done=1 concatenated the files together.
14 * more info at: http://firefogg.org/dev/chunk_post.html
15 */
16 class UploadFromChunks extends UploadBase {
17
18 var $chunk_mode; // init, chunk, done
19 var $mSessionKey = false;
20 var $status = array();
21
22 const INIT = 1;
23 const CHUNK = 2;
24 const DONE = 3;
25 public function initializeFromRequest( &$request ){
26 //should merge initializeFromParams (but just needs to be working atm)
27 }
28 public function initializeFromParams( $param, &$request ) {
29 $this->initFromSessionKey( $param['chunksessionkey'], $request );
30 // set the chunk mode:
31 if( !$this->mSessionKey && !$param['done'] ){
32 // session key not set init the chunk upload system:
33 $this->chunk_mode = UploadFromChunks::INIT;
34 $this->mDesiredDestName = $param['filename'];
35 } else if( $this->mSessionKey && !$param['done'] ){
36 // this is a chunk piece
37 $this->chunk_mode = UploadFromChunks::CHUNK;
38 } else if( $this->mSessionKey && $param['done'] ){
39 // this is the last chunk
40 $this->chunk_mode = UploadFromChunks::DONE;
41 }
42 if( $this->chunk_mode == UploadFromChunks::CHUNK ||
43 $this->chunk_mode == UploadFromChunks::DONE ){
44 // set chunk related vars:
45 $this->mTempPath = $request->getFileTempName( 'chunk' );
46 $this->mFileSize = $request->getFileSize( 'chunk' );
47 }
48
49 return $this->status;
50 }
51 static function isValidRequest( $request ) {
52 $sessionData = $request->getSessionData( 'wsUploadData' );
53 if( !self::isValidSessionKey(
54 $request->getInt( 'wpSessionKey' ),
55 $sessionData ) )
56 return false;
57 // check for the file:
58 return (bool)$request->getFileTempName( 'file' );
59 }
60
61 /* check warnings depending on chunk_mode */
62 function checkWarnings(){
63 $warning = array();
64 return $warning;
65 }
66
67 function isEmptyFile(){
68 // does not apply to chunk init
69 if( $this->chunk_mode == UploadFromChunks::INIT ){
70 return false;
71 } else {
72 return parent::isEmptyFile();
73 }
74 }
75
76 /**
77 * Verify whether the upload is sane.
78 * Returns self::OK or else an array with error information
79 */
80 function verifyUpload() {
81 // no checks on chunk upload mode:
82 if( $this->chunk_mode == UploadFromChunks::INIT )
83 return array( 'status' => self::OK );
84
85 // verify on init and last chunk request
86 if( $this->chunk_mode == UploadFromChunks::CHUNK ||
87 $this->chunk_mode == UploadFromChunks::DONE )
88 return parent::verifyUpload();
89 }
90
91 // only run verifyFile on completed uploaded chunks
92 function verifyFile(){
93 if( $this->chunk_mode == UploadFromChunks::DONE ){
94 // first append last chunk (so we can do a real verifyFile check... (check file type etc)
95 $status = $this->doChunkAppend();
96 if( $status->isOK() ){
97 $this->mTempPath = $this->getRealPath( $this->mTempAppendPath );
98 // verify the completed merged chunks as if it was the file that got uploaded:
99 return parent::verifyFile( $this->mTempPath );
100 } else {
101 // conflict of status returns (have to return the error ary) ... why we don't consistantly use a status object is beyond me..
102 return $status->getErrorsArray();
103 }
104 } else {
105 return true;
106 }
107 }
108
109 // pretty ugly inter-mixing of mParam and local vars
110 function setupChunkSession( $summary, $comment, $watch ) {
111 $this->mSessionKey = $this->getSessionKey();
112 $_SESSION['wsUploadData'][$this->mSessionKey] = array(
113 'mComment' => $comment,
114 'mSummary' => $summary,
115 'mWatch' => $watch,
116 'mIgnorewarnings' => true, //ignore warning on chunk uploads (for now)
117 'mFilteredName' => $this->mFilteredName,
118 'mTempAppendPath' => null, // the repo append path (not temporary local node mTempPath)
119 'mDesiredDestName' => $this->mDesiredDestName,
120 'version' => self::SESSION_VERSION,
121 );
122 return $this->mSessionKey;
123 }
124
125 function initFromSessionKey( $sessionKey, $request ){
126 if( !$sessionKey || empty( $sessionKey ) ){
127 return false;
128 }
129 $this->mSessionKey = $sessionKey;
130 // load the sessionData array:
131 $sessionData = $request->getSessionData( 'wsUploadData' );
132
133 if( isset( $sessionData[$this->mSessionKey]['version'] ) &&
134 $sessionData[$this->mSessionKey]['version'] == self::SESSION_VERSION ) {
135 // update the local object from the session
136 $this->mComment = $sessionData[$this->mSessionKey]['mComment'];
137 $this->mSummary = $sessionData[$this->mSessionKey]['mSummary'];
138 $this->mWatch = $sessionData[$this->mSessionKey]['mWatch'];
139 $this->mIgnorewarnings = $sessionData[$this->mSessionKey]['mIgnorewarnings'];
140 $this->mFilteredName = $sessionData[$this->mSessionKey]['mFilteredName'];
141 $this->mTempAppendPath = $sessionData[$this->mSessionKey]['mTempAppendPath'];
142 $this->mDesiredDestName = $sessionData[$this->mSessionKey]['mDesiredDestName'];
143 } else {
144 $this->status = array( 'error' => 'missing session data' );
145 return false;
146 }
147 }
148
149 // Lets us return an api result (as flow for chunk uploads is kind of different than others.
150 function performUpload( $summary = '', $comment = '', $watch = '', $user ){
151 global $wgUser;
152
153 if( $this->chunk_mode == UploadFromChunks::INIT ){
154 // firefogg expects a specific result per:
155 // http://www.firefogg.org/dev/chunk_post.html
156
157 // it's okay to return the token here because
158 // a) the user must have requested the token to get here and
159 // b) should only happen over POST
160 // c) (we need the token to validate chunks are coming from a non-xss request)
161 $token = urlencode( $wgUser->editToken() );
162 ob_clean();
163 echo FormatJson::encode( array(
164 'uploadUrl' => wfExpandUrl( wfScript( 'api' ) ) . "?action=upload&".
165 "token={$token}&format=json&enablechunks=true&chunksessionkey=".
166 $this->setupChunkSession( $summary, $comment, $watch ) ) );
167 exit( 0 );
168 } else if( $this->chunk_mode == UploadFromChunks::CHUNK ){
169 $status = $this->doChunkAppend();
170 if( $status->isOK() ){
171 // return success:
172 // firefogg expects a specific result per:
173 // http://www.firefogg.org/dev/chunk_post.html
174 ob_clean();
175 echo FormatJson::encode( array(
176 'result' => 1,
177 'filesize' => filesize( $this->getRealPath( $this->mTempAppendPath ) )
178 )
179 );
180 exit( 0 );
181 /*return array(
182 'result' => 1
183 );*/
184 } else {
185 return $status;
186 }
187 } else if( $this->chunk_mode == UploadFromChunks::DONE ){
188 // update the values from the local (session init) if not paseed again)
189 if( $summary == '' )
190 $summary = $this->mSummary;
191
192 if( $comment == '' )
193 $comment = $this->mComment;
194
195 if( $watch == '' )
196 $watch = $this->mWatch;
197 $status = parent::performUpload( $summary, $comment, $watch, $user );
198 if( !$status->isGood() ) {
199 return $status;
200 }
201 $file = $this->getLocalFile();
202 // firefogg expects a specific result per:
203 // http://www.firefogg.org/dev/chunk_post.html
204 ob_clean();
205 echo FormatJson::encode( array(
206 'result' => 1,
207 'done' => 1,
208 'resultUrl' => $file->getDescriptionUrl()
209 )
210 );
211 exit( 0 );
212
213 }
214 }
215
216 // append the given chunk to the temporary uploaded file. (if no temporary uploaded file exists created it.
217 function doChunkAppend(){
218 global $wgMaxUploadSize;
219 // if we don't have a mTempAppendPath to generate a file from the chunk packaged var:
220 if( !$this->mTempAppendPath ){
221 // get temp name:
222 // make a chunk store path. (append tmp file to chunk)
223 $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
224
225 if( $status->isOK() ) {
226 $this->mTempAppendPath = $status->value;
227 $_SESSION['wsUploadData'][$this->mSessionKey]['mTempAppendPath'] = $this->mTempAppendPath;
228 }
229 return $status;
230 } else {
231 if( is_file( $this->getRealPath( $this->mTempAppendPath ) ) ){
232 $status = $this->appendToUploadFile( $this->mTempAppendPath, $this->mTempPath );
233 } else {
234 $status = Status::newFatal( 'filenotfound', $this->mTempAppendPath );
235 }
236 //check to make sure we have not expanded beyond $wgMaxUploadSize
237 if( filesize( $this->getRealPath( $this->mTempAppendPath ) ) > $wgMaxUploadSize )
238 $status = Status::newFatal( 'largefileserver' );
239
240 return $status;
241 }
242 }
243
244 }