3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class to handle encoding and decoding Variable Enveleope Return Path (VERP)
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Utils_Verp
{
21 * Mapping of reserved characters to hex codes
24 public static $encodeMap = [
36 * Mapping of hex codes to reserved characters
39 public static $decodeMap = [
51 * Encode the sender's address with the VERPed recipient.
53 * @param string $sender
54 * The address of the sender.
55 * @param string $recipient
56 * The address of the recipient.
59 * The VERP encoded address
61 public static function encode($sender, $recipient) {
62 preg_match('/(.+)\@([^\@]+)$/', $sender, $match);
66 preg_match('/(.+)\@([^\@]+)$/', $recipient, $match);
67 $rlocal = CRM_Utils_Array
::value(1, $match);
68 $rdomain = CRM_Utils_Array
::value(2, $match);
70 foreach (self
::$encodeMap as $char => $code) {
71 $rlocal = preg_replace('/' . preg_quote($char) . '/i', "+$code", $rlocal);
72 $rdomain = preg_replace('/' . preg_quote($char) . '/i', "+$code", $rdomain);
75 return "$slocal-$rlocal=$rdomain@$sdomain";
79 * Decode the address and return the sender and recipient as an array.
81 * @param string $address
82 * The address to be decoded.
85 * The tuple ($sender, $recipient)
87 public static function &verpdecode($address) {
88 preg_match('/^(.+)-([^=]+)=([^\@]+)\@(.+)/', $address, $match);
95 foreach (self
::$decodeMap as $code => $char) {
96 $rlocal = preg_replace("/+$code/i", $char, $rlocal);
97 $rdomain = preg_replace("/+$code/i", $char, $rdomain);
100 return ["$slocal@$sdomain", "$rlocal@$rdomain"];