1 : <?php
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10 :
11 :
12 :
13 :
14 :
15 :
16 :
17 :
18 :
19 :
20 :
21 :
22 :
23 :
24 : require_once 'HTTP/Request2.php';
25 : require_once 'HTTP/Request2/Observer/Log.php';
26 : require_once 'HTTP/OAuth/Message.php';
27 : require_once 'HTTP/OAuth/Consumer/Response.php';
28 : require_once 'HTTP/OAuth/Signature.php';
29 : require_once 'HTTP/OAuth/Exception.php';
30 :
31 :
32 :
33 :
34 :
35 :
36 :
37 :
38 :
39 :
40 :
41 :
42 :
43 :
44 :
45 : class HTTP_OAuth_Consumer_Request extends HTTP_OAuth_Message
46 : {
47 :
48 :
49 :
50 :
51 : const AUTH_HEADER = 1;
52 : const AUTH_POST = 2;
53 : const AUTH_GET = 3;
54 :
55 :
56 :
57 :
58 :
59 :
60 : protected $authType = self::AUTH_HEADER;
61 :
62 :
63 :
64 :
65 :
66 :
67 :
68 :
69 :
70 : protected $secrets = array('', '');
71 :
72 :
73 :
74 :
75 :
76 :
77 : protected $request = null;
78 :
79 :
80 :
81 :
82 :
83 :
84 :
85 :
86 :
87 :
88 :
89 : public function __construct($url = null, array $secrets = array())
90 : {
91 15 : if ($url !== null) {
92 10 : $this->setUrl($url);
93 10 : }
94 :
95 15 : if (count($secrets)) {
96 1 : $this->setSecrets($secrets);
97 1 : }
98 15 : }
99 :
100 :
101 :
102 :
103 :
104 :
105 :
106 :
107 :
108 : public function accept($object)
109 : {
110 13 : switch (get_class($object)) {
111 13 : case 'HTTP_Request2':
112 13 : $this->request = $object;
113 13 : foreach (self::$logs as $log) {
114 1 : $this->request->attach(new HTTP_Request2_Observer_Log($log));
115 13 : }
116 13 : break;
117 1 : default:
118 1 : if ($object instanceof Log) {
119 1 : HTTP_OAuth::attachLog($object);
120 1 : $this->getHTTPRequest2()->attach(
121 1 : new HTTP_Request2_Observer_Log($object)
122 1 : );
123 1 : }
124 1 : break;
125 13 : }
126 13 : }
127 :
128 :
129 :
130 :
131 :
132 :
133 :
134 : protected function getHTTPRequest2()
135 : {
136 13 : if (!$this->request instanceof HTTP_Request2) {
137 10 : $this->accept(new HTTP_Request2);
138 10 : }
139 13 : return $this->request;
140 : }
141 :
142 :
143 :
144 :
145 :
146 :
147 :
148 :
149 : public function setSecrets(array $secrets = array())
150 : {
151 7 : if (count($secrets) == 1) {
152 1 : $secrets[1] = '';
153 1 : }
154 :
155 7 : $this->secrets = $secrets;
156 7 : }
157 :
158 :
159 :
160 :
161 :
162 :
163 : public function getSecrets()
164 : {
165 2 : return $this->secrets;
166 : }
167 :
168 :
169 :
170 :
171 :
172 :
173 :
174 :
175 :
176 :
177 :
178 : public function setAuthType($type)
179 : {
180 : static $valid = array(self::AUTH_HEADER, self::AUTH_POST,
181 2 : self::AUTH_GET);
182 2 : if (!in_array($type, $valid)) {
183 1 : throw new InvalidArgumentException('Invalid Auth Type, see class ' .
184 1 : 'constants');
185 : }
186 :
187 1 : $this->authType = $type;
188 1 : }
189 :
190 :
191 :
192 :
193 :
194 :
195 : public function getAuthType()
196 : {
197 4 : return $this->authType;
198 : }
199 :
200 :
201 :
202 :
203 :
204 :
205 :
206 :
207 :
208 :
209 : public function send()
210 : {
211 3 : $this->buildRequest();
212 3 : $request = $this->getHTTPRequest2();
213 :
214 :
215 :
216 :
217 3 : $headers = $request->getHeaders();
218 3 : $contentType = isset($headers['content-type'])
219 3 : ? $headers['content-type'] : '';
220 3 : if ($this->getMethod() == 'POST'
221 3 : && $contentType == 'application/x-www-form-urlencoded') {
222 :
223 0 : $body = $this->getHTTPRequest2()->getBody();
224 0 : $body = str_replace('+', '%20', $body);
225 0 : $this->getHTTPRequest2()->setBody($body);
226 0 : }
227 :
228 : try {
229 3 : $response = $this->getHTTPRequest2()->send();
230 3 : } catch (Exception $e) {
231 0 : throw new HTTP_OAuth_Exception($e->getMessage(), $e->getCode());
232 : }
233 :
234 3 : return new HTTP_OAuth_Consumer_Response($response);
235 : }
236 :
237 :
238 :
239 :
240 :
241 :
242 :
243 :
244 : protected function buildRequest()
245 : {
246 3 : $method = $this->getSignatureMethod();
247 3 : $this->debug('signing request with: ' . $method);
248 3 : $sig = HTTP_OAuth_Signature::factory($this->getSignatureMethod());
249 :
250 3 : $this->oauth_timestamp = time();
251 3 : $this->oauth_nonce = md5(microtime(true) . rand(1, 999));
252 3 : $this->oauth_version = '1.0';
253 3 : $params = array_merge(
254 3 : $this->getParameters(),
255 3 : $this->getUrl()->getQueryVariables()
256 3 : );
257 3 : $this->oauth_signature = $sig->build(
258 3 : $this->getMethod(),
259 3 : $this->getUrl()->getURL(),
260 3 : $params,
261 3 : $this->secrets[0],
262 3 : $this->secrets[1]
263 3 : );
264 :
265 3 : $params = $this->getOAuthParameters();
266 3 : switch ($this->getAuthType()) {
267 3 : case self::AUTH_HEADER:
268 3 : $auth = $this->getAuthForHeader($params);
269 3 : $this->setHeader('Authorization', $auth);
270 3 : break;
271 0 : case self::AUTH_POST:
272 0 : foreach ($params as $name => $value) {
273 0 : $this->addPostParameter($name, $value);
274 0 : }
275 0 : break;
276 0 : case self::AUTH_GET:
277 0 : break;
278 3 : }
279 :
280 3 : switch ($this->getMethod()) {
281 3 : case 'POST':
282 0 : foreach ($this->getParameters() as $name => $value) {
283 0 : if (substr($name, 0, 6) == 'oauth_') {
284 0 : continue;
285 : }
286 :
287 0 : $this->addPostParameter($name, $value);
288 0 : }
289 0 : break;
290 3 : case 'GET':
291 3 : $url = $this->getUrl();
292 3 : foreach ($this->getParameters() as $name => $value) {
293 3 : if (substr($name, 0, 6) == 'oauth_') {
294 3 : continue;
295 : }
296 :
297 2 : $url->setQueryVariable($name, $value);
298 3 : }
299 :
300 3 : $this->setUrl($url);
301 3 : break;
302 0 : default:
303 0 : break;
304 3 : }
305 3 : }
306 :
307 :
308 :
309 :
310 :
311 :
312 :
313 :
314 :
315 :
316 :
317 : protected function getAuthForHeader(array $params)
318 : {
319 3 : $url = $this->getUrl();
320 3 : $realm = $url->getScheme() . '://' . $url->getHost() . '/';
321 3 : $header = 'OAuth realm="' . $realm . '"';
322 3 : foreach ($params as $name => $value) {
323 3 : $header .= ", " . HTTP_OAuth::urlencode($name) . '="' .
324 3 : HTTP_OAuth::urlencode($value) . '"';
325 3 : }
326 :
327 3 : return $header;
328 : }
329 :
330 :
331 :
332 :
333 :
334 :
335 :
336 :
337 :
338 :
339 :
340 :
341 :
342 : public function __call($method, $args)
343 : {
344 13 : $httpRequest2 = $this->getHTTPRequest2();
345 :
346 13 : if (is_callable(array($httpRequest2, $method))) {
347 13 : return call_user_func_array(
348 13 : array($httpRequest2, $method),
349 : $args
350 13 : );
351 : }
352 :
353 0 : throw new BadMethodCallException($method);
354 : }
355 :
356 : }
357 :
358 : ?>
|