3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Feed\PubSubHubbub;
13 use Zend\Http\PhpEnvironment\Response as PhpResponse;
14 use Zend\Stdlib\ArrayUtils;
16 abstract class AbstractCallback implements CallbackInterface
19 * An instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistenceInterface
20 * used to background save any verification tokens associated with a subscription
23 * @var Model\SubscriptionPersistenceInterface
25 protected $storage = null;
28 * An instance of a class handling Http Responses. This is implemented in
29 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
30 * (i.e. not inherited from) Zend\Controller\Response\Http.
32 * @var HttpResponse|PhpResponse
34 protected $httpResponse = null;
37 * The number of Subscribers for which any updates are on behalf of.
41 protected $subscriberCount = 1;
44 * Constructor; accepts an array or Traversable object to preset
45 * options for the Subscriber without calling all supported setter
48 * @param array|Traversable $options Options array or Traversable object
50 public function __construct($options = null)
52 if ($options !== null) {
53 $this->setOptions($options);
58 * Process any injected configuration options
60 * @param array|Traversable $options Options array or Traversable object
61 * @return AbstractCallback
62 * @throws Exception\InvalidArgumentException
64 public function setOptions($options)
66 if ($options instanceof Traversable) {
67 $options = ArrayUtils::iteratorToArray($options);
70 if (! is_array($options)) {
71 throw new Exception\InvalidArgumentException('Array or Traversable object'
72 . 'expected, got ' . gettype($options));
75 if (is_array($options)) {
76 $this->setOptions($options);
79 if (array_key_exists('storage', $options)) {
80 $this->setStorage($options['storage']);
86 * Send the response, including all headers.
87 * If you wish to handle this via Zend\Http, use the getter methods
88 * to retrieve any data needed to be set on your HTTP Response object, or
89 * simply give this object the HTTP Response instance to work with for you!
93 public function sendResponse()
95 $this->getHttpResponse()->send();
99 * Sets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
100 * to background save any verification tokens associated with a subscription
103 * @param Model\SubscriptionPersistenceInterface $storage
104 * @return AbstractCallback
106 public function setStorage(Model\SubscriptionPersistenceInterface $storage)
108 $this->storage = $storage;
113 * Gets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
114 * to background save any verification tokens associated with a subscription
117 * @return Model\SubscriptionPersistenceInterface
118 * @throws Exception\RuntimeException
120 public function getStorage()
122 if ($this->storage === null) {
123 throw new Exception\RuntimeException('No storage object has been'
124 . ' set that subclasses Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence');
126 return $this->storage;
130 * An instance of a class handling Http Responses. This is implemented in
131 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
132 * (i.e. not inherited from) Zend\Controller\Response\Http.
134 * @param HttpResponse|PhpResponse $httpResponse
135 * @return AbstractCallback
136 * @throws Exception\InvalidArgumentException
138 public function setHttpResponse($httpResponse)
140 if (! $httpResponse instanceof HttpResponse && ! $httpResponse instanceof PhpResponse) {
141 throw new Exception\InvalidArgumentException('HTTP Response object must'
142 . ' implement one of Zend\Feed\Pubsubhubbub\HttpResponse or'
143 . ' Zend\Http\PhpEnvironment\Response');
145 $this->httpResponse = $httpResponse;
150 * An instance of a class handling Http Responses. This is implemented in
151 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
152 * (i.e. not inherited from) Zend\Controller\Response\Http.
154 * @return HttpResponse|PhpResponse
156 public function getHttpResponse()
158 if ($this->httpResponse === null) {
159 $this->httpResponse = new HttpResponse;
161 return $this->httpResponse;
165 * Sets the number of Subscribers for which any updates are on behalf of.
166 * In other words, is this class serving one or more subscribers? How many?
167 * Defaults to 1 if left unchanged.
169 * @param string|int $count
170 * @return AbstractCallback
171 * @throws Exception\InvalidArgumentException
173 public function setSubscriberCount($count)
175 $count = intval($count);
177 throw new Exception\InvalidArgumentException('Subscriber count must be'
178 . ' greater than zero');
180 $this->subscriberCount = $count;
185 * Gets the number of Subscribers for which any updates are on behalf of.
186 * In other words, is this class serving one or more subscribers? How many?
190 public function getSubscriberCount()
192 return $this->subscriberCount;
196 * Attempt to detect the callback URL (specifically the path forward)
199 // @codingStandardsIgnoreStart
200 protected function _detectCallbackUrl()
202 // @codingStandardsIgnoreEnd
204 if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
205 $callbackUrl = $_SERVER['HTTP_X_ORIGINAL_URL'];
206 } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
207 $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
208 } elseif (isset($_SERVER['REQUEST_URI'])) {
209 $callbackUrl = $_SERVER['REQUEST_URI'];
211 if ($_SERVER['HTTPS'] == 'on') {
214 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
215 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
216 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
218 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
219 $callbackUrl = $_SERVER['ORIG_PATH_INFO'];
220 if (! empty($_SERVER['QUERY_STRING'])) {
221 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
232 // @codingStandardsIgnoreStart
233 protected function _getHttpHost()
235 // @codingStandardsIgnoreEnd
236 if (! empty($_SERVER['HTTP_HOST'])) {
237 return $_SERVER['HTTP_HOST'];
240 if ($_SERVER['HTTPS'] == 'on') {
243 $name = $_SERVER['SERVER_NAME'];
244 $port = $_SERVER['SERVER_PORT'];
245 if (($scheme == 'http' && $port == 80)
246 || ($scheme == 'https' && $port == 443)
251 return $name . ':' . $port;
255 * Retrieve a Header value from either $_SERVER or Apache
257 * @param string $header
258 * @return bool|string
260 // @codingStandardsIgnoreStart
261 protected function _getHeader($header)
263 // @codingStandardsIgnoreEnd
264 $temp = strtoupper(str_replace('-', '_', $header));
265 if (! empty($_SERVER[$temp])) {
266 return $_SERVER[$temp];
268 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
269 if (! empty($_SERVER[$temp])) {
270 return $_SERVER[$temp];
272 if (function_exists('apache_request_headers')) {
273 $headers = apache_request_headers();
274 if (! empty($headers[$header])) {
275 return $headers[$header];
282 * Return the raw body of the request
284 * @return string|false Raw body, or false if not present
286 // @codingStandardsIgnoreStart
287 protected function _getRawBody()
289 // @codingStandardsIgnoreEnd
290 $body = file_get_contents('php://input');
291 if (strlen(trim($body)) == 0 && isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
292 $body = $GLOBALS['HTTP_RAW_POST_DATA'];
294 if (strlen(trim($body)) > 0) {