webform_encrypt.module 3.5 KB
Newer Older
Jake Bell's avatar
Jake Bell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?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.
39
    $form['#submit'][] = '_webform_encrypt_component_save';
Jake Bell's avatar
Jake Bell committed
40 41 42 43 44 45 46 47 48
  }

}

/**
 * Submit callback to save form component settings.
 */
function _webform_encrypt_component_save($form, $form_state) {
  $record = new StdClass();
49
  $record->nid = $form_state['values']['nid'];
Jake Bell's avatar
Jake Bell committed
50 51 52 53
  $record->cid = $form_state['values']['cid'];
  foreach ($form_state['values']['encryption'] as $key => $value) {
    $record->$key = $value;
  }
54
  drupal_write_record('webform_encrypt', $record);
Jake Bell's avatar
Jake Bell committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
}

/**
 * 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'))
72 73
                  ->condition('we.nid', $node->nid)                  
                  ->condition('we.cid', $component_ids, 'IN')
Jake Bell's avatar
Jake Bell committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                  ->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>');
      }
    }
  }
}