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 input stream to use when retrieving the request body. Defaults to
38 * php://input, but can be set to another value in order to force usage
39 * of another input method. This should primarily be used for testing
42 * @var string|resource String indicates a filename or stream to open;
43 * resource indicates an already created stream to use.
45 protected $inputStream = 'php://input';
48 * The number of Subscribers for which any updates are on behalf of.
52 protected $subscriberCount = 1;
55 * Constructor; accepts an array or Traversable object to preset
56 * options for the Subscriber without calling all supported setter
59 * @param array|Traversable $options Options array or Traversable object
61 public function __construct($options = null)
63 if ($options !== null) {
64 $this->setOptions($options);
69 * Process any injected configuration options
71 * @param array|Traversable $options Options array or Traversable object
72 * @return AbstractCallback
73 * @throws Exception\InvalidArgumentException
75 public function setOptions($options)
77 if ($options instanceof Traversable) {
78 $options = ArrayUtils::iteratorToArray($options);
81 if (! is_array($options)) {
82 throw new Exception\InvalidArgumentException('Array or Traversable object'
83 . 'expected, got ' . gettype($options));
86 if (is_array($options)) {
87 $this->setOptions($options);
90 if (array_key_exists('storage', $options)) {
91 $this->setStorage($options['storage']);
97 * Send the response, including all headers.
98 * If you wish to handle this via Zend\Http, use the getter methods
99 * to retrieve any data needed to be set on your HTTP Response object, or
100 * simply give this object the HTTP Response instance to work with for you!
104 public function sendResponse()
106 $this->getHttpResponse()->send();
110 * Sets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
111 * to background save any verification tokens associated with a subscription
114 * @param Model\SubscriptionPersistenceInterface $storage
115 * @return AbstractCallback
117 public function setStorage(Model\SubscriptionPersistenceInterface $storage)
119 $this->storage = $storage;
124 * Gets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
125 * to background save any verification tokens associated with a subscription
128 * @return Model\SubscriptionPersistenceInterface
129 * @throws Exception\RuntimeException
131 public function getStorage()
133 if ($this->storage === null) {
134 throw new Exception\RuntimeException('No storage object has been'
135 . ' set that subclasses Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence');
137 return $this->storage;
141 * An instance of a class handling Http Responses. This is implemented in
142 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
143 * (i.e. not inherited from) Zend\Controller\Response\Http.
145 * @param HttpResponse|PhpResponse $httpResponse
146 * @return AbstractCallback
147 * @throws Exception\InvalidArgumentException
149 public function setHttpResponse($httpResponse)
151 if (! $httpResponse instanceof HttpResponse && ! $httpResponse instanceof PhpResponse) {
152 throw new Exception\InvalidArgumentException('HTTP Response object must'
153 . ' implement one of Zend\Feed\Pubsubhubbub\HttpResponse or'
154 . ' Zend\Http\PhpEnvironment\Response');
156 $this->httpResponse = $httpResponse;
161 * An instance of a class handling Http Responses. This is implemented in
162 * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
163 * (i.e. not inherited from) Zend\Controller\Response\Http.
165 * @return HttpResponse|PhpResponse
167 public function getHttpResponse()
169 if ($this->httpResponse === null) {
170 $this->httpResponse = new HttpResponse;
172 return $this->httpResponse;
176 * Sets the number of Subscribers for which any updates are on behalf of.
177 * In other words, is this class serving one or more subscribers? How many?
178 * Defaults to 1 if left unchanged.
180 * @param string|int $count
181 * @return AbstractCallback
182 * @throws Exception\InvalidArgumentException
184 public function setSubscriberCount($count)
186 $count = intval($count);
188 throw new Exception\InvalidArgumentException('Subscriber count must be'
189 . ' greater than zero');
191 $this->subscriberCount = $count;
196 * Gets the number of Subscribers for which any updates are on behalf of.
197 * In other words, is this class serving one or more subscribers? How many?
201 public function getSubscriberCount()
203 return $this->subscriberCount;
207 * Attempt to detect the callback URL (specifically the path forward)
210 // @codingStandardsIgnoreStart
211 protected function _detectCallbackUrl()
213 // @codingStandardsIgnoreEnd
216 // IIS7 with URL Rewrite: make sure we get the unencoded url
217 // (double slash problem).
218 $iisUrlRewritten = isset($_SERVER['IIS_WasUrlRewritten']) ? $_SERVER['IIS_WasUrlRewritten'] : null;
219 $unencodedUrl = isset($_SERVER['UNENCODED_URL']) ? $_SERVER['UNENCODED_URL'] : null;
220 if ('1' == $iisUrlRewritten && ! empty($unencodedUrl)) {
221 return $unencodedUrl;
224 // HTTP proxy requests setup request URI with scheme and host [and port]
225 // + the URL path, only use URL path.
226 if (isset($_SERVER['REQUEST_URI'])) {
227 $callbackUrl = $this->buildCallbackUrlFromRequestUri();
230 if (null !== $callbackUrl) {
234 if (isset($_SERVER['ORIG_PATH_INFO'])) {
235 return $this->buildCallbackUrlFromOrigPathInfo();
246 // @codingStandardsIgnoreStart
247 protected function _getHttpHost()
249 // @codingStandardsIgnoreEnd
250 if (! empty($_SERVER['HTTP_HOST'])) {
251 return $_SERVER['HTTP_HOST'];
254 $https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : null;
255 $scheme = $https === 'on' ? 'https' : 'http';
256 $name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
257 $port = isset($_SERVER['SERVER_PORT']) ? (int) $_SERVER['SERVER_PORT'] : 80;
259 if (($scheme === 'http' && $port === 80)
260 || ($scheme === 'https' && $port === 443)
265 return sprintf('%s:%d', $name, $port);
269 * Retrieve a Header value from either $_SERVER or Apache
271 * @param string $header
272 * @return bool|string
274 // @codingStandardsIgnoreStart
275 protected function _getHeader($header)
277 // @codingStandardsIgnoreEnd
278 $temp = strtoupper(str_replace('-', '_', $header));
279 if (! empty($_SERVER[$temp])) {
280 return $_SERVER[$temp];
282 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
283 if (! empty($_SERVER[$temp])) {
284 return $_SERVER[$temp];
286 if (function_exists('apache_request_headers')) {
287 $headers = apache_request_headers();
288 if (! empty($headers[$header])) {
289 return $headers[$header];
296 * Return the raw body of the request
298 * @return string|false Raw body, or false if not present
300 // @codingStandardsIgnoreStart
301 protected function _getRawBody()
303 // @codingStandardsIgnoreEnd
304 $body = is_resource($this->inputStream)
305 ? stream_get_contents($this->inputStream)
306 : file_get_contents($this->inputStream);
308 return strlen(trim($body)) > 0 ? $body : false;
312 * Build the callback URL from the REQUEST_URI server parameter.
316 private function buildCallbackUrlFromRequestUri()
318 $callbackUrl = $_SERVER['REQUEST_URI'];
319 $https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : null;
320 $scheme = $https === 'on' ? 'https' : 'http';
321 if ($https === 'on') {
324 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
325 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
326 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
332 * Build the callback URL from the ORIG_PATH_INFO server parameter.
336 private function buildCallbackUrlFromOrigPathInfo()
338 $callbackUrl = $_SERVER['ORIG_PATH_INFO'];
339 if (! empty($_SERVER['QUERY_STRING'])) {
340 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];