|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nextcloud - passman |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
|
6
|
|
|
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected]) |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Passman\Service; |
|
25
|
|
|
|
|
26
|
|
|
use OCA\Passman\Db\File; |
|
27
|
|
|
use OCP\IConfig; |
|
28
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
29
|
|
|
|
|
30
|
|
|
use OCA\Passman\Db\FileMapper; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class FileService { |
|
34
|
|
|
|
|
35
|
|
|
private $fileMapper; |
|
36
|
|
|
private $encryptService; |
|
37
|
|
|
private $server_key; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(FileMapper $fileMapper, EncryptService $encryptService) { |
|
40
|
|
|
$this->fileMapper = $fileMapper; |
|
41
|
|
|
$this->encryptService = $encryptService; |
|
42
|
|
|
$this->server_key = \OC::$server->getConfig()->getSystemValue('passwordsalt', ''); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get a single file. This function also returns the file content. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $fileId |
|
49
|
|
|
* @param null $userId |
|
50
|
|
|
* @return \OCA\Passman\Db\File |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getFile($fileId, $userId = null) { |
|
53
|
|
|
$file = $this->fileMapper->getFile($fileId, $userId); |
|
54
|
|
|
return $this->encryptService->decryptFile($file); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get a single file. This function also returns the file content. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $file_guid |
|
61
|
|
|
* @param null $userId |
|
62
|
|
|
* @return \OCA\Passman\Db\File |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getFileByGuid($file_guid, $userId = null) { |
|
65
|
|
|
$file = $this->fileMapper->getFileByGuid($file_guid, $userId); |
|
66
|
|
|
return $this->encryptService->decryptFile($file); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Upload a new file, |
|
71
|
|
|
* |
|
72
|
|
|
* @param $file array |
|
73
|
|
|
* @param $userId |
|
74
|
|
|
* @return \OCA\Passman\Db\File |
|
75
|
|
|
*/ |
|
76
|
|
|
public function createFile($file, $userId) { |
|
77
|
|
|
$file = $this->encryptService->encryptFile($file); |
|
78
|
|
|
$file = $this->fileMapper->create($file, $userId); |
|
79
|
|
|
return $this->getFile($file->getId()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Delete file |
|
84
|
|
|
* |
|
85
|
|
|
* @param $file_id |
|
86
|
|
|
* @param $userId |
|
87
|
|
|
* @return \OCA\Passman\Db\File |
|
88
|
|
|
*/ |
|
89
|
|
|
public function deleteFile($file_id, $userId) { |
|
90
|
|
|
return $this->fileMapper->deleteFile($file_id, $userId); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Update file |
|
95
|
|
|
* |
|
96
|
|
|
* @param File $file |
|
97
|
|
|
* @return \OCA\Passman\Db\File |
|
98
|
|
|
*/ |
|
99
|
|
|
public function updateFile($file) { |
|
100
|
|
|
$file = $this->encryptService->encryptFile($file); |
|
101
|
|
|
return $this->fileMapper->updateFile($file); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Update file |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $userId |
|
108
|
|
|
* @return File[] |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getFilesFromUser($userId){ |
|
111
|
|
|
$files = $this->fileMapper->getFilesFromUser($userId); |
|
112
|
|
|
$results = array(); |
|
113
|
|
|
foreach ($files as $file){ |
|
114
|
|
|
array_push($results, $this->encryptService->decryptFile($file)); |
|
115
|
|
|
} |
|
116
|
|
|
return $results; |
|
117
|
|
|
} |
|
118
|
|
|
} |