update.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Encryption;
  23. use OC\Files\Filesystem;
  24. use \OC\Files\Mount;
  25. use \OC\Files\View;
  26. /**
  27. * update encrypted files, e.g. because a file was shared
  28. */
  29. class Update {
  30. /** @var \OC\Files\View */
  31. protected $view;
  32. /** @var \OC\Encryption\Util */
  33. protected $util;
  34. /** @var \OC\Files\Mount\Manager */
  35. protected $mountManager;
  36. /** @var \OC\Encryption\Manager */
  37. protected $encryptionManager;
  38. /** @var string */
  39. protected $uid;
  40. /** @var \OC\Encryption\File */
  41. protected $file;
  42. /**
  43. *
  44. * @param \OC\Files\View $view
  45. * @param \OC\Encryption\Util $util
  46. * @param \OC\Files\Mount\Manager $mountManager
  47. * @param \OC\Encryption\Manager $encryptionManager
  48. * @param \OC\Encryption\File $file
  49. * @param string $uid
  50. */
  51. public function __construct(
  52. View $view,
  53. Util $util,
  54. Mount\Manager $mountManager,
  55. Manager $encryptionManager,
  56. File $file,
  57. $uid
  58. ) {
  59. $this->view = $view;
  60. $this->util = $util;
  61. $this->mountManager = $mountManager;
  62. $this->encryptionManager = $encryptionManager;
  63. $this->file = $file;
  64. $this->uid = $uid;
  65. }
  66. /**
  67. * hook after file was shared
  68. *
  69. * @param array $params
  70. */
  71. public function postShared($params) {
  72. if ($this->encryptionManager->isEnabled()) {
  73. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  74. $path = Filesystem::getPath($params['fileSource']);
  75. list($owner, $ownerPath) = $this->getOwnerPath($path);
  76. $absPath = '/' . $owner . '/files/' . $ownerPath;
  77. $this->update($absPath);
  78. }
  79. }
  80. }
  81. /**
  82. * hook after file was unshared
  83. *
  84. * @param array $params
  85. */
  86. public function postUnshared($params) {
  87. if ($this->encryptionManager->isEnabled()) {
  88. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  89. $path = Filesystem::getPath($params['fileSource']);
  90. list($owner, $ownerPath) = $this->getOwnerPath($path);
  91. $absPath = '/' . $owner . '/files/' . $ownerPath;
  92. $this->update($absPath);
  93. }
  94. }
  95. }
  96. /**
  97. * get owner and path relative to data/<owner>/files
  98. *
  99. * @param string $path path to file for current user
  100. * @return array ['owner' => $owner, 'path' => $path]
  101. * @throw \InvalidArgumentException
  102. */
  103. private function getOwnerPath($path) {
  104. $info = Filesystem::getFileInfo($path);
  105. $owner = Filesystem::getOwner($path);
  106. $view = new View('/' . $owner . '/files');
  107. $path = $view->getPath($info->getId());
  108. if ($path === null) {
  109. throw new \InvalidArgumentException('No file found for ' . $info->getId());
  110. }
  111. return array($owner, $path);
  112. }
  113. /**
  114. * notify encryption module about added/removed users from a file/folder
  115. *
  116. * @param string $path relative to data/
  117. * @throws Exceptions\ModuleDoesNotExistsException
  118. */
  119. public function update($path) {
  120. // if a folder was shared, get a list of all (sub-)folders
  121. if ($this->view->is_dir($path)) {
  122. $allFiles = $this->util->getAllFiles($path);
  123. } else {
  124. $allFiles = array($path);
  125. }
  126. $encryptionModule = $this->encryptionManager->getEncryptionModule();
  127. foreach ($allFiles as $file) {
  128. $usersSharing = $this->file->getAccessList($file);
  129. $encryptionModule->update($file, $this->uid, $usersSharing);
  130. }
  131. }
  132. }