File indexing completed on 2024-12-22 05:36:38
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Feed_Pubsubhubbub 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Feed_Pubsubhubbub 0024 */ 0025 // require_once 'Zend/Feed/Pubsubhubbub.php'; 0026 0027 /** 0028 * @category Zend 0029 * @package Zend_Feed_Pubsubhubbub 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 class Zend_Feed_Pubsubhubbub_HttpResponse 0034 { 0035 /** 0036 * The body of any response to the current callback request 0037 * 0038 * @var string 0039 */ 0040 protected $_body = ''; 0041 0042 /** 0043 * Array of headers. Each header is an array with keys 'name' and 'value' 0044 * 0045 * @var array 0046 */ 0047 protected $_headers = array(); 0048 0049 /** 0050 * HTTP response code to use in headers 0051 * 0052 * @var int 0053 */ 0054 protected $_httpResponseCode = 200; 0055 0056 /** 0057 * Send the response, including all headers 0058 * 0059 * @return void 0060 */ 0061 public function sendResponse() 0062 { 0063 $this->sendHeaders(); 0064 echo $this->getBody(); 0065 } 0066 0067 /** 0068 * Send all headers 0069 * 0070 * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code} 0071 * has been specified, it is sent with the first header. 0072 * 0073 * @return void 0074 */ 0075 public function sendHeaders() 0076 { 0077 if (count($this->_headers) || (200 != $this->_httpResponseCode)) { 0078 $this->canSendHeaders(true); 0079 } elseif (200 == $this->_httpResponseCode) { 0080 return; 0081 } 0082 $httpCodeSent = false; 0083 foreach ($this->_headers as $header) { 0084 if (!$httpCodeSent && $this->_httpResponseCode) { 0085 header($header['name'] . ': ' . $header['value'], $header['replace'], $this->_httpResponseCode); 0086 $httpCodeSent = true; 0087 } else { 0088 header($header['name'] . ': ' . $header['value'], $header['replace']); 0089 } 0090 } 0091 if (!$httpCodeSent) { 0092 header('HTTP/1.1 ' . $this->_httpResponseCode); 0093 $httpCodeSent = true; 0094 } 0095 } 0096 0097 /** 0098 * Set a header 0099 * 0100 * If $replace is true, replaces any headers already defined with that 0101 * $name. 0102 * 0103 * @param string $name 0104 * @param string $value 0105 * @param boolean $replace 0106 * @return Zend_Feed_Pubsubhubbub_HttpResponse 0107 */ 0108 public function setHeader($name, $value, $replace = false) 0109 { 0110 $name = $this->_normalizeHeader($name); 0111 $value = (string) $value; 0112 if ($replace) { 0113 foreach ($this->_headers as $key => $header) { 0114 if ($name == $header['name']) { 0115 unset($this->_headers[$key]); 0116 } 0117 } 0118 } 0119 $this->_headers[] = array( 0120 'name' => $name, 0121 'value' => $value, 0122 'replace' => $replace, 0123 ); 0124 0125 return $this; 0126 } 0127 0128 /** 0129 * Check if a specific Header is set and return its value 0130 * 0131 * @param string $name 0132 * @return string|null 0133 */ 0134 public function getHeader($name) 0135 { 0136 $name = $this->_normalizeHeader($name); 0137 foreach ($this->_headers as $header) { 0138 if ($header['name'] == $name) { 0139 return $header['value']; 0140 } 0141 } 0142 } 0143 0144 /** 0145 * Return array of headers; see {@link $_headers} for format 0146 * 0147 * @return array 0148 */ 0149 public function getHeaders() 0150 { 0151 return $this->_headers; 0152 } 0153 0154 /** 0155 * Can we send headers? 0156 * 0157 * @param boolean $throw Whether or not to throw an exception if headers have been sent; defaults to false 0158 * @return boolean 0159 * @throws Zend_Feed_Pubsubhubbub_Exception 0160 */ 0161 public function canSendHeaders($throw = false) 0162 { 0163 $ok = headers_sent($file, $line); 0164 if ($ok && $throw) { 0165 // require_once 'Zend/Feed/Pubsubhubbub/Exception.php'; 0166 throw new Zend_Feed_Pubsubhubbub_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line); 0167 } 0168 return !$ok; 0169 } 0170 0171 /** 0172 * Set HTTP response code to use with headers 0173 * 0174 * @param int $code 0175 * @throws Zend_Feed_Pubsubhubbub_Exception 0176 * @return Zend_Feed_Pubsubhubbub_HttpResponse 0177 */ 0178 public function setHttpResponseCode($code) 0179 { 0180 if (!is_int($code) || (100 > $code) || (599 < $code)) { 0181 // require_once 'Zend/Feed/Pubsubhubbub/Exception.php'; 0182 throw new Zend_Feed_Pubsubhubbub_Exception('Invalid HTTP response' 0183 . ' code:' . $code); 0184 } 0185 $this->_httpResponseCode = $code; 0186 return $this; 0187 } 0188 0189 /** 0190 * Retrieve HTTP response code 0191 * 0192 * @return int 0193 */ 0194 public function getHttpResponseCode() 0195 { 0196 return $this->_httpResponseCode; 0197 } 0198 0199 /** 0200 * Set body content 0201 * 0202 * @param string $content 0203 * @return Zend_Feed_Pubsubhubbub_HttpResponse 0204 */ 0205 public function setBody($content) 0206 { 0207 $this->_body = (string) $content; 0208 $this->setHeader('content-length', strlen($content)); 0209 return $this; 0210 } 0211 0212 /** 0213 * Return the body content 0214 * 0215 * @return string 0216 */ 0217 public function getBody() 0218 { 0219 return $this->_body; 0220 } 0221 0222 /** 0223 * Normalizes a header name to X-Capitalized-Names 0224 * 0225 * @param string $name 0226 * @return string 0227 */ 0228 protected function _normalizeHeader($name) 0229 { 0230 $filtered = str_replace(array('-', '_'), ' ', (string) $name); 0231 $filtered = ucwords(strtolower($filtered)); 0232 $filtered = str_replace(' ', '-', $filtered); 0233 return $filtered; 0234 } 0235 }