When downloading information, if the user has permissions to decrypt the data, the data should be decrypted in the results tab (e.g. when viewing the table or downloading)

Comments

mgladding’s picture

I am also interested in this feature.

Thanks for the help!

origami’s picture

This is what I am using to fix the problem of encrypted values in downloads and table view--replace the function webform_encrypt_webform_submission_render_alter with a function using the submission load hook instead:

/**
 * Implementation of hook_webform_submission_load().
 * Decrypt values when displaying webform submissions.
 */

function webform_encrypt_webform_submission_load(&$submissions){
	global $user;
	$account = user_load($user->uid);

	foreach ($submissions as $sid => $submission) {
  		foreach ($submission->data as $cid => $entry) {
			$node = node_load($submission->nid);
	 		if (isset($node->webform['components'][$cid]['extra']['encrypt']) &&
        		$node->webform['components'][$cid]['extra']['encrypt']) {
    	  		if (user_access('view encrypted values', $account)) {
   	     			$submission->data[$cid]['value'][0] = decrypt($entry['value'][0], array('base64' => TRUE));
   	   			} else {
     	   			$submission->data[$cid]['value'][0] = t('[Value Encrypted]');
   	   			}
			}
    	        }
  	}
}
robbyclark’s picture

Thanks, patched the module with this code and it has saved my bacon on a project today. Tested and it seems to work well. Will this be commited as an official patch to the module? it would make sense!

theunraveler’s picture

Thanks for patching. I'm looking in to it currently.

bernman’s picture

To make this work for multiple entries in a select, we need to iterate though the values array. My suggestion would be:

/**
* Implementation of hook_webform_submission_load().
* Decrypt values when displaying webform submissions.
*/
function webform_encrypt_webform_submission_load(&$submissions) {

  global $user;

  $viewable = user_access('view encrypted values', user_load($user->uid));

  foreach ($submissions as $sid => $submission) {
    foreach ($submission->data as $cid => $entry) {
      $node = node_load($submission->nid);
      if (isset($node->webform['components'][$cid]['extra']['encrypt']) &&
        $node->webform['components'][$cid]['extra']['encrypt']) {
        for ($i = 0; $i < sizeof($entry['value']); $i++) {
          if (isset($entry['value'][$i])) {
            if ($viewable) {
              $submission->data[$cid]['value'][$i] = decrypt($entry['value'][$i], array('base64' => TRUE));
            }
            else {
              $submission->data[$cid]['value'][$i] = t('[Value Encrypted]');
            }
          }
        }
      }
    }
  }
}
apb1991’s picture

This code works great! Thanks!

theunraveler’s picture

Status: Active » Closed (duplicate)

I'm moving this to a more comprehensive discussion in #1433524: Webform Encrypt displays encrypted data where it shouldn't.

swara05’s picture

Many thanks...I am also looking for the same solution. After replacing function i can see the original value. But the problem raised after replacing the function and when see the results-submissions in my webform ---Notice: unserialize() [function.unserialize]: Error at offset 0 of 7 bytes in _encrypt_decrypt() (line...\encrypt\includes\encrypt.crypt.inc) -- i seen this notice as many times occurred on that page..so what is going wrong here i cant understand...

Thank you so much for that code.I really appreciate that.

ydahi’s picture

Seeing the same notice as swara10:

Notice: unserialize(): Error at offset 0 of 4 bytes in _encrypt_decrypt() (line 175 of /var/vhosts/spg/sites/all/modules/encrypt/includes/encrypt.crypt.inc).

Encryption/Decryption works perfectly thanks to the code provided above.

Would be grateful to see the notices addressed as well.

ydahi’s picture

After some mucking around, I figured out the issue causing the "unserialized()" notice.

The submissions previous to the encryption of the field(s) were causing the notice, as the module did not have anything to decrypt for those submissions.

If you clear your submissions and start fresh, the notices should not appear.

Alternatively, you can set "Logging and Errors" to only show "Errors and warnings" in admin/config/development/logging, as the notices are not detrimental to your site. However, this means that the submissions prior to encryption are not, well, encrypted.