Skip to content

Commit 543fd8d

Browse files
Added a method to strip crowdhandler parameters and redirect on promotion
1 parent 546f812 commit 543fd8d

1 file changed

Lines changed: 53 additions & 4 deletions

File tree

src/GateKeeper.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -45,15 +54,55 @@ public function __construct(Client $client, \Psr\Http\Message\ServerRequestInter
4554
} elseif (isset($cookies[self::TOKEN_COOKIE])) {
4655
$this->token = $cookies[self::TOKEN_COOKIE];
4756
}
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);
57+
58+
// now we've extracted the token we sanitize the url
59+
$this->url = $this->sanitizeURL($this->url, $get);
60+
5261
$this->detectClientIp($server);
5362
if (isset($server['HTTP_USER_AGENT'])) $this->agent = $server['HTTP_USER_AGENT'];
5463
if (isset($server['HTTP_ACCEPT_LANGUAGE'])) $this->lang = $server['HTTP_ACCEPT_LANGUAGE'];
5564
}
5665

66+
/**
67+
* Removes crowdhandler specific query parameters on promotion
68+
* @param string $url The url that is currently being requested
69+
* @param array $get An array of the current query sring parameters
70+
*/
71+
private function sanitizeURL ($url, $get)
72+
{
73+
74+
$isPromoted = false;
75+
76+
$parsed_url = parse_url($url);
77+
$this->url = 'https://' . $parsed_url['host'] . $parsed_url['path'];
78+
79+
$ch_params_to_remove = array();
80+
for ($i=0; $i < Count(self::CROWDHANDLER_PARAMS); $i++) {
81+
if (isset($get[self::CROWDHANDLER_PARAMS[$i]]))
82+
{
83+
$isPromoted = true;
84+
array_push($ch_params_to_remove, $get[self::CROWDHANDLER_PARAMS[$i]]);
85+
}
86+
}
87+
88+
$remaining_query_parameters = array_diff($get, $ch_params_to_remove);
89+
90+
if (Count($remaining_query_parameters) > 0) {
91+
$this->url = $this->url .= '?' . http_build_query($remaining_query_parameters);
92+
}
93+
94+
if($isPromoted) {
95+
// and redirect
96+
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
97+
header('location: '.$this->url, true, self::HTTP_REDIRECT_CODE);
98+
exit;
99+
} else {
100+
return $this->url;
101+
}
102+
103+
104+
}
105+
57106
private function detectClientIp($server)
58107
{
59108
if (array_key_exists('HTTP_X_FORWARDED_FOR', $server)) {

0 commit comments

Comments
 (0)