mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-12-24 10:21:32 +01:00
swiftmailer
This commit is contained in:
@@ -12,13 +12,13 @@
|
||||
* DKIM Signer used to apply DKIM Signature to a message
|
||||
* Takes advantage of pecl extension.
|
||||
*
|
||||
* @author Xavier De Cock <xdecock@gmail.com>
|
||||
* @author Xavier De Cock <xdecock@gmail.com>
|
||||
*/
|
||||
class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
{
|
||||
private $_peclLoaded = false;
|
||||
private $peclLoaded = false;
|
||||
|
||||
private $_dkimHandler = null;
|
||||
private $dkimHandler = null;
|
||||
|
||||
private $dropFirstLF = true;
|
||||
|
||||
@@ -33,22 +33,17 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
throw new Swift_SwiftException('php-opendkim extension not found');
|
||||
}
|
||||
|
||||
$this->_peclLoaded = true;
|
||||
$this->peclLoaded = true;
|
||||
|
||||
parent::__construct($privateKey, $domainName, $selector);
|
||||
}
|
||||
|
||||
public static function newInstance($privateKey, $domainName, $selector)
|
||||
{
|
||||
return new static($privateKey, $domainName, $selector);
|
||||
}
|
||||
|
||||
public function addSignature(Swift_Mime_HeaderSet $headers)
|
||||
public function addSignature(Swift_Mime_SimpleHeaderSet $headers)
|
||||
{
|
||||
$header = new Swift_Mime_Headers_OpenDKIMHeader('DKIM-Signature');
|
||||
$headerVal = $this->_dkimHandler->getSignatureHeader();
|
||||
$headerVal = $this->dkimHandler->getSignatureHeader();
|
||||
if (!$headerVal) {
|
||||
throw new Swift_SwiftException('OpenDKIM Error: '.$this->_dkimHandler->getError());
|
||||
throw new Swift_SwiftException('OpenDKIM Error: '.$this->dkimHandler->getError());
|
||||
}
|
||||
$header->setValue($headerVal);
|
||||
$headers->set($header);
|
||||
@@ -56,40 +51,40 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setHeaders(Swift_Mime_HeaderSet $headers)
|
||||
public function setHeaders(Swift_Mime_SimpleHeaderSet $headers)
|
||||
{
|
||||
$bodyLen = $this->_bodyLen;
|
||||
$bodyLen = $this->bodyLen;
|
||||
if (is_bool($bodyLen)) {
|
||||
$bodyLen = -1;
|
||||
}
|
||||
$hash = $this->_hashAlgorithm == 'rsa-sha1' ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256;
|
||||
$bodyCanon = $this->_bodyCanon == 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
|
||||
$headerCanon = $this->_headerCanon == 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
|
||||
$this->_dkimHandler = new OpenDKIMSign($this->_privateKey, $this->_selector, $this->_domainName, $headerCanon, $bodyCanon, $hash, $bodyLen);
|
||||
$hash = $this->hashAlgorithm == 'rsa-sha1' ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256;
|
||||
$bodyCanon = $this->bodyCanon == 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
|
||||
$headerCanon = $this->headerCanon == 'simple' ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
|
||||
$this->dkimHandler = new OpenDKIMSign($this->privateKey, $this->selector, $this->domainName, $headerCanon, $bodyCanon, $hash, $bodyLen);
|
||||
// Hardcode signature Margin for now
|
||||
$this->_dkimHandler->setMargin(78);
|
||||
$this->dkimHandler->setMargin(78);
|
||||
|
||||
if (!is_numeric($this->_signatureTimestamp)) {
|
||||
if (!is_numeric($this->signatureTimestamp)) {
|
||||
OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time());
|
||||
} else {
|
||||
if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->_signatureTimestamp)) {
|
||||
if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->signatureTimestamp)) {
|
||||
throw new Swift_SwiftException('Unable to force signature timestamp ['.openssl_error_string().']');
|
||||
}
|
||||
}
|
||||
if (isset($this->_signerIdentity)) {
|
||||
$this->_dkimHandler->setSigner($this->_signerIdentity);
|
||||
if (isset($this->signerIdentity)) {
|
||||
$this->dkimHandler->setSigner($this->signerIdentity);
|
||||
}
|
||||
$listHeaders = $headers->listAll();
|
||||
foreach ($listHeaders as $hName) {
|
||||
// Check if we need to ignore Header
|
||||
if (!isset($this->_ignoredHeaders[strtolower($hName)])) {
|
||||
if (!isset($this->ignoredHeaders[strtolower($hName)])) {
|
||||
$tmp = $headers->getAll($hName);
|
||||
if ($headers->has($hName)) {
|
||||
foreach ($tmp as $header) {
|
||||
if ($header->getFieldBody() != '') {
|
||||
$htosign = $header->toString();
|
||||
$this->_dkimHandler->header($htosign);
|
||||
$this->_signedHeaders[] = $header->getFieldName();
|
||||
$this->dkimHandler->header($htosign);
|
||||
$this->signedHeaders[] = $header->getFieldName();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,28 +96,28 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
|
||||
public function startBody()
|
||||
{
|
||||
if (!$this->_peclLoaded) {
|
||||
if (!$this->peclLoaded) {
|
||||
return parent::startBody();
|
||||
}
|
||||
$this->dropFirstLF = true;
|
||||
$this->_dkimHandler->eoh();
|
||||
$this->dkimHandler->eoh();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function endBody()
|
||||
{
|
||||
if (!$this->_peclLoaded) {
|
||||
if (!$this->peclLoaded) {
|
||||
return parent::endBody();
|
||||
}
|
||||
$this->_dkimHandler->eom();
|
||||
$this->dkimHandler->eom();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->_dkimHandler = null;
|
||||
$this->dkimHandler = null;
|
||||
parent::reset();
|
||||
|
||||
return $this;
|
||||
@@ -133,11 +128,11 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
*
|
||||
* @param int $time
|
||||
*
|
||||
* @return Swift_Signers_DKIMSigner
|
||||
* @return $this
|
||||
*/
|
||||
public function setSignatureTimestamp($time)
|
||||
{
|
||||
$this->_signatureTimestamp = $time;
|
||||
$this->signatureTimestamp = $time;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -147,11 +142,11 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
*
|
||||
* @param int $time
|
||||
*
|
||||
* @return Swift_Signers_DKIMSigner
|
||||
* @return $this
|
||||
*/
|
||||
public function setSignatureExpiration($time)
|
||||
{
|
||||
$this->_signatureExpiration = $time;
|
||||
$this->signatureExpiration = $time;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -161,21 +156,21 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
*
|
||||
* @param bool $debug
|
||||
*
|
||||
* @return Swift_Signers_DKIMSigner
|
||||
* @return $this
|
||||
*/
|
||||
public function setDebugHeaders($debug)
|
||||
{
|
||||
$this->_debugHeaders = (bool) $debug;
|
||||
$this->debugHeaders = (bool) $debug;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Protected
|
||||
|
||||
protected function _canonicalizeBody($string)
|
||||
protected function canonicalizeBody($string)
|
||||
{
|
||||
if (!$this->_peclLoaded) {
|
||||
return parent::_canonicalizeBody($string);
|
||||
if (!$this->peclLoaded) {
|
||||
return parent::canonicalizeBody($string);
|
||||
}
|
||||
if (false && $this->dropFirstLF === true) {
|
||||
if ($string[0] == "\r" && $string[1] == "\n") {
|
||||
@@ -184,7 +179,7 @@ class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
|
||||
}
|
||||
$this->dropFirstLF = false;
|
||||
if (strlen($string)) {
|
||||
$this->_dkimHandler->body($string);
|
||||
$this->dkimHandler->body($string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user