PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/lib/core/Zend/Feed/Pubsubhubbub/Publisher.php

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