hooks.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Björn Schießle <schiessle@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. * @author Sam Tuke <mail@samtuke.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. /**
  28. * This class contains all hooks.
  29. */
  30. namespace OCA\Files_Versions;
  31. class Hooks {
  32. public static function connectHooks() {
  33. // Listen to write signals
  34. \OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook');
  35. // Listen to delete and rename signals
  36. \OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook');
  37. \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook');
  38. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook');
  39. \OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook');
  40. \OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
  41. \OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
  42. }
  43. /**
  44. * listen to write event.
  45. */
  46. public static function write_hook( $params ) {
  47. if (\OCP\App::isEnabled('files_versions')) {
  48. $path = $params[\OC\Files\Filesystem::signal_param_path];
  49. if($path<>'') {
  50. Storage::store($path);
  51. }
  52. }
  53. }
  54. /**
  55. * Erase versions of deleted file
  56. * @param array $params
  57. *
  58. * This function is connected to the delete signal of OC_Filesystem
  59. * cleanup the versions directory if the actual file gets deleted
  60. */
  61. public static function remove_hook($params) {
  62. if (\OCP\App::isEnabled('files_versions')) {
  63. $path = $params[\OC\Files\Filesystem::signal_param_path];
  64. if($path<>'') {
  65. Storage::delete($path);
  66. }
  67. }
  68. }
  69. /**
  70. * mark file as "deleted" so that we can clean up the versions if the file is gone
  71. * @param array $params
  72. */
  73. public static function pre_remove_hook($params) {
  74. $path = $params[\OC\Files\Filesystem::signal_param_path];
  75. if($path<>'') {
  76. Storage::markDeletedFile($path);
  77. }
  78. }
  79. /**
  80. * rename/move versions of renamed/moved files
  81. * @param array $params array with oldpath and newpath
  82. *
  83. * This function is connected to the rename signal of OC_Filesystem and adjust the name and location
  84. * of the stored versions along the actual file
  85. */
  86. public static function rename_hook($params) {
  87. if (\OCP\App::isEnabled('files_versions')) {
  88. $oldpath = $params['oldpath'];
  89. $newpath = $params['newpath'];
  90. if($oldpath<>'' && $newpath<>'') {
  91. Storage::renameOrCopy($oldpath, $newpath, 'rename');
  92. }
  93. }
  94. }
  95. /**
  96. * copy versions of copied files
  97. * @param array $params array with oldpath and newpath
  98. *
  99. * This function is connected to the copy signal of OC_Filesystem and copies the
  100. * the stored versions to the new location
  101. */
  102. public static function copy_hook($params) {
  103. if (\OCP\App::isEnabled('files_versions')) {
  104. $oldpath = $params['oldpath'];
  105. $newpath = $params['newpath'];
  106. if($oldpath<>'' && $newpath<>'') {
  107. Storage::renameOrCopy($oldpath, $newpath, 'copy');
  108. }
  109. }
  110. }
  111. /**
  112. * Remember owner and the owner path of the source file.
  113. * If the file already exists, then it was a upload of a existing file
  114. * over the web interface and we call Storage::store() directly
  115. *
  116. * @param array $params array with oldpath and newpath
  117. *
  118. */
  119. public static function pre_renameOrCopy_hook($params) {
  120. if (\OCP\App::isEnabled('files_versions')) {
  121. // if we rename a movable mount point, then the versions don't have
  122. // to be renamed
  123. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']);
  124. $manager = \OC\Files\Filesystem::getMountManager();
  125. $mount = $manager->find($absOldPath);
  126. $internalPath = $mount->getInternalPath($absOldPath);
  127. if ($internalPath === '' and $mount instanceof \OC\Files\Mount\MoveableMount) {
  128. return;
  129. }
  130. $view = new \OC\Files\View(\OCP\User::getUser() . '/files');
  131. if ($view->file_exists($params['newpath'])) {
  132. Storage::store($params['newpath']);
  133. } else {
  134. Storage::setSourcePathAndUser($params['oldpath']);
  135. }
  136. }
  137. }
  138. }