Update.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Encryption;
  25. use OC\Files\Filesystem;
  26. use \OC\Files\Mount;
  27. use \OC\Files\View;
  28. /**
  29. * update encrypted files, e.g. because a file was shared
  30. */
  31. class Update {
  32. /** @var \OC\Files\View */
  33. protected $view;
  34. /** @var \OC\Encryption\Util */
  35. protected $util;
  36. /** @var \OC\Files\Mount\Manager */
  37. protected $mountManager;
  38. /** @var \OC\Encryption\Manager */
  39. protected $encryptionManager;
  40. /** @var string */
  41. protected $uid;
  42. /** @var \OC\Encryption\File */
  43. protected $file;
  44. /**
  45. *
  46. * @param \OC\Files\View $view
  47. * @param \OC\Encryption\Util $util
  48. * @param \OC\Files\Mount\Manager $mountManager
  49. * @param \OC\Encryption\Manager $encryptionManager
  50. * @param \OC\Encryption\File $file
  51. * @param string $uid
  52. */
  53. public function __construct(
  54. View $view,
  55. Util $util,
  56. Mount\Manager $mountManager,
  57. Manager $encryptionManager,
  58. File $file,
  59. $uid
  60. ) {
  61. $this->view = $view;
  62. $this->util = $util;
  63. $this->mountManager = $mountManager;
  64. $this->encryptionManager = $encryptionManager;
  65. $this->file = $file;
  66. $this->uid = $uid;
  67. }
  68. /**
  69. * hook after file was shared
  70. *
  71. * @param array $params
  72. */
  73. public function postShared($params) {
  74. if ($this->encryptionManager->isEnabled()) {
  75. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  76. $path = Filesystem::getPath($params['fileSource']);
  77. list($owner, $ownerPath) = $this->getOwnerPath($path);
  78. $absPath = '/' . $owner . '/files/' . $ownerPath;
  79. $this->update($absPath);
  80. }
  81. }
  82. }
  83. /**
  84. * hook after file was unshared
  85. *
  86. * @param array $params
  87. */
  88. public function postUnshared($params) {
  89. if ($this->encryptionManager->isEnabled()) {
  90. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  91. $path = Filesystem::getPath($params['fileSource']);
  92. list($owner, $ownerPath) = $this->getOwnerPath($path);
  93. $absPath = '/' . $owner . '/files/' . $ownerPath;
  94. $this->update($absPath);
  95. }
  96. }
  97. }
  98. /**
  99. * inform encryption module that a file was restored from the trash bin,
  100. * e.g. to update the encryption keys
  101. *
  102. * @param array $params
  103. */
  104. public function postRestore($params) {
  105. if ($this->encryptionManager->isEnabled()) {
  106. $path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']);
  107. $this->update($path);
  108. }
  109. }
  110. /**
  111. * inform encryption module that a file was renamed,
  112. * e.g. to update the encryption keys
  113. *
  114. * @param array $params
  115. */
  116. public function postRename($params) {
  117. $source = $params['oldpath'];
  118. $target = $params['newpath'];
  119. if(
  120. $this->encryptionManager->isEnabled() &&
  121. dirname($source) !== dirname($target)
  122. ) {
  123. list($owner, $ownerPath) = $this->getOwnerPath($target);
  124. $absPath = '/' . $owner . '/files/' . $ownerPath;
  125. $this->update($absPath);
  126. }
  127. }
  128. /**
  129. * get owner and path relative to data/<owner>/files
  130. *
  131. * @param string $path path to file for current user
  132. * @return array ['owner' => $owner, 'path' => $path]
  133. * @throw \InvalidArgumentException
  134. */
  135. protected function getOwnerPath($path) {
  136. $info = Filesystem::getFileInfo($path);
  137. $owner = Filesystem::getOwner($path);
  138. $view = new View('/' . $owner . '/files');
  139. $path = $view->getPath($info->getId());
  140. if ($path === null) {
  141. throw new \InvalidArgumentException('No file found for ' . $info->getId());
  142. }
  143. return array($owner, $path);
  144. }
  145. /**
  146. * notify encryption module about added/removed users from a file/folder
  147. *
  148. * @param string $path relative to data/
  149. * @throws Exceptions\ModuleDoesNotExistsException
  150. */
  151. public function update($path) {
  152. // if a folder was shared, get a list of all (sub-)folders
  153. if ($this->view->is_dir($path)) {
  154. $allFiles = $this->util->getAllFiles($path);
  155. } else {
  156. $allFiles = array($path);
  157. }
  158. $encryptionModule = $this->encryptionManager->getEncryptionModule();
  159. foreach ($allFiles as $file) {
  160. $usersSharing = $this->file->getAccessList($file);
  161. $encryptionModule->update($file, $this->uid, $usersSharing);
  162. }
  163. }
  164. }