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