You are here

webform_encrypt.module in Webform Encrypt 7

Same filename and directory in other branches
  1. 8 webform_encrypt.module
  2. 6 webform_encrypt.module

Main module file for the Webform Encrypt module.

File

webform_encrypt.module
View source
<?php

/**
 * @file
 * Main module file for the Webform Encrypt module.
 */

/**
 * 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_FORM_ID_alter().
 */
function webform_encrypt_form_webform_admin_settings_alter(&$form, $form_state) {

  // Add our config options to the webform settings page.
  $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(
      '#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(t('Click here'), 'admin/config/system/encrypt'),
      )),
      '#default_value' => isset($component['extra']['encrypt']) ? $component['extra']['encrypt'] : 0,
    );
  }
}

/**
 * Implementation of hook_webform_component_presave().
 * Save encryption settings for a component.
 */
function webform_encrypt_webform_component_presave(&$component) {
  if (!empty($component['encryption'])) {
    $component['extra'] = array_merge($component['extra'], $component['encryption']);
    unset($component['encryption']);
    if ($component['extra']['encrypt']) {
      webform_encrypt_encrypt_component_data($component['nid'], $component['cid'], $component['extra']);
    }
    else {
      webform_encrypt_decrypt_component_data($component['nid'], $component['cid'], $component['extra']);
    }
  }
}

/**
 * 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 (!empty($node->webform['components'][$cid]['extra']['encrypt'])) {
      foreach ($submission->data[$cid] as $delta => $value) {
        $submission->data[$cid][$delta] = encrypt($entry[$delta], array(
          'base64' => TRUE,
        ));
      }
    }
  }
}

/**
 * Implementation of hook_webform_submission_load().
 * Decrypt values if encrypted
 */
function webform_encrypt_webform_submission_load($submissions) {
  foreach ($submissions as $submission) {
    $node = node_load($submission->nid);
    foreach ($submission->data as $cid => $entry) {
      if (!empty($node->webform['components'][$cid]['extra']['encrypt'])) {
        foreach ($submission->data[$cid] as $delta => $value) {
          if (!empty($entry[$delta]) && @unserialize($entry[$delta]) !== FALSE) {
            $submission->data[$cid][$delta] = user_access('view encrypted values') ? decrypt($entry[$delta], array(
              'base64' => TRUE,
            )) : t('[Value Encrypted]');
          }
        }
      }
    }
  }
}

/**
 * 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) {
          if (!empty($value)) {
            $value = user_access('view encrypted values') ? decrypt($value, array(
              'base64' => TRUE,
            )) : t('[Value Encrypted]');
          }
        }
      }
    }
  }
}

/**
 * Encrypt all non-encrypted data of a component.
 */
function webform_encrypt_encrypt_component_data($nid = NULL, $cid = NULL, $extra = array()) {
  $results = db_query('SELECT nid, cid, extra FROM {webform_component} where nid = :nid AND cid = :cid', array(
    ':nid' => $nid,
    ':cid' => $cid,
  ))
    ->fetchAll();
  foreach ($results as $row) {
    $components[$row->nid . ':' . $row->cid] = unserialize($row->extra);
  }
  $data = db_query('SELECT nid, sid, cid, data FROM {webform_submitted_data} where nid = :nid AND cid = :cid', array(
    ':nid' => $nid,
    ':cid' => $cid,
  ))
    ->fetchAll();
  foreach ($data as $row) {
    $key = $row->nid . ':' . $row->cid;
    if (isset($components[$key]['encrypt']) && !$components[$key]['encrypt']) {
      db_update('webform_submitted_data')
        ->fields(array(
        'data' => encrypt($row->data, array(
          'base64' => TRUE,
        )),
      ))
        ->condition('nid', $row->nid)
        ->condition('sid', $row->sid)
        ->condition('cid', $row->cid)
        ->execute();
    }
  }
}

/**
 * Decrypt all encrypted data of a component.
 */
function webform_encrypt_decrypt_component_data($nid = NULL, $cid = NULL, $extra = array()) {
  $results = db_query('SELECT nid, cid, extra FROM {webform_component} where nid = :nid AND cid = :cid', array(
    ':nid' => $nid,
    ':cid' => $cid,
  ))
    ->fetchAll();
  foreach ($results as $row) {
    $components[$row->nid . ':' . $row->cid] = unserialize($row->extra);
  }
  $data = db_query('SELECT nid, sid, cid, data FROM {webform_submitted_data} where nid = :nid AND cid = :cid', array(
    ':nid' => $nid,
    ':cid' => $cid,
  ))
    ->fetchAll();
  foreach ($data as $row) {
    $key = $row->nid . ':' . $row->cid;
    if (!empty($components[$key]['encrypt']) && is_array(@unserialize($row->data))) {

      //echo "<pre>";print_R(decrypt($row->data, array('base64' => TRUE)));exit;
      db_update('webform_submitted_data')
        ->fields(array(
        'data' => decrypt($row->data, array(
          'base64' => TRUE,
        )),
      ))
        ->condition('nid', $row->nid)
        ->condition('sid', $row->sid)
        ->condition('cid', $row->cid)
        ->execute();
    }
  }
}

Functions

Namesort descending Description
webform_encrypt_decrypt_component_data Decrypt all encrypted data of a component.
webform_encrypt_encrypt_component_data Encrypt all non-encrypted data of a component.
webform_encrypt_form_webform_admin_settings_alter Implementation of hook_form_FORM_ID_alter().
webform_encrypt_form_webform_component_edit_form_alter Implementation of hook_form_FORM_ID_alter().
webform_encrypt_permission Implementation of hook_permission().
webform_encrypt_preprocess_webform_results_table Preprocess for theme('webform_results_table').
webform_encrypt_webform_component_presave Implementation of hook_webform_component_presave(). Save encryption settings for a component.
webform_encrypt_webform_submission_load Implementation of hook_webform_submission_load(). Decrypt values if encrypted
webform_encrypt_webform_submission_presave Implementation of hook_webform_submission_presave(). Encrypt the value if the component has been marked as such.