Classes List

Swift_Plugins_DecoratorPlugin

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 
 108 
 109 
 110 
 111 
 112 
 113 
 114 
 115 
 116 
 117 
 118 
 119 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 130 
 131 
 132 
 133 
 134 
 135 
 136 
 137 
 138 
 139 
 140 
 141 
 142 
 143 
 144 
 145 
 146 
 147 
 148 
 149 
 150 
 151 
 152 
 153 
 154 
 155 
 156 
 157 
 158 
 159 
 160 
 161 
 162 
 163 
 164 
 165 
 166 
 167 
 168 
 169 
 170 
 171 
 172 
 173 
 174 
 175 
 176 
 177 
 178 
 179 
 180 
 181 
 182 
 183 
 184 
 185 
 186 
 187 
 188 
 189 
 190 
 191 
 192 
 193 
 194 
 195 
 196 
 197 
 198 
 199 
 200 
 201 
<?php

/* * This file is part of SwiftMailer. * (c) 2004-2009 Chris Corbyn * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


/** * Allows customization of Messages on-the-fly. * * @author Chris Corbyn * @author Fabien Potencier */
class Swift_Plugins_DecoratorPlugin implements Swift_Events_SendListenerSwift_Plugins_Decorator_Replacements
{
    /** The replacement map */
    private $replacements;

    /** The body as it was before replacements */
    private $originalBody;

    /** The original headers of the message, before replacements */
    private $originalHeaders = [];

    /** Bodies of children before they are replaced */
    private $originalChildBodies = [];

    /** The Message that was last replaced */
    private $lastMessage;

    /** * Create a new DecoratorPlugin with $replacements. * * The $replacements can either be an associative array, or an implementation * of {@link Swift_Plugins_Decorator_Replacements}. * * When using an array, it should be of the form: * <code> * $replacements = array( * "address1@domain.tld" => array("{a}" => "b", "{c}" => "d"), * "address2@domain.tld" => array("{a}" => "x", "{c}" => "y") * ) * </code> * * When using an instance of {@link Swift_Plugins_Decorator_Replacements}, * the object should return just the array of replacements for the address * given to {@link Swift_Plugins_Decorator_Replacements::getReplacementsFor()}. * * @param mixed $replacements Array or Swift_Plugins_Decorator_Replacements */
    public function __construct($replacements)
    {
        $this->setReplacements($replacements);
    }

    /** * Sets replacements. * * @param mixed $replacements Array or Swift_Plugins_Decorator_Replacements * * @see __construct() */
    public function setReplacements($replacements)
    {
        if (!($replacements instanceof Swift_Plugins_Decorator_Replacements)) {
            $this->replacements = (array) $replacements;
        } else {
            $this->replacements = $replacements;
        }
    }

    /** * Invoked immediately before the Message is sent. */
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
        $message = $evt->getMessage();
        $this->restoreMessage($message);
        $to = array_keys($message->getTo());
        $address = array_shift($to);
        if ($replacements = $this->getReplacementsFor($address)) {
            $body = $message->getBody();
            $search = array_keys($replacements);
            $replace = array_values($replacements);
            $bodyReplaced = str_replace(
                $search$replace$body
                );
            if ($body != $bodyReplaced) {
                $this->originalBody = $body;
                $message->setBody($bodyReplaced);
            }

            foreach ($message->getHeaders()->getAll() as $header) {
                $body = $header->getFieldBodyModel();
                $count = 0;
                if (is_array($body)) {
                    $bodyReplaced = [];
                    foreach ($body as $key => $value) {
                        $count1 = 0;
                        $count2 = 0;
                        $key = is_string($key) ? str_replace($search$replace$key$count1) : $key;
                        $value = is_string($value) ? str_replace($search$replace$value$count2) : $value;
                        $bodyReplaced[$key] = $value;

                        if (!$count && ($count1 || $count2)) {
                            $count = 1;
                        }
                    }
                } elseif (is_string($body)) {
                    $bodyReplaced = str_replace($search$replace$body$count);
                }

                if ($count) {
                    $this->originalHeaders[$header->getFieldName()] = $body;
                    $header->setFieldBodyModel($bodyReplaced);
                }
            }

            $children = (array) $message->getChildren();
            foreach ($children as $child) {
                list($type) = sscanf($child->getContentType(), '%[^/]/%s');
                if ('text' == $type) {
                    $body = $child->getBody();
                    $bodyReplaced = str_replace(
                        $search$replace$body
                        );
                    if ($body != $bodyReplaced) {
                        $child->setBody($bodyReplaced);
                        $this->originalChildBodies[$child->getId()] = $body;
                    }
                }
            }
            $this->lastMessage = $message;
        }
    }

    /** * Find a map of replacements for the address. * * If this plugin was provided with a delegate instance of * {@link Swift_Plugins_Decorator_Replacements} then the call will be * delegated to it. Otherwise, it will attempt to find the replacements * from the array provided in the constructor. * * If no replacements can be found, an empty value (NULL) is returned. * * @param string $address * * @return array */
    public function getReplacementsFor($address)
    {
        if ($this->replacements instanceof Swift_Plugins_Decorator_Replacements) {
            return $this->replacements->getReplacementsFor($address);
        }

        return $this->replacements[$address?? null;
    }

    /** * Invoked immediately after the Message is sent. */
    public function sendPerformed(Swift_Events_SendEvent $evt)
    {
        $this->restoreMessage($evt->getMessage());
    }

    /** Restore a changed message back to its original state */
    private function restoreMessage(Swift_Mime_SimpleMessage $message)
    {
        if ($this->lastMessage === $message) {
            if (isset($this->originalBody)) {
                $message->setBody($this->originalBody);
                $this->originalBody = null;
            }
            if (!empty($this->originalHeaders)) {
                foreach ($message->getHeaders()->getAll() as $header) {
                    if (array_key_exists($header->getFieldName(), $this->originalHeaders)) {
                        $header->setFieldBodyModel($this->originalHeaders[$header->getFieldName()]);
                    }
                }
                $this->originalHeaders = [];
            }
            if (!empty($this->originalChildBodies)) {
                $children = (array) $message->getChildren();
                foreach ($children as $child) {
                    $id = $child->getId();
                    if (array_key_exists($id$this->originalChildBodies)) {
                        $child->setBody($this->originalChildBodies[$id]);
                    }
                }
                $this->originalChildBodies = [];
            }
            $this->lastMessage = null;
        }
    }
}