forked from queueit/KnownUser.V3.PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInQueueService.php
188 lines (160 loc) · 7.6 KB
/
UserInQueueService.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
<?php
namespace QueueIT\KnownUserV3\SDK;
require_once('Models.php');
require_once('UserInQueueStateCookieRepository.php');
require_once('QueueITHelpers.php');
interface IUserInQueueService
{
public function validateQueueRequest(
$currentPageUrl,
$queueitToken,
QueueEventConfig $config,
$customerId,
$secretKey);
public function validateCancelRequest($targetUrl,
CancelEventConfig $cancelConfig,
$customerId,
$secretKey);
public function extendQueueCookie(
$eventId,
$cookieValidityMinute,
$cookieDomain,
$secretKey);
public function getIgnoreActionResult();
}
class UserInQueueService implements IUserInQueueService
{
const SDK_VERSION = "3.4.0";
private $userInQueueStateRepository;
function __construct(IUserInQueueStateRepository $userInQueueStateRepository) {
$this->userInQueueStateRepository = $userInQueueStateRepository;
}
public function validateQueueRequest(
$targetUrl,
$queueitToken,
QueueEventConfig $config,
$customerId,
$secretKey) {
$state = $this->userInQueueStateRepository->getState($config->eventId, $secretKey);
if ($state->isValid) {
if ($state->isStateExtendable && $config->extendCookieValidity) {
$this->userInQueueStateRepository->store(
$config->eventId,
$state->queueId,
true,
$config->cookieValidityMinute,
!Utils::isNullOrEmptyString($config->cookieDomain) ? $config->cookieDomain : '',
$secretKey);
}
$result = new RequestValidationResult(ActionTypes::QueueAction,$config->eventId,$state->queueId, NULL);
return $result;
}
if(!Utils::isNullOrEmptyString($queueitToken)) {
$queueParams = QueueUrlParams::extractQueueParams($queueitToken);
return $this->getQueueITTokenValidationResult($customerId, $targetUrl, $config->eventId, $secretKey, $config, $queueParams);
} else {
return $this->getInQueueRedirectResult($customerId, $targetUrl, $config);
}
}
private function getQueueITTokenValidationResult(
$customerId,
$targetUrl,
$eventId,
$secretKey,
QueueEventConfig $config,
QueueUrlParams $queueParams) {
$calculatedHash = hash_hmac('sha256', $queueParams->queueITTokenWithoutHash, $secretKey);
if (strtoupper($calculatedHash) != strtoupper($queueParams->hashCode)) {
return $this->getVaidationErrorResult($customerId, $targetUrl, $config, $queueParams, "hash");
}
if (strtoupper($queueParams->eventId) != strtoupper($eventId)) {
return $this->getVaidationErrorResult($customerId, $targetUrl, $config, $queueParams, "eventid");
}
if ($queueParams->timeStamp < time()) {
return $this->getVaidationErrorResult($customerId, $targetUrl, $config, $queueParams, "timestamp");
}
$this->userInQueueStateRepository->store(
$config->eventId,
$queueParams->queueId,
$queueParams->extendableCookie,
!is_null( $queueParams->cookieValidityMinute) ? $queueParams->cookieValidityMinute : $config->cookieValidityMinute,
!Utils::isNullOrEmptyString($config->cookieDomain) ? $config->cookieDomain : '',
$secretKey);
$result = new RequestValidationResult(ActionTypes::QueueAction,$config->eventId, $queueParams->queueId, NULL);
return $result;
}
private function getVaidationErrorResult(
$customerId,
$targetUrl,
QueueEventConfig $config,
QueueUrlParams $qParams,
$errorCode) {
$query =$this->getQueryString($customerId, $config->eventId,$config->version,$config->culture,$config->layoutName)
."&queueittoken=" .$qParams->queueITToken
."&ts=" . time()
.(!Utils::isNullOrEmptyString($targetUrl) ? ("&t=". urlencode( $targetUrl)) : "");
$domainAlias = $config->queueDomain;
if (substr($domainAlias, -1) !== "/") {
$domainAlias = $domainAlias . "/";
}
$redirectUrl = "https://". $domainAlias. "error/". $errorCode. "/?" .$query;
$result = new RequestValidationResult(ActionTypes::QueueAction,$config->eventId, null, $redirectUrl);
return $result;
}
private function getInQueueRedirectResult($customerId, $targetUrl, QueueEventConfig $config) {
$redirectUrl = "https://". $config->queueDomain ."/?"
.$this->getQueryString($customerId, $config->eventId,$config->version,$config->culture,$config->layoutName)
.(!Utils::isNullOrEmptyString($targetUrl) ? "&t=". urlencode( $targetUrl) : "");
$result = new RequestValidationResult(ActionTypes::QueueAction,$config->eventId, null, $redirectUrl);
return $result;
}
private function getQueryString($customerId,
$eventId,
$configVersion,
$culture,
$layoutName) {
$queryStringList = array();
array_push($queryStringList,"c=".urlencode($customerId));
array_push($queryStringList,"e=".urlencode($eventId));
array_push($queryStringList,"ver=v3-php-".UserInQueueService::SDK_VERSION);
array_push($queryStringList,"cver=". (!is_null($configVersion)?$configVersion:'-1'));
if (!Utils::isNullOrEmptyString($culture)) {
array_push($queryStringList, "cid=" . urlencode($culture));
}
if (!Utils::isNullOrEmptyString($layoutName)) {
array_push($queryStringList, "l=" . urlencode($layoutName));
}
return implode("&", $queryStringList);
}
public function extendQueueCookie(
$eventId,
$cookieValidityMinute,
$cookieDomain,
$secretKey) {
$this->userInQueueStateRepository->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $secretKey);
}
public function validateCancelRequest($targetUrl,CancelEventConfig $cancelConfig,$customerId,$secretKey)
{
$state = $this->userInQueueStateRepository->getState($cancelConfig->eventId, $secretKey);
if ($state->isValid)
{
$this->userInQueueStateRepository->cancelQueueCookie($cancelConfig->eventId, $cancelConfig->cookieDomain);
$query = $this->getQueryString($customerId, $cancelConfig->eventId, $cancelConfig->version,NULL,NULL)
.(!Utils::isNullOrEmptyString($targetUrl) ? ("&r=". urlencode($targetUrl)) : "");
$domainAlias = $cancelConfig->queueDomain;
if (substr($domainAlias, -1) !== "/") {
$domainAlias = $domainAlias . "/";
}
$redirectUrl = "https://" . $domainAlias . "cancel/" . $customerId . "/" . $cancelConfig->eventId . "/?" . $query;
return new RequestValidationResult(ActionTypes::CancelAction,$cancelConfig->eventId, $state->queueId, $redirectUrl );
}
else
{
return new RequestValidationResult(ActionTypes::CancelAction,$cancelConfig->eventId,NULL,NULL);
}
}
public function getIgnoreActionResult()
{
return new RequestValidationResult(ActionTypes::IgnoreAction, NULL,NULL,NULL);
}
}