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
215 if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
216 $callbackUrl = $_SERVER['HTTP_X_ORIGINAL_URL'];
217 } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
218 $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
219 } elseif (isset($_SERVER['REQUEST_URI'])) {
220 $callbackUrl = $_SERVER['REQUEST_URI'];
222 if ($_SERVER['HTTPS'] == 'on') {
225 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
226 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
227 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
229 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
230 $callbackUrl = $_SERVER['ORIG_PATH_INFO'];
231 if (! empty($_SERVER['QUERY_STRING'])) {
232 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
243 // @codingStandardsIgnoreStart
244 protected function _getHttpHost()
246 // @codingStandardsIgnoreEnd
247 if (! empty($_SERVER['HTTP_HOST'])) {
248 return $_SERVER['HTTP_HOST'];
251 if ($_SERVER['HTTPS'] == 'on') {
254 $name = $_SERVER['SERVER_NAME'];
255 $port = $_SERVER['SERVER_PORT'];
256 if (($scheme == 'http' && $port == 80)
257 || ($scheme == 'https' && $port == 443)
262 return $name . ':' . $port;
266 * Retrieve a Header value from either $_SERVER or Apache
268 * @param string $header
269 * @return bool|string
271 // @codingStandardsIgnoreStart
272 protected function _getHeader($header)
274 // @codingStandardsIgnoreEnd
275 $temp = strtoupper(str_replace('-', '_', $header));
276 if (! empty($_SERVER[$temp])) {
277 return $_SERVER[$temp];
279 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
280 if (! empty($_SERVER[$temp])) {
281 return $_SERVER[$temp];
283 if (function_exists('apache_request_headers')) {
284 $headers = apache_request_headers();
285 if (! empty($headers[$header])) {
286 return $headers[$header];
293 * Return the raw body of the request
295 * @return string|false Raw body, or false if not present
297 // @codingStandardsIgnoreStart
298 protected function _getRawBody()
300 // @codingStandardsIgnoreEnd
301 $body = is_resource($this->inputStream)
302 ? stream_get_contents($this->inputStream)
303 : file_get_contents($this->inputStream);
305 return strlen(trim($body)) > 0 ? $body : false;