diff --git a/webform_encrypt.module b/webform_encrypt.module index ca3e8be36afef1d4c6e81b97f7678cc2eba993ce..4a6d45c6523b57e224914fd6d4d60a468b9918e7 100644 --- a/webform_encrypt.module +++ b/webform_encrypt.module @@ -18,48 +18,57 @@ function webform_encrypt_permission() { } /** - * Implementation of hook_form_alter() + * Implementation of hook_form_FORM_ID_alter(). */ -function webform_encrypt_form_alter(&$form, $form_state, $form_id) { +function webform_encrypt_form_webform_admin_settings_alter(&$form, $form_state) { // Add our config options to the webform settings page. - if ($form_id == 'webform_admin_settings') { - $form['encrypt'] = array( + $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('Webform Encrypt'), - '#collapsible' => TRUE, - '#collapsed' => FALSE, + '#title' => t('Encryption'), + '#tree' => TRUE, ); - $form['encrypt']['webform_encrypt_match_user'] = array( + $form['encryption']['encrypt'] = 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), + '#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, ); } +} - // Add our fields to the component add/edit form. - if ($form_id == 'webform_component_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('Click here', 'admin/config/system/encrypt'))), - '#default_value' => isset($component['extra']['encrypt']) ? $component['extra']['encrypt'] : 0, - ); - } - +/** + * Implementation of hook_form_alter(). + */ +function webform_encrypt_form_alter(&$form, &$form_state, $form_id) { + // When we are editing a webform submission, + if (strpos($form_id, 'webform_client_form_') === 0) { + _webform_encrypt_decrypt_nested_values($form['submitted']); } } @@ -80,8 +89,7 @@ function webform_encrypt_webform_component_presave(&$component) { */ function webform_encrypt_webform_submission_presave($node, &$submission) { foreach ($submission->data as $cid => $entry) { - if (isset($node->webform['components'][$cid]['extra']['encrypt']) && - $node->webform['components'][$cid]['extra']['encrypt']) { + if (!empty($node->webform['components'][$cid]['extra']['encrypt'])) { $submission->data[$cid]['value'][0] = encrypt($entry['value'][0], array('base64' => TRUE)); } } @@ -94,7 +102,7 @@ function webform_encrypt_webform_submission_presave($node, &$submission) { function webform_encrypt_webform_submission_render_alter(&$renderable) { // First, determine if 1) if we are dealing with an email or a page view, and 2) if user matching // is enabled. - if (variable_get('webform_encrypt_match_user', 0) && !empty($renderable['#email'])) { + if (!empty($renderable['#email']) && variable_get('webform_encrypt_match_user', 0)) { // 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; @@ -104,8 +112,7 @@ function webform_encrypt_webform_submission_render_alter(&$renderable) { // Next, we loop through components and decrypt as necessary. foreach ($renderable['#submission']->data as $cid => $entry) { - if (isset($renderable['#node']->webform['components'][$cid]['extra']['encrypt']) && - $renderable['#node']->webform['components'][$cid]['extra']['encrypt']) { + if (!empty($renderable['#node']->webform['components'][$cid]['extra']['encrypt'])) { $form_key = $renderable['#node']->webform['components'][$cid]['form_key']; if (user_access('view encrypted values', $account)) { $renderable[$form_key]['#value'] = decrypt($entry['value'][0], array('base64' => TRUE)); @@ -115,3 +122,16 @@ function webform_encrypt_webform_submission_render_alter(&$renderable) { } } } + +/** + * Helper function to recursively decrypt values in a webform structure. + */ +function _webform_encrypt_decrypt_nested_values(&$element) { + foreach (element_children($element) as $name) { + $component = &$element[$name]; + if (!empty($component['#webform_component']['extra']['encrypt'])) { + $component['#default_value'] = decrypt($component['#default_value'], array('base64' => TRUE)); + } + _webform_encrypt_decrypt_nested_values($component); + } +}