55require_once ('UserInQueueService.php ' );
66require_once ('UserInQueueStateCookieRepository.php ' );
77require_once ('IntegrationConfigHelpers.php ' );
8+ require_once ('QueueITHelpers.php ' );
9+
10+
811
912class KnownUser
1013{
1114 //used for unittest
1215 private static $ userInQueueService = NULL ;
13-
1416 private static function getUserInQueueService () {
1517 if (KnownUser::$ userInQueueService == NULL )
1618 {
17- return new UserInQueueService (new UserInQueueStateCookieRepository (new CookieManager ()));
19+ return new UserInQueueService (new UserInQueueStateCookieRepository (KnownUser:: getHttpRequestProvider ()-> getCookieManager ()));
1820 }
1921 return KnownUser::$ userInQueueService ;
2022 }
2123
2224 //used for unittest
23- private static $ httpRequestProvider = null ;
25+ private static $ httpRequestProvider = NULL ;
2426 private static function getHttpRequestProvider () {
2527 if (KnownUser::$ httpRequestProvider == NULL )
2628 {
2729 return new HttpRequestProvider ();
2830 }
2931 return KnownUser::$ httpRequestProvider ;
3032 }
31-
32- public static function cancelQueueCookie ($ eventId , $ cookieDomain ) {
33- if (empty ($ eventId )) {
34- throw new KnownUserException ("eventId can not be null or empty. " );
35- }
36-
37- $ userInQueueService = KnownUser::getUserInQueueService ();
38- $ userInQueueService ->cancelQueueCookie ($ eventId , $ cookieDomain );
39- }
40-
33+ private static $ debugInfoArray =NULL ;
4134 public static function extendQueueCookie ($ eventId , $ cookieValidityMinute , $ cookieDomain , $ secretKey ) {
4235 if (empty ($ eventId )) {
4336 throw new KnownUserException ("eventId can not be null or empty. " );
@@ -52,90 +45,208 @@ public static function extendQueueCookie($eventId, $cookieValidityMinute, $cooki
5245 $ userInQueueService ->extendQueueCookie ($ eventId , $ cookieValidityMinute , $ cookieDomain , $ secretKey );
5346 }
5447
55- public static function validateRequestByLocalEventConfig ($ targetUrl , $ queueitToken , EventConfig $ eventConfig , $ customerId , $ secretKey ) {
56- if (empty ($ customerId )) {
48+ public static function resolveRequestByLocalEventConfig ($ targetUrl , $ queueitToken , QueueEventConfig $ queueConfig , $ customerId , $ secretKey ) {
49+ if (KnownUser::getIsDebug ($ queueitToken , $ secretKey ))
50+ {
51+ $ dic = array (
52+ "targetUrl " => $ targetUrl ,
53+ "queueitToken " => $ queueitToken ,
54+ "queueConfig " =>$ queueConfig != null ? $ queueConfig ->getString () : "NULL " ,
55+ "OriginalURL " => KnownUser::getHttpRequestProvider ()->getAbsoluteUri ());
56+ KnownUser::doCookieLog ($ dic );
57+ }
58+ if (Utils::isNullOrEmptyString ($ customerId )) {
5759 throw new KnownUserException ("customerId can not be null or empty. " );
5860 }
5961
60- if (empty ($ secretKey )) {
62+ if (Utils:: isNullOrEmptyString ($ secretKey )) {
6163 throw new KnownUserException ("secretKey can not be null or empty. " );
6264 }
6365
64- if (empty ( $ eventConfig ->eventId )) {
65- throw new KnownUserException ("eventId can not be null or empty. " );
66+ if (Utils:: isNullOrEmptyString ( $ queueConfig ->eventId )) {
67+ throw new KnownUserException ("eventId from queueConfig can not be null or empty. " );
6668 }
6769
68- if (empty ( $ eventConfig ->queueDomain )) {
69- throw new KnownUserException ("queueDomain can not be null or empty. " );
70+ if (Utils:: isNullOrEmptyString ( $ queueConfig ->queueDomain )) {
71+ throw new KnownUserException ("queueDomain from queueConfig can not be null or empty. " );
7072 }
7173
72- if (!is_int ($ eventConfig ->cookieValidityMinute ) || intval ($ eventConfig ->cookieValidityMinute ) <= 0 ) {
73- throw new KnownUserException ("cookieValidityMinute should be integer greater than 0. " );
74+ if (!is_int ($ queueConfig ->cookieValidityMinute ) || intval ($ queueConfig ->cookieValidityMinute ) <= 0 ) {
75+ throw new KnownUserException ("cookieValidityMinute from queueConfig should be integer greater than 0. " );
76+ }
77+
78+ if (!is_bool ($ queueConfig ->extendCookieValidity )) {
79+ throw new KnownUserException ("extendCookieValidity from queueConfig should be valid boolean. " );
80+ }
81+
82+ $ userInQueueService = KnownUser::getUserInQueueService ();
83+ return $ userInQueueService ->validateQueueRequest ($ targetUrl , $ queueitToken , $ queueConfig , $ customerId , $ secretKey );
84+ }
85+
86+ public static function cancelRequestByLocalConfig ($ targetUrl , $ queueitToken ,CancelEventConfig $ cancelConfig , $ customerId , $ secretKey ) {
87+ if (KnownUser::getIsDebug ($ queueitToken , $ secretKey ))
88+ {
89+ $ dic = array (
90+ "targetUrl " => $ targetUrl ,
91+ "queueitToken " => $ queueitToken ,
92+ "cancelConfig " =>$ cancelConfig != null ? $ cancelConfig ->getString () : "NULL " ,
93+ "OriginalURL " => KnownUser::getHttpRequestProvider ()->getAbsoluteUri ());
94+ KnownUser::doCookieLog ($ dic );
95+ }
96+
97+ if (Utils::isNullOrEmptyString ($ targetUrl )) {
98+ throw new KnownUserException ("targetUrl can not be null or empty. " );
99+ }
100+ if (Utils::isNullOrEmptyString ($ customerId )) {
101+ throw new KnownUserException ("customerId can not be null or empty. " );
102+ }
103+
104+ if (Utils::isNullOrEmptyString ($ secretKey )) {
105+ throw new KnownUserException ("secretKey can not be null or empty. " );
74106 }
75107
76- if (! is_bool ( $ eventConfig -> extendCookieValidity )) {
77- throw new KnownUserException ("extendCookieValidity should be valid boolean . " );
108+ if (Utils:: isNullOrEmptyString ( $ cancelConfig -> eventId )) {
109+ throw new KnownUserException ("eventId from cancelConfig can not be null or empty . " );
78110 }
79111
112+ if (Utils::isNullOrEmptyString ($ cancelConfig ->queueDomain )) {
113+ throw new KnownUserException ("queueDomain from cancelConfig can not be null or empty. " );
114+ }
80115 $ userInQueueService = KnownUser::getUserInQueueService ();
81- return $ userInQueueService ->validateRequest ($ targetUrl , $ queueitToken , $ eventConfig , $ customerId , $ secretKey );
116+ return $ userInQueueService ->validateCancelRequest ($ targetUrl , $ cancelConfig , $ customerId , $ secretKey );
82117 }
83118
84119 public static function validateRequestByIntegrationConfig ($ currentUrl , $ queueitToken , $ integrationsConfigString , $ customerId , $ secretKey ) {
85- if (empty ($ currentUrl )) {
120+ $ isDebug = KnownUser::getIsDebug ($ queueitToken , $ secretKey );
121+ if ($ isDebug )
122+ {
123+ $ dic = array (
124+ "queueitToken " => $ queueitToken ,
125+ "pureUrl " => $ currentUrl ,
126+ "OriginalURL " => KnownUser::getHttpRequestProvider ()->getAbsoluteUri ());
127+ KnownUser::doCookieLog ($ dic );
128+ }
129+ if (Utils::isNullOrEmptyString ($ currentUrl )) {
86130 throw new KnownUserException ("currentUrl can not be null or empty. " );
87131 }
88132
89- if (empty ($ integrationsConfigString )) {
133+ if (Utils:: isNullOrEmptyString ($ integrationsConfigString )) {
90134 throw new KnownUserException ("integrationsConfigString can not be null or empty. " );
91135 }
92136
93- $ eventConfig = new EventConfig ();
94- $ targetUrl = "" ;
95137
96138 try {
97- $ integrationEvaluator = new IntegrationEvaluator ();
98- $ customerIntegration = json_decode ($ integrationsConfigString , true );
99- $ integrationConfig = $ integrationEvaluator ->getMatchedIntegrationConfig ($ customerIntegration , $ currentUrl ,
100- KnownUser::getCookieArray (), KnownUser::getHttpRequestProvider ()->getUserAgent ());
101-
102- if ($ integrationConfig == null ) {
103- return new RequestValidationResult (NULL , NULL , NULL );
104- }
105- $ eventConfig ->eventId = $ integrationConfig ["EventId " ];
106- $ eventConfig ->queueDomain = $ integrationConfig ["QueueDomain " ];
107- $ eventConfig ->layoutName = $ integrationConfig ["LayoutName " ];
108- $ eventConfig ->culture = $ integrationConfig ["Culture " ];
109- $ eventConfig ->cookieDomain = $ integrationConfig ["CookieDomain " ];
110- $ eventConfig ->extendCookieValidity = $ integrationConfig ["ExtendCookieValidity " ];
111- $ eventConfig ->cookieValidityMinute = $ integrationConfig ["CookieValidityMinute " ];
112- $ eventConfig ->version = $ customerIntegration ["Version " ];
113-
114- switch ($ integrationConfig ["RedirectLogic " ]) {
115- case "ForcedTargetUrl " :
116- case "ForecedTargetUrl " :
117- $ targetUrl = $ integrationConfig ["ForcedTargetUrl " ];
118- break ;
119- case "EventTargetUrl " :
139+ $ integrationEvaluator = new IntegrationEvaluator ();
140+ $ customerIntegration = json_decode ($ integrationsConfigString , true );
141+ if ($ isDebug )
142+ {
143+ $ dic = array ("configVersion " =>$ customerIntegration ["Version " ]);
144+ KnownUser::doCookieLog ($ dic );
145+ }
146+ $ matchedConfig = $ integrationEvaluator ->getMatchedIntegrationConfig ($ customerIntegration , $ currentUrl ,
147+ KnownUser::getCookieArray (), KnownUser::getHttpRequestProvider ()->getUserAgent ());
148+
149+ if ($ isDebug )
150+ {
151+ $ dic = array ("matchedConfig " =>(($ matchedConfig !=NULL ) ? $ matchedConfig ["Name " ]:"NULL " ));
152+ KnownUser::doCookieLog ($ dic );
153+ }
154+
155+ if ($ matchedConfig == NULL ) {
156+ return new RequestValidationResult (NULL ,NULL , NULL , NULL );
157+ }
158+
159+
160+ if (!array_key_exists ("ActionType " ,$ matchedConfig ) || $ matchedConfig ["ActionType " ]== ActionTypes::QueueAction)
161+ {
162+
163+ $ eventConfig = new QueueEventConfig ();
120164 $ targetUrl = "" ;
121- break ;
122- default :
123- $ targetUrl = $ currentUrl ;
124- }
125- } catch (\Exception $ e ) {
126- throw new KnownUserException ("integrationConfiguration text was not valid: " . $ e ->getMessage ());
165+ $ eventConfig ->eventId = $ matchedConfig ["EventId " ];
166+ $ eventConfig ->queueDomain = $ matchedConfig ["QueueDomain " ];
167+ $ eventConfig ->layoutName = $ matchedConfig ["LayoutName " ];
168+ $ eventConfig ->culture = $ matchedConfig ["Culture " ];
169+ $ eventConfig ->cookieDomain = $ matchedConfig ["CookieDomain " ];
170+ $ eventConfig ->extendCookieValidity = $ matchedConfig ["ExtendCookieValidity " ];
171+ $ eventConfig ->cookieValidityMinute = $ matchedConfig ["CookieValidityMinute " ];
172+ $ eventConfig ->version = $ customerIntegration ["Version " ];
173+
174+ switch ($ matchedConfig ["RedirectLogic " ]) {
175+ case "ForcedTargetUrl " :
176+ case "ForecedTargetUrl " :
177+ $ targetUrl = $ matchedConfig ["ForcedTargetUrl " ];
178+ break ;
179+ case "EventTargetUrl " :
180+ $ targetUrl = "" ;
181+ break ;
182+ default :
183+ $ targetUrl = $ currentUrl ;
184+ }
185+ return KnownUser::resolveRequestByLocalEventConfig ($ targetUrl , $ queueitToken , $ eventConfig , $ customerId , $ secretKey );
186+ }
187+ else //cancel action
188+ {
189+ $ cancelEventConfig = new CancelEventConfig ();
190+ $ cancelEventConfig ->eventId = $ matchedConfig ["EventId " ];
191+ $ cancelEventConfig ->queueDomain = $ matchedConfig ["QueueDomain " ];
192+ $ cancelEventConfig ->cookieDomain = $ matchedConfig ["CookieDomain " ];
193+ $ cancelEventConfig ->version = $ customerIntegration ["Version " ];
194+ return KnownUser::cancelRequestByLocalConfig ($ currentUrl , $ queueitToken , $ cancelEventConfig , $ customerId , $ secretKey );
195+ }
127196 }
128- return KnownUser::validateRequestByLocalEventConfig ($ targetUrl , $ queueitToken , $ eventConfig , $ customerId , $ secretKey );
197+ catch (\Exception $ e ) {
198+ throw new KnownUserException ("integrationConfiguration text was not valid: " . $ e ->getMessage ());
199+ }
200+
201+
129202 }
130203
131204 private static function getCookieArray () {
132205 $ arryCookie = array ();
133206 foreach ($ _COOKIE as $ key => $ val ) {
134207 $ arryCookie [$ key ] = $ val ;
135208 }
136-
137209 return $ arryCookie ;
138210 }
211+
212+ private static function doCookieLog (array $ debugInfos )
213+ {
214+ if (KnownUser::$ debugInfoArray !=NULL )
215+ {
216+ foreach (KnownUser::$ debugInfoArray as $ key => $ value )
217+ {
218+ if (!array_key_exists ($ key ,$ debugInfos )) {
219+ $ debugInfos [$ key ]= $ value ;
220+ }
221+ }
222+ }
223+
224+ $ cookieNameValues = array ();
225+ foreach ($ debugInfos as $ key => $ value )
226+ {
227+ array_push ( $ cookieNameValues , $ key .'= ' .$ value );
228+ }
229+
230+ KnownUser::getHttpRequestProvider ()->getCookieManager ()->setCookie ("queueitdebug " , implode ('& ' , $ cookieNameValues ),0 ,NULL );
231+ KnownUser::$ debugInfoArray = $ debugInfos ;
232+ }
233+
234+ private static function getIsDebug ($ queueitToken , $ secretKey )
235+ {
236+
237+ $ queueParams = QueueUrlParams::extractQueueParams ($ queueitToken );
238+ if (!Utils::isNullOrEmptyString ($ queueitToken )) {
239+ if (!Utils::isNullOrEmptyString ($ queueParams ->redirectType ) && strtolower ($ queueParams ->redirectType ) == "debug " )
240+ {
241+
242+
243+ $ calculatedHash = hash_hmac ('sha256 ' , $ queueParams ->queueITTokenWithoutHash , $ secretKey );
244+ return strtoupper ($ calculatedHash ) == strtoupper ($ queueParams ->hashCode );
245+ }
246+ }
247+ return false ;
248+ }
249+
139250}
140251
141252class CookieManager implements ICookieManager
@@ -157,12 +268,31 @@ public function setCookie($name, $value, $expire, $domain) {
157268}
158269interface IHttpRequestProvider
159270{
160- function getUserAgent ();
271+ function getUserAgent ();
272+ function getCookieManager ();
273+ function getAbsoluteUri ();
161274}
162275
163276class HttpRequestProvider implements IHttpRequestProvider
164277{
165278 function getUserAgent () {
166279 return array_key_exists ('HTTP_USER_AGENT ' ,$ _SERVER ) ? $ _SERVER ['HTTP_USER_AGENT ' ] : "" ;
167280 }
281+ function getCookieManager ()
282+ {
283+ return new CookieManager ();
284+ }
285+ function getAbsoluteUri ()
286+ {
287+ // Get HTTP/HTTPS (the possible values for this vary from server to server)
288+ $ myUrl = (isset ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] && !in_array (strtolower ($ _SERVER ['HTTPS ' ]),array ('off ' ,'no ' ))) ? 'https ' : 'http ' ;
289+ // Get domain portion
290+ $ myUrl .= ':// ' .$ _SERVER ['HTTP_HOST ' ];
291+ // Get path to script
292+ $ myUrl .= $ _SERVER ['REQUEST_URI ' ];
293+ // Add path info, if any
294+ if (!empty ($ _SERVER ['PATH_INFO ' ])) $ myUrl .= $ _SERVER ['PATH_INFO ' ];
295+
296+ return $ myUrl ;
297+ }
168298}
0 commit comments