Provider.php 16 KB

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