PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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