Callback.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Feed_Pubsubhubbub
  23. */
  24. require_once 'Zend/Feed/Pubsubhubbub.php';
  25. /**
  26. * @see Zend_Feed_Pubsubhubbub
  27. */
  28. require_once 'Zend/Feed/Pubsubhubbub/CallbackAbstract.php';
  29. /**
  30. * @see Zend_Feed_Reader
  31. */
  32. require_once 'Zend/Feed/Reader.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Feed_Pubsubhubbub
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Feed_Pubsubhubbub_Subscriber_Callback
  40. extends Zend_Feed_Pubsubhubbub_CallbackAbstract
  41. {
  42. /**
  43. * Contains the content of any feeds sent as updates to the Callback URL
  44. *
  45. * @var string
  46. */
  47. protected $_feedUpdate = null;
  48. /**
  49. * Holds a manually set subscription key (i.e. identifies a unique
  50. * subscription) which is typical when it is not passed in the query string
  51. * but is part of the Callback URL path, requiring manual retrieval e.g.
  52. * using a route and the Zend_Controller_Action::_getParam() method.
  53. *
  54. * @var string
  55. */
  56. protected $_subscriptionKey = null;
  57. /**
  58. * After verification, this is set to the verified subscription's data.
  59. *
  60. * @var array
  61. */
  62. protected $_currentSubscriptionData = null;
  63. /**
  64. * Set a subscription key to use for the current callback request manually.
  65. * Required if usePathParameter is enabled for the Subscriber.
  66. *
  67. * @param string $key
  68. * @return Zend_Feed_Pubsubhubbub_Subscriber_Callback
  69. */
  70. public function setSubscriptionKey($key)
  71. {
  72. $this->_subscriptionKey = $key;
  73. return $this;
  74. }
  75. /**
  76. * Handle any callback from a Hub Server responding to a subscription or
  77. * unsubscription request. This should be the Hub Server confirming the
  78. * the request prior to taking action on it.
  79. *
  80. * @param array $httpGetData GET data if available and not in $_GET
  81. * @param bool $sendResponseNow Whether to send response now or when asked
  82. * @return void
  83. */
  84. public function handle(array $httpGetData = null, $sendResponseNow = false)
  85. {
  86. if ($httpGetData === null) {
  87. $httpGetData = $_GET;
  88. }
  89. /**
  90. * Handle any feed updates (sorry for the mess :P)
  91. *
  92. * This DOES NOT attempt to process a feed update. Feed updates
  93. * SHOULD be validated/processed by an asynchronous process so as
  94. * to avoid holding up responses to the Hub.
  95. */
  96. if (strtolower($_SERVER['REQUEST_METHOD']) == 'post'
  97. && $this->_hasValidVerifyToken(null, false)
  98. && ($this->_getHeader('Content-Type') == 'application/atom+xml'
  99. || $this->_getHeader('Content-Type') == 'application/rss+xml'
  100. || $this->_getHeader('Content-Type') == 'application/xml'
  101. || $this->_getHeader('Content-Type') == 'text/xml'
  102. || $this->_getHeader('Content-Type') == 'application/rdf+xml')
  103. ) {
  104. $this->setFeedUpdate($this->_getRawBody());
  105. $this->getHttpResponse()
  106. ->setHeader('X-Hub-On-Behalf-Of', $this->getSubscriberCount());
  107. /**
  108. * Handle any (un)subscribe confirmation requests
  109. */
  110. } elseif ($this->isValidHubVerification($httpGetData)) {
  111. $data = $this->_currentSubscriptionData;
  112. $this->getHttpResponse()->setBody($httpGetData['hub_challenge']);
  113. $data['subscription_state'] = Zend_Feed_Pubsubhubbub::SUBSCRIPTION_VERIFIED;
  114. if (isset($httpGetData['hub_lease_seconds'])) {
  115. $data['lease_seconds'] = $httpGetData['hub_lease_seconds'];
  116. }
  117. $this->getStorage()->setSubscription($data);
  118. /**
  119. * Hey, C'mon! We tried everything else!
  120. */
  121. } else {
  122. $this->getHttpResponse()->setHttpResponseCode(404);
  123. }
  124. if ($sendResponseNow) {
  125. $this->sendResponse();
  126. }
  127. }
  128. /**
  129. * Checks validity of the request simply by making a quick pass and
  130. * confirming the presence of all REQUIRED parameters.
  131. *
  132. * @param array $httpGetData
  133. * @return bool
  134. */
  135. public function isValidHubVerification(array $httpGetData)
  136. {
  137. /**
  138. * As per the specification, the hub.verify_token is OPTIONAL. This
  139. * implementation of Pubsubhubbub considers it REQUIRED and will
  140. * always send a hub.verify_token parameter to be echoed back
  141. * by the Hub Server. Therefore, its absence is considered invalid.
  142. */
  143. if (strtolower($_SERVER['REQUEST_METHOD']) !== 'get') {
  144. return false;
  145. }
  146. $required = array(
  147. 'hub_mode',
  148. 'hub_topic',
  149. 'hub_challenge',
  150. 'hub_verify_token',
  151. );
  152. foreach ($required as $key) {
  153. if (!array_key_exists($key, $httpGetData)) {
  154. return false;
  155. }
  156. }
  157. if ($httpGetData['hub_mode'] !== 'subscribe'
  158. && $httpGetData['hub_mode'] !== 'unsubscribe'
  159. ) {
  160. return false;
  161. }
  162. if ($httpGetData['hub_mode'] == 'subscribe'
  163. && !array_key_exists('hub_lease_seconds', $httpGetData)
  164. ) {
  165. return false;
  166. }
  167. if (!Zend_Uri::check($httpGetData['hub_topic'])) {
  168. return false;
  169. }
  170. /**
  171. * Attempt to retrieve any Verification Token Key attached to Callback
  172. * URL's path by our Subscriber implementation
  173. */
  174. if (!$this->_hasValidVerifyToken($httpGetData)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. /**
  180. * Sets a newly received feed (Atom/RSS) sent by a Hub as an update to a
  181. * Topic we've subscribed to.
  182. *
  183. * @param string $feed
  184. * @return Zend_Feed_Pubsubhubbub_Subscriber_Callback
  185. */
  186. public function setFeedUpdate($feed)
  187. {
  188. $this->_feedUpdate = $feed;
  189. return $this;
  190. }
  191. /**
  192. * Check if any newly received feed (Atom/RSS) update was received
  193. *
  194. * @return bool
  195. */
  196. public function hasFeedUpdate()
  197. {
  198. if (is_null($this->_feedUpdate)) {
  199. return false;
  200. }
  201. return true;
  202. }
  203. /**
  204. * Gets a newly received feed (Atom/RSS) sent by a Hub as an update to a
  205. * Topic we've subscribed to.
  206. *
  207. * @return string
  208. */
  209. public function getFeedUpdate()
  210. {
  211. return $this->_feedUpdate;
  212. }
  213. /**
  214. * Check for a valid verify_token. By default attempts to compare values
  215. * with that sent from Hub, otherwise merely ascertains its existence.
  216. *
  217. * @param array $httpGetData
  218. * @param bool $checkValue
  219. * @return bool
  220. */
  221. protected function _hasValidVerifyToken(array $httpGetData = null, $checkValue = true)
  222. {
  223. $verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData);
  224. if (empty($verifyTokenKey)) {
  225. return false;
  226. }
  227. $verifyTokenExists = $this->getStorage()->hasSubscription($verifyTokenKey);
  228. if (!$verifyTokenExists) {
  229. return false;
  230. }
  231. if ($checkValue) {
  232. $data = $this->getStorage()->getSubscription($verifyTokenKey);
  233. $verifyToken = $data['verify_token'];
  234. if ($verifyToken !== hash('sha256', $httpGetData['hub_verify_token'])) {
  235. return false;
  236. }
  237. $this->_currentSubscriptionData = $data;
  238. return true;
  239. }
  240. return true;
  241. }
  242. /**
  243. * Attempt to detect the verification token key. This would be passed in
  244. * the Callback URL (which we are handling with this class!) as a URI
  245. * path part (the last part by convention).
  246. *
  247. * @param null|array $httpGetData
  248. * @return false|string
  249. */
  250. protected function _detectVerifyTokenKey(array $httpGetData = null)
  251. {
  252. /**
  253. * Available when sub keys encoding in Callback URL path
  254. */
  255. if (isset($this->_subscriptionKey)) {
  256. return $this->_subscriptionKey;
  257. }
  258. /**
  259. * Available only if allowed by PuSH 0.2 Hubs
  260. */
  261. if (is_array($httpGetData)
  262. && isset($httpGetData['xhub_subscription'])
  263. ) {
  264. return $httpGetData['xhub_subscription'];
  265. }
  266. /**
  267. * Available (possibly) if corrupted in transit and not part of $_GET
  268. */
  269. $params = $this->_parseQueryString();
  270. if (isset($params['xhub.subscription'])) {
  271. return rawurldecode($params['xhub.subscription']);
  272. }
  273. return false;
  274. }
  275. /**
  276. * Build an array of Query String parameters.
  277. * This bypasses $_GET which munges parameter names and cannot accept
  278. * multiple parameters with the same key.
  279. *
  280. * @return array|void
  281. */
  282. protected function _parseQueryString()
  283. {
  284. $params = array();
  285. $queryString = '';
  286. if (isset($_SERVER['QUERY_STRING'])) {
  287. $queryString = $_SERVER['QUERY_STRING'];
  288. }
  289. if (empty($queryString)) {
  290. return array();
  291. }
  292. $parts = explode('&', $queryString);
  293. foreach ($parts as $kvpair) {
  294. $pair = explode('=', $kvpair);
  295. $key = rawurldecode($pair[0]);
  296. $value = rawurldecode($pair[1]);
  297. if (isset($params[$key])) {
  298. if (is_array($params[$key])) {
  299. $params[$key][] = $value;
  300. } else {
  301. $params[$key] = array($params[$key], $value);
  302. }
  303. } else {
  304. $params[$key] = $value;
  305. }
  306. }
  307. return $params;
  308. }
  309. }