702b2d02095256698299d4d40eff0568cb5e45ed
[yaffs-website] / vendor / zendframework / zend-feed / src / PubSubHubbub / AbstractCallback.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
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
8  */
9
10 namespace Zend\Feed\PubSubHubbub;
11
12 use Traversable;
13 use Zend\Http\PhpEnvironment\Response as PhpResponse;
14 use Zend\Stdlib\ArrayUtils;
15
16 abstract class AbstractCallback implements CallbackInterface
17 {
18     /**
19      * An instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistenceInterface
20      * used to background save any verification tokens associated with a subscription
21      * or other.
22      *
23      * @var Model\SubscriptionPersistenceInterface
24      */
25     protected $storage = null;
26
27     /**
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.
31      *
32      * @var HttpResponse|PhpResponse
33      */
34     protected $httpResponse = null;
35
36     /**
37      * The number of Subscribers for which any updates are on behalf of.
38      *
39      * @var int
40      */
41     protected $subscriberCount = 1;
42
43     /**
44      * Constructor; accepts an array or Traversable object to preset
45      * options for the Subscriber without calling all supported setter
46      * methods in turn.
47      *
48      * @param  array|Traversable $options Options array or Traversable object
49      */
50     public function __construct($options = null)
51     {
52         if ($options !== null) {
53             $this->setOptions($options);
54         }
55     }
56
57     /**
58      * Process any injected configuration options
59      *
60      * @param  array|Traversable $options Options array or Traversable object
61      * @return AbstractCallback
62      * @throws Exception\InvalidArgumentException
63      */
64     public function setOptions($options)
65     {
66         if ($options instanceof Traversable) {
67             $options = ArrayUtils::iteratorToArray($options);
68         }
69
70         if (! is_array($options)) {
71             throw new Exception\InvalidArgumentException('Array or Traversable object'
72             . 'expected, got ' . gettype($options));
73         }
74
75         if (is_array($options)) {
76             $this->setOptions($options);
77         }
78
79         if (array_key_exists('storage', $options)) {
80             $this->setStorage($options['storage']);
81         }
82         return $this;
83     }
84
85     /**
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!
90      *
91      * @return void
92      */
93     public function sendResponse()
94     {
95         $this->getHttpResponse()->send();
96     }
97
98     /**
99      * Sets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
100      * to background save any verification tokens associated with a subscription
101      * or other.
102      *
103      * @param  Model\SubscriptionPersistenceInterface $storage
104      * @return AbstractCallback
105      */
106     public function setStorage(Model\SubscriptionPersistenceInterface $storage)
107     {
108         $this->storage = $storage;
109         return $this;
110     }
111
112     /**
113      * Gets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
114      * to background save any verification tokens associated with a subscription
115      * or other.
116      *
117      * @return Model\SubscriptionPersistenceInterface
118      * @throws Exception\RuntimeException
119      */
120     public function getStorage()
121     {
122         if ($this->storage === null) {
123             throw new Exception\RuntimeException('No storage object has been'
124                 . ' set that subclasses Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence');
125         }
126         return $this->storage;
127     }
128
129     /**
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.
133      *
134      * @param  HttpResponse|PhpResponse $httpResponse
135      * @return AbstractCallback
136      * @throws Exception\InvalidArgumentException
137      */
138     public function setHttpResponse($httpResponse)
139     {
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');
144         }
145         $this->httpResponse = $httpResponse;
146         return $this;
147     }
148
149     /**
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.
153      *
154      * @return HttpResponse|PhpResponse
155      */
156     public function getHttpResponse()
157     {
158         if ($this->httpResponse === null) {
159             $this->httpResponse = new HttpResponse;
160         }
161         return $this->httpResponse;
162     }
163
164     /**
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.
168      *
169      * @param  string|int $count
170      * @return AbstractCallback
171      * @throws Exception\InvalidArgumentException
172      */
173     public function setSubscriberCount($count)
174     {
175         $count = intval($count);
176         if ($count <= 0) {
177             throw new Exception\InvalidArgumentException('Subscriber count must be'
178                 . ' greater than zero');
179         }
180         $this->subscriberCount = $count;
181         return $this;
182     }
183
184     /**
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?
187      *
188      * @return int
189      */
190     public function getSubscriberCount()
191     {
192         return $this->subscriberCount;
193     }
194
195     /**
196      * Attempt to detect the callback URL (specifically the path forward)
197      * @return string
198      */
199     // @codingStandardsIgnoreStart
200     protected function _detectCallbackUrl()
201     {
202         // @codingStandardsIgnoreEnd
203         $callbackUrl = '';
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'];
210             $scheme = 'http';
211             if ($_SERVER['HTTPS'] == 'on') {
212                 $scheme = 'https';
213             }
214             $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
215             if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
216                 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
217             }
218         } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
219             $callbackUrl = $_SERVER['ORIG_PATH_INFO'];
220             if (! empty($_SERVER['QUERY_STRING'])) {
221                 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
222             }
223         }
224         return $callbackUrl;
225     }
226
227     /**
228      * Get the HTTP host
229      *
230      * @return string
231      */
232     // @codingStandardsIgnoreStart
233     protected function _getHttpHost()
234     {
235         // @codingStandardsIgnoreEnd
236         if (! empty($_SERVER['HTTP_HOST'])) {
237             return $_SERVER['HTTP_HOST'];
238         }
239         $scheme = 'http';
240         if ($_SERVER['HTTPS'] == 'on') {
241             $scheme = 'https';
242         }
243         $name = $_SERVER['SERVER_NAME'];
244         $port = $_SERVER['SERVER_PORT'];
245         if (($scheme == 'http' && $port == 80)
246             || ($scheme == 'https' && $port == 443)
247         ) {
248             return $name;
249         }
250
251         return $name . ':' . $port;
252     }
253
254     /**
255      * Retrieve a Header value from either $_SERVER or Apache
256      *
257      * @param string $header
258      * @return bool|string
259      */
260     // @codingStandardsIgnoreStart
261     protected function _getHeader($header)
262     {
263         // @codingStandardsIgnoreEnd
264         $temp = strtoupper(str_replace('-', '_', $header));
265         if (! empty($_SERVER[$temp])) {
266             return $_SERVER[$temp];
267         }
268         $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
269         if (! empty($_SERVER[$temp])) {
270             return $_SERVER[$temp];
271         }
272         if (function_exists('apache_request_headers')) {
273             $headers = apache_request_headers();
274             if (! empty($headers[$header])) {
275                 return $headers[$header];
276             }
277         }
278         return false;
279     }
280
281     /**
282      * Return the raw body of the request
283      *
284      * @return string|false Raw body, or false if not present
285      */
286     // @codingStandardsIgnoreStart
287     protected function _getRawBody()
288     {
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'];
293         }
294         if (strlen(trim($body)) > 0) {
295             return $body;
296         }
297         return false;
298     }
299 }