webform_encrypt.module 5.66 KB
Newer Older
Jake Bell's avatar
Jake Bell committed
1 2
<?php

Jake Bell's avatar
Jake Bell committed
3 4 5 6 7
/**
 * @file
 * Main module file for the Webform Encrypt module.
 */

Jake Bell's avatar
Jake Bell committed
8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * 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.'),
    ),
  );
}

/**
21
 * Implementation of hook_form_FORM_ID_alter().
Jake Bell's avatar
Jake Bell committed
22
 */
23
function webform_encrypt_form_webform_admin_settings_alter(&$form, $form_state) {
Jake Bell's avatar
Jake Bell committed
24
  // Add our config options to the webform settings page.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  $form['encrypt'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform Encrypt'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['encrypt']['webform_encrypt_match_user'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable email to user matching'),
    '#description' => t('If enabled, every time webform sends an email, it will attempt to find a user that matches the email address the mail will be sent to in order to correctly determine permissions.'),
    '#default_value' => variable_get('webform_encrypt_match_user', 0),
  );
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function webform_encrypt_form_webform_component_edit_form_alter(&$form, $form_state) {
  // Add our fields to the component add/edit form.
  $component = $form_state['build_info']['args'][1];

  // Exclude webform component types that don't make sense to encrypt.
  $excluded_types = array('fieldset', 'file', 'markup', 'pagebreak');
  if (!in_array($form['type']['#value'], $excluded_types)) {

    // Add settings for encryption.
    $form['encryption'] = array(
Jake Bell's avatar
Jake Bell committed
52
      '#type' => 'fieldset',
53 54
      '#title' => t('Encryption'),
      '#tree' => TRUE,
Jake Bell's avatar
Jake Bell committed
55
    );
56
    $form['encryption']['encrypt'] = array(
Jake Bell's avatar
Jake Bell committed
57
      '#type' => 'checkbox',
58 59 60
      '#title' => t('Encrypt this field\'s value'),
      '#description' => t('!link to edit encryption settings.', array('!link' => l('Click here', 'admin/config/system/encrypt'))),
      '#default_value' => isset($component['extra']['encrypt']) ? $component['extra']['encrypt'] : 0,
Jake Bell's avatar
Jake Bell committed
61 62
    );
  }
63
}
Jake Bell's avatar
Jake Bell committed
64

65 66 67 68 69
/**
 * Implementation of hook_form_alter().
 */
function webform_encrypt_form_alter(&$form, &$form_state, $form_id) {
  // When we are editing a webform submission, 
70
  if (strpos($form_id, 'webform_client_form_') === 0 && $form['details']['sid']['#value']) {
71
    _webform_encrypt_decrypt_nested_values($form['submitted']);
Jake Bell's avatar
Jake Bell committed
72 73 74 75
  }
}

/**
76 77
 * Implementation of hook_webform_component_presave().
 * Save encryption settings for a component.
Jake Bell's avatar
Jake Bell committed
78
 */
79
function webform_encrypt_webform_component_presave(&$component) {
80 81 82 83
  if (!empty($component['encryption'])) {
    $component['extra'] = array_merge($component['extra'], $component['encryption']);
    unset($component['encryption']);
  }
Jake Bell's avatar
Jake Bell committed
84 85 86 87 88 89 90 91
}

/**
 * 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) {
92
    if (!empty($node->webform['components'][$cid]['extra']['encrypt'])) {
93 94 95
      foreach ($submission->data[$cid]['value'] as $delta => $value) {
        $submission->data[$cid]['value'][$delta] = encrypt($entry['value'][$delta], array('base64' => TRUE));
      }
Jake Bell's avatar
Jake Bell committed
96 97 98 99 100 101 102 103 104
    }
  }
}

/**
 * Implementation of hook_webform_submission_render_alter().
 * Decrypt values when displaying webform submissions.
 */
function webform_encrypt_webform_submission_render_alter(&$renderable) {
Jake Bell's avatar
Jake Bell committed
105 106
  // First, determine if 1) if we are dealing with an email or a page view, and 2) if user matching
  // is enabled.
107
  if (!empty($renderable['#email']) && variable_get('webform_encrypt_match_user', 0)) {
Jake Bell's avatar
Jake Bell committed
108 109 110 111 112 113 114 115
    // If we are, then try to match a user to the email address we are sending to.
    $uid = db_query('SELECT uid FROM {users} WHERE mail = ?', array($renderable['#email']['email']))->fetchField();
    $account = $uid ? user_load($uid) : NULL;
  } else {
    $account = NULL;
  }

  // Next, we loop through components and decrypt as necessary.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  _webform_encrypt_decrypt_nested_values($renderable, '#value', array('account' => $account));
}

/**
 * Preprocess for theme('webform_results_table').
 *
 * Decrypt webform values in the table display.
 */
function webform_encrypt_preprocess_webform_results_table(&$vars) {
  foreach ($vars['submissions'] as $sid => &$submission) {
    foreach ($submission->data as $cid => &$item) {
      $component = $vars['components'][$cid];
      if (!empty($component['extra']['encrypt'])) {
        foreach ($item['value'] as &$value) {
          $value = user_access('view encrypted values') ? decrypt($value, array('base64' => TRUE)) : t('[Value Encrypted]');
        }
Jake Bell's avatar
Jake Bell committed
132 133 134 135
      }
    }
  }
}
136 137 138 139

/**
 * Helper function to recursively decrypt values in a webform structure.
 */
140 141 142 143 144
function _webform_encrypt_decrypt_nested_values(&$element, $key = '#default_value', $restrict = array()) {
  // Determine if we are checking access.
  $access = empty($restrict) ? TRUE : user_access('view encrypted values', $restrict['account']);

  // Loop through each item and decrypt the value.
145 146 147
  foreach (element_children($element) as $name) {
    $component = &$element[$name];
    if (!empty($component['#webform_component']['extra']['encrypt'])) {
148 149 150 151 152 153 154 155
      if (is_array($component[$key])) {
        foreach ($component[$key] as &$value) {
          $value = $access ? decrypt($value, array('base64' => TRUE)) : t('[Value Encrypted]');
        }
      }
      else {
        $component[$key] = $access ? decrypt($component[$key], array('base64' => TRUE)) : t('[Value Encrypted]');
      }
156
    }
157 158 159

    // Recurse if the current item has children.
    _webform_encrypt_decrypt_nested_values($component, $key, $restrict);
160 161
  }
}