Open Journal Systems  3.3.0
Md5ValidatorPlugin.php
1 <?php
2 
3 namespace Guzzle\Plugin\Md5;
4 
8 
15 {
17  protected $contentLengthCutoff;
18 
20  protected $contentEncoded;
21 
31  public function __construct($contentEncoded = true, $contentLengthCutoff = false)
32  {
33  $this->contentLengthCutoff = $contentLengthCutoff;
34  $this->contentEncoded = $contentEncoded;
35  }
36 
37  public static function getSubscribedEvents()
38  {
39  return array('request.complete' => array('onRequestComplete', 255));
40  }
41 
46  public function onRequestComplete(Event $event)
47  {
48  $response = $event['response'];
49 
50  if (!$contentMd5 = $response->getContentMd5()) {
51  return;
52  }
53 
54  $contentEncoding = $response->getContentEncoding();
55  if ($contentEncoding && !$this->contentEncoded) {
56  return false;
57  }
58 
59  // Make sure that the size of the request is under the cutoff size
60  if ($this->contentLengthCutoff) {
61  $size = $response->getContentLength() ?: $response->getBody()->getSize();
62  if (!$size || $size > $this->contentLengthCutoff) {
63  return;
64  }
65  }
66 
67  if (!$contentEncoding) {
68  $hash = $response->getBody()->getContentMd5();
69  } elseif ($contentEncoding == 'gzip') {
70  $response->getBody()->compress('zlib.deflate');
71  $hash = $response->getBody()->getContentMd5();
72  $response->getBody()->uncompress();
73  } elseif ($contentEncoding == 'compress') {
74  $response->getBody()->compress('bzip2.compress');
75  $hash = $response->getBody()->getContentMd5();
76  $response->getBody()->uncompress();
77  } else {
78  return;
79  }
80 
81  if ($contentMd5 !== $hash) {
82  throw new UnexpectedValueException(
83  "The response entity body may have been modified over the wire. The Content-MD5 "
84  . "received ({$contentMd5}) did not match the calculated MD5 hash ({$hash})."
85  );
86  }
87  }
88 }
Guzzle\Plugin\Md5\Md5ValidatorPlugin\onRequestComplete
onRequestComplete(Event $event)
Definition: Md5ValidatorPlugin.php:52
Guzzle\Plugin\Md5
Definition: CommandContentMd5Plugin.php:3
Guzzle\Plugin\Md5\Md5ValidatorPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: Md5ValidatorPlugin.php:43
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Common\Exception\UnexpectedValueException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/UnexpectedValueException.php:5
Guzzle\Plugin\Md5\Md5ValidatorPlugin
Definition: Md5ValidatorPlugin.php:14
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Plugin\Md5\Md5ValidatorPlugin\__construct
__construct($contentEncoded=true, $contentLengthCutoff=false)
Definition: Md5ValidatorPlugin.php:37
Guzzle\Plugin\Md5\Md5ValidatorPlugin\$contentEncoded
$contentEncoded
Definition: Md5ValidatorPlugin.php:26
Guzzle\Plugin\Md5\Md5ValidatorPlugin\$contentLengthCutoff
$contentLengthCutoff
Definition: Md5ValidatorPlugin.php:20