annotate vendor/zendframework/zend-feed/src/PubSubHubbub/AbstractCallback.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Zend Framework (http://framework.zend.com/)
Chris@0 4 *
Chris@0 5 * @link http://github.com/zendframework/zf2 for the canonical source repository
Chris@0 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 7 * @license http://framework.zend.com/license/new-bsd New BSD License
Chris@0 8 */
Chris@0 9
Chris@0 10 namespace Zend\Feed\PubSubHubbub;
Chris@0 11
Chris@0 12 use Traversable;
Chris@0 13 use Zend\Http\PhpEnvironment\Response as PhpResponse;
Chris@0 14 use Zend\Stdlib\ArrayUtils;
Chris@0 15
Chris@0 16 abstract class AbstractCallback implements CallbackInterface
Chris@0 17 {
Chris@0 18 /**
Chris@0 19 * An instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistenceInterface
Chris@0 20 * used to background save any verification tokens associated with a subscription
Chris@0 21 * or other.
Chris@0 22 *
Chris@0 23 * @var Model\SubscriptionPersistenceInterface
Chris@0 24 */
Chris@0 25 protected $storage = null;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * An instance of a class handling Http Responses. This is implemented in
Chris@0 29 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
Chris@0 30 * (i.e. not inherited from) Zend\Controller\Response\Http.
Chris@0 31 *
Chris@0 32 * @var HttpResponse|PhpResponse
Chris@0 33 */
Chris@0 34 protected $httpResponse = null;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The number of Subscribers for which any updates are on behalf of.
Chris@0 38 *
Chris@0 39 * @var int
Chris@0 40 */
Chris@0 41 protected $subscriberCount = 1;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Constructor; accepts an array or Traversable object to preset
Chris@0 45 * options for the Subscriber without calling all supported setter
Chris@0 46 * methods in turn.
Chris@0 47 *
Chris@0 48 * @param array|Traversable $options Options array or Traversable object
Chris@0 49 */
Chris@0 50 public function __construct($options = null)
Chris@0 51 {
Chris@0 52 if ($options !== null) {
Chris@0 53 $this->setOptions($options);
Chris@0 54 }
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Process any injected configuration options
Chris@0 59 *
Chris@0 60 * @param array|Traversable $options Options array or Traversable object
Chris@0 61 * @return AbstractCallback
Chris@0 62 * @throws Exception\InvalidArgumentException
Chris@0 63 */
Chris@0 64 public function setOptions($options)
Chris@0 65 {
Chris@0 66 if ($options instanceof Traversable) {
Chris@0 67 $options = ArrayUtils::iteratorToArray($options);
Chris@0 68 }
Chris@0 69
Chris@0 70 if (!is_array($options)) {
Chris@0 71 throw new Exception\InvalidArgumentException('Array or Traversable object'
Chris@0 72 . 'expected, got ' . gettype($options));
Chris@0 73 }
Chris@0 74
Chris@0 75 if (is_array($options)) {
Chris@0 76 $this->setOptions($options);
Chris@0 77 }
Chris@0 78
Chris@0 79 if (array_key_exists('storage', $options)) {
Chris@0 80 $this->setStorage($options['storage']);
Chris@0 81 }
Chris@0 82 return $this;
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Send the response, including all headers.
Chris@0 87 * If you wish to handle this via Zend\Http, use the getter methods
Chris@0 88 * to retrieve any data needed to be set on your HTTP Response object, or
Chris@0 89 * simply give this object the HTTP Response instance to work with for you!
Chris@0 90 *
Chris@0 91 * @return void
Chris@0 92 */
Chris@0 93 public function sendResponse()
Chris@0 94 {
Chris@0 95 $this->getHttpResponse()->send();
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Sets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
Chris@0 100 * to background save any verification tokens associated with a subscription
Chris@0 101 * or other.
Chris@0 102 *
Chris@0 103 * @param Model\SubscriptionPersistenceInterface $storage
Chris@0 104 * @return AbstractCallback
Chris@0 105 */
Chris@0 106 public function setStorage(Model\SubscriptionPersistenceInterface $storage)
Chris@0 107 {
Chris@0 108 $this->storage = $storage;
Chris@0 109 return $this;
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Gets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
Chris@0 114 * to background save any verification tokens associated with a subscription
Chris@0 115 * or other.
Chris@0 116 *
Chris@0 117 * @return Model\SubscriptionPersistenceInterface
Chris@0 118 * @throws Exception\RuntimeException
Chris@0 119 */
Chris@0 120 public function getStorage()
Chris@0 121 {
Chris@0 122 if ($this->storage === null) {
Chris@0 123 throw new Exception\RuntimeException('No storage object has been'
Chris@0 124 . ' set that subclasses Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence');
Chris@0 125 }
Chris@0 126 return $this->storage;
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * An instance of a class handling Http Responses. This is implemented in
Chris@0 131 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
Chris@0 132 * (i.e. not inherited from) Zend\Controller\Response\Http.
Chris@0 133 *
Chris@0 134 * @param HttpResponse|PhpResponse $httpResponse
Chris@0 135 * @return AbstractCallback
Chris@0 136 * @throws Exception\InvalidArgumentException
Chris@0 137 */
Chris@0 138 public function setHttpResponse($httpResponse)
Chris@0 139 {
Chris@0 140 if (!$httpResponse instanceof HttpResponse && !$httpResponse instanceof PhpResponse) {
Chris@0 141 throw new Exception\InvalidArgumentException('HTTP Response object must'
Chris@0 142 . ' implement one of Zend\Feed\Pubsubhubbub\HttpResponse or'
Chris@0 143 . ' Zend\Http\PhpEnvironment\Response');
Chris@0 144 }
Chris@0 145 $this->httpResponse = $httpResponse;
Chris@0 146 return $this;
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * An instance of a class handling Http Responses. This is implemented in
Chris@0 151 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
Chris@0 152 * (i.e. not inherited from) Zend\Controller\Response\Http.
Chris@0 153 *
Chris@0 154 * @return HttpResponse|PhpResponse
Chris@0 155 */
Chris@0 156 public function getHttpResponse()
Chris@0 157 {
Chris@0 158 if ($this->httpResponse === null) {
Chris@0 159 $this->httpResponse = new HttpResponse;
Chris@0 160 }
Chris@0 161 return $this->httpResponse;
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Sets the number of Subscribers for which any updates are on behalf of.
Chris@0 166 * In other words, is this class serving one or more subscribers? How many?
Chris@0 167 * Defaults to 1 if left unchanged.
Chris@0 168 *
Chris@0 169 * @param string|int $count
Chris@0 170 * @return AbstractCallback
Chris@0 171 * @throws Exception\InvalidArgumentException
Chris@0 172 */
Chris@0 173 public function setSubscriberCount($count)
Chris@0 174 {
Chris@0 175 $count = intval($count);
Chris@0 176 if ($count <= 0) {
Chris@0 177 throw new Exception\InvalidArgumentException('Subscriber count must be'
Chris@0 178 . ' greater than zero');
Chris@0 179 }
Chris@0 180 $this->subscriberCount = $count;
Chris@0 181 return $this;
Chris@0 182 }
Chris@0 183
Chris@0 184 /**
Chris@0 185 * Gets the number of Subscribers for which any updates are on behalf of.
Chris@0 186 * In other words, is this class serving one or more subscribers? How many?
Chris@0 187 *
Chris@0 188 * @return int
Chris@0 189 */
Chris@0 190 public function getSubscriberCount()
Chris@0 191 {
Chris@0 192 return $this->subscriberCount;
Chris@0 193 }
Chris@0 194
Chris@0 195 /**
Chris@0 196 * Attempt to detect the callback URL (specifically the path forward)
Chris@0 197 * @return string
Chris@0 198 */
Chris@0 199 protected function _detectCallbackUrl()
Chris@0 200 {
Chris@0 201 $callbackUrl = '';
Chris@0 202 if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
Chris@0 203 $callbackUrl = $_SERVER['HTTP_X_ORIGINAL_URL'];
Chris@0 204 } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
Chris@0 205 $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
Chris@0 206 } elseif (isset($_SERVER['REQUEST_URI'])) {
Chris@0 207 $callbackUrl = $_SERVER['REQUEST_URI'];
Chris@0 208 $scheme = 'http';
Chris@0 209 if ($_SERVER['HTTPS'] == 'on') {
Chris@0 210 $scheme = 'https';
Chris@0 211 }
Chris@0 212 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
Chris@0 213 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
Chris@0 214 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
Chris@0 215 }
Chris@0 216 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
Chris@0 217 $callbackUrl= $_SERVER['ORIG_PATH_INFO'];
Chris@0 218 if (!empty($_SERVER['QUERY_STRING'])) {
Chris@0 219 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
Chris@0 220 }
Chris@0 221 }
Chris@0 222 return $callbackUrl;
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * Get the HTTP host
Chris@0 227 *
Chris@0 228 * @return string
Chris@0 229 */
Chris@0 230 protected function _getHttpHost()
Chris@0 231 {
Chris@0 232 if (!empty($_SERVER['HTTP_HOST'])) {
Chris@0 233 return $_SERVER['HTTP_HOST'];
Chris@0 234 }
Chris@0 235 $scheme = 'http';
Chris@0 236 if ($_SERVER['HTTPS'] == 'on') {
Chris@0 237 $scheme = 'https';
Chris@0 238 }
Chris@0 239 $name = $_SERVER['SERVER_NAME'];
Chris@0 240 $port = $_SERVER['SERVER_PORT'];
Chris@0 241 if (($scheme == 'http' && $port == 80)
Chris@0 242 || ($scheme == 'https' && $port == 443)
Chris@0 243 ) {
Chris@0 244 return $name;
Chris@0 245 }
Chris@0 246
Chris@0 247 return $name . ':' . $port;
Chris@0 248 }
Chris@0 249
Chris@0 250 /**
Chris@0 251 * Retrieve a Header value from either $_SERVER or Apache
Chris@0 252 *
Chris@0 253 * @param string $header
Chris@0 254 * @return bool|string
Chris@0 255 */
Chris@0 256 protected function _getHeader($header)
Chris@0 257 {
Chris@0 258 $temp = strtoupper(str_replace('-', '_', $header));
Chris@0 259 if (!empty($_SERVER[$temp])) {
Chris@0 260 return $_SERVER[$temp];
Chris@0 261 }
Chris@0 262 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
Chris@0 263 if (!empty($_SERVER[$temp])) {
Chris@0 264 return $_SERVER[$temp];
Chris@0 265 }
Chris@0 266 if (function_exists('apache_request_headers')) {
Chris@0 267 $headers = apache_request_headers();
Chris@0 268 if (!empty($headers[$header])) {
Chris@0 269 return $headers[$header];
Chris@0 270 }
Chris@0 271 }
Chris@0 272 return false;
Chris@0 273 }
Chris@0 274
Chris@0 275 /**
Chris@0 276 * Return the raw body of the request
Chris@0 277 *
Chris@0 278 * @return string|false Raw body, or false if not present
Chris@0 279 */
Chris@0 280 protected function _getRawBody()
Chris@0 281 {
Chris@0 282 $body = file_get_contents('php://input');
Chris@0 283 if (strlen(trim($body)) == 0 && isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
Chris@0 284 $body = $GLOBALS['HTTP_RAW_POST_DATA'];
Chris@0 285 }
Chris@0 286 if (strlen(trim($body)) > 0) {
Chris@0 287 return $body;
Chris@0 288 }
Chris@0 289 return false;
Chris@0 290 }
Chris@0 291 }