/libraries/Zend/Feed/PubSubHubbub/HttpResponse.php

https://github.com/kiranatama/sagalaya · PHP · 217 lines · 103 code · 16 blank · 98 comment · 18 complexity · 8771d337a658c7ac49bfbfe376c2a190 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Feed
  9. */
  10. namespace Zend\Feed\PubSubHubbub;
  11. /**
  12. * @category Zend
  13. * @package Zend_Feed_Pubsubhubbub
  14. */
  15. class HttpResponse
  16. {
  17. /**
  18. * The body of any response to the current callback request
  19. *
  20. * @var string
  21. */
  22. protected $content = '';
  23. /**
  24. * Array of headers. Each header is an array with keys 'name' and 'value'
  25. *
  26. * @var array
  27. */
  28. protected $_headers = array();
  29. /**
  30. * HTTP response code to use in headers
  31. *
  32. * @var int
  33. */
  34. protected $statusCode = 200;
  35. /**
  36. * Send the response, including all headers
  37. *
  38. * @return void
  39. */
  40. public function send()
  41. {
  42. $this->sendHeaders();
  43. echo $this->getContent();
  44. }
  45. /**
  46. * Send all headers
  47. *
  48. * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code}
  49. * has been specified, it is sent with the first header.
  50. *
  51. * @return void
  52. */
  53. public function sendHeaders()
  54. {
  55. if (count($this->_headers) || (200 != $this->statusCode)) {
  56. $this->canSendHeaders(true);
  57. } elseif (200 == $this->statusCode) {
  58. return;
  59. }
  60. $httpCodeSent = false;
  61. foreach ($this->_headers as $header) {
  62. if (!$httpCodeSent && $this->statusCode) {
  63. header($header['name'] . ': ' . $header['value'], $header['replace'], $this->statusCode);
  64. $httpCodeSent = true;
  65. } else {
  66. header($header['name'] . ': ' . $header['value'], $header['replace']);
  67. }
  68. }
  69. if (!$httpCodeSent) {
  70. header('HTTP/1.1 ' . $this->statusCode);
  71. $httpCodeSent = true;
  72. }
  73. }
  74. /**
  75. * Set a header
  76. *
  77. * If $replace is true, replaces any headers already defined with that
  78. * $name.
  79. *
  80. * @param string $name
  81. * @param string $value
  82. * @param boolean $replace
  83. * @return \Zend\Feed\PubSubHubbub\HttpResponse
  84. */
  85. public function setHeader($name, $value, $replace = false)
  86. {
  87. $name = $this->_normalizeHeader($name);
  88. $value = (string) $value;
  89. if ($replace) {
  90. foreach ($this->_headers as $key => $header) {
  91. if ($name == $header['name']) {
  92. unset($this->_headers[$key]);
  93. }
  94. }
  95. }
  96. $this->_headers[] = array(
  97. 'name' => $name,
  98. 'value' => $value,
  99. 'replace' => $replace,
  100. );
  101. return $this;
  102. }
  103. /**
  104. * Check if a specific Header is set and return its value
  105. *
  106. * @param string $name
  107. * @return string|null
  108. */
  109. public function getHeader($name)
  110. {
  111. $name = $this->_normalizeHeader($name);
  112. foreach ($this->_headers as $header) {
  113. if ($header['name'] == $name) {
  114. return $header['value'];
  115. }
  116. }
  117. }
  118. /**
  119. * Return array of headers; see {@link $_headers} for format
  120. *
  121. * @return array
  122. */
  123. public function getHeaders()
  124. {
  125. return $this->_headers;
  126. }
  127. /**
  128. * Can we send headers?
  129. *
  130. * @param boolean $throw Whether or not to throw an exception if headers have been sent; defaults to false
  131. * @return HttpResponse
  132. * @throws Exception\RuntimeException
  133. */
  134. public function canSendHeaders($throw = false)
  135. {
  136. $ok = headers_sent($file, $line);
  137. if ($ok && $throw) {
  138. throw new Exception\RuntimeException('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
  139. }
  140. return !$ok;
  141. }
  142. /**
  143. * Set HTTP response code to use with headers
  144. *
  145. * @param int $code
  146. * @return HttpResponse
  147. * @throws Exception\InvalidArgumentException
  148. */
  149. public function setStatusCode($code)
  150. {
  151. if (!is_int($code) || (100 > $code) || (599 < $code)) {
  152. throw new Exception\InvalidArgumentException('Invalid HTTP response'
  153. . ' code:' . $code);
  154. }
  155. $this->statusCode = $code;
  156. return $this;
  157. }
  158. /**
  159. * Retrieve HTTP response code
  160. *
  161. * @return int
  162. */
  163. public function getStatusCode()
  164. {
  165. return $this->statusCode;
  166. }
  167. /**
  168. * Set body content
  169. *
  170. * @param string $content
  171. * @return \Zend\Feed\PubSubHubbub\HttpResponse
  172. */
  173. public function setContent($content)
  174. {
  175. $this->content = (string) $content;
  176. $this->setHeader('content-length', strlen($content));
  177. return $this;
  178. }
  179. /**
  180. * Return the body content
  181. *
  182. * @return string
  183. */
  184. public function getContent()
  185. {
  186. return $this->content;
  187. }
  188. /**
  189. * Normalizes a header name to X-Capitalized-Names
  190. *
  191. * @param string $name
  192. * @return string
  193. */
  194. protected function _normalizeHeader($name)
  195. {
  196. $filtered = str_replace(array('-', '_'), ' ', (string) $name);
  197. $filtered = ucwords(strtolower($filtered));
  198. $filtered = str_replace(' ', '-', $filtered);
  199. return $filtered;
  200. }
  201. }