encryptfapi.module in Encrypt Form API 7.2
Main module file for Encrypt Form API.
File
encryptfapi.moduleView source
<?php
/**
* @file
*
* Main module file for Encrypt Form API.
*/
/**
* Implements hook_module_implements_alter().
*
* This moves the hook_form_alter() implementation to the bottom of the
* list so that other modules can alter forms first and the added
* validation function will be triggered last.
*/
function encryptfapi_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter' && isset($implementations['encryptfapi'])) {
$group = $implementations['encryptfapi'];
unset($implementations['encryptfapi']);
$implementations = array_merge($implementations, array(
'encryptfapi' => $group,
));
}
}
/**
* Implements hook_form_alter().
*
* Alters forms that are marked as having encrypted fields.
*/
function encryptfapi_form_alter(&$form, &$form_state, $form_id) {
// If the form has no encrypted fields, don't do anything.
if (!isset($form['#encrypted_fields']) || $form['#encrypted_fields'] != TRUE) {
return;
}
_encryptfapi_decrypt_element($form);
// Add the validate function.
$form['#validate'][] = '_encryptfapi_form_validate';
}
/**
* Form validation callback.
*/
function _encryptfapi_form_validate($form, &$form_state) {
_encryptfapi_encrypt_element($form, $form_state);
}
/**
* Helper function to recursively process an element for encryption.
*
* @param $element
* The element to process.
*/
function _encryptfapi_encrypt_element(&$element, &$form_state) {
$element_types =& drupal_static(__FUNCTION__);
if (!isset($element_types)) {
$element_types = _encryptfapi_supported_element_types();
}
foreach (element_children($element) as $key) {
$child =& $element[$key];
if (is_array($child)) {
_encryptfapi_encrypt_element($child, $form_state);
}
// If the element is not marked for encryption or if it's not a type
// supported by this module, skip it.
if (!isset($child['#encrypt']) || $child['#encrypt'] != TRUE || !in_array($child['#type'], $element_types)) {
continue;
}
$value = drupal_array_get_nested_value($form_state['values'], $child['#parents']);
// If the value is empty, skip it.
if (empty($value)) {
continue;
}
// Encrypt the value.
$value = _encryptfapi_encrypt_element_value($child, $value);
// If encryption failed, display an error.
if (empty($value)) {
form_error($child, t('The field %field could not be encrypted.', array(
'%field' => $child['#title'],
)));
}
else {
drupal_array_set_nested_value($form_state['values'], $child['#parents'], $value);
}
}
}
/**
* Helper function to encrypt an element's value.
*
* @param $element
* The element with the value to encrypt.
*
* @return string
* The encrypted value.
*/
function _encryptfapi_encrypt_element_value($element, $value) {
$encrypt = $element['#encrypt'];
// If the value is an array, serialize it.
if (is_array($value)) {
$value = serialize($value);
}
if (is_array($encrypt)) {
$config = isset($encrypt['config']) ? $encrypt['config'] : NULL;
$options = isset($encrypt['options']) ? $encrypt['options'] : NULL;
$encrypted_value = encrypt($value, $options, NULL, NULL, $config);
}
else {
$encrypted_value = encrypt($value);
}
return $encrypted_value;
}
/**
* Helper function to recursively process an element for decryption.
*
* @param $element
* The element to process.
*/
function _encryptfapi_decrypt_element(&$element) {
$element_types =& drupal_static(__FUNCTION__);
if (!isset($element_types)) {
$element_types = _encryptfapi_supported_element_types();
}
foreach (element_children($element) as $key) {
$child =& $element[$key];
if (is_array($child)) {
_encryptfapi_decrypt_element($child);
}
// If the element is not marked for encryption or if it's not a type
// supported by this module, skip it.
if (!isset($child['#encrypt']) || $child['#encrypt'] != TRUE || !in_array($child['#type'], $element_types)) {
continue;
}
// If the value is empty, skip it.
if (empty($child['#default_value'])) {
// If the field accepts multiple values, make sure that the value is
// an array.
if ($child['#type'] == 'checkboxes') {
$child['#default_value'] = array();
}
continue;
}
// If the element has already been decrypted, skip it.
if (isset($child['#decrypted']) && $child['#decrypted'] == TRUE) {
continue;
}
// If the value does not appear to be encrypted, skip it.
$unserialized = @unserialize($child['#default_value']);
if ($unserialized === FALSE || !isset($unserialized['method']) || !isset($unserialized['key_provider'])) {
continue;
}
$child['#default_value'] = decrypt($child['#default_value']);
// If the decrypted text is serialized, unserialize it.
$unserialized = @unserialize($child['#default_value']);
if ($unserialized !== FALSE) {
$child['#default_value'] = $unserialized;
}
$child['#decrypted'] = TRUE;
}
}
/**
* Helper function to define the element types that can be encrypted.
*/
function _encryptfapi_supported_element_types() {
return array(
'textfield',
'textarea',
'checkboxes',
'checkbox',
'radios',
'radio',
'select',
'password',
'password_confirm',
);
}
Functions
Name | Description |
---|---|
encryptfapi_form_alter | Implements hook_form_alter(). |
encryptfapi_module_implements_alter | Implements hook_module_implements_alter(). |
_encryptfapi_decrypt_element | Helper function to recursively process an element for decryption. |
_encryptfapi_encrypt_element | Helper function to recursively process an element for encryption. |
_encryptfapi_encrypt_element_value | Helper function to encrypt an element's value. |
_encryptfapi_form_validate | Form validation callback. |
_encryptfapi_supported_element_types | Helper function to define the element types that can be encrypted. |