summaryrefslogtreecommitdiff
path: root/includes/upload/UploadFromStash.php
blob: 17e922b0ad72f573978d3bb9bd48a80f65c76839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
 * @file
 * @ingroup upload
 *
 * Implements uploading from previously stored file.
 *
 * @author Bryan Tong Minh
 */

class UploadFromStash extends UploadBase {
	public static function isValidSessionKey( $key, $sessionData ) {
		return !empty( $key ) &&
			is_array( $sessionData ) &&
			isset( $sessionData[$key] ) &&
			isset( $sessionData[$key]['version'] ) &&
			$sessionData[$key]['version'] == self::SESSION_VERSION;
	}

	public static function isValidRequest( $request ) {
		$sessionData = $request->getSessionData( 'wsUploadData' );
		return self::isValidSessionKey(
			$request->getInt( 'wpSessionKey' ),
			$sessionData
		);
	}

	public function initialize( $name, $sessionKey, $sessionData ) {
			/**
			 * Confirming a temporarily stashed upload.
			 * We don't want path names to be forged, so we keep
			 * them in the session on the server and just give
			 * an opaque key to the user agent.
			 */

			$this->initializePathInfo( $name,
				$this->getRealPath ( $sessionData['mTempPath'] ),
				$sessionData['mFileSize'],
				false
			);
			
			$this->mSessionKey = $sessionKey;
			$this->mVirtualTempPath = $sessionData['mTempPath'];
			$this->mFileProps = $sessionData['mFileProps'];
	}

	public function initializeFromRequest( &$request ) {
		$sessionKey = $request->getInt( 'wpSessionKey' );
		$sessionData = $request->getSessionData('wsUploadData');

		$desiredDestName = $request->getText( 'wpDestFile' );
		if( !$desiredDestName )
			$desiredDestName = $request->getText( 'wpUploadFile' );
		return $this->initialize( $desiredDestName, $sessionKey, $sessionData[$sessionKey] );
	}

	/**
	 * File has been previously verified so no need to do so again.
	 */
	protected function verifyFile() {
		return true;
	}


	/**
	 * There is no need to stash the image twice
	 */
	public function stashSession() {
		if ( !empty( $this->mSessionKey ) )
			return $this->mSessionKey;
		return parent::stashSession();
	}

	/**
	 * Remove a temporarily kept file stashed by saveTempUploadedFile().
	 * @return success
	 */
	public function unsaveUploadedFile() {
		$repo = RepoGroup::singleton()->getLocalRepo();
		$success = $repo->freeTemp( $this->mVirtualTempPath );
		return $success;
	}

}