|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Feed_Pubsubhubbub |
|
17 * @subpackage Callback |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: CallbackAbstract.php 22662 2010-07-24 17:37:36Z mabe $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Feed_Pubsubhubbub_CallbackInterface |
|
25 */ |
|
26 require_once 'Zend/Feed/Pubsubhubbub/CallbackInterface.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Feed_Pubsubhubbub_HttpResponse |
|
30 */ |
|
31 require_once 'Zend/Feed/Pubsubhubbub/HttpResponse.php'; |
|
32 |
|
33 /** |
|
34 * @category Zend |
|
35 * @package Zend_Feed_Pubsubhubbub |
|
36 * @subpackage Callback |
|
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
39 */ |
|
40 abstract class Zend_Feed_Pubsubhubbub_CallbackAbstract |
|
41 implements Zend_Feed_Pubsubhubbub_CallbackInterface |
|
42 { |
|
43 /** |
|
44 * An instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used |
|
45 * to background save any verification tokens associated with a subscription |
|
46 * or other. |
|
47 * |
|
48 * @var Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface |
|
49 */ |
|
50 protected $_storage = null; |
|
51 |
|
52 /** |
|
53 * An instance of a class handling Http Responses. This is implemented in |
|
54 * Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with |
|
55 * (i.e. not inherited from) Zend_Controller_Response_Http. |
|
56 * |
|
57 * @var Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http |
|
58 */ |
|
59 protected $_httpResponse = null; |
|
60 |
|
61 /** |
|
62 * The number of Subscribers for which any updates are on behalf of. |
|
63 * |
|
64 * @var int |
|
65 */ |
|
66 protected $_subscriberCount = 1; |
|
67 |
|
68 /** |
|
69 * Constructor; accepts an array or Zend_Config instance to preset |
|
70 * options for the Subscriber without calling all supported setter |
|
71 * methods in turn. |
|
72 * |
|
73 * @param array|Zend_Config $options Options array or Zend_Config instance |
|
74 */ |
|
75 public function __construct($config = null) |
|
76 { |
|
77 if ($config !== null) { |
|
78 $this->setConfig($config); |
|
79 } |
|
80 } |
|
81 |
|
82 /** |
|
83 * Process any injected configuration options |
|
84 * |
|
85 * @param array|Zend_Config $options Options array or Zend_Config instance |
|
86 * @return Zend_Feed_Pubsubhubbub_CallbackAbstract |
|
87 */ |
|
88 public function setConfig($config) |
|
89 { |
|
90 if ($config instanceof Zend_Config) { |
|
91 $config = $config->toArray(); |
|
92 } elseif (!is_array($config)) { |
|
93 require_once 'Zend/Feed/Pubsubhubbub/Exception.php'; |
|
94 throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object' |
|
95 . 'expected, got ' . gettype($config)); |
|
96 } |
|
97 if (array_key_exists('storage', $config)) { |
|
98 $this->setStorage($config['storage']); |
|
99 } |
|
100 return $this; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Send the response, including all headers. |
|
105 * If you wish to handle this via Zend_Controller, use the getter methods |
|
106 * to retrieve any data needed to be set on your HTTP Response object, or |
|
107 * simply give this object the HTTP Response instance to work with for you! |
|
108 * |
|
109 * @return void |
|
110 */ |
|
111 public function sendResponse() |
|
112 { |
|
113 $this->getHttpResponse()->sendResponse(); |
|
114 } |
|
115 |
|
116 /** |
|
117 * Sets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used |
|
118 * to background save any verification tokens associated with a subscription |
|
119 * or other. |
|
120 * |
|
121 * @param Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage |
|
122 * @return Zend_Feed_Pubsubhubbub_CallbackAbstract |
|
123 */ |
|
124 public function setStorage(Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage) |
|
125 { |
|
126 $this->_storage = $storage; |
|
127 return $this; |
|
128 } |
|
129 |
|
130 /** |
|
131 * Gets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used |
|
132 * to background save any verification tokens associated with a subscription |
|
133 * or other. |
|
134 * |
|
135 * @return Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface |
|
136 */ |
|
137 public function getStorage() |
|
138 { |
|
139 if ($this->_storage === null) { |
|
140 require_once 'Zend/Feed/Pubsubhubbub/Exception.php'; |
|
141 throw new Zend_Feed_Pubsubhubbub_Exception('No storage object has been' |
|
142 . ' set that subclasses Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface'); |
|
143 } |
|
144 return $this->_storage; |
|
145 } |
|
146 |
|
147 /** |
|
148 * An instance of a class handling Http Responses. This is implemented in |
|
149 * Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with |
|
150 * (i.e. not inherited from) Zend_Controller_Response_Http. |
|
151 * |
|
152 * @param Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http $httpResponse |
|
153 * @return Zend_Feed_Pubsubhubbub_CallbackAbstract |
|
154 */ |
|
155 public function setHttpResponse($httpResponse) |
|
156 { |
|
157 if (!is_object($httpResponse) |
|
158 || (!$httpResponse instanceof Zend_Feed_Pubsubhubbub_HttpResponse |
|
159 && !$httpResponse instanceof Zend_Controller_Response_Http) |
|
160 ) { |
|
161 require_once 'Zend/Feed/Pubsubhubbub/Exception.php'; |
|
162 throw new Zend_Feed_Pubsubhubbub_Exception('HTTP Response object must' |
|
163 . ' implement one of Zend_Feed_Pubsubhubbub_HttpResponse or' |
|
164 . ' Zend_Controller_Response_Http'); |
|
165 } |
|
166 $this->_httpResponse = $httpResponse; |
|
167 return $this; |
|
168 } |
|
169 |
|
170 /** |
|
171 * An instance of a class handling Http Responses. This is implemented in |
|
172 * Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with |
|
173 * (i.e. not inherited from) Zend_Controller_Response_Http. |
|
174 * |
|
175 * @return Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http |
|
176 */ |
|
177 public function getHttpResponse() |
|
178 { |
|
179 if ($this->_httpResponse === null) { |
|
180 $this->_httpResponse = new Zend_Feed_Pubsubhubbub_HttpResponse; |
|
181 } |
|
182 return $this->_httpResponse; |
|
183 } |
|
184 |
|
185 /** |
|
186 * Sets the number of Subscribers for which any updates are on behalf of. |
|
187 * In other words, is this class serving one or more subscribers? How many? |
|
188 * Defaults to 1 if left unchanged. |
|
189 * |
|
190 * @param string|int $count |
|
191 * @return Zend_Feed_Pubsubhubbub_CallbackAbstract |
|
192 */ |
|
193 public function setSubscriberCount($count) |
|
194 { |
|
195 $count = intval($count); |
|
196 if ($count <= 0) { |
|
197 require_once 'Zend/Feed/Pubsubhubbub/Exception.php'; |
|
198 throw new Zend_Feed_Pubsubhubbub_Exception('Subscriber count must be' |
|
199 . ' greater than zero'); |
|
200 } |
|
201 $this->_subscriberCount = $count; |
|
202 return $this; |
|
203 } |
|
204 |
|
205 /** |
|
206 * Gets the number of Subscribers for which any updates are on behalf of. |
|
207 * In other words, is this class serving one or more subscribers? How many? |
|
208 * |
|
209 * @return int |
|
210 */ |
|
211 public function getSubscriberCount() |
|
212 { |
|
213 return $this->_subscriberCount; |
|
214 } |
|
215 |
|
216 /** |
|
217 * Attempt to detect the callback URL (specifically the path forward) |
|
218 */ |
|
219 protected function _detectCallbackUrl() |
|
220 { |
|
221 $callbackUrl = ''; |
|
222 if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { |
|
223 $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL']; |
|
224 } elseif (isset($_SERVER['REQUEST_URI'])) { |
|
225 $callbackUrl = $_SERVER['REQUEST_URI']; |
|
226 $scheme = 'http'; |
|
227 if ($_SERVER['HTTPS'] == 'on') { |
|
228 $scheme = 'https'; |
|
229 } |
|
230 $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost(); |
|
231 if (strpos($callbackUrl, $schemeAndHttpHost) === 0) { |
|
232 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost)); |
|
233 } |
|
234 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { |
|
235 $callbackUrl= $_SERVER['ORIG_PATH_INFO']; |
|
236 if (!empty($_SERVER['QUERY_STRING'])) { |
|
237 $callbackUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
238 } |
|
239 } |
|
240 return $callbackUrl; |
|
241 } |
|
242 |
|
243 /** |
|
244 * Get the HTTP host |
|
245 * |
|
246 * @return string |
|
247 */ |
|
248 protected function _getHttpHost() |
|
249 { |
|
250 if (!empty($_SERVER['HTTP_HOST'])) { |
|
251 return $_SERVER['HTTP_HOST']; |
|
252 } |
|
253 $scheme = 'http'; |
|
254 if ($_SERVER['HTTPS'] == 'on') { |
|
255 $scheme = 'https'; |
|
256 } |
|
257 $name = $_SERVER['SERVER_NAME']; |
|
258 $port = $_SERVER['SERVER_PORT']; |
|
259 if (($scheme == 'http' && $port == 80) |
|
260 || ($scheme == 'https' && $port == 443) |
|
261 ) { |
|
262 return $name; |
|
263 } else { |
|
264 return $name . ':' . $port; |
|
265 } |
|
266 } |
|
267 |
|
268 /** |
|
269 * Retrieve a Header value from either $_SERVER or Apache |
|
270 * |
|
271 * @param string $header |
|
272 */ |
|
273 protected function _getHeader($header) |
|
274 { |
|
275 $temp = strtoupper(str_replace('-', '_', $header)); |
|
276 if (!empty($_SERVER[$temp])) { |
|
277 return $_SERVER[$temp]; |
|
278 } |
|
279 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); |
|
280 if (!empty($_SERVER[$temp])) { |
|
281 return $_SERVER[$temp]; |
|
282 } |
|
283 if (function_exists('apache_request_headers')) { |
|
284 $headers = apache_request_headers(); |
|
285 if (!empty($headers[$header])) { |
|
286 return $headers[$header]; |
|
287 } |
|
288 } |
|
289 return false; |
|
290 } |
|
291 |
|
292 /** |
|
293 * Return the raw body of the request |
|
294 * |
|
295 * @return string|false Raw body, or false if not present |
|
296 */ |
|
297 protected function _getRawBody() |
|
298 { |
|
299 $body = file_get_contents('php://input'); |
|
300 if (strlen(trim($body)) == 0 && isset($GLOBALS['HTTP_RAW_POST_DATA'])) { |
|
301 $body = $GLOBALS['HTTP_RAW_POST_DATA']; |
|
302 } |
|
303 if (strlen(trim($body)) > 0) { |
|
304 return $body; |
|
305 } |
|
306 return false; |
|
307 } |
|
308 } |