PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Feed/Pubsubhubbub/HttpResponse.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 235 lines | 102 code | 16 blank | 117 comment | 18 complexity | 6a929e3ac877b89fd0b8664b9894f953 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2015 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_HttpResponse
  32. {
  33. /**
  34. * The body of any response to the current callback request
  35. *
  36. * @var string
  37. */
  38. protected $_body = '';
  39. /**
  40. * Array of headers. Each header is an array with keys 'name' and 'value'
  41. *
  42. * @var array
  43. */
  44. protected $_headers = array();
  45. /**
  46. * HTTP response code to use in headers
  47. *
  48. * @var int
  49. */
  50. protected $_httpResponseCode = 200;
  51. /**
  52. * Send the response, including all headers
  53. *
  54. * @return void
  55. */
  56. public function sendResponse()
  57. {
  58. $this->sendHeaders();
  59. echo $this->getBody();
  60. }
  61. /**
  62. * Send all headers
  63. *
  64. * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code}
  65. * has been specified, it is sent with the first header.
  66. *
  67. * @return void
  68. */
  69. public function sendHeaders()
  70. {
  71. if (count($this->_headers) || (200 != $this->_httpResponseCode)) {
  72. $this->canSendHeaders(true);
  73. } elseif (200 == $this->_httpResponseCode) {
  74. return;
  75. }
  76. $httpCodeSent = false;
  77. foreach ($this->_headers as $header) {
  78. if (!$httpCodeSent && $this->_httpResponseCode) {
  79. header($header['name'] . ': ' . $header['value'], $header['replace'], $this->_httpResponseCode);
  80. $httpCodeSent = true;
  81. } else {
  82. header($header['name'] . ': ' . $header['value'], $header['replace']);
  83. }
  84. }
  85. if (!$httpCodeSent) {
  86. header('HTTP/1.1 ' . $this->_httpResponseCode);
  87. $httpCodeSent = true;
  88. }
  89. }
  90. /**
  91. * Set a header
  92. *
  93. * If $replace is true, replaces any headers already defined with that
  94. * $name.
  95. *
  96. * @param string $name
  97. * @param string $value
  98. * @param boolean $replace
  99. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  100. */
  101. public function setHeader($name, $value, $replace = false)
  102. {
  103. $name = $this->_normalizeHeader($name);
  104. $value = (string) $value;
  105. if ($replace) {
  106. foreach ($this->_headers as $key => $header) {
  107. if ($name == $header['name']) {
  108. unset($this->_headers[$key]);
  109. }
  110. }
  111. }
  112. $this->_headers[] = array(
  113. 'name' => $name,
  114. 'value' => $value,
  115. 'replace' => $replace,
  116. );
  117. return $this;
  118. }
  119. /**
  120. * Check if a specific Header is set and return its value
  121. *
  122. * @param string $name
  123. * @return string|null
  124. */
  125. public function getHeader($name)
  126. {
  127. $name = $this->_normalizeHeader($name);
  128. foreach ($this->_headers as $header) {
  129. if ($header['name'] == $name) {
  130. return $header['value'];
  131. }
  132. }
  133. }
  134. /**
  135. * Return array of headers; see {@link $_headers} for format
  136. *
  137. * @return array
  138. */
  139. public function getHeaders()
  140. {
  141. return $this->_headers;
  142. }
  143. /**
  144. * Can we send headers?
  145. *
  146. * @param boolean $throw Whether or not to throw an exception if headers have been sent; defaults to false
  147. * @return boolean
  148. * @throws Zend_Feed_Pubsubhubbub_Exception
  149. */
  150. public function canSendHeaders($throw = false)
  151. {
  152. $ok = headers_sent($file, $line);
  153. if ($ok && $throw) {
  154. #require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  155. throw new Zend_Feed_Pubsubhubbub_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
  156. }
  157. return !$ok;
  158. }
  159. /**
  160. * Set HTTP response code to use with headers
  161. *
  162. * @param int $code
  163. * @throws Zend_Feed_Pubsubhubbub_Exception
  164. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  165. */
  166. public function setHttpResponseCode($code)
  167. {
  168. if (!is_int($code) || (100 > $code) || (599 < $code)) {
  169. #require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  170. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid HTTP response'
  171. . ' code:' . $code);
  172. }
  173. $this->_httpResponseCode = $code;
  174. return $this;
  175. }
  176. /**
  177. * Retrieve HTTP response code
  178. *
  179. * @return int
  180. */
  181. public function getHttpResponseCode()
  182. {
  183. return $this->_httpResponseCode;
  184. }
  185. /**
  186. * Set body content
  187. *
  188. * @param string $content
  189. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  190. */
  191. public function setBody($content)
  192. {
  193. $this->_body = (string) $content;
  194. $this->setHeader('content-length', strlen($content));
  195. return $this;
  196. }
  197. /**
  198. * Return the body content
  199. *
  200. * @return string
  201. */
  202. public function getBody()
  203. {
  204. return $this->_body;
  205. }
  206. /**
  207. * Normalizes a header name to X-Capitalized-Names
  208. *
  209. * @param string $name
  210. * @return string
  211. */
  212. protected function _normalizeHeader($name)
  213. {
  214. $filtered = str_replace(array('-', '_'), ' ', (string) $name);
  215. $filtered = ucwords(strtolower($filtered));
  216. $filtered = str_replace(' ', '-', $filtered);
  217. return $filtered;
  218. }
  219. }