Provider.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files\Activity;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IEventMerger;
  28. use OCP\Activity\IManager;
  29. use OCP\Activity\IProvider;
  30. use OCP\Contacts\IManager as IContactsManager;
  31. use OCP\Federation\ICloudIdManager;
  32. use OCP\Files\Folder;
  33. use OCP\Files\InvalidPathException;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Node;
  36. use OCP\Files\NotFoundException;
  37. use OCP\IL10N;
  38. use OCP\IURLGenerator;
  39. use OCP\IUser;
  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. $subject = $this->l->t('You renamed {oldfile} to {newfile}');
  207. $this->setIcon($event, 'change');
  208. } elseif ($event->getSubject() === 'renamed_by') {
  209. $subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
  210. $this->setIcon($event, 'change');
  211. } elseif ($event->getSubject() === 'moved_self') {
  212. $subject = $this->l->t('You moved {oldfile} to {newfile}');
  213. $this->setIcon($event, 'change');
  214. } elseif ($event->getSubject() === 'moved_by') {
  215. $subject = $this->l->t('{user} moved {oldfile} to {newfile}');
  216. $this->setIcon($event, 'change');
  217. } else {
  218. throw new \InvalidArgumentException();
  219. }
  220. if ($this->fileIsEncrypted) {
  221. $event->setSubject($event->getSubject() . '_enc', $event->getSubjectParameters());
  222. }
  223. if (!isset($parsedParameters['user'])) {
  224. // External user via public link share
  225. $subject = str_replace('{user}', $this->activityLang->t('"remote user"'), $subject);
  226. }
  227. $this->setSubjects($event, $subject, $parsedParameters);
  228. if ($event->getSubject() === 'moved_self' || $event->getSubject() === 'moved_by') {
  229. $event = $this->eventMerger->mergeEvents('oldfile', $event, $previousEvent);
  230. } else {
  231. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  232. }
  233. if ($event->getChildEvent() === null) {
  234. // Couldn't group by file, maybe we can group by user
  235. $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  236. }
  237. return $event;
  238. }
  239. protected function setSubjects(IEvent $event, $subject, array $parameters) {
  240. $placeholders = $replacements = [];
  241. foreach ($parameters as $placeholder => $parameter) {
  242. $placeholders[] = '{' . $placeholder . '}';
  243. if ($parameter['type'] === 'file') {
  244. $replacements[] = $parameter['path'];
  245. } else {
  246. $replacements[] = $parameter['name'];
  247. }
  248. }
  249. $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
  250. ->setRichSubject($subject, $parameters);
  251. }
  252. /**
  253. * @param IEvent $event
  254. * @return array
  255. * @throws \InvalidArgumentException
  256. */
  257. protected function getParameters(IEvent $event) {
  258. $parameters = $event->getSubjectParameters();
  259. switch ($event->getSubject()) {
  260. case 'created_self':
  261. case 'created_public':
  262. case 'changed_self':
  263. case 'deleted_self':
  264. case 'restored_self':
  265. return [
  266. 'file' => $this->getFile($parameters[0], $event),
  267. ];
  268. case 'created_by':
  269. case 'changed_by':
  270. case 'deleted_by':
  271. case 'restored_by':
  272. if ($parameters[1] === '') {
  273. // External user via public link share
  274. return [
  275. 'file' => $this->getFile($parameters[0], $event),
  276. ];
  277. }
  278. return [
  279. 'file' => $this->getFile($parameters[0], $event),
  280. 'user' => $this->getUser($parameters[1]),
  281. ];
  282. case 'renamed_self':
  283. case 'moved_self':
  284. return [
  285. 'newfile' => $this->getFile($parameters[0]),
  286. 'oldfile' => $this->getFile($parameters[1]),
  287. ];
  288. case 'renamed_by':
  289. case 'moved_by':
  290. if ($parameters[1] === '') {
  291. // External user via public link share
  292. return [
  293. 'newfile' => $this->getFile($parameters[0]),
  294. 'oldfile' => $this->getFile($parameters[2]),
  295. ];
  296. }
  297. return [
  298. 'newfile' => $this->getFile($parameters[0]),
  299. 'user' => $this->getUser($parameters[1]),
  300. 'oldfile' => $this->getFile($parameters[2]),
  301. ];
  302. }
  303. return [];
  304. }
  305. /**
  306. * @param array|string $parameter
  307. * @param IEvent|null $event
  308. * @return array
  309. * @throws \InvalidArgumentException
  310. */
  311. protected function getFile($parameter, IEvent $event = null) {
  312. if (is_array($parameter)) {
  313. $path = reset($parameter);
  314. $id = (string) key($parameter);
  315. } elseif ($event !== null) {
  316. // Legacy from before ownCloud 8.2
  317. $path = $parameter;
  318. $id = $event->getObjectId();
  319. } else {
  320. throw new \InvalidArgumentException('Could not generate file parameter');
  321. }
  322. $encryptionContainer = $this->getEndToEndEncryptionContainer($id, $path);
  323. if ($encryptionContainer instanceof Folder) {
  324. $this->fileIsEncrypted = true;
  325. try {
  326. $fullPath = rtrim($encryptionContainer->getPath(), '/');
  327. // Remove /user/files/...
  328. list(,,, $path) = explode('/', $fullPath, 4);
  329. if (!$path) {
  330. throw new InvalidPathException('Path could not be split correctly');
  331. }
  332. return [
  333. 'type' => 'file',
  334. 'id' => $encryptionContainer->getId(),
  335. 'name' => $encryptionContainer->getName(),
  336. 'path' => $path,
  337. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $encryptionContainer->getId()]),
  338. ];
  339. } catch (\Exception $e) {
  340. // fall back to the normal one
  341. $this->fileIsEncrypted = false;
  342. }
  343. }
  344. return [
  345. 'type' => 'file',
  346. 'id' => $id,
  347. 'name' => basename($path),
  348. 'path' => trim($path, '/'),
  349. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  350. ];
  351. }
  352. protected $fileEncrypted = [];
  353. /**
  354. * Check if a file is end2end encrypted
  355. * @param int $fileId
  356. * @param string $path
  357. * @return Folder|null
  358. */
  359. protected function getEndToEndEncryptionContainer($fileId, $path) {
  360. if (isset($this->fileEncrypted[$fileId])) {
  361. return $this->fileEncrypted[$fileId];
  362. }
  363. $fileName = basename($path);
  364. if (!preg_match('/^[0-9a-fA-F]{32}$/', $fileName)) {
  365. $this->fileEncrypted[$fileId] = false;
  366. return $this->fileEncrypted[$fileId];
  367. }
  368. $userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId());
  369. $files = $userFolder->getById($fileId);
  370. if (empty($files)) {
  371. try {
  372. // Deleted, try with parent
  373. $file = $this->findExistingParent($userFolder, dirname($path));
  374. } catch (NotFoundException $e) {
  375. return null;
  376. }
  377. if (!$file instanceof Folder || !$file->isEncrypted()) {
  378. return null;
  379. }
  380. $this->fileEncrypted[$fileId] = $file;
  381. return $file;
  382. }
  383. $file = array_shift($files);
  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. $user = $this->userManager->get($uid);
  438. if ($user instanceof IUser) {
  439. return [
  440. 'type' => 'user',
  441. 'id' => $user->getUID(),
  442. 'name' => $user->getDisplayName(),
  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. foreach ($addressBookContacts as $contact) {
  468. if (isset($contact['isLocalSystemBook'])) {
  469. continue;
  470. }
  471. if (isset($contact['CLOUD'])) {
  472. $cloudIds = $contact['CLOUD'];
  473. if (is_string($cloudIds)) {
  474. $cloudIds = [$cloudIds];
  475. }
  476. $lowerSearch = strtolower($search);
  477. foreach ($cloudIds as $cloudId) {
  478. if (strtolower($cloudId) === $lowerSearch) {
  479. $this->displayNames[$search] = $contact['FN'] . " ($cloudId)";
  480. return $this->displayNames[$search];
  481. }
  482. }
  483. }
  484. }
  485. return $search;
  486. }
  487. }