PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Feed/Pubsubhubbub/Subscriber/Callback.php

https://github.com/tanduy/zf
PHP | 327 lines | 159 code | 20 blank | 148 comment | 37 complexity | bf8c84d1b99b2f98cce07ff004c329c3 MD5 | raw file
  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/rdf+xml')
  101. ) {
  102. $this->setFeedUpdate($this->_getRawBody());
  103. $this->getHttpResponse()
  104. ->setHeader('X-Hub-On-Behalf-Of', $this->getSubscriberCount());
  105. /**
  106. * Handle any (un)subscribe confirmation requests
  107. */
  108. } elseif ($this->isValidHubVerification($httpGetData)) {
  109. $data = $this->_currentSubscriptionData;
  110. $this->getHttpResponse()->setBody($httpGetData['hub_challenge']);
  111. $data['subscription_state'] = Zend_Feed_Pubsubhubbub::SUBSCRIPTION_VERIFIED;
  112. if (isset($httpGetData['hub_lease_seconds'])) {
  113. $data['lease_seconds'] = $httpGetData['hub_lease_seconds'];
  114. }
  115. $this->getStorage()->setSubscription($data);
  116. /**
  117. * Hey, C'mon! We tried everything else!
  118. */
  119. } else {
  120. $this->getHttpResponse()->setHttpResponseCode(404);
  121. }
  122. if ($sendResponseNow) {
  123. $this->sendResponse();
  124. }
  125. }
  126. /**
  127. * Checks validity of the request simply by making a quick pass and
  128. * confirming the presence of all REQUIRED parameters.
  129. *
  130. * @param array $httpGetData
  131. * @return bool
  132. */
  133. public function isValidHubVerification(array $httpGetData)
  134. {
  135. /**
  136. * As per the specification, the hub.verify_token is OPTIONAL. This
  137. * implementation of Pubsubhubbub considers it REQUIRED and will
  138. * always send a hub.verify_token parameter to be echoed back
  139. * by the Hub Server. Therefore, its absence is considered invalid.
  140. */
  141. if (strtolower($_SERVER['REQUEST_METHOD']) !== 'get') {
  142. return false;
  143. }
  144. $required = array(
  145. 'hub_mode',
  146. 'hub_topic',
  147. 'hub_challenge',
  148. 'hub_verify_token',
  149. );
  150. foreach ($required as $key) {
  151. if (!array_key_exists($key, $httpGetData)) {
  152. return false;
  153. }
  154. }
  155. if ($httpGetData['hub_mode'] !== 'subscribe'
  156. && $httpGetData['hub_mode'] !== 'unsubscribe'
  157. ) {
  158. return false;
  159. }
  160. if ($httpGetData['hub_mode'] == 'subscribe'
  161. && !array_key_exists('hub_lease_seconds', $httpGetData)
  162. ) {
  163. return false;
  164. }
  165. if (!Zend_Uri::check($httpGetData['hub_topic'])) {
  166. return false;
  167. }
  168. /**
  169. * Attempt to retrieve any Verification Token Key attached to Callback
  170. * URL's path by our Subscriber implementation
  171. */
  172. if (!$this->_hasValidVerifyToken($httpGetData)) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * Sets a newly received feed (Atom/RSS) sent by a Hub as an update to a
  179. * Topic we've subscribed to.
  180. *
  181. * @param string $feed
  182. * @return Zend_Feed_Pubsubhubbub_Subscriber_Callback
  183. */
  184. public function setFeedUpdate($feed)
  185. {
  186. $this->_feedUpdate = $feed;
  187. return $this;
  188. }
  189. /**
  190. * Check if any newly received feed (Atom/RSS) update was received
  191. *
  192. * @return bool
  193. */
  194. public function hasFeedUpdate()
  195. {
  196. if (is_null($this->_feedUpdate)) {
  197. return false;
  198. }
  199. return true;
  200. }
  201. /**
  202. * Gets a newly received feed (Atom/RSS) sent by a Hub as an update to a
  203. * Topic we've subscribed to.
  204. *
  205. * @return string
  206. */
  207. public function getFeedUpdate()
  208. {
  209. return $this->_feedUpdate;
  210. }
  211. /**
  212. * Check for a valid verify_token. By default attempts to compare values
  213. * with that sent from Hub, otherwise merely ascertains its existence.
  214. *
  215. * @param array $httpGetData
  216. * @param bool $checkValue
  217. * @return bool
  218. */
  219. protected function _hasValidVerifyToken(array $httpGetData = null, $checkValue = true)
  220. {
  221. $verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData);
  222. if (empty($verifyTokenKey)) {
  223. return false;
  224. }
  225. $verifyTokenExists = $this->getStorage()->hasSubscription($verifyTokenKey);
  226. if (!$verifyTokenExists) {
  227. return false;
  228. }
  229. if ($checkValue) {
  230. $data = $this->getStorage()->getSubscription($verifyTokenKey);
  231. $verifyToken = $data['verify_token'];
  232. if ($verifyToken !== hash('sha256', $httpGetData['hub_verify_token'])) {
  233. return false;
  234. }
  235. $this->_currentSubscriptionData = $data;
  236. return true;
  237. }
  238. return true;
  239. }
  240. /**
  241. * Attempt to detect the verification token key. This would be passed in
  242. * the Callback URL (which we are handling with this class!) as a URI
  243. * path part (the last part by convention).
  244. *
  245. * @param null|array $httpGetData
  246. * @return false|string
  247. */
  248. protected function _detectVerifyTokenKey(array $httpGetData = null)
  249. {
  250. /**
  251. * Available when sub keys encoding in Callback URL path
  252. */
  253. if (isset($this->_subscriptionKey)) {
  254. return $this->_subscriptionKey;
  255. }
  256. /**
  257. * Available only if allowed by PuSH 0.2 Hubs
  258. */
  259. if (is_array($httpGetData)
  260. && isset($httpGetData['xhub_subscription'])
  261. ) {
  262. return $httpGetData['xhub_subscription'];
  263. }
  264. /**
  265. * Available (possibly) if corrupted in transit and not part of $_GET
  266. */
  267. $params = $this->_parseQueryString();
  268. if (isset($params['xhub.subscription'])) {
  269. return rawurldecode($params['xhub.subscription']);
  270. }
  271. return false;
  272. }
  273. /**
  274. * Build an array of Query String parameters.
  275. * This bypasses $_GET which munges parameter names and cannot accept
  276. * multiple parameters with the same key.
  277. *
  278. * @return array|void
  279. */
  280. protected function _parseQueryString()
  281. {
  282. $params = array();
  283. $queryString = '';
  284. if (isset($_SERVER['QUERY_STRING'])) {
  285. $queryString = $_SERVER['QUERY_STRING'];
  286. }
  287. if (empty($queryString)) {
  288. return array();
  289. }
  290. $parts = explode('&', $queryString);
  291. foreach ($parts as $kvpair) {
  292. $pair = explode('=', $kvpair);
  293. $key = rawurldecode($pair[0]);
  294. $value = rawurldecode($pair[1]);
  295. if (isset($params[$key])) {
  296. if (is_array($params[$key])) {
  297. $params[$key][] = $value;
  298. } else {
  299. $params[$key] = array($params[$key], $value);
  300. }
  301. } else {
  302. $params[$key] = $value;
  303. }
  304. }
  305. return $params;
  306. }
  307. }