Created
March 17, 2017 19:50
-
-
Save atwellpub/155e6a3743d0ba6e4c0a435540cc48ef to your computer and use it in GitHub Desktop.
encode / decode PHP array for URL travel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Encodes data into an unsubscribe token | |
* @param ARRAY $params contains: lead_id (INT ), list_ids (MIXED), email_id (INT) | |
* @return INT $token | |
*/ | |
public static function encode_unsubscribe_token( $params ) { | |
if (isset($params['doing_wp_cron'])) { | |
unset($params['doing_wp_cron']); | |
} | |
$json = json_encode($params); | |
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); | |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); | |
$encrypted_string = | |
base64_encode( | |
trim( | |
mcrypt_encrypt( | |
MCRYPT_RIJNDAEL_256, substr( SECURE_AUTH_KEY , 0 , 16 ) , $json, MCRYPT_MODE_ECB, $iv | |
) | |
) | |
); | |
$decode_test = self::decode_unsubscribe_token($encrypted_string); | |
return str_replace(array('+', '/', '='), array('-', '_', '^'), $encrypted_string); | |
} | |
/** | |
* Decodes unsubscribe encoded reader id into a lead id | |
* @param STRING $reader_id Encoded lead id. | |
* @return ARRAY $unsubscribe array of unsubscribe data | |
*/ | |
public static function decode_unsubscribe_token( $token ) { | |
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); | |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); | |
$decrypted_string = | |
trim( | |
mcrypt_decrypt( | |
MCRYPT_RIJNDAEL_256 , substr( SECURE_AUTH_KEY , 0 , 16 ) , base64_decode( str_replace(array('-', '_', '^'), array('+', '/', '='), $token ) ) , MCRYPT_MODE_ECB, $iv | |
) | |
); | |
return json_decode($decrypted_string , true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment