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