PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/fabiancarlos/feature_seguimentos
PHP | 860 lines | 441 code | 58 blank | 361 comment | 61 complexity | 58fc0c578d3c58108560cf213b761895 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Subscriber.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Feed_Pubsubhubbub
  23. */
  24. require_once 'Zend/Feed/Pubsubhubbub.php';
  25. /**
  26. * @see Zend_Date
  27. */
  28. require_once 'Zend/Date.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Feed_Pubsubhubbub
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Feed_Pubsubhubbub_Subscriber
  36. {
  37. /**
  38. * An array of URLs for all Hub Servers to subscribe/unsubscribe.
  39. *
  40. * @var array
  41. */
  42. protected $_hubUrls = array();
  43. /**
  44. * An array of optional parameters to be included in any
  45. * (un)subscribe requests.
  46. *
  47. * @var array
  48. */
  49. protected $_parameters = array();
  50. /**
  51. * The URL of the topic (Rss or Atom feed) which is the subject of
  52. * our current intent to subscribe to/unsubscribe from updates from
  53. * the currently configured Hub Servers.
  54. *
  55. * @var string
  56. */
  57. protected $_topicUrl = '';
  58. /**
  59. * The URL Hub Servers must use when communicating with this Subscriber
  60. *
  61. * @var string
  62. */
  63. protected $_callbackUrl = '';
  64. /**
  65. * The number of seconds for which the subscriber would like to have the
  66. * subscription active. Defaults to null, i.e. not sent, to setup a
  67. * permanent subscription if possible.
  68. *
  69. * @var int
  70. */
  71. protected $_leaseSeconds = null;
  72. /**
  73. * The preferred verification mode (sync or async). By default, this
  74. * Subscriber prefers synchronous verification, but is considered
  75. * desireable to support asynchronous verification if possible.
  76. *
  77. * Zend_Feed_Pubsubhubbub_Subscriber will always send both modes, whose
  78. * order of occurance in the parameter list determines this preference.
  79. *
  80. * @var string
  81. */
  82. protected $_preferredVerificationMode
  83. = Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC;
  84. /**
  85. * An array of any errors including keys for 'response', 'hubUrl'.
  86. * The response is the actual Zend_Http_Response object.
  87. *
  88. * @var array
  89. */
  90. protected $_errors = array();
  91. /**
  92. * An array of Hub Server URLs for Hubs operating at this time in
  93. * asynchronous verification mode.
  94. *
  95. * @var array
  96. */
  97. protected $_asyncHubs = array();
  98. /**
  99. * An instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used to background
  100. * save any verification tokens associated with a subscription or other.
  101. *
  102. * @var Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
  103. */
  104. protected $_storage = null;
  105. /**
  106. * An array of authentication credentials for HTTP Basic Authentication
  107. * if required by specific Hubs. The array is indexed by Hub Endpoint URI
  108. * and the value is a simple array of the username and password to apply.
  109. *
  110. * @var array
  111. */
  112. protected $_authentications = array();
  113. /**
  114. * Tells the Subscriber to append any subscription identifier to the path
  115. * of the base Callback URL. E.g. an identifier "subkey1" would be added
  116. * to the callback URL "http://www.example.com/callback" to create a subscription
  117. * specific Callback URL of "http://www.example.com/callback/subkey1".
  118. *
  119. * This is required for all Hubs using the Pubsubhubbub 0.1 Specification.
  120. * It should be manually intercepted and passed to the Callback class using
  121. * Zend_Feed_Pubsubhubbub_Subscriber_Callback::setSubscriptionKey(). Will
  122. * require a route in the form "callback/:subkey" to allow the parameter be
  123. * retrieved from an action using the Zend_Controller_Action::_getParam()
  124. * method.
  125. *
  126. * @var string
  127. */
  128. protected $_usePathParameter = false;
  129. /**
  130. * Constructor; accepts an array or Zend_Config instance to preset
  131. * options for the Subscriber without calling all supported setter
  132. * methods in turn.
  133. *
  134. * @param array|Zend_Config $options Options array or Zend_Config instance
  135. * @return void
  136. */
  137. public function __construct($config = null)
  138. {
  139. if ($config !== null) {
  140. $this->setConfig($config);
  141. }
  142. }
  143. /**
  144. * Process any injected configuration options
  145. *
  146. * @param array|Zend_Config $options Options array or Zend_Config instance
  147. * @return Zend_Feed_Pubsubhubbub_Subscriber
  148. */
  149. public function setConfig($config)
  150. {
  151. if ($config instanceof Zend_Config) {
  152. $config = $config->toArray();
  153. } elseif (!is_array($config)) {
  154. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  155. throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object'
  156. . ' expected, got ' . gettype($config));
  157. }
  158. if (array_key_exists('hubUrls', $config)) {
  159. $this->addHubUrls($config['hubUrls']);
  160. }
  161. if (array_key_exists('callbackUrl', $config)) {
  162. $this->setCallbackUrl($config['callbackUrl']);
  163. }
  164. if (array_key_exists('topicUrl', $config)) {
  165. $this->setTopicUrl($config['topicUrl']);
  166. }
  167. if (array_key_exists('storage', $config)) {
  168. $this->setStorage($config['storage']);
  169. }
  170. if (array_key_exists('leaseSeconds', $config)) {
  171. $this->setLeaseSeconds($config['leaseSeconds']);
  172. }
  173. if (array_key_exists('parameters', $config)) {
  174. $this->setParameters($config['parameters']);
  175. }
  176. if (array_key_exists('authentications', $config)) {
  177. $this->addAuthentications($config['authentications']);
  178. }
  179. if (array_key_exists('usePathParameter', $config)) {
  180. $this->usePathParameter($config['usePathParameter']);
  181. }
  182. if (array_key_exists('preferredVerificationMode', $config)) {
  183. $this->setPreferredVerificationMode(
  184. $config['preferredVerificationMode']
  185. );
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Set the topic URL (RSS or Atom feed) to which the intended (un)subscribe
  191. * event will relate
  192. *
  193. * @param string $url
  194. * @return Zend_Feed_Pubsubhubbub_Subscriber
  195. */
  196. public function setTopicUrl($url)
  197. {
  198. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  199. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  200. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  201. .' of "' . $url . '" must be a non-empty string and a valid'
  202. .' URL');
  203. }
  204. $this->_topicUrl = $url;
  205. return $this;
  206. }
  207. /**
  208. * Set the topic URL (RSS or Atom feed) to which the intended (un)subscribe
  209. * event will relate
  210. *
  211. * @return string
  212. */
  213. public function getTopicUrl()
  214. {
  215. if (empty($this->_topicUrl)) {
  216. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  217. throw new Zend_Feed_Pubsubhubbub_Exception('A valid Topic (RSS or Atom'
  218. . ' feed) URL MUST be set before attempting any operation');
  219. }
  220. return $this->_topicUrl;
  221. }
  222. /**
  223. * Set the number of seconds for which any subscription will remain valid
  224. *
  225. * @param int $seconds
  226. * @return Zend_Feed_Pubsubhubbub_Subscriber
  227. */
  228. public function setLeaseSeconds($seconds)
  229. {
  230. $seconds = intval($seconds);
  231. if ($seconds <= 0) {
  232. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  233. throw new Zend_Feed_Pubsubhubbub_Exception('Expected lease seconds'
  234. . ' must be an integer greater than zero');
  235. }
  236. $this->_leaseSeconds = $seconds;
  237. return $this;
  238. }
  239. /**
  240. * Get the number of lease seconds on subscriptions
  241. *
  242. * @return int
  243. */
  244. public function getLeaseSeconds()
  245. {
  246. return $this->_leaseSeconds;
  247. }
  248. /**
  249. * Set the callback URL to be used by Hub Servers when communicating with
  250. * this Subscriber
  251. *
  252. * @param string $url
  253. * @return Zend_Feed_Pubsubhubbub_Subscriber
  254. */
  255. public function setCallbackUrl($url)
  256. {
  257. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  258. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  259. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  260. . ' of "' . $url . '" must be a non-empty string and a valid'
  261. . ' URL');
  262. }
  263. $this->_callbackUrl = $url;
  264. return $this;
  265. }
  266. /**
  267. * Get the callback URL to be used by Hub Servers when communicating with
  268. * this Subscriber
  269. *
  270. * @return string
  271. */
  272. public function getCallbackUrl()
  273. {
  274. if (empty($this->_callbackUrl)) {
  275. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  276. throw new Zend_Feed_Pubsubhubbub_Exception('A valid Callback URL MUST be'
  277. . ' set before attempting any operation');
  278. }
  279. return $this->_callbackUrl;
  280. }
  281. /**
  282. * Set preferred verification mode (sync or async). By default, this
  283. * Subscriber prefers synchronous verification, but does support
  284. * asynchronous if that's the Hub Server's utilised mode.
  285. *
  286. * Zend_Feed_Pubsubhubbub_Subscriber will always send both modes, whose
  287. * order of occurance in the parameter list determines this preference.
  288. *
  289. * @param string $mode Should be 'sync' or 'async'
  290. * @return Zend_Feed_Pubsubhubbub_Subscriber
  291. */
  292. public function setPreferredVerificationMode($mode)
  293. {
  294. if ($mode !== Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC
  295. && $mode !== Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC) {
  296. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  297. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid preferred'
  298. . ' mode specified: "' . $mode . '" but should be one of'
  299. . ' Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC or'
  300. . ' Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC');
  301. }
  302. $this->_preferredVerificationMode = $mode;
  303. return $this;
  304. }
  305. /**
  306. * Get preferred verification mode (sync or async).
  307. *
  308. * @return string
  309. */
  310. public function getPreferredVerificationMode()
  311. {
  312. return $this->_preferredVerificationMode;
  313. }
  314. /**
  315. * Add a Hub Server URL supported by Publisher
  316. *
  317. * @param string $url
  318. * @return Zend_Feed_Pubsubhubbub_Subscriber
  319. */
  320. public function addHubUrl($url)
  321. {
  322. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  323. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  324. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  325. . ' of "' . $url . '" must be a non-empty string and a valid'
  326. . ' URL');
  327. }
  328. $this->_hubUrls[] = $url;
  329. return $this;
  330. }
  331. /**
  332. * Add an array of Hub Server URLs supported by Publisher
  333. *
  334. * @param array $urls
  335. * @return Zend_Feed_Pubsubhubbub_Subscriber
  336. */
  337. public function addHubUrls(array $urls)
  338. {
  339. foreach ($urls as $url) {
  340. $this->addHubUrl($url);
  341. }
  342. return $this;
  343. }
  344. /**
  345. * Remove a Hub Server URL
  346. *
  347. * @param string $url
  348. * @return Zend_Feed_Pubsubhubbub_Subscriber
  349. */
  350. public function removeHubUrl($url)
  351. {
  352. if (!in_array($url, $this->getHubUrls())) {
  353. return $this;
  354. }
  355. $key = array_search($url, $this->_hubUrls);
  356. unset($this->_hubUrls[$key]);
  357. return $this;
  358. }
  359. /**
  360. * Return an array of unique Hub Server URLs currently available
  361. *
  362. * @return array
  363. */
  364. public function getHubUrls()
  365. {
  366. $this->_hubUrls = array_unique($this->_hubUrls);
  367. return $this->_hubUrls;
  368. }
  369. /**
  370. * Add authentication credentials for a given URL
  371. *
  372. * @param string $url
  373. * @param array $authentication
  374. * @return Zend_Feed_Pubsubhubbub_Subscriber
  375. */
  376. public function addAuthentication($url, array $authentication)
  377. {
  378. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  379. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  380. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  381. . ' of "' . $url . '" must be a non-empty string and a valid'
  382. . ' URL');
  383. }
  384. $this->_authentications[$url] = $authentication;
  385. return $this;
  386. }
  387. /**
  388. * Add authentication credentials for hub URLs
  389. *
  390. * @param array $authentications
  391. * @return Zend_Feed_Pubsubhubbub_Subscriber
  392. */
  393. public function addAuthentications(array $authentications)
  394. {
  395. foreach ($authentications as $url => $authentication) {
  396. $this->addAuthentication($url, $authentication);
  397. }
  398. return $this;
  399. }
  400. /**
  401. * Get all hub URL authentication credentials
  402. *
  403. * @return array
  404. */
  405. public function getAuthentications()
  406. {
  407. return $this->_authentications;
  408. }
  409. /**
  410. * Set flag indicating whether or not to use a path parameter
  411. *
  412. * @param bool $bool
  413. * @return Zend_Feed_Pubsubhubbub_Subscriber
  414. */
  415. public function usePathParameter($bool = true)
  416. {
  417. $this->_usePathParameter = $bool;
  418. return $this;
  419. }
  420. /**
  421. * Add an optional parameter to the (un)subscribe requests
  422. *
  423. * @param string $name
  424. * @param string|null $value
  425. * @return Zend_Feed_Pubsubhubbub_Subscriber
  426. */
  427. public function setParameter($name, $value = null)
  428. {
  429. if (is_array($name)) {
  430. $this->setParameters($name);
  431. return $this;
  432. }
  433. if (empty($name) || !is_string($name)) {
  434. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  435. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
  436. . ' of "' . $name . '" must be a non-empty string');
  437. }
  438. if ($value === null) {
  439. $this->removeParameter($name);
  440. return $this;
  441. }
  442. if (empty($value) || (!is_string($value) && $value !== null)) {
  443. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  444. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "value"'
  445. . ' of "' . $value . '" must be a non-empty string');
  446. }
  447. $this->_parameters[$name] = $value;
  448. return $this;
  449. }
  450. /**
  451. * Add an optional parameter to the (un)subscribe requests
  452. *
  453. * @param string $name
  454. * @param string|null $value
  455. * @return Zend_Feed_Pubsubhubbub_Subscriber
  456. */
  457. public function setParameters(array $parameters)
  458. {
  459. foreach ($parameters as $name => $value) {
  460. $this->setParameter($name, $value);
  461. }
  462. return $this;
  463. }
  464. /**
  465. * Remove an optional parameter for the (un)subscribe requests
  466. *
  467. * @param string $name
  468. * @return Zend_Feed_Pubsubhubbub_Subscriber
  469. */
  470. public function removeParameter($name)
  471. {
  472. if (empty($name) || !is_string($name)) {
  473. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  474. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
  475. . ' of "' . $name . '" must be a non-empty string');
  476. }
  477. if (array_key_exists($name, $this->_parameters)) {
  478. unset($this->_parameters[$name]);
  479. }
  480. return $this;
  481. }
  482. /**
  483. * Return an array of optional parameters for (un)subscribe requests
  484. *
  485. * @return array
  486. */
  487. public function getParameters()
  488. {
  489. return $this->_parameters;
  490. }
  491. /**
  492. * Sets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used to background
  493. * save any verification tokens associated with a subscription or other.
  494. *
  495. * @param Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage
  496. * @return Zend_Feed_Pubsubhubbub_Subscriber
  497. */
  498. public function setStorage(Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage)
  499. {
  500. $this->_storage = $storage;
  501. return $this;
  502. }
  503. /**
  504. * Gets an instance of Zend_Feed_Pubsubhubbub_Storage_StorageInterface used
  505. * to background save any verification tokens associated with a subscription
  506. * or other.
  507. *
  508. * @return Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
  509. */
  510. public function getStorage()
  511. {
  512. if ($this->_storage === null) {
  513. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  514. throw new Zend_Feed_Pubsubhubbub_Exception('No storage vehicle '
  515. . 'has been set.');
  516. }
  517. return $this->_storage;
  518. }
  519. /**
  520. * Subscribe to one or more Hub Servers using the stored Hub URLs
  521. * for the given Topic URL (RSS or Atom feed)
  522. *
  523. * @return void
  524. */
  525. public function subscribeAll()
  526. {
  527. return $this->_doRequest('subscribe');
  528. }
  529. /**
  530. * Unsubscribe from one or more Hub Servers using the stored Hub URLs
  531. * for the given Topic URL (RSS or Atom feed)
  532. *
  533. * @return void
  534. */
  535. public function unsubscribeAll()
  536. {
  537. return $this->_doRequest('unsubscribe');
  538. }
  539. /**
  540. * Returns a boolean indicator of whether the notifications to Hub
  541. * Servers were ALL successful. If even one failed, FALSE is returned.
  542. *
  543. * @return bool
  544. */
  545. public function isSuccess()
  546. {
  547. if (count($this->_errors) > 0) {
  548. return false;
  549. }
  550. return true;
  551. }
  552. /**
  553. * Return an array of errors met from any failures, including keys:
  554. * 'response' => the Zend_Http_Response object from the failure
  555. * 'hubUrl' => the URL of the Hub Server whose notification failed
  556. *
  557. * @return array
  558. */
  559. public function getErrors()
  560. {
  561. return $this->_errors;
  562. }
  563. /**
  564. * Return an array of Hub Server URLs who returned a response indicating
  565. * operation in Asynchronous Verification Mode, i.e. they will not confirm
  566. * any (un)subscription immediately but at a later time (Hubs may be
  567. * doing this as a batch process when load balancing)
  568. *
  569. * @return array
  570. */
  571. public function getAsyncHubs()
  572. {
  573. return $this->_asyncHubs;
  574. }
  575. /**
  576. * Executes an (un)subscribe request
  577. *
  578. * @param string $mode
  579. * @return void
  580. */
  581. protected function _doRequest($mode)
  582. {
  583. $client = $this->_getHttpClient();
  584. $hubs = $this->getHubUrls();
  585. if (empty($hubs)) {
  586. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  587. throw new Zend_Feed_Pubsubhubbub_Exception('No Hub Server URLs'
  588. . ' have been set so no subscriptions can be attempted');
  589. }
  590. $this->_errors = array();
  591. $this->_asyncHubs = array();
  592. foreach ($hubs as $url) {
  593. if (array_key_exists($url, $this->_authentications)) {
  594. $auth = $this->_authentications[$url];
  595. $client->setAuth($auth[0], $auth[1]);
  596. }
  597. $client->setUri($url);
  598. $client->setRawData(
  599. $this->_getRequestParameters($url, $mode),
  600. 'application/x-www-form-urlencoded'
  601. );
  602. $response = $client->request();
  603. if ($response->getStatus() !== 204
  604. && $response->getStatus() !== 202
  605. ) {
  606. $this->_errors[] = array(
  607. 'response' => $response,
  608. 'hubUrl' => $url,
  609. );
  610. /**
  611. * At first I thought it was needed, but the backend storage will
  612. * allow tracking async without any user interference. It's left
  613. * here in case the user is interested in knowing what Hubs
  614. * are using async verification modes so they may update Models and
  615. * move these to asynchronous processes.
  616. */
  617. } elseif ($response->getStatus() == 202) {
  618. $this->_asyncHubs[] = array(
  619. 'response' => $response,
  620. 'hubUrl' => $url,
  621. );
  622. }
  623. }
  624. }
  625. /**
  626. * Get a basic prepared HTTP client for use
  627. *
  628. * @param string $mode Must be "subscribe" or "unsubscribe"
  629. * @return Zend_Http_Client
  630. */
  631. protected function _getHttpClient()
  632. {
  633. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  634. $client->setMethod(Zend_Http_Client::POST);
  635. $client->setConfig(array('useragent' => 'Zend_Feed_Pubsubhubbub_Subscriber/'
  636. . Zend_Version::VERSION));
  637. return $client;
  638. }
  639. /**
  640. * Return a list of standard protocol/optional parameters for addition to
  641. * client's POST body that are specific to the current Hub Server URL
  642. *
  643. * @param string $hubUrl
  644. * @param mode $hubUrl
  645. * @return string
  646. */
  647. protected function _getRequestParameters($hubUrl, $mode)
  648. {
  649. if (!in_array($mode, array('subscribe', 'unsubscribe'))) {
  650. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  651. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid mode specified: "'
  652. . $mode . '" which should have been "subscribe" or "unsubscribe"');
  653. }
  654. $params = array(
  655. 'hub.mode' => $mode,
  656. 'hub.topic' => $this->getTopicUrl(),
  657. );
  658. if ($this->getPreferredVerificationMode()
  659. == Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC
  660. ) {
  661. $vmodes = array(
  662. Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC,
  663. Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC,
  664. );
  665. } else {
  666. $vmodes = array(
  667. Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC,
  668. Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC,
  669. );
  670. }
  671. $params['hub.verify'] = array();
  672. foreach($vmodes as $vmode) {
  673. $params['hub.verify'][] = $vmode;
  674. }
  675. /**
  676. * Establish a persistent verify_token and attach key to callback
  677. * URL's path/querystring
  678. */
  679. $key = $this->_generateSubscriptionKey($params, $hubUrl);
  680. $token = $this->_generateVerifyToken();
  681. $params['hub.verify_token'] = $token;
  682. // Note: query string only usable with PuSH 0.2 Hubs
  683. if (!$this->_usePathParameter) {
  684. $params['hub.callback'] = $this->getCallbackUrl()
  685. . '?xhub.subscription=' . Zend_Feed_Pubsubhubbub::urlencode($key);
  686. } else {
  687. $params['hub.callback'] = rtrim($this->getCallbackUrl(), '/')
  688. . '/' . Zend_Feed_Pubsubhubbub::urlencode($key);
  689. }
  690. if ($mode == 'subscribe' && $this->getLeaseSeconds() !== null) {
  691. $params['hub.lease_seconds'] = $this->getLeaseSeconds();
  692. }
  693. // hub.secret not currently supported
  694. $optParams = $this->getParameters();
  695. foreach ($optParams as $name => $value) {
  696. $params[$name] = $value;
  697. }
  698. // store subscription to storage
  699. $now = new Zend_Date;
  700. $expires = null;
  701. if (isset($params['hub.lease_seconds'])) {
  702. $expires = $now->add($params['hub.lease_seconds'], Zend_Date::SECOND)
  703. ->get('yyyy-MM-dd HH:mm:ss');
  704. }
  705. $data = array(
  706. 'id' => $key,
  707. 'topic_url' => $params['hub.topic'],
  708. 'hub_url' => $hubUrl,
  709. 'created_time' => $now->get('yyyy-MM-dd HH:mm:ss'),
  710. 'lease_seconds' => $expires,
  711. 'verify_token' => hash('sha256', $params['hub.verify_token']),
  712. 'secret' => null,
  713. 'expiration_time' => $expires,
  714. 'subscription_state' => Zend_Feed_Pubsubhubbub::SUBSCRIPTION_NOTVERIFIED,
  715. );
  716. $this->getStorage()->setSubscription($data);
  717. return $this->_toByteValueOrderedString(
  718. $this->_urlEncode($params)
  719. );
  720. }
  721. /**
  722. * Simple helper to generate a verification token used in (un)subscribe
  723. * requests to a Hub Server. Follows no particular method, which means
  724. * it might be improved/changed in future.
  725. *
  726. * @param string $hubUrl The Hub Server URL for which this token will apply
  727. * @return string
  728. */
  729. protected function _generateVerifyToken()
  730. {
  731. if (!empty($this->_testStaticToken)) {
  732. return $this->_testStaticToken;
  733. }
  734. return uniqid(rand(), true) . time();
  735. }
  736. /**
  737. * Simple helper to generate a verification token used in (un)subscribe
  738. * requests to a Hub Server.
  739. *
  740. * @param string $hubUrl The Hub Server URL for which this token will apply
  741. * @return string
  742. */
  743. protected function _generateSubscriptionKey(array $params, $hubUrl)
  744. {
  745. $keyBase = $params['hub.topic'] . $hubUrl;
  746. $key = md5($keyBase);
  747. return $key;
  748. }
  749. /**
  750. * URL Encode an array of parameters
  751. *
  752. * @param array $params
  753. * @return array
  754. */
  755. protected function _urlEncode(array $params)
  756. {
  757. $encoded = array();
  758. foreach ($params as $key => $value) {
  759. if (is_array($value)) {
  760. $ekey = Zend_Feed_Pubsubhubbub::urlencode($key);
  761. $encoded[$ekey] = array();
  762. foreach ($value as $duplicateKey) {
  763. $encoded[$ekey][]
  764. = Zend_Feed_Pubsubhubbub::urlencode($duplicateKey);
  765. }
  766. } else {
  767. $encoded[Zend_Feed_Pubsubhubbub::urlencode($key)]
  768. = Zend_Feed_Pubsubhubbub::urlencode($value);
  769. }
  770. }
  771. return $encoded;
  772. }
  773. /**
  774. * Order outgoing parameters
  775. *
  776. * @param array $params
  777. * @return array
  778. */
  779. protected function _toByteValueOrderedString(array $params)
  780. {
  781. $return = array();
  782. uksort($params, 'strnatcmp');
  783. foreach ($params as $key => $value) {
  784. if (is_array($value)) {
  785. foreach ($value as $keyduplicate) {
  786. $return[] = $key . '=' . $keyduplicate;
  787. }
  788. } else {
  789. $return[] = $key . '=' . $value;
  790. }
  791. }
  792. return implode('&', $return);
  793. }
  794. /**
  795. * This is STRICTLY for testing purposes only...
  796. */
  797. protected $_testStaticToken = null;
  798. final public function setTestStaticToken($token)
  799. {
  800. $this->_testStaticToken = (string) $token;
  801. }
  802. }