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