1 : <?php
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10 :
11 :
12 :
13 :
14 :
15 :
16 :
17 :
18 :
19 :
20 :
21 :
22 : require_once 'HTTP/OAuth/Message.php';
23 :
24 :
25 :
26 :
27 :
28 :
29 :
30 :
31 :
32 :
33 :
34 :
35 : class HTTP_OAuth_Provider_Response extends HTTP_OAuth_Message
36 : {
37 :
38 : const STATUS_UNSUPPORTED_PARAMETER = 0;
39 : const STATUS_UNSUPPORTED_SIGNATURE_METHOD = 1;
40 : const STATUS_MISSING_REQUIRED_PARAMETER = 2;
41 : const STATUS_DUPLICATED_OAUTH_PARAMETER = 3;
42 :
43 : const STATUS_INVALID_CONSUMER_KEY = 4;
44 : const STATUS_INVALID_TOKEN = 5;
45 : const STATUS_INVALID_SIGNATURE = 6;
46 : const STATUS_INVALID_NONCE = 7;
47 : const STATUS_INVALID_VERIFIER = 8;
48 : const STATUS_INVALID_TIMESTAMP = 9;
49 :
50 :
51 :
52 :
53 :
54 :
55 :
56 :
57 : static protected $statusMap = array(
58 : self::STATUS_UNSUPPORTED_PARAMETER => array(
59 : 400, 'Unsupported parameter'
60 : ),
61 : self::STATUS_UNSUPPORTED_SIGNATURE_METHOD => array(
62 : 400, 'Unsupported signature method'
63 : ),
64 : self::STATUS_MISSING_REQUIRED_PARAMETER => array(
65 : 400, 'Missing required parameter'
66 : ),
67 : self::STATUS_DUPLICATED_OAUTH_PARAMETER => array(
68 : 400, 'Duplicated OAuth Protocol Parameter'
69 : ),
70 : self::STATUS_INVALID_CONSUMER_KEY => array(
71 : 401, 'Invalid Consumer Key'
72 : ),
73 : self::STATUS_INVALID_TOKEN => array(
74 : 401, 'Invalid / expired Token'
75 : ),
76 : self::STATUS_INVALID_SIGNATURE => array(
77 : 401, 'Invalid signature'
78 : ),
79 : self::STATUS_INVALID_NONCE => array(
80 : 401, 'Invalid / used nonce'
81 : ),
82 : self::STATUS_INVALID_VERIFIER => array(
83 : 401, 'Invalid verifier'
84 : ),
85 : self::STATUS_INVALID_TIMESTAMP => array(
86 : 401, 'Invalid timestamp'
87 : ),
88 : );
89 :
90 :
91 :
92 :
93 :
94 :
95 : protected $headers = array();
96 :
97 :
98 :
99 :
100 :
101 :
102 : protected $body = '';
103 :
104 :
105 :
106 :
107 :
108 :
109 :
110 :
111 : public function setRealm($realm)
112 : {
113 1 : $header = 'OAuth realm="' . $realm . '"';
114 1 : $this->setHeader('WWW-Authenticate', $header);
115 1 : }
116 :
117 :
118 :
119 :
120 :
121 :
122 :
123 :
124 :
125 : public function setHeader($name, $value)
126 : {
127 2 : $this->headers[$name] = $value;
128 2 : }
129 :
130 :
131 :
132 :
133 :
134 :
135 :
136 :
137 : public function getHeader($name)
138 : {
139 1 : if (array_key_exists($name, $this->headers)) {
140 1 : return $this->headers[$name];
141 : }
142 :
143 1 : return null;
144 : }
145 :
146 :
147 :
148 :
149 :
150 :
151 : public function getHeaders()
152 : {
153 3 : return $this->headers;
154 : }
155 :
156 :
157 :
158 :
159 :
160 :
161 :
162 :
163 : public function setHeaders(array $headers)
164 : {
165 1 : $this->headers = $headers;
166 1 : }
167 :
168 :
169 :
170 :
171 :
172 :
173 :
174 :
175 : public function setStatus($status)
176 : {
177 3 : if (!array_key_exists($status, self::$statusMap)) {
178 1 : throw new HTTP_OAuth_Exception('Invalid status');
179 : }
180 :
181 2 : list($code, $text) = self::$statusMap[$status];
182 2 : $this->setBody($text);
183 :
184 2 : if ($this->headersSent()) {
185 1 : throw new HTTP_OAuth_Exception('Status already sent');
186 : }
187 :
188 : switch ($code) {
189 1 : case 400:
190 1 : $this->header('HTTP/1.1 400 Bad Request');
191 1 : break;
192 1 : case 401:
193 1 : $this->header('HTTP/1.1 401 Unauthorized');
194 1 : break;
195 : }
196 1 : }
197 :
198 :
199 :
200 :
201 :
202 :
203 :
204 : protected function headersSent()
205 : {
206 : return headers_sent();
207 : }
208 :
209 :
210 :
211 :
212 :
213 :
214 :
215 :
216 : protected function header($header)
217 : {
218 : return header($header);
219 : }
220 :
221 :
222 :
223 :
224 :
225 :
226 :
227 :
228 :
229 : protected function prepareBody()
230 : {
231 1 : if ($this->headersSent() && $this->getBody() !== '') {
232 1 : $this->err('Body already sent, not setting');
233 1 : } else {
234 1 : $this->setBody(HTTP_OAuth::buildHTTPQuery($this->getParameters()));
235 : }
236 1 : }
237 :
238 :
239 :
240 :
241 :
242 :
243 :
244 :
245 : public function setBody($body)
246 : {
247 3 : $this->body = $body;
248 3 : }
249 :
250 :
251 :
252 :
253 :
254 :
255 : public function getBody()
256 : {
257 2 : return $this->body;
258 : }
259 :
260 :
261 :
262 :
263 :
264 :
265 :
266 :
267 :
268 : public function send()
269 : {
270 1 : $this->prepareBody();
271 1 : if (!$this->headersSent()) {
272 1 : $this->header('HTTP/1.1 200 OK');
273 1 : foreach ($this->getHeaders() as $name => $value) {
274 :
275 : $this->header($name . ': ' . $value);
276 :
277 1 : }
278 1 : } else {
279 1 : $this->err('Headers already sent, can not send headers');
280 : }
281 :
282 1 : echo $this->getBody();
283 1 : }
284 :
285 : }
286 :
287 : ?>
|