webform_encrypt.module 4.22 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 21 22 23
/**
 * 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) {
Jake Bell's avatar
Jake Bell committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  // Add our config options to the webform settings page.
  if ($form_id == 'webform_admin_settings') {
    $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),
    );
  }

Jake Bell's avatar
Jake Bell committed
40 41 42
  // Add our fields to the component add/edit form.
  if ($form_id == 'webform_component_edit_form') {

43
    $component = $form_state['build_info']['args'][1];
Jake Bell's avatar
Jake Bell committed
44

45 46 47
    // 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)) {
Jake Bell's avatar
Jake Bell committed
48

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

  }
}

/**
67 68
 * Implementation of hook_webform_component_presave().
 * Save encryption settings for a component.
Jake Bell's avatar
Jake Bell committed
69
 */
70
function webform_encrypt_webform_component_presave(&$component) {
71 72 73 74
  if (!empty($component['encryption'])) {
    $component['extra'] = array_merge($component['extra'], $component['encryption']);
    unset($component['encryption']);
  }
Jake Bell's avatar
Jake Bell committed
75 76 77 78 79 80 81 82
}

/**
 * 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) {
83 84 85
    if (isset($node->webform['components'][$cid]['extra']['encrypt']) &&
        $node->webform['components'][$cid]['extra']['encrypt']) {
      $submission->data[$cid]['value'][0] = encrypt($entry['value'][0], array('base64' => TRUE));
Jake Bell's avatar
Jake Bell committed
86 87 88 89 90 91 92 93 94
    }
  }
}

/**
 * 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
95 96
  // First, determine if 1) if we are dealing with an email or a page view, and 2) if user matching
  // is enabled.
97
  if (variable_get('webform_encrypt_match_user', 0) && !empty($renderable['#email'])) {
Jake Bell's avatar
Jake Bell committed
98 99 100 101 102 103 104 105
    // 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.
Jake Bell's avatar
Jake Bell committed
106
  foreach ($renderable['#submission']->data as $cid => $entry) {
107 108
    if (isset($renderable['#node']->webform['components'][$cid]['extra']['encrypt']) &&
        $renderable['#node']->webform['components'][$cid]['extra']['encrypt']) {
Jake Bell's avatar
Jake Bell committed
109
      $form_key = $renderable['#node']->webform['components'][$cid]['form_key'];
Jake Bell's avatar
Jake Bell committed
110
      if (user_access('view encrypted values', $account)) {
111
        $renderable[$form_key]['#value'] = decrypt($entry['value'][0], array('base64' => TRUE));
Jake Bell's avatar
Jake Bell committed
112
      } else {
113
        $renderable[$form_key]['#value'] = t('[Value Encrypted]');
Jake Bell's avatar
Jake Bell committed
114 115 116 117
      }
    }
  }
}