Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage the streaming support, even if its not real streaming yet #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions src/EncryptStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\Url;
use Drupal\encrypt\EncryptionProfileInterface;
use Drupal\encrypt\Stream\NoStreaming;

/**
* Provides a scheme wrapper which encrypts / decrypts automatically.
Expand Down Expand Up @@ -88,37 +89,14 @@ public static function basePath() {
}

/**
* Decrypts a given file.
* Gets an instance of the encryption stream.
*
* @param string $raw_file
* The encrypted content of the raw file.
* @param \Drupal\encrypt\EncryptionProfileInterface $encryption_profile
* The used encryption profile.
*
* @return string
* The decrypted string.
*/
protected function decrypt($raw_file, EncryptionProfileInterface $encryption_profile) {
/** @var \Drupal\encrypt\EncryptService $encryption */
$encryption = \Drupal::service('encryption');
return $encryption->decrypt($raw_file, $encryption_profile);
}

/**
* Encrypts a given file.
*
* @param string $raw_file
* The descrypted content of the raw file.
* @param \Drupal\encrypt\EncryptionProfileInterface $encryption_profile
* The used encryption profile.
*
* @return string
* The encrypted string.
* @return \Drupal\encrypt\Stream\EncryptStreamInterface
* The stream interface.
*/
protected function encrypt($raw_file, EncryptionProfileInterface $encryption_profile) {
/** @var \Drupal\encrypt\EncryptService $encryption */
$encryption = \Drupal::service('encryption');
return $encryption->encrypt($raw_file, $encryption_profile);
protected function getStreamEncryption(EncryptionProfileInterface $encryption_profile) {
$encryption_method = $encryption_profile->getEncryptionMethod();
return $encryption_method->getStreamingInstance();
}

/**
Expand Down Expand Up @@ -157,12 +135,15 @@ public function stream_open($uri, $mode, $options, &$opened_path) {
$this->handle = fopen('php://memory', 'w+b');
// If file exists, decrypt and load it into memory.
if (file_exists($path)) {
$raw_file = file_get_contents($path);
$decrypted_file = $this->decrypt($raw_file, $encryption_profile);
$this->setFileInfo($decrypted_file, $uri);

// Write to memory.
fwrite($this->handle, $decrypted_file);
$encrypted_resource = fopen($path, 'r');
$this->getStreamEncryption($encryption_profile)->decrypt($encrypted_resource, $this->handle, $encryption_profile);
fclose($encrypted_resource);
rewind($this->handle);

// @todo What do we do with this ...
// $this->setFileInfo($decrypted_file, $uri);
}
// Set $opened_path.
if ((bool) $this->handle && $options & STREAM_USE_PATH) {
Expand Down Expand Up @@ -202,10 +183,14 @@ public function stream_close() {

// Encrypt file and save.
rewind($this->handle);
$file_contents = stream_get_contents($this->handle);
file_put_contents($this->getLocalPath(), $this->encrypt($file_contents, $encryption_profile));
// Store important file info.
$this->setFileInfo($file_contents, $this->uri);

$output_resource = fopen($this->getLocalPath(), 'w+');
$this->getStreamEncryption($encryption_profile)->encrypt($this->handle, $output_resource, $encryption_profile);
fclose($output_resource);

// @todo what do we do with this.
// $this->setFileInfo($file_contents, $this->uri);

// Close handle and reset manual key.
fclose($this->handle);
}
Expand Down