You are here

abstract class BaseMethod in DRD Agent 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Crypt/BaseMethod.php \Drupal\drd_agent\Crypt\BaseMethod

Provides base encryption method.

Hierarchy

Expanded class hierarchy of BaseMethod

3 files declare their use of BaseMethod
MCrypt.php in src/Crypt/Method/MCrypt.php
OpenSSL.php in src/Crypt/Method/OpenSSL.php
TLS.php in src/Crypt/Method/TLS.php

File

src/Crypt/BaseMethod.php, line 13

Namespace

Drupal\drd_agent\Crypt
View source
abstract class BaseMethod implements BaseMethodInterface {

  /**
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * BaseMethod constructor.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   */
  public function __construct(ContainerInterface $container) {
    $this->container = $container;

    /** @noinspection NullPointerExceptionInspection */
    $this->logger = $container
      ->get('logger.factory')
      ->get('DRD Agent');
  }

  /**
   * Callback to encrypt and decrypt files.
   *
   * @param string $mode
   *   This is "-e" to encrypt or "-d" to decrypt.
   * @param string $in
   *   Input filename.
   * @param string $out
   *   Output filename.
   *
   * @return int
   *   Exit code of the openssl command.
   */
  private function cryptFileExecute($mode, $in, $out) : int {
    $output = [];
    $cmd = [
      'openssl',
      $this
        ->getCipher(),
      $mode,
      '-a',
      '-salt',
      '-in',
      $in,
      '-out',
      $out,
      '-k',
      base64_encode($this
        ->getPassword()),
    ];
    exec(implode(' ', $cmd), $output, $ret);
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function encryptFile($filename) : string {
    if ($this
      ->getCipher()) {
      exec('openssl version', $output, $ret);
      if ($ret === 0) {
        $in = $filename;
        $filename .= '.openssl';
        if ($this
          ->cryptFileExecute('-e', $in, $filename) !== 0) {
          $filename = $in;
        }
      }
    }
    return $filename;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseMethod::$container protected property
BaseMethod::$logger protected property
BaseMethod::cryptFileExecute private function Callback to encrypt and decrypt files.
BaseMethod::encryptFile public function Encrypt a file. Overrides BaseMethodInterface::encryptFile
BaseMethod::__construct public function BaseMethod constructor. 2
BaseMethodInterface::decrypt public function Decode, decrypt and unserialize arguments from the other end. 3
BaseMethodInterface::encrypt public function Encrypt and encode any list of arguments. 3
BaseMethodInterface::getCipher public function Get the selected cipher. 3
BaseMethodInterface::getCipherMethods public function Get a list of available cipher methods. 3
BaseMethodInterface::getIv public function Get an initialiation vector. 3
BaseMethodInterface::getLabel public function Get the crypt method label. 3
BaseMethodInterface::getPassword public function Get the password. 3
BaseMethodInterface::isAvailable public function Find out if the crypt method is available. 3