Skip to content

Commit

Permalink
refactoring php pageView tracker request call to include user agent n…
Browse files Browse the repository at this point in the history
…ad accept headers. Refactoring setcooie function to work properly with php 7.3+, fixes #8.
  • Loading branch information
padams committed Dec 2, 2022
1 parent b9eb438 commit ae0704a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/OwaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function makeRequest( $params ) {

}

catch( RequestException | ConnectException | ClientException $e ) {
catch( \GuzzleHttp\Exception\RequestException | \GuzzleHttp\Exception\ConnectException | \GuzzleHttp\Exception\ClientException $e ) {

$r = $e->getRequest();
$res = null;
Expand Down
33 changes: 26 additions & 7 deletions src/Tracker/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function __construct( $config = []) {
'cookie_domain' => ''
];

// merge incomming config params
// merge incoming config params
$this->config = array_merge( $this->config, $config );


Expand Down Expand Up @@ -346,7 +346,11 @@ public static function implode_assoc($inner_glue, $outer_glue, $array) {
}

private function createCookie($cookie_name, $cookie_value, $expires = 0, $path = '/', $domain = '') {


$samesite = 'lax';
$secure = false;
$httponly = false;

if (! $domain ) {

$domain = $this->getSetting( 'cookie_domain' );
Expand All @@ -364,13 +368,28 @@ private function createCookie($cookie_name, $cookie_value, $expires = 0, $path =
sdk::debug(sprintf('Setting cookie %s with values: %s under domain: %s', $cookie_name, $cookie_value, $domain));

// makes cookie to session cookie only
if ( !$this->getSetting( 'cookie_persistence' ) ) {
if ( ! $this->getSetting( 'cookie_persistence' ) ) {
$expires = 0;
}

$path .= '; SameSite=lax';

setcookie($cookie_name, $cookie_value, $expires, $path, $domain);

// check for php version to set samesite attribute.
//php 7.2
if (PHP_VERSION_ID < 70300) {

$path .= '; SameSite='.$samesite;
setcookie($cookie_name, $cookie_value, $expires, $path, $domain);

} else {
//php 7.3+
setcookie($cookie_name, $cookie_value, [
'expires' => $expires,
'path' => $path,
'domain' => $domain,
'samesite' => $samesite,
'secure' => $secure,
'httponly' => $httponly,
]);
}
}

private function deleteCookie($cookie_name, $path = '/', $domain = '') {
Expand Down
36 changes: 31 additions & 5 deletions src/Tracker/TrackerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,11 +1172,37 @@ private function logEvent($event_type, $event) {

$params = $this->applyNamespaceToKeys( $params );

$res = $http->request(
'GET', 'log.php',
['query' => $params ]
);

try {
$res = $http->request(
'GET', 'log.php',
[
'query' => $params,
'headers' => [
'User-Agent' => 'OWA SDK Client',
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
]
]
);
} catch( \GuzzleHttp\Exception\RequestException | \GuzzleHttp\Exception\ConnectException | \GuzzleHttp\Exception\ClientException $e ) {

$r = $e->getRequest();
$res = null;

error_log( print_r( $r, true ) );

if ( $e->hasResponse() ) {

$res = $e->getResponse();

error_log( print_r( $res, true ) );
}

if ( $this->getSetting( 'debug' ) ) {

print_r($r);
print_r($res);
}
}
return $res;
}

Expand Down

0 comments on commit ae0704a

Please sign in to comment.