3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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 protected function _detectCallbackUrl()
202 if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
203 $callbackUrl = $_SERVER['HTTP_X_ORIGINAL_URL'];
204 } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
205 $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
206 } elseif (isset($_SERVER['REQUEST_URI'])) {
207 $callbackUrl = $_SERVER['REQUEST_URI'];
209 if ($_SERVER['HTTPS'] == 'on') {
212 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
213 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
214 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
216 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
217 $callbackUrl= $_SERVER['ORIG_PATH_INFO'];
218 if (!empty($_SERVER['QUERY_STRING'])) {
219 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
230 protected function _getHttpHost()
232 if (!empty($_SERVER['HTTP_HOST'])) {
233 return $_SERVER['HTTP_HOST'];
236 if ($_SERVER['HTTPS'] == 'on') {
239 $name = $_SERVER['SERVER_NAME'];
240 $port = $_SERVER['SERVER_PORT'];
241 if (($scheme == 'http' && $port == 80)
242 ||
($scheme == 'https' && $port == 443)
247 return $name . ':' . $port;
251 * Retrieve a Header value from either $_SERVER or Apache
253 * @param string $header
254 * @return bool|string
256 protected function _getHeader($header)
258 $temp = strtoupper(str_replace('-', '_', $header));
259 if (!empty($_SERVER[$temp])) {
260 return $_SERVER[$temp];
262 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
263 if (!empty($_SERVER[$temp])) {
264 return $_SERVER[$temp];
266 if (function_exists('apache_request_headers')) {
267 $headers = apache_request_headers();
268 if (!empty($headers[$header])) {
269 return $headers[$header];
276 * Return the raw body of the request
278 * @return string|false Raw body, or false if not present
280 protected function _getRawBody()
282 $body = file_get_contents('php://input');
283 if (strlen(trim($body)) == 0 && isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
284 $body = $GLOBALS['HTTP_RAW_POST_DATA'];
286 if (strlen(trim($body)) > 0) {