/library/Zend/Feed/Pubsubhubbub/HttpResponse.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 234 lines · 102 code · 16 blank · 116 comment · 18 complexity · 24c41016714c3ebf3ef0eb669695a48a 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-2011 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-2011 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. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  164. */
  165. public function setHttpResponseCode($code)
  166. {
  167. if (!is_int($code) || (100 > $code) || (599 < $code)) {
  168. // require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  169. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid HTTP response'
  170. . ' code:' . $code);
  171. }
  172. $this->_httpResponseCode = $code;
  173. return $this;
  174. }
  175. /**
  176. * Retrieve HTTP response code
  177. *
  178. * @return int
  179. */
  180. public function getHttpResponseCode()
  181. {
  182. return $this->_httpResponseCode;
  183. }
  184. /**
  185. * Set body content
  186. *
  187. * @param string $content
  188. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  189. */
  190. public function setBody($content)
  191. {
  192. $this->_body = (string) $content;
  193. $this->setHeader('content-length', strlen($content));
  194. return $this;
  195. }
  196. /**
  197. * Return the body content
  198. *
  199. * @return string
  200. */
  201. public function getBody()
  202. {
  203. return $this->_body;
  204. }
  205. /**
  206. * Normalizes a header name to X-Capitalized-Names
  207. *
  208. * @param string $name
  209. * @return string
  210. */
  211. protected function _normalizeHeader($name)
  212. {
  213. $filtered = str_replace(array('-', '_'), ' ', (string) $name);
  214. $filtered = ucwords(strtolower($filtered));
  215. $filtered = str_replace(' ', '-', $filtered);
  216. return $filtered;
  217. }
  218. }