@@ -15,95 +15,95 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Mime_Headers_UnstructuredHeader extends Swift_Mime_Headers_AbstractHeader |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * The value of this Header. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - private $value; |
|
| 18 | + /** |
|
| 19 | + * The value of this Header. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + private $value; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Creates a new SimpleHeader with $name. |
|
| 27 | - * |
|
| 28 | - * @param string $name |
|
| 29 | - */ |
|
| 30 | - public function __construct($name, Swift_Mime_HeaderEncoder $encoder) |
|
| 31 | - { |
|
| 32 | - $this->setFieldName($name); |
|
| 33 | - $this->setEncoder($encoder); |
|
| 34 | - } |
|
| 25 | + /** |
|
| 26 | + * Creates a new SimpleHeader with $name. |
|
| 27 | + * |
|
| 28 | + * @param string $name |
|
| 29 | + */ |
|
| 30 | + public function __construct($name, Swift_Mime_HeaderEncoder $encoder) |
|
| 31 | + { |
|
| 32 | + $this->setFieldName($name); |
|
| 33 | + $this->setEncoder($encoder); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get the type of Header that this instance represents. |
|
| 38 | - * |
|
| 39 | - * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 40 | - * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 41 | - * |
|
| 42 | - * @return int |
|
| 43 | - */ |
|
| 44 | - public function getFieldType() |
|
| 45 | - { |
|
| 46 | - return self::TYPE_TEXT; |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * Get the type of Header that this instance represents. |
|
| 38 | + * |
|
| 39 | + * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 40 | + * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 41 | + * |
|
| 42 | + * @return int |
|
| 43 | + */ |
|
| 44 | + public function getFieldType() |
|
| 45 | + { |
|
| 46 | + return self::TYPE_TEXT; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Set the model for the field body. |
|
| 51 | - * |
|
| 52 | - * This method takes a string for the field value. |
|
| 53 | - * |
|
| 54 | - * @param string $model |
|
| 55 | - */ |
|
| 56 | - public function setFieldBodyModel($model) |
|
| 57 | - { |
|
| 58 | - $this->setValue($model); |
|
| 59 | - } |
|
| 49 | + /** |
|
| 50 | + * Set the model for the field body. |
|
| 51 | + * |
|
| 52 | + * This method takes a string for the field value. |
|
| 53 | + * |
|
| 54 | + * @param string $model |
|
| 55 | + */ |
|
| 56 | + public function setFieldBodyModel($model) |
|
| 57 | + { |
|
| 58 | + $this->setValue($model); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get the model for the field body. |
|
| 63 | - * |
|
| 64 | - * This method returns a string. |
|
| 65 | - * |
|
| 66 | - * @return string |
|
| 67 | - */ |
|
| 68 | - public function getFieldBodyModel() |
|
| 69 | - { |
|
| 70 | - return $this->getValue(); |
|
| 71 | - } |
|
| 61 | + /** |
|
| 62 | + * Get the model for the field body. |
|
| 63 | + * |
|
| 64 | + * This method returns a string. |
|
| 65 | + * |
|
| 66 | + * @return string |
|
| 67 | + */ |
|
| 68 | + public function getFieldBodyModel() |
|
| 69 | + { |
|
| 70 | + return $this->getValue(); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get the (unencoded) value of this header. |
|
| 75 | - * |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public function getValue() |
|
| 79 | - { |
|
| 80 | - return $this->value; |
|
| 81 | - } |
|
| 73 | + /** |
|
| 74 | + * Get the (unencoded) value of this header. |
|
| 75 | + * |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public function getValue() |
|
| 79 | + { |
|
| 80 | + return $this->value; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Set the (unencoded) value of this header. |
|
| 85 | - * |
|
| 86 | - * @param string $value |
|
| 87 | - */ |
|
| 88 | - public function setValue($value) |
|
| 89 | - { |
|
| 90 | - $this->clearCachedValueIf($this->value != $value); |
|
| 91 | - $this->value = $value; |
|
| 92 | - } |
|
| 83 | + /** |
|
| 84 | + * Set the (unencoded) value of this header. |
|
| 85 | + * |
|
| 86 | + * @param string $value |
|
| 87 | + */ |
|
| 88 | + public function setValue($value) |
|
| 89 | + { |
|
| 90 | + $this->clearCachedValueIf($this->value != $value); |
|
| 91 | + $this->value = $value; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Get the value of this header prepared for rendering. |
|
| 96 | - * |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - public function getFieldBody() |
|
| 100 | - { |
|
| 101 | - if (!$this->getCachedValue()) { |
|
| 102 | - $this->setCachedValue( |
|
| 103 | - $this->encodeWords($this, $this->value) |
|
| 104 | - ); |
|
| 105 | - } |
|
| 94 | + /** |
|
| 95 | + * Get the value of this header prepared for rendering. |
|
| 96 | + * |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + public function getFieldBody() |
|
| 100 | + { |
|
| 101 | + if (!$this->getCachedValue()) { |
|
| 102 | + $this->setCachedValue( |
|
| 103 | + $this->encodeWords($this, $this->value) |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - return $this->getCachedValue(); |
|
| 108 | - } |
|
| 107 | + return $this->getCachedValue(); |
|
| 108 | + } |
|
| 109 | 109 | } |
@@ -18,341 +18,341 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * The mailboxes used in this Header. |
|
| 23 | - * |
|
| 24 | - * @var string[] |
|
| 25 | - */ |
|
| 26 | - private $mailboxes = []; |
|
| 21 | + /** |
|
| 22 | + * The mailboxes used in this Header. |
|
| 23 | + * |
|
| 24 | + * @var string[] |
|
| 25 | + */ |
|
| 26 | + private $mailboxes = []; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The strict EmailValidator. |
|
| 30 | - * |
|
| 31 | - * @var EmailValidator |
|
| 32 | - */ |
|
| 33 | - private $emailValidator; |
|
| 28 | + /** |
|
| 29 | + * The strict EmailValidator. |
|
| 30 | + * |
|
| 31 | + * @var EmailValidator |
|
| 32 | + */ |
|
| 33 | + private $emailValidator; |
|
| 34 | 34 | |
| 35 | - private $addressEncoder; |
|
| 35 | + private $addressEncoder; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Creates a new MailboxHeader with $name. |
|
| 39 | - * |
|
| 40 | - * @param string $name of Header |
|
| 41 | - */ |
|
| 42 | - public function __construct($name, Swift_Mime_HeaderEncoder $encoder, EmailValidator $emailValidator, Swift_AddressEncoder $addressEncoder = null) |
|
| 43 | - { |
|
| 44 | - $this->setFieldName($name); |
|
| 45 | - $this->setEncoder($encoder); |
|
| 46 | - $this->emailValidator = $emailValidator; |
|
| 47 | - $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder(); |
|
| 48 | - } |
|
| 37 | + /** |
|
| 38 | + * Creates a new MailboxHeader with $name. |
|
| 39 | + * |
|
| 40 | + * @param string $name of Header |
|
| 41 | + */ |
|
| 42 | + public function __construct($name, Swift_Mime_HeaderEncoder $encoder, EmailValidator $emailValidator, Swift_AddressEncoder $addressEncoder = null) |
|
| 43 | + { |
|
| 44 | + $this->setFieldName($name); |
|
| 45 | + $this->setEncoder($encoder); |
|
| 46 | + $this->emailValidator = $emailValidator; |
|
| 47 | + $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Get the type of Header that this instance represents. |
|
| 52 | - * |
|
| 53 | - * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 54 | - * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 55 | - * |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public function getFieldType() |
|
| 59 | - { |
|
| 60 | - return self::TYPE_MAILBOX; |
|
| 61 | - } |
|
| 50 | + /** |
|
| 51 | + * Get the type of Header that this instance represents. |
|
| 52 | + * |
|
| 53 | + * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 54 | + * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 55 | + * |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public function getFieldType() |
|
| 59 | + { |
|
| 60 | + return self::TYPE_MAILBOX; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Set the model for the field body. |
|
| 65 | - * |
|
| 66 | - * This method takes a string, or an array of addresses. |
|
| 67 | - * |
|
| 68 | - * @param mixed $model |
|
| 69 | - * |
|
| 70 | - * @throws Swift_RfcComplianceException |
|
| 71 | - */ |
|
| 72 | - public function setFieldBodyModel($model) |
|
| 73 | - { |
|
| 74 | - $this->setNameAddresses($model); |
|
| 75 | - } |
|
| 63 | + /** |
|
| 64 | + * Set the model for the field body. |
|
| 65 | + * |
|
| 66 | + * This method takes a string, or an array of addresses. |
|
| 67 | + * |
|
| 68 | + * @param mixed $model |
|
| 69 | + * |
|
| 70 | + * @throws Swift_RfcComplianceException |
|
| 71 | + */ |
|
| 72 | + public function setFieldBodyModel($model) |
|
| 73 | + { |
|
| 74 | + $this->setNameAddresses($model); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Get the model for the field body. |
|
| 79 | - * |
|
| 80 | - * This method returns an associative array like {@link getNameAddresses()} |
|
| 81 | - * |
|
| 82 | - * @throws Swift_RfcComplianceException |
|
| 83 | - * |
|
| 84 | - * @return array |
|
| 85 | - */ |
|
| 86 | - public function getFieldBodyModel() |
|
| 87 | - { |
|
| 88 | - return $this->getNameAddresses(); |
|
| 89 | - } |
|
| 77 | + /** |
|
| 78 | + * Get the model for the field body. |
|
| 79 | + * |
|
| 80 | + * This method returns an associative array like {@link getNameAddresses()} |
|
| 81 | + * |
|
| 82 | + * @throws Swift_RfcComplianceException |
|
| 83 | + * |
|
| 84 | + * @return array |
|
| 85 | + */ |
|
| 86 | + public function getFieldBodyModel() |
|
| 87 | + { |
|
| 88 | + return $this->getNameAddresses(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Set a list of mailboxes to be shown in this Header. |
|
| 93 | - * |
|
| 94 | - * The mailboxes can be a simple array of addresses, or an array of |
|
| 95 | - * key=>value pairs where (email => personalName). |
|
| 96 | - * Example: |
|
| 97 | - * <code> |
|
| 98 | - * <?php |
|
| 99 | - * //Sets two mailboxes in the Header, one with a personal name |
|
| 100 | - * $header->setNameAddresses(array( |
|
| 101 | - * '[email protected]' => 'Chris Corbyn', |
|
| 102 | - * '[email protected]' //No associated personal name |
|
| 103 | - * )); |
|
| 104 | - * ?> |
|
| 105 | - * </code> |
|
| 106 | - * |
|
| 107 | - * @see __construct() |
|
| 108 | - * @see setAddresses() |
|
| 109 | - * @see setValue() |
|
| 110 | - * |
|
| 111 | - * @param string|string[] $mailboxes |
|
| 112 | - * |
|
| 113 | - * @throws Swift_RfcComplianceException |
|
| 114 | - */ |
|
| 115 | - public function setNameAddresses($mailboxes) |
|
| 116 | - { |
|
| 117 | - $this->mailboxes = $this->normalizeMailboxes((array) $mailboxes); |
|
| 118 | - $this->setCachedValue(null); //Clear any cached value |
|
| 119 | - } |
|
| 91 | + /** |
|
| 92 | + * Set a list of mailboxes to be shown in this Header. |
|
| 93 | + * |
|
| 94 | + * The mailboxes can be a simple array of addresses, or an array of |
|
| 95 | + * key=>value pairs where (email => personalName). |
|
| 96 | + * Example: |
|
| 97 | + * <code> |
|
| 98 | + * <?php |
|
| 99 | + * //Sets two mailboxes in the Header, one with a personal name |
|
| 100 | + * $header->setNameAddresses(array( |
|
| 101 | + * '[email protected]' => 'Chris Corbyn', |
|
| 102 | + * '[email protected]' //No associated personal name |
|
| 103 | + * )); |
|
| 104 | + * ?> |
|
| 105 | + * </code> |
|
| 106 | + * |
|
| 107 | + * @see __construct() |
|
| 108 | + * @see setAddresses() |
|
| 109 | + * @see setValue() |
|
| 110 | + * |
|
| 111 | + * @param string|string[] $mailboxes |
|
| 112 | + * |
|
| 113 | + * @throws Swift_RfcComplianceException |
|
| 114 | + */ |
|
| 115 | + public function setNameAddresses($mailboxes) |
|
| 116 | + { |
|
| 117 | + $this->mailboxes = $this->normalizeMailboxes((array) $mailboxes); |
|
| 118 | + $this->setCachedValue(null); //Clear any cached value |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Get the full mailbox list of this Header as an array of valid RFC 2822 strings. |
|
| 123 | - * |
|
| 124 | - * Example: |
|
| 125 | - * <code> |
|
| 126 | - * <?php |
|
| 127 | - * $header = new Swift_Mime_Headers_MailboxHeader('From', |
|
| 128 | - * array('[email protected]' => 'Chris Corbyn', |
|
| 129 | - * '[email protected]' => 'Mark Corbyn') |
|
| 130 | - * ); |
|
| 131 | - * print_r($header->getNameAddressStrings()); |
|
| 132 | - * // array ( |
|
| 133 | - * // 0 => Chris Corbyn <[email protected]>, |
|
| 134 | - * // 1 => Mark Corbyn <[email protected]> |
|
| 135 | - * // ) |
|
| 136 | - * ?> |
|
| 137 | - * </code> |
|
| 138 | - * |
|
| 139 | - * @see getNameAddresses() |
|
| 140 | - * @see toString() |
|
| 141 | - * |
|
| 142 | - * @throws Swift_RfcComplianceException |
|
| 143 | - * |
|
| 144 | - * @return string[] |
|
| 145 | - */ |
|
| 146 | - public function getNameAddressStrings() |
|
| 147 | - { |
|
| 148 | - return $this->createNameAddressStrings($this->getNameAddresses()); |
|
| 149 | - } |
|
| 121 | + /** |
|
| 122 | + * Get the full mailbox list of this Header as an array of valid RFC 2822 strings. |
|
| 123 | + * |
|
| 124 | + * Example: |
|
| 125 | + * <code> |
|
| 126 | + * <?php |
|
| 127 | + * $header = new Swift_Mime_Headers_MailboxHeader('From', |
|
| 128 | + * array('[email protected]' => 'Chris Corbyn', |
|
| 129 | + * '[email protected]' => 'Mark Corbyn') |
|
| 130 | + * ); |
|
| 131 | + * print_r($header->getNameAddressStrings()); |
|
| 132 | + * // array ( |
|
| 133 | + * // 0 => Chris Corbyn <[email protected]>, |
|
| 134 | + * // 1 => Mark Corbyn <[email protected]> |
|
| 135 | + * // ) |
|
| 136 | + * ?> |
|
| 137 | + * </code> |
|
| 138 | + * |
|
| 139 | + * @see getNameAddresses() |
|
| 140 | + * @see toString() |
|
| 141 | + * |
|
| 142 | + * @throws Swift_RfcComplianceException |
|
| 143 | + * |
|
| 144 | + * @return string[] |
|
| 145 | + */ |
|
| 146 | + public function getNameAddressStrings() |
|
| 147 | + { |
|
| 148 | + return $this->createNameAddressStrings($this->getNameAddresses()); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Get all mailboxes in this Header as key=>value pairs. |
|
| 153 | - * |
|
| 154 | - * The key is the address and the value is the name (or null if none set). |
|
| 155 | - * Example: |
|
| 156 | - * <code> |
|
| 157 | - * <?php |
|
| 158 | - * $header = new Swift_Mime_Headers_MailboxHeader('From', |
|
| 159 | - * array('[email protected]' => 'Chris Corbyn', |
|
| 160 | - * '[email protected]' => 'Mark Corbyn') |
|
| 161 | - * ); |
|
| 162 | - * print_r($header->getNameAddresses()); |
|
| 163 | - * // array ( |
|
| 164 | - * // [email protected] => Chris Corbyn, |
|
| 165 | - * // [email protected] => Mark Corbyn |
|
| 166 | - * // ) |
|
| 167 | - * ?> |
|
| 168 | - * </code> |
|
| 169 | - * |
|
| 170 | - * @see getAddresses() |
|
| 171 | - * @see getNameAddressStrings() |
|
| 172 | - * |
|
| 173 | - * @return string[] |
|
| 174 | - */ |
|
| 175 | - public function getNameAddresses() |
|
| 176 | - { |
|
| 177 | - return $this->mailboxes; |
|
| 178 | - } |
|
| 151 | + /** |
|
| 152 | + * Get all mailboxes in this Header as key=>value pairs. |
|
| 153 | + * |
|
| 154 | + * The key is the address and the value is the name (or null if none set). |
|
| 155 | + * Example: |
|
| 156 | + * <code> |
|
| 157 | + * <?php |
|
| 158 | + * $header = new Swift_Mime_Headers_MailboxHeader('From', |
|
| 159 | + * array('[email protected]' => 'Chris Corbyn', |
|
| 160 | + * '[email protected]' => 'Mark Corbyn') |
|
| 161 | + * ); |
|
| 162 | + * print_r($header->getNameAddresses()); |
|
| 163 | + * // array ( |
|
| 164 | + * // [email protected] => Chris Corbyn, |
|
| 165 | + * // [email protected] => Mark Corbyn |
|
| 166 | + * // ) |
|
| 167 | + * ?> |
|
| 168 | + * </code> |
|
| 169 | + * |
|
| 170 | + * @see getAddresses() |
|
| 171 | + * @see getNameAddressStrings() |
|
| 172 | + * |
|
| 173 | + * @return string[] |
|
| 174 | + */ |
|
| 175 | + public function getNameAddresses() |
|
| 176 | + { |
|
| 177 | + return $this->mailboxes; |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Makes this Header represent a list of plain email addresses with no names. |
|
| 182 | - * |
|
| 183 | - * Example: |
|
| 184 | - * <code> |
|
| 185 | - * <?php |
|
| 186 | - * //Sets three email addresses as the Header data |
|
| 187 | - * $header->setAddresses( |
|
| 188 | - * array('[email protected]', '[email protected]', '[email protected]') |
|
| 189 | - * ); |
|
| 190 | - * ?> |
|
| 191 | - * </code> |
|
| 192 | - * |
|
| 193 | - * @see setNameAddresses() |
|
| 194 | - * @see setValue() |
|
| 195 | - * |
|
| 196 | - * @param string[] $addresses |
|
| 197 | - * |
|
| 198 | - * @throws Swift_RfcComplianceException |
|
| 199 | - */ |
|
| 200 | - public function setAddresses($addresses) |
|
| 201 | - { |
|
| 202 | - $this->setNameAddresses(array_values((array) $addresses)); |
|
| 203 | - } |
|
| 180 | + /** |
|
| 181 | + * Makes this Header represent a list of plain email addresses with no names. |
|
| 182 | + * |
|
| 183 | + * Example: |
|
| 184 | + * <code> |
|
| 185 | + * <?php |
|
| 186 | + * //Sets three email addresses as the Header data |
|
| 187 | + * $header->setAddresses( |
|
| 188 | + * array('[email protected]', '[email protected]', '[email protected]') |
|
| 189 | + * ); |
|
| 190 | + * ?> |
|
| 191 | + * </code> |
|
| 192 | + * |
|
| 193 | + * @see setNameAddresses() |
|
| 194 | + * @see setValue() |
|
| 195 | + * |
|
| 196 | + * @param string[] $addresses |
|
| 197 | + * |
|
| 198 | + * @throws Swift_RfcComplianceException |
|
| 199 | + */ |
|
| 200 | + public function setAddresses($addresses) |
|
| 201 | + { |
|
| 202 | + $this->setNameAddresses(array_values((array) $addresses)); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * Get all email addresses in this Header. |
|
| 207 | - * |
|
| 208 | - * @see getNameAddresses() |
|
| 209 | - * |
|
| 210 | - * @return string[] |
|
| 211 | - */ |
|
| 212 | - public function getAddresses() |
|
| 213 | - { |
|
| 214 | - return array_keys($this->mailboxes); |
|
| 215 | - } |
|
| 205 | + /** |
|
| 206 | + * Get all email addresses in this Header. |
|
| 207 | + * |
|
| 208 | + * @see getNameAddresses() |
|
| 209 | + * |
|
| 210 | + * @return string[] |
|
| 211 | + */ |
|
| 212 | + public function getAddresses() |
|
| 213 | + { |
|
| 214 | + return array_keys($this->mailboxes); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Remove one or more addresses from this Header. |
|
| 219 | - * |
|
| 220 | - * @param string|string[] $addresses |
|
| 221 | - */ |
|
| 222 | - public function removeAddresses($addresses) |
|
| 223 | - { |
|
| 224 | - $this->setCachedValue(null); |
|
| 225 | - foreach ((array) $addresses as $address) { |
|
| 226 | - unset($this->mailboxes[$address]); |
|
| 227 | - } |
|
| 228 | - } |
|
| 217 | + /** |
|
| 218 | + * Remove one or more addresses from this Header. |
|
| 219 | + * |
|
| 220 | + * @param string|string[] $addresses |
|
| 221 | + */ |
|
| 222 | + public function removeAddresses($addresses) |
|
| 223 | + { |
|
| 224 | + $this->setCachedValue(null); |
|
| 225 | + foreach ((array) $addresses as $address) { |
|
| 226 | + unset($this->mailboxes[$address]); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Get the string value of the body in this Header. |
|
| 232 | - * |
|
| 233 | - * This is not necessarily RFC 2822 compliant since folding white space will |
|
| 234 | - * not be added at this stage (see {@link toString()} for that). |
|
| 235 | - * |
|
| 236 | - * @see toString() |
|
| 237 | - * |
|
| 238 | - * @throws Swift_RfcComplianceException |
|
| 239 | - * |
|
| 240 | - * @return string |
|
| 241 | - */ |
|
| 242 | - public function getFieldBody() |
|
| 243 | - { |
|
| 244 | - // Compute the string value of the header only if needed |
|
| 245 | - if (null === $this->getCachedValue()) { |
|
| 246 | - $this->setCachedValue($this->createMailboxListString($this->mailboxes)); |
|
| 247 | - } |
|
| 230 | + /** |
|
| 231 | + * Get the string value of the body in this Header. |
|
| 232 | + * |
|
| 233 | + * This is not necessarily RFC 2822 compliant since folding white space will |
|
| 234 | + * not be added at this stage (see {@link toString()} for that). |
|
| 235 | + * |
|
| 236 | + * @see toString() |
|
| 237 | + * |
|
| 238 | + * @throws Swift_RfcComplianceException |
|
| 239 | + * |
|
| 240 | + * @return string |
|
| 241 | + */ |
|
| 242 | + public function getFieldBody() |
|
| 243 | + { |
|
| 244 | + // Compute the string value of the header only if needed |
|
| 245 | + if (null === $this->getCachedValue()) { |
|
| 246 | + $this->setCachedValue($this->createMailboxListString($this->mailboxes)); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - return $this->getCachedValue(); |
|
| 250 | - } |
|
| 249 | + return $this->getCachedValue(); |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - /** |
|
| 253 | - * Normalizes a user-input list of mailboxes into consistent key=>value pairs. |
|
| 254 | - * |
|
| 255 | - * @param string[] $mailboxes |
|
| 256 | - * |
|
| 257 | - * @return string[] |
|
| 258 | - */ |
|
| 259 | - protected function normalizeMailboxes(array $mailboxes) |
|
| 260 | - { |
|
| 261 | - $actualMailboxes = []; |
|
| 252 | + /** |
|
| 253 | + * Normalizes a user-input list of mailboxes into consistent key=>value pairs. |
|
| 254 | + * |
|
| 255 | + * @param string[] $mailboxes |
|
| 256 | + * |
|
| 257 | + * @return string[] |
|
| 258 | + */ |
|
| 259 | + protected function normalizeMailboxes(array $mailboxes) |
|
| 260 | + { |
|
| 261 | + $actualMailboxes = []; |
|
| 262 | 262 | |
| 263 | - foreach ($mailboxes as $key => $value) { |
|
| 264 | - if (\is_string($key)) { |
|
| 265 | - //key is email addr |
|
| 266 | - $address = $key; |
|
| 267 | - $name = $value; |
|
| 268 | - } else { |
|
| 269 | - $address = $value; |
|
| 270 | - $name = null; |
|
| 271 | - } |
|
| 272 | - $this->assertValidAddress($address); |
|
| 273 | - $actualMailboxes[$address] = $name; |
|
| 274 | - } |
|
| 263 | + foreach ($mailboxes as $key => $value) { |
|
| 264 | + if (\is_string($key)) { |
|
| 265 | + //key is email addr |
|
| 266 | + $address = $key; |
|
| 267 | + $name = $value; |
|
| 268 | + } else { |
|
| 269 | + $address = $value; |
|
| 270 | + $name = null; |
|
| 271 | + } |
|
| 272 | + $this->assertValidAddress($address); |
|
| 273 | + $actualMailboxes[$address] = $name; |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - return $actualMailboxes; |
|
| 277 | - } |
|
| 276 | + return $actualMailboxes; |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Produces a compliant, formatted display-name based on the string given. |
|
| 281 | - * |
|
| 282 | - * @param string $displayName as displayed |
|
| 283 | - * @param bool $shorten the first line to make remove for header name |
|
| 284 | - * |
|
| 285 | - * @return string |
|
| 286 | - */ |
|
| 287 | - protected function createDisplayNameString($displayName, $shorten = false) |
|
| 288 | - { |
|
| 289 | - return $this->createPhrase($this, $displayName, $this->getCharset(), $this->getEncoder(), $shorten); |
|
| 290 | - } |
|
| 279 | + /** |
|
| 280 | + * Produces a compliant, formatted display-name based on the string given. |
|
| 281 | + * |
|
| 282 | + * @param string $displayName as displayed |
|
| 283 | + * @param bool $shorten the first line to make remove for header name |
|
| 284 | + * |
|
| 285 | + * @return string |
|
| 286 | + */ |
|
| 287 | + protected function createDisplayNameString($displayName, $shorten = false) |
|
| 288 | + { |
|
| 289 | + return $this->createPhrase($this, $displayName, $this->getCharset(), $this->getEncoder(), $shorten); |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * Creates a string form of all the mailboxes in the passed array. |
|
| 294 | - * |
|
| 295 | - * @param string[] $mailboxes |
|
| 296 | - * |
|
| 297 | - * @throws Swift_RfcComplianceException |
|
| 298 | - * |
|
| 299 | - * @return string |
|
| 300 | - */ |
|
| 301 | - protected function createMailboxListString(array $mailboxes) |
|
| 302 | - { |
|
| 303 | - return implode(', ', $this->createNameAddressStrings($mailboxes)); |
|
| 304 | - } |
|
| 292 | + /** |
|
| 293 | + * Creates a string form of all the mailboxes in the passed array. |
|
| 294 | + * |
|
| 295 | + * @param string[] $mailboxes |
|
| 296 | + * |
|
| 297 | + * @throws Swift_RfcComplianceException |
|
| 298 | + * |
|
| 299 | + * @return string |
|
| 300 | + */ |
|
| 301 | + protected function createMailboxListString(array $mailboxes) |
|
| 302 | + { |
|
| 303 | + return implode(', ', $this->createNameAddressStrings($mailboxes)); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Redefine the encoding requirements for mailboxes. |
|
| 308 | - * |
|
| 309 | - * All "specials" must be encoded as the full header value will not be quoted |
|
| 310 | - * |
|
| 311 | - * @see RFC 2822 3.2.1 |
|
| 312 | - * |
|
| 313 | - * @param string $token |
|
| 314 | - * |
|
| 315 | - * @return bool |
|
| 316 | - */ |
|
| 317 | - protected function tokenNeedsEncoding($token) |
|
| 318 | - { |
|
| 319 | - return preg_match('/[()<>\[\]:;@\,."]/', $token) || parent::tokenNeedsEncoding($token); |
|
| 320 | - } |
|
| 306 | + /** |
|
| 307 | + * Redefine the encoding requirements for mailboxes. |
|
| 308 | + * |
|
| 309 | + * All "specials" must be encoded as the full header value will not be quoted |
|
| 310 | + * |
|
| 311 | + * @see RFC 2822 3.2.1 |
|
| 312 | + * |
|
| 313 | + * @param string $token |
|
| 314 | + * |
|
| 315 | + * @return bool |
|
| 316 | + */ |
|
| 317 | + protected function tokenNeedsEncoding($token) |
|
| 318 | + { |
|
| 319 | + return preg_match('/[()<>\[\]:;@\,."]/', $token) || parent::tokenNeedsEncoding($token); |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - /** |
|
| 323 | - * Return an array of strings conforming the the name-addr spec of RFC 2822. |
|
| 324 | - * |
|
| 325 | - * @param string[] $mailboxes |
|
| 326 | - * |
|
| 327 | - * @return string[] |
|
| 328 | - */ |
|
| 329 | - private function createNameAddressStrings(array $mailboxes) |
|
| 330 | - { |
|
| 331 | - $strings = []; |
|
| 322 | + /** |
|
| 323 | + * Return an array of strings conforming the the name-addr spec of RFC 2822. |
|
| 324 | + * |
|
| 325 | + * @param string[] $mailboxes |
|
| 326 | + * |
|
| 327 | + * @return string[] |
|
| 328 | + */ |
|
| 329 | + private function createNameAddressStrings(array $mailboxes) |
|
| 330 | + { |
|
| 331 | + $strings = []; |
|
| 332 | 332 | |
| 333 | - foreach ($mailboxes as $email => $name) { |
|
| 334 | - $mailboxStr = $this->addressEncoder->encodeString($email); |
|
| 335 | - if (null !== $name) { |
|
| 336 | - $nameStr = $this->createDisplayNameString($name, empty($strings)); |
|
| 337 | - $mailboxStr = $nameStr.' <'.$mailboxStr.'>'; |
|
| 338 | - } |
|
| 339 | - $strings[] = $mailboxStr; |
|
| 340 | - } |
|
| 333 | + foreach ($mailboxes as $email => $name) { |
|
| 334 | + $mailboxStr = $this->addressEncoder->encodeString($email); |
|
| 335 | + if (null !== $name) { |
|
| 336 | + $nameStr = $this->createDisplayNameString($name, empty($strings)); |
|
| 337 | + $mailboxStr = $nameStr.' <'.$mailboxStr.'>'; |
|
| 338 | + } |
|
| 339 | + $strings[] = $mailboxStr; |
|
| 340 | + } |
|
| 341 | 341 | |
| 342 | - return $strings; |
|
| 343 | - } |
|
| 342 | + return $strings; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 346 | - * Throws an Exception if the address passed does not comply with RFC 2822. |
|
| 347 | - * |
|
| 348 | - * @param string $address |
|
| 349 | - * |
|
| 350 | - * @throws Swift_RfcComplianceException if invalid |
|
| 351 | - */ |
|
| 352 | - private function assertValidAddress($address) |
|
| 353 | - { |
|
| 354 | - if (!$this->emailValidator->isValid($address, new RFCValidation())) { |
|
| 355 | - throw new Swift_RfcComplianceException('Address in mailbox given ['.$address.'] does not comply with RFC 2822, 3.6.2.'); |
|
| 356 | - } |
|
| 357 | - } |
|
| 345 | + /** |
|
| 346 | + * Throws an Exception if the address passed does not comply with RFC 2822. |
|
| 347 | + * |
|
| 348 | + * @param string $address |
|
| 349 | + * |
|
| 350 | + * @throws Swift_RfcComplianceException if invalid |
|
| 351 | + */ |
|
| 352 | + private function assertValidAddress($address) |
|
| 353 | + { |
|
| 354 | + if (!$this->emailValidator->isValid($address, new RFCValidation())) { |
|
| 355 | + throw new Swift_RfcComplianceException('Address in mailbox given ['.$address.'] does not comply with RFC 2822, 3.6.2.'); |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | 358 | } |
@@ -15,99 +15,99 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Mime_Headers_DateHeader extends Swift_Mime_Headers_AbstractHeader |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Date-time value of this Header. |
|
| 20 | - * |
|
| 21 | - * @var DateTimeImmutable |
|
| 22 | - */ |
|
| 23 | - private $dateTime; |
|
| 18 | + /** |
|
| 19 | + * Date-time value of this Header. |
|
| 20 | + * |
|
| 21 | + * @var DateTimeImmutable |
|
| 22 | + */ |
|
| 23 | + private $dateTime; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Creates a new DateHeader with $name. |
|
| 27 | - * |
|
| 28 | - * @param string $name of Header |
|
| 29 | - */ |
|
| 30 | - public function __construct($name) |
|
| 31 | - { |
|
| 32 | - $this->setFieldName($name); |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * Creates a new DateHeader with $name. |
|
| 27 | + * |
|
| 28 | + * @param string $name of Header |
|
| 29 | + */ |
|
| 30 | + public function __construct($name) |
|
| 31 | + { |
|
| 32 | + $this->setFieldName($name); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get the type of Header that this instance represents. |
|
| 37 | - * |
|
| 38 | - * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 39 | - * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 40 | - * |
|
| 41 | - * @return int |
|
| 42 | - */ |
|
| 43 | - public function getFieldType() |
|
| 44 | - { |
|
| 45 | - return self::TYPE_DATE; |
|
| 46 | - } |
|
| 35 | + /** |
|
| 36 | + * Get the type of Header that this instance represents. |
|
| 37 | + * |
|
| 38 | + * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 39 | + * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 40 | + * |
|
| 41 | + * @return int |
|
| 42 | + */ |
|
| 43 | + public function getFieldType() |
|
| 44 | + { |
|
| 45 | + return self::TYPE_DATE; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Set the model for the field body. |
|
| 50 | - * |
|
| 51 | - * @param DateTimeInterface $model |
|
| 52 | - */ |
|
| 53 | - public function setFieldBodyModel($model) |
|
| 54 | - { |
|
| 55 | - $this->setDateTime($model); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Set the model for the field body. |
|
| 50 | + * |
|
| 51 | + * @param DateTimeInterface $model |
|
| 52 | + */ |
|
| 53 | + public function setFieldBodyModel($model) |
|
| 54 | + { |
|
| 55 | + $this->setDateTime($model); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get the model for the field body. |
|
| 60 | - * |
|
| 61 | - * @return DateTimeImmutable |
|
| 62 | - */ |
|
| 63 | - public function getFieldBodyModel() |
|
| 64 | - { |
|
| 65 | - return $this->getDateTime(); |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Get the model for the field body. |
|
| 60 | + * |
|
| 61 | + * @return DateTimeImmutable |
|
| 62 | + */ |
|
| 63 | + public function getFieldBodyModel() |
|
| 64 | + { |
|
| 65 | + return $this->getDateTime(); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get the date-time representing the Date in this Header. |
|
| 70 | - * |
|
| 71 | - * @return DateTimeImmutable |
|
| 72 | - */ |
|
| 73 | - public function getDateTime() |
|
| 74 | - { |
|
| 75 | - return $this->dateTime; |
|
| 76 | - } |
|
| 68 | + /** |
|
| 69 | + * Get the date-time representing the Date in this Header. |
|
| 70 | + * |
|
| 71 | + * @return DateTimeImmutable |
|
| 72 | + */ |
|
| 73 | + public function getDateTime() |
|
| 74 | + { |
|
| 75 | + return $this->dateTime; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Set the date-time of the Date in this Header. |
|
| 80 | - * |
|
| 81 | - * If a DateTime instance is provided, it is converted to DateTimeImmutable. |
|
| 82 | - */ |
|
| 83 | - public function setDateTime(DateTimeInterface $dateTime) |
|
| 84 | - { |
|
| 85 | - $this->clearCachedValueIf($this->getCachedValue() != $dateTime->format(DateTime::RFC2822)); |
|
| 86 | - if ($dateTime instanceof DateTime) { |
|
| 87 | - $immutable = new DateTimeImmutable('@'.$dateTime->getTimestamp()); |
|
| 88 | - $dateTime = $immutable->setTimezone($dateTime->getTimezone()); |
|
| 89 | - } |
|
| 90 | - $this->dateTime = $dateTime; |
|
| 91 | - } |
|
| 78 | + /** |
|
| 79 | + * Set the date-time of the Date in this Header. |
|
| 80 | + * |
|
| 81 | + * If a DateTime instance is provided, it is converted to DateTimeImmutable. |
|
| 82 | + */ |
|
| 83 | + public function setDateTime(DateTimeInterface $dateTime) |
|
| 84 | + { |
|
| 85 | + $this->clearCachedValueIf($this->getCachedValue() != $dateTime->format(DateTime::RFC2822)); |
|
| 86 | + if ($dateTime instanceof DateTime) { |
|
| 87 | + $immutable = new DateTimeImmutable('@'.$dateTime->getTimestamp()); |
|
| 88 | + $dateTime = $immutable->setTimezone($dateTime->getTimezone()); |
|
| 89 | + } |
|
| 90 | + $this->dateTime = $dateTime; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get the string value of the body in this Header. |
|
| 95 | - * |
|
| 96 | - * This is not necessarily RFC 2822 compliant since folding white space will |
|
| 97 | - * not be added at this stage (see {@link toString()} for that). |
|
| 98 | - * |
|
| 99 | - * @see toString() |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - public function getFieldBody() |
|
| 104 | - { |
|
| 105 | - if (!$this->getCachedValue()) { |
|
| 106 | - if (isset($this->dateTime)) { |
|
| 107 | - $this->setCachedValue($this->dateTime->format(DateTime::RFC2822)); |
|
| 108 | - } |
|
| 109 | - } |
|
| 93 | + /** |
|
| 94 | + * Get the string value of the body in this Header. |
|
| 95 | + * |
|
| 96 | + * This is not necessarily RFC 2822 compliant since folding white space will |
|
| 97 | + * not be added at this stage (see {@link toString()} for that). |
|
| 98 | + * |
|
| 99 | + * @see toString() |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + public function getFieldBody() |
|
| 104 | + { |
|
| 105 | + if (!$this->getCachedValue()) { |
|
| 106 | + if (isset($this->dateTime)) { |
|
| 107 | + $this->setCachedValue($this->dateTime->format(DateTime::RFC2822)); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - return $this->getCachedValue(); |
|
| 112 | - } |
|
| 111 | + return $this->getCachedValue(); |
|
| 112 | + } |
|
| 113 | 113 | } |
@@ -15,472 +15,472 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header |
| 17 | 17 | { |
| 18 | - const PHRASE_PATTERN = '(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]+(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?)|(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?"((?:(?:[ \t]*(?:\r\n))?[ \t])?(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21\x23-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])))*(?:(?:[ \t]*(?:\r\n))?[ \t])?"(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?))+?)'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The name of this Header. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - private $name; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Encoder used to encode this Header. |
|
| 29 | - * |
|
| 30 | - * @var Swift_Encoder |
|
| 31 | - */ |
|
| 32 | - private $encoder; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The maximum length of a line in the header. |
|
| 36 | - * |
|
| 37 | - * @var int |
|
| 38 | - */ |
|
| 39 | - private $lineLength = 78; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The language used in this Header. |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - private $lang; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The character set of the text in this Header. |
|
| 50 | - * |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - private $charset = 'utf-8'; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The value of this Header, cached. |
|
| 57 | - * |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - private $cachedValue = null; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Set the character set used in this Header. |
|
| 64 | - * |
|
| 65 | - * @param string $charset |
|
| 66 | - */ |
|
| 67 | - public function setCharset($charset) |
|
| 68 | - { |
|
| 69 | - $this->clearCachedValueIf($charset != $this->charset); |
|
| 70 | - $this->charset = $charset; |
|
| 71 | - if (isset($this->encoder)) { |
|
| 72 | - $this->encoder->charsetChanged($charset); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Get the character set used in this Header. |
|
| 78 | - * |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function getCharset() |
|
| 82 | - { |
|
| 83 | - return $this->charset; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Set the language used in this Header. |
|
| 88 | - * |
|
| 89 | - * For example, for US English, 'en-us'. |
|
| 90 | - * This can be unspecified. |
|
| 91 | - * |
|
| 92 | - * @param string $lang |
|
| 93 | - */ |
|
| 94 | - public function setLanguage($lang) |
|
| 95 | - { |
|
| 96 | - $this->clearCachedValueIf($this->lang != $lang); |
|
| 97 | - $this->lang = $lang; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the language used in this Header. |
|
| 102 | - * |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - public function getLanguage() |
|
| 106 | - { |
|
| 107 | - return $this->lang; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Set the encoder used for encoding the header. |
|
| 112 | - */ |
|
| 113 | - public function setEncoder(Swift_Mime_HeaderEncoder $encoder) |
|
| 114 | - { |
|
| 115 | - $this->encoder = $encoder; |
|
| 116 | - $this->setCachedValue(null); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Get the encoder used for encoding this Header. |
|
| 121 | - * |
|
| 122 | - * @return Swift_Mime_HeaderEncoder |
|
| 123 | - */ |
|
| 124 | - public function getEncoder() |
|
| 125 | - { |
|
| 126 | - return $this->encoder; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get the name of this header (e.g. charset). |
|
| 131 | - * |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function getFieldName() |
|
| 135 | - { |
|
| 136 | - return $this->name; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Set the maximum length of lines in the header (excluding EOL). |
|
| 141 | - * |
|
| 142 | - * @param int $lineLength |
|
| 143 | - */ |
|
| 144 | - public function setMaxLineLength($lineLength) |
|
| 145 | - { |
|
| 146 | - $this->clearCachedValueIf($this->lineLength != $lineLength); |
|
| 147 | - $this->lineLength = $lineLength; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Get the maximum permitted length of lines in this Header. |
|
| 152 | - * |
|
| 153 | - * @return int |
|
| 154 | - */ |
|
| 155 | - public function getMaxLineLength() |
|
| 156 | - { |
|
| 157 | - return $this->lineLength; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Get this Header rendered as a RFC 2822 compliant string. |
|
| 162 | - * |
|
| 163 | - * @return string |
|
| 164 | - * |
|
| 165 | - * @throws Swift_RfcComplianceException |
|
| 166 | - */ |
|
| 167 | - public function toString() |
|
| 168 | - { |
|
| 169 | - return $this->tokensToString($this->toTokens()); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Returns a string representation of this object. |
|
| 174 | - * |
|
| 175 | - * @return string |
|
| 176 | - * |
|
| 177 | - * @see toString() |
|
| 178 | - */ |
|
| 179 | - public function __toString() |
|
| 180 | - { |
|
| 181 | - return $this->toString(); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Set the name of this Header field. |
|
| 186 | - * |
|
| 187 | - * @param string $name |
|
| 188 | - */ |
|
| 189 | - protected function setFieldName($name) |
|
| 190 | - { |
|
| 191 | - $this->name = $name; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Produces a compliant, formatted RFC 2822 'phrase' based on the string given. |
|
| 196 | - * |
|
| 197 | - * @param string $string as displayed |
|
| 198 | - * @param string $charset of the text |
|
| 199 | - * @param bool $shorten the first line to make remove for header name |
|
| 200 | - * |
|
| 201 | - * @return string |
|
| 202 | - */ |
|
| 203 | - protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Swift_Mime_HeaderEncoder $encoder = null, $shorten = false) |
|
| 204 | - { |
|
| 205 | - // Treat token as exactly what was given |
|
| 206 | - $phraseStr = $string; |
|
| 207 | - // If it's not valid |
|
| 208 | - |
|
| 209 | - if (!preg_match('/^'.self::PHRASE_PATTERN.'$/D', $phraseStr)) { |
|
| 210 | - // .. but it is just ascii text, try escaping some characters |
|
| 211 | - // and make it a quoted-string |
|
| 212 | - if (preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $phraseStr)) { |
|
| 213 | - $phraseStr = $this->escapeSpecials($phraseStr, ['"']); |
|
| 214 | - $phraseStr = '"'.$phraseStr.'"'; |
|
| 215 | - } else { |
|
| 216 | - // ... otherwise it needs encoding |
|
| 217 | - // Determine space remaining on line if first line |
|
| 218 | - if ($shorten) { |
|
| 219 | - $usedLength = \strlen($header->getFieldName().': '); |
|
| 220 | - } else { |
|
| 221 | - $usedLength = 0; |
|
| 222 | - } |
|
| 223 | - $phraseStr = $this->encodeWords($header, $string, $usedLength); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - return $phraseStr; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Escape special characters in a string (convert to quoted-pairs). |
|
| 232 | - * |
|
| 233 | - * @param string $token |
|
| 234 | - * @param string[] $include additional chars to escape |
|
| 235 | - * |
|
| 236 | - * @return string |
|
| 237 | - */ |
|
| 238 | - private function escapeSpecials($token, $include = []) |
|
| 239 | - { |
|
| 240 | - foreach (array_merge(['\\'], $include) as $char) { |
|
| 241 | - $token = str_replace($char, '\\'.$char, $token); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return $token; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Encode needed word tokens within a string of input. |
|
| 249 | - * |
|
| 250 | - * @param string $input |
|
| 251 | - * @param string $usedLength optional |
|
| 252 | - * |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - protected function encodeWords(Swift_Mime_Header $header, $input, $usedLength = -1) |
|
| 256 | - { |
|
| 257 | - $value = ''; |
|
| 258 | - |
|
| 259 | - $tokens = $this->getEncodableWordTokens($input); |
|
| 260 | - |
|
| 261 | - foreach ($tokens as $token) { |
|
| 262 | - // See RFC 2822, Sect 2.2 (really 2.2 ??) |
|
| 263 | - if ($this->tokenNeedsEncoding($token)) { |
|
| 264 | - // Don't encode starting WSP |
|
| 265 | - $firstChar = substr($token, 0, 1); |
|
| 266 | - switch ($firstChar) { |
|
| 267 | - case ' ': |
|
| 268 | - case "\t": |
|
| 269 | - $value .= $firstChar; |
|
| 270 | - $token = substr($token, 1); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - if (-1 == $usedLength) { |
|
| 274 | - $usedLength = \strlen($header->getFieldName().': ') + \strlen($value); |
|
| 275 | - } |
|
| 276 | - $value .= $this->getTokenAsEncodedWord($token, $usedLength); |
|
| 277 | - |
|
| 278 | - $header->setMaxLineLength(76); // Forcefully override |
|
| 279 | - } else { |
|
| 280 | - $value .= $token; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return $value; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Test if a token needs to be encoded or not. |
|
| 289 | - * |
|
| 290 | - * @param string $token |
|
| 291 | - * |
|
| 292 | - * @return bool |
|
| 293 | - */ |
|
| 294 | - protected function tokenNeedsEncoding($token) |
|
| 295 | - { |
|
| 296 | - return preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Splits a string into tokens in blocks of words which can be encoded quickly. |
|
| 301 | - * |
|
| 302 | - * @param string $string |
|
| 303 | - * |
|
| 304 | - * @return string[] |
|
| 305 | - */ |
|
| 306 | - protected function getEncodableWordTokens($string) |
|
| 307 | - { |
|
| 308 | - $tokens = []; |
|
| 309 | - |
|
| 310 | - $encodedToken = ''; |
|
| 311 | - // Split at all whitespace boundaries |
|
| 312 | - foreach (preg_split('~(?=[\t ])~', $string ?? '') as $token) { |
|
| 313 | - if ($this->tokenNeedsEncoding($token)) { |
|
| 314 | - $encodedToken .= $token; |
|
| 315 | - } else { |
|
| 316 | - if (\strlen($encodedToken) > 0) { |
|
| 317 | - $tokens[] = $encodedToken; |
|
| 318 | - $encodedToken = ''; |
|
| 319 | - } |
|
| 320 | - $tokens[] = $token; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - if (\strlen($encodedToken)) { |
|
| 324 | - $tokens[] = $encodedToken; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return $tokens; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get a token as an encoded word for safe insertion into headers. |
|
| 332 | - * |
|
| 333 | - * @param string $token token to encode |
|
| 334 | - * @param int $firstLineOffset optional |
|
| 335 | - * |
|
| 336 | - * @return string |
|
| 337 | - */ |
|
| 338 | - protected function getTokenAsEncodedWord($token, $firstLineOffset = 0) |
|
| 339 | - { |
|
| 340 | - // Adjust $firstLineOffset to account for space needed for syntax |
|
| 341 | - $charsetDecl = $this->charset; |
|
| 342 | - if (isset($this->lang)) { |
|
| 343 | - $charsetDecl .= '*'.$this->lang; |
|
| 344 | - } |
|
| 345 | - $encodingWrapperLength = \strlen( |
|
| 346 | - '=?'.$charsetDecl.'?'.$this->encoder->getName().'??=' |
|
| 347 | - ); |
|
| 348 | - |
|
| 349 | - if ($firstLineOffset >= 75) { |
|
| 350 | - //Does this logic need to be here? |
|
| 351 | - $firstLineOffset = 0; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $encodedTextLines = explode("\r\n", |
|
| 355 | - $this->encoder->encodeString( |
|
| 356 | - $token, $firstLineOffset, 75 - $encodingWrapperLength, $this->charset |
|
| 357 | - ) ?? '' |
|
| 358 | - ); |
|
| 359 | - |
|
| 360 | - if ('iso-2022-jp' !== strtolower($this->charset ?? '')) { |
|
| 361 | - // special encoding for iso-2022-jp using mb_encode_mimeheader |
|
| 362 | - foreach ($encodedTextLines as $lineNum => $line) { |
|
| 363 | - $encodedTextLines[$lineNum] = '=?'.$charsetDecl. |
|
| 364 | - '?'.$this->encoder->getName(). |
|
| 365 | - '?'.$line.'?='; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return implode("\r\n ", $encodedTextLines); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Generates tokens from the given string which include CRLF as individual tokens. |
|
| 374 | - * |
|
| 375 | - * @param string $token |
|
| 376 | - * |
|
| 377 | - * @return string[] |
|
| 378 | - */ |
|
| 379 | - protected function generateTokenLines($token) |
|
| 380 | - { |
|
| 381 | - return preg_split('~(\r\n)~', $token ?? '', -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Set a value into the cache. |
|
| 386 | - * |
|
| 387 | - * @param string $value |
|
| 388 | - */ |
|
| 389 | - protected function setCachedValue($value) |
|
| 390 | - { |
|
| 391 | - $this->cachedValue = $value; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Get the value in the cache. |
|
| 396 | - * |
|
| 397 | - * @return string |
|
| 398 | - */ |
|
| 399 | - protected function getCachedValue() |
|
| 400 | - { |
|
| 401 | - return $this->cachedValue; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Clear the cached value if $condition is met. |
|
| 406 | - * |
|
| 407 | - * @param bool $condition |
|
| 408 | - */ |
|
| 409 | - protected function clearCachedValueIf($condition) |
|
| 410 | - { |
|
| 411 | - if ($condition) { |
|
| 412 | - $this->setCachedValue(null); |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Generate a list of all tokens in the final header. |
|
| 418 | - * |
|
| 419 | - * @param string $string The string to tokenize |
|
| 420 | - * |
|
| 421 | - * @return array An array of tokens as strings |
|
| 422 | - */ |
|
| 423 | - protected function toTokens($string = null) |
|
| 424 | - { |
|
| 425 | - if (null === $string) { |
|
| 426 | - $string = $this->getFieldBody(); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - $tokens = []; |
|
| 430 | - |
|
| 431 | - // Generate atoms; split at all invisible boundaries followed by WSP |
|
| 432 | - foreach (preg_split('~(?=[ \t])~', $string ?? '') as $token) { |
|
| 433 | - $newTokens = $this->generateTokenLines($token); |
|
| 434 | - foreach ($newTokens as $newToken) { |
|
| 435 | - $tokens[] = $newToken; |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $tokens; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Takes an array of tokens which appear in the header and turns them into |
|
| 444 | - * an RFC 2822 compliant string, adding FWSP where needed. |
|
| 445 | - * |
|
| 446 | - * @param string[] $tokens |
|
| 447 | - * |
|
| 448 | - * @return string |
|
| 449 | - */ |
|
| 450 | - private function tokensToString(array $tokens) |
|
| 451 | - { |
|
| 452 | - $lineCount = 0; |
|
| 453 | - $headerLines = []; |
|
| 454 | - $headerLines[] = $this->name.': '; |
|
| 455 | - $currentLine = &$headerLines[$lineCount++]; |
|
| 456 | - |
|
| 457 | - // Build all tokens back into compliant header |
|
| 458 | - foreach ($tokens as $i => $token) { |
|
| 459 | - // Line longer than specified maximum or token was just a new line |
|
| 460 | - if (("\r\n" == $token) || |
|
| 461 | - ($i > 0 && \strlen($currentLine.$token) > $this->lineLength) |
|
| 462 | - && 0 < \strlen($currentLine)) { |
|
| 463 | - $headerLines[] = ''; |
|
| 464 | - $currentLine = &$headerLines[$lineCount++]; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - // Append token to the line |
|
| 468 | - if ("\r\n" != $token) { |
|
| 469 | - $currentLine .= $token; |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // Implode with FWS (RFC 2822, 2.2.3) |
|
| 474 | - return implode("\r\n", $headerLines)."\r\n"; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Make a deep copy of object. |
|
| 479 | - */ |
|
| 480 | - public function __clone() |
|
| 481 | - { |
|
| 482 | - if ($this->encoder) { |
|
| 483 | - $this->encoder = clone $this->encoder; |
|
| 484 | - } |
|
| 485 | - } |
|
| 18 | + const PHRASE_PATTERN = '(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]+(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?)|(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?"((?:(?:[ \t]*(?:\r\n))?[ \t])?(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21\x23-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])))*(?:(?:[ \t]*(?:\r\n))?[ \t])?"(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?))+?)'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The name of this Header. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + private $name; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The Encoder used to encode this Header. |
|
| 29 | + * |
|
| 30 | + * @var Swift_Encoder |
|
| 31 | + */ |
|
| 32 | + private $encoder; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The maximum length of a line in the header. |
|
| 36 | + * |
|
| 37 | + * @var int |
|
| 38 | + */ |
|
| 39 | + private $lineLength = 78; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The language used in this Header. |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + private $lang; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The character set of the text in this Header. |
|
| 50 | + * |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + private $charset = 'utf-8'; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The value of this Header, cached. |
|
| 57 | + * |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + private $cachedValue = null; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Set the character set used in this Header. |
|
| 64 | + * |
|
| 65 | + * @param string $charset |
|
| 66 | + */ |
|
| 67 | + public function setCharset($charset) |
|
| 68 | + { |
|
| 69 | + $this->clearCachedValueIf($charset != $this->charset); |
|
| 70 | + $this->charset = $charset; |
|
| 71 | + if (isset($this->encoder)) { |
|
| 72 | + $this->encoder->charsetChanged($charset); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Get the character set used in this Header. |
|
| 78 | + * |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function getCharset() |
|
| 82 | + { |
|
| 83 | + return $this->charset; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Set the language used in this Header. |
|
| 88 | + * |
|
| 89 | + * For example, for US English, 'en-us'. |
|
| 90 | + * This can be unspecified. |
|
| 91 | + * |
|
| 92 | + * @param string $lang |
|
| 93 | + */ |
|
| 94 | + public function setLanguage($lang) |
|
| 95 | + { |
|
| 96 | + $this->clearCachedValueIf($this->lang != $lang); |
|
| 97 | + $this->lang = $lang; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the language used in this Header. |
|
| 102 | + * |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + public function getLanguage() |
|
| 106 | + { |
|
| 107 | + return $this->lang; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Set the encoder used for encoding the header. |
|
| 112 | + */ |
|
| 113 | + public function setEncoder(Swift_Mime_HeaderEncoder $encoder) |
|
| 114 | + { |
|
| 115 | + $this->encoder = $encoder; |
|
| 116 | + $this->setCachedValue(null); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Get the encoder used for encoding this Header. |
|
| 121 | + * |
|
| 122 | + * @return Swift_Mime_HeaderEncoder |
|
| 123 | + */ |
|
| 124 | + public function getEncoder() |
|
| 125 | + { |
|
| 126 | + return $this->encoder; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get the name of this header (e.g. charset). |
|
| 131 | + * |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function getFieldName() |
|
| 135 | + { |
|
| 136 | + return $this->name; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Set the maximum length of lines in the header (excluding EOL). |
|
| 141 | + * |
|
| 142 | + * @param int $lineLength |
|
| 143 | + */ |
|
| 144 | + public function setMaxLineLength($lineLength) |
|
| 145 | + { |
|
| 146 | + $this->clearCachedValueIf($this->lineLength != $lineLength); |
|
| 147 | + $this->lineLength = $lineLength; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Get the maximum permitted length of lines in this Header. |
|
| 152 | + * |
|
| 153 | + * @return int |
|
| 154 | + */ |
|
| 155 | + public function getMaxLineLength() |
|
| 156 | + { |
|
| 157 | + return $this->lineLength; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Get this Header rendered as a RFC 2822 compliant string. |
|
| 162 | + * |
|
| 163 | + * @return string |
|
| 164 | + * |
|
| 165 | + * @throws Swift_RfcComplianceException |
|
| 166 | + */ |
|
| 167 | + public function toString() |
|
| 168 | + { |
|
| 169 | + return $this->tokensToString($this->toTokens()); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Returns a string representation of this object. |
|
| 174 | + * |
|
| 175 | + * @return string |
|
| 176 | + * |
|
| 177 | + * @see toString() |
|
| 178 | + */ |
|
| 179 | + public function __toString() |
|
| 180 | + { |
|
| 181 | + return $this->toString(); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Set the name of this Header field. |
|
| 186 | + * |
|
| 187 | + * @param string $name |
|
| 188 | + */ |
|
| 189 | + protected function setFieldName($name) |
|
| 190 | + { |
|
| 191 | + $this->name = $name; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Produces a compliant, formatted RFC 2822 'phrase' based on the string given. |
|
| 196 | + * |
|
| 197 | + * @param string $string as displayed |
|
| 198 | + * @param string $charset of the text |
|
| 199 | + * @param bool $shorten the first line to make remove for header name |
|
| 200 | + * |
|
| 201 | + * @return string |
|
| 202 | + */ |
|
| 203 | + protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Swift_Mime_HeaderEncoder $encoder = null, $shorten = false) |
|
| 204 | + { |
|
| 205 | + // Treat token as exactly what was given |
|
| 206 | + $phraseStr = $string; |
|
| 207 | + // If it's not valid |
|
| 208 | + |
|
| 209 | + if (!preg_match('/^'.self::PHRASE_PATTERN.'$/D', $phraseStr)) { |
|
| 210 | + // .. but it is just ascii text, try escaping some characters |
|
| 211 | + // and make it a quoted-string |
|
| 212 | + if (preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $phraseStr)) { |
|
| 213 | + $phraseStr = $this->escapeSpecials($phraseStr, ['"']); |
|
| 214 | + $phraseStr = '"'.$phraseStr.'"'; |
|
| 215 | + } else { |
|
| 216 | + // ... otherwise it needs encoding |
|
| 217 | + // Determine space remaining on line if first line |
|
| 218 | + if ($shorten) { |
|
| 219 | + $usedLength = \strlen($header->getFieldName().': '); |
|
| 220 | + } else { |
|
| 221 | + $usedLength = 0; |
|
| 222 | + } |
|
| 223 | + $phraseStr = $this->encodeWords($header, $string, $usedLength); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + return $phraseStr; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Escape special characters in a string (convert to quoted-pairs). |
|
| 232 | + * |
|
| 233 | + * @param string $token |
|
| 234 | + * @param string[] $include additional chars to escape |
|
| 235 | + * |
|
| 236 | + * @return string |
|
| 237 | + */ |
|
| 238 | + private function escapeSpecials($token, $include = []) |
|
| 239 | + { |
|
| 240 | + foreach (array_merge(['\\'], $include) as $char) { |
|
| 241 | + $token = str_replace($char, '\\'.$char, $token); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return $token; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Encode needed word tokens within a string of input. |
|
| 249 | + * |
|
| 250 | + * @param string $input |
|
| 251 | + * @param string $usedLength optional |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + protected function encodeWords(Swift_Mime_Header $header, $input, $usedLength = -1) |
|
| 256 | + { |
|
| 257 | + $value = ''; |
|
| 258 | + |
|
| 259 | + $tokens = $this->getEncodableWordTokens($input); |
|
| 260 | + |
|
| 261 | + foreach ($tokens as $token) { |
|
| 262 | + // See RFC 2822, Sect 2.2 (really 2.2 ??) |
|
| 263 | + if ($this->tokenNeedsEncoding($token)) { |
|
| 264 | + // Don't encode starting WSP |
|
| 265 | + $firstChar = substr($token, 0, 1); |
|
| 266 | + switch ($firstChar) { |
|
| 267 | + case ' ': |
|
| 268 | + case "\t": |
|
| 269 | + $value .= $firstChar; |
|
| 270 | + $token = substr($token, 1); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + if (-1 == $usedLength) { |
|
| 274 | + $usedLength = \strlen($header->getFieldName().': ') + \strlen($value); |
|
| 275 | + } |
|
| 276 | + $value .= $this->getTokenAsEncodedWord($token, $usedLength); |
|
| 277 | + |
|
| 278 | + $header->setMaxLineLength(76); // Forcefully override |
|
| 279 | + } else { |
|
| 280 | + $value .= $token; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return $value; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Test if a token needs to be encoded or not. |
|
| 289 | + * |
|
| 290 | + * @param string $token |
|
| 291 | + * |
|
| 292 | + * @return bool |
|
| 293 | + */ |
|
| 294 | + protected function tokenNeedsEncoding($token) |
|
| 295 | + { |
|
| 296 | + return preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Splits a string into tokens in blocks of words which can be encoded quickly. |
|
| 301 | + * |
|
| 302 | + * @param string $string |
|
| 303 | + * |
|
| 304 | + * @return string[] |
|
| 305 | + */ |
|
| 306 | + protected function getEncodableWordTokens($string) |
|
| 307 | + { |
|
| 308 | + $tokens = []; |
|
| 309 | + |
|
| 310 | + $encodedToken = ''; |
|
| 311 | + // Split at all whitespace boundaries |
|
| 312 | + foreach (preg_split('~(?=[\t ])~', $string ?? '') as $token) { |
|
| 313 | + if ($this->tokenNeedsEncoding($token)) { |
|
| 314 | + $encodedToken .= $token; |
|
| 315 | + } else { |
|
| 316 | + if (\strlen($encodedToken) > 0) { |
|
| 317 | + $tokens[] = $encodedToken; |
|
| 318 | + $encodedToken = ''; |
|
| 319 | + } |
|
| 320 | + $tokens[] = $token; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + if (\strlen($encodedToken)) { |
|
| 324 | + $tokens[] = $encodedToken; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return $tokens; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get a token as an encoded word for safe insertion into headers. |
|
| 332 | + * |
|
| 333 | + * @param string $token token to encode |
|
| 334 | + * @param int $firstLineOffset optional |
|
| 335 | + * |
|
| 336 | + * @return string |
|
| 337 | + */ |
|
| 338 | + protected function getTokenAsEncodedWord($token, $firstLineOffset = 0) |
|
| 339 | + { |
|
| 340 | + // Adjust $firstLineOffset to account for space needed for syntax |
|
| 341 | + $charsetDecl = $this->charset; |
|
| 342 | + if (isset($this->lang)) { |
|
| 343 | + $charsetDecl .= '*'.$this->lang; |
|
| 344 | + } |
|
| 345 | + $encodingWrapperLength = \strlen( |
|
| 346 | + '=?'.$charsetDecl.'?'.$this->encoder->getName().'??=' |
|
| 347 | + ); |
|
| 348 | + |
|
| 349 | + if ($firstLineOffset >= 75) { |
|
| 350 | + //Does this logic need to be here? |
|
| 351 | + $firstLineOffset = 0; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $encodedTextLines = explode("\r\n", |
|
| 355 | + $this->encoder->encodeString( |
|
| 356 | + $token, $firstLineOffset, 75 - $encodingWrapperLength, $this->charset |
|
| 357 | + ) ?? '' |
|
| 358 | + ); |
|
| 359 | + |
|
| 360 | + if ('iso-2022-jp' !== strtolower($this->charset ?? '')) { |
|
| 361 | + // special encoding for iso-2022-jp using mb_encode_mimeheader |
|
| 362 | + foreach ($encodedTextLines as $lineNum => $line) { |
|
| 363 | + $encodedTextLines[$lineNum] = '=?'.$charsetDecl. |
|
| 364 | + '?'.$this->encoder->getName(). |
|
| 365 | + '?'.$line.'?='; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return implode("\r\n ", $encodedTextLines); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Generates tokens from the given string which include CRLF as individual tokens. |
|
| 374 | + * |
|
| 375 | + * @param string $token |
|
| 376 | + * |
|
| 377 | + * @return string[] |
|
| 378 | + */ |
|
| 379 | + protected function generateTokenLines($token) |
|
| 380 | + { |
|
| 381 | + return preg_split('~(\r\n)~', $token ?? '', -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Set a value into the cache. |
|
| 386 | + * |
|
| 387 | + * @param string $value |
|
| 388 | + */ |
|
| 389 | + protected function setCachedValue($value) |
|
| 390 | + { |
|
| 391 | + $this->cachedValue = $value; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Get the value in the cache. |
|
| 396 | + * |
|
| 397 | + * @return string |
|
| 398 | + */ |
|
| 399 | + protected function getCachedValue() |
|
| 400 | + { |
|
| 401 | + return $this->cachedValue; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Clear the cached value if $condition is met. |
|
| 406 | + * |
|
| 407 | + * @param bool $condition |
|
| 408 | + */ |
|
| 409 | + protected function clearCachedValueIf($condition) |
|
| 410 | + { |
|
| 411 | + if ($condition) { |
|
| 412 | + $this->setCachedValue(null); |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Generate a list of all tokens in the final header. |
|
| 418 | + * |
|
| 419 | + * @param string $string The string to tokenize |
|
| 420 | + * |
|
| 421 | + * @return array An array of tokens as strings |
|
| 422 | + */ |
|
| 423 | + protected function toTokens($string = null) |
|
| 424 | + { |
|
| 425 | + if (null === $string) { |
|
| 426 | + $string = $this->getFieldBody(); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + $tokens = []; |
|
| 430 | + |
|
| 431 | + // Generate atoms; split at all invisible boundaries followed by WSP |
|
| 432 | + foreach (preg_split('~(?=[ \t])~', $string ?? '') as $token) { |
|
| 433 | + $newTokens = $this->generateTokenLines($token); |
|
| 434 | + foreach ($newTokens as $newToken) { |
|
| 435 | + $tokens[] = $newToken; |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $tokens; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Takes an array of tokens which appear in the header and turns them into |
|
| 444 | + * an RFC 2822 compliant string, adding FWSP where needed. |
|
| 445 | + * |
|
| 446 | + * @param string[] $tokens |
|
| 447 | + * |
|
| 448 | + * @return string |
|
| 449 | + */ |
|
| 450 | + private function tokensToString(array $tokens) |
|
| 451 | + { |
|
| 452 | + $lineCount = 0; |
|
| 453 | + $headerLines = []; |
|
| 454 | + $headerLines[] = $this->name.': '; |
|
| 455 | + $currentLine = &$headerLines[$lineCount++]; |
|
| 456 | + |
|
| 457 | + // Build all tokens back into compliant header |
|
| 458 | + foreach ($tokens as $i => $token) { |
|
| 459 | + // Line longer than specified maximum or token was just a new line |
|
| 460 | + if (("\r\n" == $token) || |
|
| 461 | + ($i > 0 && \strlen($currentLine.$token) > $this->lineLength) |
|
| 462 | + && 0 < \strlen($currentLine)) { |
|
| 463 | + $headerLines[] = ''; |
|
| 464 | + $currentLine = &$headerLines[$lineCount++]; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + // Append token to the line |
|
| 468 | + if ("\r\n" != $token) { |
|
| 469 | + $currentLine .= $token; |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // Implode with FWS (RFC 2822, 2.2.3) |
|
| 474 | + return implode("\r\n", $headerLines)."\r\n"; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Make a deep copy of object. |
|
| 479 | + */ |
|
| 480 | + public function __clone() |
|
| 481 | + { |
|
| 482 | + if ($this->encoder) { |
|
| 483 | + $this->encoder = clone $this->encoder; |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | 486 | } |
@@ -15,10 +15,10 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Swift_Mime_CharsetObserver |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Notify this observer that the entity's charset has changed. |
|
| 20 | - * |
|
| 21 | - * @param string $charset |
|
| 22 | - */ |
|
| 23 | - public function charsetChanged($charset); |
|
| 18 | + /** |
|
| 19 | + * Notify this observer that the entity's charset has changed. |
|
| 20 | + * |
|
| 21 | + * @param string $charset |
|
| 22 | + */ |
|
| 23 | + public function charsetChanged($charset); |
|
| 24 | 24 | } |
@@ -15,79 +15,79 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Swift_Mime_Header |
| 17 | 17 | { |
| 18 | - /** Text headers */ |
|
| 19 | - const TYPE_TEXT = 2; |
|
| 18 | + /** Text headers */ |
|
| 19 | + const TYPE_TEXT = 2; |
|
| 20 | 20 | |
| 21 | - /** headers (text + params) */ |
|
| 22 | - const TYPE_PARAMETERIZED = 6; |
|
| 21 | + /** headers (text + params) */ |
|
| 22 | + const TYPE_PARAMETERIZED = 6; |
|
| 23 | 23 | |
| 24 | - /** Mailbox and address headers */ |
|
| 25 | - const TYPE_MAILBOX = 8; |
|
| 24 | + /** Mailbox and address headers */ |
|
| 25 | + const TYPE_MAILBOX = 8; |
|
| 26 | 26 | |
| 27 | - /** Date and time headers */ |
|
| 28 | - const TYPE_DATE = 16; |
|
| 27 | + /** Date and time headers */ |
|
| 28 | + const TYPE_DATE = 16; |
|
| 29 | 29 | |
| 30 | - /** Identification headers */ |
|
| 31 | - const TYPE_ID = 32; |
|
| 30 | + /** Identification headers */ |
|
| 31 | + const TYPE_ID = 32; |
|
| 32 | 32 | |
| 33 | - /** Address path headers */ |
|
| 34 | - const TYPE_PATH = 64; |
|
| 33 | + /** Address path headers */ |
|
| 34 | + const TYPE_PATH = 64; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get the type of Header that this instance represents. |
|
| 38 | - * |
|
| 39 | - * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 40 | - * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 41 | - * |
|
| 42 | - * @return int |
|
| 43 | - */ |
|
| 44 | - public function getFieldType(); |
|
| 36 | + /** |
|
| 37 | + * Get the type of Header that this instance represents. |
|
| 38 | + * |
|
| 39 | + * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX |
|
| 40 | + * @see TYPE_DATE, TYPE_ID, TYPE_PATH |
|
| 41 | + * |
|
| 42 | + * @return int |
|
| 43 | + */ |
|
| 44 | + public function getFieldType(); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Set the model for the field body. |
|
| 48 | - * |
|
| 49 | - * The actual types needed will vary depending upon the type of Header. |
|
| 50 | - * |
|
| 51 | - * @param mixed $model |
|
| 52 | - */ |
|
| 53 | - public function setFieldBodyModel($model); |
|
| 46 | + /** |
|
| 47 | + * Set the model for the field body. |
|
| 48 | + * |
|
| 49 | + * The actual types needed will vary depending upon the type of Header. |
|
| 50 | + * |
|
| 51 | + * @param mixed $model |
|
| 52 | + */ |
|
| 53 | + public function setFieldBodyModel($model); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Set the charset used when rendering the Header. |
|
| 57 | - * |
|
| 58 | - * @param string $charset |
|
| 59 | - */ |
|
| 60 | - public function setCharset($charset); |
|
| 55 | + /** |
|
| 56 | + * Set the charset used when rendering the Header. |
|
| 57 | + * |
|
| 58 | + * @param string $charset |
|
| 59 | + */ |
|
| 60 | + public function setCharset($charset); |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get the model for the field body. |
|
| 64 | - * |
|
| 65 | - * The return type depends on the specifics of the Header. |
|
| 66 | - * |
|
| 67 | - * @return mixed |
|
| 68 | - */ |
|
| 69 | - public function getFieldBodyModel(); |
|
| 62 | + /** |
|
| 63 | + * Get the model for the field body. |
|
| 64 | + * |
|
| 65 | + * The return type depends on the specifics of the Header. |
|
| 66 | + * |
|
| 67 | + * @return mixed |
|
| 68 | + */ |
|
| 69 | + public function getFieldBodyModel(); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get the name of this header (e.g. Subject). |
|
| 73 | - * |
|
| 74 | - * The name is an identifier and as such will be immutable. |
|
| 75 | - * |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public function getFieldName(); |
|
| 71 | + /** |
|
| 72 | + * Get the name of this header (e.g. Subject). |
|
| 73 | + * |
|
| 74 | + * The name is an identifier and as such will be immutable. |
|
| 75 | + * |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public function getFieldName(); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Get the field body, prepared for folding into a final header value. |
|
| 82 | - * |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public function getFieldBody(); |
|
| 80 | + /** |
|
| 81 | + * Get the field body, prepared for folding into a final header value. |
|
| 82 | + * |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public function getFieldBody(); |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Get this Header rendered as a compliant string, including trailing CRLF. |
|
| 89 | - * |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - public function toString(); |
|
| 87 | + /** |
|
| 88 | + * Get this Header rendered as a compliant string, including trailing CRLF. |
|
| 89 | + * |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + public function toString(); |
|
| 93 | 93 | } |
@@ -15,385 +15,385 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Mime_SimpleHeaderSet implements Swift_Mime_CharsetObserver |
| 17 | 17 | { |
| 18 | - /** HeaderFactory */ |
|
| 19 | - private $factory; |
|
| 20 | - |
|
| 21 | - /** Collection of set Headers */ |
|
| 22 | - private $headers = []; |
|
| 23 | - |
|
| 24 | - /** Field ordering details */ |
|
| 25 | - private $order = []; |
|
| 26 | - |
|
| 27 | - /** List of fields which are required to be displayed */ |
|
| 28 | - private $required = []; |
|
| 29 | - |
|
| 30 | - /** The charset used by Headers */ |
|
| 31 | - private $charset; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Create a new SimpleHeaderSet with the given $factory. |
|
| 35 | - * |
|
| 36 | - * @param string $charset |
|
| 37 | - */ |
|
| 38 | - public function __construct(Swift_Mime_SimpleHeaderFactory $factory, $charset = null) |
|
| 39 | - { |
|
| 40 | - $this->factory = $factory; |
|
| 41 | - if (isset($charset)) { |
|
| 42 | - $this->setCharset($charset); |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function newInstance() |
|
| 47 | - { |
|
| 48 | - return new self($this->factory); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Set the charset used by these headers. |
|
| 53 | - * |
|
| 54 | - * @param string $charset |
|
| 55 | - */ |
|
| 56 | - public function setCharset($charset) |
|
| 57 | - { |
|
| 58 | - $this->charset = $charset; |
|
| 59 | - $this->factory->charsetChanged($charset); |
|
| 60 | - $this->notifyHeadersOfCharset($charset); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Add a new Mailbox Header with a list of $addresses. |
|
| 65 | - * |
|
| 66 | - * @param string $name |
|
| 67 | - * @param array|string $addresses |
|
| 68 | - */ |
|
| 69 | - public function addMailboxHeader($name, $addresses = null) |
|
| 70 | - { |
|
| 71 | - $this->storeHeader($name, $this->factory->createMailboxHeader($name, $addresses)); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Add a new Date header using $dateTime. |
|
| 76 | - * |
|
| 77 | - * @param string $name |
|
| 78 | - */ |
|
| 79 | - public function addDateHeader($name, DateTimeInterface $dateTime = null) |
|
| 80 | - { |
|
| 81 | - $this->storeHeader($name, $this->factory->createDateHeader($name, $dateTime)); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Add a new basic text header with $name and $value. |
|
| 86 | - * |
|
| 87 | - * @param string $name |
|
| 88 | - * @param string $value |
|
| 89 | - */ |
|
| 90 | - public function addTextHeader($name, $value = null) |
|
| 91 | - { |
|
| 92 | - $this->storeHeader($name, $this->factory->createTextHeader($name, $value)); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Add a new ParameterizedHeader with $name, $value and $params. |
|
| 97 | - * |
|
| 98 | - * @param string $name |
|
| 99 | - * @param string $value |
|
| 100 | - * @param array $params |
|
| 101 | - */ |
|
| 102 | - public function addParameterizedHeader($name, $value = null, $params = []) |
|
| 103 | - { |
|
| 104 | - $this->storeHeader($name, $this->factory->createParameterizedHeader($name, $value, $params)); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Add a new ID header for Message-ID or Content-ID. |
|
| 109 | - * |
|
| 110 | - * @param string $name |
|
| 111 | - * @param string|array $ids |
|
| 112 | - */ |
|
| 113 | - public function addIdHeader($name, $ids = null) |
|
| 114 | - { |
|
| 115 | - $this->storeHeader($name, $this->factory->createIdHeader($name, $ids)); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Add a new Path header with an address (path) in it. |
|
| 120 | - * |
|
| 121 | - * @param string $name |
|
| 122 | - * @param string $path |
|
| 123 | - */ |
|
| 124 | - public function addPathHeader($name, $path = null) |
|
| 125 | - { |
|
| 126 | - $this->storeHeader($name, $this->factory->createPathHeader($name, $path)); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Returns true if at least one header with the given $name exists. |
|
| 131 | - * |
|
| 132 | - * If multiple headers match, the actual one may be specified by $index. |
|
| 133 | - * |
|
| 134 | - * @param string $name |
|
| 135 | - * @param int $index |
|
| 136 | - * |
|
| 137 | - * @return bool |
|
| 138 | - */ |
|
| 139 | - public function has($name, $index = 0) |
|
| 140 | - { |
|
| 141 | - $lowerName = strtolower($name ?? ''); |
|
| 142 | - |
|
| 143 | - if (!\array_key_exists($lowerName, $this->headers)) { |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - if (\func_num_args() < 2) { |
|
| 148 | - // index was not specified, so we only need to check that there is at least one header value set |
|
| 149 | - return (bool) \count($this->headers[$lowerName]); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return \array_key_exists($index, $this->headers[$lowerName]); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Set a header in the HeaderSet. |
|
| 157 | - * |
|
| 158 | - * The header may be a previously fetched header via {@link get()} or it may |
|
| 159 | - * be one that has been created separately. |
|
| 160 | - * |
|
| 161 | - * If $index is specified, the header will be inserted into the set at this |
|
| 162 | - * offset. |
|
| 163 | - * |
|
| 164 | - * @param int $index |
|
| 165 | - */ |
|
| 166 | - public function set(Swift_Mime_Header $header, $index = 0) |
|
| 167 | - { |
|
| 168 | - $this->storeHeader($header->getFieldName(), $header, $index); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get the header with the given $name. |
|
| 173 | - * |
|
| 174 | - * If multiple headers match, the actual one may be specified by $index. |
|
| 175 | - * Returns NULL if none present. |
|
| 176 | - * |
|
| 177 | - * @param string $name |
|
| 178 | - * @param int $index |
|
| 179 | - * |
|
| 180 | - * @return Swift_Mime_Header|null |
|
| 181 | - */ |
|
| 182 | - public function get($name, $index = 0) |
|
| 183 | - { |
|
| 184 | - $name = strtolower($name ?? ''); |
|
| 185 | - |
|
| 186 | - if (\func_num_args() < 2) { |
|
| 187 | - if ($this->has($name)) { |
|
| 188 | - $values = array_values($this->headers[$name]); |
|
| 189 | - |
|
| 190 | - return array_shift($values); |
|
| 191 | - } |
|
| 192 | - } else { |
|
| 193 | - if ($this->has($name, $index)) { |
|
| 194 | - return $this->headers[$name][$index]; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Get all headers with the given $name. |
|
| 201 | - * |
|
| 202 | - * @param string $name |
|
| 203 | - * |
|
| 204 | - * @return array |
|
| 205 | - */ |
|
| 206 | - public function getAll($name = null) |
|
| 207 | - { |
|
| 208 | - if (!isset($name)) { |
|
| 209 | - $headers = []; |
|
| 210 | - foreach ($this->headers as $collection) { |
|
| 211 | - $headers = array_merge($headers, $collection); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return $headers; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - $lowerName = strtolower($name ?? ''); |
|
| 218 | - if (!\array_key_exists($lowerName, $this->headers)) { |
|
| 219 | - return []; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $this->headers[$lowerName]; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Return the name of all Headers. |
|
| 227 | - * |
|
| 228 | - * @return array |
|
| 229 | - */ |
|
| 230 | - public function listAll() |
|
| 231 | - { |
|
| 232 | - $headers = $this->headers; |
|
| 233 | - if ($this->canSort()) { |
|
| 234 | - uksort($headers, [$this, 'sortHeaders']); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return array_keys($headers); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Remove the header with the given $name if it's set. |
|
| 242 | - * |
|
| 243 | - * If multiple headers match, the actual one may be specified by $index. |
|
| 244 | - * |
|
| 245 | - * @param string $name |
|
| 246 | - * @param int $index |
|
| 247 | - */ |
|
| 248 | - public function remove($name, $index = 0) |
|
| 249 | - { |
|
| 250 | - $lowerName = strtolower($name ?? ''); |
|
| 251 | - unset($this->headers[$lowerName][$index]); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Remove all headers with the given $name. |
|
| 256 | - * |
|
| 257 | - * @param string $name |
|
| 258 | - */ |
|
| 259 | - public function removeAll($name) |
|
| 260 | - { |
|
| 261 | - $lowerName = strtolower($name ?? ''); |
|
| 262 | - unset($this->headers[$lowerName]); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Define a list of Header names as an array in the correct order. |
|
| 267 | - * |
|
| 268 | - * These Headers will be output in the given order where present. |
|
| 269 | - */ |
|
| 270 | - public function defineOrdering(array $sequence) |
|
| 271 | - { |
|
| 272 | - $this->order = array_flip(array_map('strtolower', $sequence)); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Set a list of header names which must always be displayed when set. |
|
| 277 | - * |
|
| 278 | - * Usually headers without a field value won't be output unless set here. |
|
| 279 | - */ |
|
| 280 | - public function setAlwaysDisplayed(array $names) |
|
| 281 | - { |
|
| 282 | - $this->required = array_flip(array_map('strtolower', $names)); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Notify this observer that the entity's charset has changed. |
|
| 287 | - * |
|
| 288 | - * @param string $charset |
|
| 289 | - */ |
|
| 290 | - public function charsetChanged($charset) |
|
| 291 | - { |
|
| 292 | - $this->setCharset($charset); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Returns a string with a representation of all headers. |
|
| 297 | - * |
|
| 298 | - * @return string |
|
| 299 | - */ |
|
| 300 | - public function toString() |
|
| 301 | - { |
|
| 302 | - $string = ''; |
|
| 303 | - $headers = $this->headers; |
|
| 304 | - if ($this->canSort()) { |
|
| 305 | - uksort($headers, [$this, 'sortHeaders']); |
|
| 306 | - } |
|
| 307 | - foreach ($headers as $collection) { |
|
| 308 | - foreach ($collection as $header) { |
|
| 309 | - if ($this->isDisplayed($header) || '' != $header->getFieldBody()) { |
|
| 310 | - $string .= $header->toString(); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - return $string; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Returns a string representation of this object. |
|
| 320 | - * |
|
| 321 | - * @return string |
|
| 322 | - * |
|
| 323 | - * @see toString() |
|
| 324 | - */ |
|
| 325 | - public function __toString() |
|
| 326 | - { |
|
| 327 | - return $this->toString(); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** Save a Header to the internal collection */ |
|
| 331 | - private function storeHeader($name, Swift_Mime_Header $header, $offset = null) |
|
| 332 | - { |
|
| 333 | - if (!isset($this->headers[strtolower($name ?? '')])) { |
|
| 334 | - $this->headers[strtolower($name ?? '')] = []; |
|
| 335 | - } |
|
| 336 | - if (!isset($offset)) { |
|
| 337 | - $this->headers[strtolower($name ?? '')][] = $header; |
|
| 338 | - } else { |
|
| 339 | - $this->headers[strtolower($name ?? '')][$offset] = $header; |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** Test if the headers can be sorted */ |
|
| 344 | - private function canSort() |
|
| 345 | - { |
|
| 346 | - return \count($this->order) > 0; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** uksort() algorithm for Header ordering */ |
|
| 350 | - private function sortHeaders($a, $b) |
|
| 351 | - { |
|
| 352 | - $lowerA = strtolower($a ?? ''); |
|
| 353 | - $lowerB = strtolower($b ?? ''); |
|
| 354 | - $aPos = \array_key_exists($lowerA, $this->order) ? $this->order[$lowerA] : -1; |
|
| 355 | - $bPos = \array_key_exists($lowerB, $this->order) ? $this->order[$lowerB] : -1; |
|
| 356 | - |
|
| 357 | - if (-1 === $aPos && -1 === $bPos) { |
|
| 358 | - // just be sure to be determinist here |
|
| 359 | - return $a > $b ? -1 : 1; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if (-1 == $aPos) { |
|
| 363 | - return 1; |
|
| 364 | - } elseif (-1 == $bPos) { |
|
| 365 | - return -1; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - return $aPos < $bPos ? -1 : 1; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** Test if the given Header is always displayed */ |
|
| 372 | - private function isDisplayed(Swift_Mime_Header $header) |
|
| 373 | - { |
|
| 374 | - return \array_key_exists(strtolower($header->getFieldName() ?? ''), $this->required); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** Notify all Headers of the new charset */ |
|
| 378 | - private function notifyHeadersOfCharset($charset) |
|
| 379 | - { |
|
| 380 | - foreach ($this->headers as $headerGroup) { |
|
| 381 | - foreach ($headerGroup as $header) { |
|
| 382 | - $header->setCharset($charset); |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Make a deep copy of object. |
|
| 389 | - */ |
|
| 390 | - public function __clone() |
|
| 391 | - { |
|
| 392 | - $this->factory = clone $this->factory; |
|
| 393 | - foreach ($this->headers as $groupKey => $headerGroup) { |
|
| 394 | - foreach ($headerGroup as $key => $header) { |
|
| 395 | - $this->headers[$groupKey][$key] = clone $header; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - } |
|
| 18 | + /** HeaderFactory */ |
|
| 19 | + private $factory; |
|
| 20 | + |
|
| 21 | + /** Collection of set Headers */ |
|
| 22 | + private $headers = []; |
|
| 23 | + |
|
| 24 | + /** Field ordering details */ |
|
| 25 | + private $order = []; |
|
| 26 | + |
|
| 27 | + /** List of fields which are required to be displayed */ |
|
| 28 | + private $required = []; |
|
| 29 | + |
|
| 30 | + /** The charset used by Headers */ |
|
| 31 | + private $charset; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Create a new SimpleHeaderSet with the given $factory. |
|
| 35 | + * |
|
| 36 | + * @param string $charset |
|
| 37 | + */ |
|
| 38 | + public function __construct(Swift_Mime_SimpleHeaderFactory $factory, $charset = null) |
|
| 39 | + { |
|
| 40 | + $this->factory = $factory; |
|
| 41 | + if (isset($charset)) { |
|
| 42 | + $this->setCharset($charset); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function newInstance() |
|
| 47 | + { |
|
| 48 | + return new self($this->factory); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Set the charset used by these headers. |
|
| 53 | + * |
|
| 54 | + * @param string $charset |
|
| 55 | + */ |
|
| 56 | + public function setCharset($charset) |
|
| 57 | + { |
|
| 58 | + $this->charset = $charset; |
|
| 59 | + $this->factory->charsetChanged($charset); |
|
| 60 | + $this->notifyHeadersOfCharset($charset); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Add a new Mailbox Header with a list of $addresses. |
|
| 65 | + * |
|
| 66 | + * @param string $name |
|
| 67 | + * @param array|string $addresses |
|
| 68 | + */ |
|
| 69 | + public function addMailboxHeader($name, $addresses = null) |
|
| 70 | + { |
|
| 71 | + $this->storeHeader($name, $this->factory->createMailboxHeader($name, $addresses)); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Add a new Date header using $dateTime. |
|
| 76 | + * |
|
| 77 | + * @param string $name |
|
| 78 | + */ |
|
| 79 | + public function addDateHeader($name, DateTimeInterface $dateTime = null) |
|
| 80 | + { |
|
| 81 | + $this->storeHeader($name, $this->factory->createDateHeader($name, $dateTime)); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Add a new basic text header with $name and $value. |
|
| 86 | + * |
|
| 87 | + * @param string $name |
|
| 88 | + * @param string $value |
|
| 89 | + */ |
|
| 90 | + public function addTextHeader($name, $value = null) |
|
| 91 | + { |
|
| 92 | + $this->storeHeader($name, $this->factory->createTextHeader($name, $value)); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Add a new ParameterizedHeader with $name, $value and $params. |
|
| 97 | + * |
|
| 98 | + * @param string $name |
|
| 99 | + * @param string $value |
|
| 100 | + * @param array $params |
|
| 101 | + */ |
|
| 102 | + public function addParameterizedHeader($name, $value = null, $params = []) |
|
| 103 | + { |
|
| 104 | + $this->storeHeader($name, $this->factory->createParameterizedHeader($name, $value, $params)); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Add a new ID header for Message-ID or Content-ID. |
|
| 109 | + * |
|
| 110 | + * @param string $name |
|
| 111 | + * @param string|array $ids |
|
| 112 | + */ |
|
| 113 | + public function addIdHeader($name, $ids = null) |
|
| 114 | + { |
|
| 115 | + $this->storeHeader($name, $this->factory->createIdHeader($name, $ids)); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Add a new Path header with an address (path) in it. |
|
| 120 | + * |
|
| 121 | + * @param string $name |
|
| 122 | + * @param string $path |
|
| 123 | + */ |
|
| 124 | + public function addPathHeader($name, $path = null) |
|
| 125 | + { |
|
| 126 | + $this->storeHeader($name, $this->factory->createPathHeader($name, $path)); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Returns true if at least one header with the given $name exists. |
|
| 131 | + * |
|
| 132 | + * If multiple headers match, the actual one may be specified by $index. |
|
| 133 | + * |
|
| 134 | + * @param string $name |
|
| 135 | + * @param int $index |
|
| 136 | + * |
|
| 137 | + * @return bool |
|
| 138 | + */ |
|
| 139 | + public function has($name, $index = 0) |
|
| 140 | + { |
|
| 141 | + $lowerName = strtolower($name ?? ''); |
|
| 142 | + |
|
| 143 | + if (!\array_key_exists($lowerName, $this->headers)) { |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + if (\func_num_args() < 2) { |
|
| 148 | + // index was not specified, so we only need to check that there is at least one header value set |
|
| 149 | + return (bool) \count($this->headers[$lowerName]); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return \array_key_exists($index, $this->headers[$lowerName]); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Set a header in the HeaderSet. |
|
| 157 | + * |
|
| 158 | + * The header may be a previously fetched header via {@link get()} or it may |
|
| 159 | + * be one that has been created separately. |
|
| 160 | + * |
|
| 161 | + * If $index is specified, the header will be inserted into the set at this |
|
| 162 | + * offset. |
|
| 163 | + * |
|
| 164 | + * @param int $index |
|
| 165 | + */ |
|
| 166 | + public function set(Swift_Mime_Header $header, $index = 0) |
|
| 167 | + { |
|
| 168 | + $this->storeHeader($header->getFieldName(), $header, $index); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get the header with the given $name. |
|
| 173 | + * |
|
| 174 | + * If multiple headers match, the actual one may be specified by $index. |
|
| 175 | + * Returns NULL if none present. |
|
| 176 | + * |
|
| 177 | + * @param string $name |
|
| 178 | + * @param int $index |
|
| 179 | + * |
|
| 180 | + * @return Swift_Mime_Header|null |
|
| 181 | + */ |
|
| 182 | + public function get($name, $index = 0) |
|
| 183 | + { |
|
| 184 | + $name = strtolower($name ?? ''); |
|
| 185 | + |
|
| 186 | + if (\func_num_args() < 2) { |
|
| 187 | + if ($this->has($name)) { |
|
| 188 | + $values = array_values($this->headers[$name]); |
|
| 189 | + |
|
| 190 | + return array_shift($values); |
|
| 191 | + } |
|
| 192 | + } else { |
|
| 193 | + if ($this->has($name, $index)) { |
|
| 194 | + return $this->headers[$name][$index]; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Get all headers with the given $name. |
|
| 201 | + * |
|
| 202 | + * @param string $name |
|
| 203 | + * |
|
| 204 | + * @return array |
|
| 205 | + */ |
|
| 206 | + public function getAll($name = null) |
|
| 207 | + { |
|
| 208 | + if (!isset($name)) { |
|
| 209 | + $headers = []; |
|
| 210 | + foreach ($this->headers as $collection) { |
|
| 211 | + $headers = array_merge($headers, $collection); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return $headers; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + $lowerName = strtolower($name ?? ''); |
|
| 218 | + if (!\array_key_exists($lowerName, $this->headers)) { |
|
| 219 | + return []; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $this->headers[$lowerName]; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Return the name of all Headers. |
|
| 227 | + * |
|
| 228 | + * @return array |
|
| 229 | + */ |
|
| 230 | + public function listAll() |
|
| 231 | + { |
|
| 232 | + $headers = $this->headers; |
|
| 233 | + if ($this->canSort()) { |
|
| 234 | + uksort($headers, [$this, 'sortHeaders']); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return array_keys($headers); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Remove the header with the given $name if it's set. |
|
| 242 | + * |
|
| 243 | + * If multiple headers match, the actual one may be specified by $index. |
|
| 244 | + * |
|
| 245 | + * @param string $name |
|
| 246 | + * @param int $index |
|
| 247 | + */ |
|
| 248 | + public function remove($name, $index = 0) |
|
| 249 | + { |
|
| 250 | + $lowerName = strtolower($name ?? ''); |
|
| 251 | + unset($this->headers[$lowerName][$index]); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Remove all headers with the given $name. |
|
| 256 | + * |
|
| 257 | + * @param string $name |
|
| 258 | + */ |
|
| 259 | + public function removeAll($name) |
|
| 260 | + { |
|
| 261 | + $lowerName = strtolower($name ?? ''); |
|
| 262 | + unset($this->headers[$lowerName]); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Define a list of Header names as an array in the correct order. |
|
| 267 | + * |
|
| 268 | + * These Headers will be output in the given order where present. |
|
| 269 | + */ |
|
| 270 | + public function defineOrdering(array $sequence) |
|
| 271 | + { |
|
| 272 | + $this->order = array_flip(array_map('strtolower', $sequence)); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Set a list of header names which must always be displayed when set. |
|
| 277 | + * |
|
| 278 | + * Usually headers without a field value won't be output unless set here. |
|
| 279 | + */ |
|
| 280 | + public function setAlwaysDisplayed(array $names) |
|
| 281 | + { |
|
| 282 | + $this->required = array_flip(array_map('strtolower', $names)); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Notify this observer that the entity's charset has changed. |
|
| 287 | + * |
|
| 288 | + * @param string $charset |
|
| 289 | + */ |
|
| 290 | + public function charsetChanged($charset) |
|
| 291 | + { |
|
| 292 | + $this->setCharset($charset); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Returns a string with a representation of all headers. |
|
| 297 | + * |
|
| 298 | + * @return string |
|
| 299 | + */ |
|
| 300 | + public function toString() |
|
| 301 | + { |
|
| 302 | + $string = ''; |
|
| 303 | + $headers = $this->headers; |
|
| 304 | + if ($this->canSort()) { |
|
| 305 | + uksort($headers, [$this, 'sortHeaders']); |
|
| 306 | + } |
|
| 307 | + foreach ($headers as $collection) { |
|
| 308 | + foreach ($collection as $header) { |
|
| 309 | + if ($this->isDisplayed($header) || '' != $header->getFieldBody()) { |
|
| 310 | + $string .= $header->toString(); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + return $string; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Returns a string representation of this object. |
|
| 320 | + * |
|
| 321 | + * @return string |
|
| 322 | + * |
|
| 323 | + * @see toString() |
|
| 324 | + */ |
|
| 325 | + public function __toString() |
|
| 326 | + { |
|
| 327 | + return $this->toString(); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** Save a Header to the internal collection */ |
|
| 331 | + private function storeHeader($name, Swift_Mime_Header $header, $offset = null) |
|
| 332 | + { |
|
| 333 | + if (!isset($this->headers[strtolower($name ?? '')])) { |
|
| 334 | + $this->headers[strtolower($name ?? '')] = []; |
|
| 335 | + } |
|
| 336 | + if (!isset($offset)) { |
|
| 337 | + $this->headers[strtolower($name ?? '')][] = $header; |
|
| 338 | + } else { |
|
| 339 | + $this->headers[strtolower($name ?? '')][$offset] = $header; |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** Test if the headers can be sorted */ |
|
| 344 | + private function canSort() |
|
| 345 | + { |
|
| 346 | + return \count($this->order) > 0; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** uksort() algorithm for Header ordering */ |
|
| 350 | + private function sortHeaders($a, $b) |
|
| 351 | + { |
|
| 352 | + $lowerA = strtolower($a ?? ''); |
|
| 353 | + $lowerB = strtolower($b ?? ''); |
|
| 354 | + $aPos = \array_key_exists($lowerA, $this->order) ? $this->order[$lowerA] : -1; |
|
| 355 | + $bPos = \array_key_exists($lowerB, $this->order) ? $this->order[$lowerB] : -1; |
|
| 356 | + |
|
| 357 | + if (-1 === $aPos && -1 === $bPos) { |
|
| 358 | + // just be sure to be determinist here |
|
| 359 | + return $a > $b ? -1 : 1; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if (-1 == $aPos) { |
|
| 363 | + return 1; |
|
| 364 | + } elseif (-1 == $bPos) { |
|
| 365 | + return -1; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + return $aPos < $bPos ? -1 : 1; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** Test if the given Header is always displayed */ |
|
| 372 | + private function isDisplayed(Swift_Mime_Header $header) |
|
| 373 | + { |
|
| 374 | + return \array_key_exists(strtolower($header->getFieldName() ?? ''), $this->required); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** Notify all Headers of the new charset */ |
|
| 378 | + private function notifyHeadersOfCharset($charset) |
|
| 379 | + { |
|
| 380 | + foreach ($this->headers as $headerGroup) { |
|
| 381 | + foreach ($headerGroup as $header) { |
|
| 382 | + $header->setCharset($charset); |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Make a deep copy of object. |
|
| 389 | + */ |
|
| 390 | + public function __clone() |
|
| 391 | + { |
|
| 392 | + $this->factory = clone $this->factory; |
|
| 393 | + foreach ($this->headers as $groupKey => $headerGroup) { |
|
| 394 | + foreach ($headerGroup as $key => $header) { |
|
| 395 | + $this->headers[$groupKey][$key] = clone $header; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | 399 | } |
@@ -15,86 +15,86 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Preferences |
| 17 | 17 | { |
| 18 | - /** Singleton instance */ |
|
| 19 | - private static $instance = null; |
|
| 18 | + /** Singleton instance */ |
|
| 19 | + private static $instance = null; |
|
| 20 | 20 | |
| 21 | - /** Constructor not to be used */ |
|
| 22 | - private function __construct() |
|
| 23 | - { |
|
| 24 | - } |
|
| 21 | + /** Constructor not to be used */ |
|
| 22 | + private function __construct() |
|
| 23 | + { |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Gets the instance of Preferences. |
|
| 28 | - * |
|
| 29 | - * @return self |
|
| 30 | - */ |
|
| 31 | - public static function getInstance() |
|
| 32 | - { |
|
| 33 | - if (!isset(self::$instance)) { |
|
| 34 | - self::$instance = new self(); |
|
| 35 | - } |
|
| 26 | + /** |
|
| 27 | + * Gets the instance of Preferences. |
|
| 28 | + * |
|
| 29 | + * @return self |
|
| 30 | + */ |
|
| 31 | + public static function getInstance() |
|
| 32 | + { |
|
| 33 | + if (!isset(self::$instance)) { |
|
| 34 | + self::$instance = new self(); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - return self::$instance; |
|
| 38 | - } |
|
| 37 | + return self::$instance; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Set the default charset used. |
|
| 42 | - * |
|
| 43 | - * @param string $charset |
|
| 44 | - * |
|
| 45 | - * @return $this |
|
| 46 | - */ |
|
| 47 | - public function setCharset($charset) |
|
| 48 | - { |
|
| 49 | - Swift_DependencyContainer::getInstance()->register('properties.charset')->asValue($charset); |
|
| 40 | + /** |
|
| 41 | + * Set the default charset used. |
|
| 42 | + * |
|
| 43 | + * @param string $charset |
|
| 44 | + * |
|
| 45 | + * @return $this |
|
| 46 | + */ |
|
| 47 | + public function setCharset($charset) |
|
| 48 | + { |
|
| 49 | + Swift_DependencyContainer::getInstance()->register('properties.charset')->asValue($charset); |
|
| 50 | 50 | |
| 51 | - return $this; |
|
| 52 | - } |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Set the directory where temporary files can be saved. |
|
| 56 | - * |
|
| 57 | - * @param string $dir |
|
| 58 | - * |
|
| 59 | - * @return $this |
|
| 60 | - */ |
|
| 61 | - public function setTempDir($dir) |
|
| 62 | - { |
|
| 63 | - Swift_DependencyContainer::getInstance()->register('tempdir')->asValue($dir); |
|
| 54 | + /** |
|
| 55 | + * Set the directory where temporary files can be saved. |
|
| 56 | + * |
|
| 57 | + * @param string $dir |
|
| 58 | + * |
|
| 59 | + * @return $this |
|
| 60 | + */ |
|
| 61 | + public function setTempDir($dir) |
|
| 62 | + { |
|
| 63 | + Swift_DependencyContainer::getInstance()->register('tempdir')->asValue($dir); |
|
| 64 | 64 | |
| 65 | - return $this; |
|
| 66 | - } |
|
| 65 | + return $this; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Set the type of cache to use (i.e. "disk" or "array"). |
|
| 70 | - * |
|
| 71 | - * @param string $type |
|
| 72 | - * |
|
| 73 | - * @return $this |
|
| 74 | - */ |
|
| 75 | - public function setCacheType($type) |
|
| 76 | - { |
|
| 77 | - Swift_DependencyContainer::getInstance()->register('cache')->asAliasOf(sprintf('cache.%s', $type)); |
|
| 68 | + /** |
|
| 69 | + * Set the type of cache to use (i.e. "disk" or "array"). |
|
| 70 | + * |
|
| 71 | + * @param string $type |
|
| 72 | + * |
|
| 73 | + * @return $this |
|
| 74 | + */ |
|
| 75 | + public function setCacheType($type) |
|
| 76 | + { |
|
| 77 | + Swift_DependencyContainer::getInstance()->register('cache')->asAliasOf(sprintf('cache.%s', $type)); |
|
| 78 | 78 | |
| 79 | - return $this; |
|
| 80 | - } |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Set the QuotedPrintable dot escaper preference. |
|
| 84 | - * |
|
| 85 | - * @param bool $dotEscape |
|
| 86 | - * |
|
| 87 | - * @return $this |
|
| 88 | - */ |
|
| 89 | - public function setQPDotEscape($dotEscape) |
|
| 90 | - { |
|
| 91 | - $dotEscape = !empty($dotEscape); |
|
| 92 | - Swift_DependencyContainer::getInstance() |
|
| 93 | - ->register('mime.qpcontentencoder') |
|
| 94 | - ->asNewInstanceOf('Swift_Mime_ContentEncoder_QpContentEncoder') |
|
| 95 | - ->withDependencies(['mime.charstream', 'mime.bytecanonicalizer']) |
|
| 96 | - ->addConstructorValue($dotEscape); |
|
| 82 | + /** |
|
| 83 | + * Set the QuotedPrintable dot escaper preference. |
|
| 84 | + * |
|
| 85 | + * @param bool $dotEscape |
|
| 86 | + * |
|
| 87 | + * @return $this |
|
| 88 | + */ |
|
| 89 | + public function setQPDotEscape($dotEscape) |
|
| 90 | + { |
|
| 91 | + $dotEscape = !empty($dotEscape); |
|
| 92 | + Swift_DependencyContainer::getInstance() |
|
| 93 | + ->register('mime.qpcontentencoder') |
|
| 94 | + ->asNewInstanceOf('Swift_Mime_ContentEncoder_QpContentEncoder') |
|
| 95 | + ->withDependencies(['mime.charstream', 'mime.bytecanonicalizer']) |
|
| 96 | + ->addConstructorValue($dotEscape); |
|
| 97 | 97 | |
| 98 | - return $this; |
|
| 99 | - } |
|
| 98 | + return $this; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -15,19 +15,19 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_LoadBalancedTransport extends Swift_Transport_LoadBalancedTransport |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Creates a new LoadBalancedTransport with $transports. |
|
| 20 | - * |
|
| 21 | - * @param array $transports |
|
| 22 | - */ |
|
| 23 | - public function __construct($transports = []) |
|
| 24 | - { |
|
| 25 | - \call_user_func_array( |
|
| 26 | - [$this, 'Swift_Transport_LoadBalancedTransport::__construct'], |
|
| 27 | - Swift_DependencyContainer::getInstance() |
|
| 28 | - ->createDependenciesFor('transport.loadbalanced') |
|
| 29 | - ); |
|
| 18 | + /** |
|
| 19 | + * Creates a new LoadBalancedTransport with $transports. |
|
| 20 | + * |
|
| 21 | + * @param array $transports |
|
| 22 | + */ |
|
| 23 | + public function __construct($transports = []) |
|
| 24 | + { |
|
| 25 | + \call_user_func_array( |
|
| 26 | + [$this, 'Swift_Transport_LoadBalancedTransport::__construct'], |
|
| 27 | + Swift_DependencyContainer::getInstance() |
|
| 28 | + ->createDependenciesFor('transport.loadbalanced') |
|
| 29 | + ); |
|
| 30 | 30 | |
| 31 | - $this->setTransports($transports); |
|
| 32 | - } |
|
| 31 | + $this->setTransports($transports); |
|
| 32 | + } |
|
| 33 | 33 | } |