PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Feed/Pubsubhubbub/Publisher.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 446 lines | 222 code | 40 blank | 184 comment | 31 complexity | 59905e5ee228238f4229f365ac93d344 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Publisher.php 24593 2012-01-05 20:35:02Z matthew $
  20. */
  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-2012 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_Publisher
  32. {
  33. /**
  34. * An array of URLs for all Hub Servers used by the Publisher, and to
  35. * which all topic update notifications will be sent.
  36. *
  37. * @var array
  38. */
  39. protected $_hubUrls = array();
  40. /**
  41. * An array of topic (Atom or RSS feed) URLs which have been updated and
  42. * whose updated status will be notified to all Hub Servers.
  43. *
  44. * @var array
  45. */
  46. protected $_updatedTopicUrls = array();
  47. /**
  48. * An array of any errors including keys for 'response', 'hubUrl'.
  49. * The response is the actual Zend_Http_Response object.
  50. *
  51. * @var array
  52. */
  53. protected $_errors = array();
  54. /**
  55. * An array of topic (Atom or RSS feed) URLs which have been updated and
  56. * whose updated status will be notified to all Hub Servers.
  57. *
  58. * @var array
  59. */
  60. protected $_parameters = array();
  61. /**
  62. * Constructor; accepts an array or Zend_Config instance to preset
  63. * options for the Publisher without calling all supported setter
  64. * methods in turn.
  65. *
  66. * @param array|Zend_Config $options Options array or Zend_Config instance
  67. *
  68. * @return void
  69. */
  70. public function __construct ($config = null)
  71. {
  72. if ($config !== null) {
  73. $this->setConfig($config);
  74. }
  75. }
  76. /**
  77. * Process any injected configuration options
  78. *
  79. * @param array|Zend_Config $options Options array or Zend_Config instance
  80. *
  81. * @return Zend_Feed_Pubsubhubbub_Publisher
  82. */
  83. public function setConfig ($config)
  84. {
  85. if ($config instanceof Zend_Config) {
  86. $config = $config->toArray();
  87. } elseif (!is_array($config)) {
  88. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  89. throw new Zend_Feed_Pubsubhubbub_Exception( 'Array or Zend_Config object'
  90. . 'expected, got ' . gettype($config) );
  91. }
  92. if (array_key_exists('hubUrls', $config)) {
  93. $this->addHubUrls($config['hubUrls']);
  94. }
  95. if (array_key_exists('updatedTopicUrls', $config)) {
  96. $this->addUpdatedTopicUrls($config['updatedTopicUrls']);
  97. }
  98. if (array_key_exists('parameters', $config)) {
  99. $this->setParameters($config['parameters']);
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Add a Hub Server URL supported by Publisher
  105. *
  106. * @param string $url
  107. *
  108. * @return Zend_Feed_Pubsubhubbub_Publisher
  109. */
  110. public function addHubUrl ($url)
  111. {
  112. if (empty( $url ) || !is_string($url) || !Zend_Uri::check($url)) {
  113. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  114. throw new Zend_Feed_Pubsubhubbub_Exception( 'Invalid parameter "url"'
  115. . ' of "' . $url . '" must be a non-empty string and a valid'
  116. . 'URL' );
  117. }
  118. $this->_hubUrls[] = $url;
  119. return $this;
  120. }
  121. /**
  122. * Add an array of Hub Server URLs supported by Publisher
  123. *
  124. * @param array $urls
  125. *
  126. * @return Zend_Feed_Pubsubhubbub_Publisher
  127. */
  128. public function addHubUrls (array $urls)
  129. {
  130. foreach ($urls as $url) {
  131. $this->addHubUrl($url);
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Remove a Hub Server URL
  137. *
  138. * @param string $url
  139. *
  140. * @return Zend_Feed_Pubsubhubbub_Publisher
  141. */
  142. public function removeHubUrl ($url)
  143. {
  144. if (!in_array($url, $this->getHubUrls())) {
  145. return $this;
  146. }
  147. $key = array_search($url, $this->_hubUrls);
  148. unset( $this->_hubUrls[$key] );
  149. return $this;
  150. }
  151. /**
  152. * Return an array of unique Hub Server URLs currently available
  153. *
  154. * @return array
  155. */
  156. public function getHubUrls ()
  157. {
  158. $this->_hubUrls = array_unique($this->_hubUrls);
  159. return $this->_hubUrls;
  160. }
  161. /**
  162. * Add a URL to a topic (Atom or RSS feed) which has been updated
  163. *
  164. * @param string $url
  165. *
  166. * @return Zend_Feed_Pubsubhubbub_Publisher
  167. */
  168. public function addUpdatedTopicUrl ($url)
  169. {
  170. if (empty( $url ) || !is_string($url) || !Zend_Uri::check($url)) {
  171. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  172. throw new Zend_Feed_Pubsubhubbub_Exception( 'Invalid parameter "url"'
  173. . ' of "' . $url . '" must be a non-empty string and a valid'
  174. . 'URL' );
  175. }
  176. $this->_updatedTopicUrls[] = $url;
  177. return $this;
  178. }
  179. /**
  180. * Add an array of Topic URLs which have been updated
  181. *
  182. * @param array $urls
  183. *
  184. * @return Zend_Feed_Pubsubhubbub_Publisher
  185. */
  186. public function addUpdatedTopicUrls (array $urls)
  187. {
  188. foreach ($urls as $url) {
  189. $this->addUpdatedTopicUrl($url);
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Remove an updated topic URL
  195. *
  196. * @param string $url
  197. *
  198. * @return Zend_Feed_Pubsubhubbub_Publisher
  199. */
  200. public function removeUpdatedTopicUrl ($url)
  201. {
  202. if (!in_array($url, $this->getUpdatedTopicUrls())) {
  203. return $this;
  204. }
  205. $key = array_search($url, $this->_updatedTopicUrls);
  206. unset( $this->_updatedTopicUrls[$key] );
  207. return $this;
  208. }
  209. /**
  210. * Return an array of unique updated topic URLs currently available
  211. *
  212. * @return array
  213. */
  214. public function getUpdatedTopicUrls ()
  215. {
  216. $this->_updatedTopicUrls = array_unique($this->_updatedTopicUrls);
  217. return $this->_updatedTopicUrls;
  218. }
  219. /**
  220. * Notifies a single Hub Server URL of changes
  221. *
  222. * @param string $url The Hub Server's URL
  223. *
  224. * @return void
  225. * @throws Zend_Feed_Pubsubhubbub_Exception Thrown on failure
  226. */
  227. public function notifyHub ($url)
  228. {
  229. if (empty( $url ) || !is_string($url) || !Zend_Uri::check($url)) {
  230. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  231. throw new Zend_Feed_Pubsubhubbub_Exception( 'Invalid parameter "url"'
  232. . ' of "' . $url . '" must be a non-empty string and a valid'
  233. . 'URL' );
  234. }
  235. $client = $this->_getHttpClient();
  236. $client->setUri($url);
  237. $response = $client->request();
  238. if ($response->getStatus() !== 204) {
  239. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  240. throw new Zend_Feed_Pubsubhubbub_Exception( 'Notification to Hub Server '
  241. . 'at "' . $url . '" appears to have failed with a status code of "'
  242. . $response->getStatus() . '" and message "'
  243. . $response->getMessage() . '"' );
  244. }
  245. }
  246. /**
  247. * Notifies all Hub Server URLs of changes
  248. *
  249. * If a Hub notification fails, certain data will be retained in an
  250. * an array retrieved using getErrors(), if a failure occurs for any Hubs
  251. * the isSuccess() check will return FALSE. This method is designed not
  252. * to needlessly fail with an Exception/Error unless from Zend_Http_Client.
  253. *
  254. * @return void
  255. * @throws Zend_Feed_Pubsubhubbub_Exception Thrown if no hubs attached
  256. */
  257. public function notifyAll ()
  258. {
  259. $client = $this->_getHttpClient();
  260. $hubs = $this->getHubUrls();
  261. if (empty( $hubs )) {
  262. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  263. throw new Zend_Feed_Pubsubhubbub_Exception( 'No Hub Server URLs'
  264. . ' have been set so no notifcations can be sent' );
  265. }
  266. $this->_errors = array();
  267. foreach ($hubs as $url) {
  268. $client->setUri($url);
  269. $response = $client->request();
  270. if ($response->getStatus() !== 204) {
  271. $this->_errors[] = array(
  272. 'response' => $response,
  273. 'hubUrl' => $url
  274. );
  275. }
  276. }
  277. }
  278. /**
  279. * Add an optional parameter to the update notification requests
  280. *
  281. * @param string $name
  282. * @param string|null $value
  283. *
  284. * @return Zend_Feed_Pubsubhubbub_Publisher
  285. */
  286. public function setParameter ($name, $value = null)
  287. {
  288. if (is_array($name)) {
  289. $this->setParameters($name);
  290. return $this;
  291. }
  292. if (empty( $name ) || !is_string($name)) {
  293. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  294. throw new Zend_Feed_Pubsubhubbub_Exception( 'Invalid parameter "name"'
  295. . ' of "' . $name . '" must be a non-empty string' );
  296. }
  297. if ($value === null) {
  298. $this->removeParameter($name);
  299. return $this;
  300. }
  301. if (empty( $value ) || ( !is_string($value) && $value !== null )) {
  302. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  303. throw new Zend_Feed_Pubsubhubbub_Exception( 'Invalid parameter "value"'
  304. . ' of "' . $value . '" must be a non-empty string' );
  305. }
  306. $this->_parameters[$name] = $value;
  307. return $this;
  308. }
  309. /**
  310. * Add an optional parameter to the update notification requests
  311. *
  312. * @param array $parameters
  313. *
  314. * @return Zend_Feed_Pubsubhubbub_Publisher
  315. */
  316. public function setParameters (array $parameters)
  317. {
  318. foreach ($parameters as $name => $value) {
  319. $this->setParameter($name, $value);
  320. }
  321. return $this;
  322. }
  323. /**
  324. * Remove an optional parameter for the notification requests
  325. *
  326. * @param string $name
  327. *
  328. * @return Zend_Feed_Pubsubhubbub_Publisher
  329. */
  330. public function removeParameter ($name)
  331. {
  332. if (empty( $name ) || !is_string($name)) {
  333. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  334. throw new Zend_Feed_Pubsubhubbub_Exception( 'Invalid parameter "name"'
  335. . ' of "' . $name . '" must be a non-empty string' );
  336. }
  337. if (array_key_exists($name, $this->_parameters)) {
  338. unset( $this->_parameters[$name] );
  339. }
  340. return $this;
  341. }
  342. /**
  343. * Return an array of optional parameters for notification requests
  344. *
  345. * @return array
  346. */
  347. public function getParameters ()
  348. {
  349. return $this->_parameters;
  350. }
  351. /**
  352. * Returns a boolean indicator of whether the notifications to Hub
  353. * Servers were ALL successful. If even one failed, FALSE is returned.
  354. *
  355. * @return bool
  356. */
  357. public function isSuccess ()
  358. {
  359. if (count($this->_errors) > 0) {
  360. return false;
  361. }
  362. return true;
  363. }
  364. /**
  365. * Return an array of errors met from any failures, including keys:
  366. * 'response' => the Zend_Http_Response object from the failure
  367. * 'hubUrl' => the URL of the Hub Server whose notification failed
  368. *
  369. * @return array
  370. */
  371. public function getErrors ()
  372. {
  373. return $this->_errors;
  374. }
  375. /**
  376. * Get a basic prepared HTTP client for use
  377. *
  378. * @return Zend_Http_Client
  379. */
  380. protected function _getHttpClient ()
  381. {
  382. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  383. $client->setMethod(Zend_Http_Client::POST);
  384. $client->setConfig(array(
  385. 'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Zend_Version::VERSION,
  386. ));
  387. $params = array();
  388. $params[] = 'hub.mode=publish';
  389. $topics = $this->getUpdatedTopicUrls();
  390. if (empty( $topics )) {
  391. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  392. throw new Zend_Feed_Pubsubhubbub_Exception( 'No updated topic URLs'
  393. . ' have been set' );
  394. }
  395. foreach ($topics as $topicUrl) {
  396. $params[] = 'hub.url=' . urlencode($topicUrl);
  397. }
  398. $optParams = $this->getParameters();
  399. foreach ($optParams as $name => $value) {
  400. $params[] = urlencode($name) . '=' . urlencode($value);
  401. }
  402. $paramString = implode('&', $params);
  403. $client->setRawData($paramString, 'application/x-www-form-urlencoded');
  404. return $client;
  405. }
  406. }