Provider.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files\Activity;
  7. use OCP\Activity\Exceptions\UnknownActivityException;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IEventMerger;
  10. use OCP\Activity\IManager;
  11. use OCP\Activity\IProvider;
  12. use OCP\Contacts\IManager as IContactsManager;
  13. use OCP\Federation\ICloudIdManager;
  14. use OCP\Files\Folder;
  15. use OCP\Files\InvalidPathException;
  16. use OCP\Files\IRootFolder;
  17. use OCP\Files\Node;
  18. use OCP\Files\NotFoundException;
  19. use OCP\IL10N;
  20. use OCP\IURLGenerator;
  21. use OCP\IUserManager;
  22. use OCP\L10N\IFactory;
  23. class Provider implements IProvider {
  24. /** @var IFactory */
  25. protected $languageFactory;
  26. /** @var IL10N */
  27. protected $l;
  28. /** @var IL10N */
  29. protected $activityLang;
  30. /** @var IURLGenerator */
  31. protected $url;
  32. /** @var IManager */
  33. protected $activityManager;
  34. /** @var IUserManager */
  35. protected $userManager;
  36. /** @var IRootFolder */
  37. protected $rootFolder;
  38. /** @var IEventMerger */
  39. protected $eventMerger;
  40. /** @var ICloudIdManager */
  41. protected $cloudIdManager;
  42. /** @var IContactsManager */
  43. protected $contactsManager;
  44. /** @var string[] cached displayNames - key is the cloud id and value the displayname */
  45. protected $displayNames = [];
  46. protected $fileIsEncrypted = false;
  47. public function __construct(IFactory $languageFactory,
  48. IURLGenerator $url,
  49. IManager $activityManager,
  50. IUserManager $userManager,
  51. IRootFolder $rootFolder,
  52. ICloudIdManager $cloudIdManager,
  53. IContactsManager $contactsManager,
  54. IEventMerger $eventMerger) {
  55. $this->languageFactory = $languageFactory;
  56. $this->url = $url;
  57. $this->activityManager = $activityManager;
  58. $this->userManager = $userManager;
  59. $this->rootFolder = $rootFolder;
  60. $this->cloudIdManager = $cloudIdManager;
  61. $this->contactsManager = $contactsManager;
  62. $this->eventMerger = $eventMerger;
  63. }
  64. /**
  65. * @param string $language
  66. * @param IEvent $event
  67. * @param IEvent|null $previousEvent
  68. * @return IEvent
  69. * @throws UnknownActivityException
  70. * @since 11.0.0
  71. */
  72. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  73. if ($event->getApp() !== 'files') {
  74. throw new UnknownActivityException();
  75. }
  76. $this->l = $this->languageFactory->get('files', $language);
  77. $this->activityLang = $this->languageFactory->get('activity', $language);
  78. if ($this->activityManager->isFormattingFilteredObject()) {
  79. try {
  80. return $this->parseShortVersion($event, $previousEvent);
  81. } catch (UnknownActivityException) {
  82. // Ignore and simply use the long version...
  83. }
  84. }
  85. return $this->parseLongVersion($event, $previousEvent);
  86. }
  87. protected function setIcon(IEvent $event, string $icon, string $app = 'files') {
  88. if ($this->activityManager->getRequirePNG()) {
  89. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath($app, $icon . '.png')));
  90. } else {
  91. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath($app, $icon . '.svg')));
  92. }
  93. }
  94. /**
  95. * @param IEvent $event
  96. * @param IEvent|null $previousEvent
  97. * @return IEvent
  98. * @throws UnknownActivityException
  99. * @since 11.0.0
  100. */
  101. public function parseShortVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent {
  102. $parsedParameters = $this->getParameters($event);
  103. if ($event->getSubject() === 'created_by') {
  104. $subject = $this->l->t('Created by {user}');
  105. $this->setIcon($event, 'add-color');
  106. } elseif ($event->getSubject() === 'changed_by') {
  107. $subject = $this->l->t('Changed by {user}');
  108. $this->setIcon($event, 'change');
  109. } elseif ($event->getSubject() === 'deleted_by') {
  110. $subject = $this->l->t('Deleted by {user}');
  111. $this->setIcon($event, 'delete-color');
  112. } elseif ($event->getSubject() === 'restored_by') {
  113. $subject = $this->l->t('Restored by {user}');
  114. $this->setIcon($event, 'actions/history', 'core');
  115. } elseif ($event->getSubject() === 'renamed_by') {
  116. $subject = $this->l->t('Renamed by {user}');
  117. $this->setIcon($event, 'change');
  118. } elseif ($event->getSubject() === 'moved_by') {
  119. $subject = $this->l->t('Moved by {user}');
  120. $this->setIcon($event, 'change');
  121. } else {
  122. throw new UnknownActivityException();
  123. }
  124. if (!isset($parsedParameters['user'])) {
  125. // External user via public link share
  126. $subject = str_replace('{user}', $this->activityLang->t('"remote account"'), $subject);
  127. }
  128. $this->setSubjects($event, $subject, $parsedParameters);
  129. return $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  130. }
  131. /**
  132. * @param IEvent $event
  133. * @param IEvent|null $previousEvent
  134. * @return IEvent
  135. * @throws UnknownActivityException
  136. * @since 11.0.0
  137. */
  138. public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent {
  139. $this->fileIsEncrypted = false;
  140. $parsedParameters = $this->getParameters($event);
  141. if ($event->getSubject() === 'created_self') {
  142. $subject = $this->l->t('You created {file}');
  143. if ($this->fileIsEncrypted) {
  144. $subject = $this->l->t('You created an encrypted file in {file}');
  145. }
  146. $this->setIcon($event, 'add-color');
  147. } elseif ($event->getSubject() === 'created_by') {
  148. $subject = $this->l->t('{user} created {file}');
  149. if ($this->fileIsEncrypted) {
  150. $subject = $this->l->t('{user} created an encrypted file in {file}');
  151. }
  152. $this->setIcon($event, 'add-color');
  153. } elseif ($event->getSubject() === 'created_public') {
  154. $subject = $this->l->t('{file} was created in a public folder');
  155. $this->setIcon($event, 'add-color');
  156. } elseif ($event->getSubject() === 'changed_self') {
  157. $subject = $this->l->t('You changed {file}');
  158. if ($this->fileIsEncrypted) {
  159. $subject = $this->l->t('You changed an encrypted file in {file}');
  160. }
  161. $this->setIcon($event, 'change');
  162. } elseif ($event->getSubject() === 'changed_by') {
  163. $subject = $this->l->t('{user} changed {file}');
  164. if ($this->fileIsEncrypted) {
  165. $subject = $this->l->t('{user} changed an encrypted file in {file}');
  166. }
  167. $this->setIcon($event, 'change');
  168. } elseif ($event->getSubject() === 'deleted_self') {
  169. $subject = $this->l->t('You deleted {file}');
  170. if ($this->fileIsEncrypted) {
  171. $subject = $this->l->t('You deleted an encrypted file in {file}');
  172. }
  173. $this->setIcon($event, 'delete-color');
  174. } elseif ($event->getSubject() === 'deleted_by') {
  175. $subject = $this->l->t('{user} deleted {file}');
  176. if ($this->fileIsEncrypted) {
  177. $subject = $this->l->t('{user} deleted an encrypted file in {file}');
  178. }
  179. $this->setIcon($event, 'delete-color');
  180. } elseif ($event->getSubject() === 'restored_self') {
  181. $subject = $this->l->t('You restored {file}');
  182. $this->setIcon($event, 'actions/history', 'core');
  183. } elseif ($event->getSubject() === 'restored_by') {
  184. $subject = $this->l->t('{user} restored {file}');
  185. $this->setIcon($event, 'actions/history', 'core');
  186. } elseif ($event->getSubject() === 'renamed_self') {
  187. $oldFileName = $parsedParameters['oldfile']['name'];
  188. $newFileName = $parsedParameters['newfile']['name'];
  189. if ($this->isHiddenFile($oldFileName)) {
  190. if ($this->isHiddenFile($newFileName)) {
  191. $subject = $this->l->t('You renamed {oldfile} (hidden) to {newfile} (hidden)');
  192. } else {
  193. $subject = $this->l->t('You renamed {oldfile} (hidden) to {newfile}');
  194. }
  195. } else {
  196. if ($this->isHiddenFile($newFileName)) {
  197. $subject = $this->l->t('You renamed {oldfile} to {newfile} (hidden)');
  198. } else {
  199. $subject = $this->l->t('You renamed {oldfile} to {newfile}');
  200. }
  201. }
  202. $this->setIcon($event, 'change');
  203. } elseif ($event->getSubject() === 'renamed_by') {
  204. $oldFileName = $parsedParameters['oldfile']['name'];
  205. $newFileName = $parsedParameters['newfile']['name'];
  206. if ($this->isHiddenFile($oldFileName)) {
  207. if ($this->isHiddenFile($newFileName)) {
  208. $subject = $this->l->t('{user} renamed {oldfile} (hidden) to {newfile} (hidden)');
  209. } else {
  210. $subject = $this->l->t('{user} renamed {oldfile} (hidden) to {newfile}');
  211. }
  212. } else {
  213. if ($this->isHiddenFile($newFileName)) {
  214. $subject = $this->l->t('{user} renamed {oldfile} to {newfile} (hidden)');
  215. } else {
  216. $subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
  217. }
  218. }
  219. $this->setIcon($event, 'change');
  220. } elseif ($event->getSubject() === 'moved_self') {
  221. $subject = $this->l->t('You moved {oldfile} to {newfile}');
  222. $this->setIcon($event, 'change');
  223. } elseif ($event->getSubject() === 'moved_by') {
  224. $subject = $this->l->t('{user} moved {oldfile} to {newfile}');
  225. $this->setIcon($event, 'change');
  226. } else {
  227. throw new UnknownActivityException();
  228. }
  229. if ($this->fileIsEncrypted) {
  230. $event->setSubject($event->getSubject() . '_enc', $event->getSubjectParameters());
  231. }
  232. if (!isset($parsedParameters['user'])) {
  233. // External user via public link share
  234. $subject = str_replace('{user}', $this->activityLang->t('"remote account"'), $subject);
  235. }
  236. $this->setSubjects($event, $subject, $parsedParameters);
  237. if ($event->getSubject() === 'moved_self' || $event->getSubject() === 'moved_by') {
  238. $event = $this->eventMerger->mergeEvents('oldfile', $event, $previousEvent);
  239. } else {
  240. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  241. }
  242. if ($event->getChildEvent() === null) {
  243. // Couldn't group by file, maybe we can group by user
  244. $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  245. }
  246. return $event;
  247. }
  248. private function isHiddenFile(string $filename): bool {
  249. return strlen($filename) > 0 && $filename[0] === '.';
  250. }
  251. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  252. $event->setRichSubject($subject, $parameters);
  253. }
  254. /**
  255. * @param IEvent $event
  256. * @return array
  257. * @throws UnknownActivityException
  258. */
  259. protected function getParameters(IEvent $event): array {
  260. $parameters = $event->getSubjectParameters();
  261. switch ($event->getSubject()) {
  262. case 'created_self':
  263. case 'created_public':
  264. case 'changed_self':
  265. case 'deleted_self':
  266. case 'restored_self':
  267. return [
  268. 'file' => $this->getFile($parameters[0], $event),
  269. ];
  270. case 'created_by':
  271. case 'changed_by':
  272. case 'deleted_by':
  273. case 'restored_by':
  274. if ($parameters[1] === '') {
  275. // External user via public link share
  276. return [
  277. 'file' => $this->getFile($parameters[0], $event),
  278. ];
  279. }
  280. return [
  281. 'file' => $this->getFile($parameters[0], $event),
  282. 'user' => $this->getUser($parameters[1]),
  283. ];
  284. case 'renamed_self':
  285. case 'moved_self':
  286. return [
  287. 'newfile' => $this->getFile($parameters[0]),
  288. 'oldfile' => $this->getFile($parameters[1]),
  289. ];
  290. case 'renamed_by':
  291. case 'moved_by':
  292. if ($parameters[1] === '') {
  293. // External user via public link share
  294. return [
  295. 'newfile' => $this->getFile($parameters[0]),
  296. 'oldfile' => $this->getFile($parameters[2]),
  297. ];
  298. }
  299. return [
  300. 'newfile' => $this->getFile($parameters[0]),
  301. 'user' => $this->getUser($parameters[1]),
  302. 'oldfile' => $this->getFile($parameters[2]),
  303. ];
  304. }
  305. return [];
  306. }
  307. /**
  308. * @param array|string $parameter
  309. * @param IEvent|null $event
  310. * @return array
  311. * @throws UnknownActivityException
  312. */
  313. protected function getFile($parameter, ?IEvent $event = null): array {
  314. if (is_array($parameter)) {
  315. $path = reset($parameter);
  316. $id = (string)key($parameter);
  317. } elseif ($event !== null) {
  318. // Legacy from before ownCloud 8.2
  319. $path = $parameter;
  320. $id = $event->getObjectId();
  321. } else {
  322. throw new UnknownActivityException('Could not generate file parameter');
  323. }
  324. $encryptionContainer = $this->getEndToEndEncryptionContainer($id, $path);
  325. if ($encryptionContainer instanceof Folder) {
  326. $this->fileIsEncrypted = true;
  327. try {
  328. $fullPath = rtrim($encryptionContainer->getPath(), '/');
  329. // Remove /user/files/...
  330. [,,, $path] = explode('/', $fullPath, 4);
  331. if (!$path) {
  332. throw new InvalidPathException('Path could not be split correctly');
  333. }
  334. return [
  335. 'type' => 'file',
  336. 'id' => $encryptionContainer->getId(),
  337. 'name' => $encryptionContainer->getName(),
  338. 'path' => $path,
  339. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $encryptionContainer->getId()]),
  340. ];
  341. } catch (\Exception $e) {
  342. // fall back to the normal one
  343. $this->fileIsEncrypted = false;
  344. }
  345. }
  346. return [
  347. 'type' => 'file',
  348. 'id' => $id,
  349. 'name' => basename($path),
  350. 'path' => trim($path, '/'),
  351. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  352. ];
  353. }
  354. protected $fileEncrypted = [];
  355. /**
  356. * Check if a file is end2end encrypted
  357. * @param int $fileId
  358. * @param string $path
  359. * @return Folder|null
  360. */
  361. protected function getEndToEndEncryptionContainer($fileId, $path) {
  362. if (isset($this->fileEncrypted[$fileId])) {
  363. return $this->fileEncrypted[$fileId];
  364. }
  365. $fileName = basename($path);
  366. if (!preg_match('/^[0-9a-fA-F]{32}$/', $fileName)) {
  367. $this->fileEncrypted[$fileId] = false;
  368. return $this->fileEncrypted[$fileId];
  369. }
  370. $userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId());
  371. $file = $userFolder->getFirstNodeById($fileId);
  372. if (!$file) {
  373. try {
  374. // Deleted, try with parent
  375. $file = $this->findExistingParent($userFolder, dirname($path));
  376. } catch (NotFoundException $e) {
  377. return null;
  378. }
  379. if (!$file instanceof Folder || !$file->isEncrypted()) {
  380. return null;
  381. }
  382. $this->fileEncrypted[$fileId] = $file;
  383. return $file;
  384. }
  385. if ($file instanceof Folder && $file->isEncrypted()) {
  386. // If the folder is encrypted, it is the Container,
  387. // but can be the name is just fine.
  388. $this->fileEncrypted[$fileId] = true;
  389. return null;
  390. }
  391. $this->fileEncrypted[$fileId] = $this->getParentEndToEndEncryptionContainer($userFolder, $file);
  392. return $this->fileEncrypted[$fileId];
  393. }
  394. /**
  395. * @param Folder $userFolder
  396. * @param string $path
  397. * @return Folder
  398. * @throws NotFoundException
  399. */
  400. protected function findExistingParent(Folder $userFolder, $path) {
  401. if ($path === '/') {
  402. throw new NotFoundException('Reached the root');
  403. }
  404. try {
  405. $folder = $userFolder->get(dirname($path));
  406. } catch (NotFoundException $e) {
  407. return $this->findExistingParent($userFolder, dirname($path));
  408. }
  409. return $folder;
  410. }
  411. /**
  412. * Check all parents until the user's root folder if one is encrypted
  413. *
  414. * @param Folder $userFolder
  415. * @param Node $file
  416. * @return Node|null
  417. */
  418. protected function getParentEndToEndEncryptionContainer(Folder $userFolder, Node $file) {
  419. try {
  420. $parent = $file->getParent();
  421. if ($userFolder->getId() === $parent->getId()) {
  422. return null;
  423. }
  424. } catch (\Exception $e) {
  425. return null;
  426. }
  427. if ($parent->isEncrypted()) {
  428. return $parent;
  429. }
  430. return $this->getParentEndToEndEncryptionContainer($userFolder, $parent);
  431. }
  432. /**
  433. * @param string $uid
  434. * @return array
  435. */
  436. protected function getUser($uid) {
  437. // First try local user
  438. $displayName = $this->userManager->getDisplayName($uid);
  439. if ($displayName !== null) {
  440. return [
  441. 'type' => 'user',
  442. 'id' => $uid,
  443. 'name' => $displayName,
  444. ];
  445. }
  446. // Then a contact from the addressbook
  447. if ($this->cloudIdManager->isValidCloudId($uid)) {
  448. $cloudId = $this->cloudIdManager->resolveCloudId($uid);
  449. return [
  450. 'type' => 'user',
  451. 'id' => $cloudId->getUser(),
  452. 'name' => $this->getDisplayNameFromAddressBook($cloudId->getDisplayId()),
  453. 'server' => $cloudId->getRemote(),
  454. ];
  455. }
  456. // Fallback to empty dummy data
  457. return [
  458. 'type' => 'user',
  459. 'id' => $uid,
  460. 'name' => $uid,
  461. ];
  462. }
  463. protected function getDisplayNameFromAddressBook(string $search): string {
  464. if (isset($this->displayNames[$search])) {
  465. return $this->displayNames[$search];
  466. }
  467. $addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
  468. 'limit' => 1,
  469. 'enumeration' => false,
  470. 'fullmatch' => false,
  471. 'strict_search' => true,
  472. ]);
  473. foreach ($addressBookContacts as $contact) {
  474. if (isset($contact['isLocalSystemBook'])) {
  475. continue;
  476. }
  477. if (isset($contact['CLOUD'])) {
  478. $cloudIds = $contact['CLOUD'];
  479. if (is_string($cloudIds)) {
  480. $cloudIds = [$cloudIds];
  481. }
  482. $lowerSearch = strtolower($search);
  483. foreach ($cloudIds as $cloudId) {
  484. if (strtolower($cloudId) === $lowerSearch) {
  485. $this->displayNames[$search] = $contact['FN'] . " ($cloudId)";
  486. return $this->displayNames[$search];
  487. }
  488. }
  489. }
  490. }
  491. return $search;
  492. }
  493. }