Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Swift_Signers_DKIMSigner often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Swift_Signers_DKIMSigner, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_Signers_DKIMSigner implements Swift_Signers_HeaderSigner |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * PrivateKey. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $_privateKey; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * DomainName. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $_domainName; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Selector. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_selector; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Hash algorithm used. |
||
| 41 | * |
||
| 42 | * @see RFC6376 3.3: Signers MUST implement and SHOULD sign using rsa-sha256. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $_hashAlgorithm = 'rsa-sha256'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Body canon method. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $_bodyCanon = 'simple'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Header canon method. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $_headerCanon = 'simple'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Headers not being signed. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $_ignoredHeaders = array('return-path' => true); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Signer identity. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $_signerIdentity; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * BodyLength. |
||
| 78 | * |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | protected $_bodyLen = 0; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Maximum signedLen. |
||
| 85 | * |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | protected $_maxLen = PHP_INT_MAX; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Embbed bodyLen in signature. |
||
| 92 | * |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $_showLen = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * When the signature has been applied (true means time()), false means not embedded. |
||
| 99 | * |
||
| 100 | * @var mixed |
||
| 101 | */ |
||
| 102 | protected $_signatureTimestamp = true; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * When will the signature expires false means not embedded, if sigTimestamp is auto |
||
| 106 | * Expiration is relative, otherwise it's absolute. |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | protected $_signatureExpiration = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Must we embed signed headers? |
||
| 114 | * |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $_debugHeaders = false; |
||
| 118 | |||
| 119 | // work variables |
||
| 120 | /** |
||
| 121 | * Headers used to generate hash. |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected $_signedHeaders = array(); |
||
| 126 | |||
| 127 | /** |
||
| 128 | * If debugHeaders is set store debugData here. |
||
| 129 | * |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | private $_debugHeadersData = ''; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Stores the bodyHash. |
||
| 136 | * |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | private $_bodyHash = ''; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Stores the signature header. |
||
| 143 | * |
||
| 144 | * @var Swift_Mime_Headers_ParameterizedHeader |
||
| 145 | */ |
||
| 146 | protected $_dkimHeader; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var resource |
||
| 150 | */ |
||
| 151 | private $_bodyHashHandler; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var null |
||
| 155 | * |
||
| 156 | * no used? |
||
| 157 | */ |
||
| 158 | private $_headerHash; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var string |
||
| 162 | */ |
||
| 163 | private $_headerCanonData = ''; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var int |
||
| 167 | */ |
||
| 168 | private $_bodyCanonEmptyCounter = 0; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var int |
||
| 172 | */ |
||
| 173 | private $_bodyCanonIgnoreStart = 2; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var bool |
||
| 177 | */ |
||
| 178 | private $_bodyCanonSpace = false; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var null|string |
||
| 182 | */ |
||
| 183 | private $_bodyCanonLastChar = null; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var string |
||
| 187 | */ |
||
| 188 | private $_bodyCanonLine = ''; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var array |
||
| 192 | */ |
||
| 193 | private $_bound = array(); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Constructor. |
||
| 197 | * |
||
| 198 | * @param string $privateKey |
||
| 199 | * @param string $domainName |
||
| 200 | * @param string $selector |
||
| 201 | */ |
||
| 202 | 1 | public function __construct($privateKey, $domainName, $selector) |
|
| 203 | { |
||
| 204 | 1 | $this->_privateKey = $privateKey; |
|
| 205 | 1 | $this->_domainName = $domainName; |
|
| 206 | 1 | $this->_signerIdentity = '@' . $domainName; |
|
| 207 | 1 | $this->_selector = $selector; |
|
| 208 | |||
| 209 | // keep fallback hash algorithm sha1 if php version is lower than 5.4.8 |
||
| 210 | 1 | if (PHP_VERSION_ID < 50408) { |
|
| 211 | 1 | $this->_hashAlgorithm = 'rsa-sha1'; |
|
| 212 | 1 | } |
|
| 213 | 1 | } |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Instanciate DKIMSigner. |
||
| 217 | * |
||
| 218 | * @param string $privateKey |
||
| 219 | * @param string $domainName |
||
| 220 | * @param string $selector |
||
| 221 | * |
||
| 222 | * @return self |
||
| 223 | */ |
||
| 224 | public static function newInstance($privateKey, $domainName, $selector) |
||
| 225 | { |
||
| 226 | return new static($privateKey, $domainName, $selector); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Reset the Signer. |
||
| 231 | * |
||
| 232 | * @see Swift_Signer::reset() |
||
| 233 | */ |
||
| 234 | public function reset() |
||
| 235 | { |
||
| 236 | $this->_headerHash = null; |
||
| 237 | $this->_signedHeaders = array(); |
||
| 238 | $this->_bodyHash = null; |
||
| 239 | $this->_bodyHashHandler = null; |
||
| 240 | $this->_bodyCanonIgnoreStart = 2; |
||
| 241 | $this->_bodyCanonEmptyCounter = 0; |
||
| 242 | $this->_bodyCanonLastChar = null; |
||
| 243 | $this->_bodyCanonSpace = false; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Writes $bytes to the end of the stream. |
||
| 248 | * |
||
| 249 | * Writing may not happen immediately if the stream chooses to buffer. If |
||
| 250 | * you want to write these bytes with immediate effect, call {@link commit()} |
||
| 251 | * after calling write(). |
||
| 252 | * |
||
| 253 | * This method returns the sequence ID of the write (i.e. 1 for first, 2 for |
||
| 254 | * second, etc etc). |
||
| 255 | * |
||
| 256 | * @param string $bytes |
||
| 257 | * |
||
| 258 | * @throws Swift_IoException |
||
| 259 | * |
||
| 260 | * @return int |
||
| 261 | */ |
||
| 262 | // TODO fix return |
||
| 263 | public function write($bytes) |
||
| 264 | { |
||
| 265 | $this->_canonicalizeBody($bytes); |
||
| 266 | foreach ($this->_bound as $is) { |
||
| 267 | $is->write($bytes); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * For any bytes that are currently buffered inside the stream, force them |
||
| 273 | * off the buffer. |
||
| 274 | */ |
||
| 275 | public function commit() |
||
| 276 | { |
||
| 277 | // Nothing to do |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Attach $is to this stream. |
||
| 283 | * The stream acts as an observer, receiving all data that is written. |
||
| 284 | * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
||
| 285 | * |
||
| 286 | * @param Swift_InputByteStream $is |
||
| 287 | */ |
||
| 288 | public function bind(Swift_InputByteStream $is) |
||
| 289 | { |
||
| 290 | // Don't have to mirror anything |
||
| 291 | $this->_bound[] = $is; |
||
| 292 | |||
| 293 | return; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Remove an already bound stream. |
||
| 298 | * If $is is not bound, no errors will be raised. |
||
| 299 | * If the stream currently has any buffered data it will be written to $is |
||
| 300 | * before unbinding occurs. |
||
| 301 | * |
||
| 302 | * @param Swift_InputByteStream $is |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function unbind(Swift_InputByteStream $is) |
|
| 305 | { |
||
| 306 | // Don't have to mirror anything |
||
| 307 | foreach ($this->_bound as $k => $stream) { |
||
| 308 | if ($stream === $is) { |
||
| 309 | unset($this->_bound[$k]); |
||
| 310 | |||
| 311 | return; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Flush the contents of the stream (empty it) and set the internal pointer |
||
| 318 | * to the beginning. |
||
| 319 | * |
||
| 320 | * @throws Swift_IoException |
||
| 321 | */ |
||
| 322 | public function flushBuffers() |
||
| 323 | { |
||
| 324 | $this->reset(); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Set hash_algorithm, must be one of rsa-sha256 | rsa-sha1. |
||
| 329 | * |
||
| 330 | * @param string $hash 'rsa-sha1' or 'rsa-sha256' |
||
| 331 | * |
||
| 332 | * @throws Swift_SwiftException |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function setHashAlgorithm($hash) |
||
| 337 | { |
||
| 338 | switch ($hash) { |
||
| 339 | case 'rsa-sha1': |
||
| 340 | $this->_hashAlgorithm = 'rsa-sha1'; |
||
| 341 | break; |
||
| 342 | case 'rsa-sha256': |
||
| 343 | $this->_hashAlgorithm = 'rsa-sha256'; |
||
| 344 | if (!defined('OPENSSL_ALGO_SHA256')) { |
||
| 345 | throw new Swift_SwiftException('Unable to set sha256 as it is not supported by OpenSSL.'); |
||
| 346 | } |
||
| 347 | break; |
||
| 348 | default: |
||
| 349 | throw new Swift_SwiftException('Unable to set the hash algorithm, must be one of rsa-sha1 or rsa-sha256 (%s given).', $hash); |
||
| 350 | } |
||
| 351 | |||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Set the body canonicalization algorithm. |
||
| 357 | * |
||
| 358 | * @param string $canon |
||
| 359 | * |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | public function setBodyCanon($canon) |
||
| 363 | { |
||
| 364 | if ($canon === 'relaxed') { |
||
| 365 | $this->_bodyCanon = 'relaxed'; |
||
| 366 | } else { |
||
| 367 | $this->_bodyCanon = 'simple'; |
||
| 368 | } |
||
| 369 | |||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the header canonicalization algorithm. |
||
| 375 | * |
||
| 376 | * @param string $canon |
||
| 377 | * |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | public function setHeaderCanon($canon) |
||
| 381 | { |
||
| 382 | if ($canon === 'relaxed') { |
||
| 383 | $this->_headerCanon = 'relaxed'; |
||
| 384 | } else { |
||
| 385 | $this->_headerCanon = 'simple'; |
||
| 386 | } |
||
| 387 | |||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the signer identity. |
||
| 393 | * |
||
| 394 | * @param string $identity |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function setSignerIdentity($identity) |
||
| 399 | { |
||
| 400 | $this->_signerIdentity = $identity; |
||
| 401 | |||
| 402 | return $this; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set the length of the body to sign. |
||
| 407 | * |
||
| 408 | * @param mixed $len (bool or int) |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | public function setBodySignedLen($len) |
||
| 413 | { |
||
| 414 | if ($len === true) { |
||
| 415 | $this->_showLen = true; |
||
| 416 | $this->_maxLen = PHP_INT_MAX; |
||
| 417 | } elseif ($len === false) { |
||
| 418 | $this->_showLen = false; |
||
| 419 | $this->_maxLen = PHP_INT_MAX; |
||
| 420 | } else { |
||
| 421 | $this->_showLen = true; |
||
| 422 | $this->_maxLen = (int) $len; |
||
| 423 | } |
||
| 424 | |||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Set the signature timestamp. |
||
| 430 | * |
||
| 431 | * @param int $time A timestamp |
||
| 432 | * |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | public function setSignatureTimestamp($time) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set the signature expiration timestamp. |
||
| 444 | * |
||
| 445 | * @param int $time A timestamp |
||
| 446 | * |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function setSignatureExpiration($time) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Enable / disable the DebugHeaders. |
||
| 458 | * |
||
| 459 | * @param bool $debug |
||
| 460 | * |
||
| 461 | * @return Swift_Signers_DKIMSigner |
||
| 462 | */ |
||
| 463 | public function setDebugHeaders($debug) |
||
| 464 | { |
||
| 465 | $this->_debugHeaders = (bool) $debug; |
||
| 466 | |||
| 467 | return $this; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Start Body. |
||
| 472 | */ |
||
| 473 | public function startBody() |
||
| 474 | { |
||
| 475 | // Init |
||
| 476 | switch ($this->_hashAlgorithm) { |
||
| 477 | case 'rsa-sha256': |
||
| 478 | $this->_bodyHashHandler = hash_init('sha256'); |
||
| 479 | break; |
||
| 480 | case 'rsa-sha1': |
||
| 481 | $this->_bodyHashHandler = hash_init('sha1'); |
||
| 482 | break; |
||
| 483 | } |
||
| 484 | $this->_bodyCanonLine = ''; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * End Body. |
||
| 489 | */ |
||
| 490 | public function endBody() |
||
| 491 | { |
||
| 492 | $this->_endOfBody(); |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns the list of Headers Tampered by this plugin. |
||
| 497 | * |
||
| 498 | * @return string[] |
||
| 499 | */ |
||
| 500 | public function getAlteredHeaders() |
||
| 501 | { |
||
| 502 | if ($this->_debugHeaders) { |
||
| 503 | return array('DKIM-Signature', 'X-DebugHash'); |
||
| 504 | } else { |
||
| 505 | return array('DKIM-Signature'); |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Adds an ignored Header. |
||
| 511 | * |
||
| 512 | * @param string $header_name |
||
| 513 | * |
||
| 514 | * @return Swift_Signers_DKIMSigner |
||
| 515 | */ |
||
| 516 | public function ignoreHeader($header_name) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set the headers to sign. |
||
| 525 | * |
||
| 526 | * @param Swift_Mime_HeaderSet $headers |
||
| 527 | * |
||
| 528 | * @return Swift_Signers_DKIMSigner |
||
| 529 | */ |
||
| 530 | View Code Duplication | public function setHeaders(Swift_Mime_HeaderSet $headers) |
|
| 531 | { |
||
| 532 | $this->_headerCanonData = ''; |
||
| 533 | // Loop through Headers |
||
| 534 | $listHeaders = $headers->listAll(); |
||
| 535 | foreach ($listHeaders as $hName) { |
||
| 536 | // Check if we need to ignore Header |
||
| 537 | if (!isset($this->_ignoredHeaders[Swift::strtolowerWithStaticCache($hName)])) { |
||
| 538 | if ($headers->has($hName)) { |
||
| 539 | $tmp = $headers->getAll($hName); |
||
| 540 | foreach ($tmp as $header) { |
||
| 541 | if ($header->getFieldBody() != '') { |
||
| 542 | $this->_addHeader($header->toString()); |
||
| 543 | $this->_signedHeaders[] = $header->getFieldName(); |
||
| 544 | } |
||
| 545 | } |
||
| 546 | } |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | return $this; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Add the signature to the given Headers. |
||
| 555 | * |
||
| 556 | * @param Swift_Mime_HeaderSet $headers |
||
| 557 | * |
||
| 558 | * @return Swift_Signers_DKIMSigner |
||
| 559 | */ |
||
| 560 | public function addSignature(Swift_Mime_HeaderSet $headers) |
||
| 561 | { |
||
| 562 | // Prepare the DKIM-Signature |
||
| 563 | $params = array('v' => '1', 'a' => $this->_hashAlgorithm, 'bh' => base64_encode($this->_bodyHash), 'd' => $this->_domainName, 'h' => implode(': ', $this->_signedHeaders), 'i' => $this->_signerIdentity, 's' => $this->_selector); |
||
| 564 | if ($this->_bodyCanon != 'simple') { |
||
| 565 | $params['c'] = $this->_headerCanon . '/' . $this->_bodyCanon; |
||
| 566 | } elseif ($this->_headerCanon != 'simple') { |
||
| 567 | $params['c'] = $this->_headerCanon; |
||
| 568 | } |
||
| 569 | if ($this->_showLen) { |
||
| 570 | $params['l'] = $this->_bodyLen; |
||
| 571 | } |
||
| 572 | if ($this->_signatureTimestamp === true) { |
||
| 573 | $params['t'] = time(); |
||
| 574 | if ($this->_signatureExpiration !== false) { |
||
| 575 | $params['x'] = $params['t'] + $this->_signatureExpiration; |
||
| 576 | } |
||
| 577 | } else { |
||
| 578 | if ($this->_signatureTimestamp !== false) { |
||
| 579 | $params['t'] = $this->_signatureTimestamp; |
||
| 580 | } |
||
| 581 | if ($this->_signatureExpiration !== false) { |
||
| 582 | $params['x'] = $this->_signatureExpiration; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | if ($this->_debugHeaders) { |
||
| 586 | $params['z'] = implode('|', $this->_debugHeadersData); |
||
| 587 | } |
||
| 588 | $string = ''; |
||
| 589 | foreach ($params as $k => $v) { |
||
| 590 | $string .= $k . '=' . $v . '; '; |
||
| 591 | } |
||
| 592 | $string = trim($string); |
||
| 593 | $headers->addTextHeader('DKIM-Signature', $string); |
||
| 594 | // Add the last DKIM-Signature |
||
| 595 | $tmp = $headers->getAll('DKIM-Signature'); |
||
| 596 | $this->_dkimHeader = end($tmp); |
||
| 597 | $this->_addHeader(trim($this->_dkimHeader->toString()) . "\r\n b=", true); |
||
| 598 | $this->_endOfHeaders(); |
||
| 599 | if ($this->_debugHeaders) { |
||
| 600 | $headers->addTextHeader('X-DebugHash', base64_encode($this->_headerHash)); |
||
| 601 | } |
||
| 602 | $this->_dkimHeader->setValue($string . ' b=' . trim(chunk_split(base64_encode($this->_getEncryptedHash()), 73, ' '))); |
||
| 603 | |||
| 604 | return $this; |
||
| 605 | } |
||
| 606 | |||
| 607 | /* Private helpers */ |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @param string $header |
||
| 611 | * @param bool $is_sig |
||
| 612 | */ |
||
| 613 | View Code Duplication | protected function _addHeader($header, $is_sig = false) |
|
| 614 | { |
||
| 615 | switch ($this->_headerCanon) { |
||
| 616 | case 'relaxed': |
||
| 617 | // Prepare Header and cascade |
||
| 618 | $exploded = explode(':', $header, 2); |
||
| 619 | $name = Swift::strtolowerWithStaticCache(trim($exploded[0])); |
||
| 620 | $value = str_replace("\r\n", '', $exploded[1]); |
||
| 621 | $value = preg_replace("/[ \t][ \t]+/", ' ', $value); |
||
| 622 | $header = $name . ':' . trim($value) . ($is_sig ? '' : "\r\n"); |
||
| 623 | case 'simple': |
||
| 624 | // Nothing to do |
||
| 625 | } |
||
| 626 | $this->_addToHeaderHash($header); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @deprecated This method is currently useless in this class but it must be |
||
| 631 | * kept for BC reasons due to its "protected" scope. This method |
||
| 632 | * might be overridden by custom client code. |
||
| 633 | */ |
||
| 634 | protected function _endOfHeaders() |
||
| 635 | { |
||
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @param string $string |
||
| 640 | */ |
||
| 641 | protected function _canonicalizeBody($string) |
||
| 642 | { |
||
| 643 | $len = strlen($string); |
||
| 644 | $canon = ''; |
||
| 645 | $method = ($this->_bodyCanon === 'relaxed'); |
||
| 646 | for ($i = 0; $i < $len; ++$i) { |
||
| 647 | if ($this->_bodyCanonIgnoreStart > 0) { |
||
| 648 | --$this->_bodyCanonIgnoreStart; |
||
| 649 | continue; |
||
| 650 | } |
||
| 651 | switch ($string[$i]) { |
||
| 652 | case "\r": |
||
| 653 | $this->_bodyCanonLastChar = "\r"; |
||
| 654 | break; |
||
| 655 | View Code Duplication | case "\n": |
|
| 656 | if ($this->_bodyCanonLastChar === "\r") { |
||
| 657 | if ($method) { |
||
| 658 | $this->_bodyCanonSpace = false; |
||
| 659 | } |
||
| 660 | |||
| 661 | if ($this->_bodyCanonLine === '') { |
||
| 662 | ++$this->_bodyCanonEmptyCounter; |
||
| 663 | } else { |
||
| 664 | $this->_bodyCanonLine = ''; |
||
| 665 | $canon .= "\r\n"; |
||
| 666 | } |
||
| 667 | |||
| 668 | } else { |
||
| 669 | // Wooops Error |
||
| 670 | // todo handle it but should never happen |
||
| 671 | } |
||
| 672 | break; |
||
| 673 | case ' ': |
||
| 674 | case "\t": |
||
| 675 | if ($method) { |
||
| 676 | $this->_bodyCanonSpace = true; |
||
| 677 | break; |
||
| 678 | } |
||
| 679 | default: |
||
| 680 | View Code Duplication | if ($this->_bodyCanonEmptyCounter > 0) { |
|
| 681 | $canon .= str_repeat("\r\n", $this->_bodyCanonEmptyCounter); |
||
| 682 | $this->_bodyCanonEmptyCounter = 0; |
||
| 683 | } |
||
| 684 | if ($this->_bodyCanonSpace) { |
||
| 685 | $this->_bodyCanonLine .= ' '; |
||
| 686 | $canon .= ' '; |
||
| 687 | $this->_bodyCanonSpace = false; |
||
| 688 | } |
||
| 689 | $this->_bodyCanonLine .= $string[$i]; |
||
| 690 | $canon .= $string[$i]; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | $this->_addToBodyHash($canon); |
||
| 694 | } |
||
| 695 | |||
| 696 | protected function _endOfBody() |
||
| 697 | { |
||
| 698 | // Add trailing Line return if last line is non empty |
||
| 699 | if ($this->_bodyCanonLine !== '') { |
||
| 700 | $this->_addToBodyHash("\r\n"); |
||
| 701 | } |
||
| 702 | |||
| 703 | $this->_bodyHash = hash_final($this->_bodyHashHandler, true); |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param string $string |
||
| 708 | */ |
||
| 709 | private function _addToBodyHash($string) |
||
| 710 | { |
||
| 711 | $len = strlen($string); |
||
| 712 | if ($len > ($new_len = ($this->_maxLen - $this->_bodyLen))) { |
||
| 713 | $string = substr($string, 0, $new_len); |
||
| 714 | $len = $new_len; |
||
| 715 | } |
||
| 716 | hash_update($this->_bodyHashHandler, $string); |
||
| 717 | $this->_bodyLen += $len; |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @param string $header |
||
| 722 | */ |
||
| 723 | private function _addToHeaderHash($header) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @throws Swift_SwiftException |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | private function _getEncryptedHash() |
||
| 737 | { |
||
| 738 | $signature = ''; |
||
| 739 | |||
| 740 | switch ($this->_hashAlgorithm) { |
||
| 741 | case 'rsa-sha1': |
||
| 742 | $algorithm = OPENSSL_ALGO_SHA1; |
||
| 743 | break; |
||
| 744 | case 'rsa-sha256': |
||
| 745 | $algorithm = OPENSSL_ALGO_SHA256; |
||
| 746 | break; |
||
| 747 | } |
||
| 748 | $pkeyId = openssl_get_privatekey($this->_privateKey); |
||
| 749 | if (!$pkeyId) { |
||
| 750 | throw new Swift_SwiftException('Unable to load DKIM Private Key [' . openssl_error_string() . ']'); |
||
| 751 | } |
||
| 752 | if (openssl_sign($this->_headerCanonData, $signature, $pkeyId, $algorithm)) { |
||
| 753 | return $signature; |
||
| 754 | } |
||
| 757 | } |
||
| 758 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.