From ae0704a02c56b5ca0022d73e1357da2a6b168b07 Mon Sep 17 00:00:00 2001 From: padams Date: Fri, 2 Dec 2022 07:14:31 +0000 Subject: [PATCH] refactoring php pageView tracker request call to include user agent nad accept headers. Refactoring setcooie function to work properly with php 7.3+, fixes #8. --- src/OwaClient.php | 2 +- src/Tracker/State.php | 33 +++++++++++++++++++++++++------- src/Tracker/TrackerClient.php | 36 ++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/OwaClient.php b/src/OwaClient.php index 9a18c52..a327060 100644 --- a/src/OwaClient.php +++ b/src/OwaClient.php @@ -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; diff --git a/src/Tracker/State.php b/src/Tracker/State.php index d093112..4db866e 100644 --- a/src/Tracker/State.php +++ b/src/Tracker/State.php @@ -35,7 +35,7 @@ function __construct( $config = []) { 'cookie_domain' => '' ]; - // merge incomming config params + // merge incoming config params $this->config = array_merge( $this->config, $config ); @@ -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' ); @@ -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 = '') { diff --git a/src/Tracker/TrackerClient.php b/src/Tracker/TrackerClient.php index 4381ca7..590b2b4 100644 --- a/src/Tracker/TrackerClient.php +++ b/src/Tracker/TrackerClient.php @@ -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; }