/Foundry 1.8.1/mail/lib/classes/Swift/Signers/OpenDKIMSigner.php

https://gitlab.com/shivkumarsah/shivkumarsah.com · PHP · 186 lines · 126 code · 26 blank · 34 comment · 23 complexity · c8fbbdd7bc4e6cfdc5fbf767f59876b0 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. class Swift_Signers_OpenDKIMSigner extends Swift_Signers_DKIMSigner
  16. {
  17. private $_peclLoaded = false;
  18. private $_dkimHandler = null;
  19. private $dropFirstLF = true;
  20. const CANON_RELAXED = 1;
  21. const CANON_SIMPLE = 2;
  22. const SIG_RSA_SHA1 = 3;
  23. const SIG_RSA_SHA256 = 4;
  24. public function __construct($privateKey, $domainName, $selector)
  25. {
  26. if (extension_loaded('opendkim')) {
  27. $this->_peclLoaded = true;
  28. } else {
  29. throw new Swift_SwiftException('php-opendkim extension not found');
  30. }
  31. parent::__construct($privateKey, $domainName, $selector);
  32. }
  33. public static function newInstance($privateKey, $domainName, $selector)
  34. {
  35. return new static($privateKey, $domainName, $selector);
  36. }
  37. public function addSignature(Swift_Mime_HeaderSet $headers)
  38. {
  39. $header = new Swift_Mime_Headers_OpenDKIMHeader('DKIM-Signature');
  40. $headerVal=$this->_dkimHandler->getSignatureHeader();
  41. if (!$headerVal) {
  42. throw new Swift_SwiftException('OpenDKIM Error: '.$this->_dkimHandler->getError());
  43. }
  44. $header->setValue($headerVal);
  45. $headers->set($header);
  46. return $this;
  47. }
  48. public function setHeaders(Swift_Mime_HeaderSet $headers)
  49. {
  50. $bodyLen = $this->_bodyLen;
  51. if (is_bool($bodyLen)) {
  52. $bodyLen = - 1;
  53. }
  54. $hash = ($this->_hashAlgorithm == 'rsa-sha1') ? OpenDKIMSign::ALG_RSASHA1 : OpenDKIMSign::ALG_RSASHA256;
  55. $bodyCanon = ($this->_bodyCanon == 'simple') ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
  56. $headerCanon = ($this->_headerCanon == 'simple') ? OpenDKIMSign::CANON_SIMPLE : OpenDKIMSign::CANON_RELAXED;
  57. $this->_dkimHandler = new OpenDKIMSign($this->_privateKey, $this->_selector, $this->_domainName, $headerCanon, $bodyCanon, $hash, $bodyLen);
  58. // Hardcode signature Margin for now
  59. $this->_dkimHandler->setMargin(78);
  60. if (!is_numeric($this->_signatureTimestamp)) {
  61. OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, time());
  62. } else {
  63. if (!OpenDKIM::setOption(OpenDKIM::OPTS_FIXEDTIME, $this->_signatureTimestamp)) {
  64. throw new Swift_SwiftException('Unable to force signature timestamp ['.openssl_error_string().']');
  65. }
  66. }
  67. if (isset($this->_signerIdentity)) {
  68. $this->_dkimHandler->setSigner($this->_signerIdentity);
  69. }
  70. $listHeaders = $headers->listAll();
  71. foreach ($listHeaders as $hName) {
  72. // Check if we need to ignore Header
  73. if (! isset($this->_ignoredHeaders[strtolower($hName)])) {
  74. $tmp = $headers->getAll($hName);
  75. if ($headers->has($hName)) {
  76. foreach ($tmp as $header) {
  77. if ($header->getFieldBody() != '') {
  78. $htosign = $header->toString();
  79. $this->_dkimHandler->header($htosign);
  80. $this->_signedHeaders[] = $header->getFieldName();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. return $this;
  87. }
  88. public function startBody()
  89. {
  90. if (! $this->_peclLoaded) {
  91. return parent::startBody();
  92. }
  93. $this->dropFirstLF = true;
  94. $this->_dkimHandler->eoh();
  95. return $this;
  96. }
  97. public function endBody()
  98. {
  99. if (! $this->_peclLoaded) {
  100. return parent::endBody();
  101. }
  102. $this->_dkimHandler->eom();
  103. return $this;
  104. }
  105. public function reset()
  106. {
  107. $this->_dkimHandler = null;
  108. parent::reset();
  109. return $this;
  110. }
  111. /**
  112. * Set the signature timestamp
  113. *
  114. * @param timestamp $time
  115. * @return Swift_Signers_DKIMSigner
  116. */
  117. public function setSignatureTimestamp($time)
  118. {
  119. $this->_signatureTimestamp = $time;
  120. return $this;
  121. }
  122. /**
  123. * Set the signature expiration timestamp
  124. *
  125. * @param timestamp $time
  126. * @return Swift_Signers_DKIMSigner
  127. */
  128. public function setSignatureExpiration($time)
  129. {
  130. $this->_signatureExpiration = $time;
  131. return $this;
  132. }
  133. /**
  134. * Enable / disable the DebugHeaders
  135. *
  136. * @param bool $debug
  137. * @return Swift_Signers_DKIMSigner
  138. */
  139. public function setDebugHeaders($debug)
  140. {
  141. $this->_debugHeaders = (bool) $debug;
  142. return $this;
  143. }
  144. // Protected
  145. protected function _canonicalizeBody($string)
  146. {
  147. if (! $this->_peclLoaded) {
  148. return parent::_canonicalizeBody($string);
  149. }
  150. if (false && $this->dropFirstLF === true) {
  151. if ($string[0]=="\r" && $string[1]=="\n") {
  152. $string=substr($string, 2);
  153. }
  154. }
  155. $this->dropFirstLF = false;
  156. if (strlen($string)) {
  157. $this->_dkimHandler->body($string);
  158. }
  159. }
  160. }