forked from tripit/php_binding_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripit.php
520 lines (411 loc) · 16.5 KB
/
tripit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
// Copyright 2008-2012 Concur Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
assert(function_exists("curl_init"));
class WebAuthCredential {
var $_username;
var $_password;
function WebAuthCredential($username, $password) {
$this->_username = $username;
$this->_password = $password;
}
function getUsername() {
return $this->_username;
}
function getPassword() {
return $this->_password;
}
function authorize($curl, $http_method, $realm, $base_url, $args) {
curl_setopt($curl, CURLOPT_USERPWD, $this->_username . ":" . $this->_password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
}
class OAuthUtil {
public static function urlencodeRFC3986($string) {
return str_replace('+', ' ', str_replace('%7E', '~', rawurlencode($string)));
}
public static function generate_nonce() {
return md5(microtime() . mt_rand()); // md5s look nicer than numbers
}
public static function generate_timestamp() {
return time();
}
}
class OAuthConsumerCredential {
const OAUTH_SIGNATURE_METHOD = 'HMAC-SHA1';
const OAUTH_VERSION = '1.0';
var $_oauth_consumer_key;
var $_oauth_consumer_secret;
var $_oauth_token;
var $_oauth_token_secret;
var $_oauth_requestor_id;
function OAuthConsumerCredential($oauth_consumer_key, $oauth_consumer_secret, $oauth_token_or_requestor_id='', $oauth_token_secret='') {
$this->_oauth_consumer_key = $oauth_consumer_key;
$this->_oauth_consumer_secret = $oauth_consumer_secret;
$this->_oauth_token = $this->_oauth_token_secret = $this->_oauth_requestor_id = '';
if ($oauth_token_or_requestor_id && $oauth_token_secret) {
$this->_oauth_token = $oauth_token_or_requestor_id;
$this->_oauth_token_secret = $oauth_token_secret;
}
elseif ($oauth_token_or_requestor_id) {
$this->_oauth_requestor_id = $oauth_token_or_requestor_id;
}
}
function getOAuthConsumerKey() {
return $this->_oauth_consumer_key;
}
function getOAuthConsumerSecret() {
return $this->_oauth_consumer_secret;
}
function getOAuthToken() {
return $this->_oauth_token;
}
function getOAuthTokenSecret() {
return $this->_oauth_token_secret;
}
function getOAuthRequestorId() {
return $this->_oauth_requestor_id;
}
function authorize($curl, $http_method, $realm, $base_url, $args) {
$authorization_header = $this->_generate_authorization_header($http_method, $realm, $base_url, $args);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ' . $authorization_header));
}
function validate_signature($url) {
list($base_url, $query) = explode('?', $url, 2);
$params = array();
parse_str($query, $params);
$signature = $params['oauth_signature'];
return ($signature == $this->_generate_signature('GET', $base_url, $params));
}
function get_session_parameters($redirect_url, $action) {
$parameters = $this->_generate_oauth_parameters('GET', $action, array('redirect_url' => $redirect_url));
$parameters['redirect_url'] = $redirect_url;
$parameters['action'] = $action;
return json_encode($parameters);
}
function get_signable_parameters($params) {
// Remove oauth_signature if present
if (isset ($params['oauth_signature'])) {
unset ($params['oauth_signature']);
}
// Urlencode both keys and values
$keys = array_map(array (
'OAuthUtil',
'urlencodeRFC3986'
), array_keys($params));
$values = array_map(array (
'OAuthUtil',
'urlencodeRFC3986'
), array_values($params));
$params = array_combine($keys, $values);
// Sort by keys (natsort)
uksort($params, 'strnatcmp');
// Generate key=value pairs
$pairs = array ();
foreach ($params as $key => $value) {
if (is_array($value)) {
// If the value is an array, it's because there are multiple
// with the same key, sort them, then add all the pairs
natsort($value);
foreach ($value as $v2) {
$pairs[] = $key . '=' . $v2;
}
}
else {
$pairs[] = $key . '=' . $value;
}
}
// Return the pairs, concated with &
return implode('&', $pairs);
}
private function _generate_signature($method, $base_url, $params) {
$normalized_parameters = OAuthUtil :: urlencodeRFC3986($this->get_signable_parameters($params));
$normalized_http_url = OAuthUtil :: urlencodeRFC3986($base_url);
$base_string = $method . '&' . $normalized_http_url;
if ($normalized_parameters) {
$base_string .= '&' . $normalized_parameters;
}
$key_parts = array ( $this->_oauth_consumer_secret, $this->_oauth_token_secret );
$key_parts = array_map(array (
'OAuthUtil',
'urlencodeRFC3986'
), $key_parts);
$key = implode('&', $key_parts);
return base64_encode(hash_hmac('sha1', $base_string, $key, true));
}
private function _generate_oauth_parameters($http_method, $base_url, $args=null) {
$http_method = strtoupper($http_method);
$parameters = array( 'oauth_consumer_key' => $this->_oauth_consumer_key,
'oauth_nonce' => OAuthUtil :: generate_nonce(),
'oauth_timestamp' => OAuthUtil :: generate_timestamp(),
'oauth_signature_method' => self :: OAUTH_SIGNATURE_METHOD,
'oauth_version' => self :: OAUTH_VERSION);
if ($this->_oauth_token != '') {
$parameters['oauth_token'] = $this->_oauth_token;
}
if ($this->_oauth_requestor_id != '') {
$parameters['xoauth_requestor_id'] = $this->_oauth_requestor_id;
}
$parameters_for_base_string = $parameters;
if ($args) {
$parameters_for_base_string = array_merge($parameters, $args);
}
$parameters['oauth_signature'] = $this->_generate_signature($http_method, $base_url, $parameters_for_base_string);
return $parameters;
}
private function _generate_authorization_header($http_method, $realm, $base_url, $args) {
$authorization_header = 'OAuth realm="' . $realm . '",';
$params = array();
foreach (
$this->_generate_oauth_parameters($http_method, $base_url, $args)
as $k => $v)
{
if (substr($k, 0, 5) == 'oauth' || substr($k, 0, 6) == 'xoauth') {
$params[] = OAuthUtil :: urlencodeRFC3986($k) . '="' . OAuthUtil :: urlencodeRFC3986($v) . '"';
}
}
$authorization_header .= implode(',', $params);
return $authorization_header;
}
}
class TripIt {
var $_credential;
var $_api_version;
var $_api_url;
var $resource;
var $http_code;
var $response;
var $info;
function TripIt($credential, $api_url='https://api.tripit.com') {
$this->_credential = $credential;
$this->_api_version = 'v1';
$this->_api_url = $api_url;
$this->resource = null;
$this->http_code = null;
$this->response = null;
}
function _do_request($verb, $entity=null, $url_args=null, $post_args=null) {
if (in_array($verb, array('/oauth/request_token', '/oauth/access_token') )) {
$base_url = implode('/', array($this->_api_url, $verb));
} else {
if ($entity) {
$base_url = implode('/', array($this->_api_url, $this->_api_version, $verb, $entity));
} else {
$base_url = implode('/', array($this->_api_url, $this->_api_version, $verb));
}
}
$args = null;
if ($url_args) {
$args = $url_args;
$pairs = array();
foreach ($url_args as $name => $value) {
$pairs[] = urlencode($name) . '=' . urlencode($value);
}
$url = $base_url . '?' . join('&', $pairs);
} else {
$url = $base_url;
}
$this->resource = $url;
$curl = curl_init($this->_api_url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// In case you're running this against a server w/o
// properly signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if ($post_args) {
$args = $post_args;
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
$http_method = 'POST';
} else {
$http_method = 'GET';
}
$this->_credential->authorize($curl, $http_method, $this->_api_url, $base_url, $args);
if (FALSE === $this->response = curl_exec($curl)) {
throw new Exception(curl_error($curl));
}
$this->info = curl_getinfo($curl);
$this->http_code = $this->info['http_code'];
curl_close($curl);
return $this->response;
}
function _xml_to_php($xml) {
return new SimpleXMLElement($xml);
}
function _parse_command($func_name, $url_args = null, $post_args = null) {
$pieces = explode('_', $func_name, 2);
$verb = $pieces[0];
$entity = count($pieces) > 1 ? $pieces[1] : null;
$response = $this->_do_request($verb, $entity, $url_args, $post_args);
return $this->_xml_to_php($response);
}
function get_trip($id, $filter=array()) {
$filter['id'] = $id;
return $this->_parse_command(__FUNCTION__, $filter);
}
function get_air($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_lodging($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_car($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_rail($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_transport($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_cruise($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_restaurant($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_activity($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_note($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_map($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_directions($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function get_profile() {
return $this->_parse_command(__FUNCTION__);
}
function get_points_program($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_trip($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_air($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_lodging($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_car($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_rail($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_transport($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_cruise($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_restaurant($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_activity($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_note($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_map($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function delete_directions($id) {
return $this->_parse_command(__FUNCTION__, array( 'id' => $id ));
}
function replace_trip($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_air($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_lodging($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_car($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_rail($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_transport($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_cruise($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_restaurant($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_activity($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_note($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_map($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function replace_directions($id, $xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'xml' => $xml ));
}
function list_trip($filter=null) {
return $this->_parse_command(__FUNCTION__, $filter);
}
function list_object($filter=null) {
return $this->_parse_command(__FUNCTION__, $filter);
}
function list_points_program() {
return $this->_parse_command(__FUNCTION__);
}
function create($xml) {
return $this->_parse_command(__FUNCTION__, null, array( 'xml' => $xml));
}
function crs_load_reservations($xml, $company_key=null) {
$args = array('xml' => $xml);
if ($company_key !== null) {
$args['company_key'] = $company_key;
}
return $this->_parse_command('crsLoadReservations', null, $args);
}
function crs_delete_reservations($record_locator) {
return $this->_parse_command('crsDeleteReservations', array('record_locator' => $record_locator), null);
}
function get_request_token() {
$response = $this->_do_request('/oauth/request_token');
if ($this->http_code == 200) {
$request_token = array();
parse_str($response, $request_token);
return $request_token;
} else {
return $response;
}
}
function get_access_token() {
$response = $this->_do_request('/oauth/access_token');
if ($this->http_code == 200) {
$access_token = array();
parse_str($response, $access_token);
return $access_token;
} else {
return $response;
}
}
}