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