Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * Class to handle encoding and decoding Variable Enveleope Return Path (VERP) | |
30 | * headers. | |
31 | * | |
32 | * @package CRM | |
6b83d5bd | 33 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
34 | */ |
35 | class CRM_Utils_Verp { | |
6714d8d2 SL |
36 | /** |
37 | * Mapping of reserved characters to hex codes | |
38 | * @var array | |
39 | */ | |
40 | public static $encodeMap = [ | |
6a488035 TO |
41 | '+' => '2B', |
42 | '@' => '40', | |
43 | ':' => '3A', | |
44 | '%' => '25', | |
45 | '!' => '21', | |
46 | '-' => '2D', | |
47 | '[' => '5B', | |
48 | ']' => '5D', | |
be2fb01f | 49 | ]; |
6a488035 | 50 | |
6714d8d2 SL |
51 | /** |
52 | * Mapping of hex codes to reserved characters | |
53 | * @var array | |
54 | */ | |
55 | public static $decodeMap = [ | |
6a488035 TO |
56 | '40' => '@', |
57 | '3A' => ':', | |
58 | '25' => '%', | |
59 | '21' => '!', | |
60 | '2D' => '-', | |
61 | '5B' => '[', | |
62 | '5D' => ']', | |
63 | '2B' => '+', | |
be2fb01f | 64 | ]; |
6a488035 TO |
65 | |
66 | /** | |
67 | * Encode the sender's address with the VERPed recipient. | |
68 | * | |
77855840 TO |
69 | * @param string $sender |
70 | * The address of the sender. | |
71 | * @param string $recipient | |
72 | * The address of the recipient. | |
6a488035 | 73 | * |
a6c01b45 CW |
74 | * @return string |
75 | * The VERP encoded address | |
6a488035 TO |
76 | */ |
77 | public static function encode($sender, $recipient) { | |
78 | preg_match('/(.+)\@([^\@]+)$/', $sender, $match); | |
79 | $slocal = $match[1]; | |
80 | $sdomain = $match[2]; | |
81 | ||
82 | preg_match('/(.+)\@([^\@]+)$/', $recipient, $match); | |
83 | $rlocal = CRM_Utils_Array::value(1, $match); | |
84 | $rdomain = CRM_Utils_Array::value(2, $match); | |
85 | ||
86 | foreach (self::$encodeMap as $char => $code) { | |
87 | $rlocal = preg_replace('/' . preg_quote($char) . '/i', "+$code", $rlocal); | |
88 | $rdomain = preg_replace('/' . preg_quote($char) . '/i', "+$code", $rdomain); | |
89 | } | |
90 | ||
91 | return "$slocal-$rlocal=$rdomain@$sdomain"; | |
92 | } | |
93 | ||
94 | /** | |
fe482240 | 95 | * Decode the address and return the sender and recipient as an array. |
6a488035 | 96 | * |
77855840 TO |
97 | * @param string $address |
98 | * The address to be decoded. | |
6a488035 | 99 | * |
a6c01b45 CW |
100 | * @return array |
101 | * The tuple ($sender, $recipient) | |
6a488035 TO |
102 | */ |
103 | public static function &verpdecode($address) { | |
104 | preg_match('/^(.+)-([^=]+)=([^\@]+)\@(.+)/', $address, $match); | |
105 | ||
353ffa53 TO |
106 | $slocal = $match[1]; |
107 | $rlocal = $match[2]; | |
6a488035 TO |
108 | $rdomain = $match[3]; |
109 | $sdomain = $match[4]; | |
110 | ||
111 | foreach (self::$decodeMap as $code => $char) { | |
112 | $rlocal = preg_replace("/+$code/i", $char, $rlocal); | |
113 | $rdomain = preg_replace("/+$code/i", $char, $rdomain); | |
114 | } | |
115 | ||
be2fb01f | 116 | return ["$slocal@$sdomain", "$rlocal@$rdomain"]; |
6a488035 | 117 | } |
96025800 | 118 | |
6a488035 | 119 | } |