[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Signers/ -> OpenDKIMSigner.php (source)

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


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1