Skip to content

Commit

Permalink
Allow decryption of versions when being accessed by getContentOfVersi…
Browse files Browse the repository at this point in the history
…on()
  • Loading branch information
DeepDiver1975 committed Nov 8, 2017
1 parent c3518b8 commit d451c1f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 45 deletions.
6 changes: 6 additions & 0 deletions apps/files_versions/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ public static function getVersions($uid, $filename, $userFullPath = '') {
$versions[$key]['timestamp'] = $timestamp;
$versions[$key]['etag'] = $view->getETag($dir . '/' . $entryName);
$versions[$key]['storage_location'] = "$dir/$entryName";
$versions[$key]['owner'] = $uid;
}
}
}
Expand Down Expand Up @@ -835,4 +836,9 @@ protected static function getExpiration(){
return self::$application->getContainer()->query('Expiration');
}

public static function getContentOfVersion($uid, $storage_location) {
$users_view = new View('/'.$uid);
return $users_view->fopen($storage_location, 'r');
}

}
6 changes: 5 additions & 1 deletion lib/private/Files/Meta/MetaFileVersionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public function getSize() {
* @inheritdoc
*/
public function getContent() {
return $this->storage->getContentOfVersion($this->internalPath, $this->versionId);
$handle = $this->fopen('r+');
$data = stream_get_contents($handle);
fclose($handle);

return $data;
}

/**
Expand Down
45 changes: 21 additions & 24 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,26 @@ public function getVersions($internalPath) {
if (!\OC_App::isEnabled('files_versions')) {
return [];
}
$p = $this->convertInternalPathToGlobalPath($internalPath);
list ($uid, $filename) = $this->convertInternalPathToGlobalPath($internalPath);

return array_values(
\OCA\Files_Versions\Storage::getVersions($this->getOwner($internalPath), $p));
\OCA\Files_Versions\Storage::getVersions($uid, $filename));
}

/**
* @param $internalPath
* @return array
*/
private function convertInternalPathToGlobalPath($internalPath) {
$mounts = \OC::$server->getMountManager()->findByStorageId($this->getId());
$mount = end($mounts);
$p = $mount->getMountPoint() . $internalPath;
$p = explode('/', ltrim($p, '/'));
array_shift($p);
array_shift($p);
$p = implode('/', $p);
$o = explode('/', $mount->getMountPoint());
return [$o[1], $p];
}

public function getVersion($internalPath, $versionId) {
Expand All @@ -708,34 +724,15 @@ public function getVersion($internalPath, $versionId) {

public function getContentOfVersion($internalPath, $versionId) {
$v = $this->getVersion($internalPath, $versionId);
return $this->file_get_contents($v['storage_location']);
}

public function getContentOfVersionAsStream($internalPath, $versionId) {
$v = $this->getVersion($internalPath, $versionId);
return $this->fopen($v['storage_location'], 'r');
return \OCA\Files_Versions\Storage::getContentOfVersion($v['owner'], $v['storage_location']);
}

public function restoreVersion($internalPath, $versionId) {
// KISS implementation
if (!\OC_App::isEnabled('files_versions')) {
return;
}
$p = $this->convertInternalPathToGlobalPath($internalPath);
\OCA\Files_Versions\Storage::rollback($p, $versionId);
}

/**
* @param $internalPath
* @return array|string
*/
private function convertInternalPathToGlobalPath($internalPath) {
$mount = \OC::$server->getMountManager()->findByStorageId($this->getId());
$p = $mount[0]->getMountPoint() . $internalPath;
$p = explode('/', ltrim($p, '/'));
array_shift($p);
array_shift($p);
$p = implode('/', $p);
return $p;
list ($uid, $filename) = $this->convertInternalPathToGlobalPath($internalPath);
\OCA\Files_Versions\Storage::rollback($filename, $versionId);
}
}
31 changes: 21 additions & 10 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,10 +887,15 @@ protected function getFullPath($path) {
* read first block of encrypted file, typically this will contain the
* encryption header
*
* @param string $path
* @param string|resource $path
* @return string
*/
protected function readFirstBlock($path) {
if (is_resource($path)) {
$firstBlock = fread($path, $this->util->getHeaderSize());
rewind($path);
return $firstBlock;
}
$firstBlock = '';
if ($this->storage->file_exists($path)) {
$handle = $this->storage->fopen($path, 'r');
Expand All @@ -903,14 +908,16 @@ protected function readFirstBlock($path) {
/**
* return header size of given file
*
* @param string $path
* @param string|resource $path
* @return int
*/
protected function getHeaderSize($path) {
$headerSize = 0;
$realFile = $this->util->stripPartialFileExtension($path);
if ($this->storage->file_exists($realFile)) {
$path = $realFile;
if (!is_resource($path)) {
$realFile = $this->util->stripPartialFileExtension($path);
if ($this->storage->file_exists($realFile)) {
$path = $realFile;
}
}
$firstBlock = $this->readFirstBlock($path);

Expand Down Expand Up @@ -952,14 +959,18 @@ protected function parseRawHeader($rawHeader) {
/**
* read header from file
*
* @param string $path
* @param string|resource $path
* @return array
*/
protected function getHeader($path) {
$realFile = $this->util->stripPartialFileExtension($path);
$exists = $this->storage->file_exists($realFile);
if ($exists) {
$path = $realFile;
if (is_resource($path)) {
$exists = false;
} else {
$realFile = $this->util->stripPartialFileExtension($path);
$exists = $this->storage->file_exists($realFile);
if ($exists) {
$path = $realFile;
}
}

$firstBlock = $this->readFirstBlock($path);
Expand Down
12 changes: 2 additions & 10 deletions lib/public/Files/Storage/IVersionedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,14 @@ public function getVersions($internalPath);
public function getVersion($internalPath, $versionId);

/**
* Get the content of a given version of a given file as string
* Get the content of a given version of a given file as stream resource
*
* @param string $internalPath
* @param string $versionId
* @return string
* @since 10.1.0
*/
public function getContentOfVersion($internalPath, $versionId);

/**
* @param string $internalPath
* @param string $versionId
* @return resource
* @since 10.1.0
*/
public function getContentOfVersionAsStream($internalPath, $versionId);
public function getContentOfVersion($internalPath, $versionId);

/**
* Restore the given version of a given file
Expand Down

0 comments on commit d451c1f

Please sign in to comment.