Commit a4b0e6f8 authored by Jake Bell's avatar Jake Bell

Initial commit.

parents
name = Webform Encrypt
description = Provides encryption for webform components
core = 7.x
package = Webform
files[] = webform_encrypt.module
files[] = webform_encrypt.install
dependencies[] = aes
dependencies[] = webform
<?php
/**
* Implementation of hook_schema()
*/
function webform_encrypt_schema() {
$schema['webform_encrypt'] = array(
'description' => 'Form metadata',
'fields' => array(
'cid' => array(
'description' => 'The component ID from webform',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'encrypt' => array(
'description' => 'Whether to encrypt the value',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
),
),
'unique keys' => array(
'cid' => array('cid'),
),
);
return $schema;
}
/**
* Implementation of hook_uninstall().
*/
function webform_encrypt_uninstall() {
drupal_uninstall_schema('webform_encrypt');
}
<?php
/**
* Implementation of hook_permission().
*/
function webform_encrypt_permission() {
return array(
'view encrypted values' => array(
'title' => t('View Encrypted Values in Webform Results'),
'description' => t('Users that do not have this permission will see placeholder text.'),
),
);
}
/**
* Implementation of hook_form_alter()
*/
function webform_encrypt_form_alter(&$form, $form_state, $form_id) {
// Add our fields to the component add/edit form.
if ($form_id == 'webform_component_edit_form') {
$profile = db_query('SELECT * FROM {webform_encrypt} WHERE cid = ?', array($form['cid']['#value']))->fetchAssoc();
// Add settings for security.
$form['encryption'] = array(
'#type' => 'fieldset',
'#title' => t('Encryption'),
'#tree' => TRUE,
);
$form['encryption']['encrypt'] = array(
'#type' => 'checkbox',
'#title' => t('Encrypt this field\'s value'),
'#description' => t('!link to edit encryption settings.', array('!link' => l('Click here', 'admin/settings/aes'))),
'#default_value' => isset($profile['encrypt']) ? $profile['encrypt'] : 0,
);
// Add new submit handler to save our config data.
array_unshift($form['#submit'], '_webform_encrypt_component_save');
}
}
/**
* Submit callback to save form component settings.
*/
function _webform_encrypt_component_save($form, $form_state) {
$record = new StdClass();
$record->cid = $form_state['values']['cid'];
foreach ($form_state['values']['encryption'] as $key => $value) {
$record->$key = $value;
}
drupal_write_record('webform_encrypt', $record, 'cid');
}
/**
* Implementation of hook_node_load().
* Adding our extra data in to the webform component.
*/
function webform_encrypt_node_load($nodes, $types) {
// Abort if none of the nodes are webforms.
if (!in_array('webform', $types)) {
return;
}
// Otherwise, let's loop through the nodes.
foreach ($nodes as $nid => $node) {
$component_ids = array_keys($node->webform['components']);
$encrypt = db_select('webform_encrypt', 'we')
->fields('we', array('cid', 'encrypt'))
->condition('cid', $component_ids, 'IN')
->execute()
->fetchAllKeyed();
foreach ($node->webform['components'] as $cid => $component) {
$node->webform['components'][$cid]['encrypt'] = $encrypt[$cid];
}
}
}
/**
* Implementation of hook_webform_submission_presave().
* Encrypt the value if the component has been marked as such.
*/
function webform_encrypt_webform_submission_presave($node, &$submission) {
foreach ($submission->data as $cid => $entry) {
if ($node->webform['components'][$cid]['encrypt']) {
$submission->data[$cid]['value'][0] = aes_encrypt($entry['value'][0]);
}
}
}
/**
* Implementation of hook_webform_submission_render_alter().
* Decrypt values when displaying webform submissions.
*/
function webform_encrypt_webform_submission_render_alter(&$renderable) {
foreach ($renderable['#submission']->data as $cid => $entry) {
if ($renderable['#node']->webform['components'][$cid]['encrypt']) {
$form_key = $renderable['#node']->webform['components'][$cid]['form_key'];
if (user_access('view encrypted values')) {
$renderable[$form_key]['#value'] = aes_decrypt($entry['value'][0]);
} else {
$renderable[$form_key]['#value'] = t('<Value Encrypted>');
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment