function uc_credit_settings_form_validate in Ubercart 6.2
Same name and namespace in other branches
- 5 payment/uc_credit/uc_credit.module \uc_credit_settings_form_validate()
- 7.3 payment/uc_credit/uc_credit.admin.inc \uc_credit_settings_form_validate()
Validates the encryption key directory and key file.
Checks that the encryption key directory has been specified, that it exists, and is readable. and writeable so
See also
uc_credit_settings_form()
uc_credit_settings_form_submit()
1 string reference to 'uc_credit_settings_form_validate'
- uc_credit_form_alter in payment/
uc_credit/ uc_credit.module - Implements hook_form_alter().
File
- payment/
uc_credit/ uc_credit.module, line 894 - Defines the credit card payment method and hooks in payment gateways.
Code
function uc_credit_settings_form_validate($form, &$form_state) {
// Trim trailing whitespace and any trailing / or \ from the key path name.
$key_path = rtrim(trim($form_state['values']['uc_credit_encryption_path']), '/\\');
// Test to see if a path was entered.
if (empty($key_path)) {
form_set_error('uc_credit_encryption_path', 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_set_error('uc_credit_encryption_path', 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['values']['uc_credit_encryption_path'] = $key_path;
return;
}
}
}
else {
form_set_error('uc_credit_encryption_path', 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['uc_credit']['update_cc_encrypt_dir'] = TRUE;
// Can we open for writing?
$file = @fopen($key_path . '/encrypt.test', 'w');
if ($file === FALSE) {
form_set_error('uc_credit_encryption_path', t('Cannot write to directory, please verify the directory permissions.'));
unset($form_state['uc_credit']['update_cc_encrypt_dir']);
}
else {
// Can we actually write?
if (@fwrite($file, '0123456789') === FALSE) {
form_set_error('uc_credit_encryption_path', t('Cannot write to directory, please verify the directory permissions.'));
unset($form_state['uc_credit']['update_cc_encrypt_dir']);
fclose($file);
}
else {
// Can we read now?
fclose($file);
$file = @fopen($key_path . '/encrypt.test', 'r');
if ($file === FALSE) {
form_set_error('uc_credit_encryption_path', t('Cannot read from directory, please verify the directory permissions.'));
unset($form_state['uc_credit']['update_cc_encrypt_dir']);
}
else {
fclose($file);
}
}
unlink($key_path . '/encrypt.test');
}
}
else {
// Directory doesn't exist.
form_set_error('uc_credit_encryption_path', t('You have specified a non-existent directory.'));
}
// If validation succeeds, save result of trim() back into $form_state.
$form_state['values']['uc_credit_encryption_path'] = $key_path;
}