@@ -24,7 +24,6 @@ |
||
24 | 24 | namespace OCA\DAV; |
25 | 25 | |
26 | 26 | use OCA\DAV\CalDAV\Schedule\IMipPlugin; |
27 | -use OCA\DAV\Connector\FedAuth; |
|
28 | 27 | use OCA\DAV\Connector\Sabre\Auth; |
29 | 28 | use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin; |
30 | 29 | use OCA\DAV\Connector\Sabre\DavAclPlugin; |
@@ -39,117 +39,117 @@ |
||
39 | 39 | |
40 | 40 | class Server { |
41 | 41 | |
42 | - /** @var IRequest */ |
|
43 | - private $request; |
|
44 | - |
|
45 | - public function __construct(IRequest $request, $baseUri) { |
|
46 | - $this->request = $request; |
|
47 | - $this->baseUri = $baseUri; |
|
48 | - $logger = \OC::$server->getLogger(); |
|
49 | - $mailer = \OC::$server->getMailer(); |
|
50 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
51 | - |
|
52 | - $root = new RootCollection(); |
|
53 | - $this->server = new \OCA\DAV\Connector\Sabre\Server($root); |
|
54 | - |
|
55 | - // Backends |
|
56 | - $authBackend = new Auth( |
|
57 | - \OC::$server->getSession(), |
|
58 | - \OC::$server->getUserSession(), |
|
59 | - \OC::$server->getRequest() |
|
60 | - ); |
|
61 | - |
|
62 | - // Set URL explicitly due to reverse-proxy situations |
|
63 | - $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
64 | - $this->server->setBaseUri($this->baseUri); |
|
65 | - |
|
66 | - $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
67 | - $authPlugin = new Plugin($authBackend, 'ownCloud'); |
|
68 | - $this->server->addPlugin($authPlugin); |
|
69 | - |
|
70 | - // allow setup of additional auth backends |
|
71 | - $event = new SabrePluginEvent($this->server); |
|
72 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
73 | - |
|
74 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin()); |
|
75 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
76 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
77 | - $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
78 | - |
|
79 | - // acl |
|
80 | - $acl = new DavAclPlugin(); |
|
81 | - $acl->defaultUsernamePath = 'principals/users'; |
|
82 | - $this->server->addPlugin($acl); |
|
83 | - |
|
84 | - // calendar plugins |
|
85 | - $this->server->addPlugin(new \Sabre\CalDAV\Plugin()); |
|
86 | - $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
87 | - $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin()); |
|
88 | - $this->server->addPlugin(new IMipPlugin($mailer, $logger)); |
|
89 | - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
90 | - $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
91 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
92 | - |
|
93 | - // addressbook plugins |
|
94 | - $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
95 | - $this->server->addPlugin(new VCFExportPlugin()); |
|
96 | - |
|
97 | - // system tags plugins |
|
98 | - $this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin( |
|
99 | - \OC::$server->getSystemTagManager(), |
|
100 | - \OC::$server->getGroupManager(), |
|
101 | - \OC::$server->getUserSession() |
|
102 | - )); |
|
103 | - |
|
104 | - // comments plugin |
|
105 | - $this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin( |
|
106 | - \OC::$server->getCommentsManager(), |
|
107 | - \OC::$server->getUserSession() |
|
108 | - )); |
|
109 | - |
|
110 | - // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
111 | - // we do not provide locking we emulate it using a fake locking plugin. |
|
112 | - if($request->isUserAgent([ |
|
113 | - '/WebDAVFS/', |
|
114 | - '/Microsoft Office OneNote 2013/', |
|
115 | - ])) { |
|
116 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin()); |
|
117 | - } |
|
118 | - |
|
119 | - if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
120 | - $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
121 | - } |
|
122 | - |
|
123 | - // wait with registering these until auth is handled and the filesystem is setup |
|
124 | - $this->server->on('beforeMethod', function () { |
|
125 | - // custom properties plugin must be the last one |
|
126 | - $user = \OC::$server->getUserSession()->getUser(); |
|
127 | - if (!is_null($user)) { |
|
128 | - $view = \OC\Files\Filesystem::getView(); |
|
129 | - $this->server->addPlugin(new FilesPlugin( |
|
130 | - $this->server->tree, |
|
131 | - $view, |
|
132 | - $this->request |
|
133 | - )); |
|
134 | - |
|
135 | - $this->server->addPlugin( |
|
136 | - new \Sabre\DAV\PropertyStorage\Plugin( |
|
137 | - new CustomPropertiesBackend( |
|
138 | - $this->server->tree, |
|
139 | - \OC::$server->getDatabaseConnection(), |
|
140 | - \OC::$server->getUserSession()->getUser() |
|
141 | - ) |
|
142 | - ) |
|
143 | - ); |
|
144 | - |
|
145 | - $this->server->addPlugin( |
|
146 | - new \OCA\DAV\Connector\Sabre\QuotaPlugin($view) |
|
147 | - ); |
|
148 | - } |
|
149 | - }); |
|
150 | - } |
|
151 | - |
|
152 | - public function exec() { |
|
153 | - $this->server->exec(); |
|
154 | - } |
|
42 | + /** @var IRequest */ |
|
43 | + private $request; |
|
44 | + |
|
45 | + public function __construct(IRequest $request, $baseUri) { |
|
46 | + $this->request = $request; |
|
47 | + $this->baseUri = $baseUri; |
|
48 | + $logger = \OC::$server->getLogger(); |
|
49 | + $mailer = \OC::$server->getMailer(); |
|
50 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
51 | + |
|
52 | + $root = new RootCollection(); |
|
53 | + $this->server = new \OCA\DAV\Connector\Sabre\Server($root); |
|
54 | + |
|
55 | + // Backends |
|
56 | + $authBackend = new Auth( |
|
57 | + \OC::$server->getSession(), |
|
58 | + \OC::$server->getUserSession(), |
|
59 | + \OC::$server->getRequest() |
|
60 | + ); |
|
61 | + |
|
62 | + // Set URL explicitly due to reverse-proxy situations |
|
63 | + $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
64 | + $this->server->setBaseUri($this->baseUri); |
|
65 | + |
|
66 | + $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
67 | + $authPlugin = new Plugin($authBackend, 'ownCloud'); |
|
68 | + $this->server->addPlugin($authPlugin); |
|
69 | + |
|
70 | + // allow setup of additional auth backends |
|
71 | + $event = new SabrePluginEvent($this->server); |
|
72 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
73 | + |
|
74 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin()); |
|
75 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
76 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
77 | + $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
78 | + |
|
79 | + // acl |
|
80 | + $acl = new DavAclPlugin(); |
|
81 | + $acl->defaultUsernamePath = 'principals/users'; |
|
82 | + $this->server->addPlugin($acl); |
|
83 | + |
|
84 | + // calendar plugins |
|
85 | + $this->server->addPlugin(new \Sabre\CalDAV\Plugin()); |
|
86 | + $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
87 | + $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin()); |
|
88 | + $this->server->addPlugin(new IMipPlugin($mailer, $logger)); |
|
89 | + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
90 | + $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
91 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
92 | + |
|
93 | + // addressbook plugins |
|
94 | + $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
95 | + $this->server->addPlugin(new VCFExportPlugin()); |
|
96 | + |
|
97 | + // system tags plugins |
|
98 | + $this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin( |
|
99 | + \OC::$server->getSystemTagManager(), |
|
100 | + \OC::$server->getGroupManager(), |
|
101 | + \OC::$server->getUserSession() |
|
102 | + )); |
|
103 | + |
|
104 | + // comments plugin |
|
105 | + $this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin( |
|
106 | + \OC::$server->getCommentsManager(), |
|
107 | + \OC::$server->getUserSession() |
|
108 | + )); |
|
109 | + |
|
110 | + // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
111 | + // we do not provide locking we emulate it using a fake locking plugin. |
|
112 | + if($request->isUserAgent([ |
|
113 | + '/WebDAVFS/', |
|
114 | + '/Microsoft Office OneNote 2013/', |
|
115 | + ])) { |
|
116 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin()); |
|
117 | + } |
|
118 | + |
|
119 | + if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
120 | + $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
121 | + } |
|
122 | + |
|
123 | + // wait with registering these until auth is handled and the filesystem is setup |
|
124 | + $this->server->on('beforeMethod', function () { |
|
125 | + // custom properties plugin must be the last one |
|
126 | + $user = \OC::$server->getUserSession()->getUser(); |
|
127 | + if (!is_null($user)) { |
|
128 | + $view = \OC\Files\Filesystem::getView(); |
|
129 | + $this->server->addPlugin(new FilesPlugin( |
|
130 | + $this->server->tree, |
|
131 | + $view, |
|
132 | + $this->request |
|
133 | + )); |
|
134 | + |
|
135 | + $this->server->addPlugin( |
|
136 | + new \Sabre\DAV\PropertyStorage\Plugin( |
|
137 | + new CustomPropertiesBackend( |
|
138 | + $this->server->tree, |
|
139 | + \OC::$server->getDatabaseConnection(), |
|
140 | + \OC::$server->getUserSession()->getUser() |
|
141 | + ) |
|
142 | + ) |
|
143 | + ); |
|
144 | + |
|
145 | + $this->server->addPlugin( |
|
146 | + new \OCA\DAV\Connector\Sabre\QuotaPlugin($view) |
|
147 | + ); |
|
148 | + } |
|
149 | + }); |
|
150 | + } |
|
151 | + |
|
152 | + public function exec() { |
|
153 | + $this->server->exec(); |
|
154 | + } |
|
155 | 155 | } |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | |
110 | 110 | // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
111 | 111 | // we do not provide locking we emulate it using a fake locking plugin. |
112 | - if($request->isUserAgent([ |
|
112 | + if ($request->isUserAgent([ |
|
113 | 113 | '/WebDAVFS/', |
114 | 114 | '/Microsoft Office OneNote 2013/', |
115 | 115 | ])) { |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | } |
122 | 122 | |
123 | 123 | // wait with registering these until auth is handled and the filesystem is setup |
124 | - $this->server->on('beforeMethod', function () { |
|
124 | + $this->server->on('beforeMethod', function() { |
|
125 | 125 | // custom properties plugin must be the last one |
126 | 126 | $user = \OC::$server->getUserSession()->getUser(); |
127 | 127 | if (!is_null($user)) { |
@@ -369,7 +369,7 @@ |
||
369 | 369 | * @param string $path path to the file which should be updated |
370 | 370 | * @param string $uid of the user who performs the operation |
371 | 371 | * @param array $accessList who has access to the file contains the key 'users' and 'public' |
372 | - * @return boolean |
|
372 | + * @return null|boolean |
|
373 | 373 | */ |
374 | 374 | public function update($path, $uid, array $accessList) { |
375 | 375 |
@@ -43,522 +43,522 @@ |
||
43 | 43 | |
44 | 44 | class Encryption implements IEncryptionModule { |
45 | 45 | |
46 | - const ID = 'OC_DEFAULT_MODULE'; |
|
47 | - const DISPLAY_NAME = 'Default encryption module'; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var Crypt |
|
51 | - */ |
|
52 | - private $crypt; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $cipher; |
|
56 | - |
|
57 | - /** @var string */ |
|
58 | - private $path; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - private $user; |
|
62 | - |
|
63 | - /** @var string */ |
|
64 | - private $fileKey; |
|
65 | - |
|
66 | - /** @var string */ |
|
67 | - private $writeCache; |
|
68 | - |
|
69 | - /** @var KeyManager */ |
|
70 | - private $keyManager; |
|
71 | - |
|
72 | - /** @var array */ |
|
73 | - private $accessList; |
|
74 | - |
|
75 | - /** @var boolean */ |
|
76 | - private $isWriteOperation; |
|
77 | - |
|
78 | - /** @var Util */ |
|
79 | - private $util; |
|
80 | - |
|
81 | - /** @var Session */ |
|
82 | - private $session; |
|
83 | - |
|
84 | - /** @var ILogger */ |
|
85 | - private $logger; |
|
86 | - |
|
87 | - /** @var IL10N */ |
|
88 | - private $l; |
|
89 | - |
|
90 | - /** @var EncryptAll */ |
|
91 | - private $encryptAll; |
|
92 | - |
|
93 | - /** @var bool */ |
|
94 | - private $useMasterPassword; |
|
95 | - |
|
96 | - /** @var DecryptAll */ |
|
97 | - private $decryptAll; |
|
98 | - |
|
99 | - /** @var int unencrypted block size if block contains signature */ |
|
100 | - private $unencryptedBlockSizeSigned = 6072; |
|
101 | - |
|
102 | - /** @var int unencrypted block size */ |
|
103 | - private $unencryptedBlockSize = 6126; |
|
104 | - |
|
105 | - /** @var int Current version of the file */ |
|
106 | - private $version = 0; |
|
107 | - |
|
108 | - /** @var array remember encryption signature version */ |
|
109 | - private static $rememberVersion = []; |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * |
|
114 | - * @param Crypt $crypt |
|
115 | - * @param KeyManager $keyManager |
|
116 | - * @param Util $util |
|
117 | - * @param Session $session |
|
118 | - * @param EncryptAll $encryptAll |
|
119 | - * @param DecryptAll $decryptAll |
|
120 | - * @param ILogger $logger |
|
121 | - * @param IL10N $il10n |
|
122 | - */ |
|
123 | - public function __construct(Crypt $crypt, |
|
124 | - KeyManager $keyManager, |
|
125 | - Util $util, |
|
126 | - Session $session, |
|
127 | - EncryptAll $encryptAll, |
|
128 | - DecryptAll $decryptAll, |
|
129 | - ILogger $logger, |
|
130 | - IL10N $il10n) { |
|
131 | - $this->crypt = $crypt; |
|
132 | - $this->keyManager = $keyManager; |
|
133 | - $this->util = $util; |
|
134 | - $this->session = $session; |
|
135 | - $this->encryptAll = $encryptAll; |
|
136 | - $this->decryptAll = $decryptAll; |
|
137 | - $this->logger = $logger; |
|
138 | - $this->l = $il10n; |
|
139 | - $this->useMasterPassword = $util->isMasterKeyEnabled(); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return string defining the technical unique id |
|
144 | - */ |
|
145 | - public function getId() { |
|
146 | - return self::ID; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
151 | - * |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - public function getDisplayName() { |
|
155 | - return self::DISPLAY_NAME; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * start receiving chunks from a file. This is the place where you can |
|
160 | - * perform some initial step before starting encrypting/decrypting the |
|
161 | - * chunks |
|
162 | - * |
|
163 | - * @param string $path to the file |
|
164 | - * @param string $user who read/write the file |
|
165 | - * @param string $mode php stream open mode |
|
166 | - * @param array $header contains the header data read from the file |
|
167 | - * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
168 | - * |
|
169 | - * @return array $header contain data as key-value pairs which should be |
|
170 | - * written to the header, in case of a write operation |
|
171 | - * or if no additional data is needed return a empty array |
|
172 | - */ |
|
173 | - public function begin($path, $user, $mode, array $header, array $accessList) { |
|
174 | - $this->path = $this->getPathToRealFile($path); |
|
175 | - $this->accessList = $accessList; |
|
176 | - $this->user = $user; |
|
177 | - $this->isWriteOperation = false; |
|
178 | - $this->writeCache = ''; |
|
179 | - |
|
180 | - if ($this->session->decryptAllModeActivated()) { |
|
181 | - $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path); |
|
182 | - $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid()); |
|
183 | - $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
184 | - $shareKey, |
|
185 | - $this->session->getDecryptAllKey()); |
|
186 | - } else { |
|
187 | - $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user); |
|
188 | - } |
|
189 | - |
|
190 | - // always use the version from the original file, also part files |
|
191 | - // need to have a correct version number if they get moved over to the |
|
192 | - // final location |
|
193 | - $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
194 | - |
|
195 | - if ( |
|
196 | - $mode === 'w' |
|
197 | - || $mode === 'w+' |
|
198 | - || $mode === 'wb' |
|
199 | - || $mode === 'wb+' |
|
200 | - ) { |
|
201 | - $this->isWriteOperation = true; |
|
202 | - if (empty($this->fileKey)) { |
|
203 | - $this->fileKey = $this->crypt->generateFileKey(); |
|
204 | - } |
|
205 | - } else { |
|
206 | - // if we read a part file we need to increase the version by 1 |
|
207 | - // because the version number was also increased by writing |
|
208 | - // the part file |
|
209 | - if(Scanner::isPartialFile($path)) { |
|
210 | - $this->version = $this->version + 1; |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - if ($this->isWriteOperation) { |
|
215 | - $this->cipher = $this->crypt->getCipher(); |
|
216 | - } elseif (isset($header['cipher'])) { |
|
217 | - $this->cipher = $header['cipher']; |
|
218 | - } else { |
|
219 | - // if we read a file without a header we fall-back to the legacy cipher |
|
220 | - // which was used in <=oC6 |
|
221 | - $this->cipher = $this->crypt->getLegacyCipher(); |
|
222 | - } |
|
223 | - |
|
224 | - return array('cipher' => $this->cipher, 'signed' => 'true'); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * last chunk received. This is the place where you can perform some final |
|
229 | - * operation and return some remaining data if something is left in your |
|
230 | - * buffer. |
|
231 | - * |
|
232 | - * @param string $path to the file |
|
233 | - * @param int $position |
|
234 | - * @return string remained data which should be written to the file in case |
|
235 | - * of a write operation |
|
236 | - * @throws PublicKeyMissingException |
|
237 | - * @throws \Exception |
|
238 | - * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException |
|
239 | - */ |
|
240 | - public function end($path, $position = 0) { |
|
241 | - $result = ''; |
|
242 | - if ($this->isWriteOperation) { |
|
243 | - $this->keyManager->setVersion($path, $this->version + 1, new View()); |
|
244 | - // in case of a part file we remember the new signature versions |
|
245 | - // the version will be set later on update. |
|
246 | - // This way we make sure that other apps listening to the pre-hooks |
|
247 | - // still get the old version which should be the correct value for them |
|
248 | - if (Scanner::isPartialFile($path)) { |
|
249 | - self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1; |
|
250 | - } |
|
251 | - if (!empty($this->writeCache)) { |
|
252 | - $result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position); |
|
253 | - $this->writeCache = ''; |
|
254 | - } |
|
255 | - $publicKeys = array(); |
|
256 | - if ($this->useMasterPassword === true) { |
|
257 | - $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
258 | - } else { |
|
259 | - foreach ($this->accessList['users'] as $uid) { |
|
260 | - try { |
|
261 | - $publicKeys[$uid] = $this->keyManager->getPublicKey($uid); |
|
262 | - } catch (PublicKeyMissingException $e) { |
|
263 | - $this->logger->warning( |
|
264 | - 'no public key found for user "{uid}", user will not be able to read the file', |
|
265 | - ['app' => 'encryption', 'uid' => $uid] |
|
266 | - ); |
|
267 | - // if the public key of the owner is missing we should fail |
|
268 | - if ($uid === $this->user) { |
|
269 | - throw $e; |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - $publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->user); |
|
276 | - $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys); |
|
277 | - $this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles); |
|
278 | - } |
|
279 | - return $result; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * encrypt data |
|
284 | - * |
|
285 | - * @param string $data you want to encrypt |
|
286 | - * @param int $position |
|
287 | - * @return string encrypted data |
|
288 | - */ |
|
289 | - public function encrypt($data, $position = 0) { |
|
290 | - // If extra data is left over from the last round, make sure it |
|
291 | - // is integrated into the next block |
|
292 | - if ($this->writeCache) { |
|
293 | - |
|
294 | - // Concat writeCache to start of $data |
|
295 | - $data = $this->writeCache . $data; |
|
296 | - |
|
297 | - // Clear the write cache, ready for reuse - it has been |
|
298 | - // flushed and its old contents processed |
|
299 | - $this->writeCache = ''; |
|
300 | - |
|
301 | - } |
|
302 | - |
|
303 | - $encrypted = ''; |
|
304 | - // While there still remains some data to be processed & written |
|
305 | - while (strlen($data) > 0) { |
|
306 | - |
|
307 | - // Remaining length for this iteration, not of the |
|
308 | - // entire file (may be greater than 8192 bytes) |
|
309 | - $remainingLength = strlen($data); |
|
310 | - |
|
311 | - // If data remaining to be written is less than the |
|
312 | - // size of 1 6126 byte block |
|
313 | - if ($remainingLength < $this->unencryptedBlockSizeSigned) { |
|
314 | - |
|
315 | - // Set writeCache to contents of $data |
|
316 | - // The writeCache will be carried over to the |
|
317 | - // next write round, and added to the start of |
|
318 | - // $data to ensure that written blocks are |
|
319 | - // always the correct length. If there is still |
|
320 | - // data in writeCache after the writing round |
|
321 | - // has finished, then the data will be written |
|
322 | - // to disk by $this->flush(). |
|
323 | - $this->writeCache = $data; |
|
324 | - |
|
325 | - // Clear $data ready for next round |
|
326 | - $data = ''; |
|
327 | - |
|
328 | - } else { |
|
329 | - |
|
330 | - // Read the chunk from the start of $data |
|
331 | - $chunk = substr($data, 0, $this->unencryptedBlockSizeSigned); |
|
332 | - |
|
333 | - $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, $position); |
|
334 | - |
|
335 | - // Remove the chunk we just processed from |
|
336 | - // $data, leaving only unprocessed data in $data |
|
337 | - // var, for handling on the next round |
|
338 | - $data = substr($data, $this->unencryptedBlockSizeSigned); |
|
339 | - |
|
340 | - } |
|
341 | - |
|
342 | - } |
|
343 | - |
|
344 | - return $encrypted; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * decrypt data |
|
349 | - * |
|
350 | - * @param string $data you want to decrypt |
|
351 | - * @param int $position |
|
352 | - * @return string decrypted data |
|
353 | - * @throws DecryptionFailedException |
|
354 | - */ |
|
355 | - public function decrypt($data, $position = 0) { |
|
356 | - if (empty($this->fileKey)) { |
|
357 | - $msg = 'Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'; |
|
358 | - $hint = $this->l->t('Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
359 | - $this->logger->error($msg); |
|
360 | - |
|
361 | - throw new DecryptionFailedException($msg, $hint); |
|
362 | - } |
|
363 | - |
|
364 | - return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * update encrypted file, e.g. give additional users access to the file |
|
369 | - * |
|
370 | - * @param string $path path to the file which should be updated |
|
371 | - * @param string $uid of the user who performs the operation |
|
372 | - * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
373 | - * @return boolean |
|
374 | - */ |
|
375 | - public function update($path, $uid, array $accessList) { |
|
376 | - |
|
377 | - if (empty($accessList)) { |
|
378 | - if (isset(self::$rememberVersion[$path])) { |
|
379 | - $this->keyManager->setVersion($path, self::$rememberVersion[$path], new View()); |
|
380 | - unset(self::$rememberVersion[$path]); |
|
381 | - } |
|
382 | - return; |
|
383 | - } |
|
384 | - |
|
385 | - $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
386 | - |
|
387 | - if (!empty($fileKey)) { |
|
388 | - |
|
389 | - $publicKeys = array(); |
|
390 | - if ($this->useMasterPassword === true) { |
|
391 | - $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
392 | - } else { |
|
393 | - foreach ($accessList['users'] as $user) { |
|
394 | - try { |
|
395 | - $publicKeys[$user] = $this->keyManager->getPublicKey($user); |
|
396 | - } catch (PublicKeyMissingException $e) { |
|
397 | - $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage()); |
|
398 | - } |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $uid); |
|
403 | - |
|
404 | - $encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys); |
|
405 | - |
|
406 | - $this->keyManager->deleteAllFileKeys($path); |
|
407 | - |
|
408 | - $this->keyManager->setAllFileKeys($path, $encryptedFileKey); |
|
409 | - |
|
410 | - } else { |
|
411 | - $this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted', |
|
412 | - array('file' => $path, 'app' => 'encryption')); |
|
413 | - |
|
414 | - return false; |
|
415 | - } |
|
416 | - |
|
417 | - return true; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * should the file be encrypted or not |
|
422 | - * |
|
423 | - * @param string $path |
|
424 | - * @return boolean |
|
425 | - */ |
|
426 | - public function shouldEncrypt($path) { |
|
427 | - if ($this->util->shouldEncryptHomeStorage() === false) { |
|
428 | - $storage = $this->util->getStorage($path); |
|
429 | - if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
430 | - return false; |
|
431 | - } |
|
432 | - } |
|
433 | - $parts = explode('/', $path); |
|
434 | - if (count($parts) < 4) { |
|
435 | - return false; |
|
436 | - } |
|
437 | - |
|
438 | - if ($parts[2] == 'files') { |
|
439 | - return true; |
|
440 | - } |
|
441 | - if ($parts[2] == 'files_versions') { |
|
442 | - return true; |
|
443 | - } |
|
444 | - if ($parts[2] == 'files_trashbin') { |
|
445 | - return true; |
|
446 | - } |
|
447 | - |
|
448 | - return false; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * get size of the unencrypted payload per block. |
|
453 | - * ownCloud read/write files with a block size of 8192 byte |
|
454 | - * |
|
455 | - * @param bool $signed |
|
456 | - * @return int |
|
457 | - */ |
|
458 | - public function getUnencryptedBlockSize($signed = false) { |
|
459 | - if ($signed === false) { |
|
460 | - return $this->unencryptedBlockSize; |
|
461 | - } |
|
462 | - |
|
463 | - return $this->unencryptedBlockSizeSigned; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * check if the encryption module is able to read the file, |
|
468 | - * e.g. if all encryption keys exists |
|
469 | - * |
|
470 | - * @param string $path |
|
471 | - * @param string $uid user for whom we want to check if he can read the file |
|
472 | - * @return bool |
|
473 | - * @throws DecryptionFailedException |
|
474 | - */ |
|
475 | - public function isReadable($path, $uid) { |
|
476 | - $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
477 | - if (empty($fileKey)) { |
|
478 | - $owner = $this->util->getOwner($path); |
|
479 | - if ($owner !== $uid) { |
|
480 | - // if it is a shared file we throw a exception with a useful |
|
481 | - // error message because in this case it means that the file was |
|
482 | - // shared with the user at a point where the user didn't had a |
|
483 | - // valid private/public key |
|
484 | - $msg = 'Encryption module "' . $this->getDisplayName() . |
|
485 | - '" is not able to read ' . $path; |
|
486 | - $hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
487 | - $this->logger->warning($msg); |
|
488 | - throw new DecryptionFailedException($msg, $hint); |
|
489 | - } |
|
490 | - return false; |
|
491 | - } |
|
492 | - |
|
493 | - return true; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Initial encryption of all files |
|
498 | - * |
|
499 | - * @param InputInterface $input |
|
500 | - * @param OutputInterface $output write some status information to the terminal during encryption |
|
501 | - */ |
|
502 | - public function encryptAll(InputInterface $input, OutputInterface $output) { |
|
503 | - $this->encryptAll->encryptAll($input, $output); |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * prepare module to perform decrypt all operation |
|
508 | - * |
|
509 | - * @param InputInterface $input |
|
510 | - * @param OutputInterface $output |
|
511 | - * @param string $user |
|
512 | - * @return bool |
|
513 | - */ |
|
514 | - public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
|
515 | - return $this->decryptAll->prepare($input, $output, $user); |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * @param string $path |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - protected function getPathToRealFile($path) { |
|
524 | - $realPath = $path; |
|
525 | - $parts = explode('/', $path); |
|
526 | - if ($parts[2] === 'files_versions') { |
|
527 | - $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3)); |
|
528 | - $length = strrpos($realPath, '.'); |
|
529 | - $realPath = substr($realPath, 0, $length); |
|
530 | - } |
|
531 | - |
|
532 | - return $realPath; |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * remove .part file extension and the ocTransferId from the file to get the |
|
537 | - * original file name |
|
538 | - * |
|
539 | - * @param string $path |
|
540 | - * @return string |
|
541 | - */ |
|
542 | - protected function stripPartFileExtension($path) { |
|
543 | - if (pathinfo($path, PATHINFO_EXTENSION) === 'part') { |
|
544 | - $pos = strrpos($path, '.', -6); |
|
545 | - $path = substr($path, 0, $pos); |
|
546 | - } |
|
547 | - |
|
548 | - return $path; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Check if the module is ready to be used by that specific user. |
|
553 | - * In case a module is not ready - because e.g. key pairs have not been generated |
|
554 | - * upon login this method can return false before any operation starts and might |
|
555 | - * cause issues during operations. |
|
556 | - * |
|
557 | - * @param string $user |
|
558 | - * @return boolean |
|
559 | - * @since 9.1.0 |
|
560 | - */ |
|
561 | - public function isReadyForUser($user) { |
|
562 | - return $this->keyManager->userHasKeys($user); |
|
563 | - } |
|
46 | + const ID = 'OC_DEFAULT_MODULE'; |
|
47 | + const DISPLAY_NAME = 'Default encryption module'; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var Crypt |
|
51 | + */ |
|
52 | + private $crypt; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $cipher; |
|
56 | + |
|
57 | + /** @var string */ |
|
58 | + private $path; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + private $user; |
|
62 | + |
|
63 | + /** @var string */ |
|
64 | + private $fileKey; |
|
65 | + |
|
66 | + /** @var string */ |
|
67 | + private $writeCache; |
|
68 | + |
|
69 | + /** @var KeyManager */ |
|
70 | + private $keyManager; |
|
71 | + |
|
72 | + /** @var array */ |
|
73 | + private $accessList; |
|
74 | + |
|
75 | + /** @var boolean */ |
|
76 | + private $isWriteOperation; |
|
77 | + |
|
78 | + /** @var Util */ |
|
79 | + private $util; |
|
80 | + |
|
81 | + /** @var Session */ |
|
82 | + private $session; |
|
83 | + |
|
84 | + /** @var ILogger */ |
|
85 | + private $logger; |
|
86 | + |
|
87 | + /** @var IL10N */ |
|
88 | + private $l; |
|
89 | + |
|
90 | + /** @var EncryptAll */ |
|
91 | + private $encryptAll; |
|
92 | + |
|
93 | + /** @var bool */ |
|
94 | + private $useMasterPassword; |
|
95 | + |
|
96 | + /** @var DecryptAll */ |
|
97 | + private $decryptAll; |
|
98 | + |
|
99 | + /** @var int unencrypted block size if block contains signature */ |
|
100 | + private $unencryptedBlockSizeSigned = 6072; |
|
101 | + |
|
102 | + /** @var int unencrypted block size */ |
|
103 | + private $unencryptedBlockSize = 6126; |
|
104 | + |
|
105 | + /** @var int Current version of the file */ |
|
106 | + private $version = 0; |
|
107 | + |
|
108 | + /** @var array remember encryption signature version */ |
|
109 | + private static $rememberVersion = []; |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * |
|
114 | + * @param Crypt $crypt |
|
115 | + * @param KeyManager $keyManager |
|
116 | + * @param Util $util |
|
117 | + * @param Session $session |
|
118 | + * @param EncryptAll $encryptAll |
|
119 | + * @param DecryptAll $decryptAll |
|
120 | + * @param ILogger $logger |
|
121 | + * @param IL10N $il10n |
|
122 | + */ |
|
123 | + public function __construct(Crypt $crypt, |
|
124 | + KeyManager $keyManager, |
|
125 | + Util $util, |
|
126 | + Session $session, |
|
127 | + EncryptAll $encryptAll, |
|
128 | + DecryptAll $decryptAll, |
|
129 | + ILogger $logger, |
|
130 | + IL10N $il10n) { |
|
131 | + $this->crypt = $crypt; |
|
132 | + $this->keyManager = $keyManager; |
|
133 | + $this->util = $util; |
|
134 | + $this->session = $session; |
|
135 | + $this->encryptAll = $encryptAll; |
|
136 | + $this->decryptAll = $decryptAll; |
|
137 | + $this->logger = $logger; |
|
138 | + $this->l = $il10n; |
|
139 | + $this->useMasterPassword = $util->isMasterKeyEnabled(); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return string defining the technical unique id |
|
144 | + */ |
|
145 | + public function getId() { |
|
146 | + return self::ID; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
151 | + * |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + public function getDisplayName() { |
|
155 | + return self::DISPLAY_NAME; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * start receiving chunks from a file. This is the place where you can |
|
160 | + * perform some initial step before starting encrypting/decrypting the |
|
161 | + * chunks |
|
162 | + * |
|
163 | + * @param string $path to the file |
|
164 | + * @param string $user who read/write the file |
|
165 | + * @param string $mode php stream open mode |
|
166 | + * @param array $header contains the header data read from the file |
|
167 | + * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
168 | + * |
|
169 | + * @return array $header contain data as key-value pairs which should be |
|
170 | + * written to the header, in case of a write operation |
|
171 | + * or if no additional data is needed return a empty array |
|
172 | + */ |
|
173 | + public function begin($path, $user, $mode, array $header, array $accessList) { |
|
174 | + $this->path = $this->getPathToRealFile($path); |
|
175 | + $this->accessList = $accessList; |
|
176 | + $this->user = $user; |
|
177 | + $this->isWriteOperation = false; |
|
178 | + $this->writeCache = ''; |
|
179 | + |
|
180 | + if ($this->session->decryptAllModeActivated()) { |
|
181 | + $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path); |
|
182 | + $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid()); |
|
183 | + $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
184 | + $shareKey, |
|
185 | + $this->session->getDecryptAllKey()); |
|
186 | + } else { |
|
187 | + $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user); |
|
188 | + } |
|
189 | + |
|
190 | + // always use the version from the original file, also part files |
|
191 | + // need to have a correct version number if they get moved over to the |
|
192 | + // final location |
|
193 | + $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
194 | + |
|
195 | + if ( |
|
196 | + $mode === 'w' |
|
197 | + || $mode === 'w+' |
|
198 | + || $mode === 'wb' |
|
199 | + || $mode === 'wb+' |
|
200 | + ) { |
|
201 | + $this->isWriteOperation = true; |
|
202 | + if (empty($this->fileKey)) { |
|
203 | + $this->fileKey = $this->crypt->generateFileKey(); |
|
204 | + } |
|
205 | + } else { |
|
206 | + // if we read a part file we need to increase the version by 1 |
|
207 | + // because the version number was also increased by writing |
|
208 | + // the part file |
|
209 | + if(Scanner::isPartialFile($path)) { |
|
210 | + $this->version = $this->version + 1; |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + if ($this->isWriteOperation) { |
|
215 | + $this->cipher = $this->crypt->getCipher(); |
|
216 | + } elseif (isset($header['cipher'])) { |
|
217 | + $this->cipher = $header['cipher']; |
|
218 | + } else { |
|
219 | + // if we read a file without a header we fall-back to the legacy cipher |
|
220 | + // which was used in <=oC6 |
|
221 | + $this->cipher = $this->crypt->getLegacyCipher(); |
|
222 | + } |
|
223 | + |
|
224 | + return array('cipher' => $this->cipher, 'signed' => 'true'); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * last chunk received. This is the place where you can perform some final |
|
229 | + * operation and return some remaining data if something is left in your |
|
230 | + * buffer. |
|
231 | + * |
|
232 | + * @param string $path to the file |
|
233 | + * @param int $position |
|
234 | + * @return string remained data which should be written to the file in case |
|
235 | + * of a write operation |
|
236 | + * @throws PublicKeyMissingException |
|
237 | + * @throws \Exception |
|
238 | + * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException |
|
239 | + */ |
|
240 | + public function end($path, $position = 0) { |
|
241 | + $result = ''; |
|
242 | + if ($this->isWriteOperation) { |
|
243 | + $this->keyManager->setVersion($path, $this->version + 1, new View()); |
|
244 | + // in case of a part file we remember the new signature versions |
|
245 | + // the version will be set later on update. |
|
246 | + // This way we make sure that other apps listening to the pre-hooks |
|
247 | + // still get the old version which should be the correct value for them |
|
248 | + if (Scanner::isPartialFile($path)) { |
|
249 | + self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1; |
|
250 | + } |
|
251 | + if (!empty($this->writeCache)) { |
|
252 | + $result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position); |
|
253 | + $this->writeCache = ''; |
|
254 | + } |
|
255 | + $publicKeys = array(); |
|
256 | + if ($this->useMasterPassword === true) { |
|
257 | + $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
258 | + } else { |
|
259 | + foreach ($this->accessList['users'] as $uid) { |
|
260 | + try { |
|
261 | + $publicKeys[$uid] = $this->keyManager->getPublicKey($uid); |
|
262 | + } catch (PublicKeyMissingException $e) { |
|
263 | + $this->logger->warning( |
|
264 | + 'no public key found for user "{uid}", user will not be able to read the file', |
|
265 | + ['app' => 'encryption', 'uid' => $uid] |
|
266 | + ); |
|
267 | + // if the public key of the owner is missing we should fail |
|
268 | + if ($uid === $this->user) { |
|
269 | + throw $e; |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + $publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->user); |
|
276 | + $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys); |
|
277 | + $this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles); |
|
278 | + } |
|
279 | + return $result; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * encrypt data |
|
284 | + * |
|
285 | + * @param string $data you want to encrypt |
|
286 | + * @param int $position |
|
287 | + * @return string encrypted data |
|
288 | + */ |
|
289 | + public function encrypt($data, $position = 0) { |
|
290 | + // If extra data is left over from the last round, make sure it |
|
291 | + // is integrated into the next block |
|
292 | + if ($this->writeCache) { |
|
293 | + |
|
294 | + // Concat writeCache to start of $data |
|
295 | + $data = $this->writeCache . $data; |
|
296 | + |
|
297 | + // Clear the write cache, ready for reuse - it has been |
|
298 | + // flushed and its old contents processed |
|
299 | + $this->writeCache = ''; |
|
300 | + |
|
301 | + } |
|
302 | + |
|
303 | + $encrypted = ''; |
|
304 | + // While there still remains some data to be processed & written |
|
305 | + while (strlen($data) > 0) { |
|
306 | + |
|
307 | + // Remaining length for this iteration, not of the |
|
308 | + // entire file (may be greater than 8192 bytes) |
|
309 | + $remainingLength = strlen($data); |
|
310 | + |
|
311 | + // If data remaining to be written is less than the |
|
312 | + // size of 1 6126 byte block |
|
313 | + if ($remainingLength < $this->unencryptedBlockSizeSigned) { |
|
314 | + |
|
315 | + // Set writeCache to contents of $data |
|
316 | + // The writeCache will be carried over to the |
|
317 | + // next write round, and added to the start of |
|
318 | + // $data to ensure that written blocks are |
|
319 | + // always the correct length. If there is still |
|
320 | + // data in writeCache after the writing round |
|
321 | + // has finished, then the data will be written |
|
322 | + // to disk by $this->flush(). |
|
323 | + $this->writeCache = $data; |
|
324 | + |
|
325 | + // Clear $data ready for next round |
|
326 | + $data = ''; |
|
327 | + |
|
328 | + } else { |
|
329 | + |
|
330 | + // Read the chunk from the start of $data |
|
331 | + $chunk = substr($data, 0, $this->unencryptedBlockSizeSigned); |
|
332 | + |
|
333 | + $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, $position); |
|
334 | + |
|
335 | + // Remove the chunk we just processed from |
|
336 | + // $data, leaving only unprocessed data in $data |
|
337 | + // var, for handling on the next round |
|
338 | + $data = substr($data, $this->unencryptedBlockSizeSigned); |
|
339 | + |
|
340 | + } |
|
341 | + |
|
342 | + } |
|
343 | + |
|
344 | + return $encrypted; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * decrypt data |
|
349 | + * |
|
350 | + * @param string $data you want to decrypt |
|
351 | + * @param int $position |
|
352 | + * @return string decrypted data |
|
353 | + * @throws DecryptionFailedException |
|
354 | + */ |
|
355 | + public function decrypt($data, $position = 0) { |
|
356 | + if (empty($this->fileKey)) { |
|
357 | + $msg = 'Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'; |
|
358 | + $hint = $this->l->t('Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
359 | + $this->logger->error($msg); |
|
360 | + |
|
361 | + throw new DecryptionFailedException($msg, $hint); |
|
362 | + } |
|
363 | + |
|
364 | + return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * update encrypted file, e.g. give additional users access to the file |
|
369 | + * |
|
370 | + * @param string $path path to the file which should be updated |
|
371 | + * @param string $uid of the user who performs the operation |
|
372 | + * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
373 | + * @return boolean |
|
374 | + */ |
|
375 | + public function update($path, $uid, array $accessList) { |
|
376 | + |
|
377 | + if (empty($accessList)) { |
|
378 | + if (isset(self::$rememberVersion[$path])) { |
|
379 | + $this->keyManager->setVersion($path, self::$rememberVersion[$path], new View()); |
|
380 | + unset(self::$rememberVersion[$path]); |
|
381 | + } |
|
382 | + return; |
|
383 | + } |
|
384 | + |
|
385 | + $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
386 | + |
|
387 | + if (!empty($fileKey)) { |
|
388 | + |
|
389 | + $publicKeys = array(); |
|
390 | + if ($this->useMasterPassword === true) { |
|
391 | + $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
392 | + } else { |
|
393 | + foreach ($accessList['users'] as $user) { |
|
394 | + try { |
|
395 | + $publicKeys[$user] = $this->keyManager->getPublicKey($user); |
|
396 | + } catch (PublicKeyMissingException $e) { |
|
397 | + $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage()); |
|
398 | + } |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $uid); |
|
403 | + |
|
404 | + $encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys); |
|
405 | + |
|
406 | + $this->keyManager->deleteAllFileKeys($path); |
|
407 | + |
|
408 | + $this->keyManager->setAllFileKeys($path, $encryptedFileKey); |
|
409 | + |
|
410 | + } else { |
|
411 | + $this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted', |
|
412 | + array('file' => $path, 'app' => 'encryption')); |
|
413 | + |
|
414 | + return false; |
|
415 | + } |
|
416 | + |
|
417 | + return true; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * should the file be encrypted or not |
|
422 | + * |
|
423 | + * @param string $path |
|
424 | + * @return boolean |
|
425 | + */ |
|
426 | + public function shouldEncrypt($path) { |
|
427 | + if ($this->util->shouldEncryptHomeStorage() === false) { |
|
428 | + $storage = $this->util->getStorage($path); |
|
429 | + if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
430 | + return false; |
|
431 | + } |
|
432 | + } |
|
433 | + $parts = explode('/', $path); |
|
434 | + if (count($parts) < 4) { |
|
435 | + return false; |
|
436 | + } |
|
437 | + |
|
438 | + if ($parts[2] == 'files') { |
|
439 | + return true; |
|
440 | + } |
|
441 | + if ($parts[2] == 'files_versions') { |
|
442 | + return true; |
|
443 | + } |
|
444 | + if ($parts[2] == 'files_trashbin') { |
|
445 | + return true; |
|
446 | + } |
|
447 | + |
|
448 | + return false; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * get size of the unencrypted payload per block. |
|
453 | + * ownCloud read/write files with a block size of 8192 byte |
|
454 | + * |
|
455 | + * @param bool $signed |
|
456 | + * @return int |
|
457 | + */ |
|
458 | + public function getUnencryptedBlockSize($signed = false) { |
|
459 | + if ($signed === false) { |
|
460 | + return $this->unencryptedBlockSize; |
|
461 | + } |
|
462 | + |
|
463 | + return $this->unencryptedBlockSizeSigned; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * check if the encryption module is able to read the file, |
|
468 | + * e.g. if all encryption keys exists |
|
469 | + * |
|
470 | + * @param string $path |
|
471 | + * @param string $uid user for whom we want to check if he can read the file |
|
472 | + * @return bool |
|
473 | + * @throws DecryptionFailedException |
|
474 | + */ |
|
475 | + public function isReadable($path, $uid) { |
|
476 | + $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
477 | + if (empty($fileKey)) { |
|
478 | + $owner = $this->util->getOwner($path); |
|
479 | + if ($owner !== $uid) { |
|
480 | + // if it is a shared file we throw a exception with a useful |
|
481 | + // error message because in this case it means that the file was |
|
482 | + // shared with the user at a point where the user didn't had a |
|
483 | + // valid private/public key |
|
484 | + $msg = 'Encryption module "' . $this->getDisplayName() . |
|
485 | + '" is not able to read ' . $path; |
|
486 | + $hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
487 | + $this->logger->warning($msg); |
|
488 | + throw new DecryptionFailedException($msg, $hint); |
|
489 | + } |
|
490 | + return false; |
|
491 | + } |
|
492 | + |
|
493 | + return true; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Initial encryption of all files |
|
498 | + * |
|
499 | + * @param InputInterface $input |
|
500 | + * @param OutputInterface $output write some status information to the terminal during encryption |
|
501 | + */ |
|
502 | + public function encryptAll(InputInterface $input, OutputInterface $output) { |
|
503 | + $this->encryptAll->encryptAll($input, $output); |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * prepare module to perform decrypt all operation |
|
508 | + * |
|
509 | + * @param InputInterface $input |
|
510 | + * @param OutputInterface $output |
|
511 | + * @param string $user |
|
512 | + * @return bool |
|
513 | + */ |
|
514 | + public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
|
515 | + return $this->decryptAll->prepare($input, $output, $user); |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * @param string $path |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + protected function getPathToRealFile($path) { |
|
524 | + $realPath = $path; |
|
525 | + $parts = explode('/', $path); |
|
526 | + if ($parts[2] === 'files_versions') { |
|
527 | + $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3)); |
|
528 | + $length = strrpos($realPath, '.'); |
|
529 | + $realPath = substr($realPath, 0, $length); |
|
530 | + } |
|
531 | + |
|
532 | + return $realPath; |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * remove .part file extension and the ocTransferId from the file to get the |
|
537 | + * original file name |
|
538 | + * |
|
539 | + * @param string $path |
|
540 | + * @return string |
|
541 | + */ |
|
542 | + protected function stripPartFileExtension($path) { |
|
543 | + if (pathinfo($path, PATHINFO_EXTENSION) === 'part') { |
|
544 | + $pos = strrpos($path, '.', -6); |
|
545 | + $path = substr($path, 0, $pos); |
|
546 | + } |
|
547 | + |
|
548 | + return $path; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Check if the module is ready to be used by that specific user. |
|
553 | + * In case a module is not ready - because e.g. key pairs have not been generated |
|
554 | + * upon login this method can return false before any operation starts and might |
|
555 | + * cause issues during operations. |
|
556 | + * |
|
557 | + * @param string $user |
|
558 | + * @return boolean |
|
559 | + * @since 9.1.0 |
|
560 | + */ |
|
561 | + public function isReadyForUser($user) { |
|
562 | + return $this->keyManager->userHasKeys($user); |
|
563 | + } |
|
564 | 564 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | // always use the version from the original file, also part files |
191 | 191 | // need to have a correct version number if they get moved over to the |
192 | 192 | // final location |
193 | - $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
193 | + $this->version = (int) $this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
194 | 194 | |
195 | 195 | if ( |
196 | 196 | $mode === 'w' |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | // if we read a part file we need to increase the version by 1 |
207 | 207 | // because the version number was also increased by writing |
208 | 208 | // the part file |
209 | - if(Scanner::isPartialFile($path)) { |
|
209 | + if (Scanner::isPartialFile($path)) { |
|
210 | 210 | $this->version = $this->version + 1; |
211 | 211 | } |
212 | 212 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | if ($this->writeCache) { |
293 | 293 | |
294 | 294 | // Concat writeCache to start of $data |
295 | - $data = $this->writeCache . $data; |
|
295 | + $data = $this->writeCache.$data; |
|
296 | 296 | |
297 | 297 | // Clear the write cache, ready for reuse - it has been |
298 | 298 | // flushed and its old contents processed |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | try { |
395 | 395 | $publicKeys[$user] = $this->keyManager->getPublicKey($user); |
396 | 396 | } catch (PublicKeyMissingException $e) { |
397 | - $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage()); |
|
397 | + $this->logger->warning('Could not encrypt file for '.$user.': '.$e->getMessage()); |
|
398 | 398 | } |
399 | 399 | } |
400 | 400 | } |
@@ -481,8 +481,8 @@ discard block |
||
481 | 481 | // error message because in this case it means that the file was |
482 | 482 | // shared with the user at a point where the user didn't had a |
483 | 483 | // valid private/public key |
484 | - $msg = 'Encryption module "' . $this->getDisplayName() . |
|
485 | - '" is not able to read ' . $path; |
|
484 | + $msg = 'Encryption module "'.$this->getDisplayName(). |
|
485 | + '" is not able to read '.$path; |
|
486 | 486 | $hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
487 | 487 | $this->logger->warning($msg); |
488 | 488 | throw new DecryptionFailedException($msg, $hint); |
@@ -524,7 +524,7 @@ discard block |
||
524 | 524 | $realPath = $path; |
525 | 525 | $parts = explode('/', $path); |
526 | 526 | if ($parts[2] === 'files_versions') { |
527 | - $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3)); |
|
527 | + $realPath = '/'.$parts[1].'/files/'.implode('/', array_slice($parts, 3)); |
|
528 | 528 | $length = strrpos($realPath, '.'); |
529 | 529 | $realPath = substr($realPath, 0, $length); |
530 | 530 | } |
@@ -32,7 +32,6 @@ |
||
32 | 32 | use OCA\Encryption\Crypto\Crypt; |
33 | 33 | use OCP\Encryption\Keys\IStorage; |
34 | 34 | use OCP\IConfig; |
35 | -use OCP\IDBConnection; |
|
36 | 35 | use OCP\ILogger; |
37 | 36 | use OCP\IUserSession; |
38 | 37 |
@@ -488,7 +488,7 @@ |
||
488 | 488 | |
489 | 489 | |
490 | 490 | /** |
491 | - * @param $path |
|
491 | + * @param string $path |
|
492 | 492 | * @param $uid |
493 | 493 | * @return mixed |
494 | 494 | */ |
@@ -40,658 +40,658 @@ |
||
40 | 40 | |
41 | 41 | class KeyManager { |
42 | 42 | |
43 | - /** |
|
44 | - * @var Session |
|
45 | - */ |
|
46 | - protected $session; |
|
47 | - /** |
|
48 | - * @var IStorage |
|
49 | - */ |
|
50 | - private $keyStorage; |
|
51 | - /** |
|
52 | - * @var Crypt |
|
53 | - */ |
|
54 | - private $crypt; |
|
55 | - /** |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - private $recoveryKeyId; |
|
59 | - /** |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - private $publicShareKeyId; |
|
63 | - /** |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - private $masterKeyId; |
|
67 | - /** |
|
68 | - * @var string UserID |
|
69 | - */ |
|
70 | - private $keyId; |
|
71 | - /** |
|
72 | - * @var string |
|
73 | - */ |
|
74 | - private $publicKeyId = 'publicKey'; |
|
75 | - /** |
|
76 | - * @var string |
|
77 | - */ |
|
78 | - private $privateKeyId = 'privateKey'; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - private $shareKeyId = 'shareKey'; |
|
84 | - |
|
85 | - /** |
|
86 | - * @var string |
|
87 | - */ |
|
88 | - private $fileKeyId = 'fileKey'; |
|
89 | - /** |
|
90 | - * @var IConfig |
|
91 | - */ |
|
92 | - private $config; |
|
93 | - /** |
|
94 | - * @var ILogger |
|
95 | - */ |
|
96 | - private $log; |
|
97 | - /** |
|
98 | - * @var Util |
|
99 | - */ |
|
100 | - private $util; |
|
101 | - |
|
102 | - /** |
|
103 | - * @param IStorage $keyStorage |
|
104 | - * @param Crypt $crypt |
|
105 | - * @param IConfig $config |
|
106 | - * @param IUserSession $userSession |
|
107 | - * @param Session $session |
|
108 | - * @param ILogger $log |
|
109 | - * @param Util $util |
|
110 | - */ |
|
111 | - public function __construct( |
|
112 | - IStorage $keyStorage, |
|
113 | - Crypt $crypt, |
|
114 | - IConfig $config, |
|
115 | - IUserSession $userSession, |
|
116 | - Session $session, |
|
117 | - ILogger $log, |
|
118 | - Util $util |
|
119 | - ) { |
|
120 | - |
|
121 | - $this->util = $util; |
|
122 | - $this->session = $session; |
|
123 | - $this->keyStorage = $keyStorage; |
|
124 | - $this->crypt = $crypt; |
|
125 | - $this->config = $config; |
|
126 | - $this->log = $log; |
|
127 | - |
|
128 | - $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
129 | - 'recoveryKeyId'); |
|
130 | - if (empty($this->recoveryKeyId)) { |
|
131 | - $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
132 | - $this->config->setAppValue('encryption', |
|
133 | - 'recoveryKeyId', |
|
134 | - $this->recoveryKeyId); |
|
135 | - } |
|
136 | - |
|
137 | - $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
138 | - 'publicShareKeyId'); |
|
139 | - if (empty($this->publicShareKeyId)) { |
|
140 | - $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
141 | - $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
|
142 | - } |
|
143 | - |
|
144 | - $this->masterKeyId = $this->config->getAppValue('encryption', |
|
145 | - 'masterKeyId'); |
|
146 | - if (empty($this->masterKeyId)) { |
|
147 | - $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
148 | - $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
|
149 | - } |
|
150 | - |
|
151 | - $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
152 | - $this->log = $log; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * check if key pair for public link shares exists, if not we create one |
|
157 | - */ |
|
158 | - public function validateShareKey() { |
|
159 | - $shareKey = $this->getPublicShareKey(); |
|
160 | - if (empty($shareKey)) { |
|
161 | - $keyPair = $this->crypt->createKeyPair(); |
|
162 | - |
|
163 | - // Save public key |
|
164 | - $this->keyStorage->setSystemUserKey( |
|
165 | - $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'], |
|
166 | - Encryption::ID); |
|
167 | - |
|
168 | - // Encrypt private key empty passphrase |
|
169 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
|
170 | - $header = $this->crypt->generateHeader(); |
|
171 | - $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * check if a key pair for the master key exists, if not we create one |
|
177 | - */ |
|
178 | - public function validateMasterKey() { |
|
179 | - $masterKey = $this->getPublicMasterKey(); |
|
180 | - if (empty($masterKey)) { |
|
181 | - $keyPair = $this->crypt->createKeyPair(); |
|
182 | - |
|
183 | - // Save public key |
|
184 | - $this->keyStorage->setSystemUserKey( |
|
185 | - $this->masterKeyId . '.publicKey', $keyPair['publicKey'], |
|
186 | - Encryption::ID); |
|
187 | - |
|
188 | - // Encrypt private key with system password |
|
189 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
|
190 | - $header = $this->crypt->generateHeader(); |
|
191 | - $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @return bool |
|
197 | - */ |
|
198 | - public function recoveryKeyExists() { |
|
199 | - $key = $this->getRecoveryKey(); |
|
200 | - return (!empty($key)); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * get recovery key |
|
205 | - * |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - public function getRecoveryKey() { |
|
209 | - return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * get recovery key ID |
|
214 | - * |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - public function getRecoveryKeyId() { |
|
218 | - return $this->recoveryKeyId; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * @param string $password |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function checkRecoveryPassword($password) { |
|
226 | - $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID); |
|
227 | - $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
|
228 | - |
|
229 | - if ($decryptedRecoveryKey) { |
|
230 | - return true; |
|
231 | - } |
|
232 | - return false; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param string $uid |
|
237 | - * @param string $password |
|
238 | - * @param string $keyPair |
|
239 | - * @return bool |
|
240 | - */ |
|
241 | - public function storeKeyPair($uid, $password, $keyPair) { |
|
242 | - // Save Public Key |
|
243 | - $this->setPublicKey($uid, $keyPair['publicKey']); |
|
244 | - |
|
245 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid); |
|
246 | - |
|
247 | - $header = $this->crypt->generateHeader(); |
|
248 | - |
|
249 | - if ($encryptedKey) { |
|
250 | - $this->setPrivateKey($uid, $header . $encryptedKey); |
|
251 | - return true; |
|
252 | - } |
|
253 | - return false; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @param string $password |
|
258 | - * @param array $keyPair |
|
259 | - * @return bool |
|
260 | - */ |
|
261 | - public function setRecoveryKey($password, $keyPair) { |
|
262 | - // Save Public Key |
|
263 | - $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
|
264 | - '.publicKey', |
|
265 | - $keyPair['publicKey'], |
|
266 | - Encryption::ID); |
|
267 | - |
|
268 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password); |
|
269 | - $header = $this->crypt->generateHeader(); |
|
270 | - |
|
271 | - if ($encryptedKey) { |
|
272 | - $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
273 | - return true; |
|
274 | - } |
|
275 | - return false; |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param $userId |
|
280 | - * @param $key |
|
281 | - * @return bool |
|
282 | - */ |
|
283 | - public function setPublicKey($userId, $key) { |
|
284 | - return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID); |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * @param $userId |
|
289 | - * @param string $key |
|
290 | - * @return bool |
|
291 | - */ |
|
292 | - public function setPrivateKey($userId, $key) { |
|
293 | - return $this->keyStorage->setUserKey($userId, |
|
294 | - $this->privateKeyId, |
|
295 | - $key, |
|
296 | - Encryption::ID); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * write file key to key storage |
|
301 | - * |
|
302 | - * @param string $path |
|
303 | - * @param string $key |
|
304 | - * @return boolean |
|
305 | - */ |
|
306 | - public function setFileKey($path, $key) { |
|
307 | - return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * set all file keys (the file key and the corresponding share keys) |
|
312 | - * |
|
313 | - * @param string $path |
|
314 | - * @param array $keys |
|
315 | - */ |
|
316 | - public function setAllFileKeys($path, $keys) { |
|
317 | - $this->setFileKey($path, $keys['data']); |
|
318 | - foreach ($keys['keys'] as $uid => $keyFile) { |
|
319 | - $this->setShareKey($path, $uid, $keyFile); |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * write share key to the key storage |
|
325 | - * |
|
326 | - * @param string $path |
|
327 | - * @param string $uid |
|
328 | - * @param string $key |
|
329 | - * @return boolean |
|
330 | - */ |
|
331 | - public function setShareKey($path, $uid, $key) { |
|
332 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
333 | - return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Decrypt private key and store it |
|
338 | - * |
|
339 | - * @param string $uid userid |
|
340 | - * @param string $passPhrase users password |
|
341 | - * @return boolean |
|
342 | - */ |
|
343 | - public function init($uid, $passPhrase) { |
|
344 | - |
|
345 | - $this->session->setStatus(Session::INIT_EXECUTED); |
|
346 | - |
|
347 | - |
|
348 | - try { |
|
349 | - if($this->util->isMasterKeyEnabled()) { |
|
350 | - $uid = $this->getMasterKeyId(); |
|
351 | - $passPhrase = $this->getMasterKeyPassword(); |
|
352 | - $privateKey = $this->getSystemPrivateKey($uid); |
|
353 | - } else { |
|
354 | - $privateKey = $this->getPrivateKey($uid); |
|
355 | - } |
|
356 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid); |
|
357 | - } catch (PrivateKeyMissingException $e) { |
|
358 | - return false; |
|
359 | - } catch (DecryptionFailedException $e) { |
|
360 | - return false; |
|
361 | - } catch (\Exception $e) { |
|
362 | - $this->log->warning( |
|
363 | - 'Could not decrypt the private key from user "' . $uid . '"" during login. ' . |
|
364 | - 'Assume password change on the user back-end. Error message: ' |
|
365 | - . $e->getMessage() |
|
366 | - ); |
|
367 | - return false; |
|
368 | - } |
|
369 | - |
|
370 | - if ($privateKey) { |
|
371 | - $this->session->setPrivateKey($privateKey); |
|
372 | - $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
373 | - return true; |
|
374 | - } |
|
375 | - |
|
376 | - return false; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * @param $userId |
|
381 | - * @return string |
|
382 | - * @throws PrivateKeyMissingException |
|
383 | - */ |
|
384 | - public function getPrivateKey($userId) { |
|
385 | - $privateKey = $this->keyStorage->getUserKey($userId, |
|
386 | - $this->privateKeyId, Encryption::ID); |
|
387 | - |
|
388 | - if (strlen($privateKey) !== 0) { |
|
389 | - return $privateKey; |
|
390 | - } |
|
391 | - throw new PrivateKeyMissingException($userId); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * @param string $path |
|
396 | - * @param $uid |
|
397 | - * @return string |
|
398 | - */ |
|
399 | - public function getFileKey($path, $uid) { |
|
400 | - $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID); |
|
401 | - |
|
402 | - if (is_null($uid)) { |
|
403 | - $uid = $this->getPublicShareKeyId(); |
|
404 | - $shareKey = $this->getShareKey($path, $uid); |
|
405 | - $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID); |
|
406 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
|
407 | - } else { |
|
408 | - |
|
409 | - if ($this->util->isMasterKeyEnabled()) { |
|
410 | - $uid = $this->getMasterKeyId(); |
|
411 | - } |
|
412 | - |
|
413 | - $shareKey = $this->getShareKey($path, $uid); |
|
414 | - $privateKey = $this->session->getPrivateKey(); |
|
415 | - } |
|
416 | - |
|
417 | - if ($encryptedFileKey && $shareKey && $privateKey) { |
|
418 | - return $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
419 | - $shareKey, |
|
420 | - $privateKey); |
|
421 | - } |
|
422 | - |
|
423 | - return ''; |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Get the current version of a file |
|
428 | - * |
|
429 | - * @param string $path |
|
430 | - * @param View $view |
|
431 | - * @return int |
|
432 | - */ |
|
433 | - public function getVersion($path, View $view) { |
|
434 | - $fileInfo = $view->getFileInfo($path); |
|
435 | - if($fileInfo === false) { |
|
436 | - return 0; |
|
437 | - } |
|
438 | - return $fileInfo->getEncryptedVersion(); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Set the current version of a file |
|
443 | - * |
|
444 | - * @param string $path |
|
445 | - * @param int $version |
|
446 | - * @param View $view |
|
447 | - */ |
|
448 | - public function setVersion($path, $version, View $view) { |
|
449 | - $fileInfo= $view->getFileInfo($path); |
|
450 | - |
|
451 | - if($fileInfo !== false) { |
|
452 | - $cache = $fileInfo->getStorage()->getCache(); |
|
453 | - $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
|
454 | - } |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * get the encrypted file key |
|
459 | - * |
|
460 | - * @param string $path |
|
461 | - * @return string |
|
462 | - */ |
|
463 | - public function getEncryptedFileKey($path) { |
|
464 | - $encryptedFileKey = $this->keyStorage->getFileKey($path, |
|
465 | - $this->fileKeyId, Encryption::ID); |
|
466 | - |
|
467 | - return $encryptedFileKey; |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * delete share key |
|
472 | - * |
|
473 | - * @param string $path |
|
474 | - * @param string $keyId |
|
475 | - * @return boolean |
|
476 | - */ |
|
477 | - public function deleteShareKey($path, $keyId) { |
|
478 | - return $this->keyStorage->deleteFileKey( |
|
479 | - $path, |
|
480 | - $keyId . '.' . $this->shareKeyId, |
|
481 | - Encryption::ID); |
|
482 | - } |
|
483 | - |
|
484 | - |
|
485 | - /** |
|
486 | - * @param $path |
|
487 | - * @param $uid |
|
488 | - * @return mixed |
|
489 | - */ |
|
490 | - public function getShareKey($path, $uid) { |
|
491 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
492 | - return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * check if user has a private and a public key |
|
497 | - * |
|
498 | - * @param string $userId |
|
499 | - * @return bool |
|
500 | - * @throws PrivateKeyMissingException |
|
501 | - * @throws PublicKeyMissingException |
|
502 | - */ |
|
503 | - public function userHasKeys($userId) { |
|
504 | - $privateKey = $publicKey = true; |
|
505 | - $exception = null; |
|
506 | - |
|
507 | - try { |
|
508 | - $this->getPrivateKey($userId); |
|
509 | - } catch (PrivateKeyMissingException $e) { |
|
510 | - $privateKey = false; |
|
511 | - $exception = $e; |
|
512 | - } |
|
513 | - try { |
|
514 | - $this->getPublicKey($userId); |
|
515 | - } catch (PublicKeyMissingException $e) { |
|
516 | - $publicKey = false; |
|
517 | - $exception = $e; |
|
518 | - } |
|
519 | - |
|
520 | - if ($privateKey && $publicKey) { |
|
521 | - return true; |
|
522 | - } elseif (!$privateKey && !$publicKey) { |
|
523 | - return false; |
|
524 | - } else { |
|
525 | - throw $exception; |
|
526 | - } |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * @param $userId |
|
531 | - * @return mixed |
|
532 | - * @throws PublicKeyMissingException |
|
533 | - */ |
|
534 | - public function getPublicKey($userId) { |
|
535 | - $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID); |
|
536 | - |
|
537 | - if (strlen($publicKey) !== 0) { |
|
538 | - return $publicKey; |
|
539 | - } |
|
540 | - throw new PublicKeyMissingException($userId); |
|
541 | - } |
|
542 | - |
|
543 | - public function getPublicShareKeyId() { |
|
544 | - return $this->publicShareKeyId; |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * get public key for public link shares |
|
549 | - * |
|
550 | - * @return string |
|
551 | - */ |
|
552 | - public function getPublicShareKey() { |
|
553 | - return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID); |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * @param string $purpose |
|
558 | - * @param bool $timestamp |
|
559 | - * @param bool $includeUserKeys |
|
560 | - */ |
|
561 | - public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) { |
|
43 | + /** |
|
44 | + * @var Session |
|
45 | + */ |
|
46 | + protected $session; |
|
47 | + /** |
|
48 | + * @var IStorage |
|
49 | + */ |
|
50 | + private $keyStorage; |
|
51 | + /** |
|
52 | + * @var Crypt |
|
53 | + */ |
|
54 | + private $crypt; |
|
55 | + /** |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + private $recoveryKeyId; |
|
59 | + /** |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + private $publicShareKeyId; |
|
63 | + /** |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + private $masterKeyId; |
|
67 | + /** |
|
68 | + * @var string UserID |
|
69 | + */ |
|
70 | + private $keyId; |
|
71 | + /** |
|
72 | + * @var string |
|
73 | + */ |
|
74 | + private $publicKeyId = 'publicKey'; |
|
75 | + /** |
|
76 | + * @var string |
|
77 | + */ |
|
78 | + private $privateKeyId = 'privateKey'; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + private $shareKeyId = 'shareKey'; |
|
84 | + |
|
85 | + /** |
|
86 | + * @var string |
|
87 | + */ |
|
88 | + private $fileKeyId = 'fileKey'; |
|
89 | + /** |
|
90 | + * @var IConfig |
|
91 | + */ |
|
92 | + private $config; |
|
93 | + /** |
|
94 | + * @var ILogger |
|
95 | + */ |
|
96 | + private $log; |
|
97 | + /** |
|
98 | + * @var Util |
|
99 | + */ |
|
100 | + private $util; |
|
101 | + |
|
102 | + /** |
|
103 | + * @param IStorage $keyStorage |
|
104 | + * @param Crypt $crypt |
|
105 | + * @param IConfig $config |
|
106 | + * @param IUserSession $userSession |
|
107 | + * @param Session $session |
|
108 | + * @param ILogger $log |
|
109 | + * @param Util $util |
|
110 | + */ |
|
111 | + public function __construct( |
|
112 | + IStorage $keyStorage, |
|
113 | + Crypt $crypt, |
|
114 | + IConfig $config, |
|
115 | + IUserSession $userSession, |
|
116 | + Session $session, |
|
117 | + ILogger $log, |
|
118 | + Util $util |
|
119 | + ) { |
|
120 | + |
|
121 | + $this->util = $util; |
|
122 | + $this->session = $session; |
|
123 | + $this->keyStorage = $keyStorage; |
|
124 | + $this->crypt = $crypt; |
|
125 | + $this->config = $config; |
|
126 | + $this->log = $log; |
|
127 | + |
|
128 | + $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
129 | + 'recoveryKeyId'); |
|
130 | + if (empty($this->recoveryKeyId)) { |
|
131 | + $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
132 | + $this->config->setAppValue('encryption', |
|
133 | + 'recoveryKeyId', |
|
134 | + $this->recoveryKeyId); |
|
135 | + } |
|
136 | + |
|
137 | + $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
138 | + 'publicShareKeyId'); |
|
139 | + if (empty($this->publicShareKeyId)) { |
|
140 | + $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
141 | + $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
|
142 | + } |
|
143 | + |
|
144 | + $this->masterKeyId = $this->config->getAppValue('encryption', |
|
145 | + 'masterKeyId'); |
|
146 | + if (empty($this->masterKeyId)) { |
|
147 | + $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
148 | + $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
|
149 | + } |
|
150 | + |
|
151 | + $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
152 | + $this->log = $log; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * check if key pair for public link shares exists, if not we create one |
|
157 | + */ |
|
158 | + public function validateShareKey() { |
|
159 | + $shareKey = $this->getPublicShareKey(); |
|
160 | + if (empty($shareKey)) { |
|
161 | + $keyPair = $this->crypt->createKeyPair(); |
|
162 | + |
|
163 | + // Save public key |
|
164 | + $this->keyStorage->setSystemUserKey( |
|
165 | + $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'], |
|
166 | + Encryption::ID); |
|
167 | + |
|
168 | + // Encrypt private key empty passphrase |
|
169 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
|
170 | + $header = $this->crypt->generateHeader(); |
|
171 | + $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * check if a key pair for the master key exists, if not we create one |
|
177 | + */ |
|
178 | + public function validateMasterKey() { |
|
179 | + $masterKey = $this->getPublicMasterKey(); |
|
180 | + if (empty($masterKey)) { |
|
181 | + $keyPair = $this->crypt->createKeyPair(); |
|
182 | + |
|
183 | + // Save public key |
|
184 | + $this->keyStorage->setSystemUserKey( |
|
185 | + $this->masterKeyId . '.publicKey', $keyPair['publicKey'], |
|
186 | + Encryption::ID); |
|
187 | + |
|
188 | + // Encrypt private key with system password |
|
189 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
|
190 | + $header = $this->crypt->generateHeader(); |
|
191 | + $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @return bool |
|
197 | + */ |
|
198 | + public function recoveryKeyExists() { |
|
199 | + $key = $this->getRecoveryKey(); |
|
200 | + return (!empty($key)); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * get recovery key |
|
205 | + * |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + public function getRecoveryKey() { |
|
209 | + return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * get recovery key ID |
|
214 | + * |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + public function getRecoveryKeyId() { |
|
218 | + return $this->recoveryKeyId; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * @param string $password |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function checkRecoveryPassword($password) { |
|
226 | + $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID); |
|
227 | + $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
|
228 | + |
|
229 | + if ($decryptedRecoveryKey) { |
|
230 | + return true; |
|
231 | + } |
|
232 | + return false; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param string $uid |
|
237 | + * @param string $password |
|
238 | + * @param string $keyPair |
|
239 | + * @return bool |
|
240 | + */ |
|
241 | + public function storeKeyPair($uid, $password, $keyPair) { |
|
242 | + // Save Public Key |
|
243 | + $this->setPublicKey($uid, $keyPair['publicKey']); |
|
244 | + |
|
245 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid); |
|
246 | + |
|
247 | + $header = $this->crypt->generateHeader(); |
|
248 | + |
|
249 | + if ($encryptedKey) { |
|
250 | + $this->setPrivateKey($uid, $header . $encryptedKey); |
|
251 | + return true; |
|
252 | + } |
|
253 | + return false; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @param string $password |
|
258 | + * @param array $keyPair |
|
259 | + * @return bool |
|
260 | + */ |
|
261 | + public function setRecoveryKey($password, $keyPair) { |
|
262 | + // Save Public Key |
|
263 | + $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
|
264 | + '.publicKey', |
|
265 | + $keyPair['publicKey'], |
|
266 | + Encryption::ID); |
|
267 | + |
|
268 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password); |
|
269 | + $header = $this->crypt->generateHeader(); |
|
270 | + |
|
271 | + if ($encryptedKey) { |
|
272 | + $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
273 | + return true; |
|
274 | + } |
|
275 | + return false; |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param $userId |
|
280 | + * @param $key |
|
281 | + * @return bool |
|
282 | + */ |
|
283 | + public function setPublicKey($userId, $key) { |
|
284 | + return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID); |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * @param $userId |
|
289 | + * @param string $key |
|
290 | + * @return bool |
|
291 | + */ |
|
292 | + public function setPrivateKey($userId, $key) { |
|
293 | + return $this->keyStorage->setUserKey($userId, |
|
294 | + $this->privateKeyId, |
|
295 | + $key, |
|
296 | + Encryption::ID); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * write file key to key storage |
|
301 | + * |
|
302 | + * @param string $path |
|
303 | + * @param string $key |
|
304 | + * @return boolean |
|
305 | + */ |
|
306 | + public function setFileKey($path, $key) { |
|
307 | + return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * set all file keys (the file key and the corresponding share keys) |
|
312 | + * |
|
313 | + * @param string $path |
|
314 | + * @param array $keys |
|
315 | + */ |
|
316 | + public function setAllFileKeys($path, $keys) { |
|
317 | + $this->setFileKey($path, $keys['data']); |
|
318 | + foreach ($keys['keys'] as $uid => $keyFile) { |
|
319 | + $this->setShareKey($path, $uid, $keyFile); |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * write share key to the key storage |
|
325 | + * |
|
326 | + * @param string $path |
|
327 | + * @param string $uid |
|
328 | + * @param string $key |
|
329 | + * @return boolean |
|
330 | + */ |
|
331 | + public function setShareKey($path, $uid, $key) { |
|
332 | + $keyId = $uid . '.' . $this->shareKeyId; |
|
333 | + return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Decrypt private key and store it |
|
338 | + * |
|
339 | + * @param string $uid userid |
|
340 | + * @param string $passPhrase users password |
|
341 | + * @return boolean |
|
342 | + */ |
|
343 | + public function init($uid, $passPhrase) { |
|
344 | + |
|
345 | + $this->session->setStatus(Session::INIT_EXECUTED); |
|
346 | + |
|
347 | + |
|
348 | + try { |
|
349 | + if($this->util->isMasterKeyEnabled()) { |
|
350 | + $uid = $this->getMasterKeyId(); |
|
351 | + $passPhrase = $this->getMasterKeyPassword(); |
|
352 | + $privateKey = $this->getSystemPrivateKey($uid); |
|
353 | + } else { |
|
354 | + $privateKey = $this->getPrivateKey($uid); |
|
355 | + } |
|
356 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid); |
|
357 | + } catch (PrivateKeyMissingException $e) { |
|
358 | + return false; |
|
359 | + } catch (DecryptionFailedException $e) { |
|
360 | + return false; |
|
361 | + } catch (\Exception $e) { |
|
362 | + $this->log->warning( |
|
363 | + 'Could not decrypt the private key from user "' . $uid . '"" during login. ' . |
|
364 | + 'Assume password change on the user back-end. Error message: ' |
|
365 | + . $e->getMessage() |
|
366 | + ); |
|
367 | + return false; |
|
368 | + } |
|
369 | + |
|
370 | + if ($privateKey) { |
|
371 | + $this->session->setPrivateKey($privateKey); |
|
372 | + $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
373 | + return true; |
|
374 | + } |
|
375 | + |
|
376 | + return false; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * @param $userId |
|
381 | + * @return string |
|
382 | + * @throws PrivateKeyMissingException |
|
383 | + */ |
|
384 | + public function getPrivateKey($userId) { |
|
385 | + $privateKey = $this->keyStorage->getUserKey($userId, |
|
386 | + $this->privateKeyId, Encryption::ID); |
|
387 | + |
|
388 | + if (strlen($privateKey) !== 0) { |
|
389 | + return $privateKey; |
|
390 | + } |
|
391 | + throw new PrivateKeyMissingException($userId); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * @param string $path |
|
396 | + * @param $uid |
|
397 | + * @return string |
|
398 | + */ |
|
399 | + public function getFileKey($path, $uid) { |
|
400 | + $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID); |
|
401 | + |
|
402 | + if (is_null($uid)) { |
|
403 | + $uid = $this->getPublicShareKeyId(); |
|
404 | + $shareKey = $this->getShareKey($path, $uid); |
|
405 | + $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID); |
|
406 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
|
407 | + } else { |
|
408 | + |
|
409 | + if ($this->util->isMasterKeyEnabled()) { |
|
410 | + $uid = $this->getMasterKeyId(); |
|
411 | + } |
|
412 | + |
|
413 | + $shareKey = $this->getShareKey($path, $uid); |
|
414 | + $privateKey = $this->session->getPrivateKey(); |
|
415 | + } |
|
416 | + |
|
417 | + if ($encryptedFileKey && $shareKey && $privateKey) { |
|
418 | + return $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
419 | + $shareKey, |
|
420 | + $privateKey); |
|
421 | + } |
|
422 | + |
|
423 | + return ''; |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Get the current version of a file |
|
428 | + * |
|
429 | + * @param string $path |
|
430 | + * @param View $view |
|
431 | + * @return int |
|
432 | + */ |
|
433 | + public function getVersion($path, View $view) { |
|
434 | + $fileInfo = $view->getFileInfo($path); |
|
435 | + if($fileInfo === false) { |
|
436 | + return 0; |
|
437 | + } |
|
438 | + return $fileInfo->getEncryptedVersion(); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Set the current version of a file |
|
443 | + * |
|
444 | + * @param string $path |
|
445 | + * @param int $version |
|
446 | + * @param View $view |
|
447 | + */ |
|
448 | + public function setVersion($path, $version, View $view) { |
|
449 | + $fileInfo= $view->getFileInfo($path); |
|
450 | + |
|
451 | + if($fileInfo !== false) { |
|
452 | + $cache = $fileInfo->getStorage()->getCache(); |
|
453 | + $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
|
454 | + } |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * get the encrypted file key |
|
459 | + * |
|
460 | + * @param string $path |
|
461 | + * @return string |
|
462 | + */ |
|
463 | + public function getEncryptedFileKey($path) { |
|
464 | + $encryptedFileKey = $this->keyStorage->getFileKey($path, |
|
465 | + $this->fileKeyId, Encryption::ID); |
|
466 | + |
|
467 | + return $encryptedFileKey; |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * delete share key |
|
472 | + * |
|
473 | + * @param string $path |
|
474 | + * @param string $keyId |
|
475 | + * @return boolean |
|
476 | + */ |
|
477 | + public function deleteShareKey($path, $keyId) { |
|
478 | + return $this->keyStorage->deleteFileKey( |
|
479 | + $path, |
|
480 | + $keyId . '.' . $this->shareKeyId, |
|
481 | + Encryption::ID); |
|
482 | + } |
|
483 | + |
|
484 | + |
|
485 | + /** |
|
486 | + * @param $path |
|
487 | + * @param $uid |
|
488 | + * @return mixed |
|
489 | + */ |
|
490 | + public function getShareKey($path, $uid) { |
|
491 | + $keyId = $uid . '.' . $this->shareKeyId; |
|
492 | + return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * check if user has a private and a public key |
|
497 | + * |
|
498 | + * @param string $userId |
|
499 | + * @return bool |
|
500 | + * @throws PrivateKeyMissingException |
|
501 | + * @throws PublicKeyMissingException |
|
502 | + */ |
|
503 | + public function userHasKeys($userId) { |
|
504 | + $privateKey = $publicKey = true; |
|
505 | + $exception = null; |
|
506 | + |
|
507 | + try { |
|
508 | + $this->getPrivateKey($userId); |
|
509 | + } catch (PrivateKeyMissingException $e) { |
|
510 | + $privateKey = false; |
|
511 | + $exception = $e; |
|
512 | + } |
|
513 | + try { |
|
514 | + $this->getPublicKey($userId); |
|
515 | + } catch (PublicKeyMissingException $e) { |
|
516 | + $publicKey = false; |
|
517 | + $exception = $e; |
|
518 | + } |
|
519 | + |
|
520 | + if ($privateKey && $publicKey) { |
|
521 | + return true; |
|
522 | + } elseif (!$privateKey && !$publicKey) { |
|
523 | + return false; |
|
524 | + } else { |
|
525 | + throw $exception; |
|
526 | + } |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * @param $userId |
|
531 | + * @return mixed |
|
532 | + * @throws PublicKeyMissingException |
|
533 | + */ |
|
534 | + public function getPublicKey($userId) { |
|
535 | + $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID); |
|
536 | + |
|
537 | + if (strlen($publicKey) !== 0) { |
|
538 | + return $publicKey; |
|
539 | + } |
|
540 | + throw new PublicKeyMissingException($userId); |
|
541 | + } |
|
542 | + |
|
543 | + public function getPublicShareKeyId() { |
|
544 | + return $this->publicShareKeyId; |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * get public key for public link shares |
|
549 | + * |
|
550 | + * @return string |
|
551 | + */ |
|
552 | + public function getPublicShareKey() { |
|
553 | + return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID); |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * @param string $purpose |
|
558 | + * @param bool $timestamp |
|
559 | + * @param bool $includeUserKeys |
|
560 | + */ |
|
561 | + public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) { |
|
562 | 562 | // $backupDir = $this->keyStorage->; |
563 | - } |
|
564 | - |
|
565 | - /** |
|
566 | - * @param string $uid |
|
567 | - */ |
|
568 | - public function replaceUserKeys($uid) { |
|
569 | - $this->backupAllKeys('password_reset'); |
|
570 | - $this->deletePublicKey($uid); |
|
571 | - $this->deletePrivateKey($uid); |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * @param $uid |
|
576 | - * @return bool |
|
577 | - */ |
|
578 | - public function deletePublicKey($uid) { |
|
579 | - return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID); |
|
580 | - } |
|
581 | - |
|
582 | - /** |
|
583 | - * @param string $uid |
|
584 | - * @return bool |
|
585 | - */ |
|
586 | - private function deletePrivateKey($uid) { |
|
587 | - return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID); |
|
588 | - } |
|
589 | - |
|
590 | - /** |
|
591 | - * @param string $path |
|
592 | - * @return bool |
|
593 | - */ |
|
594 | - public function deleteAllFileKeys($path) { |
|
595 | - return $this->keyStorage->deleteAllFileKeys($path); |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * @param array $userIds |
|
600 | - * @return array |
|
601 | - * @throws PublicKeyMissingException |
|
602 | - */ |
|
603 | - public function getPublicKeys(array $userIds) { |
|
604 | - $keys = []; |
|
605 | - |
|
606 | - foreach ($userIds as $userId) { |
|
607 | - try { |
|
608 | - $keys[$userId] = $this->getPublicKey($userId); |
|
609 | - } catch (PublicKeyMissingException $e) { |
|
610 | - continue; |
|
611 | - } |
|
612 | - } |
|
613 | - |
|
614 | - return $keys; |
|
615 | - |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * @param string $keyId |
|
620 | - * @return string returns openssl key |
|
621 | - */ |
|
622 | - public function getSystemPrivateKey($keyId) { |
|
623 | - return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
624 | - } |
|
625 | - |
|
626 | - /** |
|
627 | - * @param string $keyId |
|
628 | - * @param string $key |
|
629 | - * @return string returns openssl key |
|
630 | - */ |
|
631 | - public function setSystemPrivateKey($keyId, $key) { |
|
632 | - return $this->keyStorage->setSystemUserKey( |
|
633 | - $keyId . '.' . $this->privateKeyId, |
|
634 | - $key, |
|
635 | - Encryption::ID); |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * add system keys such as the public share key and the recovery key |
|
640 | - * |
|
641 | - * @param array $accessList |
|
642 | - * @param array $publicKeys |
|
643 | - * @param string $uid |
|
644 | - * @return array |
|
645 | - * @throws PublicKeyMissingException |
|
646 | - */ |
|
647 | - public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
648 | - if (!empty($accessList['public'])) { |
|
649 | - $publicShareKey = $this->getPublicShareKey(); |
|
650 | - if (empty($publicShareKey)) { |
|
651 | - throw new PublicKeyMissingException($this->getPublicShareKeyId()); |
|
652 | - } |
|
653 | - $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey; |
|
654 | - } |
|
655 | - |
|
656 | - if ($this->recoveryKeyExists() && |
|
657 | - $this->util->isRecoveryEnabledForUser($uid)) { |
|
658 | - |
|
659 | - $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey(); |
|
660 | - } |
|
661 | - |
|
662 | - return $publicKeys; |
|
663 | - } |
|
664 | - |
|
665 | - /** |
|
666 | - * get master key password |
|
667 | - * |
|
668 | - * @return string |
|
669 | - * @throws \Exception |
|
670 | - */ |
|
671 | - public function getMasterKeyPassword() { |
|
672 | - $password = $this->config->getSystemValue('secret'); |
|
673 | - if (empty($password)){ |
|
674 | - throw new \Exception('Can not get secret from instance'); |
|
675 | - } |
|
676 | - |
|
677 | - return $password; |
|
678 | - } |
|
679 | - |
|
680 | - /** |
|
681 | - * return master key id |
|
682 | - * |
|
683 | - * @return string |
|
684 | - */ |
|
685 | - public function getMasterKeyId() { |
|
686 | - return $this->masterKeyId; |
|
687 | - } |
|
688 | - |
|
689 | - /** |
|
690 | - * get public master key |
|
691 | - * |
|
692 | - * @return string |
|
693 | - */ |
|
694 | - public function getPublicMasterKey() { |
|
695 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID); |
|
696 | - } |
|
563 | + } |
|
564 | + |
|
565 | + /** |
|
566 | + * @param string $uid |
|
567 | + */ |
|
568 | + public function replaceUserKeys($uid) { |
|
569 | + $this->backupAllKeys('password_reset'); |
|
570 | + $this->deletePublicKey($uid); |
|
571 | + $this->deletePrivateKey($uid); |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * @param $uid |
|
576 | + * @return bool |
|
577 | + */ |
|
578 | + public function deletePublicKey($uid) { |
|
579 | + return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID); |
|
580 | + } |
|
581 | + |
|
582 | + /** |
|
583 | + * @param string $uid |
|
584 | + * @return bool |
|
585 | + */ |
|
586 | + private function deletePrivateKey($uid) { |
|
587 | + return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID); |
|
588 | + } |
|
589 | + |
|
590 | + /** |
|
591 | + * @param string $path |
|
592 | + * @return bool |
|
593 | + */ |
|
594 | + public function deleteAllFileKeys($path) { |
|
595 | + return $this->keyStorage->deleteAllFileKeys($path); |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * @param array $userIds |
|
600 | + * @return array |
|
601 | + * @throws PublicKeyMissingException |
|
602 | + */ |
|
603 | + public function getPublicKeys(array $userIds) { |
|
604 | + $keys = []; |
|
605 | + |
|
606 | + foreach ($userIds as $userId) { |
|
607 | + try { |
|
608 | + $keys[$userId] = $this->getPublicKey($userId); |
|
609 | + } catch (PublicKeyMissingException $e) { |
|
610 | + continue; |
|
611 | + } |
|
612 | + } |
|
613 | + |
|
614 | + return $keys; |
|
615 | + |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * @param string $keyId |
|
620 | + * @return string returns openssl key |
|
621 | + */ |
|
622 | + public function getSystemPrivateKey($keyId) { |
|
623 | + return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
624 | + } |
|
625 | + |
|
626 | + /** |
|
627 | + * @param string $keyId |
|
628 | + * @param string $key |
|
629 | + * @return string returns openssl key |
|
630 | + */ |
|
631 | + public function setSystemPrivateKey($keyId, $key) { |
|
632 | + return $this->keyStorage->setSystemUserKey( |
|
633 | + $keyId . '.' . $this->privateKeyId, |
|
634 | + $key, |
|
635 | + Encryption::ID); |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * add system keys such as the public share key and the recovery key |
|
640 | + * |
|
641 | + * @param array $accessList |
|
642 | + * @param array $publicKeys |
|
643 | + * @param string $uid |
|
644 | + * @return array |
|
645 | + * @throws PublicKeyMissingException |
|
646 | + */ |
|
647 | + public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
648 | + if (!empty($accessList['public'])) { |
|
649 | + $publicShareKey = $this->getPublicShareKey(); |
|
650 | + if (empty($publicShareKey)) { |
|
651 | + throw new PublicKeyMissingException($this->getPublicShareKeyId()); |
|
652 | + } |
|
653 | + $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey; |
|
654 | + } |
|
655 | + |
|
656 | + if ($this->recoveryKeyExists() && |
|
657 | + $this->util->isRecoveryEnabledForUser($uid)) { |
|
658 | + |
|
659 | + $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey(); |
|
660 | + } |
|
661 | + |
|
662 | + return $publicKeys; |
|
663 | + } |
|
664 | + |
|
665 | + /** |
|
666 | + * get master key password |
|
667 | + * |
|
668 | + * @return string |
|
669 | + * @throws \Exception |
|
670 | + */ |
|
671 | + public function getMasterKeyPassword() { |
|
672 | + $password = $this->config->getSystemValue('secret'); |
|
673 | + if (empty($password)){ |
|
674 | + throw new \Exception('Can not get secret from instance'); |
|
675 | + } |
|
676 | + |
|
677 | + return $password; |
|
678 | + } |
|
679 | + |
|
680 | + /** |
|
681 | + * return master key id |
|
682 | + * |
|
683 | + * @return string |
|
684 | + */ |
|
685 | + public function getMasterKeyId() { |
|
686 | + return $this->masterKeyId; |
|
687 | + } |
|
688 | + |
|
689 | + /** |
|
690 | + * get public master key |
|
691 | + * |
|
692 | + * @return string |
|
693 | + */ |
|
694 | + public function getPublicMasterKey() { |
|
695 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID); |
|
696 | + } |
|
697 | 697 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | $this->recoveryKeyId = $this->config->getAppValue('encryption', |
129 | 129 | 'recoveryKeyId'); |
130 | 130 | if (empty($this->recoveryKeyId)) { |
131 | - $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
131 | + $this->recoveryKeyId = 'recoveryKey_'.substr(md5(time()), 0, 8); |
|
132 | 132 | $this->config->setAppValue('encryption', |
133 | 133 | 'recoveryKeyId', |
134 | 134 | $this->recoveryKeyId); |
@@ -137,14 +137,14 @@ discard block |
||
137 | 137 | $this->publicShareKeyId = $this->config->getAppValue('encryption', |
138 | 138 | 'publicShareKeyId'); |
139 | 139 | if (empty($this->publicShareKeyId)) { |
140 | - $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
140 | + $this->publicShareKeyId = 'pubShare_'.substr(md5(time()), 0, 8); |
|
141 | 141 | $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
142 | 142 | } |
143 | 143 | |
144 | 144 | $this->masterKeyId = $this->config->getAppValue('encryption', |
145 | 145 | 'masterKeyId'); |
146 | 146 | if (empty($this->masterKeyId)) { |
147 | - $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
147 | + $this->masterKeyId = 'master_'.substr(md5(time()), 0, 8); |
|
148 | 148 | $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
149 | 149 | } |
150 | 150 | |
@@ -162,13 +162,13 @@ discard block |
||
162 | 162 | |
163 | 163 | // Save public key |
164 | 164 | $this->keyStorage->setSystemUserKey( |
165 | - $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'], |
|
165 | + $this->publicShareKeyId.'.publicKey', $keyPair['publicKey'], |
|
166 | 166 | Encryption::ID); |
167 | 167 | |
168 | 168 | // Encrypt private key empty passphrase |
169 | 169 | $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
170 | 170 | $header = $this->crypt->generateHeader(); |
171 | - $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
171 | + $this->setSystemPrivateKey($this->publicShareKeyId, $header.$encryptedKey); |
|
172 | 172 | } |
173 | 173 | } |
174 | 174 | |
@@ -182,13 +182,13 @@ discard block |
||
182 | 182 | |
183 | 183 | // Save public key |
184 | 184 | $this->keyStorage->setSystemUserKey( |
185 | - $this->masterKeyId . '.publicKey', $keyPair['publicKey'], |
|
185 | + $this->masterKeyId.'.publicKey', $keyPair['publicKey'], |
|
186 | 186 | Encryption::ID); |
187 | 187 | |
188 | 188 | // Encrypt private key with system password |
189 | 189 | $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
190 | 190 | $header = $this->crypt->generateHeader(); |
191 | - $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
191 | + $this->setSystemPrivateKey($this->masterKeyId, $header.$encryptedKey); |
|
192 | 192 | } |
193 | 193 | } |
194 | 194 | |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | * @return string |
207 | 207 | */ |
208 | 208 | public function getRecoveryKey() { |
209 | - return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID); |
|
209 | + return $this->keyStorage->getSystemUserKey($this->recoveryKeyId.'.publicKey', Encryption::ID); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | * @return bool |
224 | 224 | */ |
225 | 225 | public function checkRecoveryPassword($password) { |
226 | - $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID); |
|
226 | + $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId.'.privateKey', Encryption::ID); |
|
227 | 227 | $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
228 | 228 | |
229 | 229 | if ($decryptedRecoveryKey) { |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | $header = $this->crypt->generateHeader(); |
248 | 248 | |
249 | 249 | if ($encryptedKey) { |
250 | - $this->setPrivateKey($uid, $header . $encryptedKey); |
|
250 | + $this->setPrivateKey($uid, $header.$encryptedKey); |
|
251 | 251 | return true; |
252 | 252 | } |
253 | 253 | return false; |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | $header = $this->crypt->generateHeader(); |
270 | 270 | |
271 | 271 | if ($encryptedKey) { |
272 | - $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
272 | + $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header.$encryptedKey); |
|
273 | 273 | return true; |
274 | 274 | } |
275 | 275 | return false; |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | * @return boolean |
330 | 330 | */ |
331 | 331 | public function setShareKey($path, $uid, $key) { |
332 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
332 | + $keyId = $uid.'.'.$this->shareKeyId; |
|
333 | 333 | return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
334 | 334 | } |
335 | 335 | |
@@ -346,7 +346,7 @@ discard block |
||
346 | 346 | |
347 | 347 | |
348 | 348 | try { |
349 | - if($this->util->isMasterKeyEnabled()) { |
|
349 | + if ($this->util->isMasterKeyEnabled()) { |
|
350 | 350 | $uid = $this->getMasterKeyId(); |
351 | 351 | $passPhrase = $this->getMasterKeyPassword(); |
352 | 352 | $privateKey = $this->getSystemPrivateKey($uid); |
@@ -360,7 +360,7 @@ discard block |
||
360 | 360 | return false; |
361 | 361 | } catch (\Exception $e) { |
362 | 362 | $this->log->warning( |
363 | - 'Could not decrypt the private key from user "' . $uid . '"" during login. ' . |
|
363 | + 'Could not decrypt the private key from user "'.$uid.'"" during login. '. |
|
364 | 364 | 'Assume password change on the user back-end. Error message: ' |
365 | 365 | . $e->getMessage() |
366 | 366 | ); |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | if (is_null($uid)) { |
403 | 403 | $uid = $this->getPublicShareKeyId(); |
404 | 404 | $shareKey = $this->getShareKey($path, $uid); |
405 | - $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID); |
|
405 | + $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId.'.privateKey', Encryption::ID); |
|
406 | 406 | $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
407 | 407 | } else { |
408 | 408 | |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | */ |
433 | 433 | public function getVersion($path, View $view) { |
434 | 434 | $fileInfo = $view->getFileInfo($path); |
435 | - if($fileInfo === false) { |
|
435 | + if ($fileInfo === false) { |
|
436 | 436 | return 0; |
437 | 437 | } |
438 | 438 | return $fileInfo->getEncryptedVersion(); |
@@ -446,9 +446,9 @@ discard block |
||
446 | 446 | * @param View $view |
447 | 447 | */ |
448 | 448 | public function setVersion($path, $version, View $view) { |
449 | - $fileInfo= $view->getFileInfo($path); |
|
449 | + $fileInfo = $view->getFileInfo($path); |
|
450 | 450 | |
451 | - if($fileInfo !== false) { |
|
451 | + if ($fileInfo !== false) { |
|
452 | 452 | $cache = $fileInfo->getStorage()->getCache(); |
453 | 453 | $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
454 | 454 | } |
@@ -477,7 +477,7 @@ discard block |
||
477 | 477 | public function deleteShareKey($path, $keyId) { |
478 | 478 | return $this->keyStorage->deleteFileKey( |
479 | 479 | $path, |
480 | - $keyId . '.' . $this->shareKeyId, |
|
480 | + $keyId.'.'.$this->shareKeyId, |
|
481 | 481 | Encryption::ID); |
482 | 482 | } |
483 | 483 | |
@@ -488,7 +488,7 @@ discard block |
||
488 | 488 | * @return mixed |
489 | 489 | */ |
490 | 490 | public function getShareKey($path, $uid) { |
491 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
491 | + $keyId = $uid.'.'.$this->shareKeyId; |
|
492 | 492 | return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
493 | 493 | } |
494 | 494 | |
@@ -550,7 +550,7 @@ discard block |
||
550 | 550 | * @return string |
551 | 551 | */ |
552 | 552 | public function getPublicShareKey() { |
553 | - return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID); |
|
553 | + return $this->keyStorage->getSystemUserKey($this->publicShareKeyId.'.publicKey', Encryption::ID); |
|
554 | 554 | } |
555 | 555 | |
556 | 556 | /** |
@@ -620,7 +620,7 @@ discard block |
||
620 | 620 | * @return string returns openssl key |
621 | 621 | */ |
622 | 622 | public function getSystemPrivateKey($keyId) { |
623 | - return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
623 | + return $this->keyStorage->getSystemUserKey($keyId.'.'.$this->privateKeyId, Encryption::ID); |
|
624 | 624 | } |
625 | 625 | |
626 | 626 | /** |
@@ -630,7 +630,7 @@ discard block |
||
630 | 630 | */ |
631 | 631 | public function setSystemPrivateKey($keyId, $key) { |
632 | 632 | return $this->keyStorage->setSystemUserKey( |
633 | - $keyId . '.' . $this->privateKeyId, |
|
633 | + $keyId.'.'.$this->privateKeyId, |
|
634 | 634 | $key, |
635 | 635 | Encryption::ID); |
636 | 636 | } |
@@ -670,7 +670,7 @@ discard block |
||
670 | 670 | */ |
671 | 671 | public function getMasterKeyPassword() { |
672 | 672 | $password = $this->config->getSystemValue('secret'); |
673 | - if (empty($password)){ |
|
673 | + if (empty($password)) { |
|
674 | 674 | throw new \Exception('Can not get secret from instance'); |
675 | 675 | } |
676 | 676 | |
@@ -692,6 +692,6 @@ discard block |
||
692 | 692 | * @return string |
693 | 693 | */ |
694 | 694 | public function getPublicMasterKey() { |
695 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID); |
|
695 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId.'.publicKey', Encryption::ID); |
|
696 | 696 | } |
697 | 697 | } |
@@ -473,7 +473,7 @@ |
||
473 | 473 | /** |
474 | 474 | * get database row of a give share |
475 | 475 | * |
476 | - * @param $id |
|
476 | + * @param integer $id |
|
477 | 477 | * @return array |
478 | 478 | * @throws ShareNotFound |
479 | 479 | */ |
@@ -43,525 +43,525 @@ |
||
43 | 43 | */ |
44 | 44 | class FederatedShareProvider implements IShareProvider { |
45 | 45 | |
46 | - const SHARE_TYPE_REMOTE = 6; |
|
47 | - |
|
48 | - /** @var IDBConnection */ |
|
49 | - private $dbConnection; |
|
50 | - |
|
51 | - /** @var AddressHandler */ |
|
52 | - private $addressHandler; |
|
53 | - |
|
54 | - /** @var Notifications */ |
|
55 | - private $notifications; |
|
56 | - |
|
57 | - /** @var TokenHandler */ |
|
58 | - private $tokenHandler; |
|
59 | - |
|
60 | - /** @var IL10N */ |
|
61 | - private $l; |
|
62 | - |
|
63 | - /** @var ILogger */ |
|
64 | - private $logger; |
|
65 | - |
|
66 | - /** @var IRootFolder */ |
|
67 | - private $rootFolder; |
|
68 | - |
|
69 | - /** |
|
70 | - * DefaultShareProvider constructor. |
|
71 | - * |
|
72 | - * @param IDBConnection $connection |
|
73 | - * @param AddressHandler $addressHandler |
|
74 | - * @param Notifications $notifications |
|
75 | - * @param TokenHandler $tokenHandler |
|
76 | - * @param IL10N $l10n |
|
77 | - * @param ILogger $logger |
|
78 | - * @param IRootFolder $rootFolder |
|
79 | - */ |
|
80 | - public function __construct( |
|
81 | - IDBConnection $connection, |
|
82 | - AddressHandler $addressHandler, |
|
83 | - Notifications $notifications, |
|
84 | - TokenHandler $tokenHandler, |
|
85 | - IL10N $l10n, |
|
86 | - ILogger $logger, |
|
87 | - IRootFolder $rootFolder |
|
88 | - ) { |
|
89 | - $this->dbConnection = $connection; |
|
90 | - $this->addressHandler = $addressHandler; |
|
91 | - $this->notifications = $notifications; |
|
92 | - $this->tokenHandler = $tokenHandler; |
|
93 | - $this->l = $l10n; |
|
94 | - $this->logger = $logger; |
|
95 | - $this->rootFolder = $rootFolder; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Return the identifier of this provider. |
|
100 | - * |
|
101 | - * @return string Containing only [a-zA-Z0-9] |
|
102 | - */ |
|
103 | - public function identifier() { |
|
104 | - return 'ocFederatedSharing'; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Share a path |
|
109 | - * |
|
110 | - * @param IShare $share |
|
111 | - * @return IShare The share object |
|
112 | - * @throws ShareNotFound |
|
113 | - * @throws \Exception |
|
114 | - */ |
|
115 | - public function create(IShare $share) { |
|
116 | - |
|
117 | - $shareWith = $share->getSharedWith(); |
|
118 | - $itemSource = $share->getNodeId(); |
|
119 | - $itemType = $share->getNodeType(); |
|
120 | - $uidOwner = $share->getShareOwner(); |
|
121 | - $permissions = $share->getPermissions(); |
|
122 | - $sharedBy = $share->getSharedBy(); |
|
123 | - |
|
124 | - /* |
|
46 | + const SHARE_TYPE_REMOTE = 6; |
|
47 | + |
|
48 | + /** @var IDBConnection */ |
|
49 | + private $dbConnection; |
|
50 | + |
|
51 | + /** @var AddressHandler */ |
|
52 | + private $addressHandler; |
|
53 | + |
|
54 | + /** @var Notifications */ |
|
55 | + private $notifications; |
|
56 | + |
|
57 | + /** @var TokenHandler */ |
|
58 | + private $tokenHandler; |
|
59 | + |
|
60 | + /** @var IL10N */ |
|
61 | + private $l; |
|
62 | + |
|
63 | + /** @var ILogger */ |
|
64 | + private $logger; |
|
65 | + |
|
66 | + /** @var IRootFolder */ |
|
67 | + private $rootFolder; |
|
68 | + |
|
69 | + /** |
|
70 | + * DefaultShareProvider constructor. |
|
71 | + * |
|
72 | + * @param IDBConnection $connection |
|
73 | + * @param AddressHandler $addressHandler |
|
74 | + * @param Notifications $notifications |
|
75 | + * @param TokenHandler $tokenHandler |
|
76 | + * @param IL10N $l10n |
|
77 | + * @param ILogger $logger |
|
78 | + * @param IRootFolder $rootFolder |
|
79 | + */ |
|
80 | + public function __construct( |
|
81 | + IDBConnection $connection, |
|
82 | + AddressHandler $addressHandler, |
|
83 | + Notifications $notifications, |
|
84 | + TokenHandler $tokenHandler, |
|
85 | + IL10N $l10n, |
|
86 | + ILogger $logger, |
|
87 | + IRootFolder $rootFolder |
|
88 | + ) { |
|
89 | + $this->dbConnection = $connection; |
|
90 | + $this->addressHandler = $addressHandler; |
|
91 | + $this->notifications = $notifications; |
|
92 | + $this->tokenHandler = $tokenHandler; |
|
93 | + $this->l = $l10n; |
|
94 | + $this->logger = $logger; |
|
95 | + $this->rootFolder = $rootFolder; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Return the identifier of this provider. |
|
100 | + * |
|
101 | + * @return string Containing only [a-zA-Z0-9] |
|
102 | + */ |
|
103 | + public function identifier() { |
|
104 | + return 'ocFederatedSharing'; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Share a path |
|
109 | + * |
|
110 | + * @param IShare $share |
|
111 | + * @return IShare The share object |
|
112 | + * @throws ShareNotFound |
|
113 | + * @throws \Exception |
|
114 | + */ |
|
115 | + public function create(IShare $share) { |
|
116 | + |
|
117 | + $shareWith = $share->getSharedWith(); |
|
118 | + $itemSource = $share->getNodeId(); |
|
119 | + $itemType = $share->getNodeType(); |
|
120 | + $uidOwner = $share->getShareOwner(); |
|
121 | + $permissions = $share->getPermissions(); |
|
122 | + $sharedBy = $share->getSharedBy(); |
|
123 | + |
|
124 | + /* |
|
125 | 125 | * Check if file is not already shared with the remote user |
126 | 126 | */ |
127 | - $alreadyShared = $this->getSharedWith($shareWith, self::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0); |
|
128 | - if (!empty($alreadyShared)) { |
|
129 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
130 | - $message_t = $this->l->t('Sharing %s failed, because this item is already shared with %s', array($share->getNode()->getName(), $shareWith)); |
|
131 | - $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']); |
|
132 | - throw new \Exception($message_t); |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - // don't allow federated shares if source and target server are the same |
|
137 | - list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith); |
|
138 | - $currentServer = $this->addressHandler->generateRemoteURL(); |
|
139 | - $currentUser = $sharedBy; |
|
140 | - if ($this->addressHandler->compareAddresses($user, $remote, $currentUser, $currentServer)) { |
|
141 | - $message = 'Not allowed to create a federated share with the same user.'; |
|
142 | - $message_t = $this->l->t('Not allowed to create a federated share with the same user'); |
|
143 | - $this->logger->debug($message, ['app' => 'Federated File Sharing']); |
|
144 | - throw new \Exception($message_t); |
|
145 | - } |
|
146 | - |
|
147 | - $token = $this->tokenHandler->generateToken(); |
|
148 | - |
|
149 | - $shareWith = $user . '@' . $remote; |
|
150 | - |
|
151 | - $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token); |
|
152 | - |
|
153 | - $send = $this->notifications->sendRemoteShare( |
|
154 | - $token, |
|
155 | - $shareWith, |
|
156 | - $share->getNode()->getName(), |
|
157 | - $shareId, |
|
158 | - $share->getSharedBy() |
|
159 | - ); |
|
160 | - |
|
161 | - $data = $this->getRawShare($shareId); |
|
162 | - $share = $this->createShare($data); |
|
163 | - |
|
164 | - if ($send === false) { |
|
165 | - $this->delete($share); |
|
166 | - $message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', |
|
167 | - [$share->getNode()->getName(), $shareWith]); |
|
168 | - throw new \Exception($message_t); |
|
169 | - } |
|
170 | - |
|
171 | - return $share; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * add share to the database and return the ID |
|
176 | - * |
|
177 | - * @param int $itemSource |
|
178 | - * @param string $itemType |
|
179 | - * @param string $shareWith |
|
180 | - * @param string $sharedBy |
|
181 | - * @param string $uidOwner |
|
182 | - * @param int $permissions |
|
183 | - * @param string $token |
|
184 | - * @return int |
|
185 | - */ |
|
186 | - private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) { |
|
187 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
188 | - $qb->insert('share') |
|
189 | - ->setValue('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)) |
|
190 | - ->setValue('item_type', $qb->createNamedParameter($itemType)) |
|
191 | - ->setValue('item_source', $qb->createNamedParameter($itemSource)) |
|
192 | - ->setValue('file_source', $qb->createNamedParameter($itemSource)) |
|
193 | - ->setValue('share_with', $qb->createNamedParameter($shareWith)) |
|
194 | - ->setValue('uid_owner', $qb->createNamedParameter($uidOwner)) |
|
195 | - ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy)) |
|
196 | - ->setValue('permissions', $qb->createNamedParameter($permissions)) |
|
197 | - ->setValue('token', $qb->createNamedParameter($token)) |
|
198 | - ->setValue('stime', $qb->createNamedParameter(time())); |
|
199 | - |
|
200 | - /* |
|
127 | + $alreadyShared = $this->getSharedWith($shareWith, self::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0); |
|
128 | + if (!empty($alreadyShared)) { |
|
129 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
130 | + $message_t = $this->l->t('Sharing %s failed, because this item is already shared with %s', array($share->getNode()->getName(), $shareWith)); |
|
131 | + $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']); |
|
132 | + throw new \Exception($message_t); |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + // don't allow federated shares if source and target server are the same |
|
137 | + list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith); |
|
138 | + $currentServer = $this->addressHandler->generateRemoteURL(); |
|
139 | + $currentUser = $sharedBy; |
|
140 | + if ($this->addressHandler->compareAddresses($user, $remote, $currentUser, $currentServer)) { |
|
141 | + $message = 'Not allowed to create a federated share with the same user.'; |
|
142 | + $message_t = $this->l->t('Not allowed to create a federated share with the same user'); |
|
143 | + $this->logger->debug($message, ['app' => 'Federated File Sharing']); |
|
144 | + throw new \Exception($message_t); |
|
145 | + } |
|
146 | + |
|
147 | + $token = $this->tokenHandler->generateToken(); |
|
148 | + |
|
149 | + $shareWith = $user . '@' . $remote; |
|
150 | + |
|
151 | + $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token); |
|
152 | + |
|
153 | + $send = $this->notifications->sendRemoteShare( |
|
154 | + $token, |
|
155 | + $shareWith, |
|
156 | + $share->getNode()->getName(), |
|
157 | + $shareId, |
|
158 | + $share->getSharedBy() |
|
159 | + ); |
|
160 | + |
|
161 | + $data = $this->getRawShare($shareId); |
|
162 | + $share = $this->createShare($data); |
|
163 | + |
|
164 | + if ($send === false) { |
|
165 | + $this->delete($share); |
|
166 | + $message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', |
|
167 | + [$share->getNode()->getName(), $shareWith]); |
|
168 | + throw new \Exception($message_t); |
|
169 | + } |
|
170 | + |
|
171 | + return $share; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * add share to the database and return the ID |
|
176 | + * |
|
177 | + * @param int $itemSource |
|
178 | + * @param string $itemType |
|
179 | + * @param string $shareWith |
|
180 | + * @param string $sharedBy |
|
181 | + * @param string $uidOwner |
|
182 | + * @param int $permissions |
|
183 | + * @param string $token |
|
184 | + * @return int |
|
185 | + */ |
|
186 | + private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) { |
|
187 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
188 | + $qb->insert('share') |
|
189 | + ->setValue('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)) |
|
190 | + ->setValue('item_type', $qb->createNamedParameter($itemType)) |
|
191 | + ->setValue('item_source', $qb->createNamedParameter($itemSource)) |
|
192 | + ->setValue('file_source', $qb->createNamedParameter($itemSource)) |
|
193 | + ->setValue('share_with', $qb->createNamedParameter($shareWith)) |
|
194 | + ->setValue('uid_owner', $qb->createNamedParameter($uidOwner)) |
|
195 | + ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy)) |
|
196 | + ->setValue('permissions', $qb->createNamedParameter($permissions)) |
|
197 | + ->setValue('token', $qb->createNamedParameter($token)) |
|
198 | + ->setValue('stime', $qb->createNamedParameter(time())); |
|
199 | + |
|
200 | + /* |
|
201 | 201 | * Added to fix https://github.com/owncloud/core/issues/22215 |
202 | 202 | * Can be removed once we get rid of ajax/share.php |
203 | 203 | */ |
204 | - $qb->setValue('file_target', $qb->createNamedParameter('')); |
|
205 | - |
|
206 | - $qb->execute(); |
|
207 | - $id = $qb->getLastInsertId(); |
|
208 | - |
|
209 | - return (int)$id; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Update a share |
|
214 | - * |
|
215 | - * @param IShare $share |
|
216 | - * @return IShare The share object |
|
217 | - */ |
|
218 | - public function update(IShare $share) { |
|
219 | - /* |
|
204 | + $qb->setValue('file_target', $qb->createNamedParameter('')); |
|
205 | + |
|
206 | + $qb->execute(); |
|
207 | + $id = $qb->getLastInsertId(); |
|
208 | + |
|
209 | + return (int)$id; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Update a share |
|
214 | + * |
|
215 | + * @param IShare $share |
|
216 | + * @return IShare The share object |
|
217 | + */ |
|
218 | + public function update(IShare $share) { |
|
219 | + /* |
|
220 | 220 | * We allow updating the permissions of federated shares |
221 | 221 | */ |
222 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
223 | - $qb->update('share') |
|
224 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
225 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
226 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
227 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
228 | - ->execute(); |
|
229 | - |
|
230 | - return $share; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * @inheritdoc |
|
235 | - */ |
|
236 | - public function move(IShare $share, $recipient) { |
|
237 | - /* |
|
222 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
223 | + $qb->update('share') |
|
224 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
225 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
226 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
227 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
228 | + ->execute(); |
|
229 | + |
|
230 | + return $share; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * @inheritdoc |
|
235 | + */ |
|
236 | + public function move(IShare $share, $recipient) { |
|
237 | + /* |
|
238 | 238 | * This function does nothing yet as it is just for outgoing |
239 | 239 | * federated shares. |
240 | 240 | */ |
241 | - return $share; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Get all children of this share |
|
246 | - * |
|
247 | - * @param IShare $parent |
|
248 | - * @return IShare[] |
|
249 | - */ |
|
250 | - public function getChildren(IShare $parent) { |
|
251 | - $children = []; |
|
252 | - |
|
253 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
254 | - $qb->select('*') |
|
255 | - ->from('share') |
|
256 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
257 | - ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) |
|
258 | - ->orderBy('id'); |
|
259 | - |
|
260 | - $cursor = $qb->execute(); |
|
261 | - while($data = $cursor->fetch()) { |
|
262 | - $children[] = $this->createShare($data); |
|
263 | - } |
|
264 | - $cursor->closeCursor(); |
|
265 | - |
|
266 | - return $children; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Delete a share |
|
271 | - * |
|
272 | - * @param IShare $share |
|
273 | - */ |
|
274 | - public function delete(IShare $share) { |
|
275 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
276 | - $qb->delete('share') |
|
277 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
278 | - $qb->execute(); |
|
279 | - |
|
280 | - list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedWith()); |
|
281 | - $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken()); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @inheritdoc |
|
286 | - */ |
|
287 | - public function deleteFromSelf(IShare $share, $recipient) { |
|
288 | - // nothing to do here. Technically deleteFromSelf in the context of federated |
|
289 | - // shares is a umount of a external storage. This is handled here |
|
290 | - // apps/files_sharing/lib/external/manager.php |
|
291 | - // TODO move this code over to this app |
|
292 | - return; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * @inheritdoc |
|
297 | - */ |
|
298 | - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
299 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
300 | - $qb->select('*') |
|
301 | - ->from('share'); |
|
302 | - |
|
303 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); |
|
304 | - |
|
305 | - /** |
|
306 | - * Reshares for this user are shares where they are the owner. |
|
307 | - */ |
|
308 | - if ($reshares === false) { |
|
309 | - //Special case for old shares created via the web UI |
|
310 | - $or1 = $qb->expr()->andX( |
|
311 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
312 | - $qb->expr()->isNull('uid_initiator') |
|
313 | - ); |
|
314 | - |
|
315 | - $qb->andWhere( |
|
316 | - $qb->expr()->orX( |
|
317 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)), |
|
318 | - $or1 |
|
319 | - ) |
|
320 | - ); |
|
321 | - } else { |
|
322 | - $qb->andWhere( |
|
323 | - $qb->expr()->orX( |
|
324 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
325 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
326 | - ) |
|
327 | - ); |
|
328 | - } |
|
329 | - |
|
330 | - if ($node !== null) { |
|
331 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
332 | - } |
|
333 | - |
|
334 | - if ($limit !== -1) { |
|
335 | - $qb->setMaxResults($limit); |
|
336 | - } |
|
337 | - |
|
338 | - $qb->setFirstResult($offset); |
|
339 | - $qb->orderBy('id'); |
|
340 | - |
|
341 | - $cursor = $qb->execute(); |
|
342 | - $shares = []; |
|
343 | - while($data = $cursor->fetch()) { |
|
344 | - $shares[] = $this->createShare($data); |
|
345 | - } |
|
346 | - $cursor->closeCursor(); |
|
347 | - |
|
348 | - return $shares; |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * @inheritdoc |
|
353 | - */ |
|
354 | - public function getShareById($id, $recipientId = null) { |
|
355 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
356 | - |
|
357 | - $qb->select('*') |
|
358 | - ->from('share') |
|
359 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
360 | - ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); |
|
241 | + return $share; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Get all children of this share |
|
246 | + * |
|
247 | + * @param IShare $parent |
|
248 | + * @return IShare[] |
|
249 | + */ |
|
250 | + public function getChildren(IShare $parent) { |
|
251 | + $children = []; |
|
252 | + |
|
253 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
254 | + $qb->select('*') |
|
255 | + ->from('share') |
|
256 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
257 | + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) |
|
258 | + ->orderBy('id'); |
|
259 | + |
|
260 | + $cursor = $qb->execute(); |
|
261 | + while($data = $cursor->fetch()) { |
|
262 | + $children[] = $this->createShare($data); |
|
263 | + } |
|
264 | + $cursor->closeCursor(); |
|
265 | + |
|
266 | + return $children; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Delete a share |
|
271 | + * |
|
272 | + * @param IShare $share |
|
273 | + */ |
|
274 | + public function delete(IShare $share) { |
|
275 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
276 | + $qb->delete('share') |
|
277 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
278 | + $qb->execute(); |
|
279 | + |
|
280 | + list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedWith()); |
|
281 | + $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken()); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @inheritdoc |
|
286 | + */ |
|
287 | + public function deleteFromSelf(IShare $share, $recipient) { |
|
288 | + // nothing to do here. Technically deleteFromSelf in the context of federated |
|
289 | + // shares is a umount of a external storage. This is handled here |
|
290 | + // apps/files_sharing/lib/external/manager.php |
|
291 | + // TODO move this code over to this app |
|
292 | + return; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * @inheritdoc |
|
297 | + */ |
|
298 | + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
299 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
300 | + $qb->select('*') |
|
301 | + ->from('share'); |
|
302 | + |
|
303 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); |
|
304 | + |
|
305 | + /** |
|
306 | + * Reshares for this user are shares where they are the owner. |
|
307 | + */ |
|
308 | + if ($reshares === false) { |
|
309 | + //Special case for old shares created via the web UI |
|
310 | + $or1 = $qb->expr()->andX( |
|
311 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
312 | + $qb->expr()->isNull('uid_initiator') |
|
313 | + ); |
|
314 | + |
|
315 | + $qb->andWhere( |
|
316 | + $qb->expr()->orX( |
|
317 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)), |
|
318 | + $or1 |
|
319 | + ) |
|
320 | + ); |
|
321 | + } else { |
|
322 | + $qb->andWhere( |
|
323 | + $qb->expr()->orX( |
|
324 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
325 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
326 | + ) |
|
327 | + ); |
|
328 | + } |
|
329 | + |
|
330 | + if ($node !== null) { |
|
331 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
332 | + } |
|
333 | + |
|
334 | + if ($limit !== -1) { |
|
335 | + $qb->setMaxResults($limit); |
|
336 | + } |
|
337 | + |
|
338 | + $qb->setFirstResult($offset); |
|
339 | + $qb->orderBy('id'); |
|
340 | + |
|
341 | + $cursor = $qb->execute(); |
|
342 | + $shares = []; |
|
343 | + while($data = $cursor->fetch()) { |
|
344 | + $shares[] = $this->createShare($data); |
|
345 | + } |
|
346 | + $cursor->closeCursor(); |
|
347 | + |
|
348 | + return $shares; |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * @inheritdoc |
|
353 | + */ |
|
354 | + public function getShareById($id, $recipientId = null) { |
|
355 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
356 | + |
|
357 | + $qb->select('*') |
|
358 | + ->from('share') |
|
359 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
360 | + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); |
|
361 | 361 | |
362 | - $cursor = $qb->execute(); |
|
363 | - $data = $cursor->fetch(); |
|
364 | - $cursor->closeCursor(); |
|
365 | - |
|
366 | - if ($data === false) { |
|
367 | - throw new ShareNotFound(); |
|
368 | - } |
|
369 | - |
|
370 | - try { |
|
371 | - $share = $this->createShare($data); |
|
372 | - } catch (InvalidShare $e) { |
|
373 | - throw new ShareNotFound(); |
|
374 | - } |
|
375 | - |
|
376 | - return $share; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Get shares for a given path |
|
381 | - * |
|
382 | - * @param \OCP\Files\Node $path |
|
383 | - * @return IShare[] |
|
384 | - */ |
|
385 | - public function getSharesByPath(Node $path) { |
|
386 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
387 | - |
|
388 | - $cursor = $qb->select('*') |
|
389 | - ->from('share') |
|
390 | - ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
391 | - ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) |
|
392 | - ->execute(); |
|
393 | - |
|
394 | - $shares = []; |
|
395 | - while($data = $cursor->fetch()) { |
|
396 | - $shares[] = $this->createShare($data); |
|
397 | - } |
|
398 | - $cursor->closeCursor(); |
|
399 | - |
|
400 | - return $shares; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * @inheritdoc |
|
405 | - */ |
|
406 | - public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
407 | - /** @var IShare[] $shares */ |
|
408 | - $shares = []; |
|
409 | - |
|
410 | - //Get shares directly with this user |
|
411 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
412 | - $qb->select('*') |
|
413 | - ->from('share'); |
|
414 | - |
|
415 | - // Order by id |
|
416 | - $qb->orderBy('id'); |
|
417 | - |
|
418 | - // Set limit and offset |
|
419 | - if ($limit !== -1) { |
|
420 | - $qb->setMaxResults($limit); |
|
421 | - } |
|
422 | - $qb->setFirstResult($offset); |
|
423 | - |
|
424 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); |
|
425 | - $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))); |
|
426 | - |
|
427 | - // Filter by node if provided |
|
428 | - if ($node !== null) { |
|
429 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
430 | - } |
|
431 | - |
|
432 | - $cursor = $qb->execute(); |
|
433 | - |
|
434 | - while($data = $cursor->fetch()) { |
|
435 | - $shares[] = $this->createShare($data); |
|
436 | - } |
|
437 | - $cursor->closeCursor(); |
|
438 | - |
|
439 | - |
|
440 | - return $shares; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Get a share by token |
|
445 | - * |
|
446 | - * @param string $token |
|
447 | - * @return IShare |
|
448 | - * @throws ShareNotFound |
|
449 | - */ |
|
450 | - public function getShareByToken($token) { |
|
451 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
452 | - |
|
453 | - $cursor = $qb->select('*') |
|
454 | - ->from('share') |
|
455 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) |
|
456 | - ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
457 | - ->execute(); |
|
458 | - |
|
459 | - $data = $cursor->fetch(); |
|
460 | - |
|
461 | - if ($data === false) { |
|
462 | - throw new ShareNotFound(); |
|
463 | - } |
|
464 | - |
|
465 | - try { |
|
466 | - $share = $this->createShare($data); |
|
467 | - } catch (InvalidShare $e) { |
|
468 | - throw new ShareNotFound(); |
|
469 | - } |
|
470 | - |
|
471 | - return $share; |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * get database row of a give share |
|
476 | - * |
|
477 | - * @param $id |
|
478 | - * @return array |
|
479 | - * @throws ShareNotFound |
|
480 | - */ |
|
481 | - private function getRawShare($id) { |
|
482 | - |
|
483 | - // Now fetch the inserted share and create a complete share object |
|
484 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
485 | - $qb->select('*') |
|
486 | - ->from('share') |
|
487 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
488 | - |
|
489 | - $cursor = $qb->execute(); |
|
490 | - $data = $cursor->fetch(); |
|
491 | - $cursor->closeCursor(); |
|
492 | - |
|
493 | - if ($data === false) { |
|
494 | - throw new ShareNotFound; |
|
495 | - } |
|
496 | - |
|
497 | - return $data; |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * Create a share object from an database row |
|
502 | - * |
|
503 | - * @param array $data |
|
504 | - * @return IShare |
|
505 | - * @throws InvalidShare |
|
506 | - * @throws ShareNotFound |
|
507 | - */ |
|
508 | - private function createShare($data) { |
|
509 | - |
|
510 | - $share = new Share($this->rootFolder); |
|
511 | - $share->setId((int)$data['id']) |
|
512 | - ->setShareType((int)$data['share_type']) |
|
513 | - ->setPermissions((int)$data['permissions']) |
|
514 | - ->setTarget($data['file_target']) |
|
515 | - ->setMailSend((bool)$data['mail_send']) |
|
516 | - ->setToken($data['token']); |
|
517 | - |
|
518 | - $shareTime = new \DateTime(); |
|
519 | - $shareTime->setTimestamp((int)$data['stime']); |
|
520 | - $share->setShareTime($shareTime); |
|
521 | - $share->setSharedWith($data['share_with']); |
|
522 | - |
|
523 | - if ($data['uid_initiator'] !== null) { |
|
524 | - $share->setShareOwner($data['uid_owner']); |
|
525 | - $share->setSharedBy($data['uid_initiator']); |
|
526 | - } else { |
|
527 | - //OLD SHARE |
|
528 | - $share->setSharedBy($data['uid_owner']); |
|
529 | - $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']); |
|
530 | - |
|
531 | - $owner = $path->getOwner(); |
|
532 | - $share->setShareOwner($owner->getUID()); |
|
533 | - } |
|
534 | - |
|
535 | - $share->setNodeId((int)$data['file_source']); |
|
536 | - $share->setNodeType($data['item_type']); |
|
537 | - |
|
538 | - $share->setProviderId($this->identifier()); |
|
539 | - |
|
540 | - return $share; |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Get the node with file $id for $user |
|
545 | - * |
|
546 | - * @param string $userId |
|
547 | - * @param int $id |
|
548 | - * @return \OCP\Files\File|\OCP\Files\Folder |
|
549 | - * @throws InvalidShare |
|
550 | - */ |
|
551 | - private function getNode($userId, $id) { |
|
552 | - try { |
|
553 | - $userFolder = $this->rootFolder->getUserFolder($userId); |
|
554 | - } catch (NotFoundException $e) { |
|
555 | - throw new InvalidShare(); |
|
556 | - } |
|
557 | - |
|
558 | - $nodes = $userFolder->getById($id); |
|
559 | - |
|
560 | - if (empty($nodes)) { |
|
561 | - throw new InvalidShare(); |
|
562 | - } |
|
563 | - |
|
564 | - return $nodes[0]; |
|
565 | - } |
|
362 | + $cursor = $qb->execute(); |
|
363 | + $data = $cursor->fetch(); |
|
364 | + $cursor->closeCursor(); |
|
365 | + |
|
366 | + if ($data === false) { |
|
367 | + throw new ShareNotFound(); |
|
368 | + } |
|
369 | + |
|
370 | + try { |
|
371 | + $share = $this->createShare($data); |
|
372 | + } catch (InvalidShare $e) { |
|
373 | + throw new ShareNotFound(); |
|
374 | + } |
|
375 | + |
|
376 | + return $share; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Get shares for a given path |
|
381 | + * |
|
382 | + * @param \OCP\Files\Node $path |
|
383 | + * @return IShare[] |
|
384 | + */ |
|
385 | + public function getSharesByPath(Node $path) { |
|
386 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
387 | + |
|
388 | + $cursor = $qb->select('*') |
|
389 | + ->from('share') |
|
390 | + ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
391 | + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) |
|
392 | + ->execute(); |
|
393 | + |
|
394 | + $shares = []; |
|
395 | + while($data = $cursor->fetch()) { |
|
396 | + $shares[] = $this->createShare($data); |
|
397 | + } |
|
398 | + $cursor->closeCursor(); |
|
399 | + |
|
400 | + return $shares; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * @inheritdoc |
|
405 | + */ |
|
406 | + public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
407 | + /** @var IShare[] $shares */ |
|
408 | + $shares = []; |
|
409 | + |
|
410 | + //Get shares directly with this user |
|
411 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
412 | + $qb->select('*') |
|
413 | + ->from('share'); |
|
414 | + |
|
415 | + // Order by id |
|
416 | + $qb->orderBy('id'); |
|
417 | + |
|
418 | + // Set limit and offset |
|
419 | + if ($limit !== -1) { |
|
420 | + $qb->setMaxResults($limit); |
|
421 | + } |
|
422 | + $qb->setFirstResult($offset); |
|
423 | + |
|
424 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); |
|
425 | + $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))); |
|
426 | + |
|
427 | + // Filter by node if provided |
|
428 | + if ($node !== null) { |
|
429 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
430 | + } |
|
431 | + |
|
432 | + $cursor = $qb->execute(); |
|
433 | + |
|
434 | + while($data = $cursor->fetch()) { |
|
435 | + $shares[] = $this->createShare($data); |
|
436 | + } |
|
437 | + $cursor->closeCursor(); |
|
438 | + |
|
439 | + |
|
440 | + return $shares; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Get a share by token |
|
445 | + * |
|
446 | + * @param string $token |
|
447 | + * @return IShare |
|
448 | + * @throws ShareNotFound |
|
449 | + */ |
|
450 | + public function getShareByToken($token) { |
|
451 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
452 | + |
|
453 | + $cursor = $qb->select('*') |
|
454 | + ->from('share') |
|
455 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) |
|
456 | + ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
457 | + ->execute(); |
|
458 | + |
|
459 | + $data = $cursor->fetch(); |
|
460 | + |
|
461 | + if ($data === false) { |
|
462 | + throw new ShareNotFound(); |
|
463 | + } |
|
464 | + |
|
465 | + try { |
|
466 | + $share = $this->createShare($data); |
|
467 | + } catch (InvalidShare $e) { |
|
468 | + throw new ShareNotFound(); |
|
469 | + } |
|
470 | + |
|
471 | + return $share; |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * get database row of a give share |
|
476 | + * |
|
477 | + * @param $id |
|
478 | + * @return array |
|
479 | + * @throws ShareNotFound |
|
480 | + */ |
|
481 | + private function getRawShare($id) { |
|
482 | + |
|
483 | + // Now fetch the inserted share and create a complete share object |
|
484 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
485 | + $qb->select('*') |
|
486 | + ->from('share') |
|
487 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
488 | + |
|
489 | + $cursor = $qb->execute(); |
|
490 | + $data = $cursor->fetch(); |
|
491 | + $cursor->closeCursor(); |
|
492 | + |
|
493 | + if ($data === false) { |
|
494 | + throw new ShareNotFound; |
|
495 | + } |
|
496 | + |
|
497 | + return $data; |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * Create a share object from an database row |
|
502 | + * |
|
503 | + * @param array $data |
|
504 | + * @return IShare |
|
505 | + * @throws InvalidShare |
|
506 | + * @throws ShareNotFound |
|
507 | + */ |
|
508 | + private function createShare($data) { |
|
509 | + |
|
510 | + $share = new Share($this->rootFolder); |
|
511 | + $share->setId((int)$data['id']) |
|
512 | + ->setShareType((int)$data['share_type']) |
|
513 | + ->setPermissions((int)$data['permissions']) |
|
514 | + ->setTarget($data['file_target']) |
|
515 | + ->setMailSend((bool)$data['mail_send']) |
|
516 | + ->setToken($data['token']); |
|
517 | + |
|
518 | + $shareTime = new \DateTime(); |
|
519 | + $shareTime->setTimestamp((int)$data['stime']); |
|
520 | + $share->setShareTime($shareTime); |
|
521 | + $share->setSharedWith($data['share_with']); |
|
522 | + |
|
523 | + if ($data['uid_initiator'] !== null) { |
|
524 | + $share->setShareOwner($data['uid_owner']); |
|
525 | + $share->setSharedBy($data['uid_initiator']); |
|
526 | + } else { |
|
527 | + //OLD SHARE |
|
528 | + $share->setSharedBy($data['uid_owner']); |
|
529 | + $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']); |
|
530 | + |
|
531 | + $owner = $path->getOwner(); |
|
532 | + $share->setShareOwner($owner->getUID()); |
|
533 | + } |
|
534 | + |
|
535 | + $share->setNodeId((int)$data['file_source']); |
|
536 | + $share->setNodeType($data['item_type']); |
|
537 | + |
|
538 | + $share->setProviderId($this->identifier()); |
|
539 | + |
|
540 | + return $share; |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Get the node with file $id for $user |
|
545 | + * |
|
546 | + * @param string $userId |
|
547 | + * @param int $id |
|
548 | + * @return \OCP\Files\File|\OCP\Files\Folder |
|
549 | + * @throws InvalidShare |
|
550 | + */ |
|
551 | + private function getNode($userId, $id) { |
|
552 | + try { |
|
553 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
|
554 | + } catch (NotFoundException $e) { |
|
555 | + throw new InvalidShare(); |
|
556 | + } |
|
557 | + |
|
558 | + $nodes = $userFolder->getById($id); |
|
559 | + |
|
560 | + if (empty($nodes)) { |
|
561 | + throw new InvalidShare(); |
|
562 | + } |
|
563 | + |
|
564 | + return $nodes[0]; |
|
565 | + } |
|
566 | 566 | |
567 | 567 | } |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | |
147 | 147 | $token = $this->tokenHandler->generateToken(); |
148 | 148 | |
149 | - $shareWith = $user . '@' . $remote; |
|
149 | + $shareWith = $user.'@'.$remote; |
|
150 | 150 | |
151 | 151 | $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token); |
152 | 152 | |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | $qb->execute(); |
207 | 207 | $id = $qb->getLastInsertId(); |
208 | 208 | |
209 | - return (int)$id; |
|
209 | + return (int) $id; |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | ->orderBy('id'); |
259 | 259 | |
260 | 260 | $cursor = $qb->execute(); |
261 | - while($data = $cursor->fetch()) { |
|
261 | + while ($data = $cursor->fetch()) { |
|
262 | 262 | $children[] = $this->createShare($data); |
263 | 263 | } |
264 | 264 | $cursor->closeCursor(); |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | |
341 | 341 | $cursor = $qb->execute(); |
342 | 342 | $shares = []; |
343 | - while($data = $cursor->fetch()) { |
|
343 | + while ($data = $cursor->fetch()) { |
|
344 | 344 | $shares[] = $this->createShare($data); |
345 | 345 | } |
346 | 346 | $cursor->closeCursor(); |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | ->execute(); |
393 | 393 | |
394 | 394 | $shares = []; |
395 | - while($data = $cursor->fetch()) { |
|
395 | + while ($data = $cursor->fetch()) { |
|
396 | 396 | $shares[] = $this->createShare($data); |
397 | 397 | } |
398 | 398 | $cursor->closeCursor(); |
@@ -431,7 +431,7 @@ discard block |
||
431 | 431 | |
432 | 432 | $cursor = $qb->execute(); |
433 | 433 | |
434 | - while($data = $cursor->fetch()) { |
|
434 | + while ($data = $cursor->fetch()) { |
|
435 | 435 | $shares[] = $this->createShare($data); |
436 | 436 | } |
437 | 437 | $cursor->closeCursor(); |
@@ -508,15 +508,15 @@ discard block |
||
508 | 508 | private function createShare($data) { |
509 | 509 | |
510 | 510 | $share = new Share($this->rootFolder); |
511 | - $share->setId((int)$data['id']) |
|
512 | - ->setShareType((int)$data['share_type']) |
|
513 | - ->setPermissions((int)$data['permissions']) |
|
511 | + $share->setId((int) $data['id']) |
|
512 | + ->setShareType((int) $data['share_type']) |
|
513 | + ->setPermissions((int) $data['permissions']) |
|
514 | 514 | ->setTarget($data['file_target']) |
515 | - ->setMailSend((bool)$data['mail_send']) |
|
515 | + ->setMailSend((bool) $data['mail_send']) |
|
516 | 516 | ->setToken($data['token']); |
517 | 517 | |
518 | 518 | $shareTime = new \DateTime(); |
519 | - $shareTime->setTimestamp((int)$data['stime']); |
|
519 | + $shareTime->setTimestamp((int) $data['stime']); |
|
520 | 520 | $share->setShareTime($shareTime); |
521 | 521 | $share->setSharedWith($data['share_with']); |
522 | 522 | |
@@ -526,13 +526,13 @@ discard block |
||
526 | 526 | } else { |
527 | 527 | //OLD SHARE |
528 | 528 | $share->setSharedBy($data['uid_owner']); |
529 | - $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']); |
|
529 | + $path = $this->getNode($share->getSharedBy(), (int) $data['file_source']); |
|
530 | 530 | |
531 | 531 | $owner = $path->getOwner(); |
532 | 532 | $share->setShareOwner($owner->getUID()); |
533 | 533 | } |
534 | 534 | |
535 | - $share->setNodeId((int)$data['file_source']); |
|
535 | + $share->setNodeId((int) $data['file_source']); |
|
536 | 536 | $share->setNodeType($data['item_type']); |
537 | 537 | |
538 | 538 | $share->setProviderId($this->identifier()); |
@@ -22,7 +22,6 @@ |
||
22 | 22 | */ |
23 | 23 | namespace OCA\Federation\Command; |
24 | 24 | |
25 | -use OCA\Federation\DbHandler; |
|
26 | 25 | use Symfony\Component\Console\Command\Command; |
27 | 26 | use Symfony\Component\Console\Helper\ProgressBar; |
28 | 27 | use Symfony\Component\Console\Input\InputInterface; |
@@ -31,45 +31,45 @@ |
||
31 | 31 | |
32 | 32 | class SyncFederationAddressBooks extends Command { |
33 | 33 | |
34 | - /** @var \OCA\Federation\SyncFederationAddressBooks */ |
|
35 | - private $syncService; |
|
34 | + /** @var \OCA\Federation\SyncFederationAddressBooks */ |
|
35 | + private $syncService; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param \OCA\Federation\SyncFederationAddressBooks $syncService |
|
39 | - */ |
|
40 | - function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) { |
|
41 | - parent::__construct(); |
|
37 | + /** |
|
38 | + * @param \OCA\Federation\SyncFederationAddressBooks $syncService |
|
39 | + */ |
|
40 | + function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) { |
|
41 | + parent::__construct(); |
|
42 | 42 | |
43 | - $this->syncService = $syncService; |
|
44 | - } |
|
43 | + $this->syncService = $syncService; |
|
44 | + } |
|
45 | 45 | |
46 | - protected function configure() { |
|
47 | - $this |
|
48 | - ->setName('federation:sync-addressbooks') |
|
49 | - ->setDescription('Synchronizes addressbooks of all federated clouds'); |
|
50 | - } |
|
46 | + protected function configure() { |
|
47 | + $this |
|
48 | + ->setName('federation:sync-addressbooks') |
|
49 | + ->setDescription('Synchronizes addressbooks of all federated clouds'); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param InputInterface $input |
|
54 | - * @param OutputInterface $output |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
52 | + /** |
|
53 | + * @param InputInterface $input |
|
54 | + * @param OutputInterface $output |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
58 | 58 | |
59 | - $progress = new ProgressBar($output); |
|
60 | - $progress->start(); |
|
61 | - $this->syncService->syncThemAll(function($url, $ex) use ($progress, $output) { |
|
62 | - if ($ex instanceof \Exception) { |
|
63 | - $output->writeln("Error while syncing $url : " . $ex->getMessage()); |
|
59 | + $progress = new ProgressBar($output); |
|
60 | + $progress->start(); |
|
61 | + $this->syncService->syncThemAll(function($url, $ex) use ($progress, $output) { |
|
62 | + if ($ex instanceof \Exception) { |
|
63 | + $output->writeln("Error while syncing $url : " . $ex->getMessage()); |
|
64 | 64 | |
65 | - } else { |
|
66 | - $progress->advance(); |
|
67 | - } |
|
68 | - }); |
|
65 | + } else { |
|
66 | + $progress->advance(); |
|
67 | + } |
|
68 | + }); |
|
69 | 69 | |
70 | - $progress->finish(); |
|
71 | - $output->writeln(''); |
|
70 | + $progress->finish(); |
|
71 | + $output->writeln(''); |
|
72 | 72 | |
73 | - return 0; |
|
74 | - } |
|
73 | + return 0; |
|
74 | + } |
|
75 | 75 | } |
@@ -60,7 +60,7 @@ |
||
60 | 60 | $progress->start(); |
61 | 61 | $this->syncService->syncThemAll(function($url, $ex) use ($progress, $output) { |
62 | 62 | if ($ex instanceof \Exception) { |
63 | - $output->writeln("Error while syncing $url : " . $ex->getMessage()); |
|
63 | + $output->writeln("Error while syncing $url : ".$ex->getMessage()); |
|
64 | 64 | |
65 | 65 | } else { |
66 | 66 | $progress->advance(); |
@@ -299,8 +299,8 @@ |
||
299 | 299 | } |
300 | 300 | |
301 | 301 | /** |
302 | - * @param $username |
|
303 | - * @param $password |
|
302 | + * @param string $username |
|
303 | + * @param string $password |
|
304 | 304 | * @return bool |
305 | 305 | */ |
306 | 306 | public function auth($username, $password) { |
@@ -41,280 +41,280 @@ |
||
41 | 41 | */ |
42 | 42 | class DbHandler { |
43 | 43 | |
44 | - /** @var IDBConnection */ |
|
45 | - private $connection; |
|
46 | - |
|
47 | - /** @var IL10N */ |
|
48 | - private $l; |
|
49 | - |
|
50 | - /** @var string */ |
|
51 | - private $dbTable = 'trusted_servers'; |
|
52 | - |
|
53 | - /** |
|
54 | - * @param IDBConnection $connection |
|
55 | - * @param IL10N $il10n |
|
56 | - */ |
|
57 | - public function __construct( |
|
58 | - IDBConnection $connection, |
|
59 | - IL10N $il10n |
|
60 | - ) { |
|
61 | - $this->connection = $connection; |
|
62 | - $this->IL10N = $il10n; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * add server to the list of trusted Nextcloud servers |
|
67 | - * |
|
68 | - * @param string $url |
|
69 | - * @return int |
|
70 | - * @throws HintException |
|
71 | - */ |
|
72 | - public function addServer($url) { |
|
73 | - $hash = $this->hash($url); |
|
74 | - $url = rtrim($url, '/'); |
|
75 | - $query = $this->connection->getQueryBuilder(); |
|
76 | - $query->insert($this->dbTable) |
|
77 | - ->values( |
|
78 | - [ |
|
79 | - 'url' => $query->createParameter('url'), |
|
80 | - 'url_hash' => $query->createParameter('url_hash'), |
|
81 | - ] |
|
82 | - ) |
|
83 | - ->setParameter('url', $url) |
|
84 | - ->setParameter('url_hash', $hash); |
|
85 | - |
|
86 | - $result = $query->execute(); |
|
87 | - |
|
88 | - if ($result) { |
|
89 | - return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
90 | - } else { |
|
91 | - $message = 'Internal failure, Could not add Nextcloud as trusted server: ' . $url; |
|
92 | - $message_t = $this->l->t('Could not add server'); |
|
93 | - throw new HintException($message, $message_t); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * remove server from the list of trusted Nextcloud servers |
|
99 | - * |
|
100 | - * @param int $id |
|
101 | - */ |
|
102 | - public function removeServer($id) { |
|
103 | - $query = $this->connection->getQueryBuilder(); |
|
104 | - $query->delete($this->dbTable) |
|
105 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
106 | - ->setParameter('id', $id); |
|
107 | - $query->execute(); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * get trusted server with given ID |
|
112 | - * |
|
113 | - * @param int $id |
|
114 | - * @return array |
|
115 | - * @throws \Exception |
|
116 | - */ |
|
117 | - public function getServerById($id) { |
|
118 | - $query = $this->connection->getQueryBuilder(); |
|
119 | - $query->select('*')->from($this->dbTable) |
|
120 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
121 | - ->setParameter('id', $id); |
|
122 | - $query->execute(); |
|
123 | - $result = $query->execute()->fetchAll(); |
|
124 | - |
|
125 | - if (empty($result)) { |
|
126 | - throw new \Exception('No Server found with ID: ' . $id); |
|
127 | - } |
|
128 | - |
|
129 | - return $result[0]; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * get all trusted servers |
|
134 | - * |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - public function getAllServer() { |
|
138 | - $query = $this->connection->getQueryBuilder(); |
|
139 | - $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])->from($this->dbTable); |
|
140 | - $result = $query->execute()->fetchAll(); |
|
141 | - return $result; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * check if server already exists in the database table |
|
146 | - * |
|
147 | - * @param string $url |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - public function serverExists($url) { |
|
151 | - $hash = $this->hash($url); |
|
152 | - $query = $this->connection->getQueryBuilder(); |
|
153 | - $query->select('url')->from($this->dbTable) |
|
154 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
155 | - ->setParameter('url_hash', $hash); |
|
156 | - $result = $query->execute()->fetchAll(); |
|
157 | - |
|
158 | - return !empty($result); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * write token to database. Token is used to exchange the secret |
|
163 | - * |
|
164 | - * @param string $url |
|
165 | - * @param string $token |
|
166 | - */ |
|
167 | - public function addToken($url, $token) { |
|
168 | - $hash = $this->hash($url); |
|
169 | - $query = $this->connection->getQueryBuilder(); |
|
170 | - $query->update($this->dbTable) |
|
171 | - ->set('token', $query->createParameter('token')) |
|
172 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
173 | - ->setParameter('url_hash', $hash) |
|
174 | - ->setParameter('token', $token); |
|
175 | - $query->execute(); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * get token stored in database |
|
180 | - * |
|
181 | - * @param string $url |
|
182 | - * @return string |
|
183 | - * @throws \Exception |
|
184 | - */ |
|
185 | - public function getToken($url) { |
|
186 | - $hash = $this->hash($url); |
|
187 | - $query = $this->connection->getQueryBuilder(); |
|
188 | - $query->select('token')->from($this->dbTable) |
|
189 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
190 | - ->setParameter('url_hash', $hash); |
|
191 | - |
|
192 | - $result = $query->execute()->fetch(); |
|
193 | - |
|
194 | - if (!isset($result['token'])) { |
|
195 | - throw new \Exception('No token found for: ' . $url); |
|
196 | - } |
|
197 | - |
|
198 | - return $result['token']; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * add shared Secret to database |
|
203 | - * |
|
204 | - * @param string $url |
|
205 | - * @param string $sharedSecret |
|
206 | - */ |
|
207 | - public function addSharedSecret($url, $sharedSecret) { |
|
208 | - $hash = $this->hash($url); |
|
209 | - $query = $this->connection->getQueryBuilder(); |
|
210 | - $query->update($this->dbTable) |
|
211 | - ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
212 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
213 | - ->setParameter('url_hash', $hash) |
|
214 | - ->setParameter('sharedSecret', $sharedSecret); |
|
215 | - $query->execute(); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * get shared secret from database |
|
220 | - * |
|
221 | - * @param string $url |
|
222 | - * @return string |
|
223 | - */ |
|
224 | - public function getSharedSecret($url) { |
|
225 | - $hash = $this->hash($url); |
|
226 | - $query = $this->connection->getQueryBuilder(); |
|
227 | - $query->select('shared_secret')->from($this->dbTable) |
|
228 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
229 | - ->setParameter('url_hash', $hash); |
|
230 | - |
|
231 | - $result = $query->execute()->fetch(); |
|
232 | - return $result['shared_secret']; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * set server status |
|
237 | - * |
|
238 | - * @param string $url |
|
239 | - * @param int $status |
|
240 | - * @param string|null $token |
|
241 | - */ |
|
242 | - public function setServerStatus($url, $status, $token = null) { |
|
243 | - $hash = $this->hash($url); |
|
244 | - $query = $this->connection->getQueryBuilder(); |
|
245 | - $query->update($this->dbTable) |
|
246 | - ->set('status', $query->createNamedParameter($status)) |
|
247 | - ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
248 | - if (!is_null($token)) { |
|
249 | - $query->set('sync_token', $query->createNamedParameter($token)); |
|
250 | - } |
|
251 | - $query->execute(); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * get server status |
|
256 | - * |
|
257 | - * @param string $url |
|
258 | - * @return int |
|
259 | - */ |
|
260 | - public function getServerStatus($url) { |
|
261 | - $hash = $this->hash($url); |
|
262 | - $query = $this->connection->getQueryBuilder(); |
|
263 | - $query->select('status')->from($this->dbTable) |
|
264 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
265 | - ->setParameter('url_hash', $hash); |
|
266 | - |
|
267 | - $result = $query->execute()->fetch(); |
|
268 | - return (int)$result['status']; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * create hash from URL |
|
273 | - * |
|
274 | - * @param string $url |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - protected function hash($url) { |
|
278 | - $normalized = $this->normalizeUrl($url); |
|
279 | - return sha1($normalized); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * normalize URL, used to create the sha1 hash |
|
284 | - * |
|
285 | - * @param string $url |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - protected function normalizeUrl($url) { |
|
289 | - $normalized = $url; |
|
290 | - |
|
291 | - if (strpos($url, 'https://') === 0) { |
|
292 | - $normalized = substr($url, strlen('https://')); |
|
293 | - } else if (strpos($url, 'http://') === 0) { |
|
294 | - $normalized = substr($url, strlen('http://')); |
|
295 | - } |
|
296 | - |
|
297 | - $normalized = Filesystem::normalizePath($normalized); |
|
298 | - $normalized = trim($normalized, '/'); |
|
299 | - |
|
300 | - return $normalized; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * @param $username |
|
305 | - * @param $password |
|
306 | - * @return bool |
|
307 | - */ |
|
308 | - public function auth($username, $password) { |
|
309 | - if ($username !== 'system') { |
|
310 | - return false; |
|
311 | - } |
|
312 | - $query = $this->connection->getQueryBuilder(); |
|
313 | - $query->select('url')->from($this->dbTable) |
|
314 | - ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
315 | - |
|
316 | - $result = $query->execute()->fetch(); |
|
317 | - return !empty($result); |
|
318 | - } |
|
44 | + /** @var IDBConnection */ |
|
45 | + private $connection; |
|
46 | + |
|
47 | + /** @var IL10N */ |
|
48 | + private $l; |
|
49 | + |
|
50 | + /** @var string */ |
|
51 | + private $dbTable = 'trusted_servers'; |
|
52 | + |
|
53 | + /** |
|
54 | + * @param IDBConnection $connection |
|
55 | + * @param IL10N $il10n |
|
56 | + */ |
|
57 | + public function __construct( |
|
58 | + IDBConnection $connection, |
|
59 | + IL10N $il10n |
|
60 | + ) { |
|
61 | + $this->connection = $connection; |
|
62 | + $this->IL10N = $il10n; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * add server to the list of trusted Nextcloud servers |
|
67 | + * |
|
68 | + * @param string $url |
|
69 | + * @return int |
|
70 | + * @throws HintException |
|
71 | + */ |
|
72 | + public function addServer($url) { |
|
73 | + $hash = $this->hash($url); |
|
74 | + $url = rtrim($url, '/'); |
|
75 | + $query = $this->connection->getQueryBuilder(); |
|
76 | + $query->insert($this->dbTable) |
|
77 | + ->values( |
|
78 | + [ |
|
79 | + 'url' => $query->createParameter('url'), |
|
80 | + 'url_hash' => $query->createParameter('url_hash'), |
|
81 | + ] |
|
82 | + ) |
|
83 | + ->setParameter('url', $url) |
|
84 | + ->setParameter('url_hash', $hash); |
|
85 | + |
|
86 | + $result = $query->execute(); |
|
87 | + |
|
88 | + if ($result) { |
|
89 | + return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
90 | + } else { |
|
91 | + $message = 'Internal failure, Could not add Nextcloud as trusted server: ' . $url; |
|
92 | + $message_t = $this->l->t('Could not add server'); |
|
93 | + throw new HintException($message, $message_t); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * remove server from the list of trusted Nextcloud servers |
|
99 | + * |
|
100 | + * @param int $id |
|
101 | + */ |
|
102 | + public function removeServer($id) { |
|
103 | + $query = $this->connection->getQueryBuilder(); |
|
104 | + $query->delete($this->dbTable) |
|
105 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
106 | + ->setParameter('id', $id); |
|
107 | + $query->execute(); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * get trusted server with given ID |
|
112 | + * |
|
113 | + * @param int $id |
|
114 | + * @return array |
|
115 | + * @throws \Exception |
|
116 | + */ |
|
117 | + public function getServerById($id) { |
|
118 | + $query = $this->connection->getQueryBuilder(); |
|
119 | + $query->select('*')->from($this->dbTable) |
|
120 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
121 | + ->setParameter('id', $id); |
|
122 | + $query->execute(); |
|
123 | + $result = $query->execute()->fetchAll(); |
|
124 | + |
|
125 | + if (empty($result)) { |
|
126 | + throw new \Exception('No Server found with ID: ' . $id); |
|
127 | + } |
|
128 | + |
|
129 | + return $result[0]; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * get all trusted servers |
|
134 | + * |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + public function getAllServer() { |
|
138 | + $query = $this->connection->getQueryBuilder(); |
|
139 | + $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])->from($this->dbTable); |
|
140 | + $result = $query->execute()->fetchAll(); |
|
141 | + return $result; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * check if server already exists in the database table |
|
146 | + * |
|
147 | + * @param string $url |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + public function serverExists($url) { |
|
151 | + $hash = $this->hash($url); |
|
152 | + $query = $this->connection->getQueryBuilder(); |
|
153 | + $query->select('url')->from($this->dbTable) |
|
154 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
155 | + ->setParameter('url_hash', $hash); |
|
156 | + $result = $query->execute()->fetchAll(); |
|
157 | + |
|
158 | + return !empty($result); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * write token to database. Token is used to exchange the secret |
|
163 | + * |
|
164 | + * @param string $url |
|
165 | + * @param string $token |
|
166 | + */ |
|
167 | + public function addToken($url, $token) { |
|
168 | + $hash = $this->hash($url); |
|
169 | + $query = $this->connection->getQueryBuilder(); |
|
170 | + $query->update($this->dbTable) |
|
171 | + ->set('token', $query->createParameter('token')) |
|
172 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
173 | + ->setParameter('url_hash', $hash) |
|
174 | + ->setParameter('token', $token); |
|
175 | + $query->execute(); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * get token stored in database |
|
180 | + * |
|
181 | + * @param string $url |
|
182 | + * @return string |
|
183 | + * @throws \Exception |
|
184 | + */ |
|
185 | + public function getToken($url) { |
|
186 | + $hash = $this->hash($url); |
|
187 | + $query = $this->connection->getQueryBuilder(); |
|
188 | + $query->select('token')->from($this->dbTable) |
|
189 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
190 | + ->setParameter('url_hash', $hash); |
|
191 | + |
|
192 | + $result = $query->execute()->fetch(); |
|
193 | + |
|
194 | + if (!isset($result['token'])) { |
|
195 | + throw new \Exception('No token found for: ' . $url); |
|
196 | + } |
|
197 | + |
|
198 | + return $result['token']; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * add shared Secret to database |
|
203 | + * |
|
204 | + * @param string $url |
|
205 | + * @param string $sharedSecret |
|
206 | + */ |
|
207 | + public function addSharedSecret($url, $sharedSecret) { |
|
208 | + $hash = $this->hash($url); |
|
209 | + $query = $this->connection->getQueryBuilder(); |
|
210 | + $query->update($this->dbTable) |
|
211 | + ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
212 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
213 | + ->setParameter('url_hash', $hash) |
|
214 | + ->setParameter('sharedSecret', $sharedSecret); |
|
215 | + $query->execute(); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * get shared secret from database |
|
220 | + * |
|
221 | + * @param string $url |
|
222 | + * @return string |
|
223 | + */ |
|
224 | + public function getSharedSecret($url) { |
|
225 | + $hash = $this->hash($url); |
|
226 | + $query = $this->connection->getQueryBuilder(); |
|
227 | + $query->select('shared_secret')->from($this->dbTable) |
|
228 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
229 | + ->setParameter('url_hash', $hash); |
|
230 | + |
|
231 | + $result = $query->execute()->fetch(); |
|
232 | + return $result['shared_secret']; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * set server status |
|
237 | + * |
|
238 | + * @param string $url |
|
239 | + * @param int $status |
|
240 | + * @param string|null $token |
|
241 | + */ |
|
242 | + public function setServerStatus($url, $status, $token = null) { |
|
243 | + $hash = $this->hash($url); |
|
244 | + $query = $this->connection->getQueryBuilder(); |
|
245 | + $query->update($this->dbTable) |
|
246 | + ->set('status', $query->createNamedParameter($status)) |
|
247 | + ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
248 | + if (!is_null($token)) { |
|
249 | + $query->set('sync_token', $query->createNamedParameter($token)); |
|
250 | + } |
|
251 | + $query->execute(); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * get server status |
|
256 | + * |
|
257 | + * @param string $url |
|
258 | + * @return int |
|
259 | + */ |
|
260 | + public function getServerStatus($url) { |
|
261 | + $hash = $this->hash($url); |
|
262 | + $query = $this->connection->getQueryBuilder(); |
|
263 | + $query->select('status')->from($this->dbTable) |
|
264 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
265 | + ->setParameter('url_hash', $hash); |
|
266 | + |
|
267 | + $result = $query->execute()->fetch(); |
|
268 | + return (int)$result['status']; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * create hash from URL |
|
273 | + * |
|
274 | + * @param string $url |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + protected function hash($url) { |
|
278 | + $normalized = $this->normalizeUrl($url); |
|
279 | + return sha1($normalized); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * normalize URL, used to create the sha1 hash |
|
284 | + * |
|
285 | + * @param string $url |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + protected function normalizeUrl($url) { |
|
289 | + $normalized = $url; |
|
290 | + |
|
291 | + if (strpos($url, 'https://') === 0) { |
|
292 | + $normalized = substr($url, strlen('https://')); |
|
293 | + } else if (strpos($url, 'http://') === 0) { |
|
294 | + $normalized = substr($url, strlen('http://')); |
|
295 | + } |
|
296 | + |
|
297 | + $normalized = Filesystem::normalizePath($normalized); |
|
298 | + $normalized = trim($normalized, '/'); |
|
299 | + |
|
300 | + return $normalized; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * @param $username |
|
305 | + * @param $password |
|
306 | + * @return bool |
|
307 | + */ |
|
308 | + public function auth($username, $password) { |
|
309 | + if ($username !== 'system') { |
|
310 | + return false; |
|
311 | + } |
|
312 | + $query = $this->connection->getQueryBuilder(); |
|
313 | + $query->select('url')->from($this->dbTable) |
|
314 | + ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
315 | + |
|
316 | + $result = $query->execute()->fetch(); |
|
317 | + return !empty($result); |
|
318 | + } |
|
319 | 319 | |
320 | 320 | } |
@@ -86,9 +86,9 @@ discard block |
||
86 | 86 | $result = $query->execute(); |
87 | 87 | |
88 | 88 | if ($result) { |
89 | - return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
89 | + return (int) $this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
90 | 90 | } else { |
91 | - $message = 'Internal failure, Could not add Nextcloud as trusted server: ' . $url; |
|
91 | + $message = 'Internal failure, Could not add Nextcloud as trusted server: '.$url; |
|
92 | 92 | $message_t = $this->l->t('Could not add server'); |
93 | 93 | throw new HintException($message, $message_t); |
94 | 94 | } |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $result = $query->execute()->fetchAll(); |
124 | 124 | |
125 | 125 | if (empty($result)) { |
126 | - throw new \Exception('No Server found with ID: ' . $id); |
|
126 | + throw new \Exception('No Server found with ID: '.$id); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | return $result[0]; |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | $result = $query->execute()->fetch(); |
193 | 193 | |
194 | 194 | if (!isset($result['token'])) { |
195 | - throw new \Exception('No token found for: ' . $url); |
|
195 | + throw new \Exception('No token found for: '.$url); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | return $result['token']; |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | ->setParameter('url_hash', $hash); |
266 | 266 | |
267 | 267 | $result = $query->execute()->fetch(); |
268 | - return (int)$result['status']; |
|
268 | + return (int) $result['status']; |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | /** |
@@ -24,10 +24,6 @@ |
||
24 | 24 | |
25 | 25 | use OCA\DAV\CardDAV\SyncService; |
26 | 26 | use OCP\AppFramework\Http; |
27 | -use Symfony\Component\Console\Command\Command; |
|
28 | -use Symfony\Component\Console\Helper\ProgressBar; |
|
29 | -use Symfony\Component\Console\Input\InputInterface; |
|
30 | -use Symfony\Component\Console\Output\OutputInterface; |
|
31 | 27 | |
32 | 28 | class SyncFederationAddressBooks { |
33 | 29 |
@@ -32,52 +32,52 @@ |
||
32 | 32 | |
33 | 33 | class SyncFederationAddressBooks { |
34 | 34 | |
35 | - /** @var DbHandler */ |
|
36 | - protected $dbHandler; |
|
35 | + /** @var DbHandler */ |
|
36 | + protected $dbHandler; |
|
37 | 37 | |
38 | - /** @var SyncService */ |
|
39 | - private $syncService; |
|
38 | + /** @var SyncService */ |
|
39 | + private $syncService; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param DbHandler $dbHandler |
|
43 | - * @param SyncService $syncService |
|
44 | - */ |
|
45 | - function __construct(DbHandler $dbHandler, SyncService $syncService) { |
|
46 | - $this->syncService = $syncService; |
|
47 | - $this->dbHandler = $dbHandler; |
|
48 | - } |
|
41 | + /** |
|
42 | + * @param DbHandler $dbHandler |
|
43 | + * @param SyncService $syncService |
|
44 | + */ |
|
45 | + function __construct(DbHandler $dbHandler, SyncService $syncService) { |
|
46 | + $this->syncService = $syncService; |
|
47 | + $this->dbHandler = $dbHandler; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param \Closure $callback |
|
52 | - */ |
|
53 | - public function syncThemAll(\Closure $callback) { |
|
50 | + /** |
|
51 | + * @param \Closure $callback |
|
52 | + */ |
|
53 | + public function syncThemAll(\Closure $callback) { |
|
54 | 54 | |
55 | - $trustedServers = $this->dbHandler->getAllServer(); |
|
56 | - foreach ($trustedServers as $trustedServer) { |
|
57 | - $url = $trustedServer['url']; |
|
58 | - $callback($url, null); |
|
59 | - $sharedSecret = $trustedServer['shared_secret']; |
|
60 | - $syncToken = $trustedServer['sync_token']; |
|
55 | + $trustedServers = $this->dbHandler->getAllServer(); |
|
56 | + foreach ($trustedServers as $trustedServer) { |
|
57 | + $url = $trustedServer['url']; |
|
58 | + $callback($url, null); |
|
59 | + $sharedSecret = $trustedServer['shared_secret']; |
|
60 | + $syncToken = $trustedServer['sync_token']; |
|
61 | 61 | |
62 | - if (is_null($sharedSecret)) { |
|
63 | - continue; |
|
64 | - } |
|
65 | - $targetBookId = $trustedServer['url_hash']; |
|
66 | - $targetPrincipal = "principals/system/system"; |
|
67 | - $targetBookProperties = [ |
|
68 | - '{DAV:}displayname' => $url |
|
69 | - ]; |
|
70 | - try { |
|
71 | - $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); |
|
72 | - if ($newToken !== $syncToken) { |
|
73 | - $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); |
|
74 | - } |
|
75 | - } catch (\Exception $ex) { |
|
76 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
77 | - $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED); |
|
78 | - } |
|
79 | - $callback($url, $ex); |
|
80 | - } |
|
81 | - } |
|
82 | - } |
|
62 | + if (is_null($sharedSecret)) { |
|
63 | + continue; |
|
64 | + } |
|
65 | + $targetBookId = $trustedServer['url_hash']; |
|
66 | + $targetPrincipal = "principals/system/system"; |
|
67 | + $targetBookProperties = [ |
|
68 | + '{DAV:}displayname' => $url |
|
69 | + ]; |
|
70 | + try { |
|
71 | + $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); |
|
72 | + if ($newToken !== $syncToken) { |
|
73 | + $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); |
|
74 | + } |
|
75 | + } catch (\Exception $ex) { |
|
76 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
77 | + $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED); |
|
78 | + } |
|
79 | + $callback($url, $ex); |
|
80 | + } |
|
81 | + } |
|
82 | + } |
|
83 | 83 | } |
@@ -35,7 +35,6 @@ |
||
35 | 35 | use OCA\Files\Service\TagService; |
36 | 36 | use OCP\IPreview; |
37 | 37 | use OCP\Share\IManager; |
38 | -use OCP\Files\FileInfo; |
|
39 | 38 | use OCP\Files\Node; |
40 | 39 | use OCP\IUserSession; |
41 | 40 |
@@ -46,156 +46,156 @@ |
||
46 | 46 | * @package OCA\Files\Controller |
47 | 47 | */ |
48 | 48 | class ApiController extends Controller { |
49 | - /** @var TagService */ |
|
50 | - private $tagService; |
|
51 | - /** @var IManager **/ |
|
52 | - private $shareManager; |
|
53 | - /** @var IPreview */ |
|
54 | - private $previewManager; |
|
55 | - /** IUserSession */ |
|
56 | - private $userSession; |
|
49 | + /** @var TagService */ |
|
50 | + private $tagService; |
|
51 | + /** @var IManager **/ |
|
52 | + private $shareManager; |
|
53 | + /** @var IPreview */ |
|
54 | + private $previewManager; |
|
55 | + /** IUserSession */ |
|
56 | + private $userSession; |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param string $appName |
|
60 | - * @param IRequest $request |
|
61 | - * @param TagService $tagService |
|
62 | - * @param IPreview $previewManager |
|
63 | - */ |
|
64 | - public function __construct($appName, |
|
65 | - IRequest $request, |
|
66 | - IUserSession $userSession, |
|
67 | - TagService $tagService, |
|
68 | - IPreview $previewManager, |
|
69 | - IManager $shareManager) { |
|
70 | - parent::__construct($appName, $request); |
|
71 | - $this->userSession = $userSession; |
|
72 | - $this->tagService = $tagService; |
|
73 | - $this->previewManager = $previewManager; |
|
74 | - $this->shareManager = $shareManager; |
|
75 | - } |
|
58 | + /** |
|
59 | + * @param string $appName |
|
60 | + * @param IRequest $request |
|
61 | + * @param TagService $tagService |
|
62 | + * @param IPreview $previewManager |
|
63 | + */ |
|
64 | + public function __construct($appName, |
|
65 | + IRequest $request, |
|
66 | + IUserSession $userSession, |
|
67 | + TagService $tagService, |
|
68 | + IPreview $previewManager, |
|
69 | + IManager $shareManager) { |
|
70 | + parent::__construct($appName, $request); |
|
71 | + $this->userSession = $userSession; |
|
72 | + $this->tagService = $tagService; |
|
73 | + $this->previewManager = $previewManager; |
|
74 | + $this->shareManager = $shareManager; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Gets a thumbnail of the specified file |
|
79 | - * |
|
80 | - * @since API version 1.0 |
|
81 | - * |
|
82 | - * @NoAdminRequired |
|
83 | - * @NoCSRFRequired |
|
84 | - * @StrictCookieRequired |
|
85 | - * |
|
86 | - * @param int $x |
|
87 | - * @param int $y |
|
88 | - * @param string $file URL-encoded filename |
|
89 | - * @return DataResponse|DataDisplayResponse |
|
90 | - */ |
|
91 | - public function getThumbnail($x, $y, $file) { |
|
92 | - if($x < 1 || $y < 1) { |
|
93 | - return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); |
|
94 | - } |
|
77 | + /** |
|
78 | + * Gets a thumbnail of the specified file |
|
79 | + * |
|
80 | + * @since API version 1.0 |
|
81 | + * |
|
82 | + * @NoAdminRequired |
|
83 | + * @NoCSRFRequired |
|
84 | + * @StrictCookieRequired |
|
85 | + * |
|
86 | + * @param int $x |
|
87 | + * @param int $y |
|
88 | + * @param string $file URL-encoded filename |
|
89 | + * @return DataResponse|DataDisplayResponse |
|
90 | + */ |
|
91 | + public function getThumbnail($x, $y, $file) { |
|
92 | + if($x < 1 || $y < 1) { |
|
93 | + return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); |
|
94 | + } |
|
95 | 95 | |
96 | - $preview = $this->previewManager->createPreview('files/'.$file, $x, $y, true); |
|
97 | - if ($preview->valid()) { |
|
98 | - return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']); |
|
99 | - } else { |
|
100 | - return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); |
|
101 | - } |
|
102 | - } |
|
96 | + $preview = $this->previewManager->createPreview('files/'.$file, $x, $y, true); |
|
97 | + if ($preview->valid()) { |
|
98 | + return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']); |
|
99 | + } else { |
|
100 | + return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Updates the info of the specified file path |
|
106 | - * The passed tags are absolute, which means they will |
|
107 | - * replace the actual tag selection. |
|
108 | - * |
|
109 | - * @NoAdminRequired |
|
110 | - * |
|
111 | - * @param string $path path |
|
112 | - * @param array|string $tags array of tags |
|
113 | - * @return DataResponse |
|
114 | - */ |
|
115 | - public function updateFileTags($path, $tags = null) { |
|
116 | - $result = []; |
|
117 | - // if tags specified or empty array, update tags |
|
118 | - if (!is_null($tags)) { |
|
119 | - try { |
|
120 | - $this->tagService->updateFileTags($path, $tags); |
|
121 | - } catch (\OCP\Files\NotFoundException $e) { |
|
122 | - return new DataResponse([ |
|
123 | - 'message' => $e->getMessage() |
|
124 | - ], Http::STATUS_NOT_FOUND); |
|
125 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
126 | - return new DataResponse([ |
|
127 | - 'message' => $e->getMessage() |
|
128 | - ], Http::STATUS_SERVICE_UNAVAILABLE); |
|
129 | - } catch (\Exception $e) { |
|
130 | - return new DataResponse([ |
|
131 | - 'message' => $e->getMessage() |
|
132 | - ], Http::STATUS_NOT_FOUND); |
|
133 | - } |
|
134 | - $result['tags'] = $tags; |
|
135 | - } |
|
136 | - return new DataResponse($result); |
|
137 | - } |
|
104 | + /** |
|
105 | + * Updates the info of the specified file path |
|
106 | + * The passed tags are absolute, which means they will |
|
107 | + * replace the actual tag selection. |
|
108 | + * |
|
109 | + * @NoAdminRequired |
|
110 | + * |
|
111 | + * @param string $path path |
|
112 | + * @param array|string $tags array of tags |
|
113 | + * @return DataResponse |
|
114 | + */ |
|
115 | + public function updateFileTags($path, $tags = null) { |
|
116 | + $result = []; |
|
117 | + // if tags specified or empty array, update tags |
|
118 | + if (!is_null($tags)) { |
|
119 | + try { |
|
120 | + $this->tagService->updateFileTags($path, $tags); |
|
121 | + } catch (\OCP\Files\NotFoundException $e) { |
|
122 | + return new DataResponse([ |
|
123 | + 'message' => $e->getMessage() |
|
124 | + ], Http::STATUS_NOT_FOUND); |
|
125 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
126 | + return new DataResponse([ |
|
127 | + 'message' => $e->getMessage() |
|
128 | + ], Http::STATUS_SERVICE_UNAVAILABLE); |
|
129 | + } catch (\Exception $e) { |
|
130 | + return new DataResponse([ |
|
131 | + 'message' => $e->getMessage() |
|
132 | + ], Http::STATUS_NOT_FOUND); |
|
133 | + } |
|
134 | + $result['tags'] = $tags; |
|
135 | + } |
|
136 | + return new DataResponse($result); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Returns a list of all files tagged with the given tag. |
|
141 | - * |
|
142 | - * @NoAdminRequired |
|
143 | - * |
|
144 | - * @param string $tagName tag name to filter by |
|
145 | - * @return DataResponse |
|
146 | - */ |
|
147 | - public function getFilesByTag($tagName) { |
|
148 | - $files = array(); |
|
149 | - $nodes = $this->tagService->getFilesByTag($tagName); |
|
150 | - foreach ($nodes as &$node) { |
|
151 | - $shareTypes = $this->getShareTypes($node); |
|
152 | - $fileInfo = $node->getFileInfo(); |
|
153 | - $file = \OCA\Files\Helper::formatFileInfo($fileInfo); |
|
154 | - $parts = explode('/', dirname($fileInfo->getPath()), 4); |
|
155 | - if(isset($parts[3])) { |
|
156 | - $file['path'] = '/' . $parts[3]; |
|
157 | - } else { |
|
158 | - $file['path'] = '/'; |
|
159 | - } |
|
160 | - $file['tags'] = [$tagName]; |
|
161 | - if (!empty($shareTypes)) { |
|
162 | - $file['shareTypes'] = $shareTypes; |
|
163 | - } |
|
164 | - $files[] = $file; |
|
165 | - } |
|
166 | - return new DataResponse(['files' => $files]); |
|
167 | - } |
|
139 | + /** |
|
140 | + * Returns a list of all files tagged with the given tag. |
|
141 | + * |
|
142 | + * @NoAdminRequired |
|
143 | + * |
|
144 | + * @param string $tagName tag name to filter by |
|
145 | + * @return DataResponse |
|
146 | + */ |
|
147 | + public function getFilesByTag($tagName) { |
|
148 | + $files = array(); |
|
149 | + $nodes = $this->tagService->getFilesByTag($tagName); |
|
150 | + foreach ($nodes as &$node) { |
|
151 | + $shareTypes = $this->getShareTypes($node); |
|
152 | + $fileInfo = $node->getFileInfo(); |
|
153 | + $file = \OCA\Files\Helper::formatFileInfo($fileInfo); |
|
154 | + $parts = explode('/', dirname($fileInfo->getPath()), 4); |
|
155 | + if(isset($parts[3])) { |
|
156 | + $file['path'] = '/' . $parts[3]; |
|
157 | + } else { |
|
158 | + $file['path'] = '/'; |
|
159 | + } |
|
160 | + $file['tags'] = [$tagName]; |
|
161 | + if (!empty($shareTypes)) { |
|
162 | + $file['shareTypes'] = $shareTypes; |
|
163 | + } |
|
164 | + $files[] = $file; |
|
165 | + } |
|
166 | + return new DataResponse(['files' => $files]); |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Return a list of share types for outgoing shares |
|
171 | - * |
|
172 | - * @param Node $node file node |
|
173 | - * |
|
174 | - * @return int[] array of share types |
|
175 | - */ |
|
176 | - private function getShareTypes(Node $node) { |
|
177 | - $userId = $this->userSession->getUser()->getUID(); |
|
178 | - $shareTypes = []; |
|
179 | - $requestedShareTypes = [ |
|
180 | - \OCP\Share::SHARE_TYPE_USER, |
|
181 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
182 | - \OCP\Share::SHARE_TYPE_LINK, |
|
183 | - \OCP\Share::SHARE_TYPE_REMOTE |
|
184 | - ]; |
|
185 | - foreach ($requestedShareTypes as $requestedShareType) { |
|
186 | - // one of each type is enough to find out about the types |
|
187 | - $shares = $this->shareManager->getSharesBy( |
|
188 | - $userId, |
|
189 | - $requestedShareType, |
|
190 | - $node, |
|
191 | - false, |
|
192 | - 1 |
|
193 | - ); |
|
194 | - if (!empty($shares)) { |
|
195 | - $shareTypes[] = $requestedShareType; |
|
196 | - } |
|
197 | - } |
|
198 | - return $shareTypes; |
|
199 | - } |
|
169 | + /** |
|
170 | + * Return a list of share types for outgoing shares |
|
171 | + * |
|
172 | + * @param Node $node file node |
|
173 | + * |
|
174 | + * @return int[] array of share types |
|
175 | + */ |
|
176 | + private function getShareTypes(Node $node) { |
|
177 | + $userId = $this->userSession->getUser()->getUID(); |
|
178 | + $shareTypes = []; |
|
179 | + $requestedShareTypes = [ |
|
180 | + \OCP\Share::SHARE_TYPE_USER, |
|
181 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
182 | + \OCP\Share::SHARE_TYPE_LINK, |
|
183 | + \OCP\Share::SHARE_TYPE_REMOTE |
|
184 | + ]; |
|
185 | + foreach ($requestedShareTypes as $requestedShareType) { |
|
186 | + // one of each type is enough to find out about the types |
|
187 | + $shares = $this->shareManager->getSharesBy( |
|
188 | + $userId, |
|
189 | + $requestedShareType, |
|
190 | + $node, |
|
191 | + false, |
|
192 | + 1 |
|
193 | + ); |
|
194 | + if (!empty($shares)) { |
|
195 | + $shareTypes[] = $requestedShareType; |
|
196 | + } |
|
197 | + } |
|
198 | + return $shareTypes; |
|
199 | + } |
|
200 | 200 | |
201 | 201 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @return DataResponse|DataDisplayResponse |
96 | 96 | */ |
97 | 97 | public function getThumbnail($x, $y, $file) { |
98 | - if($x < 1 || $y < 1) { |
|
98 | + if ($x < 1 || $y < 1) { |
|
99 | 99 | return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); |
100 | 100 | } |
101 | 101 | |
@@ -158,8 +158,8 @@ discard block |
||
158 | 158 | $fileInfo = $node->getFileInfo(); |
159 | 159 | $file = \OCA\Files\Helper::formatFileInfo($fileInfo); |
160 | 160 | $parts = explode('/', dirname($fileInfo->getPath()), 4); |
161 | - if(isset($parts[3])) { |
|
162 | - $file['path'] = '/' . $parts[3]; |
|
161 | + if (isset($parts[3])) { |
|
162 | + $file['path'] = '/'.$parts[3]; |
|
163 | 163 | } else { |
164 | 164 | $file['path'] = '/'; |
165 | 165 | } |
@@ -24,7 +24,6 @@ |
||
24 | 24 | |
25 | 25 | namespace OCA\Files\Service; |
26 | 26 | |
27 | -use OC\Files\FileInfo; |
|
28 | 27 | use OCP\Files\Node; |
29 | 28 | |
30 | 29 | /** |
@@ -33,83 +33,83 @@ |
||
33 | 33 | */ |
34 | 34 | class TagService { |
35 | 35 | |
36 | - /** |
|
37 | - * @var \OCP\IUserSession |
|
38 | - */ |
|
39 | - private $userSession; |
|
36 | + /** |
|
37 | + * @var \OCP\IUserSession |
|
38 | + */ |
|
39 | + private $userSession; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @var \OCP\ITags |
|
43 | - */ |
|
44 | - private $tagger; |
|
41 | + /** |
|
42 | + * @var \OCP\ITags |
|
43 | + */ |
|
44 | + private $tagger; |
|
45 | 45 | |
46 | - /** |
|
47 | - * @var \OCP\Files\Folder |
|
48 | - */ |
|
49 | - private $homeFolder; |
|
46 | + /** |
|
47 | + * @var \OCP\Files\Folder |
|
48 | + */ |
|
49 | + private $homeFolder; |
|
50 | 50 | |
51 | - public function __construct( |
|
52 | - \OCP\IUserSession $userSession, |
|
53 | - \OCP\ITags $tagger, |
|
54 | - \OCP\Files\Folder $homeFolder |
|
55 | - ) { |
|
56 | - $this->userSession = $userSession; |
|
57 | - $this->tagger = $tagger; |
|
58 | - $this->homeFolder = $homeFolder; |
|
59 | - } |
|
51 | + public function __construct( |
|
52 | + \OCP\IUserSession $userSession, |
|
53 | + \OCP\ITags $tagger, |
|
54 | + \OCP\Files\Folder $homeFolder |
|
55 | + ) { |
|
56 | + $this->userSession = $userSession; |
|
57 | + $this->tagger = $tagger; |
|
58 | + $this->homeFolder = $homeFolder; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Updates the tags of the specified file path. |
|
63 | - * The passed tags are absolute, which means they will |
|
64 | - * replace the actual tag selection. |
|
65 | - * |
|
66 | - * @param string $path path |
|
67 | - * @param array $tags array of tags |
|
68 | - * @return array list of tags |
|
69 | - * @throws \OCP\Files\NotFoundException if the file does not exist |
|
70 | - */ |
|
71 | - public function updateFileTags($path, $tags) { |
|
72 | - $fileId = $this->homeFolder->get($path)->getId(); |
|
61 | + /** |
|
62 | + * Updates the tags of the specified file path. |
|
63 | + * The passed tags are absolute, which means they will |
|
64 | + * replace the actual tag selection. |
|
65 | + * |
|
66 | + * @param string $path path |
|
67 | + * @param array $tags array of tags |
|
68 | + * @return array list of tags |
|
69 | + * @throws \OCP\Files\NotFoundException if the file does not exist |
|
70 | + */ |
|
71 | + public function updateFileTags($path, $tags) { |
|
72 | + $fileId = $this->homeFolder->get($path)->getId(); |
|
73 | 73 | |
74 | - $currentTags = $this->tagger->getTagsForObjects(array($fileId)); |
|
74 | + $currentTags = $this->tagger->getTagsForObjects(array($fileId)); |
|
75 | 75 | |
76 | - if (!empty($currentTags)) { |
|
77 | - $currentTags = current($currentTags); |
|
78 | - } |
|
76 | + if (!empty($currentTags)) { |
|
77 | + $currentTags = current($currentTags); |
|
78 | + } |
|
79 | 79 | |
80 | - $newTags = array_diff($tags, $currentTags); |
|
81 | - foreach ($newTags as $tag) { |
|
82 | - $this->tagger->tagAs($fileId, $tag); |
|
83 | - } |
|
84 | - $deletedTags = array_diff($currentTags, $tags); |
|
85 | - foreach ($deletedTags as $tag) { |
|
86 | - $this->tagger->unTag($fileId, $tag); |
|
87 | - } |
|
80 | + $newTags = array_diff($tags, $currentTags); |
|
81 | + foreach ($newTags as $tag) { |
|
82 | + $this->tagger->tagAs($fileId, $tag); |
|
83 | + } |
|
84 | + $deletedTags = array_diff($currentTags, $tags); |
|
85 | + foreach ($deletedTags as $tag) { |
|
86 | + $this->tagger->unTag($fileId, $tag); |
|
87 | + } |
|
88 | 88 | |
89 | - // TODO: re-read from tagger to make sure the |
|
90 | - // list is up to date, in case of concurrent changes ? |
|
91 | - return $tags; |
|
92 | - } |
|
89 | + // TODO: re-read from tagger to make sure the |
|
90 | + // list is up to date, in case of concurrent changes ? |
|
91 | + return $tags; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get all files for the given tag |
|
96 | - * |
|
97 | - * @param string $tagName tag name to filter by |
|
98 | - * @return Node[] list of matching files |
|
99 | - * @throws \Exception if the tag does not exist |
|
100 | - */ |
|
101 | - public function getFilesByTag($tagName) { |
|
102 | - try { |
|
103 | - $fileIds = $this->tagger->getIdsForTag($tagName); |
|
104 | - } catch (\Exception $e) { |
|
105 | - return []; |
|
106 | - } |
|
94 | + /** |
|
95 | + * Get all files for the given tag |
|
96 | + * |
|
97 | + * @param string $tagName tag name to filter by |
|
98 | + * @return Node[] list of matching files |
|
99 | + * @throws \Exception if the tag does not exist |
|
100 | + */ |
|
101 | + public function getFilesByTag($tagName) { |
|
102 | + try { |
|
103 | + $fileIds = $this->tagger->getIdsForTag($tagName); |
|
104 | + } catch (\Exception $e) { |
|
105 | + return []; |
|
106 | + } |
|
107 | 107 | |
108 | - $allNodes = []; |
|
109 | - foreach ($fileIds as $fileId) { |
|
110 | - $allNodes = array_merge($allNodes, $this->homeFolder->getById((int) $fileId)); |
|
111 | - } |
|
112 | - return $allNodes; |
|
113 | - } |
|
108 | + $allNodes = []; |
|
109 | + foreach ($fileIds as $fileId) { |
|
110 | + $allNodes = array_merge($allNodes, $this->homeFolder->getById((int) $fileId)); |
|
111 | + } |
|
112 | + return $allNodes; |
|
113 | + } |
|
114 | 114 | } |
115 | 115 |