diff --git a/tripit.php b/tripit.php index ac3a82e..7a1c5e1 100644 --- a/tripit.php +++ b/tripit.php @@ -17,23 +17,23 @@ assert(function_exists("curl_init")); class WebAuthCredential { - var $_username; - var $_password; + public $_username; + public $_password; - function WebAuthCredential($username, $password) { + public function __construct($username, $password) { $this->_username = $username; $this->_password = $password; } - - function getUsername() { + + public function getUsername() { return $this->_username; } - function getPassword() { + public function getPassword() { return $this->_password; } - - function authorize($curl, $http_method, $realm, $base_url, $args) { + + public 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); } @@ -58,71 +58,70 @@ 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; + public $_oauth_consumer_key; + public $_oauth_consumer_secret; + public $_oauth_token; + public $_oauth_token_secret; + public $_oauth_requestor_id; - function OAuthConsumerCredential($oauth_consumer_key, $oauth_consumer_secret, $oauth_token_or_requestor_id='', $oauth_token_secret='') { + public function __construct($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) { + } elseif ($oauth_token_or_requestor_id) { $this->_oauth_requestor_id = $oauth_token_or_requestor_id; } } - function getOAuthConsumerKey() { + public function getOAuthConsumerKey() { return $this->_oauth_consumer_key; } - function getOAuthConsumerSecret() { + public function getOAuthConsumerSecret() { return $this->_oauth_consumer_secret; } - function getOAuthToken() { + public function getOAuthToken() { return $this->_oauth_token; } - function getOAuthTokenSecret() { + public function getOAuthTokenSecret() { return $this->_oauth_token_secret; } - - function getOAuthRequestorId() { + + public function getOAuthRequestorId() { return $this->_oauth_requestor_id; } - - function authorize($curl, $http_method, $realm, $base_url, $args) { + + public 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) { + + public 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) { + + public 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) { + public function get_signable_parameters($params) { // Remove oauth_signature if present - if (isset ($params['oauth_signature'])) { - unset ($params['oauth_signature']); + if (isset($params['oauth_signature'])) { + unset($params['oauth_signature']); } // Urlencode both keys and values @@ -143,14 +142,13 @@ function get_signable_parameters($params) { $pairs = array (); foreach ($params as $key => $value) { if (is_array($value)) { - // If the value is an array, it's because there are multiple + // 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 { + } else { $pairs[] = $key . '=' . $value; } } @@ -158,17 +156,17 @@ function get_signable_parameters($params) { // 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 ( @@ -176,23 +174,23 @@ private function _generate_signature($method, $base_url, $params) { '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) { + 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); + '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; } @@ -203,10 +201,10 @@ private function _generate_oauth_parameters($http_method, $base_url, $args=null) } $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 . '",'; @@ -216,7 +214,7 @@ private function _generate_authorization_header($http_method, $realm, $base_url, as $k => $v) { if (substr($k, 0, 5) == 'oauth' || substr($k, 0, 6) == 'xoauth') { - $params[] = OAuthUtil :: urlencodeRFC3986($k) . '="' . OAuthUtil :: urlencodeRFC3986($v) . '"'; + $params[] = OAuthUtil::urlencodeRFC3986($k) . '="' . OAuthUtil::urlencodeRFC3986($v) . '"'; } } $authorization_header .= implode(',', $params); @@ -226,16 +224,16 @@ private function _generate_authorization_header($http_method, $realm, $base_url, } class TripIt { - var $_credential; - var $_api_version; - var $_api_url; + public $_credential; + public $_api_version; + public $_api_url; - var $resource; - var $http_code; - var $response; - var $info; + public $resource; + public $http_code; + public $response; + public $info; - function TripIt($credential, $api_url='https://api.tripit.com') { + public function __construct($credential, $api_url = 'https://api.tripit.com') { $this->_credential = $credential; $this->_api_version = 'v1'; $this->_api_url = $api_url; @@ -245,8 +243,8 @@ function TripIt($credential, $api_url='https://api.tripit.com') { $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') )) { + private 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 = $this->_api_url . $verb; } else { if ($entity) { @@ -287,25 +285,25 @@ function _do_request($verb, $entity=null, $url_args=null, $post_args=null) { } else { $http_method = 'GET'; } - + $this->_credential->authorize($curl, $http_method, $this->_api_url, $base_url, $args); - if (FALSE === $this->response = curl_exec($curl)) { + 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) { + private function _xml_to_php($xml) { return new SimpleXMLElement($xml); } - function _parse_command($func_name, $url_args = null, $post_args = null) { + private 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; @@ -314,224 +312,223 @@ function _parse_command($func_name, $url_args = null, $post_args = null) { $format = 'xml'; if (isset($url_args) && array_key_exists('format', $url_args)) { $format = $url_args['format']; - } - else if (isset($post_args) && array_key_exists('format', $post_args)) { + } elseif (isset($post_args) && array_key_exists('format', $post_args)) { $format = $post_args['format']; } if (strtolower($format) == 'json') { return json_decode($response, true); } - return $this->_xml_to_php($response); + return $this->_xml_to_php($response); } - function get_trip($id, $filter = array()) { + public function get_trip($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_air($id, $filter = array()) { + public function get_air($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_lodging($id, $filter = array()) { + public function get_lodging($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_car($id, $filter = array()) { + public function get_car($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_rail($id, $filter = array()) { + public function get_rail($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_transport($id, $filter = array()) { + public function get_transport($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_cruise($id, $filter = array()) { + public function get_cruise($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_restaurant($id, $filter = array()) { + public function get_restaurant($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_activity($id, $filter = array()) { + public function get_activity($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_note($id, $filter = array()) { + public function get_note($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_map($id, $filter = array()) { + public function get_map($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_directions($id, $filter = array()) { + public function get_directions($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function get_profile($filter = null) { + public function get_profile($filter = null) { return $this->_parse_command(__FUNCTION__, $filter); } - - function get_points_program($id, $filter = array()) { + + public function get_points_program($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_trip($id, $filter = array()) { + public function delete_trip($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_air($id, $filter = array()) { + public function delete_air($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_lodging($id, $filter = array()) { + public function delete_lodging($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_car($id, $filter = array()) { + public function delete_car($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_rail($id, $filter = array()) { + public function delete_rail($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_transport($id, $filter = array()) { + public function delete_transport($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_cruise($id, $filter = array()) { + public function delete_cruise($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_restaurant($id, $filter = array()) { + public function delete_restaurant($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_activity($id, $filter = array()) { + public function delete_activity($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_note($id, $filter = array()) { + public function delete_note($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_map($id, $filter = array()) { + public function delete_map($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - function delete_directions($id, $filter = array()) { + public function delete_directions($id, $filter = array()) { $filter['id'] = $id; return $this->_parse_command(__FUNCTION__, $filter); } - - function replace_trip($id, $data, $format = 'xml') { + + public function replace_trip($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_air($id, $data, $format = 'xml') { + public function replace_air($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_lodging($id, $data, $format = 'xml') { + public function replace_lodging($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_car($id, $data, $format = 'xml') { + public function replace_car($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_rail($id, $data, $format = 'xml') { + public function replace_rail($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_transport($id, $data, $format = 'xml') { + public function replace_transport($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_cruise($id, $data, $format = 'xml') { + public function replace_cruise($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_restaurant($id, $data, $format = 'xml') { + public function replace_restaurant($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_activity($id, $data, $format = 'xml') { + public function replace_activity($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_note($id, $data, $format = 'xml') { + public function replace_note($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_map($id, $data, $format = 'xml') { + public function replace_map($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function replace_directions($id, $data, $format = 'xml') { + public function replace_directions($id, $data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'id' => $id, 'format' => $format, $format => $data )); } - function list_trip($filter = null) { + public function list_trip($filter = null) { return $this->_parse_command(__FUNCTION__, $filter); } - function list_object($filter = null) { + public function list_object($filter = null) { return $this->_parse_command(__FUNCTION__, $filter); } - - function list_points_program($filter = null) { + + public function list_points_program($filter = null) { return $this->_parse_command(__FUNCTION__, $filter); } - function create($data, $format = 'xml') { + public function create($data, $format = 'xml') { return $this->_parse_command(__FUNCTION__, null, array( 'format' => $format, $format => $data )); } - - function crs_load_reservations($data, $company_key=null, $format = 'xml') { + + public function crs_load_reservations($data, $company_key = null, $format = 'xml') { $args = array('format' => $format, $format => $data); if ($company_key !== null) { $args['company_key'] = $company_key; } - + return $this->_parse_command('crsLoadReservations', null, $args); } - - function crs_delete_reservations($record_locator, $filter = array()) { + + public function crs_delete_reservations($record_locator, $filter = array()) { $filter['record_locator'] = $record_locator; return $this->_parse_command('crsDeleteReservations', $filter, null); } - function get_request_token() { + public function get_request_token() { $response = $this->_do_request('/oauth/request_token'); if ($this->http_code == 200) { $request_token = array(); @@ -542,7 +539,7 @@ function get_request_token() { } } - function get_access_token() { + public function get_access_token() { $response = $this->_do_request('/oauth/access_token'); if ($this->http_code == 200) { $access_token = array();