Provider.php 17 KB

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