Pubsubhubbub.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_Http_Client
  22. */
  23. require_once 'Zend/Http/Client.php';
  24. /**
  25. * @see Zend_Uri
  26. */
  27. require_once 'Zend/Uri.php';
  28. /**
  29. * @see Zend_Version
  30. */
  31. require_once 'Zend/Version.php';
  32. /**
  33. * @see Zend_Feed_Reader
  34. */
  35. require_once 'Zend/Feed/Reader.php';
  36. /**
  37. * @see Zend_Feed_Abstract
  38. */
  39. require_once 'Zend/Feed/Abstract.php';
  40. /**
  41. * @category Zend
  42. * @package Zend_Feed_Pubsubhubbub
  43. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. */
  46. class Zend_Feed_Pubsubhubbub
  47. {
  48. /**
  49. * Verification Modes
  50. */
  51. const VERIFICATION_MODE_SYNC = 'sync';
  52. const VERIFICATION_MODE_ASYNC = 'async';
  53. /**
  54. * Subscription States
  55. */
  56. const SUBSCRIPTION_VERIFIED = 'verified';
  57. const SUBSCRIPTION_NOTVERIFIED = 'not_verified';
  58. const SUBSCRIPTION_TODELETE = 'to_delete';
  59. /**
  60. * Singleton instance if required of the HTTP client
  61. *
  62. * @var Zend_Http_Client
  63. */
  64. protected static $httpClient = null;
  65. /**
  66. * Simple utility function which imports any feed URL and
  67. * determines the existence of Hub Server endpoints. This works
  68. * best if directly given an instance of Zend_Feed_Reader_Atom|Rss
  69. * to leverage off.
  70. *
  71. * @param Zend_Feed_Reader_FeedAbstract|Zend_Feed_Abstract|string $source
  72. * @return array
  73. */
  74. public static function detectHubs($source)
  75. {
  76. if (is_string($source)) {
  77. $feed = Zend_Feed_Reader::import($source);
  78. } elseif (is_object($source) && $source instanceof Zend_Feed_Reader_FeedAbstract) {
  79. $feed = $source;
  80. } elseif (is_object($source) && $source instanceof Zend_Feed_Abstract) {
  81. $feed = Zend_Feed_Reader::importFeed($source);
  82. } else {
  83. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  84. throw new Zend_Feed_Pubsubhubbub_Exception('The source parameter was'
  85. . ' invalid, i.e. not a URL string or an instance of type'
  86. . ' Zend_Feed_Reader_FeedAbstract or Zend_Feed_Abstract');
  87. }
  88. return $feed->getHubs();
  89. }
  90. /**
  91. * Allows the external environment to make Zend_Oauth use a specific
  92. * Client instance.
  93. *
  94. * @param Zend_Http_Client $httpClient
  95. * @return void
  96. */
  97. public static function setHttpClient(Zend_Http_Client $httpClient)
  98. {
  99. self::$httpClient = $httpClient;
  100. }
  101. /**
  102. * Return the singleton instance of the HTTP Client. Note that
  103. * the instance is reset and cleared of previous parameters GET/POST.
  104. * Headers are NOT reset but handled by this component if applicable.
  105. *
  106. * @return Zend_Http_Client
  107. */
  108. public static function getHttpClient()
  109. {
  110. if (!isset(self::$httpClient)):
  111. self::$httpClient = new Zend_Http_Client;
  112. else:
  113. self::$httpClient->resetParameters();
  114. endif;
  115. return self::$httpClient;
  116. }
  117. /**
  118. * Simple mechanism to delete the entire singleton HTTP Client instance
  119. * which forces an new instantiation for subsequent requests.
  120. *
  121. * @return void
  122. */
  123. public static function clearHttpClient()
  124. {
  125. self::$httpClient = null;
  126. }
  127. /**
  128. * RFC 3986 safe url encoding method
  129. *
  130. * @param string $string
  131. * @return string
  132. */
  133. public static function urlencode($string)
  134. {
  135. $rawencoded = rawurlencode($string);
  136. $rfcencoded = str_replace('%7E', '~', $rawencoded);
  137. return $rfcencoded;
  138. }
  139. }