Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file.php « lib - github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4668e569c07e5c2259fd5c0c4e793c97028b0973 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
 * ownCloud - Richdocuments App
 *
 * @author Victor Dubiniuk
 * @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either 
 * version 3 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *  
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

namespace OCA\Richdocuments;

use \OC\Files\View;

class File {
	protected $fileId;
	protected $owner;
	protected $sharing;
	protected $token;
	protected $passwordProtected = false;
	protected $ownerView;
	protected $ownerViewFiles;
	protected $path;
	protected $pathFiles;

	public function __construct($fileId, $shareOps = null, $token = ''){
		if (!$fileId){
			throw new \Exception('No valid file has been passed');
		}

		$this->fileId = $fileId;
		$this->sharing = $shareOps;
		$this->token = $token;
		
		if ($this->isPublicShare()) {
			if (isset($this->sharing['uid_owner'])){
				$this->owner = $this->sharing['uid_owner'];
				if (!\OC::$server->getUserManager()->userExists($this->sharing['uid_owner'])) {
					throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist ');
				}

				\OC_Util::tearDownFS();
				\OC_Util::setupFS($this->sharing['uid_owner']);
			} else {
				throw new \Exception($this->fileId . ' is a broken share');
			}
		} else {
			$this->owner = \OC::$server->getUserSession()->getUser()->getUID();
		}
		$this->initViews();
	}
	
	
	public static function getByShareToken($token){
		$linkItem = \OCP\Share::getShareByToken($token, false);
		if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
			// seems to be a valid share
			$rootLinkItem = \OCP\Share::resolveReShare($linkItem);
		} else {
			throw new \Exception('This file was probably unshared');
		}
		
		$file = new File($rootLinkItem['file_source'], $rootLinkItem, $token);
		
		if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){
			$file->setPasswordProtected(true);
		}
		
		return $file;
	}

	public function getToken(){
		return $this->token;
	}
	
	public function getFileId(){
		return $this->fileId;
	}
	
	public function setToken($token){
		$this->token = $token;
	}
	
	public function isPublicShare(){
		return  !empty($this->token);
	}
	
	public function isPasswordProtected(){
		return $this->passwordProtected;
	}

	/**
	 * @param string $password
	 * @return boolean
	 */
	public function checkPassword($password){
		$shareId  = $this->sharing['id'];
		if (!$this->isPasswordProtected()
			|| (\OC::$server->getSession()->exists('public_link_authenticated')
				&& \OC::$server->getSession()->get('public_link_authenticated') === $shareId
			)
		){
			return true;
		}
		
		// Check Password
		$newHash = '';
		if(\OC::$server->getHasher()->verify($password, $this->getPassword(), $newHash)) {
			\OC::$server->getSession()->set('public_link_authenticated', $shareId);

			/**
			 * FIXME: Migrate old hashes to new hash format
			 * Due to the fact that there is no reasonable functionality to update the password
			 * of an existing share no migration is yet performed there.
			 * The only possibility is to update the existing share which will result in a new
			 * share ID and is a major hack.
			 *
			 * In the future the migration should be performed once there is a proper method
			 * to update the share's password. (for example `$share->updatePassword($password)`
			 *
			 * @link https://github.com/owncloud/core/issues/10671
			 */
			if(!empty($newHash)) {

			}

			return true;
		}
		return false;
	}
	
	/**
	 * @param boolean $value
	 */
	public function setPasswordProtected($value){
		$this->passwordProtected = $value;
	}
	
	public function getOwner(){
		return $this->owner;
	}
	
	public function getOwnerView($relativeToFiles = false){
		return $relativeToFiles ? $this->ownerViewFiles : $this->ownerView;
	}
	
	public function getPath($relativeToFiles = false){
		return $relativeToFiles ? $this->pathFiles : $this->path;
	}
	
	public function getPermissions(){
		$fileInfo = $this->ownerView->getFileInfo($this->path);
		return $fileInfo->getPermissions();
	}
	
	protected function initViews(){
		$this->ownerView = new View('/' . $this->owner);
		$this->ownerViewFiles = new View('/' . $this->owner . '/files');
		$this->path = $this->ownerView->getPath($this->fileId);
		$this->pathFiles = $this->ownerViewFiles->getPath($this->fileId);
		
		if (!$this->path || !$this->pathFiles) {
			throw new \Exception($this->fileId . ' can not be resolved');
		}
		
		if (!$this->ownerView->file_exists($this->path)) {
			throw new \Exception($this->path . ' doesn\'t exist');
		}
		
		if (!$this->ownerViewFiles->file_exists($this->pathFiles)) {
			throw new \Exception($this->pathFiles . ' doesn\'t exist');
		}
		
		if (!$this->ownerView->is_file($this->path)){
			throw new \Exception('Object ' . $this->path . ' is not a file.');
		}
		//TODO check if it is a valid odt
		
		$mimetype = $this->ownerView->getMimeType($this->path);
		if (!Filter::isSupportedMimetype($mimetype)){
			throw new \Exception( $this->path . ' is ' . $mimetype . ' and is not supported by RichDocuments app');
		}
	}
	
	protected function getPassword(){
		return $this->sharing['share_with'];
	}
}