-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseServiceClient.php
More file actions
184 lines (160 loc) · 6.26 KB
/
BaseServiceClient.php
File metadata and controls
184 lines (160 loc) · 6.26 KB
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
<?php
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'ServiceResult.php');
abstract class BaseServiceClient {
# ----------------------------------------------
private $opa_get_parameters = array();
private $opa_request_body = array();
private $ops_request_method = '';
private $ops_service_url = '';
private $ops_table = '';
# ----------------------------------------------
private $ops_auth_url = '';
private $ops_auth_token = '';
private $ops_user = null;
private $ops_key = null;
private $ops_token_storage_path = '';
# ----------------------------------------------
private $ops_lang = null;
# ----------------------------------------------
public function __construct($ps_base_url, $ps_service) {
$this->ops_service_url = $ps_base_url."/service.php/".$ps_service;
$this->ops_auth_url = $ps_base_url."/service.php/auth/login";
// try to get user and password/key from environment
if(defined('__CA_SERVICE_API_USER__') && defined('__CA_SERVICE_API_KEY__')) {
$this->ops_user = __CA_SERVICE_API_USER__;
$this->ops_key = __CA_SERVICE_API_KEY__;
}
if(!$this->ops_user || !$this->ops_key) {
$this->ops_user = getenv('CA_SERVICE_API_USER');
$this->ops_key = getenv('CA_SERVICE_API_KEY');
}
// this is where we store the token for later reuse
$this->ops_token_storage_path =
sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'ca-service-wrapper-token'.preg_replace("/[^A-Za-z0-9\-\_]/", '', $ps_base_url).'.txt';
// try to recover token from file
$vs_potential_token = @file_get_contents($this->ops_token_storage_path);
if($vs_potential_token && (strlen($vs_potential_token) == 64) && preg_match("/^[a-f0-9]+$/", $vs_potential_token)) {
$this->ops_auth_token = $vs_potential_token;
}
}
# ----------------------------------------------
public function setRequestMethod($ps_method) {
if(!in_array($ps_method,array("GET","PUT","DELETE","OPTIONS","POST"))) {
return false;
}
$this->ops_request_method = $ps_method;
}
# ----------------------------------------------
public function getRequestMethod() {
return $this->ops_request_method;
}
# ----------------------------------------------
public function setRequestBody($pa_request_body) {
$this->opa_request_body = $pa_request_body;
}
# ----------------------------------------------
public function getRequestBody() {
return $this->opa_request_body;
}
# ----------------------------------------------
public function setTable($ps_table) {
$this->ops_table = $ps_table;
}
# ----------------------------------------------
public function getTable() {
return $this->ops_table;
}
# ----------------------------------------------
public function addGetParameter($ps_param_name,$ps_value) {
$this->opa_get_parameters[$ps_param_name] = $ps_value;
}
# ----------------------------------------------
public function getAllGetParameters() {
return $this->opa_get_parameters;
}
# ----------------------------------------------
public function getGetParameter($ps_param_name) {
return $this->opa_get_parameters[$ps_param_name];
}
# ----------------------------------------------
public function setLang($ps_lang) {
$this->ops_lang = $ps_lang;
$this->addGetParameter("lang",$ps_lang);
}
# ----------------------------------------------
public function getLang() {
return $this->ops_lang;
}
# ----------------------------------------------
public function setCredentials($ps_user, $ps_pass) {
$this->ops_user = $ps_user;
$this->ops_key = $ps_pass;
}
# ----------------------------------------------
public function request() {
if(!($vs_method = $this->getRequestMethod())) {
return false;
}
$va_get = array();
foreach($this->getAllGetParameters() as $vs_name => $vs_val) {
$va_get[] = $vs_name."=".urlencode($vs_val);
}
if(strlen($this->ops_auth_token) > 0) {
$va_get[] = 'authToken='.$this->ops_auth_token;
}
$vs_get = sizeof($va_get)>0 ? "?".join("&",$va_get) : "";
$vs_query_url = preg_replace("/\/$/", '', $this->ops_service_url."/".$this->getTable().$vs_get);
$vo_handle = curl_init($vs_query_url);
curl_setopt($vo_handle, CURLOPT_CUSTOMREQUEST, $vs_method);
curl_setopt($vo_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($vo_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($vo_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($vo_handle, CURLOPT_FOLLOWLOCATION, true);
$va_body = $this->getRequestBody();
if(is_array($va_body) && sizeof($va_body)>0) {
curl_setopt($vo_handle, CURLOPT_POSTFIELDS, json_encode($va_body));
}
$vs_exec = curl_exec($vo_handle);
$vn_code = curl_getinfo($vo_handle, CURLINFO_HTTP_CODE);
curl_close($vo_handle);
if($vn_code == 401) { // try to re-authenticate if access denied
// discard previous token
if(strlen($this->ops_auth_token)) {
@file_put_contents($this->ops_token_storage_path, '');
}
if(!$this->authenticate()) { // make up json result for failed authentication
return new ServiceResult('{ "ok": false, "errors": ["access denied"] }');
} else { // auth successful, try again
return $this->request();
}
}
return new ServiceResult($vs_exec);
}
# ----------------------------------------------
protected function authenticate() {
$vo_handle = curl_init($this->ops_auth_url);
curl_setopt($vo_handle, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($vo_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($vo_handle, CURLOPT_TIMEOUT, 3);
curl_setopt($vo_handle, CURLOPT_RETURNTRANSFER,1);
curl_setopt($vo_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($vo_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($vo_handle, CURLOPT_FOLLOWLOCATION, true);
// basic auth
curl_setopt($vo_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($vo_handle, CURLOPT_USERPWD, $this->ops_user.':'.$this->ops_key);
$vs_exec = curl_exec($vo_handle);
curl_close($vo_handle);
$va_ret = json_decode($vs_exec, true);
if(!is_array($va_ret) || !isset($va_ret['authToken']) || (strlen($va_ret['authToken']) != 64)) { return false; }
$this->ops_auth_token = $va_ret['authToken'];
// dump token into a file so we can find it later
@file_put_contents($this->ops_token_storage_path, $this->ops_auth_token);
return true;
}
# ----------------------------------------------
public function getAuthToken() {
return $this->ops_auth_token;
}
# ----------------------------------------------
}