/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Signers/OpenDKIMSigner.php

https://gitlab.com/madwanz64/laravel · PHP · 183 lines · 117 code · 27 blank · 39 comment · 23 complexity · 36e93e169ffbd81dcd0c7d24226aaef1 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * DKIM Signer used to apply DKIM Signature to a message
  11. * Takes advantage of pecl extension.
  12. *
  13. * @author Xavier De Cock <xdecock@gmail.com>
  14. *
  15. * @deprecated since SwiftMailer 6.1.0; use Swift_Signers_DKIMSigner instead.
  16. */
  17. class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
  18. {
  19. private $peclLoaded = false;
  20. private $dkimHandler = null;
  21. private $dropFirstLF = true;
  22. const CANON_RELAXED = 1;
  23. const CANON_SIMPLE = 2;
  24. const SIG_RSA_SHA1 = 3;
  25. const SIG_RSA_SHA256 = 4;
  26. public function __construct($privateKey, $domainName, $selector)
  27. {
  28. if (!\extension_loaded('opendkim')) {
  29. throw new Swift_SwiftException('php-opendkim extension not found');
  30. }
  31. $this->peclLoaded = true;
  32. parent::__construct($privateKey, $domainName, $selector);
  33. }
  34. public function addSignature(Swift_Mime_SimpleHeaderSet $headers)
  35. {
  36. $header = new Swift_Mime_Headers_OpenDKIMHeader('DKIM-Signature');
  37. $headerVal = $this->dkimHandler->getSignatureHeader();
  38. if (false === $headerVal || \is_int($headerVal)) {
  39. throw new Swift_SwiftException('OpenDKIM Error: '.$this->dkimHandler->getError());
  40. }
  41. $header->setValue($headerVal);
  42. $headers->set($header);
  43. return $this;
  44. }
  45. public function setHeaders(Swift_Mime_SimpleHeaderSet $headers)
  46. {
  47. $hash = 'rsa-sha1' == $this->hashAlgorithm ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256;
  48. $bodyCanon = 'simple' == $this->bodyCanon ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
  49. $headerCanon = 'simple' == $this->headerCanon ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
  50. $this->dkimHandler = new OpenDKIMSign($this->privateKey, $this->selector, $this->domainName, $headerCanon, $bodyCanon, $hash, -1);
  51. // Hardcode signature Margin for now
  52. $this->dkimHandler->setMargin(78);
  53. if (!is_numeric($this->signatureTimestamp)) {
  54. OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time());
  55. } else {
  56. if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->signatureTimestamp)) {
  57. throw new Swift_SwiftException('Unable to force signature timestamp ['.openssl_error_string().']');
  58. }
  59. }
  60. if (isset($this->signerIdentity)) {
  61. $this->dkimHandler->setSigner($this->signerIdentity);
  62. }
  63. $listHeaders = $headers->listAll();
  64. foreach ($listHeaders as $hName) {
  65. // Check if we need to ignore Header
  66. if (!isset($this->ignoredHeaders[strtolower($hName ?? '')])) {
  67. $tmp = $headers->getAll($hName);
  68. if ($headers->has($hName)) {
  69. foreach ($tmp as $header) {
  70. if ('' != $header->getFieldBody()) {
  71. $htosign = $header->toString();
  72. $this->dkimHandler->header($htosign);
  73. $this->signedHeaders[] = $header->getFieldName();
  74. }
  75. }
  76. }
  77. }
  78. }
  79. return $this;
  80. }
  81. public function startBody()
  82. {
  83. if (!$this->peclLoaded) {
  84. return parent::startBody();
  85. }
  86. $this->dropFirstLF = true;
  87. $this->dkimHandler->eoh();
  88. return $this;
  89. }
  90. public function endBody()
  91. {
  92. if (!$this->peclLoaded) {
  93. return parent::endBody();
  94. }
  95. $this->dkimHandler->eom();
  96. return $this;
  97. }
  98. public function reset()
  99. {
  100. $this->dkimHandler = null;
  101. parent::reset();
  102. return $this;
  103. }
  104. /**
  105. * Set the signature timestamp.
  106. *
  107. * @param int $time
  108. *
  109. * @return $this
  110. */
  111. public function setSignatureTimestamp($time)
  112. {
  113. $this->signatureTimestamp = $time;
  114. return $this;
  115. }
  116. /**
  117. * Set the signature expiration timestamp.
  118. *
  119. * @param int $time
  120. *
  121. * @return $this
  122. */
  123. public function setSignatureExpiration($time)
  124. {
  125. $this->signatureExpiration = $time;
  126. return $this;
  127. }
  128. /**
  129. * Enable / disable the DebugHeaders.
  130. *
  131. * @param bool $debug
  132. *
  133. * @return $this
  134. */
  135. public function setDebugHeaders($debug)
  136. {
  137. $this->debugHeaders = (bool) $debug;
  138. return $this;
  139. }
  140. // Protected
  141. protected function canonicalizeBody($string)
  142. {
  143. if (!$this->peclLoaded) {
  144. return parent::canonicalizeBody($string);
  145. }
  146. if (true === $this->dropFirstLF) {
  147. if ("\r" == $string[0] && "\n" == $string[1]) {
  148. $string = substr($string, 2);
  149. }
  150. }
  151. $this->dropFirstLF = false;
  152. if (\strlen($string)) {
  153. $this->dkimHandler->body($string);
  154. }
  155. }
  156. }