1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slackbot001; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
|
7
|
|
|
class CP_TDauth |
8
|
|
|
{ |
9
|
|
|
protected $auth = ''; |
10
|
|
|
protected $expires = ''; |
11
|
|
|
protected $BEID = ''; |
12
|
|
|
protected $WebServicesKey = ''; |
13
|
|
|
protected $urlroot = ''; |
14
|
|
|
protected $appsroot = ''; |
15
|
|
|
protected $appid = ''; |
16
|
|
|
protected $authstring = ''; |
17
|
|
|
protected $authsig = ''; |
18
|
|
|
protected $header = ''; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$a = func_get_args(); |
23
|
|
|
$i = func_num_args(); |
24
|
|
|
if (method_exists($this, $f = '__construct'.$i)) { |
25
|
|
|
call_user_func_array([$this, $f], $a); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function __construct0() |
30
|
|
|
{ |
31
|
|
|
Log::info('CP_TDauth: Constructing empty self.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function __construct5($b, $w, $u, $i, $e) |
35
|
|
|
{ |
36
|
|
|
Log::info('CP_TDauth: Constructing self with new authorization.'); |
37
|
|
|
$this->setEnv($e, $u); |
38
|
|
|
$this->authorize($b, $w, $this->urlroot); |
39
|
|
|
$this->BEID = $b; |
40
|
|
|
$this->WebServicesKey = $w; |
41
|
|
|
$this->appid = $i; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function __construct6($b, $w, $u, $i, $e, $a) |
45
|
|
|
{ |
46
|
|
|
Log::info('CP_TDauth: Constructing self with existing authorization.'); |
47
|
|
|
$this->setEnv($e, $u); |
48
|
|
|
$this->BEID = $b; |
49
|
|
|
$this->WebServicesKey = $w; |
50
|
|
|
$this->appid = $i; |
51
|
|
|
|
52
|
|
|
$parts = explode('.', $a); |
53
|
|
|
if(count($parts) == 3) { |
54
|
|
|
list($JWTheader, $JWTpayload, $JWTsig) = $parts; |
55
|
|
|
$this->auth = $a; |
56
|
|
|
$this->expires = json_decode(base64_decode($JWTpayload))->exp; |
57
|
|
|
$this->authstring = 'Authorization: Bearer '.$this->auth; |
58
|
|
|
$this->header = $JWTheader; |
59
|
|
|
$this->authsig = $JWTsig; |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
Log::info('CP_TDauth: Invalid token.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function setEnv($e, $u) |
66
|
|
|
{ |
67
|
|
|
if ($e == 'prod') { |
68
|
|
|
Log::info('CP_TDauth: Setup for production.'); |
69
|
|
|
$this->urlroot = $u.'TDWebApi/api/'; |
70
|
|
|
$this->appsroot = $u.'TDNext/Apps/'; |
71
|
|
|
} elseif ($e == 'sandbox') { |
72
|
|
|
Log::info('CP_TDauth: Setup for sandbox.'); |
73
|
|
|
$this->urlroot = $u.'SBTDWebApi/api/'; |
74
|
|
|
$this->appsroot = $u.'SBTDNext/Apps/'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function authorize($b, $w, $u) |
79
|
|
|
{ |
80
|
|
|
Log::info('CP_TDauth: authorize method called.'); |
81
|
|
|
Log::info('CP_TDauth: authorize requesting at ['.$u.'auth/loginadmin].'); |
82
|
|
|
$ch = curl_init($u.'auth/loginadmin'); |
83
|
|
|
$payload = json_encode(['BEID' => $b, 'WebServicesKey' => $w]); |
84
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); |
85
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); |
86
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
87
|
|
|
$bearer = curl_exec($ch); |
88
|
|
|
curl_close($ch); |
89
|
|
|
if (!$bearer) { |
90
|
|
|
Log::info('CP_TDauth: Authorization failed.'); |
91
|
|
|
} else { |
92
|
|
|
Log::info('CP_TDauth: Authorization successful.'); |
93
|
|
|
list($JWTheader, $JWTpayload, $JWTsig) = explode('.', $bearer); |
94
|
|
|
$this->auth = $bearer; |
95
|
|
|
$this->expires = json_decode(base64_decode($JWTpayload))->exp; |
96
|
|
|
$this->authstring = 'Authorization: Bearer '.$this->auth; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function checkToken() |
101
|
|
|
{ |
102
|
|
|
Log::info('CP_TDauth: checkToken method called.'); |
103
|
|
|
if ($this->authstring) { |
104
|
|
|
if (($this->expires - time()) <= 10) { |
105
|
|
|
$this->authorize($this->BEID, $this->WebServicesKey, $this->urlroot); |
106
|
|
|
Log::info('CP_TDauth: Token was expired. Replaced with new token.'); |
107
|
|
|
} else { |
108
|
|
|
$remain = $this->expires - time(); |
109
|
|
|
Log::info('CP_TDauth: Token ok, time remaining: '.$remain); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} else { |
114
|
|
|
Log::info('CP_TDauth: No token.'); |
115
|
|
|
|
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function __toString() |
121
|
|
|
{ |
122
|
|
|
Log::info('CP_TDauth: __toString method called.'); |
123
|
|
|
$this->checkToken(); |
124
|
|
|
Log::info('CP_TDauth: returning JWT.'); |
125
|
|
|
|
126
|
|
|
return $this->auth; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getVersion() |
130
|
|
|
{ |
131
|
|
|
Log::info('CP_TDauth: getVersion method called.'); |
132
|
|
|
|
133
|
|
|
return '0.98b'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
class CP_TDinstance extends CP_TDauth |
138
|
|
|
{ |
139
|
|
|
private function connect($type, $point, $data) |
140
|
|
|
{ |
141
|
|
|
Log::info('CP_TDinstance: connect method called.'); |
142
|
|
|
$this->checkToken(); |
143
|
|
|
$ch = curl_init($this->urlroot.$point); |
144
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json', $this->authstring]); |
145
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
146
|
|
|
if ($type == 'post') { |
147
|
|
|
Log::info('CP_TDinstance: connect method POST.'); |
148
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
149
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
150
|
|
|
} else { |
151
|
|
|
Log::info('CP_TDinstance: connect method GET.'); |
152
|
|
|
} |
153
|
|
|
$result = curl_exec($ch); |
154
|
|
|
|
155
|
|
|
return json_decode($result, true); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function flagCheck($r) |
159
|
|
|
{ |
160
|
|
|
Log::info('CP_TDinstance: flagCheck method called.'); |
161
|
|
|
$check = substr($r, -3); |
162
|
|
|
if (substr($check, 1, 1) == '-') { |
163
|
|
|
$flag = substr($check, 1, 2); |
164
|
|
|
Log::info('CP_TDinstance: flagCheck given flag '.$flag); |
165
|
|
|
|
166
|
|
|
return $flag; |
167
|
|
|
} else { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function ticket($ticketno) |
173
|
|
|
{ |
174
|
|
|
Log::info('CP_TDinstance: ticket method called.'); |
175
|
|
|
$ticket = $this->connect('get', $this->appid.'/tickets/'.$ticketno, ''); |
176
|
|
|
|
177
|
|
|
return $ticket; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function searchTicketsName($search) |
181
|
|
|
{ |
182
|
|
|
Log::info('CP_TDinstance: searchTicketsName method called.'); |
183
|
|
|
$flag = $this->flagCheck($search); |
184
|
|
|
|
185
|
|
|
if (!$flag) { |
|
|
|
|
186
|
|
|
$people = $this->searchPeople($search); |
187
|
|
|
foreach ($people as $person) { |
188
|
|
|
$uids[] = $person['UID']; |
189
|
|
|
} |
190
|
|
|
if (isset($uids)) { |
191
|
|
|
$tickets = $this->searchResponsibility($uids); |
|
|
|
|
192
|
|
|
|
193
|
|
|
return $tickets; |
194
|
|
|
} else { |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
} else { |
198
|
|
|
if ($flag = '-r') { |
|
|
|
|
199
|
|
|
$data = ['RequestorNameSearch' => substr($search, 0, -3)]; |
200
|
|
|
$data_string = json_encode($data); |
201
|
|
|
$tickets = $this->connect('post', $this->appid.'/tickets/search', $data_string); |
202
|
|
|
|
203
|
|
|
return $tickets; |
204
|
|
|
} else { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
View Code Duplication |
public function searchPeople($search) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
Log::info('CP_TDinstance: searchPeople method called.'); |
213
|
|
|
$data = ['SearchText' => $search]; |
214
|
|
|
$data_string = json_encode($data); |
215
|
|
|
$people = $this->connect('post', 'people/search', $data_string); |
216
|
|
|
|
217
|
|
|
return $people; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
View Code Duplication |
public function searchAssets($search) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
Log::info('CP_TDinstance: searchAssets method called.'); |
223
|
|
|
$ticketids = [(int) $search]; |
224
|
|
|
$data = ['TicketIDs' => $ticketids]; |
225
|
|
|
$data_string = json_encode($data); |
226
|
|
|
$assets = $this->connect('post', 'assets/search', $data_string); |
227
|
|
|
|
228
|
|
|
return $assets; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function searchResponsibility($search) |
232
|
|
|
{ |
233
|
|
|
Log::info('CP_TDinstance: searchResponsibility method called.'); |
234
|
|
|
if (!is_array($search)) { |
235
|
|
|
$search = [(string) $search]; |
236
|
|
|
} |
237
|
|
|
$ResponsibilityUids = $search; |
238
|
|
|
$data = ['ResponsibilityUids' => $ResponsibilityUids]; |
239
|
|
|
$data_string = json_encode($data); |
240
|
|
|
$tickets = $this->connect('post', '/tickets/search', $data_string); |
241
|
|
|
|
242
|
|
|
return $tickets; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function rootAppsUrl() |
246
|
|
|
{ |
247
|
|
|
Log::info('CP_TDinstance: rootAppsUrl method called.'); |
248
|
|
|
|
249
|
|
|
return $this->appsroot.$this->appid.'/'; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: