1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class Swift_Plugins_DecoratorPlugin implements Swift_Events_SendListener, Swift_Plugins_Decorator_Replacements
21: {
22:
23: private $_replacements;
24:
25:
26: private $_originalBody;
27:
28:
29: private $_originalHeaders = array();
30:
31:
32: private $_originalChildBodies = array();
33:
34:
35: private $_lastMessage;
36:
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
57: public function __construct($replacements)
58: {
59: $this->setReplacements($replacements);
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function setReplacements($replacements)
70: {
71: if (!($replacements instanceof \Swift_Plugins_Decorator_Replacements)) {
72: $this->_replacements = (array) $replacements;
73: } else {
74: $this->_replacements = $replacements;
75: }
76: }
77:
78: 79: 80: 81: 82:
83: public function beforeSendPerformed(Swift_Events_SendEvent $evt)
84: {
85: $message = $evt->getMessage();
86: $this->_restoreMessage($message);
87: $to = array_keys($message->getTo());
88: $address = array_shift($to);
89: if ($replacements = $this->getReplacementsFor($address)) {
90: $body = $message->getBody();
91: $search = array_keys($replacements);
92: $replace = array_values($replacements);
93: $bodyReplaced = str_replace(
94: $search, $replace, $body
95: );
96: if ($body != $bodyReplaced) {
97: $this->_originalBody = $body;
98: $message->setBody($bodyReplaced);
99: }
100:
101: foreach ($message->getHeaders()->getAll() as $header) {
102: $body = $header->getFieldBodyModel();
103: $count = 0;
104: if (is_array($body)) {
105: $bodyReplaced = array();
106: foreach ($body as $key => $value) {
107: $count1 = 0;
108: $count2 = 0;
109: $key = is_string($key) ? str_replace($search, $replace, $key, $count1) : $key;
110: $value = is_string($value) ? str_replace($search, $replace, $value, $count2) : $value;
111: $bodyReplaced[$key] = $value;
112:
113: if (!$count && ($count1 || $count2)) {
114: $count = 1;
115: }
116: }
117: } else {
118: $bodyReplaced = str_replace($search, $replace, $body, $count);
119: }
120:
121: if ($count) {
122: $this->_originalHeaders[$header->getFieldName()] = $body;
123: $header->setFieldBodyModel($bodyReplaced);
124: }
125: }
126:
127: $children = (array) $message->getChildren();
128: foreach ($children as $child) {
129: list($type, ) = sscanf($child->getContentType(), '%[^/]/%s');
130: if ('text' == $type) {
131: $body = $child->getBody();
132: $bodyReplaced = str_replace(
133: $search, $replace, $body
134: );
135: if ($body != $bodyReplaced) {
136: $child->setBody($bodyReplaced);
137: $this->_originalChildBodies[$child->getId()] = $body;
138: }
139: }
140: }
141: $this->_lastMessage = $message;
142: }
143: }
144:
145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158:
159: public function getReplacementsFor($address)
160: {
161: if ($this->_replacements instanceof Swift_Plugins_Decorator_Replacements) {
162: return $this->_replacements->getReplacementsFor($address);
163: } else {
164: return isset($this->_replacements[$address])
165: ? $this->_replacements[$address]
166: : null
167: ;
168: }
169: }
170:
171: 172: 173: 174: 175:
176: public function sendPerformed(Swift_Events_SendEvent $evt)
177: {
178: $this->_restoreMessage($evt->getMessage());
179: }
180:
181:
182:
183:
184: private function _restoreMessage(Swift_Mime_Message $message)
185: {
186: if ($this->_lastMessage === $message) {
187: if (isset($this->_originalBody)) {
188: $message->setBody($this->_originalBody);
189: $this->_originalBody = null;
190: }
191: if (!empty($this->_originalHeaders)) {
192: foreach ($message->getHeaders()->getAll() as $header) {
193: if (array_key_exists($header->getFieldName(), $this->_originalHeaders)) {
194: $header->setFieldBodyModel($this->_originalHeaders[$header->getFieldName()]);
195: }
196: }
197: $this->_originalHeaders = array();
198: }
199: if (!empty($this->_originalChildBodies)) {
200: $children = (array) $message->getChildren();
201: foreach ($children as $child) {
202: $id = $child->getId();
203: if (array_key_exists($id, $this->_originalChildBodies)) {
204: $child->setBody($this->_originalChildBodies[$id]);
205: }
206: }
207: $this->_originalChildBodies = array();
208: }
209: $this->_lastMessage = null;
210: }
211: }
212: }
213: