You are here

class DrupalEncrypt in Backup and Migrate 8.4

Class DrupalEncrypt.

@package BackupMigrate\Drupal\Filter

Hierarchy

Expanded class hierarchy of DrupalEncrypt

File

src/Filter/DrupalEncrypt.php, line 19

Namespace

BackupMigrate\Drupal\Filter
View source
class DrupalEncrypt extends PluginBase implements FileProcessorInterface {
  use FileProcessorTrait;

  /**
   * {@inheritdoc}
   */
  public function configSchema($params = []) {
    $schema = [];

    // Backup configuration.
    if ($params['operation'] == 'backup' || $params['operation'] == 'restore') {
      if (class_exists('\\Defuse\\Crypto\\File')) {
        $schema['groups']['encrypt'] = [
          'title' => 'Backup Encryption',
        ];
        $schema['fields']['encrypt'] = [
          'group' => 'encrypt',
          'type' => 'boolean',
          'title' => $params['operation'] == 'backup' ? $this
            ->t('Encrypt File') : $this
            ->t('Decrypt file'),
          'description' => $this
            ->t('Password for encrypting / decrypting the file'),
        ];
        $schema['fields']['encrypt_password'] = [
          'group' => 'encrypt',
          'type' => 'password',
          'title' => $params['operation'] == 'backup' ? $this
            ->t('Encryption Password') : $this
            ->t('Decryption Password'),
        ];
      }
      else {
        drupal_set_message($this
          ->t('Please install the Defuse PHP-encryption library via Composer to be able to encrypt backup files.'), 'warning');
      }
    }
    return $schema;
  }

  /**
   * Get the default values for the plugin.
   *
   * @return \BackupMigrate\Core\Config\Config
   */
  public function configDefaults() {
    return new Config([
      'encrypt' => FALSE,
    ]);
  }
  protected function _encryptFile(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
    $path = drupal_realpath($from
      ->realpath());
    $out_path = drupal_realpath($to
      ->realpath());
    try {
      CryptoFile::encryptFileWithPassword($path, $out_path, $this
        ->confGet('encrypt_password'));
      $fileszc = filesize(drupal_realpath($to
        ->realpath()));
      $to
        ->setMeta('filesize', $fileszc);
      return TRUE;
    } catch (Exception $e) {
      return FALSE;
    }
  }
  protected function _decryptFile(BackupFileReadableInterface $from, BackupFileWritableInterface $to) {
    $path = drupal_realpath($from
      ->realpath());
    $out_path = drupal_realpath($to
      ->realpath());
    try {
      CryptoFile::decryptFileWithPassword($path, $out_path, $this
        ->confGet('encrypt_password'));
      return TRUE;
    } catch (Exception $e) {
      return FALSE;
    }
  }
  public function beforeRestore(BackupFileReadableInterface $file) {
    $type = $file
      ->getExtLast();
    if ($type == 'ssl' && $this
      ->confGet('encrypt')) {
      $out = $this
        ->getTempFileManager()
        ->popExt($file);
      $success = $this
        ->_decryptFile($file, $out);
      if ($out && $success) {
        return $out;
      }
    }
    return $file;
  }
  public function supportedOps() {
    return [
      'getFileTypes' => [],
      'backupSettings' => [],
      'afterBackup' => [
        'weight' => 1000,
      ],
      'beforeRestore' => [
        'weight' => -1000,
      ],
    ];
  }
  public function afterBackup(BackupFileReadableInterface $file) {
    if ($this
      ->confGet('encrypt')) {
      $out = $this
        ->getTempFileManager()
        ->pushExt($file, 'ssl');
      $success = $this
        ->_encryptFile($file, $out);
      if ($out && $success) {
        return $out;
      }
    }
    return $file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableTrait::$config protected property The object's configuration object.
ConfigurableTrait::$init protected property The initial configuration. These configuration options can be overriden by the config options but will not be overwritten. If the object is re-configured after construction any missing configuration options will revert to these values.
ConfigurableTrait::confGet public function Get a specific value from the configuration.
ConfigurableTrait::config public function Get the configuration object for this item.
ConfigurableTrait::configErrors public function Get any validation errors in the config.
ConfigurableTrait::setConfig public function Set the configuration for all plugins. 1
ConfigurableTrait::__construct public function 2
DrupalEncrypt::afterBackup public function
DrupalEncrypt::beforeRestore public function
DrupalEncrypt::configDefaults public function Get the default values for the plugin. Overrides ConfigurableTrait::configDefaults
DrupalEncrypt::configSchema public function Get a default (blank) schema. Overrides ConfigurableTrait::configSchema
DrupalEncrypt::supportedOps public function Get a list of supported operations and their weight. Overrides PluginBase::supportedOps
DrupalEncrypt::_decryptFile protected function
DrupalEncrypt::_encryptFile protected function
FileProcessorTrait::$tempfilemanager protected property
FileProcessorTrait::alterMime public function Provide the file mime for the given file extension if known.
FileProcessorTrait::getTempFileManager public function Get the temp file manager.
FileProcessorTrait::setTempFileManager public function Inject the temp file manager.
PluginBase::opWeight public function What is the weight of the given operation for this plugin. Overrides PluginInterface::opWeight
PluginBase::supportsOp public function Does this plugin implement the given operation. Overrides PluginInterface::supportsOp
TranslatableTrait::$translator protected property
TranslatableTrait::setTranslator public function
TranslatableTrait::t public function Translate the given string if there is a translator service available.