@@ -46,430 +46,430 @@ |
||
46 | 46 | use Symfony\Component\EventDispatcher\GenericEvent; |
47 | 47 | |
48 | 48 | class User implements IUser { |
49 | - /** @var string */ |
|
50 | - private $uid; |
|
51 | - |
|
52 | - /** @var string */ |
|
53 | - private $displayName; |
|
54 | - |
|
55 | - /** @var UserInterface|null */ |
|
56 | - private $backend; |
|
57 | - /** @var EventDispatcherInterface */ |
|
58 | - private $dispatcher; |
|
59 | - |
|
60 | - /** @var bool */ |
|
61 | - private $enabled; |
|
62 | - |
|
63 | - /** @var Emitter|Manager */ |
|
64 | - private $emitter; |
|
65 | - |
|
66 | - /** @var string */ |
|
67 | - private $home; |
|
68 | - |
|
69 | - /** @var int */ |
|
70 | - private $lastLogin; |
|
71 | - |
|
72 | - /** @var \OCP\IConfig */ |
|
73 | - private $config; |
|
74 | - |
|
75 | - /** @var IAvatarManager */ |
|
76 | - private $avatarManager; |
|
77 | - |
|
78 | - /** @var IURLGenerator */ |
|
79 | - private $urlGenerator; |
|
80 | - |
|
81 | - public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
82 | - $this->uid = $uid; |
|
83 | - $this->backend = $backend; |
|
84 | - $this->dispatcher = $dispatcher; |
|
85 | - $this->emitter = $emitter; |
|
86 | - if(is_null($config)) { |
|
87 | - $config = \OC::$server->getConfig(); |
|
88 | - } |
|
89 | - $this->config = $config; |
|
90 | - $this->urlGenerator = $urlGenerator; |
|
91 | - $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); |
|
92 | - $this->enabled = ($enabled === 'true'); |
|
93 | - $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0); |
|
94 | - if (is_null($this->urlGenerator)) { |
|
95 | - $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * get the user id |
|
101 | - * |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function getUID() { |
|
105 | - return $this->uid; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * get the display name for the user, if no specific display name is set it will fallback to the user id |
|
110 | - * |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function getDisplayName() { |
|
114 | - if (!isset($this->displayName)) { |
|
115 | - $displayName = ''; |
|
116 | - if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
117 | - // get display name and strip whitespace from the beginning and end of it |
|
118 | - $backendDisplayName = $this->backend->getDisplayName($this->uid); |
|
119 | - if (is_string($backendDisplayName)) { |
|
120 | - $displayName = trim($backendDisplayName); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - if (!empty($displayName)) { |
|
125 | - $this->displayName = $displayName; |
|
126 | - } else { |
|
127 | - $this->displayName = $this->uid; |
|
128 | - } |
|
129 | - } |
|
130 | - return $this->displayName; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * set the displayname for the user |
|
135 | - * |
|
136 | - * @param string $displayName |
|
137 | - * @return bool |
|
138 | - */ |
|
139 | - public function setDisplayName($displayName) { |
|
140 | - $displayName = trim($displayName); |
|
141 | - if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName)) { |
|
142 | - $result = $this->backend->setDisplayName($this->uid, $displayName); |
|
143 | - if ($result) { |
|
144 | - $this->displayName = $displayName; |
|
145 | - $this->triggerChange('displayName', $displayName); |
|
146 | - } |
|
147 | - return $result !== false; |
|
148 | - } else { |
|
149 | - return false; |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * set the email address of the user |
|
155 | - * |
|
156 | - * @param string|null $mailAddress |
|
157 | - * @return void |
|
158 | - * @since 9.0.0 |
|
159 | - */ |
|
160 | - public function setEMailAddress($mailAddress) { |
|
161 | - $oldMailAddress = $this->getEMailAddress(); |
|
162 | - if($mailAddress === '') { |
|
163 | - $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
164 | - } else { |
|
165 | - $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
166 | - } |
|
167 | - if($oldMailAddress !== $mailAddress) { |
|
168 | - $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * returns the timestamp of the user's last login or 0 if the user did never |
|
174 | - * login |
|
175 | - * |
|
176 | - * @return int |
|
177 | - */ |
|
178 | - public function getLastLogin() { |
|
179 | - return $this->lastLogin; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * updates the timestamp of the most recent login of this user |
|
184 | - */ |
|
185 | - public function updateLastLoginTimestamp() { |
|
186 | - $firstTimeLogin = ($this->lastLogin === 0); |
|
187 | - $this->lastLogin = time(); |
|
188 | - $this->config->setUserValue( |
|
189 | - $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
190 | - |
|
191 | - return $firstTimeLogin; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Delete the user |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - */ |
|
199 | - public function delete() { |
|
200 | - $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
201 | - if ($this->emitter) { |
|
202 | - $this->emitter->emit('\OC\User', 'preDelete', array($this)); |
|
203 | - } |
|
204 | - // get the home now because it won't return it after user deletion |
|
205 | - $homePath = $this->getHome(); |
|
206 | - $result = $this->backend->deleteUser($this->uid); |
|
207 | - if ($result) { |
|
208 | - |
|
209 | - // FIXME: Feels like an hack - suggestions? |
|
210 | - |
|
211 | - $groupManager = \OC::$server->getGroupManager(); |
|
212 | - // We have to delete the user from all groups |
|
213 | - foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
214 | - $group = $groupManager->get($groupId); |
|
215 | - if ($group) { |
|
216 | - \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]); |
|
217 | - $group->removeUser($this); |
|
218 | - \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]); |
|
219 | - } |
|
220 | - } |
|
221 | - // Delete the user's keys in preferences |
|
222 | - \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
223 | - |
|
224 | - // Delete user files in /data/ |
|
225 | - if ($homePath !== false) { |
|
226 | - // FIXME: this operates directly on FS, should use View instead... |
|
227 | - // also this is not testable/mockable... |
|
228 | - \OC_Helper::rmdirr($homePath); |
|
229 | - } |
|
230 | - |
|
231 | - // Delete the users entry in the storage table |
|
232 | - Storage::remove('home::' . $this->uid); |
|
233 | - |
|
234 | - \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
235 | - \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
236 | - |
|
237 | - $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
238 | - $notification->setUser($this->uid); |
|
239 | - \OC::$server->getNotificationManager()->markProcessed($notification); |
|
240 | - |
|
241 | - /** @var AccountManager $accountManager */ |
|
242 | - $accountManager = \OC::$server->query(AccountManager::class); |
|
243 | - $accountManager->deleteUser($this); |
|
244 | - |
|
245 | - $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
246 | - if ($this->emitter) { |
|
247 | - $this->emitter->emit('\OC\User', 'postDelete', array($this)); |
|
248 | - } |
|
249 | - } |
|
250 | - return !($result === false); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Set the password of the user |
|
255 | - * |
|
256 | - * @param string $password |
|
257 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
258 | - * @return bool |
|
259 | - */ |
|
260 | - public function setPassword($password, $recoveryPassword = null) { |
|
261 | - $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
262 | - 'password' => $password, |
|
263 | - 'recoveryPassword' => $recoveryPassword, |
|
264 | - ])); |
|
265 | - if ($this->emitter) { |
|
266 | - $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword)); |
|
267 | - } |
|
268 | - if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
269 | - $result = $this->backend->setPassword($this->uid, $password); |
|
270 | - $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
271 | - 'password' => $password, |
|
272 | - 'recoveryPassword' => $recoveryPassword, |
|
273 | - ])); |
|
274 | - if ($this->emitter) { |
|
275 | - $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword)); |
|
276 | - } |
|
277 | - return !($result === false); |
|
278 | - } else { |
|
279 | - return false; |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * get the users home folder to mount |
|
285 | - * |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function getHome() { |
|
289 | - if (!$this->home) { |
|
290 | - if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
291 | - $this->home = $home; |
|
292 | - } elseif ($this->config) { |
|
293 | - $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
294 | - } else { |
|
295 | - $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
296 | - } |
|
297 | - } |
|
298 | - return $this->home; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Get the name of the backend class the user is connected with |
|
303 | - * |
|
304 | - * @return string |
|
305 | - */ |
|
306 | - public function getBackendClassName() { |
|
307 | - if($this->backend instanceof IUserBackend) { |
|
308 | - return $this->backend->getBackendName(); |
|
309 | - } |
|
310 | - return get_class($this->backend); |
|
311 | - } |
|
312 | - |
|
313 | - public function getBackend() { |
|
314 | - return $this->backend; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * check if the backend allows the user to change his avatar on Personal page |
|
319 | - * |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - public function canChangeAvatar() { |
|
323 | - if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
324 | - return $this->backend->canChangeAvatar($this->uid); |
|
325 | - } |
|
326 | - return true; |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * check if the backend supports changing passwords |
|
331 | - * |
|
332 | - * @return bool |
|
333 | - */ |
|
334 | - public function canChangePassword() { |
|
335 | - return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * check if the backend supports changing display names |
|
340 | - * |
|
341 | - * @return bool |
|
342 | - */ |
|
343 | - public function canChangeDisplayName() { |
|
344 | - if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
345 | - return false; |
|
346 | - } |
|
347 | - return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * check if the user is enabled |
|
352 | - * |
|
353 | - * @return bool |
|
354 | - */ |
|
355 | - public function isEnabled() { |
|
356 | - return $this->enabled; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * set the enabled status for the user |
|
361 | - * |
|
362 | - * @param bool $enabled |
|
363 | - */ |
|
364 | - public function setEnabled(bool $enabled = true) { |
|
365 | - $oldStatus = $this->isEnabled(); |
|
366 | - $this->enabled = $enabled; |
|
367 | - if ($oldStatus !== $this->enabled) { |
|
368 | - $this->triggerChange('enabled', $enabled); |
|
369 | - $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
370 | - } |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * get the users email address |
|
375 | - * |
|
376 | - * @return string|null |
|
377 | - * @since 9.0.0 |
|
378 | - */ |
|
379 | - public function getEMailAddress() { |
|
380 | - return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * get the users' quota |
|
385 | - * |
|
386 | - * @return string |
|
387 | - * @since 9.0.0 |
|
388 | - */ |
|
389 | - public function getQuota() { |
|
390 | - $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
391 | - if($quota === 'default') { |
|
392 | - $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
393 | - } |
|
394 | - return $quota; |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * set the users' quota |
|
399 | - * |
|
400 | - * @param string $quota |
|
401 | - * @return void |
|
402 | - * @since 9.0.0 |
|
403 | - */ |
|
404 | - public function setQuota($quota) { |
|
405 | - $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
406 | - if($quota !== 'none' and $quota !== 'default') { |
|
407 | - $quota = OC_Helper::computerFileSize($quota); |
|
408 | - $quota = OC_Helper::humanFileSize($quota); |
|
409 | - } |
|
410 | - $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
411 | - if($quota !== $oldQuota) { |
|
412 | - $this->triggerChange('quota', $quota); |
|
413 | - } |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * get the avatar image if it exists |
|
418 | - * |
|
419 | - * @param int $size |
|
420 | - * @return IImage|null |
|
421 | - * @since 9.0.0 |
|
422 | - */ |
|
423 | - public function getAvatarImage($size) { |
|
424 | - // delay the initialization |
|
425 | - if (is_null($this->avatarManager)) { |
|
426 | - $this->avatarManager = \OC::$server->getAvatarManager(); |
|
427 | - } |
|
428 | - |
|
429 | - $avatar = $this->avatarManager->getAvatar($this->uid); |
|
430 | - $image = $avatar->get(-1); |
|
431 | - if ($image) { |
|
432 | - return $image; |
|
433 | - } |
|
434 | - |
|
435 | - return null; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * get the federation cloud id |
|
440 | - * |
|
441 | - * @return string |
|
442 | - * @since 9.0.0 |
|
443 | - */ |
|
444 | - public function getCloudId() { |
|
445 | - $uid = $this->getUID(); |
|
446 | - $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
447 | - $server = rtrim( $this->removeProtocolFromUrl($server), '/'); |
|
448 | - return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * @param string $url |
|
453 | - * @return string |
|
454 | - */ |
|
455 | - private function removeProtocolFromUrl($url) { |
|
456 | - if (strpos($url, 'https://') === 0) { |
|
457 | - return substr($url, strlen('https://')); |
|
458 | - } else if (strpos($url, 'http://') === 0) { |
|
459 | - return substr($url, strlen('http://')); |
|
460 | - } |
|
461 | - |
|
462 | - return $url; |
|
463 | - } |
|
464 | - |
|
465 | - public function triggerChange($feature, $value = null, $oldValue = null) { |
|
466 | - $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
467 | - 'feature' => $feature, |
|
468 | - 'value' => $value, |
|
469 | - 'oldValue' => $oldValue, |
|
470 | - ])); |
|
471 | - if ($this->emitter) { |
|
472 | - $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue)); |
|
473 | - } |
|
474 | - } |
|
49 | + /** @var string */ |
|
50 | + private $uid; |
|
51 | + |
|
52 | + /** @var string */ |
|
53 | + private $displayName; |
|
54 | + |
|
55 | + /** @var UserInterface|null */ |
|
56 | + private $backend; |
|
57 | + /** @var EventDispatcherInterface */ |
|
58 | + private $dispatcher; |
|
59 | + |
|
60 | + /** @var bool */ |
|
61 | + private $enabled; |
|
62 | + |
|
63 | + /** @var Emitter|Manager */ |
|
64 | + private $emitter; |
|
65 | + |
|
66 | + /** @var string */ |
|
67 | + private $home; |
|
68 | + |
|
69 | + /** @var int */ |
|
70 | + private $lastLogin; |
|
71 | + |
|
72 | + /** @var \OCP\IConfig */ |
|
73 | + private $config; |
|
74 | + |
|
75 | + /** @var IAvatarManager */ |
|
76 | + private $avatarManager; |
|
77 | + |
|
78 | + /** @var IURLGenerator */ |
|
79 | + private $urlGenerator; |
|
80 | + |
|
81 | + public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
|
82 | + $this->uid = $uid; |
|
83 | + $this->backend = $backend; |
|
84 | + $this->dispatcher = $dispatcher; |
|
85 | + $this->emitter = $emitter; |
|
86 | + if(is_null($config)) { |
|
87 | + $config = \OC::$server->getConfig(); |
|
88 | + } |
|
89 | + $this->config = $config; |
|
90 | + $this->urlGenerator = $urlGenerator; |
|
91 | + $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); |
|
92 | + $this->enabled = ($enabled === 'true'); |
|
93 | + $this->lastLogin = $this->config->getUserValue($uid, 'login', 'lastLogin', 0); |
|
94 | + if (is_null($this->urlGenerator)) { |
|
95 | + $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * get the user id |
|
101 | + * |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function getUID() { |
|
105 | + return $this->uid; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * get the display name for the user, if no specific display name is set it will fallback to the user id |
|
110 | + * |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function getDisplayName() { |
|
114 | + if (!isset($this->displayName)) { |
|
115 | + $displayName = ''; |
|
116 | + if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
117 | + // get display name and strip whitespace from the beginning and end of it |
|
118 | + $backendDisplayName = $this->backend->getDisplayName($this->uid); |
|
119 | + if (is_string($backendDisplayName)) { |
|
120 | + $displayName = trim($backendDisplayName); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + if (!empty($displayName)) { |
|
125 | + $this->displayName = $displayName; |
|
126 | + } else { |
|
127 | + $this->displayName = $this->uid; |
|
128 | + } |
|
129 | + } |
|
130 | + return $this->displayName; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * set the displayname for the user |
|
135 | + * |
|
136 | + * @param string $displayName |
|
137 | + * @return bool |
|
138 | + */ |
|
139 | + public function setDisplayName($displayName) { |
|
140 | + $displayName = trim($displayName); |
|
141 | + if ($this->backend->implementsActions(Backend::SET_DISPLAYNAME) && !empty($displayName)) { |
|
142 | + $result = $this->backend->setDisplayName($this->uid, $displayName); |
|
143 | + if ($result) { |
|
144 | + $this->displayName = $displayName; |
|
145 | + $this->triggerChange('displayName', $displayName); |
|
146 | + } |
|
147 | + return $result !== false; |
|
148 | + } else { |
|
149 | + return false; |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * set the email address of the user |
|
155 | + * |
|
156 | + * @param string|null $mailAddress |
|
157 | + * @return void |
|
158 | + * @since 9.0.0 |
|
159 | + */ |
|
160 | + public function setEMailAddress($mailAddress) { |
|
161 | + $oldMailAddress = $this->getEMailAddress(); |
|
162 | + if($mailAddress === '') { |
|
163 | + $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
|
164 | + } else { |
|
165 | + $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
|
166 | + } |
|
167 | + if($oldMailAddress !== $mailAddress) { |
|
168 | + $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * returns the timestamp of the user's last login or 0 if the user did never |
|
174 | + * login |
|
175 | + * |
|
176 | + * @return int |
|
177 | + */ |
|
178 | + public function getLastLogin() { |
|
179 | + return $this->lastLogin; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * updates the timestamp of the most recent login of this user |
|
184 | + */ |
|
185 | + public function updateLastLoginTimestamp() { |
|
186 | + $firstTimeLogin = ($this->lastLogin === 0); |
|
187 | + $this->lastLogin = time(); |
|
188 | + $this->config->setUserValue( |
|
189 | + $this->uid, 'login', 'lastLogin', $this->lastLogin); |
|
190 | + |
|
191 | + return $firstTimeLogin; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Delete the user |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + */ |
|
199 | + public function delete() { |
|
200 | + $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
201 | + if ($this->emitter) { |
|
202 | + $this->emitter->emit('\OC\User', 'preDelete', array($this)); |
|
203 | + } |
|
204 | + // get the home now because it won't return it after user deletion |
|
205 | + $homePath = $this->getHome(); |
|
206 | + $result = $this->backend->deleteUser($this->uid); |
|
207 | + if ($result) { |
|
208 | + |
|
209 | + // FIXME: Feels like an hack - suggestions? |
|
210 | + |
|
211 | + $groupManager = \OC::$server->getGroupManager(); |
|
212 | + // We have to delete the user from all groups |
|
213 | + foreach ($groupManager->getUserGroupIds($this) as $groupId) { |
|
214 | + $group = $groupManager->get($groupId); |
|
215 | + if ($group) { |
|
216 | + \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]); |
|
217 | + $group->removeUser($this); |
|
218 | + \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]); |
|
219 | + } |
|
220 | + } |
|
221 | + // Delete the user's keys in preferences |
|
222 | + \OC::$server->getConfig()->deleteAllUserValues($this->uid); |
|
223 | + |
|
224 | + // Delete user files in /data/ |
|
225 | + if ($homePath !== false) { |
|
226 | + // FIXME: this operates directly on FS, should use View instead... |
|
227 | + // also this is not testable/mockable... |
|
228 | + \OC_Helper::rmdirr($homePath); |
|
229 | + } |
|
230 | + |
|
231 | + // Delete the users entry in the storage table |
|
232 | + Storage::remove('home::' . $this->uid); |
|
233 | + |
|
234 | + \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
|
235 | + \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
|
236 | + |
|
237 | + $notification = \OC::$server->getNotificationManager()->createNotification(); |
|
238 | + $notification->setUser($this->uid); |
|
239 | + \OC::$server->getNotificationManager()->markProcessed($notification); |
|
240 | + |
|
241 | + /** @var AccountManager $accountManager */ |
|
242 | + $accountManager = \OC::$server->query(AccountManager::class); |
|
243 | + $accountManager->deleteUser($this); |
|
244 | + |
|
245 | + $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
246 | + if ($this->emitter) { |
|
247 | + $this->emitter->emit('\OC\User', 'postDelete', array($this)); |
|
248 | + } |
|
249 | + } |
|
250 | + return !($result === false); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Set the password of the user |
|
255 | + * |
|
256 | + * @param string $password |
|
257 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
258 | + * @return bool |
|
259 | + */ |
|
260 | + public function setPassword($password, $recoveryPassword = null) { |
|
261 | + $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
262 | + 'password' => $password, |
|
263 | + 'recoveryPassword' => $recoveryPassword, |
|
264 | + ])); |
|
265 | + if ($this->emitter) { |
|
266 | + $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword)); |
|
267 | + } |
|
268 | + if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
|
269 | + $result = $this->backend->setPassword($this->uid, $password); |
|
270 | + $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
271 | + 'password' => $password, |
|
272 | + 'recoveryPassword' => $recoveryPassword, |
|
273 | + ])); |
|
274 | + if ($this->emitter) { |
|
275 | + $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword)); |
|
276 | + } |
|
277 | + return !($result === false); |
|
278 | + } else { |
|
279 | + return false; |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * get the users home folder to mount |
|
285 | + * |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function getHome() { |
|
289 | + if (!$this->home) { |
|
290 | + if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
|
291 | + $this->home = $home; |
|
292 | + } elseif ($this->config) { |
|
293 | + $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
294 | + } else { |
|
295 | + $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
296 | + } |
|
297 | + } |
|
298 | + return $this->home; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Get the name of the backend class the user is connected with |
|
303 | + * |
|
304 | + * @return string |
|
305 | + */ |
|
306 | + public function getBackendClassName() { |
|
307 | + if($this->backend instanceof IUserBackend) { |
|
308 | + return $this->backend->getBackendName(); |
|
309 | + } |
|
310 | + return get_class($this->backend); |
|
311 | + } |
|
312 | + |
|
313 | + public function getBackend() { |
|
314 | + return $this->backend; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * check if the backend allows the user to change his avatar on Personal page |
|
319 | + * |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + public function canChangeAvatar() { |
|
323 | + if ($this->backend->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
324 | + return $this->backend->canChangeAvatar($this->uid); |
|
325 | + } |
|
326 | + return true; |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * check if the backend supports changing passwords |
|
331 | + * |
|
332 | + * @return bool |
|
333 | + */ |
|
334 | + public function canChangePassword() { |
|
335 | + return $this->backend->implementsActions(Backend::SET_PASSWORD); |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * check if the backend supports changing display names |
|
340 | + * |
|
341 | + * @return bool |
|
342 | + */ |
|
343 | + public function canChangeDisplayName() { |
|
344 | + if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) { |
|
345 | + return false; |
|
346 | + } |
|
347 | + return $this->backend->implementsActions(Backend::SET_DISPLAYNAME); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * check if the user is enabled |
|
352 | + * |
|
353 | + * @return bool |
|
354 | + */ |
|
355 | + public function isEnabled() { |
|
356 | + return $this->enabled; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * set the enabled status for the user |
|
361 | + * |
|
362 | + * @param bool $enabled |
|
363 | + */ |
|
364 | + public function setEnabled(bool $enabled = true) { |
|
365 | + $oldStatus = $this->isEnabled(); |
|
366 | + $this->enabled = $enabled; |
|
367 | + if ($oldStatus !== $this->enabled) { |
|
368 | + $this->triggerChange('enabled', $enabled); |
|
369 | + $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); |
|
370 | + } |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * get the users email address |
|
375 | + * |
|
376 | + * @return string|null |
|
377 | + * @since 9.0.0 |
|
378 | + */ |
|
379 | + public function getEMailAddress() { |
|
380 | + return $this->config->getUserValue($this->uid, 'settings', 'email', null); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * get the users' quota |
|
385 | + * |
|
386 | + * @return string |
|
387 | + * @since 9.0.0 |
|
388 | + */ |
|
389 | + public function getQuota() { |
|
390 | + $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
|
391 | + if($quota === 'default') { |
|
392 | + $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
393 | + } |
|
394 | + return $quota; |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * set the users' quota |
|
399 | + * |
|
400 | + * @param string $quota |
|
401 | + * @return void |
|
402 | + * @since 9.0.0 |
|
403 | + */ |
|
404 | + public function setQuota($quota) { |
|
405 | + $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
|
406 | + if($quota !== 'none' and $quota !== 'default') { |
|
407 | + $quota = OC_Helper::computerFileSize($quota); |
|
408 | + $quota = OC_Helper::humanFileSize($quota); |
|
409 | + } |
|
410 | + $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
|
411 | + if($quota !== $oldQuota) { |
|
412 | + $this->triggerChange('quota', $quota); |
|
413 | + } |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * get the avatar image if it exists |
|
418 | + * |
|
419 | + * @param int $size |
|
420 | + * @return IImage|null |
|
421 | + * @since 9.0.0 |
|
422 | + */ |
|
423 | + public function getAvatarImage($size) { |
|
424 | + // delay the initialization |
|
425 | + if (is_null($this->avatarManager)) { |
|
426 | + $this->avatarManager = \OC::$server->getAvatarManager(); |
|
427 | + } |
|
428 | + |
|
429 | + $avatar = $this->avatarManager->getAvatar($this->uid); |
|
430 | + $image = $avatar->get(-1); |
|
431 | + if ($image) { |
|
432 | + return $image; |
|
433 | + } |
|
434 | + |
|
435 | + return null; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * get the federation cloud id |
|
440 | + * |
|
441 | + * @return string |
|
442 | + * @since 9.0.0 |
|
443 | + */ |
|
444 | + public function getCloudId() { |
|
445 | + $uid = $this->getUID(); |
|
446 | + $server = $this->urlGenerator->getAbsoluteURL('/'); |
|
447 | + $server = rtrim( $this->removeProtocolFromUrl($server), '/'); |
|
448 | + return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * @param string $url |
|
453 | + * @return string |
|
454 | + */ |
|
455 | + private function removeProtocolFromUrl($url) { |
|
456 | + if (strpos($url, 'https://') === 0) { |
|
457 | + return substr($url, strlen('https://')); |
|
458 | + } else if (strpos($url, 'http://') === 0) { |
|
459 | + return substr($url, strlen('http://')); |
|
460 | + } |
|
461 | + |
|
462 | + return $url; |
|
463 | + } |
|
464 | + |
|
465 | + public function triggerChange($feature, $value = null, $oldValue = null) { |
|
466 | + $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
467 | + 'feature' => $feature, |
|
468 | + 'value' => $value, |
|
469 | + 'oldValue' => $oldValue, |
|
470 | + ])); |
|
471 | + if ($this->emitter) { |
|
472 | + $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue)); |
|
473 | + } |
|
474 | + } |
|
475 | 475 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $this->backend = $backend; |
84 | 84 | $this->dispatcher = $dispatcher; |
85 | 85 | $this->emitter = $emitter; |
86 | - if(is_null($config)) { |
|
86 | + if (is_null($config)) { |
|
87 | 87 | $config = \OC::$server->getConfig(); |
88 | 88 | } |
89 | 89 | $this->config = $config; |
@@ -159,12 +159,12 @@ discard block |
||
159 | 159 | */ |
160 | 160 | public function setEMailAddress($mailAddress) { |
161 | 161 | $oldMailAddress = $this->getEMailAddress(); |
162 | - if($mailAddress === '') { |
|
162 | + if ($mailAddress === '') { |
|
163 | 163 | $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
164 | 164 | } else { |
165 | 165 | $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
166 | 166 | } |
167 | - if($oldMailAddress !== $mailAddress) { |
|
167 | + if ($oldMailAddress !== $mailAddress) { |
|
168 | 168 | $this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress); |
169 | 169 | } |
170 | 170 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | * @return bool |
198 | 198 | */ |
199 | 199 | public function delete() { |
200 | - $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this)); |
|
200 | + $this->dispatcher->dispatch(IUser::class.'::preDelete', new GenericEvent($this)); |
|
201 | 201 | if ($this->emitter) { |
202 | 202 | $this->emitter->emit('\OC\User', 'preDelete', array($this)); |
203 | 203 | } |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | } |
230 | 230 | |
231 | 231 | // Delete the users entry in the storage table |
232 | - Storage::remove('home::' . $this->uid); |
|
232 | + Storage::remove('home::'.$this->uid); |
|
233 | 233 | |
234 | 234 | \OC::$server->getCommentsManager()->deleteReferencesOfActor('users', $this->uid); |
235 | 235 | \OC::$server->getCommentsManager()->deleteReadMarksFromUser($this); |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | $accountManager = \OC::$server->query(AccountManager::class); |
243 | 243 | $accountManager->deleteUser($this); |
244 | 244 | |
245 | - $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this)); |
|
245 | + $this->dispatcher->dispatch(IUser::class.'::postDelete', new GenericEvent($this)); |
|
246 | 246 | if ($this->emitter) { |
247 | 247 | $this->emitter->emit('\OC\User', 'postDelete', array($this)); |
248 | 248 | } |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | * @return bool |
259 | 259 | */ |
260 | 260 | public function setPassword($password, $recoveryPassword = null) { |
261 | - $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ |
|
261 | + $this->dispatcher->dispatch(IUser::class.'::preSetPassword', new GenericEvent($this, [ |
|
262 | 262 | 'password' => $password, |
263 | 263 | 'recoveryPassword' => $recoveryPassword, |
264 | 264 | ])); |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | } |
268 | 268 | if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
269 | 269 | $result = $this->backend->setPassword($this->uid, $password); |
270 | - $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ |
|
270 | + $this->dispatcher->dispatch(IUser::class.'::postSetPassword', new GenericEvent($this, [ |
|
271 | 271 | 'password' => $password, |
272 | 272 | 'recoveryPassword' => $recoveryPassword, |
273 | 273 | ])); |
@@ -290,9 +290,9 @@ discard block |
||
290 | 290 | if ($this->backend->implementsActions(Backend::GET_HOME) and $home = $this->backend->getHome($this->uid)) { |
291 | 291 | $this->home = $home; |
292 | 292 | } elseif ($this->config) { |
293 | - $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $this->uid; |
|
293 | + $this->home = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/'.$this->uid; |
|
294 | 294 | } else { |
295 | - $this->home = \OC::$SERVERROOT . '/data/' . $this->uid; |
|
295 | + $this->home = \OC::$SERVERROOT.'/data/'.$this->uid; |
|
296 | 296 | } |
297 | 297 | } |
298 | 298 | return $this->home; |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @return string |
305 | 305 | */ |
306 | 306 | public function getBackendClassName() { |
307 | - if($this->backend instanceof IUserBackend) { |
|
307 | + if ($this->backend instanceof IUserBackend) { |
|
308 | 308 | return $this->backend->getBackendName(); |
309 | 309 | } |
310 | 310 | return get_class($this->backend); |
@@ -388,7 +388,7 @@ discard block |
||
388 | 388 | */ |
389 | 389 | public function getQuota() { |
390 | 390 | $quota = $this->config->getUserValue($this->uid, 'files', 'quota', 'default'); |
391 | - if($quota === 'default') { |
|
391 | + if ($quota === 'default') { |
|
392 | 392 | $quota = $this->config->getAppValue('files', 'default_quota', 'none'); |
393 | 393 | } |
394 | 394 | return $quota; |
@@ -403,12 +403,12 @@ discard block |
||
403 | 403 | */ |
404 | 404 | public function setQuota($quota) { |
405 | 405 | $oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', ''); |
406 | - if($quota !== 'none' and $quota !== 'default') { |
|
406 | + if ($quota !== 'none' and $quota !== 'default') { |
|
407 | 407 | $quota = OC_Helper::computerFileSize($quota); |
408 | 408 | $quota = OC_Helper::humanFileSize($quota); |
409 | 409 | } |
410 | 410 | $this->config->setUserValue($this->uid, 'files', 'quota', $quota); |
411 | - if($quota !== $oldQuota) { |
|
411 | + if ($quota !== $oldQuota) { |
|
412 | 412 | $this->triggerChange('quota', $quota); |
413 | 413 | } |
414 | 414 | } |
@@ -444,7 +444,7 @@ discard block |
||
444 | 444 | public function getCloudId() { |
445 | 445 | $uid = $this->getUID(); |
446 | 446 | $server = $this->urlGenerator->getAbsoluteURL('/'); |
447 | - $server = rtrim( $this->removeProtocolFromUrl($server), '/'); |
|
447 | + $server = rtrim($this->removeProtocolFromUrl($server), '/'); |
|
448 | 448 | return \OC::$server->getCloudIdManager()->getCloudId($uid, $server)->getId(); |
449 | 449 | } |
450 | 450 | |
@@ -463,7 +463,7 @@ discard block |
||
463 | 463 | } |
464 | 464 | |
465 | 465 | public function triggerChange($feature, $value = null, $oldValue = null) { |
466 | - $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ |
|
466 | + $this->dispatcher->dispatch(IUser::class.'::changeUser', new GenericEvent($this, [ |
|
467 | 467 | 'feature' => $feature, |
468 | 468 | 'value' => $value, |
469 | 469 | 'oldValue' => $oldValue, |
@@ -81,2094 +81,2094 @@ |
||
81 | 81 | * \OC\Files\Storage\Storage object |
82 | 82 | */ |
83 | 83 | class View { |
84 | - /** @var string */ |
|
85 | - private $fakeRoot = ''; |
|
86 | - |
|
87 | - /** |
|
88 | - * @var \OCP\Lock\ILockingProvider |
|
89 | - */ |
|
90 | - protected $lockingProvider; |
|
91 | - |
|
92 | - private $lockingEnabled; |
|
93 | - |
|
94 | - private $updaterEnabled = true; |
|
95 | - |
|
96 | - /** @var \OC\User\Manager */ |
|
97 | - private $userManager; |
|
98 | - |
|
99 | - /** @var \OCP\ILogger */ |
|
100 | - private $logger; |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $root |
|
104 | - * @throws \Exception If $root contains an invalid path |
|
105 | - */ |
|
106 | - public function __construct($root = '') { |
|
107 | - if (is_null($root)) { |
|
108 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
109 | - } |
|
110 | - if (!Filesystem::isValidPath($root)) { |
|
111 | - throw new \Exception(); |
|
112 | - } |
|
113 | - |
|
114 | - $this->fakeRoot = $root; |
|
115 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
116 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
117 | - $this->userManager = \OC::$server->getUserManager(); |
|
118 | - $this->logger = \OC::$server->getLogger(); |
|
119 | - } |
|
120 | - |
|
121 | - public function getAbsolutePath($path = '/') { |
|
122 | - if ($path === null) { |
|
123 | - return null; |
|
124 | - } |
|
125 | - $this->assertPathLength($path); |
|
126 | - if ($path === '') { |
|
127 | - $path = '/'; |
|
128 | - } |
|
129 | - if ($path[0] !== '/') { |
|
130 | - $path = '/' . $path; |
|
131 | - } |
|
132 | - return $this->fakeRoot . $path; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * change the root to a fake root |
|
137 | - * |
|
138 | - * @param string $fakeRoot |
|
139 | - * @return boolean|null |
|
140 | - */ |
|
141 | - public function chroot($fakeRoot) { |
|
142 | - if (!$fakeRoot == '') { |
|
143 | - if ($fakeRoot[0] !== '/') { |
|
144 | - $fakeRoot = '/' . $fakeRoot; |
|
145 | - } |
|
146 | - } |
|
147 | - $this->fakeRoot = $fakeRoot; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * get the fake root |
|
152 | - * |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - public function getRoot() { |
|
156 | - return $this->fakeRoot; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * get path relative to the root of the view |
|
161 | - * |
|
162 | - * @param string $path |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function getRelativePath($path) { |
|
166 | - $this->assertPathLength($path); |
|
167 | - if ($this->fakeRoot == '') { |
|
168 | - return $path; |
|
169 | - } |
|
170 | - |
|
171 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
172 | - return '/'; |
|
173 | - } |
|
174 | - |
|
175 | - // missing slashes can cause wrong matches! |
|
176 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
177 | - |
|
178 | - if (strpos($path, $root) !== 0) { |
|
179 | - return null; |
|
180 | - } else { |
|
181 | - $path = substr($path, strlen($this->fakeRoot)); |
|
182 | - if (strlen($path) === 0) { |
|
183 | - return '/'; |
|
184 | - } else { |
|
185 | - return $path; |
|
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * get the mountpoint of the storage object for a path |
|
192 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
193 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
194 | - * and does not take the chroot into account ) |
|
195 | - * |
|
196 | - * @param string $path |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function getMountPoint($path) { |
|
200 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * get the mountpoint of the storage object for a path |
|
205 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
206 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
207 | - * and does not take the chroot into account ) |
|
208 | - * |
|
209 | - * @param string $path |
|
210 | - * @return \OCP\Files\Mount\IMountPoint |
|
211 | - */ |
|
212 | - public function getMount($path) { |
|
213 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * resolve a path to a storage and internal path |
|
218 | - * |
|
219 | - * @param string $path |
|
220 | - * @return array an array consisting of the storage and the internal path |
|
221 | - */ |
|
222 | - public function resolvePath($path) { |
|
223 | - $a = $this->getAbsolutePath($path); |
|
224 | - $p = Filesystem::normalizePath($a); |
|
225 | - return Filesystem::resolvePath($p); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * return the path to a local version of the file |
|
230 | - * we need this because we can't know if a file is stored local or not from |
|
231 | - * outside the filestorage and for some purposes a local file is needed |
|
232 | - * |
|
233 | - * @param string $path |
|
234 | - * @return string |
|
235 | - */ |
|
236 | - public function getLocalFile($path) { |
|
237 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
238 | - $path = $this->getAbsolutePath($path); |
|
239 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
240 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
241 | - return $storage->getLocalFile($internalPath); |
|
242 | - } else { |
|
243 | - return null; |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @param string $path |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - public function getLocalFolder($path) { |
|
252 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
253 | - $path = $this->getAbsolutePath($path); |
|
254 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
255 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
256 | - return $storage->getLocalFolder($internalPath); |
|
257 | - } else { |
|
258 | - return null; |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * the following functions operate with arguments and return values identical |
|
264 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
265 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
266 | - */ |
|
267 | - public function mkdir($path) { |
|
268 | - return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * remove mount point |
|
273 | - * |
|
274 | - * @param \OC\Files\Mount\MoveableMount $mount |
|
275 | - * @param string $path relative to data/ |
|
276 | - * @return boolean |
|
277 | - */ |
|
278 | - protected function removeMount($mount, $path) { |
|
279 | - if ($mount instanceof MoveableMount) { |
|
280 | - // cut of /user/files to get the relative path to data/user/files |
|
281 | - $pathParts = explode('/', $path, 4); |
|
282 | - $relPath = '/' . $pathParts[3]; |
|
283 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
284 | - \OC_Hook::emit( |
|
285 | - Filesystem::CLASSNAME, "umount", |
|
286 | - array(Filesystem::signal_param_path => $relPath) |
|
287 | - ); |
|
288 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
289 | - $result = $mount->removeMount(); |
|
290 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
291 | - if ($result) { |
|
292 | - \OC_Hook::emit( |
|
293 | - Filesystem::CLASSNAME, "post_umount", |
|
294 | - array(Filesystem::signal_param_path => $relPath) |
|
295 | - ); |
|
296 | - } |
|
297 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
298 | - return $result; |
|
299 | - } else { |
|
300 | - // do not allow deleting the storage's root / the mount point |
|
301 | - // because for some storages it might delete the whole contents |
|
302 | - // but isn't supposed to work that way |
|
303 | - return false; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - public function disableCacheUpdate() { |
|
308 | - $this->updaterEnabled = false; |
|
309 | - } |
|
310 | - |
|
311 | - public function enableCacheUpdate() { |
|
312 | - $this->updaterEnabled = true; |
|
313 | - } |
|
314 | - |
|
315 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
316 | - if ($this->updaterEnabled) { |
|
317 | - if (is_null($time)) { |
|
318 | - $time = time(); |
|
319 | - } |
|
320 | - $storage->getUpdater()->update($internalPath, $time); |
|
321 | - } |
|
322 | - } |
|
323 | - |
|
324 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
325 | - if ($this->updaterEnabled) { |
|
326 | - $storage->getUpdater()->remove($internalPath); |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
331 | - if ($this->updaterEnabled) { |
|
332 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @param string $path |
|
338 | - * @return bool|mixed |
|
339 | - */ |
|
340 | - public function rmdir($path) { |
|
341 | - $absolutePath = $this->getAbsolutePath($path); |
|
342 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
343 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
344 | - return $this->removeMount($mount, $absolutePath); |
|
345 | - } |
|
346 | - if ($this->is_dir($path)) { |
|
347 | - $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
348 | - } else { |
|
349 | - $result = false; |
|
350 | - } |
|
351 | - |
|
352 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
353 | - $storage = $mount->getStorage(); |
|
354 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
355 | - $storage->getUpdater()->remove($internalPath); |
|
356 | - } |
|
357 | - return $result; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * @param string $path |
|
362 | - * @return resource |
|
363 | - */ |
|
364 | - public function opendir($path) { |
|
365 | - return $this->basicOperation('opendir', $path, array('read')); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * @param string $path |
|
370 | - * @return bool|mixed |
|
371 | - */ |
|
372 | - public function is_dir($path) { |
|
373 | - if ($path == '/') { |
|
374 | - return true; |
|
375 | - } |
|
376 | - return $this->basicOperation('is_dir', $path); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * @param string $path |
|
381 | - * @return bool|mixed |
|
382 | - */ |
|
383 | - public function is_file($path) { |
|
384 | - if ($path == '/') { |
|
385 | - return false; |
|
386 | - } |
|
387 | - return $this->basicOperation('is_file', $path); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * @param string $path |
|
392 | - * @return mixed |
|
393 | - */ |
|
394 | - public function stat($path) { |
|
395 | - return $this->basicOperation('stat', $path); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param string $path |
|
400 | - * @return mixed |
|
401 | - */ |
|
402 | - public function filetype($path) { |
|
403 | - return $this->basicOperation('filetype', $path); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * @param string $path |
|
408 | - * @return mixed |
|
409 | - */ |
|
410 | - public function filesize($path) { |
|
411 | - return $this->basicOperation('filesize', $path); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * @param string $path |
|
416 | - * @return bool|mixed |
|
417 | - * @throws \OCP\Files\InvalidPathException |
|
418 | - */ |
|
419 | - public function readfile($path) { |
|
420 | - $this->assertPathLength($path); |
|
421 | - @ob_end_clean(); |
|
422 | - $handle = $this->fopen($path, 'rb'); |
|
423 | - if ($handle) { |
|
424 | - $chunkSize = 8192; // 8 kB chunks |
|
425 | - while (!feof($handle)) { |
|
426 | - echo fread($handle, $chunkSize); |
|
427 | - flush(); |
|
428 | - } |
|
429 | - fclose($handle); |
|
430 | - return $this->filesize($path); |
|
431 | - } |
|
432 | - return false; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * @param string $path |
|
437 | - * @param int $from |
|
438 | - * @param int $to |
|
439 | - * @return bool|mixed |
|
440 | - * @throws \OCP\Files\InvalidPathException |
|
441 | - * @throws \OCP\Files\UnseekableException |
|
442 | - */ |
|
443 | - public function readfilePart($path, $from, $to) { |
|
444 | - $this->assertPathLength($path); |
|
445 | - @ob_end_clean(); |
|
446 | - $handle = $this->fopen($path, 'rb'); |
|
447 | - if ($handle) { |
|
448 | - $chunkSize = 8192; // 8 kB chunks |
|
449 | - $startReading = true; |
|
450 | - |
|
451 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
452 | - // forward file handle via chunked fread because fseek seem to have failed |
|
453 | - |
|
454 | - $end = $from + 1; |
|
455 | - while (!feof($handle) && ftell($handle) < $end) { |
|
456 | - $len = $from - ftell($handle); |
|
457 | - if ($len > $chunkSize) { |
|
458 | - $len = $chunkSize; |
|
459 | - } |
|
460 | - $result = fread($handle, $len); |
|
461 | - |
|
462 | - if ($result === false) { |
|
463 | - $startReading = false; |
|
464 | - break; |
|
465 | - } |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - if ($startReading) { |
|
470 | - $end = $to + 1; |
|
471 | - while (!feof($handle) && ftell($handle) < $end) { |
|
472 | - $len = $end - ftell($handle); |
|
473 | - if ($len > $chunkSize) { |
|
474 | - $len = $chunkSize; |
|
475 | - } |
|
476 | - echo fread($handle, $len); |
|
477 | - flush(); |
|
478 | - } |
|
479 | - return ftell($handle) - $from; |
|
480 | - } |
|
481 | - |
|
482 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
483 | - } |
|
484 | - return false; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * @param string $path |
|
489 | - * @return mixed |
|
490 | - */ |
|
491 | - public function isCreatable($path) { |
|
492 | - return $this->basicOperation('isCreatable', $path); |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * @param string $path |
|
497 | - * @return mixed |
|
498 | - */ |
|
499 | - public function isReadable($path) { |
|
500 | - return $this->basicOperation('isReadable', $path); |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * @param string $path |
|
505 | - * @return mixed |
|
506 | - */ |
|
507 | - public function isUpdatable($path) { |
|
508 | - return $this->basicOperation('isUpdatable', $path); |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * @param string $path |
|
513 | - * @return bool|mixed |
|
514 | - */ |
|
515 | - public function isDeletable($path) { |
|
516 | - $absolutePath = $this->getAbsolutePath($path); |
|
517 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
518 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
519 | - return $mount instanceof MoveableMount; |
|
520 | - } |
|
521 | - return $this->basicOperation('isDeletable', $path); |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * @param string $path |
|
526 | - * @return mixed |
|
527 | - */ |
|
528 | - public function isSharable($path) { |
|
529 | - return $this->basicOperation('isSharable', $path); |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * @param string $path |
|
534 | - * @return bool|mixed |
|
535 | - */ |
|
536 | - public function file_exists($path) { |
|
537 | - if ($path == '/') { |
|
538 | - return true; |
|
539 | - } |
|
540 | - return $this->basicOperation('file_exists', $path); |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * @param string $path |
|
545 | - * @return mixed |
|
546 | - */ |
|
547 | - public function filemtime($path) { |
|
548 | - return $this->basicOperation('filemtime', $path); |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * @param string $path |
|
553 | - * @param int|string $mtime |
|
554 | - * @return bool |
|
555 | - */ |
|
556 | - public function touch($path, $mtime = null) { |
|
557 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
558 | - $mtime = strtotime($mtime); |
|
559 | - } |
|
560 | - |
|
561 | - $hooks = array('touch'); |
|
562 | - |
|
563 | - if (!$this->file_exists($path)) { |
|
564 | - $hooks[] = 'create'; |
|
565 | - $hooks[] = 'write'; |
|
566 | - } |
|
567 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
568 | - if (!$result) { |
|
569 | - // If create file fails because of permissions on external storage like SMB folders, |
|
570 | - // check file exists and return false if not. |
|
571 | - if (!$this->file_exists($path)) { |
|
572 | - return false; |
|
573 | - } |
|
574 | - if (is_null($mtime)) { |
|
575 | - $mtime = time(); |
|
576 | - } |
|
577 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
578 | - $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
579 | - } |
|
580 | - return true; |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * @param string $path |
|
585 | - * @return mixed |
|
586 | - */ |
|
587 | - public function file_get_contents($path) { |
|
588 | - return $this->basicOperation('file_get_contents', $path, array('read')); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * @param bool $exists |
|
593 | - * @param string $path |
|
594 | - * @param bool $run |
|
595 | - */ |
|
596 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
597 | - if (!$exists) { |
|
598 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
599 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
600 | - Filesystem::signal_param_run => &$run, |
|
601 | - )); |
|
602 | - } else { |
|
603 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
604 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
605 | - Filesystem::signal_param_run => &$run, |
|
606 | - )); |
|
607 | - } |
|
608 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
609 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
610 | - Filesystem::signal_param_run => &$run, |
|
611 | - )); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * @param bool $exists |
|
616 | - * @param string $path |
|
617 | - */ |
|
618 | - protected function emit_file_hooks_post($exists, $path) { |
|
619 | - if (!$exists) { |
|
620 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
621 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
622 | - )); |
|
623 | - } else { |
|
624 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
625 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
626 | - )); |
|
627 | - } |
|
628 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
629 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
630 | - )); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * @param string $path |
|
635 | - * @param string|resource $data |
|
636 | - * @return bool|mixed |
|
637 | - * @throws \Exception |
|
638 | - */ |
|
639 | - public function file_put_contents($path, $data) { |
|
640 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
641 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
642 | - if (Filesystem::isValidPath($path) |
|
643 | - and !Filesystem::isFileBlacklisted($path) |
|
644 | - ) { |
|
645 | - $path = $this->getRelativePath($absolutePath); |
|
646 | - |
|
647 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
648 | - |
|
649 | - $exists = $this->file_exists($path); |
|
650 | - $run = true; |
|
651 | - if ($this->shouldEmitHooks($path)) { |
|
652 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
653 | - } |
|
654 | - if (!$run) { |
|
655 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
656 | - return false; |
|
657 | - } |
|
658 | - |
|
659 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
660 | - |
|
661 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
662 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
663 | - $target = $storage->fopen($internalPath, 'w'); |
|
664 | - if ($target) { |
|
665 | - list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
666 | - fclose($target); |
|
667 | - fclose($data); |
|
668 | - |
|
669 | - $this->writeUpdate($storage, $internalPath); |
|
670 | - |
|
671 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
672 | - |
|
673 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
674 | - $this->emit_file_hooks_post($exists, $path); |
|
675 | - } |
|
676 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
677 | - return $result; |
|
678 | - } else { |
|
679 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
680 | - return false; |
|
681 | - } |
|
682 | - } else { |
|
683 | - return false; |
|
684 | - } |
|
685 | - } else { |
|
686 | - $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
687 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
688 | - } |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * @param string $path |
|
693 | - * @return bool|mixed |
|
694 | - */ |
|
695 | - public function unlink($path) { |
|
696 | - if ($path === '' || $path === '/') { |
|
697 | - // do not allow deleting the root |
|
698 | - return false; |
|
699 | - } |
|
700 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
701 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
702 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
703 | - if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
704 | - return $this->removeMount($mount, $absolutePath); |
|
705 | - } |
|
706 | - if ($this->is_dir($path)) { |
|
707 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
708 | - } else { |
|
709 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
710 | - } |
|
711 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
712 | - $storage = $mount->getStorage(); |
|
713 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
714 | - $storage->getUpdater()->remove($internalPath); |
|
715 | - return true; |
|
716 | - } else { |
|
717 | - return $result; |
|
718 | - } |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * @param string $directory |
|
723 | - * @return bool|mixed |
|
724 | - */ |
|
725 | - public function deleteAll($directory) { |
|
726 | - return $this->rmdir($directory); |
|
727 | - } |
|
728 | - |
|
729 | - /** |
|
730 | - * Rename/move a file or folder from the source path to target path. |
|
731 | - * |
|
732 | - * @param string $path1 source path |
|
733 | - * @param string $path2 target path |
|
734 | - * |
|
735 | - * @return bool|mixed |
|
736 | - */ |
|
737 | - public function rename($path1, $path2) { |
|
738 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
739 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
740 | - $result = false; |
|
741 | - if ( |
|
742 | - Filesystem::isValidPath($path2) |
|
743 | - and Filesystem::isValidPath($path1) |
|
744 | - and !Filesystem::isFileBlacklisted($path2) |
|
745 | - ) { |
|
746 | - $path1 = $this->getRelativePath($absolutePath1); |
|
747 | - $path2 = $this->getRelativePath($absolutePath2); |
|
748 | - $exists = $this->file_exists($path2); |
|
749 | - |
|
750 | - if ($path1 == null or $path2 == null) { |
|
751 | - return false; |
|
752 | - } |
|
753 | - |
|
754 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
755 | - try { |
|
756 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
757 | - |
|
758 | - $run = true; |
|
759 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
760 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
761 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
762 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
763 | - \OC_Hook::emit( |
|
764 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
765 | - array( |
|
766 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
767 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
768 | - Filesystem::signal_param_run => &$run |
|
769 | - ) |
|
770 | - ); |
|
771 | - } |
|
772 | - if ($run) { |
|
773 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
774 | - |
|
775 | - $manager = Filesystem::getMountManager(); |
|
776 | - $mount1 = $this->getMount($path1); |
|
777 | - $mount2 = $this->getMount($path2); |
|
778 | - $storage1 = $mount1->getStorage(); |
|
779 | - $storage2 = $mount2->getStorage(); |
|
780 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
781 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
782 | - |
|
783 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
784 | - try { |
|
785 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
786 | - |
|
787 | - if ($internalPath1 === '') { |
|
788 | - if ($mount1 instanceof MoveableMount) { |
|
789 | - if ($this->isTargetAllowed($absolutePath2)) { |
|
790 | - /** |
|
791 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
792 | - */ |
|
793 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
794 | - $result = $mount1->moveMount($absolutePath2); |
|
795 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
796 | - } else { |
|
797 | - $result = false; |
|
798 | - } |
|
799 | - } else { |
|
800 | - $result = false; |
|
801 | - } |
|
802 | - // moving a file/folder within the same mount point |
|
803 | - } elseif ($storage1 === $storage2) { |
|
804 | - if ($storage1) { |
|
805 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
806 | - } else { |
|
807 | - $result = false; |
|
808 | - } |
|
809 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
810 | - } else { |
|
811 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
812 | - } |
|
813 | - |
|
814 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
815 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
816 | - $this->writeUpdate($storage2, $internalPath2); |
|
817 | - } else if ($result) { |
|
818 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
819 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
820 | - } |
|
821 | - } |
|
822 | - } catch(\Exception $e) { |
|
823 | - throw $e; |
|
824 | - } finally { |
|
825 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
826 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
827 | - } |
|
828 | - |
|
829 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
830 | - if ($this->shouldEmitHooks()) { |
|
831 | - $this->emit_file_hooks_post($exists, $path2); |
|
832 | - } |
|
833 | - } elseif ($result) { |
|
834 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
835 | - \OC_Hook::emit( |
|
836 | - Filesystem::CLASSNAME, |
|
837 | - Filesystem::signal_post_rename, |
|
838 | - array( |
|
839 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
840 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
841 | - ) |
|
842 | - ); |
|
843 | - } |
|
844 | - } |
|
845 | - } |
|
846 | - } catch(\Exception $e) { |
|
847 | - throw $e; |
|
848 | - } finally { |
|
849 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
850 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
851 | - } |
|
852 | - } |
|
853 | - return $result; |
|
854 | - } |
|
855 | - |
|
856 | - /** |
|
857 | - * Copy a file/folder from the source path to target path |
|
858 | - * |
|
859 | - * @param string $path1 source path |
|
860 | - * @param string $path2 target path |
|
861 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
862 | - * |
|
863 | - * @return bool|mixed |
|
864 | - */ |
|
865 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
866 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
867 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
868 | - $result = false; |
|
869 | - if ( |
|
870 | - Filesystem::isValidPath($path2) |
|
871 | - and Filesystem::isValidPath($path1) |
|
872 | - and !Filesystem::isFileBlacklisted($path2) |
|
873 | - ) { |
|
874 | - $path1 = $this->getRelativePath($absolutePath1); |
|
875 | - $path2 = $this->getRelativePath($absolutePath2); |
|
876 | - |
|
877 | - if ($path1 == null or $path2 == null) { |
|
878 | - return false; |
|
879 | - } |
|
880 | - $run = true; |
|
881 | - |
|
882 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
883 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
884 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
885 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
886 | - |
|
887 | - try { |
|
888 | - |
|
889 | - $exists = $this->file_exists($path2); |
|
890 | - if ($this->shouldEmitHooks()) { |
|
891 | - \OC_Hook::emit( |
|
892 | - Filesystem::CLASSNAME, |
|
893 | - Filesystem::signal_copy, |
|
894 | - array( |
|
895 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
896 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
897 | - Filesystem::signal_param_run => &$run |
|
898 | - ) |
|
899 | - ); |
|
900 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
901 | - } |
|
902 | - if ($run) { |
|
903 | - $mount1 = $this->getMount($path1); |
|
904 | - $mount2 = $this->getMount($path2); |
|
905 | - $storage1 = $mount1->getStorage(); |
|
906 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
907 | - $storage2 = $mount2->getStorage(); |
|
908 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
909 | - |
|
910 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
911 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
912 | - |
|
913 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
914 | - if ($storage1) { |
|
915 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
916 | - } else { |
|
917 | - $result = false; |
|
918 | - } |
|
919 | - } else { |
|
920 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
921 | - } |
|
922 | - |
|
923 | - $this->writeUpdate($storage2, $internalPath2); |
|
924 | - |
|
925 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
926 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
927 | - |
|
928 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
929 | - \OC_Hook::emit( |
|
930 | - Filesystem::CLASSNAME, |
|
931 | - Filesystem::signal_post_copy, |
|
932 | - array( |
|
933 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
934 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
935 | - ) |
|
936 | - ); |
|
937 | - $this->emit_file_hooks_post($exists, $path2); |
|
938 | - } |
|
939 | - |
|
940 | - } |
|
941 | - } catch (\Exception $e) { |
|
942 | - $this->unlockFile($path2, $lockTypePath2); |
|
943 | - $this->unlockFile($path1, $lockTypePath1); |
|
944 | - throw $e; |
|
945 | - } |
|
946 | - |
|
947 | - $this->unlockFile($path2, $lockTypePath2); |
|
948 | - $this->unlockFile($path1, $lockTypePath1); |
|
949 | - |
|
950 | - } |
|
951 | - return $result; |
|
952 | - } |
|
953 | - |
|
954 | - /** |
|
955 | - * @param string $path |
|
956 | - * @param string $mode 'r' or 'w' |
|
957 | - * @return resource |
|
958 | - */ |
|
959 | - public function fopen($path, $mode) { |
|
960 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
961 | - $hooks = array(); |
|
962 | - switch ($mode) { |
|
963 | - case 'r': |
|
964 | - $hooks[] = 'read'; |
|
965 | - break; |
|
966 | - case 'r+': |
|
967 | - case 'w+': |
|
968 | - case 'x+': |
|
969 | - case 'a+': |
|
970 | - $hooks[] = 'read'; |
|
971 | - $hooks[] = 'write'; |
|
972 | - break; |
|
973 | - case 'w': |
|
974 | - case 'x': |
|
975 | - case 'a': |
|
976 | - $hooks[] = 'write'; |
|
977 | - break; |
|
978 | - default: |
|
979 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
980 | - } |
|
981 | - |
|
982 | - if ($mode !== 'r' && $mode !== 'w') { |
|
983 | - \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
984 | - } |
|
985 | - |
|
986 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
987 | - } |
|
988 | - |
|
989 | - /** |
|
990 | - * @param string $path |
|
991 | - * @return bool|string |
|
992 | - * @throws \OCP\Files\InvalidPathException |
|
993 | - */ |
|
994 | - public function toTmpFile($path) { |
|
995 | - $this->assertPathLength($path); |
|
996 | - if (Filesystem::isValidPath($path)) { |
|
997 | - $source = $this->fopen($path, 'r'); |
|
998 | - if ($source) { |
|
999 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1000 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1001 | - file_put_contents($tmpFile, $source); |
|
1002 | - return $tmpFile; |
|
1003 | - } else { |
|
1004 | - return false; |
|
1005 | - } |
|
1006 | - } else { |
|
1007 | - return false; |
|
1008 | - } |
|
1009 | - } |
|
1010 | - |
|
1011 | - /** |
|
1012 | - * @param string $tmpFile |
|
1013 | - * @param string $path |
|
1014 | - * @return bool|mixed |
|
1015 | - * @throws \OCP\Files\InvalidPathException |
|
1016 | - */ |
|
1017 | - public function fromTmpFile($tmpFile, $path) { |
|
1018 | - $this->assertPathLength($path); |
|
1019 | - if (Filesystem::isValidPath($path)) { |
|
1020 | - |
|
1021 | - // Get directory that the file is going into |
|
1022 | - $filePath = dirname($path); |
|
1023 | - |
|
1024 | - // Create the directories if any |
|
1025 | - if (!$this->file_exists($filePath)) { |
|
1026 | - $result = $this->createParentDirectories($filePath); |
|
1027 | - if ($result === false) { |
|
1028 | - return false; |
|
1029 | - } |
|
1030 | - } |
|
1031 | - |
|
1032 | - $source = fopen($tmpFile, 'r'); |
|
1033 | - if ($source) { |
|
1034 | - $result = $this->file_put_contents($path, $source); |
|
1035 | - // $this->file_put_contents() might have already closed |
|
1036 | - // the resource, so we check it, before trying to close it |
|
1037 | - // to avoid messages in the error log. |
|
1038 | - if (is_resource($source)) { |
|
1039 | - fclose($source); |
|
1040 | - } |
|
1041 | - unlink($tmpFile); |
|
1042 | - return $result; |
|
1043 | - } else { |
|
1044 | - return false; |
|
1045 | - } |
|
1046 | - } else { |
|
1047 | - return false; |
|
1048 | - } |
|
1049 | - } |
|
1050 | - |
|
1051 | - |
|
1052 | - /** |
|
1053 | - * @param string $path |
|
1054 | - * @return mixed |
|
1055 | - * @throws \OCP\Files\InvalidPathException |
|
1056 | - */ |
|
1057 | - public function getMimeType($path) { |
|
1058 | - $this->assertPathLength($path); |
|
1059 | - return $this->basicOperation('getMimeType', $path); |
|
1060 | - } |
|
1061 | - |
|
1062 | - /** |
|
1063 | - * @param string $type |
|
1064 | - * @param string $path |
|
1065 | - * @param bool $raw |
|
1066 | - * @return bool|null|string |
|
1067 | - */ |
|
1068 | - public function hash($type, $path, $raw = false) { |
|
1069 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1070 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1071 | - if (Filesystem::isValidPath($path)) { |
|
1072 | - $path = $this->getRelativePath($absolutePath); |
|
1073 | - if ($path == null) { |
|
1074 | - return false; |
|
1075 | - } |
|
1076 | - if ($this->shouldEmitHooks($path)) { |
|
1077 | - \OC_Hook::emit( |
|
1078 | - Filesystem::CLASSNAME, |
|
1079 | - Filesystem::signal_read, |
|
1080 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
1081 | - ); |
|
1082 | - } |
|
1083 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1084 | - if ($storage) { |
|
1085 | - return $storage->hash($type, $internalPath, $raw); |
|
1086 | - } |
|
1087 | - } |
|
1088 | - return null; |
|
1089 | - } |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * @param string $path |
|
1093 | - * @return mixed |
|
1094 | - * @throws \OCP\Files\InvalidPathException |
|
1095 | - */ |
|
1096 | - public function free_space($path = '/') { |
|
1097 | - $this->assertPathLength($path); |
|
1098 | - $result = $this->basicOperation('free_space', $path); |
|
1099 | - if ($result === null) { |
|
1100 | - throw new InvalidPathException(); |
|
1101 | - } |
|
1102 | - return $result; |
|
1103 | - } |
|
1104 | - |
|
1105 | - /** |
|
1106 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1107 | - * |
|
1108 | - * @param string $operation |
|
1109 | - * @param string $path |
|
1110 | - * @param array $hooks (optional) |
|
1111 | - * @param mixed $extraParam (optional) |
|
1112 | - * @return mixed |
|
1113 | - * @throws \Exception |
|
1114 | - * |
|
1115 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1116 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1117 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1118 | - */ |
|
1119 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1120 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1121 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1122 | - if (Filesystem::isValidPath($path) |
|
1123 | - and !Filesystem::isFileBlacklisted($path) |
|
1124 | - ) { |
|
1125 | - $path = $this->getRelativePath($absolutePath); |
|
1126 | - if ($path == null) { |
|
1127 | - return false; |
|
1128 | - } |
|
1129 | - |
|
1130 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1131 | - // always a shared lock during pre-hooks so the hook can read the file |
|
1132 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1133 | - } |
|
1134 | - |
|
1135 | - $run = $this->runHooks($hooks, $path); |
|
1136 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
1137 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1138 | - if ($run and $storage) { |
|
1139 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1140 | - try { |
|
1141 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1142 | - } catch (LockedException $e) { |
|
1143 | - // release the shared lock we acquired before quiting |
|
1144 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1145 | - throw $e; |
|
1146 | - } |
|
1147 | - } |
|
1148 | - try { |
|
1149 | - if (!is_null($extraParam)) { |
|
1150 | - $result = $storage->$operation($internalPath, $extraParam); |
|
1151 | - } else { |
|
1152 | - $result = $storage->$operation($internalPath); |
|
1153 | - } |
|
1154 | - } catch (\Exception $e) { |
|
1155 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1156 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1157 | - } else if (in_array('read', $hooks)) { |
|
1158 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1159 | - } |
|
1160 | - throw $e; |
|
1161 | - } |
|
1162 | - |
|
1163 | - if ($result && in_array('delete', $hooks) and $result) { |
|
1164 | - $this->removeUpdate($storage, $internalPath); |
|
1165 | - } |
|
1166 | - if ($result && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
1167 | - $this->writeUpdate($storage, $internalPath); |
|
1168 | - } |
|
1169 | - if ($result && in_array('touch', $hooks)) { |
|
1170 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1171 | - } |
|
1172 | - |
|
1173 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1174 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1175 | - } |
|
1176 | - |
|
1177 | - $unlockLater = false; |
|
1178 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1179 | - $unlockLater = true; |
|
1180 | - // make sure our unlocking callback will still be called if connection is aborted |
|
1181 | - ignore_user_abort(true); |
|
1182 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1183 | - if (in_array('write', $hooks)) { |
|
1184 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1185 | - } else if (in_array('read', $hooks)) { |
|
1186 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1187 | - } |
|
1188 | - }); |
|
1189 | - } |
|
1190 | - |
|
1191 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1192 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1193 | - $this->runHooks($hooks, $path, true); |
|
1194 | - } |
|
1195 | - } |
|
1196 | - |
|
1197 | - if (!$unlockLater |
|
1198 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1199 | - ) { |
|
1200 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1201 | - } |
|
1202 | - return $result; |
|
1203 | - } else { |
|
1204 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1205 | - } |
|
1206 | - } |
|
1207 | - return null; |
|
1208 | - } |
|
1209 | - |
|
1210 | - /** |
|
1211 | - * get the path relative to the default root for hook usage |
|
1212 | - * |
|
1213 | - * @param string $path |
|
1214 | - * @return string |
|
1215 | - */ |
|
1216 | - private function getHookPath($path) { |
|
1217 | - if (!Filesystem::getView()) { |
|
1218 | - return $path; |
|
1219 | - } |
|
1220 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1221 | - } |
|
1222 | - |
|
1223 | - private function shouldEmitHooks($path = '') { |
|
1224 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1225 | - return false; |
|
1226 | - } |
|
1227 | - if (!Filesystem::$loaded) { |
|
1228 | - return false; |
|
1229 | - } |
|
1230 | - $defaultRoot = Filesystem::getRoot(); |
|
1231 | - if ($defaultRoot === null) { |
|
1232 | - return false; |
|
1233 | - } |
|
1234 | - if ($this->fakeRoot === $defaultRoot) { |
|
1235 | - return true; |
|
1236 | - } |
|
1237 | - $fullPath = $this->getAbsolutePath($path); |
|
1238 | - |
|
1239 | - if ($fullPath === $defaultRoot) { |
|
1240 | - return true; |
|
1241 | - } |
|
1242 | - |
|
1243 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1244 | - } |
|
1245 | - |
|
1246 | - /** |
|
1247 | - * @param string[] $hooks |
|
1248 | - * @param string $path |
|
1249 | - * @param bool $post |
|
1250 | - * @return bool |
|
1251 | - */ |
|
1252 | - private function runHooks($hooks, $path, $post = false) { |
|
1253 | - $relativePath = $path; |
|
1254 | - $path = $this->getHookPath($path); |
|
1255 | - $prefix = $post ? 'post_' : ''; |
|
1256 | - $run = true; |
|
1257 | - if ($this->shouldEmitHooks($relativePath)) { |
|
1258 | - foreach ($hooks as $hook) { |
|
1259 | - if ($hook != 'read') { |
|
1260 | - \OC_Hook::emit( |
|
1261 | - Filesystem::CLASSNAME, |
|
1262 | - $prefix . $hook, |
|
1263 | - array( |
|
1264 | - Filesystem::signal_param_run => &$run, |
|
1265 | - Filesystem::signal_param_path => $path |
|
1266 | - ) |
|
1267 | - ); |
|
1268 | - } elseif (!$post) { |
|
1269 | - \OC_Hook::emit( |
|
1270 | - Filesystem::CLASSNAME, |
|
1271 | - $prefix . $hook, |
|
1272 | - array( |
|
1273 | - Filesystem::signal_param_path => $path |
|
1274 | - ) |
|
1275 | - ); |
|
1276 | - } |
|
1277 | - } |
|
1278 | - } |
|
1279 | - return $run; |
|
1280 | - } |
|
1281 | - |
|
1282 | - /** |
|
1283 | - * check if a file or folder has been updated since $time |
|
1284 | - * |
|
1285 | - * @param string $path |
|
1286 | - * @param int $time |
|
1287 | - * @return bool |
|
1288 | - */ |
|
1289 | - public function hasUpdated($path, $time) { |
|
1290 | - return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
1291 | - } |
|
1292 | - |
|
1293 | - /** |
|
1294 | - * @param string $ownerId |
|
1295 | - * @return \OC\User\User |
|
1296 | - */ |
|
1297 | - private function getUserObjectForOwner($ownerId) { |
|
1298 | - $owner = $this->userManager->get($ownerId); |
|
1299 | - if ($owner instanceof IUser) { |
|
1300 | - return $owner; |
|
1301 | - } else { |
|
1302 | - return new User($ownerId, null, \OC::$server->getEventDispatcher()); |
|
1303 | - } |
|
1304 | - } |
|
1305 | - |
|
1306 | - /** |
|
1307 | - * Get file info from cache |
|
1308 | - * |
|
1309 | - * If the file is not in cached it will be scanned |
|
1310 | - * If the file has changed on storage the cache will be updated |
|
1311 | - * |
|
1312 | - * @param \OC\Files\Storage\Storage $storage |
|
1313 | - * @param string $internalPath |
|
1314 | - * @param string $relativePath |
|
1315 | - * @return ICacheEntry|bool |
|
1316 | - */ |
|
1317 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1318 | - $cache = $storage->getCache($internalPath); |
|
1319 | - $data = $cache->get($internalPath); |
|
1320 | - $watcher = $storage->getWatcher($internalPath); |
|
1321 | - |
|
1322 | - try { |
|
1323 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1324 | - if (!$data || $data['size'] === -1) { |
|
1325 | - if (!$storage->file_exists($internalPath)) { |
|
1326 | - return false; |
|
1327 | - } |
|
1328 | - // don't need to get a lock here since the scanner does it's own locking |
|
1329 | - $scanner = $storage->getScanner($internalPath); |
|
1330 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1331 | - $data = $cache->get($internalPath); |
|
1332 | - } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1333 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1334 | - $watcher->update($internalPath, $data); |
|
1335 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1336 | - $data = $cache->get($internalPath); |
|
1337 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1338 | - } |
|
1339 | - } catch (LockedException $e) { |
|
1340 | - // if the file is locked we just use the old cache info |
|
1341 | - } |
|
1342 | - |
|
1343 | - return $data; |
|
1344 | - } |
|
1345 | - |
|
1346 | - /** |
|
1347 | - * get the filesystem info |
|
1348 | - * |
|
1349 | - * @param string $path |
|
1350 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
1351 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1352 | - * defaults to true |
|
1353 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
1354 | - */ |
|
1355 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
1356 | - $this->assertPathLength($path); |
|
1357 | - if (!Filesystem::isValidPath($path)) { |
|
1358 | - return false; |
|
1359 | - } |
|
1360 | - if (Cache\Scanner::isPartialFile($path)) { |
|
1361 | - return $this->getPartFileInfo($path); |
|
1362 | - } |
|
1363 | - $relativePath = $path; |
|
1364 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1365 | - |
|
1366 | - $mount = Filesystem::getMountManager()->find($path); |
|
1367 | - if (!$mount) { |
|
1368 | - \OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path); |
|
1369 | - return false; |
|
1370 | - } |
|
1371 | - $storage = $mount->getStorage(); |
|
1372 | - $internalPath = $mount->getInternalPath($path); |
|
1373 | - if ($storage) { |
|
1374 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1375 | - |
|
1376 | - if (!$data instanceof ICacheEntry) { |
|
1377 | - \OC::$server->getLogger()->debug('No cache entry found for ' . $path . ' (storage: ' . $storage->getId() . ', internalPath: ' . $internalPath . ')'); |
|
1378 | - return false; |
|
1379 | - } |
|
1380 | - |
|
1381 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1382 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1383 | - } |
|
1384 | - $ownerId = $storage->getOwner($internalPath); |
|
1385 | - $owner = null; |
|
1386 | - if ($ownerId !== null) { |
|
1387 | - // ownerId might be null if files are accessed with an access token without file system access |
|
1388 | - $owner = $this->getUserObjectForOwner($ownerId); |
|
1389 | - } |
|
1390 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1391 | - |
|
1392 | - if ($data and isset($data['fileid'])) { |
|
1393 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1394 | - //add the sizes of other mount points to the folder |
|
1395 | - $extOnly = ($includeMountPoints === 'ext'); |
|
1396 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1397 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1398 | - $subStorage = $mount->getStorage(); |
|
1399 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1400 | - })); |
|
1401 | - } |
|
1402 | - } |
|
1403 | - |
|
1404 | - return $info; |
|
1405 | - } else { |
|
1406 | - \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
1407 | - } |
|
1408 | - |
|
1409 | - return false; |
|
1410 | - } |
|
1411 | - |
|
1412 | - /** |
|
1413 | - * get the content of a directory |
|
1414 | - * |
|
1415 | - * @param string $directory path under datadirectory |
|
1416 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1417 | - * @return FileInfo[] |
|
1418 | - */ |
|
1419 | - public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
1420 | - $this->assertPathLength($directory); |
|
1421 | - if (!Filesystem::isValidPath($directory)) { |
|
1422 | - return []; |
|
1423 | - } |
|
1424 | - $path = $this->getAbsolutePath($directory); |
|
1425 | - $path = Filesystem::normalizePath($path); |
|
1426 | - $mount = $this->getMount($directory); |
|
1427 | - if (!$mount) { |
|
1428 | - return []; |
|
1429 | - } |
|
1430 | - $storage = $mount->getStorage(); |
|
1431 | - $internalPath = $mount->getInternalPath($path); |
|
1432 | - if ($storage) { |
|
1433 | - $cache = $storage->getCache($internalPath); |
|
1434 | - $user = \OC_User::getUser(); |
|
1435 | - |
|
1436 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1437 | - |
|
1438 | - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
1439 | - return []; |
|
1440 | - } |
|
1441 | - |
|
1442 | - $folderId = $data['fileid']; |
|
1443 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1444 | - |
|
1445 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1446 | - |
|
1447 | - $fileNames = array_map(function(ICacheEntry $content) { |
|
1448 | - return $content->getName(); |
|
1449 | - }, $contents); |
|
1450 | - /** |
|
1451 | - * @var \OC\Files\FileInfo[] $fileInfos |
|
1452 | - */ |
|
1453 | - $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1454 | - if ($sharingDisabled) { |
|
1455 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1456 | - } |
|
1457 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1458 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1459 | - }, $contents); |
|
1460 | - $files = array_combine($fileNames, $fileInfos); |
|
1461 | - |
|
1462 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1463 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
1464 | - $dirLength = strlen($path); |
|
1465 | - foreach ($mounts as $mount) { |
|
1466 | - $mountPoint = $mount->getMountPoint(); |
|
1467 | - $subStorage = $mount->getStorage(); |
|
1468 | - if ($subStorage) { |
|
1469 | - $subCache = $subStorage->getCache(''); |
|
1470 | - |
|
1471 | - $rootEntry = $subCache->get(''); |
|
1472 | - if (!$rootEntry) { |
|
1473 | - $subScanner = $subStorage->getScanner(''); |
|
1474 | - try { |
|
1475 | - $subScanner->scanFile(''); |
|
1476 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1477 | - continue; |
|
1478 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
1479 | - continue; |
|
1480 | - } catch (\Exception $e) { |
|
1481 | - // sometimes when the storage is not available it can be any exception |
|
1482 | - \OC::$server->getLogger()->logException($e, [ |
|
1483 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1484 | - 'level' => ILogger::ERROR, |
|
1485 | - 'app' => 'lib', |
|
1486 | - ]); |
|
1487 | - continue; |
|
1488 | - } |
|
1489 | - $rootEntry = $subCache->get(''); |
|
1490 | - } |
|
1491 | - |
|
1492 | - if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
1493 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1494 | - if ($pos = strpos($relativePath, '/')) { |
|
1495 | - //mountpoint inside subfolder add size to the correct folder |
|
1496 | - $entryName = substr($relativePath, 0, $pos); |
|
1497 | - foreach ($files as &$entry) { |
|
1498 | - if ($entry->getName() === $entryName) { |
|
1499 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
1500 | - } |
|
1501 | - } |
|
1502 | - } else { //mountpoint in this folder, add an entry for it |
|
1503 | - $rootEntry['name'] = $relativePath; |
|
1504 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1505 | - $permissions = $rootEntry['permissions']; |
|
1506 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1507 | - // for shared files/folders we use the permissions given by the owner |
|
1508 | - if ($mount instanceof MoveableMount) { |
|
1509 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1510 | - } else { |
|
1511 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1512 | - } |
|
1513 | - |
|
1514 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1515 | - |
|
1516 | - // if sharing was disabled for the user we remove the share permissions |
|
1517 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
1518 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1519 | - } |
|
1520 | - |
|
1521 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1522 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1523 | - } |
|
1524 | - } |
|
1525 | - } |
|
1526 | - } |
|
1527 | - |
|
1528 | - if ($mimetype_filter) { |
|
1529 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1530 | - if (strpos($mimetype_filter, '/')) { |
|
1531 | - return $file->getMimetype() === $mimetype_filter; |
|
1532 | - } else { |
|
1533 | - return $file->getMimePart() === $mimetype_filter; |
|
1534 | - } |
|
1535 | - }); |
|
1536 | - } |
|
1537 | - |
|
1538 | - return array_values($files); |
|
1539 | - } else { |
|
1540 | - return []; |
|
1541 | - } |
|
1542 | - } |
|
1543 | - |
|
1544 | - /** |
|
1545 | - * change file metadata |
|
1546 | - * |
|
1547 | - * @param string $path |
|
1548 | - * @param array|\OCP\Files\FileInfo $data |
|
1549 | - * @return int |
|
1550 | - * |
|
1551 | - * returns the fileid of the updated file |
|
1552 | - */ |
|
1553 | - public function putFileInfo($path, $data) { |
|
1554 | - $this->assertPathLength($path); |
|
1555 | - if ($data instanceof FileInfo) { |
|
1556 | - $data = $data->getData(); |
|
1557 | - } |
|
1558 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1559 | - /** |
|
1560 | - * @var \OC\Files\Storage\Storage $storage |
|
1561 | - * @var string $internalPath |
|
1562 | - */ |
|
1563 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
1564 | - if ($storage) { |
|
1565 | - $cache = $storage->getCache($path); |
|
1566 | - |
|
1567 | - if (!$cache->inCache($internalPath)) { |
|
1568 | - $scanner = $storage->getScanner($internalPath); |
|
1569 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1570 | - } |
|
1571 | - |
|
1572 | - return $cache->put($internalPath, $data); |
|
1573 | - } else { |
|
1574 | - return -1; |
|
1575 | - } |
|
1576 | - } |
|
1577 | - |
|
1578 | - /** |
|
1579 | - * search for files with the name matching $query |
|
1580 | - * |
|
1581 | - * @param string $query |
|
1582 | - * @return FileInfo[] |
|
1583 | - */ |
|
1584 | - public function search($query) { |
|
1585 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
1586 | - } |
|
1587 | - |
|
1588 | - /** |
|
1589 | - * search for files with the name matching $query |
|
1590 | - * |
|
1591 | - * @param string $query |
|
1592 | - * @return FileInfo[] |
|
1593 | - */ |
|
1594 | - public function searchRaw($query) { |
|
1595 | - return $this->searchCommon('search', array($query)); |
|
1596 | - } |
|
1597 | - |
|
1598 | - /** |
|
1599 | - * search for files by mimetype |
|
1600 | - * |
|
1601 | - * @param string $mimetype |
|
1602 | - * @return FileInfo[] |
|
1603 | - */ |
|
1604 | - public function searchByMime($mimetype) { |
|
1605 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
1606 | - } |
|
1607 | - |
|
1608 | - /** |
|
1609 | - * search for files by tag |
|
1610 | - * |
|
1611 | - * @param string|int $tag name or tag id |
|
1612 | - * @param string $userId owner of the tags |
|
1613 | - * @return FileInfo[] |
|
1614 | - */ |
|
1615 | - public function searchByTag($tag, $userId) { |
|
1616 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
1617 | - } |
|
1618 | - |
|
1619 | - /** |
|
1620 | - * @param string $method cache method |
|
1621 | - * @param array $args |
|
1622 | - * @return FileInfo[] |
|
1623 | - */ |
|
1624 | - private function searchCommon($method, $args) { |
|
1625 | - $files = array(); |
|
1626 | - $rootLength = strlen($this->fakeRoot); |
|
1627 | - |
|
1628 | - $mount = $this->getMount(''); |
|
1629 | - $mountPoint = $mount->getMountPoint(); |
|
1630 | - $storage = $mount->getStorage(); |
|
1631 | - if ($storage) { |
|
1632 | - $cache = $storage->getCache(''); |
|
1633 | - |
|
1634 | - $results = call_user_func_array(array($cache, $method), $args); |
|
1635 | - foreach ($results as $result) { |
|
1636 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1637 | - $internalPath = $result['path']; |
|
1638 | - $path = $mountPoint . $result['path']; |
|
1639 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1640 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1641 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1642 | - } |
|
1643 | - } |
|
1644 | - |
|
1645 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1646 | - foreach ($mounts as $mount) { |
|
1647 | - $mountPoint = $mount->getMountPoint(); |
|
1648 | - $storage = $mount->getStorage(); |
|
1649 | - if ($storage) { |
|
1650 | - $cache = $storage->getCache(''); |
|
1651 | - |
|
1652 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1653 | - $results = call_user_func_array(array($cache, $method), $args); |
|
1654 | - if ($results) { |
|
1655 | - foreach ($results as $result) { |
|
1656 | - $internalPath = $result['path']; |
|
1657 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1658 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
1659 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1660 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1661 | - } |
|
1662 | - } |
|
1663 | - } |
|
1664 | - } |
|
1665 | - } |
|
1666 | - return $files; |
|
1667 | - } |
|
1668 | - |
|
1669 | - /** |
|
1670 | - * Get the owner for a file or folder |
|
1671 | - * |
|
1672 | - * @param string $path |
|
1673 | - * @return string the user id of the owner |
|
1674 | - * @throws NotFoundException |
|
1675 | - */ |
|
1676 | - public function getOwner($path) { |
|
1677 | - $info = $this->getFileInfo($path); |
|
1678 | - if (!$info) { |
|
1679 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1680 | - } |
|
1681 | - return $info->getOwner()->getUID(); |
|
1682 | - } |
|
1683 | - |
|
1684 | - /** |
|
1685 | - * get the ETag for a file or folder |
|
1686 | - * |
|
1687 | - * @param string $path |
|
1688 | - * @return string |
|
1689 | - */ |
|
1690 | - public function getETag($path) { |
|
1691 | - /** |
|
1692 | - * @var Storage\Storage $storage |
|
1693 | - * @var string $internalPath |
|
1694 | - */ |
|
1695 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
1696 | - if ($storage) { |
|
1697 | - return $storage->getETag($internalPath); |
|
1698 | - } else { |
|
1699 | - return null; |
|
1700 | - } |
|
1701 | - } |
|
1702 | - |
|
1703 | - /** |
|
1704 | - * Get the path of a file by id, relative to the view |
|
1705 | - * |
|
1706 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
1707 | - * |
|
1708 | - * @param int $id |
|
1709 | - * @throws NotFoundException |
|
1710 | - * @return string |
|
1711 | - */ |
|
1712 | - public function getPath($id) { |
|
1713 | - $id = (int)$id; |
|
1714 | - $manager = Filesystem::getMountManager(); |
|
1715 | - $mounts = $manager->findIn($this->fakeRoot); |
|
1716 | - $mounts[] = $manager->find($this->fakeRoot); |
|
1717 | - // reverse the array so we start with the storage this view is in |
|
1718 | - // which is the most likely to contain the file we're looking for |
|
1719 | - $mounts = array_reverse($mounts); |
|
1720 | - foreach ($mounts as $mount) { |
|
1721 | - /** |
|
1722 | - * @var \OC\Files\Mount\MountPoint $mount |
|
1723 | - */ |
|
1724 | - if ($mount->getStorage()) { |
|
1725 | - $cache = $mount->getStorage()->getCache(); |
|
1726 | - $internalPath = $cache->getPathById($id); |
|
1727 | - if (is_string($internalPath)) { |
|
1728 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
1729 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1730 | - return $path; |
|
1731 | - } |
|
1732 | - } |
|
1733 | - } |
|
1734 | - } |
|
1735 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1736 | - } |
|
1737 | - |
|
1738 | - /** |
|
1739 | - * @param string $path |
|
1740 | - * @throws InvalidPathException |
|
1741 | - */ |
|
1742 | - private function assertPathLength($path) { |
|
1743 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1744 | - // Check for the string length - performed using isset() instead of strlen() |
|
1745 | - // because isset() is about 5x-40x faster. |
|
1746 | - if (isset($path[$maxLen])) { |
|
1747 | - $pathLen = strlen($path); |
|
1748 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1749 | - } |
|
1750 | - } |
|
1751 | - |
|
1752 | - /** |
|
1753 | - * check if it is allowed to move a mount point to a given target. |
|
1754 | - * It is not allowed to move a mount point into a different mount point or |
|
1755 | - * into an already shared folder |
|
1756 | - * |
|
1757 | - * @param string $target path |
|
1758 | - * @return boolean |
|
1759 | - */ |
|
1760 | - private function isTargetAllowed($target) { |
|
1761 | - |
|
1762 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
1763 | - if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
1764 | - \OCP\Util::writeLog('files', |
|
1765 | - 'It is not allowed to move one mount point into another one', |
|
1766 | - ILogger::DEBUG); |
|
1767 | - return false; |
|
1768 | - } |
|
1769 | - |
|
1770 | - // note: cannot use the view because the target is already locked |
|
1771 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1772 | - if ($fileId === -1) { |
|
1773 | - // target might not exist, need to check parent instead |
|
1774 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1775 | - } |
|
1776 | - |
|
1777 | - // check if any of the parents were shared by the current owner (include collections) |
|
1778 | - $shares = \OCP\Share::getItemShared( |
|
1779 | - 'folder', |
|
1780 | - $fileId, |
|
1781 | - \OCP\Share::FORMAT_NONE, |
|
1782 | - null, |
|
1783 | - true |
|
1784 | - ); |
|
1785 | - |
|
1786 | - if (count($shares) > 0) { |
|
1787 | - \OCP\Util::writeLog('files', |
|
1788 | - 'It is not allowed to move one mount point into a shared folder', |
|
1789 | - ILogger::DEBUG); |
|
1790 | - return false; |
|
1791 | - } |
|
1792 | - |
|
1793 | - return true; |
|
1794 | - } |
|
1795 | - |
|
1796 | - /** |
|
1797 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1798 | - * |
|
1799 | - * @param string $path |
|
1800 | - * @return \OCP\Files\FileInfo |
|
1801 | - */ |
|
1802 | - private function getPartFileInfo($path) { |
|
1803 | - $mount = $this->getMount($path); |
|
1804 | - $storage = $mount->getStorage(); |
|
1805 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1806 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1807 | - return new FileInfo( |
|
1808 | - $this->getAbsolutePath($path), |
|
1809 | - $storage, |
|
1810 | - $internalPath, |
|
1811 | - [ |
|
1812 | - 'fileid' => null, |
|
1813 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
1814 | - 'name' => basename($path), |
|
1815 | - 'etag' => null, |
|
1816 | - 'size' => $storage->filesize($internalPath), |
|
1817 | - 'mtime' => $storage->filemtime($internalPath), |
|
1818 | - 'encrypted' => false, |
|
1819 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1820 | - ], |
|
1821 | - $mount, |
|
1822 | - $owner |
|
1823 | - ); |
|
1824 | - } |
|
1825 | - |
|
1826 | - /** |
|
1827 | - * @param string $path |
|
1828 | - * @param string $fileName |
|
1829 | - * @throws InvalidPathException |
|
1830 | - */ |
|
1831 | - public function verifyPath($path, $fileName) { |
|
1832 | - try { |
|
1833 | - /** @type \OCP\Files\Storage $storage */ |
|
1834 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
1835 | - $storage->verifyPath($internalPath, $fileName); |
|
1836 | - } catch (ReservedWordException $ex) { |
|
1837 | - $l = \OC::$server->getL10N('lib'); |
|
1838 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1839 | - } catch (InvalidCharacterInPathException $ex) { |
|
1840 | - $l = \OC::$server->getL10N('lib'); |
|
1841 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1842 | - } catch (FileNameTooLongException $ex) { |
|
1843 | - $l = \OC::$server->getL10N('lib'); |
|
1844 | - throw new InvalidPathException($l->t('File name is too long')); |
|
1845 | - } catch (InvalidDirectoryException $ex) { |
|
1846 | - $l = \OC::$server->getL10N('lib'); |
|
1847 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1848 | - } catch (EmptyFileNameException $ex) { |
|
1849 | - $l = \OC::$server->getL10N('lib'); |
|
1850 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1851 | - } |
|
1852 | - } |
|
1853 | - |
|
1854 | - /** |
|
1855 | - * get all parent folders of $path |
|
1856 | - * |
|
1857 | - * @param string $path |
|
1858 | - * @return string[] |
|
1859 | - */ |
|
1860 | - private function getParents($path) { |
|
1861 | - $path = trim($path, '/'); |
|
1862 | - if (!$path) { |
|
1863 | - return []; |
|
1864 | - } |
|
1865 | - |
|
1866 | - $parts = explode('/', $path); |
|
1867 | - |
|
1868 | - // remove the single file |
|
1869 | - array_pop($parts); |
|
1870 | - $result = array('/'); |
|
1871 | - $resultPath = ''; |
|
1872 | - foreach ($parts as $part) { |
|
1873 | - if ($part) { |
|
1874 | - $resultPath .= '/' . $part; |
|
1875 | - $result[] = $resultPath; |
|
1876 | - } |
|
1877 | - } |
|
1878 | - return $result; |
|
1879 | - } |
|
1880 | - |
|
1881 | - /** |
|
1882 | - * Returns the mount point for which to lock |
|
1883 | - * |
|
1884 | - * @param string $absolutePath absolute path |
|
1885 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
1886 | - * is mounted directly on the given path, false otherwise |
|
1887 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
1888 | - */ |
|
1889 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1890 | - $results = []; |
|
1891 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1892 | - if (!$mount) { |
|
1893 | - return $results; |
|
1894 | - } |
|
1895 | - |
|
1896 | - if ($useParentMount) { |
|
1897 | - // find out if something is mounted directly on the path |
|
1898 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
1899 | - if ($internalPath === '') { |
|
1900 | - // resolve the parent mount instead |
|
1901 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1902 | - } |
|
1903 | - } |
|
1904 | - |
|
1905 | - return $mount; |
|
1906 | - } |
|
1907 | - |
|
1908 | - /** |
|
1909 | - * Lock the given path |
|
1910 | - * |
|
1911 | - * @param string $path the path of the file to lock, relative to the view |
|
1912 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1913 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1914 | - * |
|
1915 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1916 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
1917 | - */ |
|
1918 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
1919 | - $absolutePath = $this->getAbsolutePath($path); |
|
1920 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1921 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1922 | - return false; |
|
1923 | - } |
|
1924 | - |
|
1925 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1926 | - if ($mount) { |
|
1927 | - try { |
|
1928 | - $storage = $mount->getStorage(); |
|
1929 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1930 | - $storage->acquireLock( |
|
1931 | - $mount->getInternalPath($absolutePath), |
|
1932 | - $type, |
|
1933 | - $this->lockingProvider |
|
1934 | - ); |
|
1935 | - } |
|
1936 | - } catch (\OCP\Lock\LockedException $e) { |
|
1937 | - // rethrow with the a human-readable path |
|
1938 | - throw new \OCP\Lock\LockedException( |
|
1939 | - $this->getPathRelativeToFiles($absolutePath), |
|
1940 | - $e |
|
1941 | - ); |
|
1942 | - } |
|
1943 | - } |
|
1944 | - |
|
1945 | - return true; |
|
1946 | - } |
|
1947 | - |
|
1948 | - /** |
|
1949 | - * Change the lock type |
|
1950 | - * |
|
1951 | - * @param string $path the path of the file to lock, relative to the view |
|
1952 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1953 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1954 | - * |
|
1955 | - * @return bool False if the path is excluded from locking, true otherwise |
|
1956 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
1957 | - */ |
|
1958 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
1959 | - $path = Filesystem::normalizePath($path); |
|
1960 | - $absolutePath = $this->getAbsolutePath($path); |
|
1961 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1962 | - if (!$this->shouldLockFile($absolutePath)) { |
|
1963 | - return false; |
|
1964 | - } |
|
1965 | - |
|
1966 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1967 | - if ($mount) { |
|
1968 | - try { |
|
1969 | - $storage = $mount->getStorage(); |
|
1970 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1971 | - $storage->changeLock( |
|
1972 | - $mount->getInternalPath($absolutePath), |
|
1973 | - $type, |
|
1974 | - $this->lockingProvider |
|
1975 | - ); |
|
1976 | - } |
|
1977 | - } catch (\OCP\Lock\LockedException $e) { |
|
1978 | - try { |
|
1979 | - // rethrow with the a human-readable path |
|
1980 | - throw new \OCP\Lock\LockedException( |
|
1981 | - $this->getPathRelativeToFiles($absolutePath), |
|
1982 | - $e |
|
1983 | - ); |
|
1984 | - } catch (\InvalidArgumentException $e) { |
|
1985 | - throw new \OCP\Lock\LockedException( |
|
1986 | - $absolutePath, |
|
1987 | - $e |
|
1988 | - ); |
|
1989 | - } |
|
1990 | - } |
|
1991 | - } |
|
1992 | - |
|
1993 | - return true; |
|
1994 | - } |
|
1995 | - |
|
1996 | - /** |
|
1997 | - * Unlock the given path |
|
1998 | - * |
|
1999 | - * @param string $path the path of the file to unlock, relative to the view |
|
2000 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2001 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2002 | - * |
|
2003 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2004 | - */ |
|
2005 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
2006 | - $absolutePath = $this->getAbsolutePath($path); |
|
2007 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2008 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2009 | - return false; |
|
2010 | - } |
|
2011 | - |
|
2012 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2013 | - if ($mount) { |
|
2014 | - $storage = $mount->getStorage(); |
|
2015 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2016 | - $storage->releaseLock( |
|
2017 | - $mount->getInternalPath($absolutePath), |
|
2018 | - $type, |
|
2019 | - $this->lockingProvider |
|
2020 | - ); |
|
2021 | - } |
|
2022 | - } |
|
2023 | - |
|
2024 | - return true; |
|
2025 | - } |
|
2026 | - |
|
2027 | - /** |
|
2028 | - * Lock a path and all its parents up to the root of the view |
|
2029 | - * |
|
2030 | - * @param string $path the path of the file to lock relative to the view |
|
2031 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2032 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2033 | - * |
|
2034 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2035 | - */ |
|
2036 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
2037 | - $absolutePath = $this->getAbsolutePath($path); |
|
2038 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2039 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2040 | - return false; |
|
2041 | - } |
|
2042 | - |
|
2043 | - $this->lockPath($path, $type, $lockMountPoint); |
|
2044 | - |
|
2045 | - $parents = $this->getParents($path); |
|
2046 | - foreach ($parents as $parent) { |
|
2047 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2048 | - } |
|
2049 | - |
|
2050 | - return true; |
|
2051 | - } |
|
2052 | - |
|
2053 | - /** |
|
2054 | - * Unlock a path and all its parents up to the root of the view |
|
2055 | - * |
|
2056 | - * @param string $path the path of the file to lock relative to the view |
|
2057 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2058 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2059 | - * |
|
2060 | - * @return bool False if the path is excluded from locking, true otherwise |
|
2061 | - */ |
|
2062 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2063 | - $absolutePath = $this->getAbsolutePath($path); |
|
2064 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2065 | - if (!$this->shouldLockFile($absolutePath)) { |
|
2066 | - return false; |
|
2067 | - } |
|
2068 | - |
|
2069 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
2070 | - |
|
2071 | - $parents = $this->getParents($path); |
|
2072 | - foreach ($parents as $parent) { |
|
2073 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2074 | - } |
|
2075 | - |
|
2076 | - return true; |
|
2077 | - } |
|
2078 | - |
|
2079 | - /** |
|
2080 | - * Only lock files in data/user/files/ |
|
2081 | - * |
|
2082 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2083 | - * @return bool |
|
2084 | - */ |
|
2085 | - protected function shouldLockFile($path) { |
|
2086 | - $path = Filesystem::normalizePath($path); |
|
2087 | - |
|
2088 | - $pathSegments = explode('/', $path); |
|
2089 | - if (isset($pathSegments[2])) { |
|
2090 | - // E.g.: /username/files/path-to-file |
|
2091 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2092 | - } |
|
2093 | - |
|
2094 | - return strpos($path, '/appdata_') !== 0; |
|
2095 | - } |
|
2096 | - |
|
2097 | - /** |
|
2098 | - * Shortens the given absolute path to be relative to |
|
2099 | - * "$user/files". |
|
2100 | - * |
|
2101 | - * @param string $absolutePath absolute path which is under "files" |
|
2102 | - * |
|
2103 | - * @return string path relative to "files" with trimmed slashes or null |
|
2104 | - * if the path was NOT relative to files |
|
2105 | - * |
|
2106 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
2107 | - * @since 8.1.0 |
|
2108 | - */ |
|
2109 | - public function getPathRelativeToFiles($absolutePath) { |
|
2110 | - $path = Filesystem::normalizePath($absolutePath); |
|
2111 | - $parts = explode('/', trim($path, '/'), 3); |
|
2112 | - // "$user", "files", "path/to/dir" |
|
2113 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2114 | - $this->logger->error( |
|
2115 | - '$absolutePath must be relative to "files", value is "%s"', |
|
2116 | - [ |
|
2117 | - $absolutePath |
|
2118 | - ] |
|
2119 | - ); |
|
2120 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2121 | - } |
|
2122 | - if (isset($parts[2])) { |
|
2123 | - return $parts[2]; |
|
2124 | - } |
|
2125 | - return ''; |
|
2126 | - } |
|
2127 | - |
|
2128 | - /** |
|
2129 | - * @param string $filename |
|
2130 | - * @return array |
|
2131 | - * @throws \OC\User\NoUserException |
|
2132 | - * @throws NotFoundException |
|
2133 | - */ |
|
2134 | - public function getUidAndFilename($filename) { |
|
2135 | - $info = $this->getFileInfo($filename); |
|
2136 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
2137 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2138 | - } |
|
2139 | - $uid = $info->getOwner()->getUID(); |
|
2140 | - if ($uid != \OCP\User::getUser()) { |
|
2141 | - Filesystem::initMountPoints($uid); |
|
2142 | - $ownerView = new View('/' . $uid . '/files'); |
|
2143 | - try { |
|
2144 | - $filename = $ownerView->getPath($info['fileid']); |
|
2145 | - } catch (NotFoundException $e) { |
|
2146 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2147 | - } |
|
2148 | - } |
|
2149 | - return [$uid, $filename]; |
|
2150 | - } |
|
2151 | - |
|
2152 | - /** |
|
2153 | - * Creates parent non-existing folders |
|
2154 | - * |
|
2155 | - * @param string $filePath |
|
2156 | - * @return bool |
|
2157 | - */ |
|
2158 | - private function createParentDirectories($filePath) { |
|
2159 | - $directoryParts = explode('/', $filePath); |
|
2160 | - $directoryParts = array_filter($directoryParts); |
|
2161 | - foreach ($directoryParts as $key => $part) { |
|
2162 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2163 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
2164 | - if ($this->is_file($currentPath)) { |
|
2165 | - return false; |
|
2166 | - } |
|
2167 | - if (!$this->file_exists($currentPath)) { |
|
2168 | - $this->mkdir($currentPath); |
|
2169 | - } |
|
2170 | - } |
|
2171 | - |
|
2172 | - return true; |
|
2173 | - } |
|
84 | + /** @var string */ |
|
85 | + private $fakeRoot = ''; |
|
86 | + |
|
87 | + /** |
|
88 | + * @var \OCP\Lock\ILockingProvider |
|
89 | + */ |
|
90 | + protected $lockingProvider; |
|
91 | + |
|
92 | + private $lockingEnabled; |
|
93 | + |
|
94 | + private $updaterEnabled = true; |
|
95 | + |
|
96 | + /** @var \OC\User\Manager */ |
|
97 | + private $userManager; |
|
98 | + |
|
99 | + /** @var \OCP\ILogger */ |
|
100 | + private $logger; |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $root |
|
104 | + * @throws \Exception If $root contains an invalid path |
|
105 | + */ |
|
106 | + public function __construct($root = '') { |
|
107 | + if (is_null($root)) { |
|
108 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
109 | + } |
|
110 | + if (!Filesystem::isValidPath($root)) { |
|
111 | + throw new \Exception(); |
|
112 | + } |
|
113 | + |
|
114 | + $this->fakeRoot = $root; |
|
115 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
116 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
117 | + $this->userManager = \OC::$server->getUserManager(); |
|
118 | + $this->logger = \OC::$server->getLogger(); |
|
119 | + } |
|
120 | + |
|
121 | + public function getAbsolutePath($path = '/') { |
|
122 | + if ($path === null) { |
|
123 | + return null; |
|
124 | + } |
|
125 | + $this->assertPathLength($path); |
|
126 | + if ($path === '') { |
|
127 | + $path = '/'; |
|
128 | + } |
|
129 | + if ($path[0] !== '/') { |
|
130 | + $path = '/' . $path; |
|
131 | + } |
|
132 | + return $this->fakeRoot . $path; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * change the root to a fake root |
|
137 | + * |
|
138 | + * @param string $fakeRoot |
|
139 | + * @return boolean|null |
|
140 | + */ |
|
141 | + public function chroot($fakeRoot) { |
|
142 | + if (!$fakeRoot == '') { |
|
143 | + if ($fakeRoot[0] !== '/') { |
|
144 | + $fakeRoot = '/' . $fakeRoot; |
|
145 | + } |
|
146 | + } |
|
147 | + $this->fakeRoot = $fakeRoot; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * get the fake root |
|
152 | + * |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + public function getRoot() { |
|
156 | + return $this->fakeRoot; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * get path relative to the root of the view |
|
161 | + * |
|
162 | + * @param string $path |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function getRelativePath($path) { |
|
166 | + $this->assertPathLength($path); |
|
167 | + if ($this->fakeRoot == '') { |
|
168 | + return $path; |
|
169 | + } |
|
170 | + |
|
171 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
172 | + return '/'; |
|
173 | + } |
|
174 | + |
|
175 | + // missing slashes can cause wrong matches! |
|
176 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
177 | + |
|
178 | + if (strpos($path, $root) !== 0) { |
|
179 | + return null; |
|
180 | + } else { |
|
181 | + $path = substr($path, strlen($this->fakeRoot)); |
|
182 | + if (strlen($path) === 0) { |
|
183 | + return '/'; |
|
184 | + } else { |
|
185 | + return $path; |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * get the mountpoint of the storage object for a path |
|
192 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
193 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
194 | + * and does not take the chroot into account ) |
|
195 | + * |
|
196 | + * @param string $path |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function getMountPoint($path) { |
|
200 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * get the mountpoint of the storage object for a path |
|
205 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
206 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
207 | + * and does not take the chroot into account ) |
|
208 | + * |
|
209 | + * @param string $path |
|
210 | + * @return \OCP\Files\Mount\IMountPoint |
|
211 | + */ |
|
212 | + public function getMount($path) { |
|
213 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * resolve a path to a storage and internal path |
|
218 | + * |
|
219 | + * @param string $path |
|
220 | + * @return array an array consisting of the storage and the internal path |
|
221 | + */ |
|
222 | + public function resolvePath($path) { |
|
223 | + $a = $this->getAbsolutePath($path); |
|
224 | + $p = Filesystem::normalizePath($a); |
|
225 | + return Filesystem::resolvePath($p); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * return the path to a local version of the file |
|
230 | + * we need this because we can't know if a file is stored local or not from |
|
231 | + * outside the filestorage and for some purposes a local file is needed |
|
232 | + * |
|
233 | + * @param string $path |
|
234 | + * @return string |
|
235 | + */ |
|
236 | + public function getLocalFile($path) { |
|
237 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
238 | + $path = $this->getAbsolutePath($path); |
|
239 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
240 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
241 | + return $storage->getLocalFile($internalPath); |
|
242 | + } else { |
|
243 | + return null; |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @param string $path |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + public function getLocalFolder($path) { |
|
252 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
253 | + $path = $this->getAbsolutePath($path); |
|
254 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
255 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
256 | + return $storage->getLocalFolder($internalPath); |
|
257 | + } else { |
|
258 | + return null; |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * the following functions operate with arguments and return values identical |
|
264 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
265 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
266 | + */ |
|
267 | + public function mkdir($path) { |
|
268 | + return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * remove mount point |
|
273 | + * |
|
274 | + * @param \OC\Files\Mount\MoveableMount $mount |
|
275 | + * @param string $path relative to data/ |
|
276 | + * @return boolean |
|
277 | + */ |
|
278 | + protected function removeMount($mount, $path) { |
|
279 | + if ($mount instanceof MoveableMount) { |
|
280 | + // cut of /user/files to get the relative path to data/user/files |
|
281 | + $pathParts = explode('/', $path, 4); |
|
282 | + $relPath = '/' . $pathParts[3]; |
|
283 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
284 | + \OC_Hook::emit( |
|
285 | + Filesystem::CLASSNAME, "umount", |
|
286 | + array(Filesystem::signal_param_path => $relPath) |
|
287 | + ); |
|
288 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
289 | + $result = $mount->removeMount(); |
|
290 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
291 | + if ($result) { |
|
292 | + \OC_Hook::emit( |
|
293 | + Filesystem::CLASSNAME, "post_umount", |
|
294 | + array(Filesystem::signal_param_path => $relPath) |
|
295 | + ); |
|
296 | + } |
|
297 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
298 | + return $result; |
|
299 | + } else { |
|
300 | + // do not allow deleting the storage's root / the mount point |
|
301 | + // because for some storages it might delete the whole contents |
|
302 | + // but isn't supposed to work that way |
|
303 | + return false; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + public function disableCacheUpdate() { |
|
308 | + $this->updaterEnabled = false; |
|
309 | + } |
|
310 | + |
|
311 | + public function enableCacheUpdate() { |
|
312 | + $this->updaterEnabled = true; |
|
313 | + } |
|
314 | + |
|
315 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
316 | + if ($this->updaterEnabled) { |
|
317 | + if (is_null($time)) { |
|
318 | + $time = time(); |
|
319 | + } |
|
320 | + $storage->getUpdater()->update($internalPath, $time); |
|
321 | + } |
|
322 | + } |
|
323 | + |
|
324 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
325 | + if ($this->updaterEnabled) { |
|
326 | + $storage->getUpdater()->remove($internalPath); |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
331 | + if ($this->updaterEnabled) { |
|
332 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @param string $path |
|
338 | + * @return bool|mixed |
|
339 | + */ |
|
340 | + public function rmdir($path) { |
|
341 | + $absolutePath = $this->getAbsolutePath($path); |
|
342 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
343 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
344 | + return $this->removeMount($mount, $absolutePath); |
|
345 | + } |
|
346 | + if ($this->is_dir($path)) { |
|
347 | + $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
348 | + } else { |
|
349 | + $result = false; |
|
350 | + } |
|
351 | + |
|
352 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
353 | + $storage = $mount->getStorage(); |
|
354 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
355 | + $storage->getUpdater()->remove($internalPath); |
|
356 | + } |
|
357 | + return $result; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * @param string $path |
|
362 | + * @return resource |
|
363 | + */ |
|
364 | + public function opendir($path) { |
|
365 | + return $this->basicOperation('opendir', $path, array('read')); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * @param string $path |
|
370 | + * @return bool|mixed |
|
371 | + */ |
|
372 | + public function is_dir($path) { |
|
373 | + if ($path == '/') { |
|
374 | + return true; |
|
375 | + } |
|
376 | + return $this->basicOperation('is_dir', $path); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * @param string $path |
|
381 | + * @return bool|mixed |
|
382 | + */ |
|
383 | + public function is_file($path) { |
|
384 | + if ($path == '/') { |
|
385 | + return false; |
|
386 | + } |
|
387 | + return $this->basicOperation('is_file', $path); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * @param string $path |
|
392 | + * @return mixed |
|
393 | + */ |
|
394 | + public function stat($path) { |
|
395 | + return $this->basicOperation('stat', $path); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param string $path |
|
400 | + * @return mixed |
|
401 | + */ |
|
402 | + public function filetype($path) { |
|
403 | + return $this->basicOperation('filetype', $path); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * @param string $path |
|
408 | + * @return mixed |
|
409 | + */ |
|
410 | + public function filesize($path) { |
|
411 | + return $this->basicOperation('filesize', $path); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * @param string $path |
|
416 | + * @return bool|mixed |
|
417 | + * @throws \OCP\Files\InvalidPathException |
|
418 | + */ |
|
419 | + public function readfile($path) { |
|
420 | + $this->assertPathLength($path); |
|
421 | + @ob_end_clean(); |
|
422 | + $handle = $this->fopen($path, 'rb'); |
|
423 | + if ($handle) { |
|
424 | + $chunkSize = 8192; // 8 kB chunks |
|
425 | + while (!feof($handle)) { |
|
426 | + echo fread($handle, $chunkSize); |
|
427 | + flush(); |
|
428 | + } |
|
429 | + fclose($handle); |
|
430 | + return $this->filesize($path); |
|
431 | + } |
|
432 | + return false; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * @param string $path |
|
437 | + * @param int $from |
|
438 | + * @param int $to |
|
439 | + * @return bool|mixed |
|
440 | + * @throws \OCP\Files\InvalidPathException |
|
441 | + * @throws \OCP\Files\UnseekableException |
|
442 | + */ |
|
443 | + public function readfilePart($path, $from, $to) { |
|
444 | + $this->assertPathLength($path); |
|
445 | + @ob_end_clean(); |
|
446 | + $handle = $this->fopen($path, 'rb'); |
|
447 | + if ($handle) { |
|
448 | + $chunkSize = 8192; // 8 kB chunks |
|
449 | + $startReading = true; |
|
450 | + |
|
451 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
452 | + // forward file handle via chunked fread because fseek seem to have failed |
|
453 | + |
|
454 | + $end = $from + 1; |
|
455 | + while (!feof($handle) && ftell($handle) < $end) { |
|
456 | + $len = $from - ftell($handle); |
|
457 | + if ($len > $chunkSize) { |
|
458 | + $len = $chunkSize; |
|
459 | + } |
|
460 | + $result = fread($handle, $len); |
|
461 | + |
|
462 | + if ($result === false) { |
|
463 | + $startReading = false; |
|
464 | + break; |
|
465 | + } |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + if ($startReading) { |
|
470 | + $end = $to + 1; |
|
471 | + while (!feof($handle) && ftell($handle) < $end) { |
|
472 | + $len = $end - ftell($handle); |
|
473 | + if ($len > $chunkSize) { |
|
474 | + $len = $chunkSize; |
|
475 | + } |
|
476 | + echo fread($handle, $len); |
|
477 | + flush(); |
|
478 | + } |
|
479 | + return ftell($handle) - $from; |
|
480 | + } |
|
481 | + |
|
482 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
483 | + } |
|
484 | + return false; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * @param string $path |
|
489 | + * @return mixed |
|
490 | + */ |
|
491 | + public function isCreatable($path) { |
|
492 | + return $this->basicOperation('isCreatable', $path); |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * @param string $path |
|
497 | + * @return mixed |
|
498 | + */ |
|
499 | + public function isReadable($path) { |
|
500 | + return $this->basicOperation('isReadable', $path); |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * @param string $path |
|
505 | + * @return mixed |
|
506 | + */ |
|
507 | + public function isUpdatable($path) { |
|
508 | + return $this->basicOperation('isUpdatable', $path); |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * @param string $path |
|
513 | + * @return bool|mixed |
|
514 | + */ |
|
515 | + public function isDeletable($path) { |
|
516 | + $absolutePath = $this->getAbsolutePath($path); |
|
517 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
518 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
519 | + return $mount instanceof MoveableMount; |
|
520 | + } |
|
521 | + return $this->basicOperation('isDeletable', $path); |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * @param string $path |
|
526 | + * @return mixed |
|
527 | + */ |
|
528 | + public function isSharable($path) { |
|
529 | + return $this->basicOperation('isSharable', $path); |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * @param string $path |
|
534 | + * @return bool|mixed |
|
535 | + */ |
|
536 | + public function file_exists($path) { |
|
537 | + if ($path == '/') { |
|
538 | + return true; |
|
539 | + } |
|
540 | + return $this->basicOperation('file_exists', $path); |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * @param string $path |
|
545 | + * @return mixed |
|
546 | + */ |
|
547 | + public function filemtime($path) { |
|
548 | + return $this->basicOperation('filemtime', $path); |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * @param string $path |
|
553 | + * @param int|string $mtime |
|
554 | + * @return bool |
|
555 | + */ |
|
556 | + public function touch($path, $mtime = null) { |
|
557 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
558 | + $mtime = strtotime($mtime); |
|
559 | + } |
|
560 | + |
|
561 | + $hooks = array('touch'); |
|
562 | + |
|
563 | + if (!$this->file_exists($path)) { |
|
564 | + $hooks[] = 'create'; |
|
565 | + $hooks[] = 'write'; |
|
566 | + } |
|
567 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
568 | + if (!$result) { |
|
569 | + // If create file fails because of permissions on external storage like SMB folders, |
|
570 | + // check file exists and return false if not. |
|
571 | + if (!$this->file_exists($path)) { |
|
572 | + return false; |
|
573 | + } |
|
574 | + if (is_null($mtime)) { |
|
575 | + $mtime = time(); |
|
576 | + } |
|
577 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
578 | + $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
579 | + } |
|
580 | + return true; |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * @param string $path |
|
585 | + * @return mixed |
|
586 | + */ |
|
587 | + public function file_get_contents($path) { |
|
588 | + return $this->basicOperation('file_get_contents', $path, array('read')); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * @param bool $exists |
|
593 | + * @param string $path |
|
594 | + * @param bool $run |
|
595 | + */ |
|
596 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
597 | + if (!$exists) { |
|
598 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
599 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
600 | + Filesystem::signal_param_run => &$run, |
|
601 | + )); |
|
602 | + } else { |
|
603 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
604 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
605 | + Filesystem::signal_param_run => &$run, |
|
606 | + )); |
|
607 | + } |
|
608 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
609 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
610 | + Filesystem::signal_param_run => &$run, |
|
611 | + )); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * @param bool $exists |
|
616 | + * @param string $path |
|
617 | + */ |
|
618 | + protected function emit_file_hooks_post($exists, $path) { |
|
619 | + if (!$exists) { |
|
620 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
621 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
622 | + )); |
|
623 | + } else { |
|
624 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
625 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
626 | + )); |
|
627 | + } |
|
628 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
629 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
630 | + )); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * @param string $path |
|
635 | + * @param string|resource $data |
|
636 | + * @return bool|mixed |
|
637 | + * @throws \Exception |
|
638 | + */ |
|
639 | + public function file_put_contents($path, $data) { |
|
640 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
641 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
642 | + if (Filesystem::isValidPath($path) |
|
643 | + and !Filesystem::isFileBlacklisted($path) |
|
644 | + ) { |
|
645 | + $path = $this->getRelativePath($absolutePath); |
|
646 | + |
|
647 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
648 | + |
|
649 | + $exists = $this->file_exists($path); |
|
650 | + $run = true; |
|
651 | + if ($this->shouldEmitHooks($path)) { |
|
652 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
653 | + } |
|
654 | + if (!$run) { |
|
655 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
656 | + return false; |
|
657 | + } |
|
658 | + |
|
659 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
660 | + |
|
661 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
662 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
663 | + $target = $storage->fopen($internalPath, 'w'); |
|
664 | + if ($target) { |
|
665 | + list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
666 | + fclose($target); |
|
667 | + fclose($data); |
|
668 | + |
|
669 | + $this->writeUpdate($storage, $internalPath); |
|
670 | + |
|
671 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
672 | + |
|
673 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
674 | + $this->emit_file_hooks_post($exists, $path); |
|
675 | + } |
|
676 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
677 | + return $result; |
|
678 | + } else { |
|
679 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
680 | + return false; |
|
681 | + } |
|
682 | + } else { |
|
683 | + return false; |
|
684 | + } |
|
685 | + } else { |
|
686 | + $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
687 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
688 | + } |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * @param string $path |
|
693 | + * @return bool|mixed |
|
694 | + */ |
|
695 | + public function unlink($path) { |
|
696 | + if ($path === '' || $path === '/') { |
|
697 | + // do not allow deleting the root |
|
698 | + return false; |
|
699 | + } |
|
700 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
701 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
702 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
703 | + if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
704 | + return $this->removeMount($mount, $absolutePath); |
|
705 | + } |
|
706 | + if ($this->is_dir($path)) { |
|
707 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
708 | + } else { |
|
709 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
710 | + } |
|
711 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
712 | + $storage = $mount->getStorage(); |
|
713 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
714 | + $storage->getUpdater()->remove($internalPath); |
|
715 | + return true; |
|
716 | + } else { |
|
717 | + return $result; |
|
718 | + } |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * @param string $directory |
|
723 | + * @return bool|mixed |
|
724 | + */ |
|
725 | + public function deleteAll($directory) { |
|
726 | + return $this->rmdir($directory); |
|
727 | + } |
|
728 | + |
|
729 | + /** |
|
730 | + * Rename/move a file or folder from the source path to target path. |
|
731 | + * |
|
732 | + * @param string $path1 source path |
|
733 | + * @param string $path2 target path |
|
734 | + * |
|
735 | + * @return bool|mixed |
|
736 | + */ |
|
737 | + public function rename($path1, $path2) { |
|
738 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
739 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
740 | + $result = false; |
|
741 | + if ( |
|
742 | + Filesystem::isValidPath($path2) |
|
743 | + and Filesystem::isValidPath($path1) |
|
744 | + and !Filesystem::isFileBlacklisted($path2) |
|
745 | + ) { |
|
746 | + $path1 = $this->getRelativePath($absolutePath1); |
|
747 | + $path2 = $this->getRelativePath($absolutePath2); |
|
748 | + $exists = $this->file_exists($path2); |
|
749 | + |
|
750 | + if ($path1 == null or $path2 == null) { |
|
751 | + return false; |
|
752 | + } |
|
753 | + |
|
754 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
755 | + try { |
|
756 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
757 | + |
|
758 | + $run = true; |
|
759 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
760 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
761 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
762 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
763 | + \OC_Hook::emit( |
|
764 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
765 | + array( |
|
766 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
767 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
768 | + Filesystem::signal_param_run => &$run |
|
769 | + ) |
|
770 | + ); |
|
771 | + } |
|
772 | + if ($run) { |
|
773 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
774 | + |
|
775 | + $manager = Filesystem::getMountManager(); |
|
776 | + $mount1 = $this->getMount($path1); |
|
777 | + $mount2 = $this->getMount($path2); |
|
778 | + $storage1 = $mount1->getStorage(); |
|
779 | + $storage2 = $mount2->getStorage(); |
|
780 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
781 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
782 | + |
|
783 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
784 | + try { |
|
785 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
786 | + |
|
787 | + if ($internalPath1 === '') { |
|
788 | + if ($mount1 instanceof MoveableMount) { |
|
789 | + if ($this->isTargetAllowed($absolutePath2)) { |
|
790 | + /** |
|
791 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
792 | + */ |
|
793 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
794 | + $result = $mount1->moveMount($absolutePath2); |
|
795 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
796 | + } else { |
|
797 | + $result = false; |
|
798 | + } |
|
799 | + } else { |
|
800 | + $result = false; |
|
801 | + } |
|
802 | + // moving a file/folder within the same mount point |
|
803 | + } elseif ($storage1 === $storage2) { |
|
804 | + if ($storage1) { |
|
805 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
806 | + } else { |
|
807 | + $result = false; |
|
808 | + } |
|
809 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
810 | + } else { |
|
811 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
812 | + } |
|
813 | + |
|
814 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
815 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
816 | + $this->writeUpdate($storage2, $internalPath2); |
|
817 | + } else if ($result) { |
|
818 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
819 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
820 | + } |
|
821 | + } |
|
822 | + } catch(\Exception $e) { |
|
823 | + throw $e; |
|
824 | + } finally { |
|
825 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
826 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
827 | + } |
|
828 | + |
|
829 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
830 | + if ($this->shouldEmitHooks()) { |
|
831 | + $this->emit_file_hooks_post($exists, $path2); |
|
832 | + } |
|
833 | + } elseif ($result) { |
|
834 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
835 | + \OC_Hook::emit( |
|
836 | + Filesystem::CLASSNAME, |
|
837 | + Filesystem::signal_post_rename, |
|
838 | + array( |
|
839 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
840 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
841 | + ) |
|
842 | + ); |
|
843 | + } |
|
844 | + } |
|
845 | + } |
|
846 | + } catch(\Exception $e) { |
|
847 | + throw $e; |
|
848 | + } finally { |
|
849 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
850 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
851 | + } |
|
852 | + } |
|
853 | + return $result; |
|
854 | + } |
|
855 | + |
|
856 | + /** |
|
857 | + * Copy a file/folder from the source path to target path |
|
858 | + * |
|
859 | + * @param string $path1 source path |
|
860 | + * @param string $path2 target path |
|
861 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
862 | + * |
|
863 | + * @return bool|mixed |
|
864 | + */ |
|
865 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
866 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
867 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
868 | + $result = false; |
|
869 | + if ( |
|
870 | + Filesystem::isValidPath($path2) |
|
871 | + and Filesystem::isValidPath($path1) |
|
872 | + and !Filesystem::isFileBlacklisted($path2) |
|
873 | + ) { |
|
874 | + $path1 = $this->getRelativePath($absolutePath1); |
|
875 | + $path2 = $this->getRelativePath($absolutePath2); |
|
876 | + |
|
877 | + if ($path1 == null or $path2 == null) { |
|
878 | + return false; |
|
879 | + } |
|
880 | + $run = true; |
|
881 | + |
|
882 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
883 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
884 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
885 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
886 | + |
|
887 | + try { |
|
888 | + |
|
889 | + $exists = $this->file_exists($path2); |
|
890 | + if ($this->shouldEmitHooks()) { |
|
891 | + \OC_Hook::emit( |
|
892 | + Filesystem::CLASSNAME, |
|
893 | + Filesystem::signal_copy, |
|
894 | + array( |
|
895 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
896 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
897 | + Filesystem::signal_param_run => &$run |
|
898 | + ) |
|
899 | + ); |
|
900 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
901 | + } |
|
902 | + if ($run) { |
|
903 | + $mount1 = $this->getMount($path1); |
|
904 | + $mount2 = $this->getMount($path2); |
|
905 | + $storage1 = $mount1->getStorage(); |
|
906 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
907 | + $storage2 = $mount2->getStorage(); |
|
908 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
909 | + |
|
910 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
911 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
912 | + |
|
913 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
914 | + if ($storage1) { |
|
915 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
916 | + } else { |
|
917 | + $result = false; |
|
918 | + } |
|
919 | + } else { |
|
920 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
921 | + } |
|
922 | + |
|
923 | + $this->writeUpdate($storage2, $internalPath2); |
|
924 | + |
|
925 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
926 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
927 | + |
|
928 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
929 | + \OC_Hook::emit( |
|
930 | + Filesystem::CLASSNAME, |
|
931 | + Filesystem::signal_post_copy, |
|
932 | + array( |
|
933 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
934 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
935 | + ) |
|
936 | + ); |
|
937 | + $this->emit_file_hooks_post($exists, $path2); |
|
938 | + } |
|
939 | + |
|
940 | + } |
|
941 | + } catch (\Exception $e) { |
|
942 | + $this->unlockFile($path2, $lockTypePath2); |
|
943 | + $this->unlockFile($path1, $lockTypePath1); |
|
944 | + throw $e; |
|
945 | + } |
|
946 | + |
|
947 | + $this->unlockFile($path2, $lockTypePath2); |
|
948 | + $this->unlockFile($path1, $lockTypePath1); |
|
949 | + |
|
950 | + } |
|
951 | + return $result; |
|
952 | + } |
|
953 | + |
|
954 | + /** |
|
955 | + * @param string $path |
|
956 | + * @param string $mode 'r' or 'w' |
|
957 | + * @return resource |
|
958 | + */ |
|
959 | + public function fopen($path, $mode) { |
|
960 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
961 | + $hooks = array(); |
|
962 | + switch ($mode) { |
|
963 | + case 'r': |
|
964 | + $hooks[] = 'read'; |
|
965 | + break; |
|
966 | + case 'r+': |
|
967 | + case 'w+': |
|
968 | + case 'x+': |
|
969 | + case 'a+': |
|
970 | + $hooks[] = 'read'; |
|
971 | + $hooks[] = 'write'; |
|
972 | + break; |
|
973 | + case 'w': |
|
974 | + case 'x': |
|
975 | + case 'a': |
|
976 | + $hooks[] = 'write'; |
|
977 | + break; |
|
978 | + default: |
|
979 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
980 | + } |
|
981 | + |
|
982 | + if ($mode !== 'r' && $mode !== 'w') { |
|
983 | + \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
984 | + } |
|
985 | + |
|
986 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
987 | + } |
|
988 | + |
|
989 | + /** |
|
990 | + * @param string $path |
|
991 | + * @return bool|string |
|
992 | + * @throws \OCP\Files\InvalidPathException |
|
993 | + */ |
|
994 | + public function toTmpFile($path) { |
|
995 | + $this->assertPathLength($path); |
|
996 | + if (Filesystem::isValidPath($path)) { |
|
997 | + $source = $this->fopen($path, 'r'); |
|
998 | + if ($source) { |
|
999 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
1000 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
1001 | + file_put_contents($tmpFile, $source); |
|
1002 | + return $tmpFile; |
|
1003 | + } else { |
|
1004 | + return false; |
|
1005 | + } |
|
1006 | + } else { |
|
1007 | + return false; |
|
1008 | + } |
|
1009 | + } |
|
1010 | + |
|
1011 | + /** |
|
1012 | + * @param string $tmpFile |
|
1013 | + * @param string $path |
|
1014 | + * @return bool|mixed |
|
1015 | + * @throws \OCP\Files\InvalidPathException |
|
1016 | + */ |
|
1017 | + public function fromTmpFile($tmpFile, $path) { |
|
1018 | + $this->assertPathLength($path); |
|
1019 | + if (Filesystem::isValidPath($path)) { |
|
1020 | + |
|
1021 | + // Get directory that the file is going into |
|
1022 | + $filePath = dirname($path); |
|
1023 | + |
|
1024 | + // Create the directories if any |
|
1025 | + if (!$this->file_exists($filePath)) { |
|
1026 | + $result = $this->createParentDirectories($filePath); |
|
1027 | + if ($result === false) { |
|
1028 | + return false; |
|
1029 | + } |
|
1030 | + } |
|
1031 | + |
|
1032 | + $source = fopen($tmpFile, 'r'); |
|
1033 | + if ($source) { |
|
1034 | + $result = $this->file_put_contents($path, $source); |
|
1035 | + // $this->file_put_contents() might have already closed |
|
1036 | + // the resource, so we check it, before trying to close it |
|
1037 | + // to avoid messages in the error log. |
|
1038 | + if (is_resource($source)) { |
|
1039 | + fclose($source); |
|
1040 | + } |
|
1041 | + unlink($tmpFile); |
|
1042 | + return $result; |
|
1043 | + } else { |
|
1044 | + return false; |
|
1045 | + } |
|
1046 | + } else { |
|
1047 | + return false; |
|
1048 | + } |
|
1049 | + } |
|
1050 | + |
|
1051 | + |
|
1052 | + /** |
|
1053 | + * @param string $path |
|
1054 | + * @return mixed |
|
1055 | + * @throws \OCP\Files\InvalidPathException |
|
1056 | + */ |
|
1057 | + public function getMimeType($path) { |
|
1058 | + $this->assertPathLength($path); |
|
1059 | + return $this->basicOperation('getMimeType', $path); |
|
1060 | + } |
|
1061 | + |
|
1062 | + /** |
|
1063 | + * @param string $type |
|
1064 | + * @param string $path |
|
1065 | + * @param bool $raw |
|
1066 | + * @return bool|null|string |
|
1067 | + */ |
|
1068 | + public function hash($type, $path, $raw = false) { |
|
1069 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1070 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1071 | + if (Filesystem::isValidPath($path)) { |
|
1072 | + $path = $this->getRelativePath($absolutePath); |
|
1073 | + if ($path == null) { |
|
1074 | + return false; |
|
1075 | + } |
|
1076 | + if ($this->shouldEmitHooks($path)) { |
|
1077 | + \OC_Hook::emit( |
|
1078 | + Filesystem::CLASSNAME, |
|
1079 | + Filesystem::signal_read, |
|
1080 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
1081 | + ); |
|
1082 | + } |
|
1083 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1084 | + if ($storage) { |
|
1085 | + return $storage->hash($type, $internalPath, $raw); |
|
1086 | + } |
|
1087 | + } |
|
1088 | + return null; |
|
1089 | + } |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * @param string $path |
|
1093 | + * @return mixed |
|
1094 | + * @throws \OCP\Files\InvalidPathException |
|
1095 | + */ |
|
1096 | + public function free_space($path = '/') { |
|
1097 | + $this->assertPathLength($path); |
|
1098 | + $result = $this->basicOperation('free_space', $path); |
|
1099 | + if ($result === null) { |
|
1100 | + throw new InvalidPathException(); |
|
1101 | + } |
|
1102 | + return $result; |
|
1103 | + } |
|
1104 | + |
|
1105 | + /** |
|
1106 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
1107 | + * |
|
1108 | + * @param string $operation |
|
1109 | + * @param string $path |
|
1110 | + * @param array $hooks (optional) |
|
1111 | + * @param mixed $extraParam (optional) |
|
1112 | + * @return mixed |
|
1113 | + * @throws \Exception |
|
1114 | + * |
|
1115 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
1116 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
1117 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
1118 | + */ |
|
1119 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
1120 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
1121 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
1122 | + if (Filesystem::isValidPath($path) |
|
1123 | + and !Filesystem::isFileBlacklisted($path) |
|
1124 | + ) { |
|
1125 | + $path = $this->getRelativePath($absolutePath); |
|
1126 | + if ($path == null) { |
|
1127 | + return false; |
|
1128 | + } |
|
1129 | + |
|
1130 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
1131 | + // always a shared lock during pre-hooks so the hook can read the file |
|
1132 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
1133 | + } |
|
1134 | + |
|
1135 | + $run = $this->runHooks($hooks, $path); |
|
1136 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
1137 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
1138 | + if ($run and $storage) { |
|
1139 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1140 | + try { |
|
1141 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1142 | + } catch (LockedException $e) { |
|
1143 | + // release the shared lock we acquired before quiting |
|
1144 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1145 | + throw $e; |
|
1146 | + } |
|
1147 | + } |
|
1148 | + try { |
|
1149 | + if (!is_null($extraParam)) { |
|
1150 | + $result = $storage->$operation($internalPath, $extraParam); |
|
1151 | + } else { |
|
1152 | + $result = $storage->$operation($internalPath); |
|
1153 | + } |
|
1154 | + } catch (\Exception $e) { |
|
1155 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
1156 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1157 | + } else if (in_array('read', $hooks)) { |
|
1158 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1159 | + } |
|
1160 | + throw $e; |
|
1161 | + } |
|
1162 | + |
|
1163 | + if ($result && in_array('delete', $hooks) and $result) { |
|
1164 | + $this->removeUpdate($storage, $internalPath); |
|
1165 | + } |
|
1166 | + if ($result && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') { |
|
1167 | + $this->writeUpdate($storage, $internalPath); |
|
1168 | + } |
|
1169 | + if ($result && in_array('touch', $hooks)) { |
|
1170 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
1171 | + } |
|
1172 | + |
|
1173 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
1174 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
1175 | + } |
|
1176 | + |
|
1177 | + $unlockLater = false; |
|
1178 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
1179 | + $unlockLater = true; |
|
1180 | + // make sure our unlocking callback will still be called if connection is aborted |
|
1181 | + ignore_user_abort(true); |
|
1182 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
1183 | + if (in_array('write', $hooks)) { |
|
1184 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
1185 | + } else if (in_array('read', $hooks)) { |
|
1186 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1187 | + } |
|
1188 | + }); |
|
1189 | + } |
|
1190 | + |
|
1191 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
1192 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
1193 | + $this->runHooks($hooks, $path, true); |
|
1194 | + } |
|
1195 | + } |
|
1196 | + |
|
1197 | + if (!$unlockLater |
|
1198 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
1199 | + ) { |
|
1200 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1201 | + } |
|
1202 | + return $result; |
|
1203 | + } else { |
|
1204 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
1205 | + } |
|
1206 | + } |
|
1207 | + return null; |
|
1208 | + } |
|
1209 | + |
|
1210 | + /** |
|
1211 | + * get the path relative to the default root for hook usage |
|
1212 | + * |
|
1213 | + * @param string $path |
|
1214 | + * @return string |
|
1215 | + */ |
|
1216 | + private function getHookPath($path) { |
|
1217 | + if (!Filesystem::getView()) { |
|
1218 | + return $path; |
|
1219 | + } |
|
1220 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
1221 | + } |
|
1222 | + |
|
1223 | + private function shouldEmitHooks($path = '') { |
|
1224 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
1225 | + return false; |
|
1226 | + } |
|
1227 | + if (!Filesystem::$loaded) { |
|
1228 | + return false; |
|
1229 | + } |
|
1230 | + $defaultRoot = Filesystem::getRoot(); |
|
1231 | + if ($defaultRoot === null) { |
|
1232 | + return false; |
|
1233 | + } |
|
1234 | + if ($this->fakeRoot === $defaultRoot) { |
|
1235 | + return true; |
|
1236 | + } |
|
1237 | + $fullPath = $this->getAbsolutePath($path); |
|
1238 | + |
|
1239 | + if ($fullPath === $defaultRoot) { |
|
1240 | + return true; |
|
1241 | + } |
|
1242 | + |
|
1243 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
1244 | + } |
|
1245 | + |
|
1246 | + /** |
|
1247 | + * @param string[] $hooks |
|
1248 | + * @param string $path |
|
1249 | + * @param bool $post |
|
1250 | + * @return bool |
|
1251 | + */ |
|
1252 | + private function runHooks($hooks, $path, $post = false) { |
|
1253 | + $relativePath = $path; |
|
1254 | + $path = $this->getHookPath($path); |
|
1255 | + $prefix = $post ? 'post_' : ''; |
|
1256 | + $run = true; |
|
1257 | + if ($this->shouldEmitHooks($relativePath)) { |
|
1258 | + foreach ($hooks as $hook) { |
|
1259 | + if ($hook != 'read') { |
|
1260 | + \OC_Hook::emit( |
|
1261 | + Filesystem::CLASSNAME, |
|
1262 | + $prefix . $hook, |
|
1263 | + array( |
|
1264 | + Filesystem::signal_param_run => &$run, |
|
1265 | + Filesystem::signal_param_path => $path |
|
1266 | + ) |
|
1267 | + ); |
|
1268 | + } elseif (!$post) { |
|
1269 | + \OC_Hook::emit( |
|
1270 | + Filesystem::CLASSNAME, |
|
1271 | + $prefix . $hook, |
|
1272 | + array( |
|
1273 | + Filesystem::signal_param_path => $path |
|
1274 | + ) |
|
1275 | + ); |
|
1276 | + } |
|
1277 | + } |
|
1278 | + } |
|
1279 | + return $run; |
|
1280 | + } |
|
1281 | + |
|
1282 | + /** |
|
1283 | + * check if a file or folder has been updated since $time |
|
1284 | + * |
|
1285 | + * @param string $path |
|
1286 | + * @param int $time |
|
1287 | + * @return bool |
|
1288 | + */ |
|
1289 | + public function hasUpdated($path, $time) { |
|
1290 | + return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
1291 | + } |
|
1292 | + |
|
1293 | + /** |
|
1294 | + * @param string $ownerId |
|
1295 | + * @return \OC\User\User |
|
1296 | + */ |
|
1297 | + private function getUserObjectForOwner($ownerId) { |
|
1298 | + $owner = $this->userManager->get($ownerId); |
|
1299 | + if ($owner instanceof IUser) { |
|
1300 | + return $owner; |
|
1301 | + } else { |
|
1302 | + return new User($ownerId, null, \OC::$server->getEventDispatcher()); |
|
1303 | + } |
|
1304 | + } |
|
1305 | + |
|
1306 | + /** |
|
1307 | + * Get file info from cache |
|
1308 | + * |
|
1309 | + * If the file is not in cached it will be scanned |
|
1310 | + * If the file has changed on storage the cache will be updated |
|
1311 | + * |
|
1312 | + * @param \OC\Files\Storage\Storage $storage |
|
1313 | + * @param string $internalPath |
|
1314 | + * @param string $relativePath |
|
1315 | + * @return ICacheEntry|bool |
|
1316 | + */ |
|
1317 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
1318 | + $cache = $storage->getCache($internalPath); |
|
1319 | + $data = $cache->get($internalPath); |
|
1320 | + $watcher = $storage->getWatcher($internalPath); |
|
1321 | + |
|
1322 | + try { |
|
1323 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
1324 | + if (!$data || $data['size'] === -1) { |
|
1325 | + if (!$storage->file_exists($internalPath)) { |
|
1326 | + return false; |
|
1327 | + } |
|
1328 | + // don't need to get a lock here since the scanner does it's own locking |
|
1329 | + $scanner = $storage->getScanner($internalPath); |
|
1330 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1331 | + $data = $cache->get($internalPath); |
|
1332 | + } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
1333 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1334 | + $watcher->update($internalPath, $data); |
|
1335 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
1336 | + $data = $cache->get($internalPath); |
|
1337 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
1338 | + } |
|
1339 | + } catch (LockedException $e) { |
|
1340 | + // if the file is locked we just use the old cache info |
|
1341 | + } |
|
1342 | + |
|
1343 | + return $data; |
|
1344 | + } |
|
1345 | + |
|
1346 | + /** |
|
1347 | + * get the filesystem info |
|
1348 | + * |
|
1349 | + * @param string $path |
|
1350 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
1351 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
1352 | + * defaults to true |
|
1353 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
1354 | + */ |
|
1355 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
1356 | + $this->assertPathLength($path); |
|
1357 | + if (!Filesystem::isValidPath($path)) { |
|
1358 | + return false; |
|
1359 | + } |
|
1360 | + if (Cache\Scanner::isPartialFile($path)) { |
|
1361 | + return $this->getPartFileInfo($path); |
|
1362 | + } |
|
1363 | + $relativePath = $path; |
|
1364 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1365 | + |
|
1366 | + $mount = Filesystem::getMountManager()->find($path); |
|
1367 | + if (!$mount) { |
|
1368 | + \OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path); |
|
1369 | + return false; |
|
1370 | + } |
|
1371 | + $storage = $mount->getStorage(); |
|
1372 | + $internalPath = $mount->getInternalPath($path); |
|
1373 | + if ($storage) { |
|
1374 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
1375 | + |
|
1376 | + if (!$data instanceof ICacheEntry) { |
|
1377 | + \OC::$server->getLogger()->debug('No cache entry found for ' . $path . ' (storage: ' . $storage->getId() . ', internalPath: ' . $internalPath . ')'); |
|
1378 | + return false; |
|
1379 | + } |
|
1380 | + |
|
1381 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
1382 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
1383 | + } |
|
1384 | + $ownerId = $storage->getOwner($internalPath); |
|
1385 | + $owner = null; |
|
1386 | + if ($ownerId !== null) { |
|
1387 | + // ownerId might be null if files are accessed with an access token without file system access |
|
1388 | + $owner = $this->getUserObjectForOwner($ownerId); |
|
1389 | + } |
|
1390 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
1391 | + |
|
1392 | + if ($data and isset($data['fileid'])) { |
|
1393 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
1394 | + //add the sizes of other mount points to the folder |
|
1395 | + $extOnly = ($includeMountPoints === 'ext'); |
|
1396 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1397 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
1398 | + $subStorage = $mount->getStorage(); |
|
1399 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
1400 | + })); |
|
1401 | + } |
|
1402 | + } |
|
1403 | + |
|
1404 | + return $info; |
|
1405 | + } else { |
|
1406 | + \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
1407 | + } |
|
1408 | + |
|
1409 | + return false; |
|
1410 | + } |
|
1411 | + |
|
1412 | + /** |
|
1413 | + * get the content of a directory |
|
1414 | + * |
|
1415 | + * @param string $directory path under datadirectory |
|
1416 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
1417 | + * @return FileInfo[] |
|
1418 | + */ |
|
1419 | + public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
1420 | + $this->assertPathLength($directory); |
|
1421 | + if (!Filesystem::isValidPath($directory)) { |
|
1422 | + return []; |
|
1423 | + } |
|
1424 | + $path = $this->getAbsolutePath($directory); |
|
1425 | + $path = Filesystem::normalizePath($path); |
|
1426 | + $mount = $this->getMount($directory); |
|
1427 | + if (!$mount) { |
|
1428 | + return []; |
|
1429 | + } |
|
1430 | + $storage = $mount->getStorage(); |
|
1431 | + $internalPath = $mount->getInternalPath($path); |
|
1432 | + if ($storage) { |
|
1433 | + $cache = $storage->getCache($internalPath); |
|
1434 | + $user = \OC_User::getUser(); |
|
1435 | + |
|
1436 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
1437 | + |
|
1438 | + if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
1439 | + return []; |
|
1440 | + } |
|
1441 | + |
|
1442 | + $folderId = $data['fileid']; |
|
1443 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
1444 | + |
|
1445 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
1446 | + |
|
1447 | + $fileNames = array_map(function(ICacheEntry $content) { |
|
1448 | + return $content->getName(); |
|
1449 | + }, $contents); |
|
1450 | + /** |
|
1451 | + * @var \OC\Files\FileInfo[] $fileInfos |
|
1452 | + */ |
|
1453 | + $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
1454 | + if ($sharingDisabled) { |
|
1455 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1456 | + } |
|
1457 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
1458 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
1459 | + }, $contents); |
|
1460 | + $files = array_combine($fileNames, $fileInfos); |
|
1461 | + |
|
1462 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
1463 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
1464 | + $dirLength = strlen($path); |
|
1465 | + foreach ($mounts as $mount) { |
|
1466 | + $mountPoint = $mount->getMountPoint(); |
|
1467 | + $subStorage = $mount->getStorage(); |
|
1468 | + if ($subStorage) { |
|
1469 | + $subCache = $subStorage->getCache(''); |
|
1470 | + |
|
1471 | + $rootEntry = $subCache->get(''); |
|
1472 | + if (!$rootEntry) { |
|
1473 | + $subScanner = $subStorage->getScanner(''); |
|
1474 | + try { |
|
1475 | + $subScanner->scanFile(''); |
|
1476 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
1477 | + continue; |
|
1478 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
1479 | + continue; |
|
1480 | + } catch (\Exception $e) { |
|
1481 | + // sometimes when the storage is not available it can be any exception |
|
1482 | + \OC::$server->getLogger()->logException($e, [ |
|
1483 | + 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
1484 | + 'level' => ILogger::ERROR, |
|
1485 | + 'app' => 'lib', |
|
1486 | + ]); |
|
1487 | + continue; |
|
1488 | + } |
|
1489 | + $rootEntry = $subCache->get(''); |
|
1490 | + } |
|
1491 | + |
|
1492 | + if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
1493 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
1494 | + if ($pos = strpos($relativePath, '/')) { |
|
1495 | + //mountpoint inside subfolder add size to the correct folder |
|
1496 | + $entryName = substr($relativePath, 0, $pos); |
|
1497 | + foreach ($files as &$entry) { |
|
1498 | + if ($entry->getName() === $entryName) { |
|
1499 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
1500 | + } |
|
1501 | + } |
|
1502 | + } else { //mountpoint in this folder, add an entry for it |
|
1503 | + $rootEntry['name'] = $relativePath; |
|
1504 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
1505 | + $permissions = $rootEntry['permissions']; |
|
1506 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
1507 | + // for shared files/folders we use the permissions given by the owner |
|
1508 | + if ($mount instanceof MoveableMount) { |
|
1509 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
1510 | + } else { |
|
1511 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
1512 | + } |
|
1513 | + |
|
1514 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
1515 | + |
|
1516 | + // if sharing was disabled for the user we remove the share permissions |
|
1517 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
1518 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
1519 | + } |
|
1520 | + |
|
1521 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
1522 | + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
1523 | + } |
|
1524 | + } |
|
1525 | + } |
|
1526 | + } |
|
1527 | + |
|
1528 | + if ($mimetype_filter) { |
|
1529 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
1530 | + if (strpos($mimetype_filter, '/')) { |
|
1531 | + return $file->getMimetype() === $mimetype_filter; |
|
1532 | + } else { |
|
1533 | + return $file->getMimePart() === $mimetype_filter; |
|
1534 | + } |
|
1535 | + }); |
|
1536 | + } |
|
1537 | + |
|
1538 | + return array_values($files); |
|
1539 | + } else { |
|
1540 | + return []; |
|
1541 | + } |
|
1542 | + } |
|
1543 | + |
|
1544 | + /** |
|
1545 | + * change file metadata |
|
1546 | + * |
|
1547 | + * @param string $path |
|
1548 | + * @param array|\OCP\Files\FileInfo $data |
|
1549 | + * @return int |
|
1550 | + * |
|
1551 | + * returns the fileid of the updated file |
|
1552 | + */ |
|
1553 | + public function putFileInfo($path, $data) { |
|
1554 | + $this->assertPathLength($path); |
|
1555 | + if ($data instanceof FileInfo) { |
|
1556 | + $data = $data->getData(); |
|
1557 | + } |
|
1558 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
1559 | + /** |
|
1560 | + * @var \OC\Files\Storage\Storage $storage |
|
1561 | + * @var string $internalPath |
|
1562 | + */ |
|
1563 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
1564 | + if ($storage) { |
|
1565 | + $cache = $storage->getCache($path); |
|
1566 | + |
|
1567 | + if (!$cache->inCache($internalPath)) { |
|
1568 | + $scanner = $storage->getScanner($internalPath); |
|
1569 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
1570 | + } |
|
1571 | + |
|
1572 | + return $cache->put($internalPath, $data); |
|
1573 | + } else { |
|
1574 | + return -1; |
|
1575 | + } |
|
1576 | + } |
|
1577 | + |
|
1578 | + /** |
|
1579 | + * search for files with the name matching $query |
|
1580 | + * |
|
1581 | + * @param string $query |
|
1582 | + * @return FileInfo[] |
|
1583 | + */ |
|
1584 | + public function search($query) { |
|
1585 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
1586 | + } |
|
1587 | + |
|
1588 | + /** |
|
1589 | + * search for files with the name matching $query |
|
1590 | + * |
|
1591 | + * @param string $query |
|
1592 | + * @return FileInfo[] |
|
1593 | + */ |
|
1594 | + public function searchRaw($query) { |
|
1595 | + return $this->searchCommon('search', array($query)); |
|
1596 | + } |
|
1597 | + |
|
1598 | + /** |
|
1599 | + * search for files by mimetype |
|
1600 | + * |
|
1601 | + * @param string $mimetype |
|
1602 | + * @return FileInfo[] |
|
1603 | + */ |
|
1604 | + public function searchByMime($mimetype) { |
|
1605 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
1606 | + } |
|
1607 | + |
|
1608 | + /** |
|
1609 | + * search for files by tag |
|
1610 | + * |
|
1611 | + * @param string|int $tag name or tag id |
|
1612 | + * @param string $userId owner of the tags |
|
1613 | + * @return FileInfo[] |
|
1614 | + */ |
|
1615 | + public function searchByTag($tag, $userId) { |
|
1616 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
1617 | + } |
|
1618 | + |
|
1619 | + /** |
|
1620 | + * @param string $method cache method |
|
1621 | + * @param array $args |
|
1622 | + * @return FileInfo[] |
|
1623 | + */ |
|
1624 | + private function searchCommon($method, $args) { |
|
1625 | + $files = array(); |
|
1626 | + $rootLength = strlen($this->fakeRoot); |
|
1627 | + |
|
1628 | + $mount = $this->getMount(''); |
|
1629 | + $mountPoint = $mount->getMountPoint(); |
|
1630 | + $storage = $mount->getStorage(); |
|
1631 | + if ($storage) { |
|
1632 | + $cache = $storage->getCache(''); |
|
1633 | + |
|
1634 | + $results = call_user_func_array(array($cache, $method), $args); |
|
1635 | + foreach ($results as $result) { |
|
1636 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
1637 | + $internalPath = $result['path']; |
|
1638 | + $path = $mountPoint . $result['path']; |
|
1639 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
1640 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1641 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1642 | + } |
|
1643 | + } |
|
1644 | + |
|
1645 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
1646 | + foreach ($mounts as $mount) { |
|
1647 | + $mountPoint = $mount->getMountPoint(); |
|
1648 | + $storage = $mount->getStorage(); |
|
1649 | + if ($storage) { |
|
1650 | + $cache = $storage->getCache(''); |
|
1651 | + |
|
1652 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
1653 | + $results = call_user_func_array(array($cache, $method), $args); |
|
1654 | + if ($results) { |
|
1655 | + foreach ($results as $result) { |
|
1656 | + $internalPath = $result['path']; |
|
1657 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
1658 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
1659 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1660 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
1661 | + } |
|
1662 | + } |
|
1663 | + } |
|
1664 | + } |
|
1665 | + } |
|
1666 | + return $files; |
|
1667 | + } |
|
1668 | + |
|
1669 | + /** |
|
1670 | + * Get the owner for a file or folder |
|
1671 | + * |
|
1672 | + * @param string $path |
|
1673 | + * @return string the user id of the owner |
|
1674 | + * @throws NotFoundException |
|
1675 | + */ |
|
1676 | + public function getOwner($path) { |
|
1677 | + $info = $this->getFileInfo($path); |
|
1678 | + if (!$info) { |
|
1679 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
1680 | + } |
|
1681 | + return $info->getOwner()->getUID(); |
|
1682 | + } |
|
1683 | + |
|
1684 | + /** |
|
1685 | + * get the ETag for a file or folder |
|
1686 | + * |
|
1687 | + * @param string $path |
|
1688 | + * @return string |
|
1689 | + */ |
|
1690 | + public function getETag($path) { |
|
1691 | + /** |
|
1692 | + * @var Storage\Storage $storage |
|
1693 | + * @var string $internalPath |
|
1694 | + */ |
|
1695 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
1696 | + if ($storage) { |
|
1697 | + return $storage->getETag($internalPath); |
|
1698 | + } else { |
|
1699 | + return null; |
|
1700 | + } |
|
1701 | + } |
|
1702 | + |
|
1703 | + /** |
|
1704 | + * Get the path of a file by id, relative to the view |
|
1705 | + * |
|
1706 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
1707 | + * |
|
1708 | + * @param int $id |
|
1709 | + * @throws NotFoundException |
|
1710 | + * @return string |
|
1711 | + */ |
|
1712 | + public function getPath($id) { |
|
1713 | + $id = (int)$id; |
|
1714 | + $manager = Filesystem::getMountManager(); |
|
1715 | + $mounts = $manager->findIn($this->fakeRoot); |
|
1716 | + $mounts[] = $manager->find($this->fakeRoot); |
|
1717 | + // reverse the array so we start with the storage this view is in |
|
1718 | + // which is the most likely to contain the file we're looking for |
|
1719 | + $mounts = array_reverse($mounts); |
|
1720 | + foreach ($mounts as $mount) { |
|
1721 | + /** |
|
1722 | + * @var \OC\Files\Mount\MountPoint $mount |
|
1723 | + */ |
|
1724 | + if ($mount->getStorage()) { |
|
1725 | + $cache = $mount->getStorage()->getCache(); |
|
1726 | + $internalPath = $cache->getPathById($id); |
|
1727 | + if (is_string($internalPath)) { |
|
1728 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
1729 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
1730 | + return $path; |
|
1731 | + } |
|
1732 | + } |
|
1733 | + } |
|
1734 | + } |
|
1735 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
1736 | + } |
|
1737 | + |
|
1738 | + /** |
|
1739 | + * @param string $path |
|
1740 | + * @throws InvalidPathException |
|
1741 | + */ |
|
1742 | + private function assertPathLength($path) { |
|
1743 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
1744 | + // Check for the string length - performed using isset() instead of strlen() |
|
1745 | + // because isset() is about 5x-40x faster. |
|
1746 | + if (isset($path[$maxLen])) { |
|
1747 | + $pathLen = strlen($path); |
|
1748 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
1749 | + } |
|
1750 | + } |
|
1751 | + |
|
1752 | + /** |
|
1753 | + * check if it is allowed to move a mount point to a given target. |
|
1754 | + * It is not allowed to move a mount point into a different mount point or |
|
1755 | + * into an already shared folder |
|
1756 | + * |
|
1757 | + * @param string $target path |
|
1758 | + * @return boolean |
|
1759 | + */ |
|
1760 | + private function isTargetAllowed($target) { |
|
1761 | + |
|
1762 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
1763 | + if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
1764 | + \OCP\Util::writeLog('files', |
|
1765 | + 'It is not allowed to move one mount point into another one', |
|
1766 | + ILogger::DEBUG); |
|
1767 | + return false; |
|
1768 | + } |
|
1769 | + |
|
1770 | + // note: cannot use the view because the target is already locked |
|
1771 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
1772 | + if ($fileId === -1) { |
|
1773 | + // target might not exist, need to check parent instead |
|
1774 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
1775 | + } |
|
1776 | + |
|
1777 | + // check if any of the parents were shared by the current owner (include collections) |
|
1778 | + $shares = \OCP\Share::getItemShared( |
|
1779 | + 'folder', |
|
1780 | + $fileId, |
|
1781 | + \OCP\Share::FORMAT_NONE, |
|
1782 | + null, |
|
1783 | + true |
|
1784 | + ); |
|
1785 | + |
|
1786 | + if (count($shares) > 0) { |
|
1787 | + \OCP\Util::writeLog('files', |
|
1788 | + 'It is not allowed to move one mount point into a shared folder', |
|
1789 | + ILogger::DEBUG); |
|
1790 | + return false; |
|
1791 | + } |
|
1792 | + |
|
1793 | + return true; |
|
1794 | + } |
|
1795 | + |
|
1796 | + /** |
|
1797 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
1798 | + * |
|
1799 | + * @param string $path |
|
1800 | + * @return \OCP\Files\FileInfo |
|
1801 | + */ |
|
1802 | + private function getPartFileInfo($path) { |
|
1803 | + $mount = $this->getMount($path); |
|
1804 | + $storage = $mount->getStorage(); |
|
1805 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
1806 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
1807 | + return new FileInfo( |
|
1808 | + $this->getAbsolutePath($path), |
|
1809 | + $storage, |
|
1810 | + $internalPath, |
|
1811 | + [ |
|
1812 | + 'fileid' => null, |
|
1813 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
1814 | + 'name' => basename($path), |
|
1815 | + 'etag' => null, |
|
1816 | + 'size' => $storage->filesize($internalPath), |
|
1817 | + 'mtime' => $storage->filemtime($internalPath), |
|
1818 | + 'encrypted' => false, |
|
1819 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
1820 | + ], |
|
1821 | + $mount, |
|
1822 | + $owner |
|
1823 | + ); |
|
1824 | + } |
|
1825 | + |
|
1826 | + /** |
|
1827 | + * @param string $path |
|
1828 | + * @param string $fileName |
|
1829 | + * @throws InvalidPathException |
|
1830 | + */ |
|
1831 | + public function verifyPath($path, $fileName) { |
|
1832 | + try { |
|
1833 | + /** @type \OCP\Files\Storage $storage */ |
|
1834 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
1835 | + $storage->verifyPath($internalPath, $fileName); |
|
1836 | + } catch (ReservedWordException $ex) { |
|
1837 | + $l = \OC::$server->getL10N('lib'); |
|
1838 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
1839 | + } catch (InvalidCharacterInPathException $ex) { |
|
1840 | + $l = \OC::$server->getL10N('lib'); |
|
1841 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
1842 | + } catch (FileNameTooLongException $ex) { |
|
1843 | + $l = \OC::$server->getL10N('lib'); |
|
1844 | + throw new InvalidPathException($l->t('File name is too long')); |
|
1845 | + } catch (InvalidDirectoryException $ex) { |
|
1846 | + $l = \OC::$server->getL10N('lib'); |
|
1847 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
1848 | + } catch (EmptyFileNameException $ex) { |
|
1849 | + $l = \OC::$server->getL10N('lib'); |
|
1850 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
1851 | + } |
|
1852 | + } |
|
1853 | + |
|
1854 | + /** |
|
1855 | + * get all parent folders of $path |
|
1856 | + * |
|
1857 | + * @param string $path |
|
1858 | + * @return string[] |
|
1859 | + */ |
|
1860 | + private function getParents($path) { |
|
1861 | + $path = trim($path, '/'); |
|
1862 | + if (!$path) { |
|
1863 | + return []; |
|
1864 | + } |
|
1865 | + |
|
1866 | + $parts = explode('/', $path); |
|
1867 | + |
|
1868 | + // remove the single file |
|
1869 | + array_pop($parts); |
|
1870 | + $result = array('/'); |
|
1871 | + $resultPath = ''; |
|
1872 | + foreach ($parts as $part) { |
|
1873 | + if ($part) { |
|
1874 | + $resultPath .= '/' . $part; |
|
1875 | + $result[] = $resultPath; |
|
1876 | + } |
|
1877 | + } |
|
1878 | + return $result; |
|
1879 | + } |
|
1880 | + |
|
1881 | + /** |
|
1882 | + * Returns the mount point for which to lock |
|
1883 | + * |
|
1884 | + * @param string $absolutePath absolute path |
|
1885 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
1886 | + * is mounted directly on the given path, false otherwise |
|
1887 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
1888 | + */ |
|
1889 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
1890 | + $results = []; |
|
1891 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
1892 | + if (!$mount) { |
|
1893 | + return $results; |
|
1894 | + } |
|
1895 | + |
|
1896 | + if ($useParentMount) { |
|
1897 | + // find out if something is mounted directly on the path |
|
1898 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
1899 | + if ($internalPath === '') { |
|
1900 | + // resolve the parent mount instead |
|
1901 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
1902 | + } |
|
1903 | + } |
|
1904 | + |
|
1905 | + return $mount; |
|
1906 | + } |
|
1907 | + |
|
1908 | + /** |
|
1909 | + * Lock the given path |
|
1910 | + * |
|
1911 | + * @param string $path the path of the file to lock, relative to the view |
|
1912 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1913 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1914 | + * |
|
1915 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1916 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
1917 | + */ |
|
1918 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
1919 | + $absolutePath = $this->getAbsolutePath($path); |
|
1920 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1921 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1922 | + return false; |
|
1923 | + } |
|
1924 | + |
|
1925 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1926 | + if ($mount) { |
|
1927 | + try { |
|
1928 | + $storage = $mount->getStorage(); |
|
1929 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1930 | + $storage->acquireLock( |
|
1931 | + $mount->getInternalPath($absolutePath), |
|
1932 | + $type, |
|
1933 | + $this->lockingProvider |
|
1934 | + ); |
|
1935 | + } |
|
1936 | + } catch (\OCP\Lock\LockedException $e) { |
|
1937 | + // rethrow with the a human-readable path |
|
1938 | + throw new \OCP\Lock\LockedException( |
|
1939 | + $this->getPathRelativeToFiles($absolutePath), |
|
1940 | + $e |
|
1941 | + ); |
|
1942 | + } |
|
1943 | + } |
|
1944 | + |
|
1945 | + return true; |
|
1946 | + } |
|
1947 | + |
|
1948 | + /** |
|
1949 | + * Change the lock type |
|
1950 | + * |
|
1951 | + * @param string $path the path of the file to lock, relative to the view |
|
1952 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
1953 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
1954 | + * |
|
1955 | + * @return bool False if the path is excluded from locking, true otherwise |
|
1956 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
1957 | + */ |
|
1958 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
1959 | + $path = Filesystem::normalizePath($path); |
|
1960 | + $absolutePath = $this->getAbsolutePath($path); |
|
1961 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
1962 | + if (!$this->shouldLockFile($absolutePath)) { |
|
1963 | + return false; |
|
1964 | + } |
|
1965 | + |
|
1966 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
1967 | + if ($mount) { |
|
1968 | + try { |
|
1969 | + $storage = $mount->getStorage(); |
|
1970 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
1971 | + $storage->changeLock( |
|
1972 | + $mount->getInternalPath($absolutePath), |
|
1973 | + $type, |
|
1974 | + $this->lockingProvider |
|
1975 | + ); |
|
1976 | + } |
|
1977 | + } catch (\OCP\Lock\LockedException $e) { |
|
1978 | + try { |
|
1979 | + // rethrow with the a human-readable path |
|
1980 | + throw new \OCP\Lock\LockedException( |
|
1981 | + $this->getPathRelativeToFiles($absolutePath), |
|
1982 | + $e |
|
1983 | + ); |
|
1984 | + } catch (\InvalidArgumentException $e) { |
|
1985 | + throw new \OCP\Lock\LockedException( |
|
1986 | + $absolutePath, |
|
1987 | + $e |
|
1988 | + ); |
|
1989 | + } |
|
1990 | + } |
|
1991 | + } |
|
1992 | + |
|
1993 | + return true; |
|
1994 | + } |
|
1995 | + |
|
1996 | + /** |
|
1997 | + * Unlock the given path |
|
1998 | + * |
|
1999 | + * @param string $path the path of the file to unlock, relative to the view |
|
2000 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2001 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2002 | + * |
|
2003 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2004 | + */ |
|
2005 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
2006 | + $absolutePath = $this->getAbsolutePath($path); |
|
2007 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2008 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2009 | + return false; |
|
2010 | + } |
|
2011 | + |
|
2012 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
2013 | + if ($mount) { |
|
2014 | + $storage = $mount->getStorage(); |
|
2015 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
2016 | + $storage->releaseLock( |
|
2017 | + $mount->getInternalPath($absolutePath), |
|
2018 | + $type, |
|
2019 | + $this->lockingProvider |
|
2020 | + ); |
|
2021 | + } |
|
2022 | + } |
|
2023 | + |
|
2024 | + return true; |
|
2025 | + } |
|
2026 | + |
|
2027 | + /** |
|
2028 | + * Lock a path and all its parents up to the root of the view |
|
2029 | + * |
|
2030 | + * @param string $path the path of the file to lock relative to the view |
|
2031 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2032 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2033 | + * |
|
2034 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2035 | + */ |
|
2036 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
2037 | + $absolutePath = $this->getAbsolutePath($path); |
|
2038 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2039 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2040 | + return false; |
|
2041 | + } |
|
2042 | + |
|
2043 | + $this->lockPath($path, $type, $lockMountPoint); |
|
2044 | + |
|
2045 | + $parents = $this->getParents($path); |
|
2046 | + foreach ($parents as $parent) { |
|
2047 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2048 | + } |
|
2049 | + |
|
2050 | + return true; |
|
2051 | + } |
|
2052 | + |
|
2053 | + /** |
|
2054 | + * Unlock a path and all its parents up to the root of the view |
|
2055 | + * |
|
2056 | + * @param string $path the path of the file to lock relative to the view |
|
2057 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
2058 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
2059 | + * |
|
2060 | + * @return bool False if the path is excluded from locking, true otherwise |
|
2061 | + */ |
|
2062 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
2063 | + $absolutePath = $this->getAbsolutePath($path); |
|
2064 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
2065 | + if (!$this->shouldLockFile($absolutePath)) { |
|
2066 | + return false; |
|
2067 | + } |
|
2068 | + |
|
2069 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
2070 | + |
|
2071 | + $parents = $this->getParents($path); |
|
2072 | + foreach ($parents as $parent) { |
|
2073 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
2074 | + } |
|
2075 | + |
|
2076 | + return true; |
|
2077 | + } |
|
2078 | + |
|
2079 | + /** |
|
2080 | + * Only lock files in data/user/files/ |
|
2081 | + * |
|
2082 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
2083 | + * @return bool |
|
2084 | + */ |
|
2085 | + protected function shouldLockFile($path) { |
|
2086 | + $path = Filesystem::normalizePath($path); |
|
2087 | + |
|
2088 | + $pathSegments = explode('/', $path); |
|
2089 | + if (isset($pathSegments[2])) { |
|
2090 | + // E.g.: /username/files/path-to-file |
|
2091 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
2092 | + } |
|
2093 | + |
|
2094 | + return strpos($path, '/appdata_') !== 0; |
|
2095 | + } |
|
2096 | + |
|
2097 | + /** |
|
2098 | + * Shortens the given absolute path to be relative to |
|
2099 | + * "$user/files". |
|
2100 | + * |
|
2101 | + * @param string $absolutePath absolute path which is under "files" |
|
2102 | + * |
|
2103 | + * @return string path relative to "files" with trimmed slashes or null |
|
2104 | + * if the path was NOT relative to files |
|
2105 | + * |
|
2106 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
2107 | + * @since 8.1.0 |
|
2108 | + */ |
|
2109 | + public function getPathRelativeToFiles($absolutePath) { |
|
2110 | + $path = Filesystem::normalizePath($absolutePath); |
|
2111 | + $parts = explode('/', trim($path, '/'), 3); |
|
2112 | + // "$user", "files", "path/to/dir" |
|
2113 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
2114 | + $this->logger->error( |
|
2115 | + '$absolutePath must be relative to "files", value is "%s"', |
|
2116 | + [ |
|
2117 | + $absolutePath |
|
2118 | + ] |
|
2119 | + ); |
|
2120 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
2121 | + } |
|
2122 | + if (isset($parts[2])) { |
|
2123 | + return $parts[2]; |
|
2124 | + } |
|
2125 | + return ''; |
|
2126 | + } |
|
2127 | + |
|
2128 | + /** |
|
2129 | + * @param string $filename |
|
2130 | + * @return array |
|
2131 | + * @throws \OC\User\NoUserException |
|
2132 | + * @throws NotFoundException |
|
2133 | + */ |
|
2134 | + public function getUidAndFilename($filename) { |
|
2135 | + $info = $this->getFileInfo($filename); |
|
2136 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
2137 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
2138 | + } |
|
2139 | + $uid = $info->getOwner()->getUID(); |
|
2140 | + if ($uid != \OCP\User::getUser()) { |
|
2141 | + Filesystem::initMountPoints($uid); |
|
2142 | + $ownerView = new View('/' . $uid . '/files'); |
|
2143 | + try { |
|
2144 | + $filename = $ownerView->getPath($info['fileid']); |
|
2145 | + } catch (NotFoundException $e) { |
|
2146 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
2147 | + } |
|
2148 | + } |
|
2149 | + return [$uid, $filename]; |
|
2150 | + } |
|
2151 | + |
|
2152 | + /** |
|
2153 | + * Creates parent non-existing folders |
|
2154 | + * |
|
2155 | + * @param string $filePath |
|
2156 | + * @return bool |
|
2157 | + */ |
|
2158 | + private function createParentDirectories($filePath) { |
|
2159 | + $directoryParts = explode('/', $filePath); |
|
2160 | + $directoryParts = array_filter($directoryParts); |
|
2161 | + foreach ($directoryParts as $key => $part) { |
|
2162 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
2163 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
2164 | + if ($this->is_file($currentPath)) { |
|
2165 | + return false; |
|
2166 | + } |
|
2167 | + if (!$this->file_exists($currentPath)) { |
|
2168 | + $this->mkdir($currentPath); |
|
2169 | + } |
|
2170 | + } |
|
2171 | + |
|
2172 | + return true; |
|
2173 | + } |
|
2174 | 2174 | } |
@@ -62,346 +62,346 @@ |
||
62 | 62 | * @package OC\Group |
63 | 63 | */ |
64 | 64 | class Manager extends PublicEmitter implements IGroupManager { |
65 | - /** @var GroupInterface[] */ |
|
66 | - private $backends = []; |
|
67 | - |
|
68 | - /** @var \OC\User\Manager */ |
|
69 | - private $userManager; |
|
70 | - /** @var EventDispatcherInterface */ |
|
71 | - private $dispatcher; |
|
72 | - /** @var ILogger */ |
|
73 | - private $logger; |
|
74 | - |
|
75 | - /** @var \OC\Group\Group[] */ |
|
76 | - private $cachedGroups = []; |
|
77 | - |
|
78 | - /** @var \OC\Group\Group[] */ |
|
79 | - private $cachedUserGroups = []; |
|
80 | - |
|
81 | - /** @var \OC\SubAdmin */ |
|
82 | - private $subAdmin = null; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param \OC\User\Manager $userManager |
|
86 | - * @param EventDispatcherInterface $dispatcher |
|
87 | - * @param ILogger $logger |
|
88 | - */ |
|
89 | - public function __construct(\OC\User\Manager $userManager, |
|
90 | - EventDispatcherInterface $dispatcher, |
|
91 | - ILogger $logger) { |
|
92 | - $this->userManager = $userManager; |
|
93 | - $this->dispatcher = $dispatcher; |
|
94 | - $this->logger = $logger; |
|
95 | - |
|
96 | - $cachedGroups = & $this->cachedGroups; |
|
97 | - $cachedUserGroups = & $this->cachedUserGroups; |
|
98 | - $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
99 | - /** |
|
100 | - * @var \OC\Group\Group $group |
|
101 | - */ |
|
102 | - unset($cachedGroups[$group->getGID()]); |
|
103 | - $cachedUserGroups = []; |
|
104 | - }); |
|
105 | - $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
106 | - /** |
|
107 | - * @var \OC\Group\Group $group |
|
108 | - */ |
|
109 | - $cachedUserGroups = []; |
|
110 | - }); |
|
111 | - $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
112 | - /** |
|
113 | - * @var \OC\Group\Group $group |
|
114 | - */ |
|
115 | - $cachedUserGroups = []; |
|
116 | - }); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Checks whether a given backend is used |
|
121 | - * |
|
122 | - * @param string $backendClass Full classname including complete namespace |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function isBackendUsed($backendClass) { |
|
126 | - $backendClass = strtolower(ltrim($backendClass, '\\')); |
|
127 | - |
|
128 | - foreach ($this->backends as $backend) { |
|
129 | - if (strtolower(get_class($backend)) === $backendClass) { |
|
130 | - return true; |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - return false; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param \OCP\GroupInterface $backend |
|
139 | - */ |
|
140 | - public function addBackend($backend) { |
|
141 | - $this->backends[] = $backend; |
|
142 | - $this->clearCaches(); |
|
143 | - } |
|
144 | - |
|
145 | - public function clearBackends() { |
|
146 | - $this->backends = []; |
|
147 | - $this->clearCaches(); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get the active backends |
|
152 | - * @return \OCP\GroupInterface[] |
|
153 | - */ |
|
154 | - public function getBackends() { |
|
155 | - return $this->backends; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - protected function clearCaches() { |
|
160 | - $this->cachedGroups = []; |
|
161 | - $this->cachedUserGroups = []; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $gid |
|
166 | - * @return \OC\Group\Group |
|
167 | - */ |
|
168 | - public function get($gid) { |
|
169 | - if (isset($this->cachedGroups[$gid])) { |
|
170 | - return $this->cachedGroups[$gid]; |
|
171 | - } |
|
172 | - return $this->getGroupObject($gid); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $gid |
|
177 | - * @param string $displayName |
|
178 | - * @return \OCP\IGroup |
|
179 | - */ |
|
180 | - protected function getGroupObject($gid, $displayName = null) { |
|
181 | - $backends = []; |
|
182 | - foreach ($this->backends as $backend) { |
|
183 | - if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { |
|
184 | - $groupData = $backend->getGroupDetails($gid); |
|
185 | - if (is_array($groupData) && !empty($groupData)) { |
|
186 | - // take the display name from the first backend that has a non-null one |
|
187 | - if (is_null($displayName) && isset($groupData['displayName'])) { |
|
188 | - $displayName = $groupData['displayName']; |
|
189 | - } |
|
190 | - $backends[] = $backend; |
|
191 | - } |
|
192 | - } else if ($backend->groupExists($gid)) { |
|
193 | - $backends[] = $backend; |
|
194 | - } |
|
195 | - } |
|
196 | - if (count($backends) === 0) { |
|
197 | - return null; |
|
198 | - } |
|
199 | - $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName); |
|
200 | - return $this->cachedGroups[$gid]; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $gid |
|
205 | - * @return bool |
|
206 | - */ |
|
207 | - public function groupExists($gid) { |
|
208 | - return $this->get($gid) instanceof IGroup; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @param string $gid |
|
213 | - * @return \OC\Group\Group |
|
214 | - */ |
|
215 | - public function createGroup($gid) { |
|
216 | - if ($gid === '' || $gid === null) { |
|
217 | - return false; |
|
218 | - } else if ($group = $this->get($gid)) { |
|
219 | - return $group; |
|
220 | - } else { |
|
221 | - $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
222 | - foreach ($this->backends as $backend) { |
|
223 | - if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
224 | - $backend->createGroup($gid); |
|
225 | - $group = $this->getGroupObject($gid); |
|
226 | - $this->emit('\OC\Group', 'postCreate', array($group)); |
|
227 | - return $group; |
|
228 | - } |
|
229 | - } |
|
230 | - return null; |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * @param string $search |
|
236 | - * @param int $limit |
|
237 | - * @param int $offset |
|
238 | - * @return \OC\Group\Group[] |
|
239 | - */ |
|
240 | - public function search($search, $limit = null, $offset = null) { |
|
241 | - $groups = []; |
|
242 | - foreach ($this->backends as $backend) { |
|
243 | - $groupIds = $backend->getGroups($search, $limit, $offset); |
|
244 | - foreach ($groupIds as $groupId) { |
|
245 | - $aGroup = $this->get($groupId); |
|
246 | - if ($aGroup instanceof IGroup) { |
|
247 | - $groups[$groupId] = $aGroup; |
|
248 | - } else { |
|
249 | - $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
250 | - } |
|
251 | - } |
|
252 | - if (!is_null($limit) and $limit <= 0) { |
|
253 | - return array_values($groups); |
|
254 | - } |
|
255 | - } |
|
256 | - return array_values($groups); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * @param IUser|null $user |
|
261 | - * @return \OC\Group\Group[] |
|
262 | - */ |
|
263 | - public function getUserGroups(IUser $user= null) { |
|
264 | - if (!$user instanceof IUser) { |
|
265 | - return []; |
|
266 | - } |
|
267 | - return $this->getUserIdGroups($user->getUID()); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @param string $uid the user id |
|
272 | - * @return \OC\Group\Group[] |
|
273 | - */ |
|
274 | - public function getUserIdGroups($uid) { |
|
275 | - if (isset($this->cachedUserGroups[$uid])) { |
|
276 | - return $this->cachedUserGroups[$uid]; |
|
277 | - } |
|
278 | - $groups = []; |
|
279 | - foreach ($this->backends as $backend) { |
|
280 | - $groupIds = $backend->getUserGroups($uid); |
|
281 | - if (is_array($groupIds)) { |
|
282 | - foreach ($groupIds as $groupId) { |
|
283 | - $aGroup = $this->get($groupId); |
|
284 | - if ($aGroup instanceof IGroup) { |
|
285 | - $groups[$groupId] = $aGroup; |
|
286 | - } else { |
|
287 | - $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
288 | - } |
|
289 | - } |
|
290 | - } |
|
291 | - } |
|
292 | - $this->cachedUserGroups[$uid] = $groups; |
|
293 | - return $this->cachedUserGroups[$uid]; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Checks if a userId is in the admin group |
|
298 | - * @param string $userId |
|
299 | - * @return bool if admin |
|
300 | - */ |
|
301 | - public function isAdmin($userId) { |
|
302 | - foreach ($this->backends as $backend) { |
|
303 | - if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
304 | - return true; |
|
305 | - } |
|
306 | - } |
|
307 | - return $this->isInGroup($userId, 'admin'); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Checks if a userId is in a group |
|
312 | - * @param string $userId |
|
313 | - * @param string $group |
|
314 | - * @return bool if in group |
|
315 | - */ |
|
316 | - public function isInGroup($userId, $group) { |
|
317 | - return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * get a list of group ids for a user |
|
322 | - * @param IUser $user |
|
323 | - * @return array with group ids |
|
324 | - */ |
|
325 | - public function getUserGroupIds(IUser $user) { |
|
326 | - return array_map(function($value) { |
|
327 | - return (string) $value; |
|
328 | - }, array_keys($this->getUserGroups($user))); |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * get an array of groupid and displayName for a user |
|
333 | - * @param IUser $user |
|
334 | - * @return array ['displayName' => displayname] |
|
335 | - */ |
|
336 | - public function getUserGroupNames(IUser $user) { |
|
337 | - return array_map(function($group) { |
|
338 | - return array('displayName' => $group->getDisplayName()); |
|
339 | - }, $this->getUserGroups($user)); |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * get a list of all display names in a group |
|
344 | - * @param string $gid |
|
345 | - * @param string $search |
|
346 | - * @param int $limit |
|
347 | - * @param int $offset |
|
348 | - * @return array an array of display names (value) and user ids (key) |
|
349 | - */ |
|
350 | - public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
351 | - $group = $this->get($gid); |
|
352 | - if(is_null($group)) { |
|
353 | - return []; |
|
354 | - } |
|
355 | - |
|
356 | - $search = trim($search); |
|
357 | - $groupUsers = []; |
|
358 | - |
|
359 | - if(!empty($search)) { |
|
360 | - // only user backends have the capability to do a complex search for users |
|
361 | - $searchOffset = 0; |
|
362 | - $searchLimit = $limit * 100; |
|
363 | - if($limit === -1) { |
|
364 | - $searchLimit = 500; |
|
365 | - } |
|
366 | - |
|
367 | - do { |
|
368 | - $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
369 | - foreach($filteredUsers as $filteredUser) { |
|
370 | - if($group->inGroup($filteredUser)) { |
|
371 | - $groupUsers[]= $filteredUser; |
|
372 | - } |
|
373 | - } |
|
374 | - $searchOffset += $searchLimit; |
|
375 | - } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
376 | - |
|
377 | - if($limit === -1) { |
|
378 | - $groupUsers = array_slice($groupUsers, $offset); |
|
379 | - } else { |
|
380 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
381 | - } |
|
382 | - } else { |
|
383 | - $groupUsers = $group->searchUsers('', $limit, $offset); |
|
384 | - } |
|
385 | - |
|
386 | - $matchingUsers = []; |
|
387 | - foreach($groupUsers as $groupUser) { |
|
388 | - $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
389 | - } |
|
390 | - return $matchingUsers; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * @return \OC\SubAdmin |
|
395 | - */ |
|
396 | - public function getSubAdmin() { |
|
397 | - if (!$this->subAdmin) { |
|
398 | - $this->subAdmin = new \OC\SubAdmin( |
|
399 | - $this->userManager, |
|
400 | - $this, |
|
401 | - \OC::$server->getDatabaseConnection() |
|
402 | - ); |
|
403 | - } |
|
404 | - |
|
405 | - return $this->subAdmin; |
|
406 | - } |
|
65 | + /** @var GroupInterface[] */ |
|
66 | + private $backends = []; |
|
67 | + |
|
68 | + /** @var \OC\User\Manager */ |
|
69 | + private $userManager; |
|
70 | + /** @var EventDispatcherInterface */ |
|
71 | + private $dispatcher; |
|
72 | + /** @var ILogger */ |
|
73 | + private $logger; |
|
74 | + |
|
75 | + /** @var \OC\Group\Group[] */ |
|
76 | + private $cachedGroups = []; |
|
77 | + |
|
78 | + /** @var \OC\Group\Group[] */ |
|
79 | + private $cachedUserGroups = []; |
|
80 | + |
|
81 | + /** @var \OC\SubAdmin */ |
|
82 | + private $subAdmin = null; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param \OC\User\Manager $userManager |
|
86 | + * @param EventDispatcherInterface $dispatcher |
|
87 | + * @param ILogger $logger |
|
88 | + */ |
|
89 | + public function __construct(\OC\User\Manager $userManager, |
|
90 | + EventDispatcherInterface $dispatcher, |
|
91 | + ILogger $logger) { |
|
92 | + $this->userManager = $userManager; |
|
93 | + $this->dispatcher = $dispatcher; |
|
94 | + $this->logger = $logger; |
|
95 | + |
|
96 | + $cachedGroups = & $this->cachedGroups; |
|
97 | + $cachedUserGroups = & $this->cachedUserGroups; |
|
98 | + $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
99 | + /** |
|
100 | + * @var \OC\Group\Group $group |
|
101 | + */ |
|
102 | + unset($cachedGroups[$group->getGID()]); |
|
103 | + $cachedUserGroups = []; |
|
104 | + }); |
|
105 | + $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
106 | + /** |
|
107 | + * @var \OC\Group\Group $group |
|
108 | + */ |
|
109 | + $cachedUserGroups = []; |
|
110 | + }); |
|
111 | + $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
112 | + /** |
|
113 | + * @var \OC\Group\Group $group |
|
114 | + */ |
|
115 | + $cachedUserGroups = []; |
|
116 | + }); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Checks whether a given backend is used |
|
121 | + * |
|
122 | + * @param string $backendClass Full classname including complete namespace |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function isBackendUsed($backendClass) { |
|
126 | + $backendClass = strtolower(ltrim($backendClass, '\\')); |
|
127 | + |
|
128 | + foreach ($this->backends as $backend) { |
|
129 | + if (strtolower(get_class($backend)) === $backendClass) { |
|
130 | + return true; |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + return false; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param \OCP\GroupInterface $backend |
|
139 | + */ |
|
140 | + public function addBackend($backend) { |
|
141 | + $this->backends[] = $backend; |
|
142 | + $this->clearCaches(); |
|
143 | + } |
|
144 | + |
|
145 | + public function clearBackends() { |
|
146 | + $this->backends = []; |
|
147 | + $this->clearCaches(); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get the active backends |
|
152 | + * @return \OCP\GroupInterface[] |
|
153 | + */ |
|
154 | + public function getBackends() { |
|
155 | + return $this->backends; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + protected function clearCaches() { |
|
160 | + $this->cachedGroups = []; |
|
161 | + $this->cachedUserGroups = []; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $gid |
|
166 | + * @return \OC\Group\Group |
|
167 | + */ |
|
168 | + public function get($gid) { |
|
169 | + if (isset($this->cachedGroups[$gid])) { |
|
170 | + return $this->cachedGroups[$gid]; |
|
171 | + } |
|
172 | + return $this->getGroupObject($gid); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $gid |
|
177 | + * @param string $displayName |
|
178 | + * @return \OCP\IGroup |
|
179 | + */ |
|
180 | + protected function getGroupObject($gid, $displayName = null) { |
|
181 | + $backends = []; |
|
182 | + foreach ($this->backends as $backend) { |
|
183 | + if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { |
|
184 | + $groupData = $backend->getGroupDetails($gid); |
|
185 | + if (is_array($groupData) && !empty($groupData)) { |
|
186 | + // take the display name from the first backend that has a non-null one |
|
187 | + if (is_null($displayName) && isset($groupData['displayName'])) { |
|
188 | + $displayName = $groupData['displayName']; |
|
189 | + } |
|
190 | + $backends[] = $backend; |
|
191 | + } |
|
192 | + } else if ($backend->groupExists($gid)) { |
|
193 | + $backends[] = $backend; |
|
194 | + } |
|
195 | + } |
|
196 | + if (count($backends) === 0) { |
|
197 | + return null; |
|
198 | + } |
|
199 | + $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName); |
|
200 | + return $this->cachedGroups[$gid]; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $gid |
|
205 | + * @return bool |
|
206 | + */ |
|
207 | + public function groupExists($gid) { |
|
208 | + return $this->get($gid) instanceof IGroup; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @param string $gid |
|
213 | + * @return \OC\Group\Group |
|
214 | + */ |
|
215 | + public function createGroup($gid) { |
|
216 | + if ($gid === '' || $gid === null) { |
|
217 | + return false; |
|
218 | + } else if ($group = $this->get($gid)) { |
|
219 | + return $group; |
|
220 | + } else { |
|
221 | + $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
222 | + foreach ($this->backends as $backend) { |
|
223 | + if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
224 | + $backend->createGroup($gid); |
|
225 | + $group = $this->getGroupObject($gid); |
|
226 | + $this->emit('\OC\Group', 'postCreate', array($group)); |
|
227 | + return $group; |
|
228 | + } |
|
229 | + } |
|
230 | + return null; |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * @param string $search |
|
236 | + * @param int $limit |
|
237 | + * @param int $offset |
|
238 | + * @return \OC\Group\Group[] |
|
239 | + */ |
|
240 | + public function search($search, $limit = null, $offset = null) { |
|
241 | + $groups = []; |
|
242 | + foreach ($this->backends as $backend) { |
|
243 | + $groupIds = $backend->getGroups($search, $limit, $offset); |
|
244 | + foreach ($groupIds as $groupId) { |
|
245 | + $aGroup = $this->get($groupId); |
|
246 | + if ($aGroup instanceof IGroup) { |
|
247 | + $groups[$groupId] = $aGroup; |
|
248 | + } else { |
|
249 | + $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
250 | + } |
|
251 | + } |
|
252 | + if (!is_null($limit) and $limit <= 0) { |
|
253 | + return array_values($groups); |
|
254 | + } |
|
255 | + } |
|
256 | + return array_values($groups); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * @param IUser|null $user |
|
261 | + * @return \OC\Group\Group[] |
|
262 | + */ |
|
263 | + public function getUserGroups(IUser $user= null) { |
|
264 | + if (!$user instanceof IUser) { |
|
265 | + return []; |
|
266 | + } |
|
267 | + return $this->getUserIdGroups($user->getUID()); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @param string $uid the user id |
|
272 | + * @return \OC\Group\Group[] |
|
273 | + */ |
|
274 | + public function getUserIdGroups($uid) { |
|
275 | + if (isset($this->cachedUserGroups[$uid])) { |
|
276 | + return $this->cachedUserGroups[$uid]; |
|
277 | + } |
|
278 | + $groups = []; |
|
279 | + foreach ($this->backends as $backend) { |
|
280 | + $groupIds = $backend->getUserGroups($uid); |
|
281 | + if (is_array($groupIds)) { |
|
282 | + foreach ($groupIds as $groupId) { |
|
283 | + $aGroup = $this->get($groupId); |
|
284 | + if ($aGroup instanceof IGroup) { |
|
285 | + $groups[$groupId] = $aGroup; |
|
286 | + } else { |
|
287 | + $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
288 | + } |
|
289 | + } |
|
290 | + } |
|
291 | + } |
|
292 | + $this->cachedUserGroups[$uid] = $groups; |
|
293 | + return $this->cachedUserGroups[$uid]; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Checks if a userId is in the admin group |
|
298 | + * @param string $userId |
|
299 | + * @return bool if admin |
|
300 | + */ |
|
301 | + public function isAdmin($userId) { |
|
302 | + foreach ($this->backends as $backend) { |
|
303 | + if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
304 | + return true; |
|
305 | + } |
|
306 | + } |
|
307 | + return $this->isInGroup($userId, 'admin'); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Checks if a userId is in a group |
|
312 | + * @param string $userId |
|
313 | + * @param string $group |
|
314 | + * @return bool if in group |
|
315 | + */ |
|
316 | + public function isInGroup($userId, $group) { |
|
317 | + return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * get a list of group ids for a user |
|
322 | + * @param IUser $user |
|
323 | + * @return array with group ids |
|
324 | + */ |
|
325 | + public function getUserGroupIds(IUser $user) { |
|
326 | + return array_map(function($value) { |
|
327 | + return (string) $value; |
|
328 | + }, array_keys($this->getUserGroups($user))); |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * get an array of groupid and displayName for a user |
|
333 | + * @param IUser $user |
|
334 | + * @return array ['displayName' => displayname] |
|
335 | + */ |
|
336 | + public function getUserGroupNames(IUser $user) { |
|
337 | + return array_map(function($group) { |
|
338 | + return array('displayName' => $group->getDisplayName()); |
|
339 | + }, $this->getUserGroups($user)); |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * get a list of all display names in a group |
|
344 | + * @param string $gid |
|
345 | + * @param string $search |
|
346 | + * @param int $limit |
|
347 | + * @param int $offset |
|
348 | + * @return array an array of display names (value) and user ids (key) |
|
349 | + */ |
|
350 | + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
351 | + $group = $this->get($gid); |
|
352 | + if(is_null($group)) { |
|
353 | + return []; |
|
354 | + } |
|
355 | + |
|
356 | + $search = trim($search); |
|
357 | + $groupUsers = []; |
|
358 | + |
|
359 | + if(!empty($search)) { |
|
360 | + // only user backends have the capability to do a complex search for users |
|
361 | + $searchOffset = 0; |
|
362 | + $searchLimit = $limit * 100; |
|
363 | + if($limit === -1) { |
|
364 | + $searchLimit = 500; |
|
365 | + } |
|
366 | + |
|
367 | + do { |
|
368 | + $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
369 | + foreach($filteredUsers as $filteredUser) { |
|
370 | + if($group->inGroup($filteredUser)) { |
|
371 | + $groupUsers[]= $filteredUser; |
|
372 | + } |
|
373 | + } |
|
374 | + $searchOffset += $searchLimit; |
|
375 | + } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
376 | + |
|
377 | + if($limit === -1) { |
|
378 | + $groupUsers = array_slice($groupUsers, $offset); |
|
379 | + } else { |
|
380 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
381 | + } |
|
382 | + } else { |
|
383 | + $groupUsers = $group->searchUsers('', $limit, $offset); |
|
384 | + } |
|
385 | + |
|
386 | + $matchingUsers = []; |
|
387 | + foreach($groupUsers as $groupUser) { |
|
388 | + $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
389 | + } |
|
390 | + return $matchingUsers; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * @return \OC\SubAdmin |
|
395 | + */ |
|
396 | + public function getSubAdmin() { |
|
397 | + if (!$this->subAdmin) { |
|
398 | + $this->subAdmin = new \OC\SubAdmin( |
|
399 | + $this->userManager, |
|
400 | + $this, |
|
401 | + \OC::$server->getDatabaseConnection() |
|
402 | + ); |
|
403 | + } |
|
404 | + |
|
405 | + return $this->subAdmin; |
|
406 | + } |
|
407 | 407 | } |
@@ -95,20 +95,20 @@ discard block |
||
95 | 95 | |
96 | 96 | $cachedGroups = & $this->cachedGroups; |
97 | 97 | $cachedUserGroups = & $this->cachedUserGroups; |
98 | - $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
98 | + $this->listen('\OC\Group', 'postDelete', function($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
99 | 99 | /** |
100 | 100 | * @var \OC\Group\Group $group |
101 | 101 | */ |
102 | 102 | unset($cachedGroups[$group->getGID()]); |
103 | 103 | $cachedUserGroups = []; |
104 | 104 | }); |
105 | - $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
105 | + $this->listen('\OC\Group', 'postAddUser', function($group) use (&$cachedUserGroups) { |
|
106 | 106 | /** |
107 | 107 | * @var \OC\Group\Group $group |
108 | 108 | */ |
109 | 109 | $cachedUserGroups = []; |
110 | 110 | }); |
111 | - $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
111 | + $this->listen('\OC\Group', 'postRemoveUser', function($group) use (&$cachedUserGroups) { |
|
112 | 112 | /** |
113 | 113 | * @var \OC\Group\Group $group |
114 | 114 | */ |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | if ($aGroup instanceof IGroup) { |
247 | 247 | $groups[$groupId] = $aGroup; |
248 | 248 | } else { |
249 | - $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
249 | + $this->logger->debug('Group "'.$groupId.'" was returned by search but not found through direct access', ['app' => 'core']); |
|
250 | 250 | } |
251 | 251 | } |
252 | 252 | if (!is_null($limit) and $limit <= 0) { |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | * @param IUser|null $user |
261 | 261 | * @return \OC\Group\Group[] |
262 | 262 | */ |
263 | - public function getUserGroups(IUser $user= null) { |
|
263 | + public function getUserGroups(IUser $user = null) { |
|
264 | 264 | if (!$user instanceof IUser) { |
265 | 265 | return []; |
266 | 266 | } |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | if ($aGroup instanceof IGroup) { |
285 | 285 | $groups[$groupId] = $aGroup; |
286 | 286 | } else { |
287 | - $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
287 | + $this->logger->debug('User "'.$uid.'" belongs to deleted group: "'.$groupId.'"', ['app' => 'core']); |
|
288 | 288 | } |
289 | 289 | } |
290 | 290 | } |
@@ -349,32 +349,32 @@ discard block |
||
349 | 349 | */ |
350 | 350 | public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
351 | 351 | $group = $this->get($gid); |
352 | - if(is_null($group)) { |
|
352 | + if (is_null($group)) { |
|
353 | 353 | return []; |
354 | 354 | } |
355 | 355 | |
356 | 356 | $search = trim($search); |
357 | 357 | $groupUsers = []; |
358 | 358 | |
359 | - if(!empty($search)) { |
|
359 | + if (!empty($search)) { |
|
360 | 360 | // only user backends have the capability to do a complex search for users |
361 | 361 | $searchOffset = 0; |
362 | 362 | $searchLimit = $limit * 100; |
363 | - if($limit === -1) { |
|
363 | + if ($limit === -1) { |
|
364 | 364 | $searchLimit = 500; |
365 | 365 | } |
366 | 366 | |
367 | 367 | do { |
368 | 368 | $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
369 | - foreach($filteredUsers as $filteredUser) { |
|
370 | - if($group->inGroup($filteredUser)) { |
|
371 | - $groupUsers[]= $filteredUser; |
|
369 | + foreach ($filteredUsers as $filteredUser) { |
|
370 | + if ($group->inGroup($filteredUser)) { |
|
371 | + $groupUsers[] = $filteredUser; |
|
372 | 372 | } |
373 | 373 | } |
374 | 374 | $searchOffset += $searchLimit; |
375 | - } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
375 | + } while (count($groupUsers) < $searchLimit + $offset && count($filteredUsers) >= $searchLimit); |
|
376 | 376 | |
377 | - if($limit === -1) { |
|
377 | + if ($limit === -1) { |
|
378 | 378 | $groupUsers = array_slice($groupUsers, $offset); |
379 | 379 | } else { |
380 | 380 | $groupUsers = array_slice($groupUsers, $offset, $limit); |
@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | } |
385 | 385 | |
386 | 386 | $matchingUsers = []; |
387 | - foreach($groupUsers as $groupUser) { |
|
387 | + foreach ($groupUsers as $groupUser) { |
|
388 | 388 | $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
389 | 389 | } |
390 | 390 | return $matchingUsers; |
@@ -41,338 +41,338 @@ |
||
41 | 41 | use Symfony\Component\EventDispatcher\GenericEvent; |
42 | 42 | |
43 | 43 | class Group implements IGroup { |
44 | - /** @var null|string */ |
|
45 | - protected $displayName; |
|
44 | + /** @var null|string */ |
|
45 | + protected $displayName; |
|
46 | 46 | |
47 | - /** @var string */ |
|
48 | - private $gid; |
|
47 | + /** @var string */ |
|
48 | + private $gid; |
|
49 | 49 | |
50 | - /** @var \OC\User\User[] */ |
|
51 | - private $users = array(); |
|
50 | + /** @var \OC\User\User[] */ |
|
51 | + private $users = array(); |
|
52 | 52 | |
53 | - /** @var bool */ |
|
54 | - private $usersLoaded; |
|
53 | + /** @var bool */ |
|
54 | + private $usersLoaded; |
|
55 | 55 | |
56 | - /** @var Backend[] */ |
|
57 | - private $backends; |
|
58 | - /** @var EventDispatcherInterface */ |
|
59 | - private $dispatcher; |
|
60 | - /** @var \OC\User\Manager|IUserManager */ |
|
61 | - private $userManager; |
|
62 | - /** @var PublicEmitter */ |
|
63 | - private $emitter; |
|
56 | + /** @var Backend[] */ |
|
57 | + private $backends; |
|
58 | + /** @var EventDispatcherInterface */ |
|
59 | + private $dispatcher; |
|
60 | + /** @var \OC\User\Manager|IUserManager */ |
|
61 | + private $userManager; |
|
62 | + /** @var PublicEmitter */ |
|
63 | + private $emitter; |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * @param string $gid |
|
68 | - * @param Backend[] $backends |
|
69 | - * @param EventDispatcherInterface $dispatcher |
|
70 | - * @param IUserManager $userManager |
|
71 | - * @param PublicEmitter $emitter |
|
72 | - * @param string $displayName |
|
73 | - */ |
|
74 | - public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { |
|
75 | - $this->gid = $gid; |
|
76 | - $this->backends = $backends; |
|
77 | - $this->dispatcher = $dispatcher; |
|
78 | - $this->userManager = $userManager; |
|
79 | - $this->emitter = $emitter; |
|
80 | - $this->displayName = $displayName; |
|
81 | - } |
|
66 | + /** |
|
67 | + * @param string $gid |
|
68 | + * @param Backend[] $backends |
|
69 | + * @param EventDispatcherInterface $dispatcher |
|
70 | + * @param IUserManager $userManager |
|
71 | + * @param PublicEmitter $emitter |
|
72 | + * @param string $displayName |
|
73 | + */ |
|
74 | + public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { |
|
75 | + $this->gid = $gid; |
|
76 | + $this->backends = $backends; |
|
77 | + $this->dispatcher = $dispatcher; |
|
78 | + $this->userManager = $userManager; |
|
79 | + $this->emitter = $emitter; |
|
80 | + $this->displayName = $displayName; |
|
81 | + } |
|
82 | 82 | |
83 | - public function getGID() { |
|
84 | - return $this->gid; |
|
85 | - } |
|
83 | + public function getGID() { |
|
84 | + return $this->gid; |
|
85 | + } |
|
86 | 86 | |
87 | - public function getDisplayName() { |
|
88 | - if (is_null($this->displayName)) { |
|
89 | - return $this->gid; |
|
90 | - } |
|
91 | - return $this->displayName; |
|
92 | - } |
|
87 | + public function getDisplayName() { |
|
88 | + if (is_null($this->displayName)) { |
|
89 | + return $this->gid; |
|
90 | + } |
|
91 | + return $this->displayName; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * get all users in the group |
|
96 | - * |
|
97 | - * @return \OC\User\User[] |
|
98 | - */ |
|
99 | - public function getUsers() { |
|
100 | - if ($this->usersLoaded) { |
|
101 | - return $this->users; |
|
102 | - } |
|
94 | + /** |
|
95 | + * get all users in the group |
|
96 | + * |
|
97 | + * @return \OC\User\User[] |
|
98 | + */ |
|
99 | + public function getUsers() { |
|
100 | + if ($this->usersLoaded) { |
|
101 | + return $this->users; |
|
102 | + } |
|
103 | 103 | |
104 | - $userIds = array(); |
|
105 | - foreach ($this->backends as $backend) { |
|
106 | - $diff = array_diff( |
|
107 | - $backend->usersInGroup($this->gid), |
|
108 | - $userIds |
|
109 | - ); |
|
110 | - if ($diff) { |
|
111 | - $userIds = array_merge($userIds, $diff); |
|
112 | - } |
|
113 | - } |
|
104 | + $userIds = array(); |
|
105 | + foreach ($this->backends as $backend) { |
|
106 | + $diff = array_diff( |
|
107 | + $backend->usersInGroup($this->gid), |
|
108 | + $userIds |
|
109 | + ); |
|
110 | + if ($diff) { |
|
111 | + $userIds = array_merge($userIds, $diff); |
|
112 | + } |
|
113 | + } |
|
114 | 114 | |
115 | - $this->users = $this->getVerifiedUsers($userIds); |
|
116 | - $this->usersLoaded = true; |
|
117 | - return $this->users; |
|
118 | - } |
|
115 | + $this->users = $this->getVerifiedUsers($userIds); |
|
116 | + $this->usersLoaded = true; |
|
117 | + return $this->users; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * check if a user is in the group |
|
122 | - * |
|
123 | - * @param IUser $user |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function inGroup(IUser $user) { |
|
127 | - if (isset($this->users[$user->getUID()])) { |
|
128 | - return true; |
|
129 | - } |
|
130 | - foreach ($this->backends as $backend) { |
|
131 | - if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
132 | - $this->users[$user->getUID()] = $user; |
|
133 | - return true; |
|
134 | - } |
|
135 | - } |
|
136 | - return false; |
|
137 | - } |
|
120 | + /** |
|
121 | + * check if a user is in the group |
|
122 | + * |
|
123 | + * @param IUser $user |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function inGroup(IUser $user) { |
|
127 | + if (isset($this->users[$user->getUID()])) { |
|
128 | + return true; |
|
129 | + } |
|
130 | + foreach ($this->backends as $backend) { |
|
131 | + if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
132 | + $this->users[$user->getUID()] = $user; |
|
133 | + return true; |
|
134 | + } |
|
135 | + } |
|
136 | + return false; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * add a user to the group |
|
141 | - * |
|
142 | - * @param IUser $user |
|
143 | - */ |
|
144 | - public function addUser(IUser $user) { |
|
145 | - if ($this->inGroup($user)) { |
|
146 | - return; |
|
147 | - } |
|
139 | + /** |
|
140 | + * add a user to the group |
|
141 | + * |
|
142 | + * @param IUser $user |
|
143 | + */ |
|
144 | + public function addUser(IUser $user) { |
|
145 | + if ($this->inGroup($user)) { |
|
146 | + return; |
|
147 | + } |
|
148 | 148 | |
149 | - $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
|
150 | - 'user' => $user, |
|
151 | - ])); |
|
149 | + $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
|
150 | + 'user' => $user, |
|
151 | + ])); |
|
152 | 152 | |
153 | - if ($this->emitter) { |
|
154 | - $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
155 | - } |
|
156 | - foreach ($this->backends as $backend) { |
|
157 | - if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
158 | - $backend->addToGroup($user->getUID(), $this->gid); |
|
159 | - if ($this->users) { |
|
160 | - $this->users[$user->getUID()] = $user; |
|
161 | - } |
|
153 | + if ($this->emitter) { |
|
154 | + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
155 | + } |
|
156 | + foreach ($this->backends as $backend) { |
|
157 | + if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
158 | + $backend->addToGroup($user->getUID(), $this->gid); |
|
159 | + if ($this->users) { |
|
160 | + $this->users[$user->getUID()] = $user; |
|
161 | + } |
|
162 | 162 | |
163 | - $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
|
164 | - 'user' => $user, |
|
165 | - ])); |
|
163 | + $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
|
164 | + 'user' => $user, |
|
165 | + ])); |
|
166 | 166 | |
167 | - if ($this->emitter) { |
|
168 | - $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
169 | - } |
|
170 | - return; |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
167 | + if ($this->emitter) { |
|
168 | + $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
169 | + } |
|
170 | + return; |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * remove a user from the group |
|
177 | - * |
|
178 | - * @param \OC\User\User $user |
|
179 | - */ |
|
180 | - public function removeUser($user) { |
|
181 | - $result = false; |
|
182 | - $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ |
|
183 | - 'user' => $user, |
|
184 | - ])); |
|
185 | - if ($this->emitter) { |
|
186 | - $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
187 | - } |
|
188 | - foreach ($this->backends as $backend) { |
|
189 | - if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
190 | - $backend->removeFromGroup($user->getUID(), $this->gid); |
|
191 | - $result = true; |
|
192 | - } |
|
193 | - } |
|
194 | - if ($result) { |
|
195 | - $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ |
|
196 | - 'user' => $user, |
|
197 | - ])); |
|
198 | - if ($this->emitter) { |
|
199 | - $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
200 | - } |
|
201 | - if ($this->users) { |
|
202 | - foreach ($this->users as $index => $groupUser) { |
|
203 | - if ($groupUser->getUID() === $user->getUID()) { |
|
204 | - unset($this->users[$index]); |
|
205 | - return; |
|
206 | - } |
|
207 | - } |
|
208 | - } |
|
209 | - } |
|
210 | - } |
|
175 | + /** |
|
176 | + * remove a user from the group |
|
177 | + * |
|
178 | + * @param \OC\User\User $user |
|
179 | + */ |
|
180 | + public function removeUser($user) { |
|
181 | + $result = false; |
|
182 | + $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ |
|
183 | + 'user' => $user, |
|
184 | + ])); |
|
185 | + if ($this->emitter) { |
|
186 | + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
187 | + } |
|
188 | + foreach ($this->backends as $backend) { |
|
189 | + if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
190 | + $backend->removeFromGroup($user->getUID(), $this->gid); |
|
191 | + $result = true; |
|
192 | + } |
|
193 | + } |
|
194 | + if ($result) { |
|
195 | + $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ |
|
196 | + 'user' => $user, |
|
197 | + ])); |
|
198 | + if ($this->emitter) { |
|
199 | + $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
200 | + } |
|
201 | + if ($this->users) { |
|
202 | + foreach ($this->users as $index => $groupUser) { |
|
203 | + if ($groupUser->getUID() === $user->getUID()) { |
|
204 | + unset($this->users[$index]); |
|
205 | + return; |
|
206 | + } |
|
207 | + } |
|
208 | + } |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * search for users in the group by userid |
|
214 | - * |
|
215 | - * @param string $search |
|
216 | - * @param int $limit |
|
217 | - * @param int $offset |
|
218 | - * @return \OC\User\User[] |
|
219 | - */ |
|
220 | - public function searchUsers($search, $limit = null, $offset = null) { |
|
221 | - $users = array(); |
|
222 | - foreach ($this->backends as $backend) { |
|
223 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
224 | - $users += $this->getVerifiedUsers($userIds); |
|
225 | - if (!is_null($limit) and $limit <= 0) { |
|
226 | - return $users; |
|
227 | - } |
|
228 | - } |
|
229 | - return $users; |
|
230 | - } |
|
212 | + /** |
|
213 | + * search for users in the group by userid |
|
214 | + * |
|
215 | + * @param string $search |
|
216 | + * @param int $limit |
|
217 | + * @param int $offset |
|
218 | + * @return \OC\User\User[] |
|
219 | + */ |
|
220 | + public function searchUsers($search, $limit = null, $offset = null) { |
|
221 | + $users = array(); |
|
222 | + foreach ($this->backends as $backend) { |
|
223 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
224 | + $users += $this->getVerifiedUsers($userIds); |
|
225 | + if (!is_null($limit) and $limit <= 0) { |
|
226 | + return $users; |
|
227 | + } |
|
228 | + } |
|
229 | + return $users; |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * returns the number of users matching the search string |
|
234 | - * |
|
235 | - * @param string $search |
|
236 | - * @return int|bool |
|
237 | - */ |
|
238 | - public function count($search = '') { |
|
239 | - $users = false; |
|
240 | - foreach ($this->backends as $backend) { |
|
241 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
242 | - if($users === false) { |
|
243 | - //we could directly add to a bool variable, but this would |
|
244 | - //be ugly |
|
245 | - $users = 0; |
|
246 | - } |
|
247 | - $users += $backend->countUsersInGroup($this->gid, $search); |
|
248 | - } |
|
249 | - } |
|
250 | - return $users; |
|
251 | - } |
|
232 | + /** |
|
233 | + * returns the number of users matching the search string |
|
234 | + * |
|
235 | + * @param string $search |
|
236 | + * @return int|bool |
|
237 | + */ |
|
238 | + public function count($search = '') { |
|
239 | + $users = false; |
|
240 | + foreach ($this->backends as $backend) { |
|
241 | + if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
242 | + if($users === false) { |
|
243 | + //we could directly add to a bool variable, but this would |
|
244 | + //be ugly |
|
245 | + $users = 0; |
|
246 | + } |
|
247 | + $users += $backend->countUsersInGroup($this->gid, $search); |
|
248 | + } |
|
249 | + } |
|
250 | + return $users; |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * returns the number of disabled users |
|
255 | - * |
|
256 | - * @return int|bool |
|
257 | - */ |
|
258 | - public function countDisabled() { |
|
259 | - $users = false; |
|
260 | - foreach ($this->backends as $backend) { |
|
261 | - if($backend instanceOf ICountDisabledInGroup) { |
|
262 | - if($users === false) { |
|
263 | - //we could directly add to a bool variable, but this would |
|
264 | - //be ugly |
|
265 | - $users = 0; |
|
266 | - } |
|
267 | - $users += $backend->countDisabledInGroup($this->gid); |
|
268 | - } |
|
269 | - } |
|
270 | - return $users; |
|
271 | - } |
|
253 | + /** |
|
254 | + * returns the number of disabled users |
|
255 | + * |
|
256 | + * @return int|bool |
|
257 | + */ |
|
258 | + public function countDisabled() { |
|
259 | + $users = false; |
|
260 | + foreach ($this->backends as $backend) { |
|
261 | + if($backend instanceOf ICountDisabledInGroup) { |
|
262 | + if($users === false) { |
|
263 | + //we could directly add to a bool variable, but this would |
|
264 | + //be ugly |
|
265 | + $users = 0; |
|
266 | + } |
|
267 | + $users += $backend->countDisabledInGroup($this->gid); |
|
268 | + } |
|
269 | + } |
|
270 | + return $users; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * search for users in the group by displayname |
|
275 | - * |
|
276 | - * @param string $search |
|
277 | - * @param int $limit |
|
278 | - * @param int $offset |
|
279 | - * @return \OC\User\User[] |
|
280 | - */ |
|
281 | - public function searchDisplayName($search, $limit = null, $offset = null) { |
|
282 | - $users = array(); |
|
283 | - foreach ($this->backends as $backend) { |
|
284 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
285 | - $users = $this->getVerifiedUsers($userIds); |
|
286 | - if (!is_null($limit) and $limit <= 0) { |
|
287 | - return array_values($users); |
|
288 | - } |
|
289 | - } |
|
290 | - return array_values($users); |
|
291 | - } |
|
273 | + /** |
|
274 | + * search for users in the group by displayname |
|
275 | + * |
|
276 | + * @param string $search |
|
277 | + * @param int $limit |
|
278 | + * @param int $offset |
|
279 | + * @return \OC\User\User[] |
|
280 | + */ |
|
281 | + public function searchDisplayName($search, $limit = null, $offset = null) { |
|
282 | + $users = array(); |
|
283 | + foreach ($this->backends as $backend) { |
|
284 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
285 | + $users = $this->getVerifiedUsers($userIds); |
|
286 | + if (!is_null($limit) and $limit <= 0) { |
|
287 | + return array_values($users); |
|
288 | + } |
|
289 | + } |
|
290 | + return array_values($users); |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * delete the group |
|
295 | - * |
|
296 | - * @return bool |
|
297 | - */ |
|
298 | - public function delete() { |
|
299 | - // Prevent users from deleting group admin |
|
300 | - if ($this->getGID() === 'admin') { |
|
301 | - return false; |
|
302 | - } |
|
293 | + /** |
|
294 | + * delete the group |
|
295 | + * |
|
296 | + * @return bool |
|
297 | + */ |
|
298 | + public function delete() { |
|
299 | + // Prevent users from deleting group admin |
|
300 | + if ($this->getGID() === 'admin') { |
|
301 | + return false; |
|
302 | + } |
|
303 | 303 | |
304 | - $result = false; |
|
305 | - $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this)); |
|
306 | - if ($this->emitter) { |
|
307 | - $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
308 | - } |
|
309 | - foreach ($this->backends as $backend) { |
|
310 | - if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
311 | - $result = true; |
|
312 | - $backend->deleteGroup($this->gid); |
|
313 | - } |
|
314 | - } |
|
315 | - if ($result) { |
|
316 | - $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this)); |
|
317 | - if ($this->emitter) { |
|
318 | - $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
319 | - } |
|
320 | - } |
|
321 | - return $result; |
|
322 | - } |
|
304 | + $result = false; |
|
305 | + $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this)); |
|
306 | + if ($this->emitter) { |
|
307 | + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
308 | + } |
|
309 | + foreach ($this->backends as $backend) { |
|
310 | + if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
311 | + $result = true; |
|
312 | + $backend->deleteGroup($this->gid); |
|
313 | + } |
|
314 | + } |
|
315 | + if ($result) { |
|
316 | + $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this)); |
|
317 | + if ($this->emitter) { |
|
318 | + $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
319 | + } |
|
320 | + } |
|
321 | + return $result; |
|
322 | + } |
|
323 | 323 | |
324 | - /** |
|
325 | - * returns all the Users from an array that really exists |
|
326 | - * @param string[] $userIds an array containing user IDs |
|
327 | - * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
328 | - */ |
|
329 | - private function getVerifiedUsers($userIds) { |
|
330 | - if (!is_array($userIds)) { |
|
331 | - return array(); |
|
332 | - } |
|
333 | - $users = array(); |
|
334 | - foreach ($userIds as $userId) { |
|
335 | - $user = $this->userManager->get($userId); |
|
336 | - if (!is_null($user)) { |
|
337 | - $users[$userId] = $user; |
|
338 | - } |
|
339 | - } |
|
340 | - return $users; |
|
341 | - } |
|
324 | + /** |
|
325 | + * returns all the Users from an array that really exists |
|
326 | + * @param string[] $userIds an array containing user IDs |
|
327 | + * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
328 | + */ |
|
329 | + private function getVerifiedUsers($userIds) { |
|
330 | + if (!is_array($userIds)) { |
|
331 | + return array(); |
|
332 | + } |
|
333 | + $users = array(); |
|
334 | + foreach ($userIds as $userId) { |
|
335 | + $user = $this->userManager->get($userId); |
|
336 | + if (!is_null($user)) { |
|
337 | + $users[$userId] = $user; |
|
338 | + } |
|
339 | + } |
|
340 | + return $users; |
|
341 | + } |
|
342 | 342 | |
343 | - /** |
|
344 | - * @return bool |
|
345 | - * @since 14.0.0 |
|
346 | - */ |
|
347 | - public function canRemoveUser() { |
|
348 | - foreach ($this->backends as $backend) { |
|
349 | - if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) { |
|
350 | - return true; |
|
351 | - } |
|
352 | - } |
|
353 | - return false; |
|
354 | - } |
|
343 | + /** |
|
344 | + * @return bool |
|
345 | + * @since 14.0.0 |
|
346 | + */ |
|
347 | + public function canRemoveUser() { |
|
348 | + foreach ($this->backends as $backend) { |
|
349 | + if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) { |
|
350 | + return true; |
|
351 | + } |
|
352 | + } |
|
353 | + return false; |
|
354 | + } |
|
355 | 355 | |
356 | - /** |
|
357 | - * @return bool |
|
358 | - * @since 14.0.0 |
|
359 | - */ |
|
360 | - public function canAddUser() { |
|
361 | - foreach ($this->backends as $backend) { |
|
362 | - if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
363 | - return true; |
|
364 | - } |
|
365 | - } |
|
366 | - return false; |
|
367 | - } |
|
356 | + /** |
|
357 | + * @return bool |
|
358 | + * @since 14.0.0 |
|
359 | + */ |
|
360 | + public function canAddUser() { |
|
361 | + foreach ($this->backends as $backend) { |
|
362 | + if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
363 | + return true; |
|
364 | + } |
|
365 | + } |
|
366 | + return false; |
|
367 | + } |
|
368 | 368 | |
369 | - /** |
|
370 | - * @return bool |
|
371 | - * @since 16.0.0 |
|
372 | - */ |
|
373 | - public function hideFromCollaboration(): bool { |
|
374 | - return array_reduce($this->backends, function(bool $hide, GroupInterface $backend) { |
|
375 | - return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid)); |
|
376 | - }, false); |
|
377 | - } |
|
369 | + /** |
|
370 | + * @return bool |
|
371 | + * @since 16.0.0 |
|
372 | + */ |
|
373 | + public function hideFromCollaboration(): bool { |
|
374 | + return array_reduce($this->backends, function(bool $hide, GroupInterface $backend) { |
|
375 | + return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid)); |
|
376 | + }, false); |
|
377 | + } |
|
378 | 378 | } |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | return; |
147 | 147 | } |
148 | 148 | |
149 | - $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
|
149 | + $this->dispatcher->dispatch(IGroup::class.'::preAddUser', new GenericEvent($this, [ |
|
150 | 150 | 'user' => $user, |
151 | 151 | ])); |
152 | 152 | |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | $this->users[$user->getUID()] = $user; |
161 | 161 | } |
162 | 162 | |
163 | - $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
|
163 | + $this->dispatcher->dispatch(IGroup::class.'::postAddUser', new GenericEvent($this, [ |
|
164 | 164 | 'user' => $user, |
165 | 165 | ])); |
166 | 166 | |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | */ |
180 | 180 | public function removeUser($user) { |
181 | 181 | $result = false; |
182 | - $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ |
|
182 | + $this->dispatcher->dispatch(IGroup::class.'::preRemoveUser', new GenericEvent($this, [ |
|
183 | 183 | 'user' => $user, |
184 | 184 | ])); |
185 | 185 | if ($this->emitter) { |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | } |
193 | 193 | } |
194 | 194 | if ($result) { |
195 | - $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ |
|
195 | + $this->dispatcher->dispatch(IGroup::class.'::postRemoveUser', new GenericEvent($this, [ |
|
196 | 196 | 'user' => $user, |
197 | 197 | ])); |
198 | 198 | if ($this->emitter) { |
@@ -238,8 +238,8 @@ discard block |
||
238 | 238 | public function count($search = '') { |
239 | 239 | $users = false; |
240 | 240 | foreach ($this->backends as $backend) { |
241 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
242 | - if($users === false) { |
|
241 | + if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
242 | + if ($users === false) { |
|
243 | 243 | //we could directly add to a bool variable, but this would |
244 | 244 | //be ugly |
245 | 245 | $users = 0; |
@@ -258,8 +258,8 @@ discard block |
||
258 | 258 | public function countDisabled() { |
259 | 259 | $users = false; |
260 | 260 | foreach ($this->backends as $backend) { |
261 | - if($backend instanceOf ICountDisabledInGroup) { |
|
262 | - if($users === false) { |
|
261 | + if ($backend instanceOf ICountDisabledInGroup) { |
|
262 | + if ($users === false) { |
|
263 | 263 | //we could directly add to a bool variable, but this would |
264 | 264 | //be ugly |
265 | 265 | $users = 0; |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | } |
303 | 303 | |
304 | 304 | $result = false; |
305 | - $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this)); |
|
305 | + $this->dispatcher->dispatch(IGroup::class.'::preDelete', new GenericEvent($this)); |
|
306 | 306 | if ($this->emitter) { |
307 | 307 | $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
308 | 308 | } |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | } |
314 | 314 | } |
315 | 315 | if ($result) { |
316 | - $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this)); |
|
316 | + $this->dispatcher->dispatch(IGroup::class.'::postDelete', new GenericEvent($this)); |
|
317 | 317 | if ($this->emitter) { |
318 | 318 | $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
319 | 319 | } |
@@ -68,348 +68,348 @@ |
||
68 | 68 | |
69 | 69 | class DIContainer extends SimpleContainer implements IAppContainer { |
70 | 70 | |
71 | - /** |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - private $middleWares = []; |
|
75 | - |
|
76 | - /** @var ServerContainer */ |
|
77 | - private $server; |
|
78 | - |
|
79 | - /** |
|
80 | - * Put your class dependencies in here |
|
81 | - * @param string $appName the name of the app |
|
82 | - * @param array $urlParams |
|
83 | - * @param ServerContainer|null $server |
|
84 | - */ |
|
85 | - public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
86 | - parent::__construct(); |
|
87 | - $this['AppName'] = $appName; |
|
88 | - $this['urlParams'] = $urlParams; |
|
89 | - |
|
90 | - $this->registerAlias('Request', IRequest::class); |
|
91 | - |
|
92 | - /** @var \OC\ServerContainer $server */ |
|
93 | - if ($server === null) { |
|
94 | - $server = \OC::$server; |
|
95 | - } |
|
96 | - $this->server = $server; |
|
97 | - $this->server->registerAppContainer($appName, $this); |
|
98 | - |
|
99 | - // aliases |
|
100 | - $this->registerAlias('appName', 'AppName'); |
|
101 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
102 | - $this->registerAlias('userId', 'UserId'); |
|
103 | - |
|
104 | - /** |
|
105 | - * Core services |
|
106 | - */ |
|
107 | - $this->registerService(IOutput::class, function(){ |
|
108 | - return new Output($this->getServer()->getWebRoot()); |
|
109 | - }); |
|
110 | - |
|
111 | - $this->registerService(Folder::class, function() { |
|
112 | - return $this->getServer()->getUserFolder(); |
|
113 | - }); |
|
114 | - |
|
115 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
116 | - return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
117 | - }); |
|
118 | - |
|
119 | - $this->registerService(IL10N::class, function($c) { |
|
120 | - return $this->getServer()->getL10N($c->query('AppName')); |
|
121 | - }); |
|
122 | - |
|
123 | - // Log wrapper |
|
124 | - $this->registerService(ILogger::class, function ($c) { |
|
125 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
126 | - }); |
|
127 | - |
|
128 | - $this->registerService(IServerContainer::class, function () { |
|
129 | - return $this->getServer(); |
|
130 | - }); |
|
131 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | - |
|
133 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | - return $c->query(Manager::class); |
|
135 | - }); |
|
136 | - |
|
137 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | - return $c; |
|
139 | - }); |
|
140 | - |
|
141 | - // commonly used attributes |
|
142 | - $this->registerService('UserId', function ($c) { |
|
143 | - return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
144 | - }); |
|
145 | - |
|
146 | - $this->registerService('WebRoot', function ($c) { |
|
147 | - return $c->query('ServerContainer')->getWebRoot(); |
|
148 | - }); |
|
149 | - |
|
150 | - $this->registerService('OC_Defaults', function ($c) { |
|
151 | - return $c->getServer()->getThemingDefaults(); |
|
152 | - }); |
|
153 | - |
|
154 | - $this->registerService(IConfig::class, function ($c) { |
|
155 | - return $c->query(OC\GlobalScale\Config::class); |
|
156 | - }); |
|
157 | - |
|
158 | - $this->registerService('Protocol', function($c){ |
|
159 | - /** @var \OC\Server $server */ |
|
160 | - $server = $c->query('ServerContainer'); |
|
161 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
162 | - return new Http($_SERVER, $protocol); |
|
163 | - }); |
|
164 | - |
|
165 | - $this->registerService('Dispatcher', function($c) { |
|
166 | - return new Dispatcher( |
|
167 | - $c['Protocol'], |
|
168 | - $c['MiddlewareDispatcher'], |
|
169 | - $c->query(IControllerMethodReflector::class), |
|
170 | - $c['Request'] |
|
171 | - ); |
|
172 | - }); |
|
173 | - |
|
174 | - /** |
|
175 | - * App Framework default arguments |
|
176 | - */ |
|
177 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
178 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
179 | - $this->registerParameter('corsMaxAge', 1728000); |
|
180 | - |
|
181 | - /** |
|
182 | - * Middleware |
|
183 | - */ |
|
184 | - $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
185 | - $server = $this->getServer(); |
|
186 | - |
|
187 | - $dispatcher = new MiddlewareDispatcher(); |
|
188 | - $dispatcher->registerMiddleware( |
|
189 | - $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
190 | - ); |
|
191 | - |
|
192 | - $dispatcher->registerMiddleware( |
|
193 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
194 | - $c->query(IRequest::class), |
|
195 | - $c->query(IControllerMethodReflector::class) |
|
196 | - ) |
|
197 | - ); |
|
198 | - $dispatcher->registerMiddleware( |
|
199 | - new CORSMiddleware( |
|
200 | - $c->query(IRequest::class), |
|
201 | - $c->query(IControllerMethodReflector::class), |
|
202 | - $c->query(IUserSession::class), |
|
203 | - $c->query(OC\Security\Bruteforce\Throttler::class) |
|
204 | - ) |
|
205 | - ); |
|
206 | - $dispatcher->registerMiddleware( |
|
207 | - new OCSMiddleware( |
|
208 | - $c->query(IRequest::class) |
|
209 | - ) |
|
210 | - ); |
|
211 | - |
|
212 | - $securityMiddleware = new SecurityMiddleware( |
|
213 | - $c->query(IRequest::class), |
|
214 | - $c->query(IControllerMethodReflector::class), |
|
215 | - $c->query(INavigationManager::class), |
|
216 | - $c->query(IURLGenerator::class), |
|
217 | - $server->getLogger(), |
|
218 | - $c['AppName'], |
|
219 | - $server->getUserSession()->isLoggedIn(), |
|
220 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
221 | - $server->getContentSecurityPolicyManager(), |
|
222 | - $server->getCsrfTokenManager(), |
|
223 | - $server->getContentSecurityPolicyNonceManager(), |
|
224 | - $server->getAppManager(), |
|
225 | - $server->getL10N('lib') |
|
226 | - ); |
|
227 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
228 | - $dispatcher->registerMiddleware( |
|
229 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
230 | - $c->query(IControllerMethodReflector::class), |
|
231 | - $c->query(ISession::class), |
|
232 | - $c->query(IUserSession::class), |
|
233 | - $c->query(ITimeFactory::class) |
|
234 | - ) |
|
235 | - ); |
|
236 | - $dispatcher->registerMiddleware( |
|
237 | - new TwoFactorMiddleware( |
|
238 | - $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
239 | - $c->query(IUserSession::class), |
|
240 | - $c->query(ISession::class), |
|
241 | - $c->query(IURLGenerator::class), |
|
242 | - $c->query(IControllerMethodReflector::class), |
|
243 | - $c->query(IRequest::class) |
|
244 | - ) |
|
245 | - ); |
|
246 | - $dispatcher->registerMiddleware( |
|
247 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
248 | - $c->query(IControllerMethodReflector::class), |
|
249 | - $c->query(OC\Security\Bruteforce\Throttler::class), |
|
250 | - $c->query(IRequest::class) |
|
251 | - ) |
|
252 | - ); |
|
253 | - $dispatcher->registerMiddleware( |
|
254 | - new RateLimitingMiddleware( |
|
255 | - $c->query(IRequest::class), |
|
256 | - $c->query(IUserSession::class), |
|
257 | - $c->query(IControllerMethodReflector::class), |
|
258 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
259 | - ) |
|
260 | - ); |
|
261 | - $dispatcher->registerMiddleware( |
|
262 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
263 | - $c->query(IRequest::class), |
|
264 | - $c->query(ISession::class), |
|
265 | - $c->query(\OCP\IConfig::class) |
|
266 | - ) |
|
267 | - ); |
|
268 | - $dispatcher->registerMiddleware( |
|
269 | - $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
270 | - ); |
|
271 | - |
|
272 | - foreach($this->middleWares as $middleWare) { |
|
273 | - $dispatcher->registerMiddleware($c[$middleWare]); |
|
274 | - } |
|
275 | - |
|
276 | - $dispatcher->registerMiddleware( |
|
277 | - new SessionMiddleware( |
|
278 | - $c->query(IRequest::class), |
|
279 | - $c->query(IControllerMethodReflector::class), |
|
280 | - $c->query(ISession::class) |
|
281 | - ) |
|
282 | - ); |
|
283 | - return $dispatcher; |
|
284 | - }); |
|
285 | - |
|
286 | - $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @return \OCP\IServerContainer |
|
291 | - */ |
|
292 | - public function getServer() |
|
293 | - { |
|
294 | - return $this->server; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * @param string $middleWare |
|
299 | - * @return boolean|null |
|
300 | - */ |
|
301 | - public function registerMiddleWare($middleWare) { |
|
302 | - $this->middleWares[] = $middleWare; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * used to return the appname of the set application |
|
307 | - * @return string the name of your application |
|
308 | - */ |
|
309 | - public function getAppName() { |
|
310 | - return $this->query('AppName'); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @deprecated use IUserSession->isLoggedIn() |
|
315 | - * @return boolean |
|
316 | - */ |
|
317 | - public function isLoggedIn() { |
|
318 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
323 | - * @return boolean |
|
324 | - */ |
|
325 | - public function isAdminUser() { |
|
326 | - $uid = $this->getUserId(); |
|
327 | - return \OC_User::isAdminUser($uid); |
|
328 | - } |
|
329 | - |
|
330 | - private function getUserId() { |
|
331 | - return $this->getServer()->getSession()->get('user_id'); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @deprecated use the ILogger instead |
|
336 | - * @param string $message |
|
337 | - * @param string $level |
|
338 | - * @return mixed |
|
339 | - */ |
|
340 | - public function log($message, $level) { |
|
341 | - switch($level){ |
|
342 | - case 'debug': |
|
343 | - $level = ILogger::DEBUG; |
|
344 | - break; |
|
345 | - case 'info': |
|
346 | - $level = ILogger::INFO; |
|
347 | - break; |
|
348 | - case 'warn': |
|
349 | - $level = ILogger::WARN; |
|
350 | - break; |
|
351 | - case 'fatal': |
|
352 | - $level = ILogger::FATAL; |
|
353 | - break; |
|
354 | - default: |
|
355 | - $level = ILogger::ERROR; |
|
356 | - break; |
|
357 | - } |
|
358 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Register a capability |
|
363 | - * |
|
364 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
365 | - */ |
|
366 | - public function registerCapability($serviceName) { |
|
367 | - $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
368 | - return $this->query($serviceName); |
|
369 | - }); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @param string $name |
|
374 | - * @return mixed |
|
375 | - * @throws QueryException if the query could not be resolved |
|
376 | - */ |
|
377 | - public function query($name) { |
|
378 | - try { |
|
379 | - return $this->queryNoFallback($name); |
|
380 | - } catch (QueryException $firstException) { |
|
381 | - try { |
|
382 | - return $this->getServer()->query($name); |
|
383 | - } catch (QueryException $secondException) { |
|
384 | - if ($firstException->getCode() === 1) { |
|
385 | - throw $secondException; |
|
386 | - } |
|
387 | - throw $firstException; |
|
388 | - } |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * @param string $name |
|
394 | - * @return mixed |
|
395 | - * @throws QueryException if the query could not be resolved |
|
396 | - */ |
|
397 | - public function queryNoFallback($name) { |
|
398 | - $name = $this->sanitizeName($name); |
|
399 | - |
|
400 | - if ($this->offsetExists($name)) { |
|
401 | - return parent::query($name); |
|
402 | - } else { |
|
403 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
404 | - return parent::query($name); |
|
405 | - } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
406 | - return parent::query($name); |
|
407 | - } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
408 | - return parent::query($name); |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
413 | - ' Class can not be instantiated', 1); |
|
414 | - } |
|
71 | + /** |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + private $middleWares = []; |
|
75 | + |
|
76 | + /** @var ServerContainer */ |
|
77 | + private $server; |
|
78 | + |
|
79 | + /** |
|
80 | + * Put your class dependencies in here |
|
81 | + * @param string $appName the name of the app |
|
82 | + * @param array $urlParams |
|
83 | + * @param ServerContainer|null $server |
|
84 | + */ |
|
85 | + public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
86 | + parent::__construct(); |
|
87 | + $this['AppName'] = $appName; |
|
88 | + $this['urlParams'] = $urlParams; |
|
89 | + |
|
90 | + $this->registerAlias('Request', IRequest::class); |
|
91 | + |
|
92 | + /** @var \OC\ServerContainer $server */ |
|
93 | + if ($server === null) { |
|
94 | + $server = \OC::$server; |
|
95 | + } |
|
96 | + $this->server = $server; |
|
97 | + $this->server->registerAppContainer($appName, $this); |
|
98 | + |
|
99 | + // aliases |
|
100 | + $this->registerAlias('appName', 'AppName'); |
|
101 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
102 | + $this->registerAlias('userId', 'UserId'); |
|
103 | + |
|
104 | + /** |
|
105 | + * Core services |
|
106 | + */ |
|
107 | + $this->registerService(IOutput::class, function(){ |
|
108 | + return new Output($this->getServer()->getWebRoot()); |
|
109 | + }); |
|
110 | + |
|
111 | + $this->registerService(Folder::class, function() { |
|
112 | + return $this->getServer()->getUserFolder(); |
|
113 | + }); |
|
114 | + |
|
115 | + $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
116 | + return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
117 | + }); |
|
118 | + |
|
119 | + $this->registerService(IL10N::class, function($c) { |
|
120 | + return $this->getServer()->getL10N($c->query('AppName')); |
|
121 | + }); |
|
122 | + |
|
123 | + // Log wrapper |
|
124 | + $this->registerService(ILogger::class, function ($c) { |
|
125 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
126 | + }); |
|
127 | + |
|
128 | + $this->registerService(IServerContainer::class, function () { |
|
129 | + return $this->getServer(); |
|
130 | + }); |
|
131 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | + |
|
133 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | + return $c->query(Manager::class); |
|
135 | + }); |
|
136 | + |
|
137 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | + return $c; |
|
139 | + }); |
|
140 | + |
|
141 | + // commonly used attributes |
|
142 | + $this->registerService('UserId', function ($c) { |
|
143 | + return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
144 | + }); |
|
145 | + |
|
146 | + $this->registerService('WebRoot', function ($c) { |
|
147 | + return $c->query('ServerContainer')->getWebRoot(); |
|
148 | + }); |
|
149 | + |
|
150 | + $this->registerService('OC_Defaults', function ($c) { |
|
151 | + return $c->getServer()->getThemingDefaults(); |
|
152 | + }); |
|
153 | + |
|
154 | + $this->registerService(IConfig::class, function ($c) { |
|
155 | + return $c->query(OC\GlobalScale\Config::class); |
|
156 | + }); |
|
157 | + |
|
158 | + $this->registerService('Protocol', function($c){ |
|
159 | + /** @var \OC\Server $server */ |
|
160 | + $server = $c->query('ServerContainer'); |
|
161 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
162 | + return new Http($_SERVER, $protocol); |
|
163 | + }); |
|
164 | + |
|
165 | + $this->registerService('Dispatcher', function($c) { |
|
166 | + return new Dispatcher( |
|
167 | + $c['Protocol'], |
|
168 | + $c['MiddlewareDispatcher'], |
|
169 | + $c->query(IControllerMethodReflector::class), |
|
170 | + $c['Request'] |
|
171 | + ); |
|
172 | + }); |
|
173 | + |
|
174 | + /** |
|
175 | + * App Framework default arguments |
|
176 | + */ |
|
177 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
178 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
179 | + $this->registerParameter('corsMaxAge', 1728000); |
|
180 | + |
|
181 | + /** |
|
182 | + * Middleware |
|
183 | + */ |
|
184 | + $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
185 | + $server = $this->getServer(); |
|
186 | + |
|
187 | + $dispatcher = new MiddlewareDispatcher(); |
|
188 | + $dispatcher->registerMiddleware( |
|
189 | + $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
190 | + ); |
|
191 | + |
|
192 | + $dispatcher->registerMiddleware( |
|
193 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
194 | + $c->query(IRequest::class), |
|
195 | + $c->query(IControllerMethodReflector::class) |
|
196 | + ) |
|
197 | + ); |
|
198 | + $dispatcher->registerMiddleware( |
|
199 | + new CORSMiddleware( |
|
200 | + $c->query(IRequest::class), |
|
201 | + $c->query(IControllerMethodReflector::class), |
|
202 | + $c->query(IUserSession::class), |
|
203 | + $c->query(OC\Security\Bruteforce\Throttler::class) |
|
204 | + ) |
|
205 | + ); |
|
206 | + $dispatcher->registerMiddleware( |
|
207 | + new OCSMiddleware( |
|
208 | + $c->query(IRequest::class) |
|
209 | + ) |
|
210 | + ); |
|
211 | + |
|
212 | + $securityMiddleware = new SecurityMiddleware( |
|
213 | + $c->query(IRequest::class), |
|
214 | + $c->query(IControllerMethodReflector::class), |
|
215 | + $c->query(INavigationManager::class), |
|
216 | + $c->query(IURLGenerator::class), |
|
217 | + $server->getLogger(), |
|
218 | + $c['AppName'], |
|
219 | + $server->getUserSession()->isLoggedIn(), |
|
220 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
221 | + $server->getContentSecurityPolicyManager(), |
|
222 | + $server->getCsrfTokenManager(), |
|
223 | + $server->getContentSecurityPolicyNonceManager(), |
|
224 | + $server->getAppManager(), |
|
225 | + $server->getL10N('lib') |
|
226 | + ); |
|
227 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
228 | + $dispatcher->registerMiddleware( |
|
229 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
230 | + $c->query(IControllerMethodReflector::class), |
|
231 | + $c->query(ISession::class), |
|
232 | + $c->query(IUserSession::class), |
|
233 | + $c->query(ITimeFactory::class) |
|
234 | + ) |
|
235 | + ); |
|
236 | + $dispatcher->registerMiddleware( |
|
237 | + new TwoFactorMiddleware( |
|
238 | + $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
239 | + $c->query(IUserSession::class), |
|
240 | + $c->query(ISession::class), |
|
241 | + $c->query(IURLGenerator::class), |
|
242 | + $c->query(IControllerMethodReflector::class), |
|
243 | + $c->query(IRequest::class) |
|
244 | + ) |
|
245 | + ); |
|
246 | + $dispatcher->registerMiddleware( |
|
247 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
248 | + $c->query(IControllerMethodReflector::class), |
|
249 | + $c->query(OC\Security\Bruteforce\Throttler::class), |
|
250 | + $c->query(IRequest::class) |
|
251 | + ) |
|
252 | + ); |
|
253 | + $dispatcher->registerMiddleware( |
|
254 | + new RateLimitingMiddleware( |
|
255 | + $c->query(IRequest::class), |
|
256 | + $c->query(IUserSession::class), |
|
257 | + $c->query(IControllerMethodReflector::class), |
|
258 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
259 | + ) |
|
260 | + ); |
|
261 | + $dispatcher->registerMiddleware( |
|
262 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
263 | + $c->query(IRequest::class), |
|
264 | + $c->query(ISession::class), |
|
265 | + $c->query(\OCP\IConfig::class) |
|
266 | + ) |
|
267 | + ); |
|
268 | + $dispatcher->registerMiddleware( |
|
269 | + $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
270 | + ); |
|
271 | + |
|
272 | + foreach($this->middleWares as $middleWare) { |
|
273 | + $dispatcher->registerMiddleware($c[$middleWare]); |
|
274 | + } |
|
275 | + |
|
276 | + $dispatcher->registerMiddleware( |
|
277 | + new SessionMiddleware( |
|
278 | + $c->query(IRequest::class), |
|
279 | + $c->query(IControllerMethodReflector::class), |
|
280 | + $c->query(ISession::class) |
|
281 | + ) |
|
282 | + ); |
|
283 | + return $dispatcher; |
|
284 | + }); |
|
285 | + |
|
286 | + $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @return \OCP\IServerContainer |
|
291 | + */ |
|
292 | + public function getServer() |
|
293 | + { |
|
294 | + return $this->server; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * @param string $middleWare |
|
299 | + * @return boolean|null |
|
300 | + */ |
|
301 | + public function registerMiddleWare($middleWare) { |
|
302 | + $this->middleWares[] = $middleWare; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * used to return the appname of the set application |
|
307 | + * @return string the name of your application |
|
308 | + */ |
|
309 | + public function getAppName() { |
|
310 | + return $this->query('AppName'); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @deprecated use IUserSession->isLoggedIn() |
|
315 | + * @return boolean |
|
316 | + */ |
|
317 | + public function isLoggedIn() { |
|
318 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
323 | + * @return boolean |
|
324 | + */ |
|
325 | + public function isAdminUser() { |
|
326 | + $uid = $this->getUserId(); |
|
327 | + return \OC_User::isAdminUser($uid); |
|
328 | + } |
|
329 | + |
|
330 | + private function getUserId() { |
|
331 | + return $this->getServer()->getSession()->get('user_id'); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @deprecated use the ILogger instead |
|
336 | + * @param string $message |
|
337 | + * @param string $level |
|
338 | + * @return mixed |
|
339 | + */ |
|
340 | + public function log($message, $level) { |
|
341 | + switch($level){ |
|
342 | + case 'debug': |
|
343 | + $level = ILogger::DEBUG; |
|
344 | + break; |
|
345 | + case 'info': |
|
346 | + $level = ILogger::INFO; |
|
347 | + break; |
|
348 | + case 'warn': |
|
349 | + $level = ILogger::WARN; |
|
350 | + break; |
|
351 | + case 'fatal': |
|
352 | + $level = ILogger::FATAL; |
|
353 | + break; |
|
354 | + default: |
|
355 | + $level = ILogger::ERROR; |
|
356 | + break; |
|
357 | + } |
|
358 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Register a capability |
|
363 | + * |
|
364 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
365 | + */ |
|
366 | + public function registerCapability($serviceName) { |
|
367 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
368 | + return $this->query($serviceName); |
|
369 | + }); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @param string $name |
|
374 | + * @return mixed |
|
375 | + * @throws QueryException if the query could not be resolved |
|
376 | + */ |
|
377 | + public function query($name) { |
|
378 | + try { |
|
379 | + return $this->queryNoFallback($name); |
|
380 | + } catch (QueryException $firstException) { |
|
381 | + try { |
|
382 | + return $this->getServer()->query($name); |
|
383 | + } catch (QueryException $secondException) { |
|
384 | + if ($firstException->getCode() === 1) { |
|
385 | + throw $secondException; |
|
386 | + } |
|
387 | + throw $firstException; |
|
388 | + } |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * @param string $name |
|
394 | + * @return mixed |
|
395 | + * @throws QueryException if the query could not be resolved |
|
396 | + */ |
|
397 | + public function queryNoFallback($name) { |
|
398 | + $name = $this->sanitizeName($name); |
|
399 | + |
|
400 | + if ($this->offsetExists($name)) { |
|
401 | + return parent::query($name); |
|
402 | + } else { |
|
403 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
404 | + return parent::query($name); |
|
405 | + } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
406 | + return parent::query($name); |
|
407 | + } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
408 | + return parent::query($name); |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
413 | + ' Class can not be instantiated', 1); |
|
414 | + } |
|
415 | 415 | } |