View source
<?php
namespace Drupal\uc_credit\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class CreditSettingsForm extends ConfigFormBase {
public function getFormId() {
return 'uc_credit_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('uc_credit.settings');
$form['cc_security']['uc_credit_encryption_path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Encryption key directory'),
'#description' => $this
->t('The card type, expiration date and last four digits of the card number are encrypted and stored temporarily while the customer is in the process of checking out.<br /><b>You must enable encryption</b> by following the <a href=":url">encryption instructions</a> in order to accept credit card payments.<br />In short, you must enter the path of a directory outside of your document root where the encryption key may be stored.<br />Relative paths will be resolved relative to the Drupal installation directory.<br />Once this directory is set, you should not change it.', [
':url' => Url::fromUri('https://www.drupal.org/node/1309226')
->toString(),
]),
'#default_value' => uc_credit_encryption_key() ? $config
->get('encryption_path') : $this
->t('Not configured.'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$key_path = rtrim(trim($form_state
->getValue('uc_credit_encryption_path')), '/\\');
if (empty($key_path)) {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Key path must be specified in security settings tab.'));
}
$key_file = $key_path . '/' . UC_CREDIT_KEYFILE_NAME;
if (file_exists($key_file)) {
if (is_readable($key_file)) {
$key = uc_credit_encryption_key();
if ($key) {
if (!preg_match("([0-9a-fA-F]{32})", $key)) {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Key file already exists in directory, but it contains an invalid key.'));
}
else {
$form_state
->setValue('uc_credit_encryption_path', $key_path);
return;
}
}
}
else {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Key file already exists in directory, but is not readable. Please verify the file permissions.'));
}
}
if (is_dir($key_path)) {
$form_state
->setValue('update_cc_encrypt_dir', TRUE);
$file = @fopen($key_path . '/encrypt.test', 'w');
if ($file === FALSE) {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Cannot write to directory, please verify the directory permissions.'));
$form_state
->setValue('update_cc_encrypt_dir', FALSE);
}
else {
if (@fwrite($file, '0123456789') === FALSE) {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Cannot write to directory, please verify the directory permissions.'));
$form_state
->setValue('update_cc_encrypt_dir', FALSE);
fclose($file);
}
else {
fclose($file);
$file = @fopen($key_path . '/encrypt.test', 'r');
if ($file === FALSE) {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Cannot read from directory, please verify the directory permissions.'));
$form_state
->setValue('update_cc_encrypt_dir', FALSE);
}
else {
fclose($file);
}
}
unlink($key_path . '/encrypt.test');
}
}
else {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('You have specified a non-existent directory.'));
}
$form_state
->setValue('uc_credit_encryption_path', $key_path);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
if ($form_state
->getValue('update_cc_encrypt_dir')) {
$key_path = $form_state
->getValue('uc_credit_encryption_path');
$key_file = $key_path . '/' . UC_CREDIT_KEYFILE_NAME;
if (!file_exists($key_file)) {
if (!($file = fopen($key_file, 'wb'))) {
$this
->messenger()
->addError($this
->t('Credit card encryption key file creation failed for file @file. Check your filepath settings and directory permissions.', [
'@file' => $key_file,
]));
$this
->logger('uc_credit')
->error('Credit card encryption key file creation failed for file @file. Check your filepath settings and directory permissions.', [
'@file' => $key_file,
]);
}
else {
fwrite($file, md5(\Drupal::csrfToken()
->get(serialize($_REQUEST) . serialize($_SERVER) . REQUEST_TIME)));
fclose($file);
$this
->messenger()
->addMessage($this
->t('Credit card encryption key file generated. Card data will now be encrypted.'));
$this
->logger('uc_credit')
->notice('Credit card encryption key file generated. Card data will now be encrypted.');
}
}
}
$this
->config('uc_credit.settings')
->set('encryption_path', $form_state
->getValue('uc_credit_encryption_path'))
->save();
}
protected function getEditableConfigNames() {
return [
'uc_credit.settings',
];
}
}