@@ -9,6 +9,15 @@ class GateKeeper
99 const TOKEN_COOKIE = 'ch-id ' ;
1010 const TOKEN_URL = 'ch-id ' ;
1111
12+ const CROWDHANDLER_PARAMS = array (
13+ 'ch-id ' ,
14+ 'ch-fresh ' ,
15+ 'ch-id-signature ' ,
16+ 'ch-public-key ' ,
17+ 'ch-requested ' ,
18+ 'ch-code '
19+ );
20+
1221 private $ ignore = "/^((?!.*\?).*(\.(avi|css|eot|gif|ico|jpg|jpeg|js|json|mov|mp4|mpeg|mpg|og[g|v]|pdf|png|svg|ttf|txt|wmv|woff|woff2|xml))$)/ " ;
1322 private $ client ;
1423 private $ failTrust = true ;
@@ -40,20 +49,53 @@ public function __construct(Client $client, \Psr\Http\Message\ServerRequestInter
4049 $ server = $ _SERVER ;
4150 $ cookies = $ _COOKIE ;
4251 }
52+
53+
54+ // Token in URL
4355 if (isset ($ get [self ::TOKEN_URL ])) {
44- $ this ->token = $ get [self ::TOKEN_URL ];
56+ $ this ->setCookie ($ get [self ::TOKEN_URL ]);
57+ // clean url and redirect
58+ $ this ->sanitizeURL ($ this ->url , $ get );
59+ header ("Cache-Control: no-store, no-cache, must-revalidate, max-age=0 " );
60+ header ('location: ' .$ this ->url , true , self ::HTTP_REDIRECT_CODE );
61+ exit ;
62+
4563 } elseif (isset ($ cookies [self ::TOKEN_COOKIE ])) {
4664 $ this ->token = $ cookies [self ::TOKEN_COOKIE ];
4765 }
48- // now we've extracted the token we sanitize the url
49- $ this ->url = 'https:// ' . parse_url ($ this ->url , PHP_URL_HOST ) . parse_url ($ this ->url , PHP_URL_PATH );
50- unset($ get [self ::TOKEN_URL ]);
51- if (count ($ get )) $ this ->url .= '? ' . http_build_query ($ get );
66+
5267 $ this ->detectClientIp ($ server );
5368 if (isset ($ server ['HTTP_USER_AGENT ' ])) $ this ->agent = $ server ['HTTP_USER_AGENT ' ];
5469 if (isset ($ server ['HTTP_ACCEPT_LANGUAGE ' ])) $ this ->lang = $ server ['HTTP_ACCEPT_LANGUAGE ' ];
5570 }
5671
72+ /**
73+ * Removes crowdhandler specific query parameters on promotion
74+ * @param string $url The url that is currently being requested
75+ * @param array $get An array of the current query sring parameters
76+ */
77+ private function sanitizeURL ($ url , $ get )
78+ {
79+
80+ $ parsed_url = parse_url ($ url );
81+ $ this ->url = 'https:// ' . $ parsed_url ['host ' ] . $ parsed_url ['path ' ];
82+
83+ $ ch_params_to_remove = array ();
84+ for ($ i =0 ; $ i < Count (self ::CROWDHANDLER_PARAMS ); $ i ++) {
85+ if (isset ($ get [self ::CROWDHANDLER_PARAMS [$ i ]]))
86+ {
87+ array_push ($ ch_params_to_remove , $ get [self ::CROWDHANDLER_PARAMS [$ i ]]);
88+ }
89+ }
90+
91+ $ remaining_query_parameters = array_diff ($ get , $ ch_params_to_remove );
92+
93+ if (Count ($ remaining_query_parameters ) > 0 ) {
94+ $ this ->url = $ this ->url .= '? ' . http_build_query ($ remaining_query_parameters );
95+ }
96+
97+ }
98+
5799 private function detectClientIp ($ server )
58100 {
59101 if (array_key_exists ('HTTP_X_FORWARDED_FOR ' , $ server )) {
@@ -155,7 +197,10 @@ public function checkRequest()
155197 $ this ->result = $ this ->client ->requests ->get ($ this ->token , $ params );
156198 } else {
157199 $ this ->result = $ this ->client ->requests ->post ($ params );
158- }
200+ }
201+ if (isset ($ this ->result ->token )) {
202+ $ this ->setCookie ($ this ->result ->token );
203+ }
159204 }
160205 catch (\Exception $ e ) {
161206 $ mock = new ApiObject ;
@@ -201,11 +246,11 @@ public function getRedirectUrl()
201246 /**
202247 * Set CrowdHandler session cookie
203248 */
204- public function setCookie ()
249+ private function setCookie ($ cookie )
205250 {
206- if (!is_null ($ this -> result -> token )) {
207- setcookie (self ::TOKEN_COOKIE , $ this -> result -> token , 0 , '/ ' , '' , $ this ->debug ? false : true );
208- $ this ->debug ('Setting cookie ' .$ this -> result -> token );
251+ if (!is_null ($ cookie )) {
252+ setcookie (self ::TOKEN_COOKIE , $ cookie , 0 , '/ ' , '' , $ this ->debug ? false : true );
253+ $ this ->debug ('Setting cookie ' .$ cookie );
209254 }
210255 }
211256
0 commit comments