* minor fix to uploadFromChunks in case its initialized with a real path instead...
[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 * getRealPath
110 * @param string $srcPath the source path
111 * @returns the real path if it was a virtual url
112 */
113 function getRealPath( $srcPath ){
114 $repo = RepoGroup::singleton()->getLocalRepo();
115 if ( $repo->isVirtualUrl( $srcPath ) ) {
116 return $repo->resolveVirtualUrl( $srcPath );
117 }
118 return $srcPath;
119 }
120
121 // pretty ugly inter-mixing of mParam and local vars
122 function setupChunkSession( $summary, $comment, $watch ) {
123 $this->mSessionKey = $this->getSessionKey();
124 $_SESSION['wsUploadData'][$this->mSessionKey] = array(
125 'mComment' => $comment,
126 'mSummary' => $summary,
127 'mWatch' => $watch,
128 'mIgnorewarnings' => true, //ignore warning on chunk uploads (for now)
129 'mFilteredName' => $this->mFilteredName,
130 'mTempAppendPath' => null, // the repo append path (not temporary local node mTempPath)
131 'mDesiredDestName' => $this->mDesiredDestName,
132 'version' => self::SESSION_VERSION,
133 );
134 return $this->mSessionKey;
135 }
136
137 function initFromSessionKey( $sessionKey, $request ){
138 if( !$sessionKey || empty( $sessionKey ) ){
139 return false;
140 }
141 $this->mSessionKey = $sessionKey;
142 // load the sessionData array:
143 $sessionData = $request->getSessionData( 'wsUploadData' );
144
145 if( isset( $sessionData[$this->mSessionKey]['version'] ) &&
146 $sessionData[$this->mSessionKey]['version'] == self::SESSION_VERSION ) {
147 // update the local object from the session
148 $this->mComment = $sessionData[$this->mSessionKey]['mComment'];
149 $this->mSummary = $sessionData[$this->mSessionKey]['mSummary'];
150 $this->mWatch = $sessionData[$this->mSessionKey]['mWatch'];
151 $this->mIgnorewarnings = $sessionData[$this->mSessionKey]['mIgnorewarnings'];
152 $this->mFilteredName = $sessionData[$this->mSessionKey]['mFilteredName'];
153 $this->mTempAppendPath = $sessionData[$this->mSessionKey]['mTempAppendPath'];
154 $this->mDesiredDestName = $sessionData[$this->mSessionKey]['mDesiredDestName'];
155 } else {
156 $this->status = array( 'error' => 'missing session data' );
157 return false;
158 }
159 }
160
161 // Lets us return an api result (as flow for chunk uploads is kind of different than others.
162 function performUpload( $summary = '', $comment = '', $watch = '', $user ){
163 global $wgUser;
164
165 if( $this->chunk_mode == UploadFromChunks::INIT ){
166 // firefogg expects a specific result per:
167 // http://www.firefogg.org/dev/chunk_post.html
168
169 // it's okay to return the token here because
170 // a) the user must have requested the token to get here and
171 // b) should only happen over POST
172 // c) (we need the token to validate chunks are coming from a non-xss request)
173 $token = urlencode( $wgUser->editToken() );
174 ob_clean();
175 echo FormatJson::encode( array(
176 'uploadUrl' => wfExpandUrl( wfScript( 'api' ) ) . "?action=upload&".
177 "token={$token}&format=json&enablechunks=true&chunksessionkey=".
178 $this->setupChunkSession( $summary, $comment, $watch ) ) );
179 exit( 0 );
180 } else if( $this->chunk_mode == UploadFromChunks::CHUNK ){
181 $status = $this->doChunkAppend();
182 if( $status->isOK() ){
183 // return success:
184 // firefogg expects a specific result per:
185 // http://www.firefogg.org/dev/chunk_post.html
186 ob_clean();
187 echo FormatJson::encode( array(
188 'result' => 1,
189 'filesize' => filesize( $this->getRealPath( $this->mTempAppendPath ) )
190 )
191 );
192 exit( 0 );
193 /*return array(
194 'result' => 1
195 );*/
196 } else {
197 return $status;
198 }
199 } else if( $this->chunk_mode == UploadFromChunks::DONE ){
200 // update the values from the local (session init) if not paseed again)
201 if( $summary == '' )
202 $summary = $this->mSummary;
203
204 if( $comment == '' )
205 $comment = $this->mComment;
206
207 if( $watch == '' )
208 $watch = $this->mWatch;
209 $status = parent::performUpload( $summary, $comment, $watch, $user );
210 if( !$status->isGood() ) {
211 return $status;
212 }
213 $file = $this->getLocalFile();
214 // firefogg expects a specific result per:
215 // http://www.firefogg.org/dev/chunk_post.html
216 ob_clean();
217 echo FormatJson::encode( array(
218 'result' => 1,
219 'done' => 1,
220 'resultUrl' => $file->getDescriptionUrl()
221 )
222 );
223 exit( 0 );
224
225 }
226 }
227
228 // append the given chunk to the temporary uploaded file. (if no temporary uploaded file exists created it.
229 function doChunkAppend(){
230 global $wgMaxUploadSize;
231 // if we don't have a mTempAppendPath to generate a file from the chunk packaged var:
232 if( !$this->mTempAppendPath ){
233 // get temp name:
234 // make a chunk store path. (append tmp file to chunk)
235 $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
236
237 if( $status->isOK() ) {
238 $this->mTempAppendPath = $status->value;
239 $_SESSION['wsUploadData'][$this->mSessionKey]['mTempAppendPath'] = $this->mTempAppendPath;
240 }
241 return $status;
242 } else {
243 if( is_file( $this->getRealPath( $this->mTempAppendPath ) ) ){
244 $status = $this->appendToUploadFile( $this->mTempAppendPath, $this->mTempPath );
245 } else {
246 $status = Status::newFatal( 'filenotfound', $this->mTempAppendPath );
247 }
248 //check to make sure we have not expanded beyond $wgMaxUploadSize
249 if( filesize( $this->getRealPath( $this->mTempAppendPath ) ) > $wgMaxUploadSize )
250 $status = Status::newFatal( 'largefileserver' );
251
252 return $status;
253 }
254 }
255
256 }