Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: 
  3: /*
  4:  * This file is part of SwiftMailer.
  5:  * (c) 2004-2009 Chris Corbyn
  6:  *
  7:  * For the full copyright and license information, please view the LICENSE
  8:  * file that was distributed with this source code.
  9:  */
 10: 
 11: /**
 12:  * Allows customization of Messages on-the-fly.
 13:  *
 14:  * @package Swift
 15:  * @subpackage Plugins
 16:  *
 17:  * @author Chris Corbyn
 18:  * @author Fabien Potencier
 19:  */
 20: class Swift_Plugins_DecoratorPlugin implements Swift_Events_SendListener, Swift_Plugins_Decorator_Replacements
 21: {
 22:     /** The replacement map */
 23:     private $_replacements;
 24: 
 25:     /** The body as it was before replacements */
 26:     private $_originalBody;
 27: 
 28:     /** The original headers of the message, before replacements */
 29:     private $_originalHeaders = array();
 30: 
 31:     /** Bodies of children before they are replaced */
 32:     private $_originalChildBodies = array();
 33: 
 34:     /** The Message that was last replaced */
 35:     private $_lastMessage;
 36: 
 37:     /**
 38:      * Create a new DecoratorPlugin with $replacements.
 39:      *
 40:      * The $replacements can either be an associative array, or an implementation
 41:      * of {@link Swift_Plugins_Decorator_Replacements}.
 42:      *
 43:      * When using an array, it should be of the form:
 44:      * <code>
 45:      * $replacements = array(
 46:      *  "address1@domain.tld" => array("{a}" => "b", "{c}" => "d"),
 47:      *  "address2@domain.tld" => array("{a}" => "x", "{c}" => "y")
 48:      * )
 49:      * </code>
 50:      *
 51:      * When using an instance of {@link Swift_Plugins_Decorator_Replacements},
 52:      * the object should return just the array of replacements for the address
 53:      * given to {@link Swift_Plugins_Decorator_Replacements::getReplacementsFor()}.
 54:      *
 55:      * @param mixed $replacements Array or Swift_Plugins_Decorator_Replacements
 56:      */
 57:     public function __construct($replacements)
 58:     {
 59:         $this->setReplacements($replacements);
 60:     }
 61: 
 62:     /**
 63:      * Sets replacements.
 64:      *
 65:      * @param mixed $replacements Array or Swift_Plugins_Decorator_Replacements
 66:      *
 67:      * @see __construct()
 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:      * Invoked immediately before the Message is sent.
 80:      *
 81:      * @param Swift_Events_SendEvent $evt
 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:      * Find a map of replacements for the address.
147:      *
148:      * If this plugin was provided with a delegate instance of
149:      * {@link Swift_Plugins_Decorator_Replacements} then the call will be
150:      * delegated to it.  Otherwise, it will attempt to find the replacements
151:      * from the array provided in the constructor.
152:      *
153:      * If no replacements can be found, an empty value (NULL) is returned.
154:      *
155:      * @param string $address
156:      *
157:      * @return array
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:      * Invoked immediately after the Message is sent.
173:      *
174:      * @param Swift_Events_SendEvent $evt
175:      */
176:     public function sendPerformed(Swift_Events_SendEvent $evt)
177:     {
178:         $this->_restoreMessage($evt->getMessage());
179:     }
180: 
181:     // -- Private methods
182: 
183:     /** Restore a changed message back to its original state */
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: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen