Files.php 6.0 KB

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