2016-11-04 13:34:42 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-11-16 14:19:42 +03:00
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
2016-11-16 14:19:42 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
2016-11-16 14:19:42 +03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-11-16 14:19:42 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-11-04 13:34:42 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
2016-11-16 14:19:42 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files\Activity;
|
|
|
|
|
|
|
|
use OCP\Activity\IEvent;
|
2016-11-25 17:21:16 +03:00
|
|
|
use OCP\Activity\IEventMerger;
|
2016-11-10 18:02:19 +03:00
|
|
|
use OCP\Activity\IManager;
|
2016-11-04 13:34:42 +03:00
|
|
|
use OCP\Activity\IProvider;
|
2020-01-28 14:27:47 +03:00
|
|
|
use OCP\Contacts\IManager as IContactsManager;
|
2020-01-28 14:51:43 +03:00
|
|
|
use OCP\Federation\ICloudIdManager;
|
2018-02-10 20:09:04 +03:00
|
|
|
use OCP\Files\Folder;
|
2018-02-12 18:05:42 +03:00
|
|
|
use OCP\Files\InvalidPathException;
|
2018-02-10 20:09:04 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\Files\NotFoundException;
|
2016-11-04 13:34:42 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IURLGenerator;
|
2016-11-22 18:39:29 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2016-12-01 13:35:23 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2016-11-04 13:34:42 +03:00
|
|
|
|
|
|
|
class Provider implements IProvider {
|
|
|
|
|
2016-12-01 13:35:23 +03:00
|
|
|
/** @var IFactory */
|
|
|
|
protected $languageFactory;
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $l;
|
2017-05-19 16:45:14 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $activityLang;
|
2016-11-04 13:34:42 +03:00
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $url;
|
|
|
|
|
2016-11-10 18:02:19 +03:00
|
|
|
/** @var IManager */
|
|
|
|
protected $activityManager;
|
|
|
|
|
2016-11-22 18:39:29 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
2018-02-10 20:09:04 +03:00
|
|
|
/** @var IRootFolder */
|
|
|
|
protected $rootFolder;
|
|
|
|
|
2016-11-25 17:21:16 +03:00
|
|
|
/** @var IEventMerger */
|
|
|
|
protected $eventMerger;
|
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
/** @var ICloudIdManager */
|
|
|
|
protected $cloudIdManager;
|
|
|
|
|
2020-01-28 14:27:47 +03:00
|
|
|
/** @var IContactsManager */
|
|
|
|
protected $contactsManager;
|
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
/** @var string[] cached displayNames - key is the cloud id and value the displayname */
|
2016-11-22 18:55:02 +03:00
|
|
|
protected $displayNames = [];
|
|
|
|
|
2018-02-10 20:09:04 +03:00
|
|
|
protected $fileIsEncrypted = false;
|
|
|
|
|
2020-01-28 14:27:47 +03:00
|
|
|
public function __construct(IFactory $languageFactory,
|
|
|
|
IURLGenerator $url,
|
|
|
|
IManager $activityManager,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IRootFolder $rootFolder,
|
2020-01-28 14:51:43 +03:00
|
|
|
ICloudIdManager $cloudIdManager,
|
2020-01-28 14:27:47 +03:00
|
|
|
IContactsManager $contactsManager,
|
|
|
|
IEventMerger $eventMerger) {
|
2016-12-01 13:35:23 +03:00
|
|
|
$this->languageFactory = $languageFactory;
|
2016-11-04 13:34:42 +03:00
|
|
|
$this->url = $url;
|
2016-11-10 18:02:19 +03:00
|
|
|
$this->activityManager = $activityManager;
|
2016-11-22 18:39:29 +03:00
|
|
|
$this->userManager = $userManager;
|
2018-02-10 20:09:04 +03:00
|
|
|
$this->rootFolder = $rootFolder;
|
2020-01-28 14:51:43 +03:00
|
|
|
$this->cloudIdManager = $cloudIdManager;
|
2020-01-28 14:27:47 +03:00
|
|
|
$this->contactsManager = $contactsManager;
|
2016-11-25 17:21:16 +03:00
|
|
|
$this->eventMerger = $eventMerger;
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-01 13:35:23 +03:00
|
|
|
* @param string $language
|
2016-11-04 13:34:42 +03:00
|
|
|
* @param IEvent $event
|
|
|
|
* @param IEvent|null $previousEvent
|
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-11-04 13:34:42 +03:00
|
|
|
*/
|
2016-12-01 13:35:23 +03:00
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
|
2016-11-04 13:34:42 +03:00
|
|
|
if ($event->getApp() !== 'files') {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2016-12-01 13:35:23 +03:00
|
|
|
$this->l = $this->languageFactory->get('files', $language);
|
2017-05-19 16:45:14 +03:00
|
|
|
$this->activityLang = $this->languageFactory->get('activity', $language);
|
2016-12-01 13:35:23 +03:00
|
|
|
|
2016-11-10 18:02:19 +03:00
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) {
|
|
|
|
try {
|
2016-11-16 22:43:28 +03:00
|
|
|
return $this->parseShortVersion($event, $previousEvent);
|
2016-11-10 18:02:19 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// Ignore and simply use the long version...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
return $this->parseLongVersion($event, $previousEvent);
|
2016-11-10 18:02:19 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:36:57 +03:00
|
|
|
protected function setIcon(IEvent $event, string $icon, string $app = 'files') {
|
2018-02-10 19:45:04 +03:00
|
|
|
if ($this->activityManager->getRequirePNG()) {
|
2019-01-22 17:36:57 +03:00
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath($app, $icon . '.png')));
|
2018-02-10 19:45:04 +03:00
|
|
|
} else {
|
2019-01-22 17:36:57 +03:00
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath($app, $icon . '.svg')));
|
2018-02-10 19:45:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:02:19 +03:00
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
2016-11-16 22:43:28 +03:00
|
|
|
* @param IEvent|null $previousEvent
|
2016-11-10 18:02:19 +03:00
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-11-10 18:02:19 +03:00
|
|
|
*/
|
2016-11-16 22:43:28 +03:00
|
|
|
public function parseShortVersion(IEvent $event, IEvent $previousEvent = null) {
|
2017-02-07 13:30:23 +03:00
|
|
|
$parsedParameters = $this->getParameters($event);
|
2016-11-10 18:02:19 +03:00
|
|
|
|
|
|
|
if ($event->getSubject() === 'created_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Created by {user}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'add-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'changed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Changed by {user}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'deleted_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Deleted by {user}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'delete-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'restored_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Restored by {user}');
|
2019-01-22 17:36:57 +03:00
|
|
|
$this->setIcon($event, 'actions/history', 'core');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'renamed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Renamed by {user}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'moved_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Moved by {user}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2016-11-10 18:02:19 +03:00
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2017-05-19 16:45:14 +03:00
|
|
|
if (!isset($parsedParameters['user'])) {
|
|
|
|
// External user via public link share
|
|
|
|
$subject = str_replace('{user}', $this->activityLang->t('"remote user"'), $subject);
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
$this->setSubjects($event, $subject, $parsedParameters);
|
|
|
|
|
2017-02-07 13:30:23 +03:00
|
|
|
return $this->eventMerger->mergeEvents('user', $event, $previousEvent);
|
2016-11-10 18:02:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
2016-11-16 22:43:28 +03:00
|
|
|
* @param IEvent|null $previousEvent
|
2016-11-10 18:02:19 +03:00
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-11-10 18:02:19 +03:00
|
|
|
*/
|
2016-11-16 22:43:28 +03:00
|
|
|
public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
|
2018-02-10 20:09:04 +03:00
|
|
|
$this->fileIsEncrypted = false;
|
2017-02-07 13:30:23 +03:00
|
|
|
$parsedParameters = $this->getParameters($event);
|
2016-11-10 18:02:19 +03:00
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
if ($event->getSubject() === 'created_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You created {file}');
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$subject = $this->l->t('You created an encrypted file in {file}');
|
|
|
|
}
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'add-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'created_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} created {file}');
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$subject = $this->l->t('{user} created an encrypted file in {file}');
|
|
|
|
}
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'add-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'created_public') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{file} was created in a public folder');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'add-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'changed_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You changed {file}');
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$subject = $this->l->t('You changed an encrypted file in {file}');
|
|
|
|
}
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'changed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} changed {file}');
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$subject = $this->l->t('{user} changed an encrypted file in {file}');
|
|
|
|
}
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'deleted_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You deleted {file}');
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$subject = $this->l->t('You deleted an encrypted file in {file}');
|
|
|
|
}
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'delete-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'deleted_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} deleted {file}');
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$subject = $this->l->t('{user} deleted an encrypted file in {file}');
|
|
|
|
}
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'delete-color');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'restored_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You restored {file}');
|
2019-01-22 17:36:57 +03:00
|
|
|
$this->setIcon($event, 'actions/history', 'core');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'restored_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} restored {file}');
|
2019-01-22 17:36:57 +03:00
|
|
|
$this->setIcon($event, 'actions/history', 'core');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'renamed_self') {
|
2021-03-04 15:14:52 +03:00
|
|
|
$oldFileName = $parsedParameters['oldfile']['name'];
|
|
|
|
$newFileName = $parsedParameters['newfile']['name'];
|
|
|
|
|
|
|
|
if ($this->isHiddenFile($oldFileName)) {
|
|
|
|
if ($this->isHiddenFile($newFileName)) {
|
|
|
|
$subject = $this->l->t('You renamed {oldfile} (hidden) to {newfile} (hidden)');
|
|
|
|
} else {
|
|
|
|
$subject = $this->l->t('You renamed {oldfile} (hidden) to {newfile}');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->isHiddenFile($newFileName)) {
|
|
|
|
$subject = $this->l->t('You renamed {oldfile} to {newfile} (hidden)');
|
|
|
|
} else {
|
|
|
|
$subject = $this->l->t('You renamed {oldfile} to {newfile}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'renamed_by') {
|
2021-03-04 15:14:52 +03:00
|
|
|
$oldFileName = $parsedParameters['oldfile']['name'];
|
|
|
|
$newFileName = $parsedParameters['newfile']['name'];
|
|
|
|
|
|
|
|
if ($this->isHiddenFile($oldFileName)) {
|
|
|
|
if ($this->isHiddenFile($newFileName)) {
|
|
|
|
$subject = $this->l->t('{user} renamed {oldfile} (hidden) to {newfile} (hidden)');
|
|
|
|
} else {
|
|
|
|
$subject = $this->l->t('{user} renamed {oldfile} (hidden) to {newfile}');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->isHiddenFile($newFileName)) {
|
|
|
|
$subject = $this->l->t('{user} renamed {oldfile} to {newfile} (hidden)');
|
|
|
|
} else {
|
|
|
|
$subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'moved_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You moved {oldfile} to {newfile}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === 'moved_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} moved {oldfile} to {newfile}');
|
2018-02-10 19:45:04 +03:00
|
|
|
$this->setIcon($event, 'change');
|
2016-11-04 13:34:42 +03:00
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($this->fileIsEncrypted) {
|
|
|
|
$event->setSubject($event->getSubject() . '_enc', $event->getSubjectParameters());
|
|
|
|
}
|
|
|
|
|
2017-05-19 16:45:14 +03:00
|
|
|
if (!isset($parsedParameters['user'])) {
|
|
|
|
// External user via public link share
|
|
|
|
$subject = str_replace('{user}', $this->activityLang->t('"remote user"'), $subject);
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
$this->setSubjects($event, $subject, $parsedParameters);
|
|
|
|
|
2020-03-10 11:32:10 +03:00
|
|
|
if ($event->getSubject() === 'moved_self' || $event->getSubject() === 'moved_by') {
|
|
|
|
$event = $this->eventMerger->mergeEvents('oldfile', $event, $previousEvent);
|
|
|
|
} else {
|
|
|
|
$event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
|
|
|
|
}
|
2016-11-25 17:21:16 +03:00
|
|
|
|
|
|
|
if ($event->getChildEvent() === null) {
|
|
|
|
// Couldn't group by file, maybe we can group by user
|
|
|
|
$event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:14:52 +03:00
|
|
|
private function isHiddenFile(string $filename): bool {
|
|
|
|
return strlen($filename) > 0 && $filename[0] === '.';
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
protected function setSubjects(IEvent $event, $subject, array $parameters) {
|
|
|
|
$placeholders = $replacements = [];
|
|
|
|
foreach ($parameters as $placeholder => $parameter) {
|
|
|
|
$placeholders[] = '{' . $placeholder . '}';
|
|
|
|
if ($parameter['type'] === 'file') {
|
2016-12-15 17:53:48 +03:00
|
|
|
$replacements[] = $parameter['path'];
|
2016-11-16 22:43:28 +03:00
|
|
|
} else {
|
|
|
|
$replacements[] = $parameter['name'];
|
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|
2016-11-16 22:43:28 +03:00
|
|
|
|
|
|
|
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
|
|
|
|
->setRichSubject($subject, $parameters);
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|
|
|
|
|
2017-02-07 13:30:23 +03:00
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
|
|
|
* @return array
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function getParameters(IEvent $event) {
|
|
|
|
$parameters = $event->getSubjectParameters();
|
|
|
|
switch ($event->getSubject()) {
|
2016-11-04 13:34:42 +03:00
|
|
|
case 'created_self':
|
|
|
|
case 'created_public':
|
|
|
|
case 'changed_self':
|
|
|
|
case 'deleted_self':
|
|
|
|
case 'restored_self':
|
|
|
|
return [
|
2017-02-07 13:49:01 +03:00
|
|
|
'file' => $this->getFile($parameters[0], $event),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
case 'created_by':
|
|
|
|
case 'changed_by':
|
|
|
|
case 'deleted_by':
|
|
|
|
case 'restored_by':
|
2017-05-19 16:45:14 +03:00
|
|
|
if ($parameters[1] === '') {
|
|
|
|
// External user via public link share
|
|
|
|
return [
|
|
|
|
'file' => $this->getFile($parameters[0], $event),
|
|
|
|
];
|
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
return [
|
2017-02-07 13:49:01 +03:00
|
|
|
'file' => $this->getFile($parameters[0], $event),
|
|
|
|
'user' => $this->getUser($parameters[1]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
case 'renamed_self':
|
|
|
|
case 'moved_self':
|
|
|
|
return [
|
2017-02-07 13:49:01 +03:00
|
|
|
'newfile' => $this->getFile($parameters[0]),
|
|
|
|
'oldfile' => $this->getFile($parameters[1]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
case 'renamed_by':
|
|
|
|
case 'moved_by':
|
2017-05-19 16:45:14 +03:00
|
|
|
if ($parameters[1] === '') {
|
|
|
|
// External user via public link share
|
|
|
|
return [
|
|
|
|
'newfile' => $this->getFile($parameters[0]),
|
|
|
|
'oldfile' => $this->getFile($parameters[2]),
|
|
|
|
];
|
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
return [
|
2017-02-07 13:49:01 +03:00
|
|
|
'newfile' => $this->getFile($parameters[0]),
|
|
|
|
'user' => $this->getUser($parameters[1]),
|
|
|
|
'oldfile' => $this->getFile($parameters[2]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:30:23 +03:00
|
|
|
/**
|
2017-02-08 13:55:50 +03:00
|
|
|
* @param array|string $parameter
|
2017-02-07 13:30:23 +03:00
|
|
|
* @param IEvent|null $event
|
|
|
|
* @return array
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2017-02-07 13:49:01 +03:00
|
|
|
protected function getFile($parameter, IEvent $event = null) {
|
2017-02-07 13:30:23 +03:00
|
|
|
if (is_array($parameter)) {
|
|
|
|
$path = reset($parameter);
|
2017-02-07 13:49:01 +03:00
|
|
|
$id = (string) key($parameter);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event !== null) {
|
2017-02-07 13:30:23 +03:00
|
|
|
// Legacy from before ownCloud 8.2
|
|
|
|
$path = $parameter;
|
|
|
|
$id = $event->getObjectId();
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException('Could not generate file parameter');
|
|
|
|
}
|
2017-02-07 13:49:01 +03:00
|
|
|
|
2018-02-12 18:25:36 +03:00
|
|
|
$encryptionContainer = $this->getEndToEndEncryptionContainer($id, $path);
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($encryptionContainer instanceof Folder) {
|
|
|
|
$this->fileIsEncrypted = true;
|
2018-02-12 18:05:42 +03:00
|
|
|
try {
|
|
|
|
$fullPath = rtrim($encryptionContainer->getPath(), '/');
|
|
|
|
// Remove /user/files/...
|
2021-01-12 12:15:48 +03:00
|
|
|
[,,, $path] = explode('/', $fullPath, 4);
|
2018-02-12 18:05:42 +03:00
|
|
|
if (!$path) {
|
|
|
|
throw new InvalidPathException('Path could not be split correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'type' => 'file',
|
|
|
|
'id' => $encryptionContainer->getId(),
|
|
|
|
'name' => $encryptionContainer->getName(),
|
|
|
|
'path' => $path,
|
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $encryptionContainer->getId()]),
|
|
|
|
];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// fall back to the normal one
|
|
|
|
$this->fileIsEncrypted = false;
|
|
|
|
}
|
2018-02-10 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
return [
|
|
|
|
'type' => 'file',
|
|
|
|
'id' => $id,
|
|
|
|
'name' => basename($path),
|
2016-12-15 17:53:48 +03:00
|
|
|
'path' => trim($path, '/'),
|
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:09:04 +03:00
|
|
|
protected $fileEncrypted = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a file is end2end encrypted
|
|
|
|
* @param int $fileId
|
2018-02-12 18:25:36 +03:00
|
|
|
* @param string $path
|
|
|
|
* @return Folder|null
|
2018-02-10 20:09:04 +03:00
|
|
|
*/
|
2018-02-12 18:25:36 +03:00
|
|
|
protected function getEndToEndEncryptionContainer($fileId, $path) {
|
2018-02-10 20:09:04 +03:00
|
|
|
if (isset($this->fileEncrypted[$fileId])) {
|
|
|
|
return $this->fileEncrypted[$fileId];
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:25:36 +03:00
|
|
|
$fileName = basename($path);
|
2018-02-10 20:09:04 +03:00
|
|
|
if (!preg_match('/^[0-9a-fA-F]{32}$/', $fileName)) {
|
|
|
|
$this->fileEncrypted[$fileId] = false;
|
|
|
|
return $this->fileEncrypted[$fileId];
|
|
|
|
}
|
|
|
|
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId());
|
|
|
|
$files = $userFolder->getById($fileId);
|
|
|
|
if (empty($files)) {
|
2018-02-28 17:40:01 +03:00
|
|
|
try {
|
|
|
|
// Deleted, try with parent
|
|
|
|
$file = $this->findExistingParent($userFolder, dirname($path));
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:25:36 +03:00
|
|
|
if (!$file instanceof Folder || !$file->isEncrypted()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fileEncrypted[$fileId] = $file;
|
|
|
|
return $file;
|
2018-02-10 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$file = array_shift($files);
|
2018-02-12 18:25:36 +03:00
|
|
|
|
2018-02-10 20:09:04 +03:00
|
|
|
if ($file instanceof Folder && $file->isEncrypted()) {
|
|
|
|
// If the folder is encrypted, it is the Container,
|
|
|
|
// but can be the name is just fine.
|
|
|
|
$this->fileEncrypted[$fileId] = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fileEncrypted[$fileId] = $this->getParentEndToEndEncryptionContainer($userFolder, $file);
|
|
|
|
return $this->fileEncrypted[$fileId];
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:40:01 +03:00
|
|
|
/**
|
|
|
|
* @param Folder $userFolder
|
|
|
|
* @param string $path
|
|
|
|
* @return Folder
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function findExistingParent(Folder $userFolder, $path) {
|
|
|
|
if ($path === '/') {
|
|
|
|
throw new NotFoundException('Reached the root');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$folder = $userFolder->get(dirname($path));
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return $this->findExistingParent($userFolder, dirname($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $folder;
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:09:04 +03:00
|
|
|
/**
|
|
|
|
* Check all parents until the user's root folder if one is encrypted
|
|
|
|
*
|
|
|
|
* @param Folder $userFolder
|
|
|
|
* @param Node $file
|
|
|
|
* @return Node|null
|
|
|
|
*/
|
|
|
|
protected function getParentEndToEndEncryptionContainer(Folder $userFolder, Node $file) {
|
|
|
|
try {
|
|
|
|
$parent = $file->getParent();
|
|
|
|
|
|
|
|
if ($userFolder->getId() === $parent->getId()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent->isEncrypted()) {
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:16:18 +03:00
|
|
|
return $this->getParentEndToEndEncryptionContainer($userFolder, $parent);
|
2018-02-10 20:09:04 +03:00
|
|
|
}
|
|
|
|
|
2017-02-08 13:55:50 +03:00
|
|
|
/**
|
|
|
|
* @param string $uid
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-07 13:49:01 +03:00
|
|
|
protected function getUser($uid) {
|
2020-01-28 14:51:43 +03:00
|
|
|
// First try local user
|
|
|
|
$user = $this->userManager->get($uid);
|
|
|
|
if ($user instanceof IUser) {
|
|
|
|
return [
|
|
|
|
'type' => 'user',
|
|
|
|
'id' => $user->getUID(),
|
|
|
|
'name' => $user->getDisplayName(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then a contact from the addressbook
|
|
|
|
if ($this->cloudIdManager->isValidCloudId($uid)) {
|
|
|
|
$cloudId = $this->cloudIdManager->resolveCloudId($uid);
|
|
|
|
return [
|
|
|
|
'type' => 'user',
|
|
|
|
'id' => $cloudId->getUser(),
|
|
|
|
'name' => $this->getDisplayNameFromAddressBook($cloudId->getDisplayId()),
|
|
|
|
'server' => $cloudId->getRemote(),
|
|
|
|
];
|
2016-11-22 18:39:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
// Fallback to empty dummy data
|
2016-11-04 13:34:42 +03:00
|
|
|
return [
|
|
|
|
'type' => 'user',
|
2016-11-22 18:39:29 +03:00
|
|
|
'id' => $uid,
|
2020-01-28 14:51:43 +03:00
|
|
|
'name' => $uid,
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
}
|
2016-11-22 18:39:29 +03:00
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
protected function getDisplayNameFromAddressBook(string $search): string {
|
|
|
|
if (isset($this->displayNames[$search])) {
|
|
|
|
return $this->displayNames[$search];
|
2016-11-22 18:39:29 +03:00
|
|
|
}
|
2020-01-28 14:27:47 +03:00
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD']);
|
2020-01-28 14:27:47 +03:00
|
|
|
foreach ($addressBookContacts as $contact) {
|
|
|
|
if (isset($contact['isLocalSystemBook'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($contact['CLOUD'])) {
|
|
|
|
$cloudIds = $contact['CLOUD'];
|
|
|
|
if (is_string($cloudIds)) {
|
|
|
|
$cloudIds = [$cloudIds];
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
$lowerSearch = strtolower($search);
|
2020-01-28 14:27:47 +03:00
|
|
|
foreach ($cloudIds as $cloudId) {
|
|
|
|
if (strtolower($cloudId) === $lowerSearch) {
|
2020-01-28 14:51:43 +03:00
|
|
|
$this->displayNames[$search] = $contact['FN'] . " ($cloudId)";
|
|
|
|
return $this->displayNames[$search];
|
2020-01-28 14:27:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:51:43 +03:00
|
|
|
return $search;
|
2016-11-22 18:39:29 +03:00
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|