Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-feed/src/PubSubHubbub/Subscriber.php @ 2:5311817fb629
Theme updates
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 13:19:18 +0000 |
parents | c75dbcec494b |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
1:0b0e5f3b1e83 | 2:5311817fb629 |
---|---|
145 { | 145 { |
146 if ($options instanceof Traversable) { | 146 if ($options instanceof Traversable) { |
147 $options = ArrayUtils::iteratorToArray($options); | 147 $options = ArrayUtils::iteratorToArray($options); |
148 } | 148 } |
149 | 149 |
150 if (!is_array($options)) { | 150 if (! is_array($options)) { |
151 throw new Exception\InvalidArgumentException('Array or Traversable object' | 151 throw new Exception\InvalidArgumentException('Array or Traversable object' |
152 . 'expected, got ' . gettype($options)); | 152 . 'expected, got ' . gettype($options)); |
153 } | 153 } |
154 if (array_key_exists('hubUrls', $options)) { | 154 if (array_key_exists('hubUrls', $options)) { |
155 $this->addHubUrls($options['hubUrls']); | 155 $this->addHubUrls($options['hubUrls']); |
191 * @return Subscriber | 191 * @return Subscriber |
192 * @throws Exception\InvalidArgumentException | 192 * @throws Exception\InvalidArgumentException |
193 */ | 193 */ |
194 public function setTopicUrl($url) | 194 public function setTopicUrl($url) |
195 { | 195 { |
196 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) { | 196 if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { |
197 throw new Exception\InvalidArgumentException('Invalid parameter "url"' | 197 throw new Exception\InvalidArgumentException('Invalid parameter "url"' |
198 .' of "' . $url . '" must be a non-empty string and a valid' | 198 .' of "' . $url . '" must be a non-empty string and a valid' |
199 .' URL'); | 199 .' URL'); |
200 } | 200 } |
201 $this->topicUrl = $url; | 201 $this->topicUrl = $url; |
254 * @return Subscriber | 254 * @return Subscriber |
255 * @throws Exception\InvalidArgumentException | 255 * @throws Exception\InvalidArgumentException |
256 */ | 256 */ |
257 public function setCallbackUrl($url) | 257 public function setCallbackUrl($url) |
258 { | 258 { |
259 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) { | 259 if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { |
260 throw new Exception\InvalidArgumentException('Invalid parameter "url"' | 260 throw new Exception\InvalidArgumentException('Invalid parameter "url"' |
261 . ' of "' . $url . '" must be a non-empty string and a valid' | 261 . ' of "' . $url . '" must be a non-empty string and a valid' |
262 . ' URL'); | 262 . ' URL'); |
263 } | 263 } |
264 $this->callbackUrl = $url; | 264 $this->callbackUrl = $url; |
324 * @return Subscriber | 324 * @return Subscriber |
325 * @throws Exception\InvalidArgumentException | 325 * @throws Exception\InvalidArgumentException |
326 */ | 326 */ |
327 public function addHubUrl($url) | 327 public function addHubUrl($url) |
328 { | 328 { |
329 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) { | 329 if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { |
330 throw new Exception\InvalidArgumentException('Invalid parameter "url"' | 330 throw new Exception\InvalidArgumentException('Invalid parameter "url"' |
331 . ' of "' . $url . '" must be a non-empty string and a valid' | 331 . ' of "' . $url . '" must be a non-empty string and a valid' |
332 . ' URL'); | 332 . ' URL'); |
333 } | 333 } |
334 $this->hubUrls[] = $url; | 334 $this->hubUrls[] = $url; |
355 * @param string $url | 355 * @param string $url |
356 * @return Subscriber | 356 * @return Subscriber |
357 */ | 357 */ |
358 public function removeHubUrl($url) | 358 public function removeHubUrl($url) |
359 { | 359 { |
360 if (!in_array($url, $this->getHubUrls())) { | 360 if (! in_array($url, $this->getHubUrls())) { |
361 return $this; | 361 return $this; |
362 } | 362 } |
363 $key = array_search($url, $this->hubUrls); | 363 $key = array_search($url, $this->hubUrls); |
364 unset($this->hubUrls[$key]); | 364 unset($this->hubUrls[$key]); |
365 return $this; | 365 return $this; |
384 * @return Subscriber | 384 * @return Subscriber |
385 * @throws Exception\InvalidArgumentException | 385 * @throws Exception\InvalidArgumentException |
386 */ | 386 */ |
387 public function addAuthentication($url, array $authentication) | 387 public function addAuthentication($url, array $authentication) |
388 { | 388 { |
389 if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) { | 389 if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) { |
390 throw new Exception\InvalidArgumentException('Invalid parameter "url"' | 390 throw new Exception\InvalidArgumentException('Invalid parameter "url"' |
391 . ' of "' . $url . '" must be a non-empty string and a valid' | 391 . ' of "' . $url . '" must be a non-empty string and a valid' |
392 . ' URL'); | 392 . ' URL'); |
393 } | 393 } |
394 $this->authentications[$url] = $authentication; | 394 $this->authentications[$url] = $authentication; |
443 { | 443 { |
444 if (is_array($name)) { | 444 if (is_array($name)) { |
445 $this->setParameters($name); | 445 $this->setParameters($name); |
446 return $this; | 446 return $this; |
447 } | 447 } |
448 if (empty($name) || !is_string($name)) { | 448 if (empty($name) || ! is_string($name)) { |
449 throw new Exception\InvalidArgumentException('Invalid parameter "name"' | 449 throw new Exception\InvalidArgumentException('Invalid parameter "name"' |
450 . ' of "' . $name . '" must be a non-empty string'); | 450 . ' of "' . $name . '" must be a non-empty string'); |
451 } | 451 } |
452 if ($value === null) { | 452 if ($value === null) { |
453 $this->removeParameter($name); | 453 $this->removeParameter($name); |
454 return $this; | 454 return $this; |
455 } | 455 } |
456 if (empty($value) || (!is_string($value) && $value !== null)) { | 456 if (empty($value) || (! is_string($value) && $value !== null)) { |
457 throw new Exception\InvalidArgumentException('Invalid parameter "value"' | 457 throw new Exception\InvalidArgumentException('Invalid parameter "value"' |
458 . ' of "' . $value . '" must be a non-empty string'); | 458 . ' of "' . $value . '" must be a non-empty string'); |
459 } | 459 } |
460 $this->parameters[$name] = $value; | 460 $this->parameters[$name] = $value; |
461 return $this; | 461 return $this; |
482 * @return Subscriber | 482 * @return Subscriber |
483 * @throws Exception\InvalidArgumentException | 483 * @throws Exception\InvalidArgumentException |
484 */ | 484 */ |
485 public function removeParameter($name) | 485 public function removeParameter($name) |
486 { | 486 { |
487 if (empty($name) || !is_string($name)) { | 487 if (empty($name) || ! is_string($name)) { |
488 throw new Exception\InvalidArgumentException('Invalid parameter "name"' | 488 throw new Exception\InvalidArgumentException('Invalid parameter "name"' |
489 . ' of "' . $name . '" must be a non-empty string'); | 489 . ' of "' . $name . '" must be a non-empty string'); |
490 } | 490 } |
491 if (array_key_exists($name, $this->parameters)) { | 491 if (array_key_exists($name, $this->parameters)) { |
492 unset($this->parameters[$name]); | 492 unset($this->parameters[$name]); |
600 * | 600 * |
601 * @param string $mode | 601 * @param string $mode |
602 * @return void | 602 * @return void |
603 * @throws Exception\RuntimeException | 603 * @throws Exception\RuntimeException |
604 */ | 604 */ |
605 // @codingStandardsIgnoreStart | |
605 protected function _doRequest($mode) | 606 protected function _doRequest($mode) |
606 { | 607 { |
608 // @codingStandardsIgnoreEnd | |
607 $client = $this->_getHttpClient(); | 609 $client = $this->_getHttpClient(); |
608 $hubs = $this->getHubUrls(); | 610 $hubs = $this->getHubUrls(); |
609 if (empty($hubs)) { | 611 if (empty($hubs)) { |
610 throw new Exception\RuntimeException('No Hub Server URLs' | 612 throw new Exception\RuntimeException('No Hub Server URLs' |
611 . ' have been set so no subscriptions can be attempted'); | 613 . ' have been set so no subscriptions can be attempted'); |
646 /** | 648 /** |
647 * Get a basic prepared HTTP client for use | 649 * Get a basic prepared HTTP client for use |
648 * | 650 * |
649 * @return \Zend\Http\Client | 651 * @return \Zend\Http\Client |
650 */ | 652 */ |
653 // @codingStandardsIgnoreStart | |
651 protected function _getHttpClient() | 654 protected function _getHttpClient() |
652 { | 655 { |
656 // @codingStandardsIgnoreEnd | |
653 $client = PubSubHubbub::getHttpClient(); | 657 $client = PubSubHubbub::getHttpClient(); |
654 $client->setMethod(HttpRequest::METHOD_POST); | 658 $client->setMethod(HttpRequest::METHOD_POST); |
655 $client->setOptions(['useragent' => 'Zend_Feed_Pubsubhubbub_Subscriber/' | 659 $client->setOptions(['useragent' => 'Zend_Feed_Pubsubhubbub_Subscriber/' |
656 . Version::VERSION]); | 660 . Version::VERSION]); |
657 return $client; | 661 return $client; |
664 * @param string $hubUrl | 668 * @param string $hubUrl |
665 * @param string $mode | 669 * @param string $mode |
666 * @return string | 670 * @return string |
667 * @throws Exception\InvalidArgumentException | 671 * @throws Exception\InvalidArgumentException |
668 */ | 672 */ |
673 // @codingStandardsIgnoreStart | |
669 protected function _getRequestParameters($hubUrl, $mode) | 674 protected function _getRequestParameters($hubUrl, $mode) |
670 { | 675 { |
671 if (!in_array($mode, ['subscribe', 'unsubscribe'])) { | 676 // @codingStandardsIgnoreEnd |
677 if (! in_array($mode, ['subscribe', 'unsubscribe'])) { | |
672 throw new Exception\InvalidArgumentException('Invalid mode specified: "' | 678 throw new Exception\InvalidArgumentException('Invalid mode specified: "' |
673 . $mode . '" which should have been "subscribe" or "unsubscribe"'); | 679 . $mode . '" which should have been "subscribe" or "unsubscribe"'); |
674 } | 680 } |
675 | 681 |
676 $params = [ | 682 $params = [ |
703 $key = $this->_generateSubscriptionKey($params, $hubUrl); | 709 $key = $this->_generateSubscriptionKey($params, $hubUrl); |
704 $token = $this->_generateVerifyToken(); | 710 $token = $this->_generateVerifyToken(); |
705 $params['hub.verify_token'] = $token; | 711 $params['hub.verify_token'] = $token; |
706 | 712 |
707 // Note: query string only usable with PuSH 0.2 Hubs | 713 // Note: query string only usable with PuSH 0.2 Hubs |
708 if (!$this->usePathParameter) { | 714 if (! $this->usePathParameter) { |
709 $params['hub.callback'] = $this->getCallbackUrl() | 715 $params['hub.callback'] = $this->getCallbackUrl() |
710 . '?xhub.subscription=' . PubSubHubbub::urlencode($key); | 716 . '?xhub.subscription=' . PubSubHubbub::urlencode($key); |
711 } else { | 717 } else { |
712 $params['hub.callback'] = rtrim($this->getCallbackUrl(), '/') | 718 $params['hub.callback'] = rtrim($this->getCallbackUrl(), '/') |
713 . '/' . PubSubHubbub::urlencode($key); | 719 . '/' . PubSubHubbub::urlencode($key); |
736 'created_time' => $now->format('Y-m-d H:i:s'), | 742 'created_time' => $now->format('Y-m-d H:i:s'), |
737 'lease_seconds' => $params['hub.lease_seconds'], | 743 'lease_seconds' => $params['hub.lease_seconds'], |
738 'verify_token' => hash('sha256', $params['hub.verify_token']), | 744 'verify_token' => hash('sha256', $params['hub.verify_token']), |
739 'secret' => null, | 745 'secret' => null, |
740 'expiration_time' => $expires, | 746 'expiration_time' => $expires, |
741 'subscription_state' => ($mode == 'unsubscribe')? PubSubHubbub::SUBSCRIPTION_TODELETE : PubSubHubbub::SUBSCRIPTION_NOTVERIFIED, | 747 // @codingStandardsIgnoreStart |
748 'subscription_state' => ($mode == 'unsubscribe') ? PubSubHubbub::SUBSCRIPTION_TODELETE : PubSubHubbub::SUBSCRIPTION_NOTVERIFIED, | |
749 // @codingStandardsIgnoreEnd | |
742 ]; | 750 ]; |
743 $this->getStorage()->setSubscription($data); | 751 $this->getStorage()->setSubscription($data); |
744 | 752 |
745 return $this->_toByteValueOrderedString( | 753 return $this->_toByteValueOrderedString( |
746 $this->_urlEncode($params) | 754 $this->_urlEncode($params) |
752 * requests to a Hub Server. Follows no particular method, which means | 760 * requests to a Hub Server. Follows no particular method, which means |
753 * it might be improved/changed in future. | 761 * it might be improved/changed in future. |
754 * | 762 * |
755 * @return string | 763 * @return string |
756 */ | 764 */ |
765 // @codingStandardsIgnoreStart | |
757 protected function _generateVerifyToken() | 766 protected function _generateVerifyToken() |
758 { | 767 { |
759 if (!empty($this->testStaticToken)) { | 768 // @codingStandardsIgnoreEnd |
769 if (! empty($this->testStaticToken)) { | |
760 return $this->testStaticToken; | 770 return $this->testStaticToken; |
761 } | 771 } |
762 return uniqid(rand(), true) . time(); | 772 return uniqid(rand(), true) . time(); |
763 } | 773 } |
764 | 774 |
768 * | 778 * |
769 * @param array $params | 779 * @param array $params |
770 * @param string $hubUrl The Hub Server URL for which this token will apply | 780 * @param string $hubUrl The Hub Server URL for which this token will apply |
771 * @return string | 781 * @return string |
772 */ | 782 */ |
783 // @codingStandardsIgnoreStart | |
773 protected function _generateSubscriptionKey(array $params, $hubUrl) | 784 protected function _generateSubscriptionKey(array $params, $hubUrl) |
774 { | 785 { |
786 // @codingStandardsIgnoreEnd | |
775 $keyBase = $params['hub.topic'] . $hubUrl; | 787 $keyBase = $params['hub.topic'] . $hubUrl; |
776 $key = md5($keyBase); | 788 $key = md5($keyBase); |
777 | 789 |
778 return $key; | 790 return $key; |
779 } | 791 } |
782 * URL Encode an array of parameters | 794 * URL Encode an array of parameters |
783 * | 795 * |
784 * @param array $params | 796 * @param array $params |
785 * @return array | 797 * @return array |
786 */ | 798 */ |
799 // @codingStandardsIgnoreStart | |
787 protected function _urlEncode(array $params) | 800 protected function _urlEncode(array $params) |
788 { | 801 { |
802 // @codingStandardsIgnoreEnd | |
789 $encoded = []; | 803 $encoded = []; |
790 foreach ($params as $key => $value) { | 804 foreach ($params as $key => $value) { |
791 if (is_array($value)) { | 805 if (is_array($value)) { |
792 $ekey = PubSubHubbub::urlencode($key); | 806 $ekey = PubSubHubbub::urlencode($key); |
793 $encoded[$ekey] = []; | 807 $encoded[$ekey] = []; |
807 * Order outgoing parameters | 821 * Order outgoing parameters |
808 * | 822 * |
809 * @param array $params | 823 * @param array $params |
810 * @return array | 824 * @return array |
811 */ | 825 */ |
826 // @codingStandardsIgnoreStart | |
812 protected function _toByteValueOrderedString(array $params) | 827 protected function _toByteValueOrderedString(array $params) |
813 { | 828 { |
829 // @codingStandardsIgnoreEnd | |
814 $return = []; | 830 $return = []; |
815 uksort($params, 'strnatcmp'); | 831 uksort($params, 'strnatcmp'); |
816 foreach ($params as $key => $value) { | 832 foreach ($params as $key => $value) { |
817 if (is_array($value)) { | 833 if (is_array($value)) { |
818 foreach ($value as $keyduplicate) { | 834 foreach ($value as $keyduplicate) { |