public function CreditSettingsForm::validateForm in Ubercart 8.4
Form validation handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormBase::validateForm
File
- payment/
uc_credit/ src/ Form/ CreditSettingsForm.php, line 42
Class
- CreditSettingsForm
- Credit card settings form.
Namespace
Drupal\uc_credit\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
/*
* Check that the encryption key directory has been specified, that it
* exists, and that it is readable.
*/
// Trim trailing whitespace and any trailing / or \ from the key path name.
$key_path = rtrim(trim($form_state
->getValue('uc_credit_encryption_path')), '/\\');
// Test to see if a path was entered.
if (empty($key_path)) {
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('Key path must be specified in security settings tab.'));
}
// Construct complete key file path.
$key_file = $key_path . '/' . UC_CREDIT_KEYFILE_NAME;
// Shortcut - test to see if we already have a usable key file.
if (file_exists($key_file)) {
if (is_readable($key_file)) {
// Test contents - must contain 32-character hexadecimal string.
$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 {
// Key file exists and is valid, save result of trim() back into
// $form_state and proceed to submit handler.
$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.'));
}
}
// Check if directory exists and is writeable.
if (is_dir($key_path)) {
// The entered directory is valid and in need of a key file.
// Flag this condition for the submit handler.
$form_state
->setValue('update_cc_encrypt_dir', TRUE);
// Can we open for writing?
$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 {
// Can we actually write?
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 {
// Can we read now?
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 {
// Directory doesn't exist.
$form_state
->setErrorByName('uc_credit_encryption_path', $this
->t('You have specified a non-existent directory.'));
}
// If validation succeeds, save result of trim() back into $form_state.
$form_state
->setValue('uc_credit_encryption_path', $key_path);
}