update.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Mount;
  24. use \OC\Files\View;
  25. /**
  26. * update encrypted files, e.g. because a file was shared
  27. */
  28. class Update {
  29. /** @var \OC\Files\View */
  30. protected $view;
  31. /** @var \OC\Encryption\Util */
  32. protected $util;
  33. /** @var \OC\Files\Mount\Manager */
  34. protected $mountManager;
  35. /** @var \OC\Encryption\Manager */
  36. protected $encryptionManager;
  37. /** @var string */
  38. protected $uid;
  39. /** @var \OC\Encryption\File */
  40. protected $file;
  41. /**
  42. *
  43. * @param \OC\Files\View $view
  44. * @param \OC\Encryption\Util $util
  45. * @param \OC\Files\Mount\Manager $mountManager
  46. * @param \OC\Encryption\Manager $encryptionManager
  47. * @param \OC\Encryption\File $file
  48. * @param string $uid
  49. */
  50. public function __construct(
  51. View $view,
  52. Util $util,
  53. Mount\Manager $mountManager,
  54. Manager $encryptionManager,
  55. File $file,
  56. $uid
  57. ) {
  58. $this->view = $view;
  59. $this->util = $util;
  60. $this->mountManager = $mountManager;
  61. $this->encryptionManager = $encryptionManager;
  62. $this->file = $file;
  63. $this->uid = $uid;
  64. }
  65. public function postShared($params) {
  66. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  67. $this->update($params['fileSource']);
  68. }
  69. }
  70. public function postUnshared($params) {
  71. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  72. $this->update($params['fileSource']);
  73. }
  74. }
  75. /**
  76. * update keyfiles and share keys recursively
  77. *
  78. * @param int $fileSource file source id
  79. */
  80. private function update($fileSource) {
  81. $path = \OC\Files\Filesystem::getPath($fileSource);
  82. $info = \OC\Files\Filesystem::getFileInfo($path);
  83. $owner = \OC\Files\Filesystem::getOwner($path);
  84. $view = new \OC\Files\View('/' . $owner . '/files');
  85. $ownerPath = $view->getPath($info->getId());
  86. $absPath = '/' . $owner . '/files' . $ownerPath;
  87. $mount = $this->mountManager->find($path);
  88. $mountPoint = $mount->getMountPoint();
  89. // if a folder was shared, get a list of all (sub-)folders
  90. if ($this->view->is_dir($absPath)) {
  91. $allFiles = $this->util->getAllFiles($absPath, $mountPoint);
  92. } else {
  93. $allFiles = array($absPath);
  94. }
  95. $encryptionModule = $this->encryptionManager->getDefaultEncryptionModule();
  96. foreach ($allFiles as $path) {
  97. $usersSharing = $this->file->getAccessList($path);
  98. $encryptionModule->update($path, $this->uid, $usersSharing);
  99. }
  100. }
  101. }