file.php
7.44 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* ownCloud - Documents 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 Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Documents;
class File {
protected $fileId;
protected $owner;
protected $path;
protected $sharing;
protected $passwordProtected = false;
public function __construct($fileId, $shareOps = null){
if (!$fileId){
throw new \Exception('No valid file has been passed');
}
$this->fileId = $fileId;
//if you know how to get sharing info by fileId via API,
//please send me a link to video tutorial :/
if (!is_null($shareOps)){
$this->sharing = $shareOps;
} else {
$this->sharing = $this->getSharingOps();
}
}
public static function getByShareToken($token){
$linkItem = \OCP\Share::getShareByToken($token);
if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
// seems to be a valid share
$rootLinkItem = \OCP\Share::resolveReShare($linkItem);
$fileOwner = $rootLinkItem['uid_owner'];
} else {
throw new \Exception('This file was probably unshared');
}
if (!isset($rootLinkItem['path']) && isset($rootLinkItem['file_target'])){
$rootLinkItem['path'] = 'files/' . $rootLinkItem['file_target'];
}
$file = new File($rootLinkItem['file_source'], array($rootLinkItem));
if (isset($rootLinkItem['uid_owner'])){
\OC_Util::tearDownFS();
\OC_Util::setupFS($rootLinkItem['uid_owner']);
$file->setOwner($rootLinkItem['uid_owner']);
$file->setPath('/files' . \OC\Files\Filesystem::getPath($linkItem['file_source']));
}
if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){
$file->setPasswordProtected(true);
}
return $file;
}
public function getFileId(){
return $this->fileId;
}
public function setOwner($owner){
$this->owner = $owner;
}
public function setPath($path){
$this->path = $path;
}
public function isPublicShare(){
foreach ($this->sharing as $share){
if (
$share['share_type'] == \OCP\Share::SHARE_TYPE_LINK
|| $share['share_type'] == \OCP\Share::SHARE_TYPE_EMAIL
){
return true;
}
}
return false;
}
public function isPasswordProtected(){
return $this->passwordProtected;
}
public function checkPassword($password){
$shareId = $this->getShareId();
if (!$this->isPasswordProtected()
|| (\OC::$session->exists('public_link_authenticated')
&& \OC::$session->get('public_link_authenticated') === $shareId)
){
return true;
}
// Check Password
$forcePortable = (CRYPT_BLOWFISH != 1);
$hasher = new \PasswordHash(8, $forcePortable);
if ($hasher->CheckPassword($password.\OC_Config::getValue('passwordsalt', ''),
$this->getPassword())) {
// Save item id in session for future request
\OC::$session->set('public_link_authenticated', $shareId);
return true;
}
return false;
}
public function setPasswordProtected($value){
$this->passwordProtected = $value;
}
public function getPermissions(){
if (count($this->sharing)){
if ($this->isPublicShare()){
$permissions = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE;
} else {
$permissions = $this->sharing[0]['permissions'];
}
} else {
list($owner, $path) = $this->getOwnerViewAndPath();
$permissions = 0;
if (\OC\Files\Filesystem::isReadable($path)){
$permissions |= \OCP\PERMISSION_READ;
}
if (\OC\Files\Filesystem::isUpdatable($path)){
$permissions |= \OCP\PERMISSION_UPDATE;
}
}
return $permissions;
}
/**
* Rename this file to the given name
* @param string $newName name to give (without path)
* @return boolean true if rename succeeded, false otherwise
*/
public function renameTo($newName) {
list($owner, $path) = $this->getOwnerViewAndPath();
$newPath = dirname($path) . '/' . $newName;
return \OC\Files\Filesystem::rename($path, $newPath);
}
/**
*
* @return string owner of the current file item
* @throws \Exception
*/
public function getOwnerViewAndPath(){
if (!$this->owner || !$this->path){
$info = $this->getSharedFileOwnerAndPath();
if (is_array($info) && count($info)){
$owner = $info[0];
$path = $info[1];
} else {
list($owner, $path) = $this->getLocalFileOwnerAndPath();
}
if (!$path){
throw new \Exception($this->fileId . ' can not be resolved');
}
$this->path = $path;
$this->owner = $owner;
}
/* to emit hooks properly, view root should contain /user/files */
if (strpos($this->path, 'files') === 0){
$path = preg_replace('|^files|', '', $this->path);
$view = new View('/' . $this->owner . '/files');
} else {
$path = $this->path;
$view = new View('/' . $this->owner);
}
if (!$view->file_exists($path)){
throw new \Exception($this->path . ' doesn\'t exist');
}
return array($view, $path);
}
public function getOwner(){
if (!$this->owner){
$this->getOwnerViewAndPath();
}
return $this->owner;
}
protected function getSharedFileOwnerAndPath(){
$result = array();
foreach ($this->sharing as $share){
return array(
$share['uid_owner'],
$share['path']
);
}
return $result;
}
protected function getLocalFileOwnerAndPath(){
$fileInfo = \OC\Files\Cache\Cache::getById($this->fileId);
$owner = \OCP\User::getUser();
if (!$owner){
throw new Exception('Guest users can\'t access local files. This one was probably unshared recently.');
}
return array ($owner, @$fileInfo[1]);
}
protected function getPassword(){
return $this->sharing[0]['share_with'];
}
protected function getShareId(){
return $this->sharing[0]['id'];
}
protected function getSharingOps(){
$where = 'AND `file_source`=?';
$values = array($this->fileId);
if (\OCP\User::isLoggedIn()){
$where .= ' AND ((`share_type`=' . \OCP\Share::SHARE_TYPE_USER . ' AND `share_with`=?) OR `share_type`=' . \OCP\Share::SHARE_TYPE_LINK . ')';
$values[] = \OCP\User::getUser();
} else {
$where .= ' AND (`share_type`=' . \OCP\Share::SHARE_TYPE_LINK . ')';
}
$query = \OC_DB::prepare('SELECT `*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `uid_owner`, '
.'`share_type`, `share_with`, `file_source`, `path`, `file_target`, '
.'`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, '
.'`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`'
.'FROM `*PREFIX*share` INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` WHERE `item_type` = \'file\' ' . $where);
$result = $query->execute($values);
$shares = $result->fetchAll();
$origins = array();
if (is_array($shares)){
foreach ($shares as $share){
$origin = \OCP\Share::resolveReShare($share);
if (!isset($origin['path']) && isset($origin['file_target'])){
$origin['path'] = 'files/' . $origin['file_target'];
}
$origins[] = $origin;
}
}
return $origins;
}
}