Exemple #1
0
	/** {@inheritdoc} */
	public function opendir($path) {
		$this->init();
		$path = $this->cleanPath($path);
		try {
			$response = $this->client->propfind(
				$this->encodePath($path),
				array(),
				1
			);
			$id = md5('webdav' . $this->root . $path);
			$content = array();
			$files = array_keys($response);
			array_shift($files); //the first entry is the current directory

			if (!$this->statCache->hasKey($path)) {
				$this->statCache->set($path, true);
			}
			foreach ($files as $file) {
				$file = urldecode($file);
				// do not store the real entry, we might not have all properties
				if (!$this->statCache->hasKey($path)) {
					$this->statCache->set($file, true);
				}
				$file = basename($file);
				$content[] = $file;
			}
			Dir::register($id, $content);
			return opendir('fakedir://' . $id);
		} catch (ClientHttpException $e) {
			if ($e->getHttpStatus() === 404) {
				$this->statCache->clear($path . '/');
				$this->statCache->set($path, false);
				return false;
			}
			$this->convertException($e);
		} catch (\Exception $e) {
			$this->convertException($e);
		}
		return false;
	}
Exemple #2
0
 /**
  * see http://php.net/manual/en/function.fopen.php
  *
  * @param string $path
  * @param string $mode
  * @return resource|bool
  * @throws GenericEncryptionException
  * @throws ModuleDoesNotExistsException
  */
 public function fopen($path, $mode)
 {
     // check if the file is stored in the array cache, this means that we
     // copy a file over to the versions folder, in this case we don't want to
     // decrypt it
     if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) {
         $this->arrayCache->remove('encryption_copy_version_' . $path);
         return $this->storage->fopen($path, $mode);
     }
     $encryptionEnabled = $this->encryptionManager->isEnabled();
     $shouldEncrypt = false;
     $encryptionModule = null;
     $header = $this->getHeader($path);
     $signed = isset($header['signed']) && $header['signed'] === 'true' ? true : false;
     $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 encryption 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);
                     $signed = true;
                 }
             } 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);
             if (!is_resource($source)) {
                 return false;
             }
             $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, $size, $unencryptedSize, $headerSize, $signed);
             return $handle;
         }
     }
     return $this->storage->fopen($path, $mode);
 }