Files.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\AdminAudit\Actions;
  8. use OCP\Files\Events\Node\BeforeNodeReadEvent;
  9. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  10. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  11. use OCP\Files\Events\Node\NodeCopiedEvent;
  12. use OCP\Files\Events\Node\NodeCreatedEvent;
  13. use OCP\Files\Events\Node\NodeDeletedEvent;
  14. use OCP\Files\Events\Node\NodeRenamedEvent;
  15. use OCP\Files\Events\Node\NodeWrittenEvent;
  16. use OCP\Files\InvalidPathException;
  17. use OCP\Files\NotFoundException;
  18. use OCP\Preview\BeforePreviewFetchedEvent;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * Class Files logs the actions to files
  22. *
  23. * @package OCA\AdminAudit\Actions
  24. */
  25. class Files extends Action {
  26. private array $renamedNodes = [];
  27. /**
  28. * Logs file read actions
  29. *
  30. * @param BeforeNodeReadEvent $event
  31. */
  32. public function read(BeforeNodeReadEvent $event): void {
  33. try {
  34. $params = [
  35. 'id' => $event->getNode()->getId(),
  36. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  37. ];
  38. } catch (InvalidPathException|NotFoundException $e) {
  39. \OCP\Server::get(LoggerInterface::class)->error(
  40. "Exception thrown in file read: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  41. );
  42. return;
  43. }
  44. $this->log(
  45. 'File with id "%s" accessed: "%s"',
  46. $params,
  47. array_keys($params)
  48. );
  49. }
  50. /**
  51. * Logs rename actions of files
  52. *
  53. * @param BeforeNodeRenamedEvent $event
  54. */
  55. public function beforeRename(BeforeNodeRenamedEvent $event): void {
  56. try {
  57. $source = $event->getSource();
  58. $this->renamedNodes[$source->getId()] = $source;
  59. } catch (InvalidPathException|NotFoundException $e) {
  60. \OCP\Server::get(LoggerInterface::class)->error(
  61. "Exception thrown in file rename: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  62. );
  63. return;
  64. }
  65. }
  66. /**
  67. * Logs rename actions of files
  68. *
  69. * @param NodeRenamedEvent $event
  70. */
  71. public function afterRename(NodeRenamedEvent $event): void {
  72. try {
  73. $target = $event->getTarget();
  74. $originalSource = $this->renamedNodes[$target->getId()];
  75. $params = [
  76. 'newid' => $target->getId(),
  77. 'oldpath' => mb_substr($originalSource->getInternalPath(), 5),
  78. 'newpath' => mb_substr($target->getInternalPath(), 5),
  79. ];
  80. } catch (InvalidPathException|NotFoundException $e) {
  81. \OCP\Server::get(LoggerInterface::class)->error(
  82. "Exception thrown in file rename: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  83. );
  84. return;
  85. }
  86. $this->log(
  87. 'File renamed with id "%s" from "%s" to "%s"',
  88. $params,
  89. array_keys($params)
  90. );
  91. }
  92. /**
  93. * Logs creation of files
  94. *
  95. * @param NodeCreatedEvent $event
  96. */
  97. public function create(NodeCreatedEvent $event): void {
  98. try {
  99. $params = [
  100. 'id' => $event->getNode()->getId(),
  101. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  102. ];
  103. } catch (InvalidPathException|NotFoundException $e) {
  104. \OCP\Server::get(LoggerInterface::class)->error(
  105. "Exception thrown in file create: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  106. );
  107. return;
  108. }
  109. if ($params['path'] === '/' || $params['path'] === '') {
  110. return;
  111. }
  112. $this->log(
  113. 'File with id "%s" created: "%s"',
  114. $params,
  115. array_keys($params)
  116. );
  117. }
  118. /**
  119. * Logs copying of files
  120. *
  121. * @param NodeCopiedEvent $event
  122. */
  123. public function copy(NodeCopiedEvent $event): void {
  124. try {
  125. $params = [
  126. 'oldid' => $event->getSource()->getId(),
  127. 'newid' => $event->getTarget()->getId(),
  128. 'oldpath' => mb_substr($event->getSource()->getInternalPath(), 5),
  129. 'newpath' => mb_substr($event->getTarget()->getInternalPath(), 5),
  130. ];
  131. } catch (InvalidPathException|NotFoundException $e) {
  132. \OCP\Server::get(LoggerInterface::class)->error(
  133. "Exception thrown in file copy: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  134. );
  135. return;
  136. }
  137. $this->log(
  138. 'File id copied from: "%s" to "%s", path from "%s" to "%s"',
  139. $params,
  140. array_keys($params)
  141. );
  142. }
  143. /**
  144. * Logs writing of files
  145. *
  146. * @param BeforeNodeWrittenEvent $event
  147. */
  148. public function write(BeforeNodeWrittenEvent $event): void {
  149. try {
  150. $params = [
  151. 'id' => $event->getNode()->getId(),
  152. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  153. ];
  154. } catch (InvalidPathException|NotFoundException $e) {
  155. \OCP\Server::get(LoggerInterface::class)->error(
  156. "Exception thrown in file write: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  157. );
  158. return;
  159. }
  160. if ($params['path'] === '/' || $params['path'] === '') {
  161. return;
  162. }
  163. $this->log(
  164. 'File with id "%s" written to: "%s"',
  165. $params,
  166. array_keys($params)
  167. );
  168. }
  169. /**
  170. * Logs update of files
  171. *
  172. * @param NodeWrittenEvent $event
  173. */
  174. public function update(NodeWrittenEvent $event): void {
  175. try {
  176. $params = [
  177. 'id' => $event->getNode()->getId(),
  178. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  179. ];
  180. } catch (InvalidPathException|NotFoundException $e) {
  181. \OCP\Server::get(LoggerInterface::class)->error(
  182. "Exception thrown in file update: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  183. );
  184. return;
  185. }
  186. $this->log(
  187. 'File with id "%s" updated: "%s"',
  188. $params,
  189. array_keys($params)
  190. );
  191. }
  192. /**
  193. * Logs deletions of files
  194. *
  195. * @param NodeDeletedEvent $event
  196. */
  197. public function delete(NodeDeletedEvent $event): void {
  198. try {
  199. $params = [
  200. 'id' => $event->getNode()->getId(),
  201. 'path' => mb_substr($event->getNode()->getInternalPath(), 5),
  202. ];
  203. } catch (InvalidPathException|NotFoundException $e) {
  204. \OCP\Server::get(LoggerInterface::class)->error(
  205. "Exception thrown in file delete: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  206. );
  207. return;
  208. }
  209. $this->log(
  210. 'File with id "%s" deleted: "%s"',
  211. $params,
  212. array_keys($params)
  213. );
  214. }
  215. /**
  216. * Logs preview access to a file
  217. *
  218. * @param BeforePreviewFetchedEvent $event
  219. */
  220. public function preview(BeforePreviewFetchedEvent $event): void {
  221. try {
  222. $file = $event->getNode();
  223. $params = [
  224. 'id' => $file->getId(),
  225. 'width' => $event->getWidth(),
  226. 'height' => $event->getHeight(),
  227. 'crop' => $event->isCrop(),
  228. 'mode' => $event->getMode(),
  229. 'path' => mb_substr($file->getInternalPath(), 5)
  230. ];
  231. } catch (InvalidPathException|NotFoundException $e) {
  232. \OCP\Server::get(LoggerInterface::class)->error(
  233. "Exception thrown in file preview: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
  234. );
  235. return;
  236. }
  237. $this->log(
  238. 'Preview accessed: (id: "%s", width: "%s", height: "%s" crop: "%s", mode: "%s", path: "%s")',
  239. $params,
  240. array_keys($params)
  241. );
  242. }
  243. }