Skip to content

Commit ae0704a

Browse files
author
padams
committed
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.
1 parent b9eb438 commit ae0704a

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/OwaClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function makeRequest( $params ) {
203203

204204
}
205205

206-
catch( RequestException | ConnectException | ClientException $e ) {
206+
catch( \GuzzleHttp\Exception\RequestException | \GuzzleHttp\Exception\ConnectException | \GuzzleHttp\Exception\ClientException $e ) {
207207

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

src/Tracker/State.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function __construct( $config = []) {
3535
'cookie_domain' => ''
3636
];
3737

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

4141

@@ -346,7 +346,11 @@ public static function implode_assoc($inner_glue, $outer_glue, $array) {
346346
}
347347

348348
private function createCookie($cookie_name, $cookie_value, $expires = 0, $path = '/', $domain = '') {
349-
349+
350+
$samesite = 'lax';
351+
$secure = false;
352+
$httponly = false;
353+
350354
if (! $domain ) {
351355

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

366370
// makes cookie to session cookie only
367-
if ( !$this->getSetting( 'cookie_persistence' ) ) {
371+
if ( ! $this->getSetting( 'cookie_persistence' ) ) {
368372
$expires = 0;
369373
}
370-
371-
$path .= '; SameSite=lax';
372-
373-
setcookie($cookie_name, $cookie_value, $expires, $path, $domain);
374+
375+
// check for php version to set samesite attribute.
376+
//php 7.2
377+
if (PHP_VERSION_ID < 70300) {
378+
379+
$path .= '; SameSite='.$samesite;
380+
setcookie($cookie_name, $cookie_value, $expires, $path, $domain);
381+
382+
} else {
383+
//php 7.3+
384+
setcookie($cookie_name, $cookie_value, [
385+
'expires' => $expires,
386+
'path' => $path,
387+
'domain' => $domain,
388+
'samesite' => $samesite,
389+
'secure' => $secure,
390+
'httponly' => $httponly,
391+
]);
392+
}
374393
}
375394

376395
private function deleteCookie($cookie_name, $path = '/', $domain = '') {

src/Tracker/TrackerClient.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,11 +1172,37 @@ private function logEvent($event_type, $event) {
11721172

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

1175-
$res = $http->request(
1176-
'GET', 'log.php',
1177-
['query' => $params ]
1178-
);
1179-
1175+
try {
1176+
$res = $http->request(
1177+
'GET', 'log.php',
1178+
[
1179+
'query' => $params,
1180+
'headers' => [
1181+
'User-Agent' => 'OWA SDK Client',
1182+
'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'
1183+
]
1184+
]
1185+
);
1186+
} catch( \GuzzleHttp\Exception\RequestException | \GuzzleHttp\Exception\ConnectException | \GuzzleHttp\Exception\ClientException $e ) {
1187+
1188+
$r = $e->getRequest();
1189+
$res = null;
1190+
1191+
error_log( print_r( $r, true ) );
1192+
1193+
if ( $e->hasResponse() ) {
1194+
1195+
$res = $e->getResponse();
1196+
1197+
error_log( print_r( $res, true ) );
1198+
}
1199+
1200+
if ( $this->getSetting( 'debug' ) ) {
1201+
1202+
print_r($r);
1203+
print_r($res);
1204+
}
1205+
}
11801206
return $res;
11811207
}
11821208

0 commit comments

Comments
 (0)