Provider.php 15 KB

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