Mercurial > hg > isophonics-drupal-site
comparison vendor/zendframework/zend-feed/src/PubSubHubbub/AbstractCallback.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 protected function _detectCallbackUrl() | |
200 { | |
201 $callbackUrl = ''; | |
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']; | |
208 $scheme = 'http'; | |
209 if ($_SERVER['HTTPS'] == 'on') { | |
210 $scheme = 'https'; | |
211 } | |
212 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost(); | |
213 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) { | |
214 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost)); | |
215 } | |
216 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { | |
217 $callbackUrl= $_SERVER['ORIG_PATH_INFO']; | |
218 if (!empty($_SERVER['QUERY_STRING'])) { | |
219 $callbackUrl .= '?' . $_SERVER['QUERY_STRING']; | |
220 } | |
221 } | |
222 return $callbackUrl; | |
223 } | |
224 | |
225 /** | |
226 * Get the HTTP host | |
227 * | |
228 * @return string | |
229 */ | |
230 protected function _getHttpHost() | |
231 { | |
232 if (!empty($_SERVER['HTTP_HOST'])) { | |
233 return $_SERVER['HTTP_HOST']; | |
234 } | |
235 $scheme = 'http'; | |
236 if ($_SERVER['HTTPS'] == 'on') { | |
237 $scheme = 'https'; | |
238 } | |
239 $name = $_SERVER['SERVER_NAME']; | |
240 $port = $_SERVER['SERVER_PORT']; | |
241 if (($scheme == 'http' && $port == 80) | |
242 || ($scheme == 'https' && $port == 443) | |
243 ) { | |
244 return $name; | |
245 } | |
246 | |
247 return $name . ':' . $port; | |
248 } | |
249 | |
250 /** | |
251 * Retrieve a Header value from either $_SERVER or Apache | |
252 * | |
253 * @param string $header | |
254 * @return bool|string | |
255 */ | |
256 protected function _getHeader($header) | |
257 { | |
258 $temp = strtoupper(str_replace('-', '_', $header)); | |
259 if (!empty($_SERVER[$temp])) { | |
260 return $_SERVER[$temp]; | |
261 } | |
262 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); | |
263 if (!empty($_SERVER[$temp])) { | |
264 return $_SERVER[$temp]; | |
265 } | |
266 if (function_exists('apache_request_headers')) { | |
267 $headers = apache_request_headers(); | |
268 if (!empty($headers[$header])) { | |
269 return $headers[$header]; | |
270 } | |
271 } | |
272 return false; | |
273 } | |
274 | |
275 /** | |
276 * Return the raw body of the request | |
277 * | |
278 * @return string|false Raw body, or false if not present | |
279 */ | |
280 protected function _getRawBody() | |
281 { | |
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']; | |
285 } | |
286 if (strlen(trim($body)) > 0) { | |
287 return $body; | |
288 } | |
289 return false; | |
290 } | |
291 } |