PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Feed/Pubsubhubbub/CallbackAbstract.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 308 lines | 149 code | 18 blank | 141 comment | 34 complexity | cbcb28534c74404eed81237e6f9dfe2b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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. * @subpackage Callback
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: CallbackAbstract.php 22662 2010-07-24 17:37:36Z mabe $
  21. */
  22. /**
  23. * @see Zend_Feed_Pubsubhubbub_CallbackInterface
  24. */
  25. #require_once 'Zend/Feed/Pubsubhubbub/CallbackInterface.php';
  26. /**
  27. * @see Zend_Feed_Pubsubhubbub_HttpResponse
  28. */
  29. #require_once 'Zend/Feed/Pubsubhubbub/HttpResponse.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Feed_Pubsubhubbub
  33. * @subpackage Callback
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. abstract class Zend_Feed_Pubsubhubbub_CallbackAbstract
  38. implements Zend_Feed_Pubsubhubbub_CallbackInterface
  39. {
  40. /**
  41. * An instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used
  42. * to background save any verification tokens associated with a subscription
  43. * or other.
  44. *
  45. * @var Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
  46. */
  47. protected $_storage = null;
  48. /**
  49. * An instance of a class handling Http Responses. This is implemented in
  50. * Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
  51. * (i.e. not inherited from) Zend_Controller_Response_Http.
  52. *
  53. * @var Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http
  54. */
  55. protected $_httpResponse = null;
  56. /**
  57. * The number of Subscribers for which any updates are on behalf of.
  58. *
  59. * @var int
  60. */
  61. protected $_subscriberCount = 1;
  62. /**
  63. * Constructor; accepts an array or Zend_Config instance to preset
  64. * options for the Subscriber without calling all supported setter
  65. * methods in turn.
  66. *
  67. * @param array|Zend_Config $options Options array or Zend_Config instance
  68. */
  69. public function __construct($config = null)
  70. {
  71. if ($config !== null) {
  72. $this->setConfig($config);
  73. }
  74. }
  75. /**
  76. * Process any injected configuration options
  77. *
  78. * @param array|Zend_Config $options Options array or Zend_Config instance
  79. * @return Zend_Feed_Pubsubhubbub_CallbackAbstract
  80. */
  81. public function setConfig($config)
  82. {
  83. if ($config instanceof Zend_Config) {
  84. $config = $config->toArray();
  85. } elseif (!is_array($config)) {
  86. #require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  87. throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object'
  88. . 'expected, got ' . gettype($config));
  89. }
  90. if (array_key_exists('storage', $config)) {
  91. $this->setStorage($config['storage']);
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Send the response, including all headers.
  97. * If you wish to handle this via Zend_Controller, use the getter methods
  98. * to retrieve any data needed to be set on your HTTP Response object, or
  99. * simply give this object the HTTP Response instance to work with for you!
  100. *
  101. * @return void
  102. */
  103. public function sendResponse()
  104. {
  105. $this->getHttpResponse()->sendResponse();
  106. }
  107. /**
  108. * Sets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used
  109. * to background save any verification tokens associated with a subscription
  110. * or other.
  111. *
  112. * @param Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage
  113. * @return Zend_Feed_Pubsubhubbub_CallbackAbstract
  114. */
  115. public function setStorage(Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage)
  116. {
  117. $this->_storage = $storage;
  118. return $this;
  119. }
  120. /**
  121. * Gets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used
  122. * to background save any verification tokens associated with a subscription
  123. * or other.
  124. *
  125. * @return Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
  126. */
  127. public function getStorage()
  128. {
  129. if ($this->_storage === null) {
  130. #require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  131. throw new Zend_Feed_Pubsubhubbub_Exception('No storage object has been'
  132. . ' set that subclasses Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface');
  133. }
  134. return $this->_storage;
  135. }
  136. /**
  137. * An instance of a class handling Http Responses. This is implemented in
  138. * Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
  139. * (i.e. not inherited from) Zend_Controller_Response_Http.
  140. *
  141. * @param Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http $httpResponse
  142. * @return Zend_Feed_Pubsubhubbub_CallbackAbstract
  143. */
  144. public function setHttpResponse($httpResponse)
  145. {
  146. if (!is_object($httpResponse)
  147. || (!$httpResponse instanceof Zend_Feed_Pubsubhubbub_HttpResponse
  148. && !$httpResponse instanceof Zend_Controller_Response_Http)
  149. ) {
  150. #require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  151. throw new Zend_Feed_Pubsubhubbub_Exception('HTTP Response object must'
  152. . ' implement one of Zend_Feed_Pubsubhubbub_HttpResponse or'
  153. . ' Zend_Controller_Response_Http');
  154. }
  155. $this->_httpResponse = $httpResponse;
  156. return $this;
  157. }
  158. /**
  159. * An instance of a class handling Http Responses. This is implemented in
  160. * Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
  161. * (i.e. not inherited from) Zend_Controller_Response_Http.
  162. *
  163. * @return Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http
  164. */
  165. public function getHttpResponse()
  166. {
  167. if ($this->_httpResponse === null) {
  168. $this->_httpResponse = new Zend_Feed_Pubsubhubbub_HttpResponse;
  169. }
  170. return $this->_httpResponse;
  171. }
  172. /**
  173. * Sets the number of Subscribers for which any updates are on behalf of.
  174. * In other words, is this class serving one or more subscribers? How many?
  175. * Defaults to 1 if left unchanged.
  176. *
  177. * @param string|int $count
  178. * @return Zend_Feed_Pubsubhubbub_CallbackAbstract
  179. */
  180. public function setSubscriberCount($count)
  181. {
  182. $count = intval($count);
  183. if ($count <= 0) {
  184. #require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  185. throw new Zend_Feed_Pubsubhubbub_Exception('Subscriber count must be'
  186. . ' greater than zero');
  187. }
  188. $this->_subscriberCount = $count;
  189. return $this;
  190. }
  191. /**
  192. * Gets the number of Subscribers for which any updates are on behalf of.
  193. * In other words, is this class serving one or more subscribers? How many?
  194. *
  195. * @return int
  196. */
  197. public function getSubscriberCount()
  198. {
  199. return $this->_subscriberCount;
  200. }
  201. /**
  202. * Attempt to detect the callback URL (specifically the path forward)
  203. */
  204. protected function _detectCallbackUrl()
  205. {
  206. $callbackUrl = '';
  207. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
  208. $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
  209. } elseif (isset($_SERVER['REQUEST_URI'])) {
  210. $callbackUrl = $_SERVER['REQUEST_URI'];
  211. $scheme = 'http';
  212. if ($_SERVER['HTTPS'] == 'on') {
  213. $scheme = 'https';
  214. }
  215. $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
  216. if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
  217. $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
  218. }
  219. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
  220. $callbackUrl= $_SERVER['ORIG_PATH_INFO'];
  221. if (!empty($_SERVER['QUERY_STRING'])) {
  222. $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
  223. }
  224. }
  225. return $callbackUrl;
  226. }
  227. /**
  228. * Get the HTTP host
  229. *
  230. * @return string
  231. */
  232. protected function _getHttpHost()
  233. {
  234. if (!empty($_SERVER['HTTP_HOST'])) {
  235. return $_SERVER['HTTP_HOST'];
  236. }
  237. $scheme = 'http';
  238. if ($_SERVER['HTTPS'] == 'on') {
  239. $scheme = 'https';
  240. }
  241. $name = $_SERVER['SERVER_NAME'];
  242. $port = $_SERVER['SERVER_PORT'];
  243. if (($scheme == 'http' && $port == 80)
  244. || ($scheme == 'https' && $port == 443)
  245. ) {
  246. return $name;
  247. } else {
  248. return $name . ':' . $port;
  249. }
  250. }
  251. /**
  252. * Retrieve a Header value from either $_SERVER or Apache
  253. *
  254. * @param string $header
  255. */
  256. protected function _getHeader($header)
  257. {
  258. $temp = strtoupper(str_replace('-', '_', $header));
  259. if (!empty($_SERVER[$temp])) {
  260. return $_SERVER[$temp];
  261. }
  262. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  263. if (!empty($_SERVER[$temp])) {
  264. return $_SERVER[$temp];
  265. }
  266. if (function_exists('apache_request_headers')) {
  267. $headers = apache_request_headers();
  268. if (!empty($headers[$header])) {
  269. return $headers[$header];
  270. }
  271. }
  272. return false;
  273. }
  274. /**
  275. * Return the raw body of the request
  276. *
  277. * @return string|false Raw body, or false if not present
  278. */
  279. protected function _getRawBody()
  280. {
  281. $body = file_get_contents('php://input');
  282. if (strlen(trim($body)) == 0 && isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
  283. $body = $GLOBALS['HTTP_RAW_POST_DATA'];
  284. }
  285. if (strlen(trim($body)) > 0) {
  286. return $body;
  287. }
  288. return false;
  289. }
  290. }