-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathKnownUser.php
554 lines (494 loc) · 22.4 KB
/
KnownUser.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
namespace QueueIT\KnownUserV3\SDK;
require_once('Models.php');
require_once('UserInQueueService.php');
require_once('UserInQueueStateCookieRepository.php');
require_once('IntegrationConfigHelpers.php');
require_once('QueueITHelpers.php');
class KnownUser
{
const QueueITAjaxHeaderKey = "x-queueit-ajaxpageurl";
//used for unittest
private static $userInQueueService = null;
private static function getUserInQueueService()
{
if (KnownUser::$userInQueueService == null) {
return new UserInQueueService(new UserInQueueStateCookieRepository(KnownUser::getHttpRequestProvider()->getCookieManager()));
}
return KnownUser::$userInQueueService;
}
public static function setHttpRequestProvider(IHttpRequestProvider $customHttpRequestProvider){
KnownUser::$httpRequestProvider = $customHttpRequestProvider;
}
//used for unittest
private static $httpRequestProvider = null;
private static function getHttpRequestProvider()
{
if (KnownUser::$httpRequestProvider == null) {
return new HttpRequestProvider();
}
return KnownUser::$httpRequestProvider;
}
private static $debugInfoArray = null;
public static function extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey)
{
if (empty($eventId)) {
throw new KnownUserException("eventId can not be null or empty.");
}
if (empty($secretKey)) {
throw new KnownUserException("secretKey can not be null or empty.");
}
if (!is_int($cookieValidityMinute) || intval($cookieValidityMinute) <= 0) {
throw new KnownUserException("cookieValidityMinute should be integer greater than 0.");
}
$userInQueueService = KnownUser::getUserInQueueService();
$userInQueueService->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey);
}
private static function _cancelRequestByLocalConfig(
$targetUrl,
$queueitToken,
CancelEventConfig $cancelConfig,
$customerId,
$secretKey,
$isDebug)
{
$targetUrl = KnownUser::generateTargetUrl($targetUrl);
if ($isDebug) {
$dic = array(
"TargetUrl" => $targetUrl,
"SdkVersion" => UserInQueueService::getSDKVersion(),
"RunTime" => KnownUser::getRuntime(),
"QueueitToken" => $queueitToken,
"CancelConfig" => $cancelConfig != null ? $cancelConfig->getString() : "NULL",
"OriginalUrl" => KnownUser::getHttpRequestProvider()->getAbsoluteUri()
);
KnownUser::logMoreRequestDetails($dic);
KnownUser::updateDebugCookieDetails($dic);
}
if (Utils::isNullOrEmptyString($targetUrl)) {
throw new KnownUserException("targetUrl can not be null or empty.");
}
if (Utils::isNullOrEmptyString($customerId)) {
throw new KnownUserException("customerId can not be null or empty.");
}
if (Utils::isNullOrEmptyString($secretKey)) {
throw new KnownUserException("secretKey can not be null or empty.");
}
if (Utils::isNullOrEmptyString($cancelConfig->eventId)) {
throw new KnownUserException("eventId from cancelConfig can not be null or empty.");
}
if (Utils::isNullOrEmptyString($cancelConfig->queueDomain)) {
throw new KnownUserException("queueDomain from cancelConfig can not be null or empty.");
}
$userInQueueService = KnownUser::getUserInQueueService();
$result = $userInQueueService->validateCancelRequest($targetUrl, $cancelConfig, $customerId, $secretKey);
$result->isAjaxResult = KnownUser::isQueueAjaxCall();
return $result;
}
public static function cancelRequestByLocalConfig(
$targetUrl,
$queueitToken,
CancelEventConfig $cancelConfig,
$customerId,
$secretKey
) {
$connectorDiagnostics = ConnectorDiagnostics::verify($customerId, $secretKey, $queueitToken);
if ($connectorDiagnostics->hasError) {
return $connectorDiagnostics->validationResult;
}
try {
$result = KnownUser::_cancelRequestByLocalConfig($targetUrl, $queueitToken, $cancelConfig, $customerId, $secretKey, $connectorDiagnostics->isEnabled);
KnownUser::sendDebugCookie();
return $result;
} catch (\Exception $e) {
if ($connectorDiagnostics->isEnabled) {
$dic = array("Exception" => $e->getMessage());
KnownUser::updateDebugCookieDetails($dic);
KnownUser::sendDebugCookie();
}
throw $e;
}
}
public static function validateRequestByIntegrationConfig($currentUrlWithoutQueueITToken, $queueitToken, $integrationsConfigString, $customerId, $secretKey)
{
$connectorDiagnostics = ConnectorDiagnostics::verify($customerId, $secretKey, $queueitToken);
if ($connectorDiagnostics->hasError) {
return $connectorDiagnostics->validationResult;
}
try {
if ($connectorDiagnostics->isEnabled) {
$dic = array(
"SdkVersion" => UserInQueueService::getSDKVersion(),
"RunTime" => KnownUser::getRuntime(),
"QueueitToken" => $queueitToken,
"PureUrl" => $currentUrlWithoutQueueITToken,
"OriginalUrl" => KnownUser::getHttpRequestProvider()->getAbsoluteUri()
);
KnownUser::logMoreRequestDetails($dic);
KnownUser::updateDebugCookieDetails($dic);
}
if (Utils::isNullOrEmptyString($currentUrlWithoutQueueITToken)) {
throw new KnownUserException("currentUrlWithoutQueueITToken can not be null or empty.");
}
if (Utils::isNullOrEmptyString($integrationsConfigString)) {
throw new KnownUserException("integrationsConfigString can not be null or empty.");
}
$integrationEvaluator = new IntegrationEvaluator();
$customerIntegration = json_decode($integrationsConfigString, true);
if ($connectorDiagnostics->isEnabled) {
if($customerIntegration == null)
{
$dic = array("ConfigVersion" => "NULL");
}
else
{
$dic = array("ConfigVersion" => $customerIntegration["Version"]);
}
KnownUser::updateDebugCookieDetails($dic);
}
if ($customerIntegration == null) {
throw new KnownUserException("integrationsConfigString is invalid.");
}
$matchedConfig = $integrationEvaluator->getMatchedIntegrationConfig($customerIntegration, $currentUrlWithoutQueueITToken, KnownUser::getHttpRequestProvider());
if ($connectorDiagnostics->isEnabled) {
$dic = array("MatchedConfig" => (($matchedConfig != null) ? $matchedConfig["Name"] : "NULL"));
KnownUser::updateDebugCookieDetails($dic);
}
$result = null;
if ($matchedConfig == null) {
$result = new RequestValidationResult(null, null, null, null, null, null);
} else if (
!array_key_exists("ActionType", $matchedConfig) ||
$matchedConfig["ActionType"] == ActionTypes::QueueAction
) {
$result = KnownUser::handleQueueAction(
$currentUrlWithoutQueueITToken,
$queueitToken,
$customerIntegration,
$customerId,
$secretKey,
$matchedConfig,
$connectorDiagnostics->isEnabled
);
} else if ($matchedConfig["ActionType"] == ActionTypes::CancelAction) {
$result = KnownUser::handleCancelAction(
$currentUrlWithoutQueueITToken,
$queueitToken,
$customerIntegration,
$customerId,
$secretKey,
$matchedConfig,
$connectorDiagnostics->isEnabled
);
} else {
$result = KnownUser::handleIgnoreAction($matchedConfig["Name"]);
}
KnownUser::sendDebugCookie();
return $result;
} catch (\Exception $e) {
if ($connectorDiagnostics->isEnabled) {
$dic = array("Exception" => $e->getMessage());
KnownUser::updateDebugCookieDetails($dic);
KnownUser::sendDebugCookie();
}
throw new KnownUserException($e->getMessage());
}
}
public static function resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey)
{
$connectorDiagnostics = ConnectorDiagnostics::verify($customerId, $secretKey, $queueitToken);
if ($connectorDiagnostics->hasError) {
return $connectorDiagnostics->validationResult;
}
try {
$targetUrl = KnownUser::generateTargetUrl($targetUrl);
$result = KnownUser::_resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey, $connectorDiagnostics->isEnabled);
KnownUser::sendDebugCookie();
return $result;
} catch (\Exception $e) {
if ($connectorDiagnostics->isEnabled) {
$dic = array("Exception" => $e->getMessage());
KnownUser::updateDebugCookieDetails($dic);
KnownUser::sendDebugCookie();
}
throw $e;
}
}
private static function _resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey, $isDebug)
{
if ($isDebug) {
$dic = array(
"TargetUrl" => $targetUrl,
"SdkVersion" => UserInQueueService::getSDKVersion(),
"RunTime" => KnownUser::getRuntime(),
"QueueitToken" => $queueitToken,
"QueueConfig" => $queueConfig != null ? $queueConfig->getString() : "NULL",
"OriginalUrl" => KnownUser::getHttpRequestProvider()->getAbsoluteUri()
);
KnownUser::logMoreRequestDetails($dic);
KnownUser::updateDebugCookieDetails($dic);
}
if ($queueConfig == null) {
throw new KnownUserException("eventConfig can not be null.");
}
if (Utils::isNullOrEmptyString($customerId)) {
throw new KnownUserException("customerId can not be null or empty.");
}
if (Utils::isNullOrEmptyString($secretKey)) {
throw new KnownUserException("secretKey can not be null or empty.");
}
if (Utils::isNullOrEmptyString($queueConfig->eventId)) {
throw new KnownUserException("eventId from queueConfig can not be null or empty.");
}
if (Utils::isNullOrEmptyString($queueConfig->queueDomain)) {
throw new KnownUserException("queueDomain from queueConfig can not be null or empty.");
}
if (!is_int($queueConfig->cookieValidityMinute) || intval($queueConfig->cookieValidityMinute) <= 0) {
throw new KnownUserException("cookieValidityMinute from queueConfig should be integer greater than 0.");
}
if (!is_bool($queueConfig->extendCookieValidity)) {
throw new KnownUserException("extendCookieValidity from queueConfig should be valid boolean.");
}
$userInQueueService = KnownUser::getUserInQueueService();
$result = $userInQueueService->validateQueueRequest($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey);
$result->isAjaxResult = KnownUser::isQueueAjaxCall();
return $result;
}
private static function handleQueueAction(
$currentUrlWithoutQueueITToken,
$queueitToken,
$customerIntegration,
$customerId,
$secretKey,
$matchedConfig,
$isDebug
) {
$eventConfig = new QueueEventConfig();
$targetUrl = "";
$eventConfig->eventId = $matchedConfig["EventId"];
$eventConfig->layoutName = $matchedConfig["LayoutName"];
$eventConfig->culture = $matchedConfig["Culture"];
$eventConfig->queueDomain = $matchedConfig["QueueDomain"];
$eventConfig->extendCookieValidity = $matchedConfig["ExtendCookieValidity"];
$eventConfig->cookieValidityMinute = $matchedConfig["CookieValidityMinute"];
$eventConfig->cookieDomain = $matchedConfig["CookieDomain"];
$eventConfig->isCookieHttpOnly = array_key_exists("IsCookieHttpOnly", $matchedConfig) ? $matchedConfig["IsCookieHttpOnly"] : false;
$eventConfig->isCookieSecure = array_key_exists("IsCookieSecure", $matchedConfig) ? $matchedConfig["IsCookieSecure"] : false;
$eventConfig->version = $customerIntegration["Version"];
$eventConfig->actionName = $matchedConfig["Name"];
switch ($matchedConfig["RedirectLogic"]) {
case "ForcedTargetUrl":
case "ForecedTargetUrl":
$targetUrl = $matchedConfig["ForcedTargetUrl"];
break;
case "EventTargetUrl":
$targetUrl = "";
break;
default:
$targetUrl = KnownUser::generateTargetUrl($currentUrlWithoutQueueITToken);
}
return KnownUser::_resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey, $isDebug);
}
private static function handleCancelAction(
$currentUrlWithoutQueueITToken,
$queueitToken,
$customerIntegration,
$customerId,
$secretKey,
$matchedConfig,
$isDebug
) {
$cancelEventConfig = new CancelEventConfig();
$cancelEventConfig->queueDomain = $matchedConfig["QueueDomain"];
$cancelEventConfig->eventId = $matchedConfig["EventId"];
$cancelEventConfig->version = $customerIntegration["Version"];
$cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"];
$cancelEventConfig->isCookieHttpOnly = array_key_exists("IsCookieHttpOnly", $matchedConfig) ? $matchedConfig["IsCookieHttpOnly"] : false;
$cancelEventConfig->isCookieSecure = array_key_exists("IsCookieSecure", $matchedConfig) ? $matchedConfig["IsCookieSecure"] : false;
$cancelEventConfig->actionName = $matchedConfig["Name"];
return KnownUser::_cancelRequestByLocalConfig($currentUrlWithoutQueueITToken, $queueitToken, $cancelEventConfig, $customerId, $secretKey, $isDebug);
}
private static function handleIgnoreAction($actionName)
{
$userInQueueService = KnownUser::getUserInQueueService();
$result = $userInQueueService->getIgnoreActionResult($actionName);
$result->isAjaxResult = KnownUser::isQueueAjaxCall();
return $result;
}
private static function logMoreRequestDetails(array &$debugInfos)
{
$allHeaders = KnownUser::getHttpRequestProvider()->getHeaderArray();
$debugInfos["ServerUtcTime"] = gmdate("Y-m-d\TH:i:s\Z");
$debugInfos["RequestIP"] = KnownUser::getHttpRequestProvider()->getUserHostAddress();
$debugInfos["RequestHttpHeader_Via"] = array_key_exists('via', $allHeaders) ? $allHeaders['via'] : "";
$debugInfos["RequestHttpHeader_Forwarded"] = array_key_exists('forwarded', $allHeaders) ? $allHeaders['forwarded'] : "";
$debugInfos["RequestHttpHeader_XForwardedFor"] = array_key_exists('x-forwarded-for', $allHeaders) ? $allHeaders['x-forwarded-for'] : "";
$debugInfos["RequestHttpHeader_XForwardedHost"] = array_key_exists('x-forwarded-host', $allHeaders) ? $allHeaders['x-forwarded-host'] : "";
$debugInfos["RequestHttpHeader_XForwardedProto"] = array_key_exists('x-forwarded-proto', $allHeaders) ? $allHeaders['x-forwarded-proto'] : "";
}
private static function updateDebugCookieDetails(array $debugInfos)
{
if (KnownUser::$debugInfoArray != null) {
foreach (KnownUser::$debugInfoArray as $key => $value) {
if (!array_key_exists($key, $debugInfos)) {
$debugInfos[$key] = $value;
}
}
}
KnownUser::$debugInfoArray = $debugInfos;
}
private static function sendDebugCookie()
{
if (KnownUser::$debugInfoArray != null) {
$cookieNameValues = array();
foreach (KnownUser::$debugInfoArray as $key => $value) {
array_push($cookieNameValues, $key . '=' . $value);
}
KnownUser::getHttpRequestProvider()->getCookieManager()->setCookie("queueitdebug", implode('|', $cookieNameValues), 0, null, false, false);
}
}
private static function generateTargetUrl($originalTargetUrl)
{
if (!KnownUser::isQueueAjaxCall()) {
return $originalTargetUrl;
}
$headers = KnownUser::getHttpRequestProvider()->getHeaderArray();
return urldecode($headers[KnownUser::QueueITAjaxHeaderKey]);
}
private static function isQueueAjaxCall()
{
return array_key_exists(
KnownUser::QueueITAjaxHeaderKey,
KnownUser::getHttpRequestProvider()->getHeaderArray()
);
}
private static function getRuntime()
{
return phpversion();
}
}
class CookieManager implements ICookieManager
{
public function getCookie($cookieName)
{
if (array_key_exists($cookieName, $_COOKIE)) {
return $_COOKIE[$cookieName];
} else {
return null;
}
}
public function setCookie($name, $value, $expire, $domain, $isHttpOnly, $isSecure)
{
if(is_null($value)){ $value = ""; }
if(is_null($expire)){ $expire = 0; }
if(is_null($domain)){ $domain = ""; }
if(is_null($isHttpOnly)){ $isHttpOnly = false; }
if(is_null($isSecure)){ $isSecure = false; }
setcookie($name, $value, $expire, "/", $domain, $isSecure, $isHttpOnly);
}
public function getCookieArray()
{
$arryCookie = array();
foreach ($_COOKIE as $key => $val) {
$arryCookie[$key] = $val;
}
return $arryCookie;
}
}
interface IHttpRequestProvider
{
function getUserAgent();
function getUserHostAddress();
function getCookieManager();
function getAbsoluteUri();
function getHeaderArray();
function getRequestBodyAsString();
}
class HttpRequestProvider implements IHttpRequestProvider
{
private $cookieManager;
private $allHeadersLowerCaseKeyArray;
function getUserAgent()
{
return array_key_exists('HTTP_USER_AGENT', $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : "";
}
function getUserHostAddress()
{
return array_key_exists('REMOTE_ADDR', $_SERVER) ? $_SERVER['REMOTE_ADDR'] : "";
}
function getCookieManager()
{
if ($this->cookieManager == null) {
$this->cookieManager = new CookieManager();
}
return $this->cookieManager;
}
function getAbsoluteUri()
{
// Get HTTP/HTTPS (the possible values for this vary from server to server)
$myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']), array('off', 'no'))) ? 'https' : 'http';
// Get domain portion
$myUrl .= '://' . $_SERVER['HTTP_HOST'];
// Get path to script
$myUrl .= $_SERVER['REQUEST_URI'];
// Add path info, if any
if (!empty($_SERVER['PATH_INFO']))
$myUrl .= $_SERVER['PATH_INFO'];
return $myUrl;
}
function getHeaderArray()
{
if ($this->allHeadersLowerCaseKeyArray == null) {
$tempArray = array();
foreach (getallheaders() as $key => $value) {
$tempArray[strtolower($key)] = $value;
}
$this->allHeadersLowerCaseKeyArray = $tempArray;
}
return $this->allHeadersLowerCaseKeyArray;
}
function getRequestBodyAsString()
{
return '';
}
}
//https://github.com/ralouphie/getallheaders/blob/master/src/getallheaders.php
//PHP getallheaders polyfill
if (!function_exists('getallheaders')) {
/**
* Get all HTTP header key/values as an associative array for the current request.
*
* @return string[string] The HTTP header key/value pairs.
*/
function getallheaders()
{
$headers = array();
$copy_server = array(
'CONTENT_TYPE' => 'Content-Type',
'CONTENT_LENGTH' => 'Content-Length',
'CONTENT_MD5' => 'Content-Md5',
);
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) === 'HTTP_') {
$key = substr($key, 5);
if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
$headers[$key] = $value;
}
} elseif (isset($copy_server[$key])) {
$headers[$copy_server[$key]] = $value;
}
}
if (!isset($headers['Authorization'])) {
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
$headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
} elseif (isset($_SERVER['PHP_AUTH_USER'])) {
$basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
$headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
} elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
$headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
}
}
return $headers;
}
}