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 : require_once 'HTTP/OAuth/Signature.php';
24 : require_once 'HTTP/OAuth/Provider/Exception/InvalidRequest.php';
25 :
26 :
27 :
28 :
29 :
30 :
31 :
32 :
33 :
34 :
35 :
36 :
37 :
38 :
39 :
40 :
41 : class HTTP_OAuth_Provider_Request extends HTTP_OAuth_Message
42 : {
43 :
44 :
45 :
46 :
47 :
48 :
49 : protected $headers = array();
50 :
51 :
52 :
53 :
54 :
55 :
56 : protected $method = '';
57 :
58 :
59 :
60 :
61 :
62 :
63 : public function __construct()
64 : {
65 14 : $this->setHeaders();
66 14 : $this->setParametersFromRequest();
67 13 : }
68 :
69 :
70 :
71 :
72 :
73 :
74 :
75 :
76 : public function setHeaders(array $headers = array())
77 : {
78 14 : if (count($headers)) {
79 4 : $this->headers = $headers;
80 14 : } else if (is_array($this->apacheRequestHeaders())) {
81 1 : $this->debug('Using apache_request_headers() to get request headers');
82 1 : $this->headers = $this->apacheRequestHeaders();
83 14 : } else if (is_array($this->peclHttpHeaders())) {
84 1 : $this->debug('Using pecl_http to get request headers');
85 1 : $this->headers = $this->peclHttpHeaders();
86 1 : } else {
87 13 : $this->debug('Using $_SERVER to get request headers');
88 13 : foreach ($_SERVER as $name => $value) {
89 13 : if (substr($name, 0, 5) == 'HTTP_') {
90 13 : $name = str_replace(
91 13 : ' ', '-',
92 13 : ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))
93 13 : );
94 13 : $this->headers[$name] = $value;
95 13 : }
96 13 : }
97 : }
98 14 : }
99 :
100 :
101 :
102 :
103 :
104 :
105 :
106 :
107 :
108 :
109 : protected function apacheRequestHeaders()
110 : {
111 1 : if (function_exists('apache_request_headers')) {
112 :
113 : return apache_request_headers();
114 :
115 : }
116 :
117 1 : return null;
118 : }
119 :
120 :
121 :
122 :
123 :
124 :
125 :
126 :
127 :
128 :
129 :
130 : protected function peclHttpHeaders()
131 : {
132 : if (extension_loaded('http') && class_exists('HttpMessage')) {
133 : $message = HttpMessage::fromEnv(HttpMessage::TYPE_REQUEST);
134 : return $message->getHeaders();
135 : }
136 :
137 : return null;
138 : }
139 :
140 :
141 :
142 :
143 :
144 :
145 :
146 : public function setParametersFromRequest()
147 : {
148 14 : $params = array();
149 14 : $auth = $this->getHeader('Authorization');
150 14 : if ($auth !== null) {
151 13 : $this->debug('Using OAuth data from header');
152 13 : $parts = explode(',', $auth);
153 13 : foreach ($parts as $part) {
154 13 : list($key, $value) = explode('=', trim($part));
155 13 : if (strstr(strtolower($key), 'oauth ')
156 13 : || strstr(strtolower($key), 'uth re')
157 13 : || substr(strtolower($key), 0, 6) != 'oauth_'
158 13 : ) {
159 13 : continue;
160 : }
161 :
162 13 : $value = trim($value);
163 13 : $value = str_replace('"', '', $value);
164 :
165 13 : $params[$key] = $value;
166 13 : }
167 13 : }
168 :
169 14 : if ($this->getRequestMethod() == 'POST') {
170 2 : $this->debug('getting data from POST');
171 2 : $contentType = substr($this->getHeader('Content-Type'), 0, 33);
172 2 : if ($contentType !== 'application/x-www-form-urlencoded') {
173 1 : throw new HTTP_OAuth_Provider_Exception_InvalidRequest('Invalid ' .
174 1 : 'content type for POST request');
175 : }
176 :
177 1 : $params = array_merge(
178 1 : $params,
179 1 : $this->parseQueryString($this->getPostData())
180 1 : );
181 1 : }
182 :
183 14 : $params = array_merge(
184 14 : $params,
185 14 : $this->parseQueryString($this->getQueryString())
186 14 : );
187 :
188 14 : if (empty($params)) {
189 1 : throw new HTTP_OAuth_Provider_Exception_InvalidRequest('No oauth ' .
190 1 : 'data found from request');
191 : }
192 :
193 13 : $this->setParameters(HTTP_OAuth::urldecode($params));
194 13 : }
195 :
196 :
197 :
198 :
199 :
200 :
201 :
202 :
203 :
204 : public function isValidSignature($consumerSecret, $tokenSecret = '')
205 : {
206 2 : if (!$this->oauth_signature_method) {
207 1 : throw new HTTP_OAuth_Provider_Exception_InvalidRequest(
208 : 'Missing oauth_signature_method in request'
209 1 : );
210 : }
211 :
212 1 : $sign = HTTP_OAuth_Signature::factory($this->oauth_signature_method);
213 1 : $check = $sign->build(
214 1 : $this->getRequestMethod(), $this->getUrl(),
215 1 : $this->getParameters(), $consumerSecret, $tokenSecret
216 1 : );
217 :
218 1 : if ($this->oauth_signature === $check) {
219 1 : $this->info('Valid signature');
220 1 : return true;
221 : }
222 :
223 1 : $this->err('Invalid signature');
224 1 : return false;
225 :
226 : }
227 :
228 :
229 :
230 :
231 :
232 :
233 : public function getQueryString()
234 : {
235 14 : if (!empty($_SERVER['QUERY_STRING'])) {
236 1 : return $_SERVER['QUERY_STRING'];
237 : }
238 :
239 14 : return null;
240 : }
241 :
242 :
243 :
244 :
245 :
246 :
247 : public function getRequestMethod()
248 : {
249 12 : if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
250 12 : return 'HEAD';
251 : }
252 :
253 1 : return $_SERVER['REQUEST_METHOD'];
254 : }
255 :
256 :
257 :
258 :
259 :
260 :
261 : public function getUrl()
262 : {
263 1 : $schema = 'http';
264 1 : if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
265 1 : $schema .= 's';
266 1 : }
267 :
268 1 : return $schema . '://' . $this->getHeader('Host')
269 1 : . $this->getRequestUri();
270 : }
271 :
272 :
273 :
274 :
275 :
276 :
277 :
278 :
279 :
280 : public function getRequestUri()
281 : {
282 3 : if (!array_key_exists('REQUEST_URI', $_SERVER)) {
283 2 : return null;
284 : }
285 :
286 2 : $uri = $_SERVER['REQUEST_URI'];
287 2 : $pos = stripos($uri, '://');
288 2 : if (!$pos) {
289 2 : return $uri;
290 : }
291 :
292 1 : return substr($uri, strpos($uri, '/', $pos + 3));
293 : }
294 :
295 :
296 :
297 :
298 :
299 :
300 :
301 :
302 : public function getHeader($header)
303 : {
304 14 : foreach ($this->headers as $name => $value) {
305 13 : if (strtolower($header) == strtolower($name)) {
306 13 : return $value;
307 : }
308 5 : }
309 :
310 4 : return null;
311 : }
312 :
313 :
314 :
315 :
316 :
317 :
318 :
319 : public function getHeaders()
320 : {
321 3 : return $this->headers;
322 : }
323 :
324 :
325 :
326 :
327 :
328 :
329 :
330 : protected function getPostData()
331 : {
332 : return file_get_contents('php://input');
333 : }
334 :
335 :
336 :
337 :
338 :
339 :
340 :
341 :
342 :
343 :
344 :
345 : protected function parseQueryString($string)
346 : {
347 14 : $data = array();
348 14 : if (empty($string)) {
349 14 : return $data;
350 : }
351 :
352 1 : foreach (explode('&', $string) as $part) {
353 1 : if (!strstr($part, '=')) {
354 1 : continue;
355 : }
356 :
357 1 : list($key, $value) = explode('=', $part);
358 1 : $data[$key] = self::urldecode($value);
359 1 : }
360 :
361 1 : return $data;
362 : }
363 :
364 : }
365 :
366 : ?>
|