2015-01-14 19:39:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author jknockaert <jasper@knockaert.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-07 15:02:49 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-01-14 19:39:23 +00:00
|
|
|
*
|
2015-04-07 15:02:49 +00:00
|
|
|
* @license AGPL-3.0
|
2015-01-14 19:39:23 +00:00
|
|
|
*
|
2015-04-07 15:02:49 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2015-01-14 19:39:23 +00:00
|
|
|
*
|
2015-04-07 15:02:49 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 19:39:23 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-04-07 15:02:49 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 19:39:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Stream;
|
|
|
|
|
|
|
|
use Icewind\Streams\Wrapper;
|
|
|
|
use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
|
|
|
|
|
|
|
|
class Encryption extends Wrapper {
|
|
|
|
|
|
|
|
/** @var \OC\Encryption\Util */
|
|
|
|
protected $util;
|
|
|
|
|
2015-03-31 14:23:31 +00:00
|
|
|
/** @var \OC\Encryption\File */
|
|
|
|
protected $file;
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
/** @var \OCP\Encryption\IEncryptionModule */
|
|
|
|
protected $encryptionModule;
|
|
|
|
|
|
|
|
/** @var \OC\Files\Storage\Storage */
|
|
|
|
protected $storage;
|
|
|
|
|
|
|
|
/** @var \OC\Files\Storage\Wrapper\Encryption */
|
|
|
|
protected $encryptionStorage;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $internalPath;
|
|
|
|
|
2015-03-30 10:21:59 +00:00
|
|
|
/** @var string */
|
|
|
|
protected $cache;
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
/** @var integer */
|
|
|
|
protected $size;
|
|
|
|
|
|
|
|
/** @var integer */
|
|
|
|
protected $position;
|
|
|
|
|
|
|
|
/** @var integer */
|
|
|
|
protected $unencryptedSize;
|
|
|
|
|
2015-04-24 11:02:06 +00:00
|
|
|
/** @var integer */
|
|
|
|
protected $headerSize;
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
/** @var integer */
|
|
|
|
protected $unencryptedBlockSize;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $header;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $fullPath;
|
|
|
|
|
2016-01-05 18:01:03 +00:00
|
|
|
/** @var bool */
|
|
|
|
protected $signed;
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
/**
|
|
|
|
* header data returned by the encryption module, will be written to the file
|
|
|
|
* in case of a write operation
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $newHeader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* user who perform the read/write operation null for public access
|
|
|
|
*
|
2015-03-30 11:59:48 +00:00
|
|
|
* @var string
|
2015-01-14 19:39:23 +00:00
|
|
|
*/
|
|
|
|
protected $uid;
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
protected $readOnly;
|
|
|
|
|
2015-03-30 10:21:59 +00:00
|
|
|
/** @var bool */
|
|
|
|
protected $writeFlag;
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
/** @var array */
|
|
|
|
protected $expectedContextProperties;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->expectedContextProperties = array(
|
|
|
|
'source',
|
|
|
|
'storage',
|
|
|
|
'internalPath',
|
|
|
|
'fullPath',
|
|
|
|
'encryptionModule',
|
|
|
|
'header',
|
|
|
|
'uid',
|
2015-03-31 14:23:31 +00:00
|
|
|
'file',
|
2015-01-14 19:39:23 +00:00
|
|
|
'util',
|
|
|
|
'size',
|
|
|
|
'unencryptedSize',
|
2015-04-24 11:02:06 +00:00
|
|
|
'encryptionStorage',
|
2016-01-05 14:29:44 +00:00
|
|
|
'headerSize',
|
|
|
|
'signed'
|
2015-01-14 19:39:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps a stream with the provided callbacks
|
|
|
|
*
|
|
|
|
* @param resource $source
|
|
|
|
* @param string $internalPath relative to mount point
|
|
|
|
* @param string $fullPath relative to data/
|
|
|
|
* @param array $header
|
2015-03-30 11:59:48 +00:00
|
|
|
* @param string $uid
|
2015-01-14 19:39:23 +00:00
|
|
|
* @param \OCP\Encryption\IEncryptionModule $encryptionModule
|
|
|
|
* @param \OC\Files\Storage\Storage $storage
|
2015-03-30 11:59:48 +00:00
|
|
|
* @param \OC\Files\Storage\Wrapper\Encryption $encStorage
|
2015-01-14 19:39:23 +00:00
|
|
|
* @param \OC\Encryption\Util $util
|
2015-03-31 14:23:31 +00:00
|
|
|
* @param \OC\Encryption\File $file
|
2015-01-14 19:39:23 +00:00
|
|
|
* @param string $mode
|
|
|
|
* @param int $size
|
|
|
|
* @param int $unencryptedSize
|
2015-04-24 11:02:06 +00:00
|
|
|
* @param int $headerSize
|
2016-01-05 14:29:44 +00:00
|
|
|
* @param bool $signed
|
2015-05-20 12:29:20 +00:00
|
|
|
* @param string $wrapper stream wrapper class
|
2015-01-14 19:39:23 +00:00
|
|
|
* @return resource
|
|
|
|
*
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public static function wrap($source, $internalPath, $fullPath, array $header,
|
2015-03-30 11:59:48 +00:00
|
|
|
$uid,
|
|
|
|
\OCP\Encryption\IEncryptionModule $encryptionModule,
|
|
|
|
\OC\Files\Storage\Storage $storage,
|
|
|
|
\OC\Files\Storage\Wrapper\Encryption $encStorage,
|
|
|
|
\OC\Encryption\Util $util,
|
2015-03-31 14:23:31 +00:00
|
|
|
\OC\Encryption\File $file,
|
2015-03-30 11:59:48 +00:00
|
|
|
$mode,
|
|
|
|
$size,
|
2015-04-24 11:02:06 +00:00
|
|
|
$unencryptedSize,
|
2015-05-20 12:29:20 +00:00
|
|
|
$headerSize,
|
2016-01-05 14:29:44 +00:00
|
|
|
$signed,
|
2015-05-20 12:29:20 +00:00
|
|
|
$wrapper = 'OC\Files\Stream\Encryption') {
|
2015-01-14 19:39:23 +00:00
|
|
|
|
|
|
|
$context = stream_context_create(array(
|
|
|
|
'ocencryption' => array(
|
|
|
|
'source' => $source,
|
|
|
|
'storage' => $storage,
|
|
|
|
'internalPath' => $internalPath,
|
|
|
|
'fullPath' => $fullPath,
|
|
|
|
'encryptionModule' => $encryptionModule,
|
|
|
|
'header' => $header,
|
|
|
|
'uid' => $uid,
|
|
|
|
'util' => $util,
|
2015-03-31 14:23:31 +00:00
|
|
|
'file' => $file,
|
2015-01-14 19:39:23 +00:00
|
|
|
'size' => $size,
|
|
|
|
'unencryptedSize' => $unencryptedSize,
|
2015-04-24 11:02:06 +00:00
|
|
|
'encryptionStorage' => $encStorage,
|
2016-01-05 14:29:44 +00:00
|
|
|
'headerSize' => $headerSize,
|
|
|
|
'signed' => $signed
|
2015-01-14 19:39:23 +00:00
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2015-11-24 12:25:27 +00:00
|
|
|
return self::wrapSource($source, $context, 'ocencryption', $wrapper, $mode);
|
2015-01-14 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add stream wrapper
|
|
|
|
*
|
|
|
|
* @param resource $source
|
|
|
|
* @param string $mode
|
2016-02-17 13:21:07 +00:00
|
|
|
* @param resource $context
|
2015-01-14 19:39:23 +00:00
|
|
|
* @param string $protocol
|
|
|
|
* @param string $class
|
|
|
|
* @return resource
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
*/
|
2015-11-24 12:25:27 +00:00
|
|
|
protected static function wrapSource($source, $context, $protocol, $class, $mode = 'r+') {
|
2015-01-14 19:39:23 +00:00
|
|
|
try {
|
|
|
|
stream_wrapper_register($protocol, $class);
|
|
|
|
if (@rewinddir($source) === false) {
|
|
|
|
$wrapped = fopen($protocol . '://', $mode, false, $context);
|
|
|
|
} else {
|
|
|
|
$wrapped = opendir($protocol . '://', $context);
|
|
|
|
}
|
|
|
|
} catch (\BadMethodCallException $e) {
|
|
|
|
stream_wrapper_unregister($protocol);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
stream_wrapper_unregister($protocol);
|
|
|
|
return $wrapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the source from the stream context and return the context options
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
*/
|
|
|
|
protected function loadContext($name) {
|
|
|
|
$context = parent::loadContext($name);
|
|
|
|
|
|
|
|
foreach ($this->expectedContextProperties as $property) {
|
2015-04-01 11:59:29 +00:00
|
|
|
if (array_key_exists($property, $context)) {
|
2015-01-14 19:39:23 +00:00
|
|
|
$this->{$property} = $context[$property];
|
|
|
|
} else {
|
|
|
|
throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $context;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
|
|
|
$this->loadContext('ocencryption');
|
|
|
|
|
|
|
|
$this->position = 0;
|
2015-03-30 11:36:56 +00:00
|
|
|
$this->cache = '';
|
|
|
|
$this->writeFlag = false;
|
2016-01-05 14:29:44 +00:00
|
|
|
$this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed);
|
2015-01-14 19:39:23 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
$mode === 'w'
|
|
|
|
|| $mode === 'w+'
|
|
|
|
|| $mode === 'wb'
|
|
|
|
|| $mode === 'wb+'
|
2015-04-21 12:21:53 +00:00
|
|
|
|| $mode === 'r+'
|
|
|
|
|| $mode === 'rb+'
|
2015-01-14 19:39:23 +00:00
|
|
|
) {
|
|
|
|
$this->readOnly = false;
|
|
|
|
} else {
|
|
|
|
$this->readOnly = true;
|
|
|
|
}
|
|
|
|
|
2015-04-21 12:43:08 +00:00
|
|
|
$sharePath = $this->fullPath;
|
|
|
|
if (!$this->storage->file_exists($this->internalPath)) {
|
|
|
|
$sharePath = dirname($sharePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
$accessList = $this->file->getAccessList($sharePath);
|
2015-04-24 11:02:06 +00:00
|
|
|
$this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList);
|
2015-04-21 12:43:08 +00:00
|
|
|
|
2015-04-21 12:21:53 +00:00
|
|
|
if (
|
|
|
|
$mode === 'w'
|
|
|
|
|| $mode === 'w+'
|
|
|
|
|| $mode === 'wb'
|
|
|
|
|| $mode === 'wb+'
|
|
|
|
) {
|
|
|
|
// We're writing a new file so start write counter with 0 bytes
|
|
|
|
$this->unencryptedSize = 0;
|
|
|
|
$this->writeHeader();
|
2015-04-24 11:02:06 +00:00
|
|
|
$this->headerSize = $this->util->getHeaderSize();
|
|
|
|
$this->size = $this->headerSize;
|
2015-04-21 12:21:53 +00:00
|
|
|
} else {
|
2015-04-24 14:47:27 +00:00
|
|
|
$this->skipHeader();
|
2015-04-22 10:28:07 +00:00
|
|
|
}
|
2015-04-24 14:47:27 +00:00
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:30:10 +00:00
|
|
|
public function stream_eof() {
|
|
|
|
return $this->position >= $this->unencryptedSize;
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
public function stream_read($count) {
|
|
|
|
|
|
|
|
$result = '';
|
|
|
|
|
2015-05-21 05:51:07 +00:00
|
|
|
$count = min($count, $this->unencryptedSize - $this->position);
|
2015-01-14 19:39:23 +00:00
|
|
|
while ($count > 0) {
|
|
|
|
$remainingLength = $count;
|
|
|
|
// update the cache of the current block
|
2015-03-30 10:21:59 +00:00
|
|
|
$this->readCache();
|
2015-01-14 19:39:23 +00:00
|
|
|
// determine the relative position in the current block
|
|
|
|
$blockPosition = ($this->position % $this->unencryptedBlockSize);
|
|
|
|
// if entire read inside current block then only position needs to be updated
|
|
|
|
if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
|
2015-03-30 10:21:59 +00:00
|
|
|
$result .= substr($this->cache, $blockPosition, $remainingLength);
|
2015-01-14 19:39:23 +00:00
|
|
|
$this->position += $remainingLength;
|
|
|
|
$count = 0;
|
2015-03-30 11:59:48 +00:00
|
|
|
// otherwise remainder of current block is fetched, the block is flushed and the position updated
|
2015-01-14 19:39:23 +00:00
|
|
|
} else {
|
2015-03-30 10:21:59 +00:00
|
|
|
$result .= substr($this->cache, $blockPosition);
|
|
|
|
$this->flush();
|
2015-01-14 19:39:23 +00:00
|
|
|
$this->position += ($this->unencryptedBlockSize - $blockPosition);
|
|
|
|
$count -= ($this->unencryptedBlockSize - $blockPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_write($data) {
|
|
|
|
|
|
|
|
$length = 0;
|
|
|
|
// loop over $data to fit it in 6126 sized unencrypted blocks
|
2015-05-18 11:27:22 +00:00
|
|
|
while (isset($data[0])) {
|
2015-01-14 19:39:23 +00:00
|
|
|
$remainingLength = strlen($data);
|
|
|
|
|
2015-03-30 10:21:59 +00:00
|
|
|
// set the cache to the current 6126 block
|
|
|
|
$this->readCache();
|
2015-01-14 19:39:23 +00:00
|
|
|
|
|
|
|
// for seekable streams the pointer is moved back to the beginning of the encrypted block
|
|
|
|
// flush will start writing there when the position moves to another block
|
2015-04-15 11:17:01 +00:00
|
|
|
$positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
|
2015-04-24 11:02:06 +00:00
|
|
|
$this->util->getBlockSize() + $this->headerSize;
|
2015-05-20 12:29:20 +00:00
|
|
|
$resultFseek = $this->parentStreamSeek($positionInFile);
|
2015-01-14 19:39:23 +00:00
|
|
|
|
|
|
|
// only allow writes on seekable streams, or at the end of the encrypted stream
|
2015-03-30 10:21:59 +00:00
|
|
|
if (!($this->readOnly) && ($resultFseek || $positionInFile === $this->size)) {
|
|
|
|
|
|
|
|
// switch the writeFlag so flush() will write the block
|
2015-03-30 11:59:48 +00:00
|
|
|
$this->writeFlag = true;
|
2015-01-14 19:39:23 +00:00
|
|
|
|
|
|
|
// determine the relative position in the current block
|
|
|
|
$blockPosition = ($this->position % $this->unencryptedBlockSize);
|
|
|
|
// check if $data fits in current block
|
|
|
|
// if so, overwrite existing data (if any)
|
|
|
|
// update position and liberate $data
|
|
|
|
if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
|
2015-03-30 10:21:59 +00:00
|
|
|
$this->cache = substr($this->cache, 0, $blockPosition)
|
|
|
|
. $data . substr($this->cache, $blockPosition + $remainingLength);
|
2015-01-14 19:39:23 +00:00
|
|
|
$this->position += $remainingLength;
|
|
|
|
$length += $remainingLength;
|
|
|
|
$data = '';
|
2015-03-30 11:59:48 +00:00
|
|
|
// if $data doesn't fit the current block, the fill the current block and reiterate
|
|
|
|
// after the block is filled, it is flushed and $data is updatedxxx
|
2015-01-14 19:39:23 +00:00
|
|
|
} else {
|
2015-03-30 10:21:59 +00:00
|
|
|
$this->cache = substr($this->cache, 0, $blockPosition) .
|
2015-01-14 19:39:23 +00:00
|
|
|
substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
|
2015-03-30 10:21:59 +00:00
|
|
|
$this->flush();
|
2015-01-14 19:39:23 +00:00
|
|
|
$this->position += ($this->unencryptedBlockSize - $blockPosition);
|
|
|
|
$length += ($this->unencryptedBlockSize - $blockPosition);
|
|
|
|
$data = substr($data, $this->unencryptedBlockSize - $blockPosition);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = '';
|
|
|
|
}
|
2015-05-18 13:06:55 +00:00
|
|
|
$this->unencryptedSize = max($this->unencryptedSize, $this->position);
|
2015-01-14 19:39:23 +00:00
|
|
|
}
|
|
|
|
return $length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_tell() {
|
|
|
|
return $this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
|
|
|
|
|
|
|
$return = false;
|
|
|
|
|
|
|
|
switch ($whence) {
|
|
|
|
case SEEK_SET:
|
2015-04-30 15:10:18 +00:00
|
|
|
$newPosition = $offset;
|
2015-01-14 19:39:23 +00:00
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2015-04-30 15:10:18 +00:00
|
|
|
$newPosition = $this->position + $offset;
|
2015-01-14 19:39:23 +00:00
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2015-04-30 15:10:18 +00:00
|
|
|
$newPosition = $this->unencryptedSize + $offset;
|
2015-01-14 19:39:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2015-04-30 15:10:18 +00:00
|
|
|
if ($newPosition > $this->unencryptedSize || $newPosition < 0) {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
$newFilePosition = floor($newPosition / $this->unencryptedBlockSize)
|
2015-04-24 11:02:06 +00:00
|
|
|
* $this->util->getBlockSize() + $this->headerSize;
|
2015-01-14 19:39:23 +00:00
|
|
|
|
2015-04-02 12:52:54 +00:00
|
|
|
$oldFilePosition = parent::stream_tell();
|
2015-05-20 12:29:20 +00:00
|
|
|
if ($this->parentStreamSeek($newFilePosition)) {
|
|
|
|
$this->parentStreamSeek($oldFilePosition);
|
2015-03-30 10:21:59 +00:00
|
|
|
$this->flush();
|
2015-05-20 12:29:20 +00:00
|
|
|
$this->parentStreamSeek($newFilePosition);
|
2015-01-14 19:39:23 +00:00
|
|
|
$this->position = $newPosition;
|
|
|
|
$return = true;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_close() {
|
2016-02-03 13:32:04 +00:00
|
|
|
$this->flush('end');
|
|
|
|
$position = (int)floor($this->position/$this->unencryptedBlockSize);
|
|
|
|
$remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
|
2015-03-30 14:07:41 +00:00
|
|
|
if ($this->readOnly === false) {
|
|
|
|
if(!empty($remainingData)) {
|
|
|
|
parent::stream_write($remainingData);
|
|
|
|
}
|
|
|
|
$this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
|
|
|
|
}
|
2015-01-14 19:39:23 +00:00
|
|
|
return parent::stream_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-30 10:21:59 +00:00
|
|
|
* write block to file
|
2016-02-03 13:32:04 +00:00
|
|
|
* @param string $positionPrefix
|
2015-01-14 19:39:23 +00:00
|
|
|
*/
|
2016-02-03 13:32:04 +00:00
|
|
|
protected function flush($positionPrefix = '') {
|
2015-03-30 10:21:59 +00:00
|
|
|
// write to disk only when writeFlag was set to 1
|
|
|
|
if ($this->writeFlag) {
|
|
|
|
// Disable the file proxies so that encryption is not
|
|
|
|
// automatically attempted when the file is written to disk -
|
|
|
|
// we are handling that separately here and we don't want to
|
|
|
|
// get into an infinite loop
|
2016-02-03 13:32:04 +00:00
|
|
|
$position = (int)floor($this->position/$this->unencryptedBlockSize);
|
|
|
|
$encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
|
2015-05-22 20:13:27 +00:00
|
|
|
$bytesWritten = parent::stream_write($encrypted);
|
2015-03-30 10:21:59 +00:00
|
|
|
$this->writeFlag = false;
|
2015-05-22 20:13:27 +00:00
|
|
|
// Check whether the write concerns the last block
|
|
|
|
// If so then update the encrypted filesize
|
2015-05-20 07:35:20 +00:00
|
|
|
// Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
|
|
|
|
// We recalculate the encrypted filesize as we do not know the context of calling flush()
|
2015-05-22 20:13:27 +00:00
|
|
|
$completeBlocksInFile=(int)floor($this->unencryptedSize/$this->unencryptedBlockSize);
|
|
|
|
if ($completeBlocksInFile === (int)floor($this->position/$this->unencryptedBlockSize)) {
|
|
|
|
$this->size = $this->util->getBlockSize() * $completeBlocksInFile;
|
|
|
|
$this->size += $bytesWritten;
|
2015-05-20 07:35:20 +00:00
|
|
|
$this->size += $this->headerSize;
|
|
|
|
}
|
2015-01-14 19:39:23 +00:00
|
|
|
}
|
2015-03-30 10:21:59 +00:00
|
|
|
// always empty the cache (otherwise readCache() will not fill it with the new block)
|
|
|
|
$this->cache = '';
|
2015-01-14 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 10:21:59 +00:00
|
|
|
/**
|
|
|
|
* read block to file
|
|
|
|
*/
|
|
|
|
protected function readCache() {
|
|
|
|
// cache should always be empty string when this function is called
|
|
|
|
// don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
|
2015-03-30 11:59:48 +00:00
|
|
|
if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
|
2015-03-30 10:21:59 +00:00
|
|
|
// Get the data from the file handle
|
|
|
|
$data = parent::stream_read($this->util->getBlockSize());
|
2016-02-03 13:32:04 +00:00
|
|
|
$position = (int)floor($this->position/$this->unencryptedBlockSize);
|
|
|
|
$numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
|
|
|
|
if($numberOfChunks === $position) {
|
|
|
|
$position .= 'end';
|
|
|
|
}
|
|
|
|
$this->cache = $this->encryptionModule->decrypt($data, $position);
|
2015-03-30 10:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 19:39:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* write header at beginning of encrypted file
|
|
|
|
*
|
2015-03-30 15:13:42 +00:00
|
|
|
* @return integer
|
2015-01-14 19:39:23 +00:00
|
|
|
* @throws EncryptionHeaderKeyExistsException if header key is already in use
|
|
|
|
*/
|
2015-04-24 14:47:27 +00:00
|
|
|
protected function writeHeader() {
|
2015-01-14 19:39:23 +00:00
|
|
|
$header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
|
2015-03-30 15:13:42 +00:00
|
|
|
return parent::stream_write($header);
|
2015-01-14 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:47:27 +00:00
|
|
|
/**
|
|
|
|
* read first block to skip the header
|
|
|
|
*/
|
|
|
|
protected function skipHeader() {
|
2015-04-24 11:02:06 +00:00
|
|
|
parent::stream_read($this->headerSize);
|
2015-04-24 14:47:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:29:20 +00:00
|
|
|
/**
|
|
|
|
* call stream_seek() from parent class
|
|
|
|
*
|
|
|
|
* @param integer $position
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function parentStreamSeek($position) {
|
|
|
|
return parent::stream_seek($position);
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:32:09 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function dir_opendir($path, $options) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:39:23 +00:00
|
|
|
}
|