/** * see http://php.net/manual/en/function.fopen.php * * @param string $path * @param string $mode * @return resource */ public function fopen($path, $mode) { $encryptionEnabled = $this->encryptionManager->isEnabled(); $shouldEncrypt = false; $encryptionModule = null; $header = $this->getHeader($path); $fullPath = $this->getFullPath($path); $encryptionModuleId = $this->util->getEncryptionModuleId($header); $size = $unencryptedSize = 0; $targetExists = $this->file_exists($path); $targetIsEncrypted = false; if ($targetExists) { // in case the file exists we require the explicit module as // specified in the file header - otherwise we need to fail hard to // prevent data loss on client side if (!empty($encryptionModuleId)) { $targetIsEncrypted = true; $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); } $size = $this->storage->filesize($path); $unencryptedSize = $this->filesize($path); } try { if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+') { if (!empty($encryptionModuleId)) { $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); } elseif ($encryptionEnabled) { $encryptionModule = $this->encryptionManager->getDefaultEncryptionModule(); $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); } } else { // only get encryption module if we found one in the header if (!empty($encryptionModuleId)) { $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); $shouldEncrypt = true; } } } catch (ModuleDoesNotExistsException $e) { $this->logger->warning('Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted (' . $e->getMessage() . ')'); } // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt if (!$encryptionEnabled || !$this->mount->getOption('encrypt', true)) { if (!$targetExists || !$targetIsEncrypted) { $shouldEncrypt = false; } } if ($shouldEncrypt === true && !$this->util->isExcluded($fullPath) && $encryptionModule !== null) { $source = $this->storage->fopen($path, $mode); $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, $size, $unencryptedSize); return $handle; } else { return $this->storage->fopen($path, $mode); } }
/** * update keyfiles and share keys recursively * * @param int $fileSource file source id */ private function update($fileSource) { $path = \OC\Files\Filesystem::getPath($fileSource); $info = \OC\Files\Filesystem::getFileInfo($path); $owner = \OC\Files\Filesystem::getOwner($path); $view = new \OC\Files\View('/' . $owner . '/files'); $ownerPath = $view->getPath($info->getId()); $absPath = '/' . $owner . '/files' . $ownerPath; $mount = $this->mountManager->find($path); $mountPoint = $mount->getMountPoint(); // if a folder was shared, get a list of all (sub-)folders if ($this->view->is_dir($absPath)) { $allFiles = $this->util->getAllFiles($absPath, $mountPoint); } else { $allFiles = array($absPath); } $encryptionModule = $this->encryptionManager->getDefaultEncryptionModule(); foreach ($allFiles as $path) { $usersSharing = $this->file->getAccessList($path); $encryptionModule->update($path, $this->uid, $usersSharing); } }