Subscriber.php 27 KB

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