File::checkPassword()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 35
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
ccs 0
cts 18
cp 0
rs 8.439
cc 6
eloc 12
nc 4
nop 1
crap 42
1
<?php
2
/**
3
 * ownCloud - Documents App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either 
11
 * version 3 of the License, or any later version.
12
 * 
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *  
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 * 
21
 */
22
23
namespace OCA\Documents;
24
25
use \OC\Files\View;
26
27
class File {
28
	protected $fileId;
29
	protected $owner;
30
	protected $sharing;
31
	protected $token;
32
	protected $passwordProtected = false;
33
	protected $ownerView;
34
	protected $ownerViewFiles;
35
	protected $path;
36
	protected $pathFiles;
37
38
	public function __construct($fileId, $shareOps = null, $token = ''){
39
		if (!$fileId){
40
			throw new \Exception('No valid file has been passed');
41
		}
42
43
		$this->fileId = $fileId;
44
		$this->sharing = $shareOps;
45
		$this->token = $token;
46
		
47
		if ($this->isPublicShare()) {
48
			if (isset($this->sharing['uid_owner'])){
49
				$this->owner = $this->sharing['uid_owner'];
50
				if (!\OC::$server->getUserManager()->userExists($this->sharing['uid_owner'])) {
51
					throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist ');
52
				}
53
54
				\OC_Util::tearDownFS();
55
				\OC_Util::setupFS($this->sharing['uid_owner']);
56
			} else {
57
				throw new \Exception($this->fileId . ' is a broken share');
58
			}
59
		} else {
60
			$this->owner = \OC::$server->getUserSession()->getUser()->getUID();
61
		}
62
		$this->initViews();
63
	}
64
	
65
	
66
	public static function getByShareToken($token){
67
		$linkItem = \OCP\Share::getShareByToken($token, false);
68
		if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
69
			// seems to be a valid share
70
			$rootLinkItem = \OCP\Share::resolveReShare($linkItem);
71
		} else {
72
			throw new \Exception('This file was probably unshared');
73
		}
74
		
75
		$file = new File($rootLinkItem['file_source'], $rootLinkItem, $token);
76
		
77
		if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){
78
			$file->setPasswordProtected(true);
79
		}
80
		
81
		return $file;
82
	}
83
84
	public function getToken(){
85
		return $this->token;
86
	}
87
	
88
	public function getFileId(){
89
		return $this->fileId;
90
	}
91
	
92
	public function setToken($token){
93
		$this->token = $token;
94
	}
95
	
96
	public function isPublicShare(){
97
		return  !empty($this->token);
98
	}
99
	
100
	public function isPasswordProtected(){
101
		return $this->passwordProtected;
102
	}
103
104
	/**
105
	 * @param string $password
106
	 * @return boolean
107
	 */
108
	public function checkPassword($password){
109
		$shareId  = $this->sharing['id'];
110
		if (!$this->isPasswordProtected()
111
			|| (\OC::$server->getSession()->exists('public_link_authenticated')
112
				&& \OC::$server->getSession()->get('public_link_authenticated') === (string)$shareId
113
			)
114
		){
115
			return true;
116
		}
117
		
118
		// Check Password
119
		$newHash = '';
120
		if(\OC::$server->getHasher()->verify($password, $this->getPassword(), $newHash)) {
121
			\OC::$server->getSession()->set('public_link_authenticated', (string)$shareId);
122
123
			/**
124
			 * FIXME: Migrate old hashes to new hash format
125
			 * Due to the fact that there is no reasonable functionality to update the password
126
			 * of an existing share no migration is yet performed there.
127
			 * The only possibility is to update the existing share which will result in a new
128
			 * share ID and is a major hack.
129
			 *
130
			 * In the future the migration should be performed once there is a proper method
131
			 * to update the share's password. (for example `$share->updatePassword($password)`
132
			 *
133
			 * @link https://github.com/owncloud/core/issues/10671
134
			 */
135
			if(!empty($newHash)) {
136
137
			}
138
139
			return true;
140
		}
141
		return false;
142
	}
143
	
144
	/**
145
	 * @param boolean $value
146
	 */
147
	public function setPasswordProtected($value){
148
		$this->passwordProtected = $value;
149
	}
150
	
151
	public function getOwner(){
152
		return $this->owner;
153
	}
154
	
155
	public function getOwnerView($relativeToFiles = false){
156
		return $relativeToFiles ? $this->ownerViewFiles : $this->ownerView;
157
	}
158
	
159
	public function getPath($relativeToFiles = false){
160
		return $relativeToFiles ? $this->pathFiles : $this->path;
161
	}
162
	
163
	public function getPermissions(){
164
		$fileInfo = $this->ownerView->getFileInfo($this->path);
165
		return $fileInfo->getPermissions();
166
	}
167
	
168
	protected function initViews(){
169
		$this->ownerView = new View('/' . $this->owner);
170
		$this->ownerViewFiles = new View('/' . $this->owner . '/files');
171
		$this->path = $this->ownerView->getPath($this->fileId);
172
		$this->pathFiles = $this->ownerViewFiles->getPath($this->fileId);
173
		
174
		if (!$this->ownerView->is_file($this->path)){
175
			throw new \Exception('Object ' . $this->path . ' is not a file.');
176
		}
177
		//TODO check if it is a valid odt
178
		
179
		$mimetype = $this->ownerView->getMimeType($this->path);
180
		if (!Filter::isSupportedMimetype($mimetype)){
181
			throw new \Exception( $this->path . ' is ' . $mimetype . ' and is not supported by Documents app');
182
		}
183
	}
184
	
185
	protected function getPassword(){
186
		return $this->sharing['share_with'];
187
	}
188
}
189