Exemplo n.º 1
0
 /**
  * @param string $mode
  * @param integer $limit
  */
 protected function getStream($fileName, $mode, $unencryptedSize)
 {
     $size = filesize($fileName);
     $source = fopen($fileName, $mode);
     $internalPath = $fileName;
     $fullPath = $fileName;
     $header = [];
     $uid = '';
     $encryptionModule = $this->buildMockModule();
     $storage = $this->getMockBuilder('\\OC\\Files\\Storage\\Storage')->disableOriginalConstructor()->getMock();
     $encStorage = $this->getMockBuilder('\\OC\\Files\\Storage\\Wrapper\\Encryption')->disableOriginalConstructor()->getMock();
     $config = $this->getMockBuilder('\\OCP\\IConfig')->disableOriginalConstructor()->getMock();
     $file = $this->getMockBuilder('\\OC\\Encryption\\File')->disableOriginalConstructor()->setMethods(['getAccessList'])->getMock();
     $file->expects($this->any())->method('getAccessList')->willReturn([]);
     $util = $this->getMock('\\OC\\Encryption\\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $config]);
     $util->expects($this->any())->method('getUidAndFilename')->willReturn(['user1', $internalPath]);
     return \OC\Files\Stream\Encryption::wrap($source, $internalPath, $fullPath, $header, $uid, $encryptionModule, $storage, $encStorage, $util, $file, $mode, $size, $unencryptedSize);
 }
Exemplo n.º 2
0
 /**
  * see http://php.net/manual/en/function.fopen.php
  *
  * @param string $path
  * @param string $mode
  * @return resource
  * @throws GenericEncryptionException
  * @throws ModuleDoesNotExistsException
  */
 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);
     if ($this->util->isExcluded($fullPath) === false) {
         $size = $unencryptedSize = 0;
         $realFile = $this->util->stripPartialFileExtension($path);
         $targetExists = $this->file_exists($realFile) || $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);
             }
             if ($this->file_exists($path)) {
                 $size = $this->storage->filesize($path);
                 $unencryptedSize = $this->filesize($path);
             } else {
                 $size = $unencryptedSize = 0;
             }
         }
         try {
             if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+') {
                 // don't overwrite encrypted files if encyption is not enabled
                 if ($targetIsEncrypted && $encryptionEnabled === false) {
                     throw new GenericEncryptionException('Tried to access encrypted file but encryption is not enabled');
                 }
                 if ($encryptionEnabled) {
                     // if $encryptionModuleId is empty, the default module will be used
                     $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
                     $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath);
                 }
             } else {
                 $info = $this->getCache()->get($path);
                 // only get encryption module if we found one in the header
                 // or if file should be encrypted according to the file cache
                 if (!empty($encryptionModuleId)) {
                     $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
                     $shouldEncrypt = true;
                 } else {
                     if (empty($encryptionModuleId) && $info['encrypted'] === true) {
                         // we come from a old installation. No header and/or no module defined
                         // but the file is encrypted. In this case we need to use the
                         // OC_DEFAULT_MODULE to read the file
                         $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE');
                         $shouldEncrypt = true;
                         $targetIsEncrypted = 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 && $encryptionModule !== null) {
             $headerSize = $this->getHeaderSize($path);
             $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, $headerSize);
             return $handle;
         }
     }
     return $this->storage->fopen($path, $mode);
 }
Exemplo n.º 3
0
 /**
  * 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);
     }
 }