Refactored UploadStash and related classes to use the database for file metadata...
[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 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
12
13 // an instance of UploadStash
14 private $stash;
15
16 //LocalFile repo
17 private $repo;
18
19 public function __construct( $stash = false, $repo = false ) {
20 if( !$this->repo ) {
21 $this->repo = RepoGroup::singleton()->getLocalRepo();
22 }
23
24 if( !$this->stash ) {
25 $this->stash = new UploadStash( $this->repo );
26 }
27
28 return true;
29 }
30
31 public static function isValidKey( $key ) {
32 // this is checked in more detail in UploadStash
33 return preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
34 }
35
36 /**
37 * @param $request WebRequest
38 *
39 * @return Boolean
40 */
41 public static function isValidRequest( $request ) {
42 return self::isValidKey( $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' ) );
43 }
44
45 public function initialize( $key, $name = 'upload_file' ) {
46 /**
47 * Confirming a temporarily stashed upload.
48 * We don't want path names to be forged, so we keep
49 * them in the session on the server and just give
50 * an opaque key to the user agent.
51 */
52 $metadata = $this->stash->getMetadata( $key );
53 $this->initializePathInfo( $name,
54 $this->getRealPath ( $metadata['us_path'] ),
55 $metadata['us_size'],
56 false
57 );
58
59 $this->mFileKey = $key;
60 $this->mVirtualTempPath = $metadata['us_path'];
61 $this->mFileProps = $this->stash->getFileProps( $key );
62 $this->mSourceType = $metadata['us_source_type'];
63 }
64
65 /**
66 * @param $request WebRequest
67 */
68 public function initializeFromRequest( &$request ) {
69 $fileKey = $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' );
70
71 $desiredDestName = $request->getText( 'wpDestFile' );
72 if( !$desiredDestName ) {
73 $desiredDestName = $request->getText( 'wpUploadFile' ) || $request->getText( 'filename' );
74 }
75 return $this->initialize( $fileKey, $desiredDestName );
76 }
77
78 public function getSourceType() {
79 return $this->mSourceType;
80 }
81
82 /**
83 * File has been previously verified so no need to do so again.
84 *
85 * @return bool
86 */
87 protected function verifyFile() {
88 return true;
89 }
90
91 /**
92 * There is no need to stash the image twice
93 */
94 public function stashFile( $key = null ) {
95 if ( !empty( $this->mFileKey ) ) {
96 return $this->mFileKey;
97 }
98 return parent::stashFileGetKey();
99 }
100
101 /**
102 * Alias for stashFile
103 */
104 public function stashSession( $key = null ) {
105 return $this->stashFile( $key );
106 }
107
108 /**
109 * Remove a temporarily kept file stashed by saveTempUploadedFile().
110 * @return success
111 */
112 public function unsaveUploadedFile() {
113 return $stash->removeFile( $this->mFileKey );
114 }
115
116 }