You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Files.php 6.8KB

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