code optimized and cleaned up unused vars
This commit is contained in:
parent
e9d1ea712c
commit
a134ffcf2c
|
@ -56,18 +56,21 @@ class Stream {
|
||||||
private $relPath; // rel path to users file dir
|
private $relPath; // rel path to users file dir
|
||||||
private $userId;
|
private $userId;
|
||||||
private $handle; // Resource returned by fopen
|
private $handle; // Resource returned by fopen
|
||||||
private $path;
|
|
||||||
private $readBuffer; // For streams that dont support seeking
|
|
||||||
private $meta = array(); // Header / meta for source stream
|
private $meta = array(); // Header / meta for source stream
|
||||||
private $count;
|
|
||||||
private $writeCache;
|
private $writeCache;
|
||||||
private $size;
|
private $size;
|
||||||
private $unencryptedSize;
|
private $unencryptedSize;
|
||||||
private $publicKey;
|
private $publicKey;
|
||||||
private $keyfile;
|
|
||||||
private $encKeyfile;
|
private $encKeyfile;
|
||||||
private static $view; // a fsview object set to user dir
|
/**
|
||||||
|
* @var \OC\Files\View
|
||||||
|
*/
|
||||||
private $rootView; // a fsview object set to '/'
|
private $rootView; // a fsview object set to '/'
|
||||||
|
/**
|
||||||
|
* @var \OCA\Encryption\Session
|
||||||
|
*/
|
||||||
|
private $session;
|
||||||
|
private $privateKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
|
@ -82,6 +85,10 @@ class Stream {
|
||||||
$this->rootView = new \OC_FilesystemView('/');
|
$this->rootView = new \OC_FilesystemView('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->session = new \OCA\Encryption\Session($this->rootView);
|
||||||
|
|
||||||
|
$this->privateKey = $this->session->getPrivateKey($this->userId);
|
||||||
|
|
||||||
$util = new Util($this->rootView, \OCP\USER::getUser());
|
$util = new Util($this->rootView, \OCP\USER::getUser());
|
||||||
|
|
||||||
$this->userId = $util->getUserId();
|
$this->userId = $util->getUserId();
|
||||||
|
@ -229,17 +236,14 @@ class Stream {
|
||||||
// If a keyfile already exists
|
// If a keyfile already exists
|
||||||
if ($this->encKeyfile) {
|
if ($this->encKeyfile) {
|
||||||
|
|
||||||
$session = new \OCA\Encryption\Session( $this->rootView );
|
|
||||||
|
|
||||||
$privateKey = $session->getPrivateKey($this->userId);
|
|
||||||
|
|
||||||
// if there is no valid private key return false
|
// if there is no valid private key return false
|
||||||
if($privateKey === false) {
|
if ($this->privateKey === false) {
|
||||||
|
|
||||||
if(\OC_Util::isCallRegistered()) {
|
if (\OC_Util::isCallRegistered()) {
|
||||||
$l = \OC_L10N::get('core');
|
$l = \OC_L10N::get('core');
|
||||||
\OCP\JSON::error(array('data' => array('message' => $l->t('Private key is not valid! Maybe the user password was changed from outside if so please change it back to gain access'))));
|
\OCP\JSON::error(array('data' => array('message' => $l->t('Private key is not valid! Maybe the user password was changed from outside if so please change it back to gain access'))));
|
||||||
throw new \Exception('Private key for user "' . $this->userId . '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access');
|
throw new \Exception('Private key for user "' . $this->userId
|
||||||
|
. '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access');
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -247,7 +251,7 @@ class Stream {
|
||||||
|
|
||||||
$shareKey = Keymanager::getShareKey($this->rootView, $this->userId, $this->relPath);
|
$shareKey = Keymanager::getShareKey($this->rootView, $this->userId, $this->relPath);
|
||||||
|
|
||||||
$this->plainKey = Crypt::multiKeyDecrypt($this->encKeyfile, $shareKey, $privateKey);
|
$this->plainKey = Crypt::multiKeyDecrypt($this->encKeyfile, $shareKey, $this->privateKey);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -270,6 +274,12 @@ class Stream {
|
||||||
*/
|
*/
|
||||||
public function stream_write($data) {
|
public function stream_write($data) {
|
||||||
|
|
||||||
|
// if there is no valid private key return false
|
||||||
|
if ($this->privateKey === false) {
|
||||||
|
$this->size = 0;
|
||||||
|
return strlen($data);
|
||||||
|
}
|
||||||
|
|
||||||
// Disable the file proxies so that encryption is not
|
// Disable the file proxies so that encryption is not
|
||||||
// automatically attempted when the file is written to disk -
|
// automatically attempted when the file is written to disk -
|
||||||
// we are handling that separately here and we don't want to
|
// we are handling that separately here and we don't want to
|
||||||
|
@ -437,19 +447,33 @@ class Stream {
|
||||||
|
|
||||||
$this->flush();
|
$this->flush();
|
||||||
|
|
||||||
$view = new \OC_FilesystemView('/');
|
|
||||||
$session = new \OCA\Encryption\Session( $this->rootView );
|
|
||||||
$privateKey = $session->getPrivateKey($this->userId);
|
|
||||||
|
|
||||||
// if there is no valid private key return false
|
// if there is no valid private key return false
|
||||||
if($privateKey === false) {
|
if ($this->privateKey === false) {
|
||||||
|
|
||||||
if(\OC_Util::isCallRegistered()) {
|
if (\OC_Util::isCallRegistered()) {
|
||||||
$l = \OC_L10N::get('core');
|
$l = \OC_L10N::get('core');
|
||||||
\OCP\JSON::error(array('data' => array('message' => $l->t('Private key is not valid! Maybe the user password was changed from outside if so please change it back to gain access'))));
|
\OCP\JSON::error(array('data' => array('message' => $l->t('Private key is not valid! Maybe the user password was changed from outside if so please change it back to gain access'))));
|
||||||
throw new \Exception('Private key for user "' . $this->userId . '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access');
|
|
||||||
|
// cleanup
|
||||||
|
if ($this->meta['mode'] !== 'r' && $this->meta['mode'] !== 'rb') {
|
||||||
|
|
||||||
|
// Disable encryption proxy to prevent recursive calls
|
||||||
|
$proxyStatus = \OC_FileProxy::$enabled;
|
||||||
|
\OC_FileProxy::$enabled = false;
|
||||||
|
|
||||||
|
if ($this->rootView->file_exists($this->rawPath) && $this->size === 0) {
|
||||||
|
$this->rootView->unlink($this->rawPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Re-enable proxy - our work is done
|
||||||
|
\OC_FileProxy::$enabled = $proxyStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \Exception('Private key for user "' . $this->userId
|
||||||
|
. '" is not valid! Maybe the user password was changed from outside if so please change it back to gain access');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -483,10 +507,10 @@ class Stream {
|
||||||
Keymanager::setFileKey($this->rootView, $this->relPath, $this->userId, $this->encKeyfiles['data']);
|
Keymanager::setFileKey($this->rootView, $this->relPath, $this->userId, $this->encKeyfiles['data']);
|
||||||
|
|
||||||
// Save the sharekeys
|
// Save the sharekeys
|
||||||
Keymanager::setShareKeys($view, $this->relPath, $this->encKeyfiles['keys']);
|
Keymanager::setShareKeys($this->rootView, $this->relPath, $this->encKeyfiles['keys']);
|
||||||
|
|
||||||
// get file info
|
// get file info
|
||||||
$fileInfo = $view->getFileInfo($this->rawPath);
|
$fileInfo = $this->rootView->getFileInfo($this->rawPath);
|
||||||
if (!is_array($fileInfo)) {
|
if (!is_array($fileInfo)) {
|
||||||
$fileInfo = array();
|
$fileInfo = array();
|
||||||
}
|
}
|
||||||
|
@ -500,7 +524,7 @@ class Stream {
|
||||||
$fileInfo['unencrypted_size'] = $this->unencryptedSize;
|
$fileInfo['unencrypted_size'] = $this->unencryptedSize;
|
||||||
|
|
||||||
// set fileinfo
|
// set fileinfo
|
||||||
$view->putFileInfo($this->rawPath, $fileInfo);
|
$this->rootView->putFileInfo($this->rawPath, $fileInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fclose($this->handle);
|
return fclose($this->handle);
|
||||||
|
|
Loading…
Reference in New Issue