Skip to content

Instantly share code, notes, and snippets.

@hiranthi
Last active November 21, 2019 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiranthi/ffcbae234edda3e08c6db13884c24450 to your computer and use it in GitHub Desktop.
Save hiranthi/ffcbae234edda3e08c6db13884c24450 to your computer and use it in GitHub Desktop.
New encryption with Gravity Forms
<?php
function my_custom_gravityforms_encrypt( $to_encrypt )
{
if ( '' === $to_encrypt ) return null;
if ( function_exists('openssl_encrypt') && function_exists('openssl_random_pseudo_bytes') )
{
$iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );
$encrypted = openssl_encrypt( $to_encrypt, 'aes-256-cbc', MY_CUSTOM_GF_ENCRYPTION_KEY, 0, $iv );
return base64_encode( $encrypted . '::' . $iv );
}
return base64_encode( $to_encrypt );
} // end my_custom_gravityforms_encrypt
function my_custom_gravityforms_decrypt( $to_decrypt )
{
if ( '' === $to_decrypt ) return null;
if ( function_exists('openssl_decrypt') )
{
@list( $encrypted_data, $iv ) = explode( '::', base64_decode( $to_decrypt ), 2 );
return openssl_decrypt( $encrypted_data, 'aes-256-cbc', MY_CUSTOM_GF_ENCRYPTION_KEY, 0, $iv );
}
return base64_decode( $to_decrypt );
} // end my_custom_gravityforms_decrypt
<?php
/**
* Encrypt all values for GF entries
**/
add_filter( 'gform_save_field_value', 'gf_custom_save_field_value', 10, 4 );
function gf_custom_save_field_value( $value, $lead, $field, $form )
{
my_custom_gravityforms_encrypt( $value );
}
// end gf_custom_save_field_value
/**
* Decrypting based on date of implementation for encryption
**/
add_filter( 'gform_get_input_value', 'gf_custom_decode_field', 10, 4 );
function gf_custom_decrypt_field( $value, $entry, $field, $input_id )
{
# Add a check to see if a field has to be decrypted,
# because it's possible you're adding this after already having entries
$date_implemented = '2019-01-23 10:36:00'; # format: yyyy-mm-dd hh:mi:ss
$date_entry = rgar( $entry, 'date_created' );
# Entry is from after implementing encrypting entries
if ( strtotime( $date_entry ) >= $date_implemented )
return my_custom_gravityforms_decrypt( $value );
# Entry is from before implementing encrypting entries
return $value;
}
// end gf_custom_decrypt_field
/**
* Or: always decrypt
**/
add_filter( 'gform_get_input_value', 'gf_custom_decode_field', 10, 4 );
function gf_custom_decrypt_field( $value, $entry, $field, $input_id )
{
return my_custom_gravityforms_decrypt( $value );
}
// end gf_custom_decrypt_field
define('MY_CUSTOM_GF_ENCRYPTION_KEY','JuSt4fEwCuSt0McHarAc73RSf0rD3M0pUrP05e5!'); # do change it to your own custom key ;-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment