Skip to content

Commit dc5376e

Browse files
Fix #91: Clone signers when cloning Message, emit warning when opendkim is used
1 parent e7209bc commit dc5376e

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Yii Framework 2 swiftmailer extension Change Log
44
2.1.3 under development
55
-----------------------
66

7-
- no changes in this release.
7+
- Enh #91: Clone signers when cloning `Message`, emit warning when `opendkim` is used (WinterSilence)
88

99

1010
2.1.2 September 24, 2018

src/Message.php

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
*
2121
* @method Mailer getMailer() returns mailer instance.
2222
*
23-
* @property array $headers Headers in format: `[name => value]`. This property is write-only.
23+
* @property-write array $headers Headers in format: `[name => value]`.
2424
* @property int $priority Priority value as integer in range: `1..5`, where 1 is the highest priority and 5
2525
* is the lowest.
2626
* @property string $readReceiptTo Receipt receive email addresses. Note that the type of this property
2727
* differs in getter and setter. See [[getReadReceiptTo()]] and [[setReadReceiptTo()]] for details.
2828
* @property string $returnPath The bounce email address.
29-
* @property array|callable|\Swift_Signer $signature Signature specification. See [[addSignature()]] for
30-
* details on how it should be specified. This property is write-only.
31-
* @property \Swift_Message $swiftMessage Swift message instance. This property is read-only.
29+
* @property-write array|callable|\Swift_Signer $signature Signature specification. See [[addSignature()]]
30+
* for details on how it should be specified.
31+
* @property-read \Swift_Message $swiftMessage Swift message instance.
3232
*
3333
* @author Paul Klimov <klimov.paul@gmail.com>
3434
* @since 2.0
@@ -47,22 +47,25 @@ class Message extends BaseMessage
4747

4848
/**
4949
* This method is called after the object is created by cloning an existing one.
50-
* It ensures [[swiftMessage]] is also cloned.
50+
* It ensures [[swiftMessage]] and [[signers]] is also cloned.
5151
* @since 2.0.7
5252
*/
5353
public function __clone()
5454
{
55-
if (is_object($this->_swiftMessage)) {
55+
if ($this->_swiftMessage !== null) {
5656
$this->_swiftMessage = clone $this->_swiftMessage;
5757
}
58+
foreach ($this->signers as $key => $signer) {
59+
$this->signers[$key] = clone $signer;
60+
}
5861
}
5962

6063
/**
6164
* @return \Swift_Message Swift message instance.
6265
*/
6366
public function getSwiftMessage()
6467
{
65-
if (!is_object($this->_swiftMessage)) {
68+
if ($this->_swiftMessage === null) {
6669
$this->_swiftMessage = $this->createSwiftMessage();
6770
}
6871

@@ -381,8 +384,9 @@ public function addSignature($signature)
381384
}
382385

383386
/**
384-
* Creates signer from its configuration
385-
* @param array $signature signature configuration
387+
* Creates signer from it's configuration.
388+
* @param array $signature signature configuration:
389+
* `[type: string, key: string|null, file: string|null, domain: string|null, selector: string|null]`
386390
* @return \Swift_Signer signer instance
387391
* @throws InvalidConfigException on invalid configuration provided
388392
* @since 2.0.6
@@ -392,32 +396,27 @@ protected function createSwiftSigner($signature)
392396
if (!isset($signature['type'])) {
393397
throw new InvalidConfigException('Signature configuration should contain "type" key');
394398
}
395-
switch (strtolower($signature['type'])) {
396-
case 'dkim' :
397-
$domain = ArrayHelper::getValue($signature, 'domain', null);
398-
$selector = ArrayHelper::getValue($signature, 'selector', null);
399-
if (isset($signature['key'])) {
400-
$privateKey = $signature['key'];
401-
} elseif (isset($signature['file'])) {
402-
$privateKey = file_get_contents(Yii::getAlias($signature['file']));
403-
} else {
404-
throw new InvalidConfigException("Either 'key' or 'file' signature option should be specified");
405-
}
406-
return new \Swift_Signers_DKIMSigner($privateKey, $domain, $selector);
407-
case 'opendkim' :
408-
$domain = ArrayHelper::getValue($signature, 'domain', null);
409-
$selector = ArrayHelper::getValue($signature, 'selector', null);
410-
if (isset($signature['key'])) {
411-
$privateKey = $signature['key'];
412-
} elseif (isset($signature['file'])) {
413-
$privateKey = file_get_contents(Yii::getAlias($signature['file']));
414-
} else {
415-
throw new InvalidConfigException("Either 'key' or 'file' signature option should be specified");
416-
}
417-
return new \Swift_Signers_OpenDKIMSigner($privateKey, $domain, $selector);
418-
default:
419-
throw new InvalidConfigException("Unrecognized signature type '{$signature['type']}'");
399+
$signature['type'] = strtolower($signature['type']);
400+
if (!in_array($signature['type'], ['dkim', 'opendkim'], true)) {
401+
throw new InvalidConfigException("Unrecognized signature type '{$signature['type']}'");
420402
}
403+
404+
if (isset($signature['key'])) {
405+
$privateKey = $signature['key'];
406+
} elseif (isset($signature['file'])) {
407+
$privateKey = file_get_contents(Yii::getAlias($signature['file']));
408+
} else {
409+
throw new InvalidConfigException("Either 'key' or 'file' signature option should be specified");
410+
}
411+
$domain = ArrayHelper::getValue($signature, 'domain');
412+
$selector = ArrayHelper::getValue($signature, 'selector');
413+
414+
if ($signature['type'] === 'opendkim') {
415+
Yii::warning(__METHOD__ . '(): signature type "opendkim" is deprecated, use "dkim" instead.');
416+
return new \Swift_Signers_OpenDKIMSigner($privateKey, $domain, $selector);
417+
}
418+
419+
return new \Swift_Signers_DKIMSigner($privateKey, $domain, $selector);
421420
}
422421

423422
/**

0 commit comments

Comments
 (0)