More explicit variable definitions, function documentation
[lhc/web/wiklou.git] / includes / upload / UploadFromStash.php
1 <?php
2 /**
3 * Implements uploading from previously stored file.
4 *
5 * @file
6 * @ingroup upload
7 * @author Bryan Tong Minh
8 */
9
10 class UploadFromStash extends UploadBase {
11
12 protected $initializePathInfo, $mSessionKey, $mVirtualTempPath,
13 $mFileProps, $mSourceType;
14
15 public static function isValidSessionKey( $key, $sessionData ) {
16 return !empty( $key ) &&
17 is_array( $sessionData ) &&
18 isset( $sessionData[$key] ) &&
19 isset( $sessionData[$key]['version'] ) &&
20 $sessionData[$key]['version'] == UploadBase::SESSION_VERSION;
21 }
22
23 /**
24 * @param $request WebRequest
25 *
26 * @return Boolean
27 */
28 public static function isValidRequest( $request ) {
29 $sessionData = $request->getSessionData( UploadBase::SESSION_KEYNAME );
30 return self::isValidSessionKey(
31 $request->getText( 'wpSessionKey' ),
32 $sessionData
33 );
34 }
35
36 public function initialize( $name, $sessionKey, $sessionData ) {
37 /**
38 * Confirming a temporarily stashed upload.
39 * We don't want path names to be forged, so we keep
40 * them in the session on the server and just give
41 * an opaque key to the user agent.
42 */
43
44 $this->initializePathInfo( $name,
45 $this->getRealPath ( $sessionData['mTempPath'] ),
46 $sessionData['mFileSize'],
47 false
48 );
49
50 $this->mSessionKey = $sessionKey;
51 $this->mVirtualTempPath = $sessionData['mTempPath'];
52 $this->mFileProps = $sessionData['mFileProps'];
53 $this->mSourceType = isset( $sessionData['mSourceType'] ) ?
54 $sessionData['mSourceType'] : null;
55 }
56
57 /**
58 * @param $request WebRequest
59 */
60 public function initializeFromRequest( &$request ) {
61 $sessionKey = $request->getText( 'wpSessionKey' );
62 $sessionData = $request->getSessionData( UploadBase::SESSION_KEYNAME );
63
64 $desiredDestName = $request->getText( 'wpDestFile' );
65 if( !$desiredDestName )
66 $desiredDestName = $request->getText( 'wpUploadFile' );
67 return $this->initialize( $desiredDestName, $sessionKey, $sessionData[$sessionKey] );
68 }
69
70 public function getSourceType() {
71 return $this->mSourceType;
72 }
73
74 /**
75 * File has been previously verified so no need to do so again.
76 */
77 protected function verifyFile() {
78 return true;
79 }
80
81 /**
82 * There is no need to stash the image twice
83 */
84 public function stashSession( $key = null ) {
85 if ( !empty( $this->mSessionKey ) )
86 return $this->mSessionKey;
87 return parent::stashSession();
88 }
89
90 /**
91 * Remove a temporarily kept file stashed by saveTempUploadedFile().
92 * @return success
93 */
94 public function unsaveUploadedFile() {
95 $repo = RepoGroup::singleton()->getLocalRepo();
96 $success = $repo->freeTemp( $this->mVirtualTempPath );
97 return $success;
98 }
99
100 }