Skip to content

Commit

Permalink
remember original fopen access type in pre-proxy because sometimes th…
Browse files Browse the repository at this point in the history
…ey change

during the fopen call, e.g. 'r' becomes 'r+' if we open an URL
  • Loading branch information
Bjoern Schiessle committed Feb 27, 2014
1 parent 577e47e commit 4ace1a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 26 additions & 6 deletions apps/files_encryption/lib/proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Proxy extends \OC_FileProxy {

private static $blackList = null; //mimetypes blacklisted from encryption
private static $unencryptedSizes = array(); // remember unencrypted size
private static $fopenMode = array(); // remember the fopen mode

/**
* Check if a file requires encryption
Expand Down Expand Up @@ -213,6 +214,16 @@ public function postTouch($path) {
return true;
}

/**
* @brief remember initial fopen mode because sometimes it gets changed during the request
* @param string $path path
* @param string $mode type of access
*/
public function preFopen($path, $mode) {
self::$fopenMode[$path] = $mode;
}


/**
* @param $path
* @param $result
Expand Down Expand Up @@ -240,7 +251,15 @@ public function postFopen($path, &$result) {
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;

$meta = stream_get_meta_data($result);
// if we remember the mode from the pre proxy we re-use it
// oterwise we fall back to stream_get_meta_data()
if (isset(self::$fopenMode[$path])) {
$mode = self::$fopenMode[$path];
unset(self::$fopenMode[$path]);
} else {
$meta = stream_get_meta_data($result);
$mode = $meta['mode'];
}

$view = new \OC_FilesystemView('');

Expand All @@ -258,14 +277,15 @@ public function postFopen($path, &$result) {

// Open the file using the crypto stream wrapper
// protocol and let it do the decryption work instead
$result = fopen('crypt://' . $path, $meta['mode']);
$result = fopen('crypt://' . $path, $mode);

} elseif (
self::shouldEncrypt($path)
and $meta['mode'] !== 'r'
and $meta['mode'] !== 'rb'
self::shouldEncrypt($path)
and $mode !== 'r'
and $mode !== 'rb'

) {
$result = fopen('crypt://' . $path, $meta['mode']);
$result = fopen('crypt://' . $path, $mode);
}

// Re-enable the proxy
Expand Down
3 changes: 3 additions & 0 deletions apps/files_encryption/lib/stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public function stream_open($path, $mode, $options, &$opened_path) {
} else {

$this->meta = stream_get_meta_data($this->handle);
// sometimes fopen changes the mode, e.g. for a url "r" convert to "r+"
// but we need to remember the original access type
$this->meta['mode'] = $mode;

}

Expand Down

0 comments on commit 4ace1a2

Please sign in to comment.