You are here

public static function AES::load_phpsec in AES encryption 8.2

Load PHPSecLib files.

Parameters

bool $display_errors: In case of problem with loading library, display errors and warnings.

Return value

bool loading result

3 calls to AES::load_phpsec()
AES::decrypt in src/AES.php
Decrypts a string of encrypted data.
AES::encrypt in src/AES.php
Encrypts a string.
AesAdminForm::buildForm in src/Form/AesAdminForm.php
Form constructor.

File

src/AES.php, line 35

Class

AES

Namespace

Drupal\aes

Code

public static function load_phpsec($display_errors = TRUE) {
  $library_error = FALSE;
  if (!\Drupal::moduleHandler()
    ->moduleExists('libraries')) {
    $library_error = t('The Libraries module should be enabled to use phpseclib.');
  }
  elseif (($phpsec_include_path = libraries_get_path('phpseclib')) == FALSE) {
    $library_error = t('The phpseclib package should be installed as a library.');
  }
  elseif (!file_exists($phpsec_include_path . '/Crypt/AES.php')) {
    $library_error = t('Cannot load /Crypt/AES.php from phpseclib root.');
  }
  elseif (!is_readable($phpsec_include_path . '/Crypt/AES.php')) {
    $library_error = t("It appears that phpseclib is installed in the right location but can't be read. Check that the permissions on the directory and its files allows for reading by the webserver.");
  }
  elseif (!function_exists('set_include_path')) {
    $library_error = t('The set_include_path function is inaccessible.');
  }
  if ($library_error) {
    if ($display_errors) {
      drupal_set_message($library_error, 'warning');
    }
    return FALSE;
  }

  // Include phpsec AES lib.
  set_include_path(get_include_path() . PATH_SEPARATOR . $phpsec_include_path);
  include_once 'Crypt/AES.php';
  if (class_exists('Crypt_AES')) {
    return TRUE;
  }
  if ($display_errors) {
    drupal_set_message('Including library error', 'error');
  }
  return FALSE;
}