Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 33 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
34 | */ |
35 | class CRM_Utils_Verp { | |
36 | /* Mapping of reserved characters to hex codes */ | |
37 | ||
38 | static $encodeMap = array( | |
39 | '+' => '2B', | |
40 | '@' => '40', | |
41 | ':' => '3A', | |
42 | '%' => '25', | |
43 | '!' => '21', | |
44 | '-' => '2D', | |
45 | '[' => '5B', | |
46 | ']' => '5D', | |
47 | ); | |
48 | ||
49 | /* Mapping of hex codes to reserved characters */ | |
50 | ||
51 | static $decodeMap = array( | |
52 | '40' => '@', | |
53 | '3A' => ':', | |
54 | '25' => '%', | |
55 | '21' => '!', | |
56 | '2D' => '-', | |
57 | '5B' => '[', | |
58 | '5D' => ']', | |
59 | '2B' => '+', | |
60 | ); | |
61 | ||
62 | /** | |
63 | * Encode the sender's address with the VERPed recipient. | |
64 | * | |
77855840 TO |
65 | * @param string $sender |
66 | * The address of the sender. | |
67 | * @param string $recipient | |
68 | * The address of the recipient. | |
6a488035 | 69 | * |
a6c01b45 CW |
70 | * @return string |
71 | * The VERP encoded address | |
6a488035 TO |
72 | */ |
73 | public static function encode($sender, $recipient) { | |
74 | preg_match('/(.+)\@([^\@]+)$/', $sender, $match); | |
75 | $slocal = $match[1]; | |
76 | $sdomain = $match[2]; | |
77 | ||
78 | preg_match('/(.+)\@([^\@]+)$/', $recipient, $match); | |
79 | $rlocal = CRM_Utils_Array::value(1, $match); | |
80 | $rdomain = CRM_Utils_Array::value(2, $match); | |
81 | ||
82 | foreach (self::$encodeMap as $char => $code) { | |
83 | $rlocal = preg_replace('/' . preg_quote($char) . '/i', "+$code", $rlocal); | |
84 | $rdomain = preg_replace('/' . preg_quote($char) . '/i', "+$code", $rdomain); | |
85 | } | |
86 | ||
87 | return "$slocal-$rlocal=$rdomain@$sdomain"; | |
88 | } | |
89 | ||
90 | /** | |
fe482240 | 91 | * Decode the address and return the sender and recipient as an array. |
6a488035 | 92 | * |
77855840 TO |
93 | * @param string $address |
94 | * The address to be decoded. | |
6a488035 | 95 | * |
a6c01b45 CW |
96 | * @return array |
97 | * The tuple ($sender, $recipient) | |
6a488035 TO |
98 | */ |
99 | public static function &verpdecode($address) { | |
100 | preg_match('/^(.+)-([^=]+)=([^\@]+)\@(.+)/', $address, $match); | |
101 | ||
353ffa53 TO |
102 | $slocal = $match[1]; |
103 | $rlocal = $match[2]; | |
6a488035 TO |
104 | $rdomain = $match[3]; |
105 | $sdomain = $match[4]; | |
106 | ||
107 | foreach (self::$decodeMap as $code => $char) { | |
108 | $rlocal = preg_replace("/+$code/i", $char, $rlocal); | |
109 | $rdomain = preg_replace("/+$code/i", $char, $rdomain); | |
110 | } | |
111 | ||
112 | return array("$slocal@$sdomain", "$rlocal@$rdomain"); | |
113 | } | |
96025800 | 114 | |
6a488035 | 115 | } |