Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime cookie #540

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"require": {
"php": ">=7.0",
"doctrine/dbal": "^2.6",
"laravel/framework": "~5|~6|~7|~8",
"pragmarx/support": "~0.6|~0.7|~0.8|~0.9",
"laravel/framework": "~5|~6|~7|~8|~9",
"pragmarx/support": "dev-master",
"ramsey/uuid": "^3 || ^4",
"jenssegers/agent": "~2.1",
"ua-parser/uap-php" : "~3.4",
Expand All @@ -28,6 +28,13 @@
"jaybizzle/crawler-detect": "~1.0",
"psr/log": "~1.0"
},

"repositories": [
{
"type": "git",
"url": "https://github.com/ilyutkin/support/"
}
],

"suggest": {
"geoip/geoip": "~1.14",
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Repositories/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getId()
if (!$cookie = $this->request->cookie($this->config->get('tracker_cookie_name'))) {
$cookie = UUID::uuid4()->toString();

$this->cookieJar->queue($this->config->get('tracker_cookie_name'), $cookie, 0);
$this->cookieJar->queue($this->config->get('tracker_cookie_name'), $cookie, $this->config->get('tracker_cookie_lifetime'));
}

return $this->findOrCreate(['uuid' => $cookie]);
Expand Down
32 changes: 29 additions & 3 deletions src/Data/Repositories/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,22 @@ private function sessionIsKnownOrCreateSession()
} else {
$session = $this->find($this->getSessionData('id'));

$session->updated_at = Carbon::now();
if ($this->config->get('tracker_visit_close') &&
(is_null($session->user_id)) &&
($session->updated_at < Carbon::now()->subMinutes($this->config->get('tracker_visit_close')))
) {
$this->resetSessionUuid();

$session->save();
$this->sessionSetId($this->findOrCreate($this->sessionInfo, ['uuid']));

$this->sessionInfo['id'] = $this->getSessionData('id');
$known = false;
} else {
$session->updated_at = Carbon::now();

$session->save();

$this->sessionInfo['id'] = $this->getSessionData('id');
}
}

return $known;
Expand Down Expand Up @@ -282,6 +293,21 @@ public function getCurrent()
return $this->getModel();
}

public function resetSession()
{
$this->sessionInfo['uuid'] = null;

$data = $this->sessionInfo;

unset($data['uuid']);

$this->putSessionData($data);

$this->checkSessionUuid();

return $data;
}

public function updateSessionData($data)
{
$session = $this->checkIfUserChanged($data, $this->find($this->getSessionData('id')));
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function handleThrowable(Throwable $throwable)
return call_user_func($this->originalExceptionHandler, $throwable);
}

public function handleError($err_severity, $err_msg, $err_file, $err_line, array $err_context)
public function handleError($err_severity, $err_msg, $err_file, $err_line, array $err_context = [])
{
try {
$error = ExceptionFactory::make($err_severity, $err_msg);
Expand Down
14 changes: 14 additions & 0 deletions src/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function currentSession()
return $this->dataRepositoryManager->sessionRepository->getCurrent();
}

public function resetSession()
{
return $this->dataRepositoryManager->sessionRepository->resetSession();
}

protected function deleteCurrentLog()
{
$this->dataRepositoryManager->logRepository->delete();
Expand Down Expand Up @@ -169,6 +174,7 @@ public function getLogData()
{
return [
'session_id' => $this->getSessionId(true),
'cookie_id' => $this->getCookieId(),
'method' => $this->request->method(),
'path_id' => $this->getPathId(),
'query_id' => $this->getQueryId(),
Expand Down Expand Up @@ -239,6 +245,7 @@ protected function logUntrackable($item)
protected function makeSessionData()
{
$sessionData = [
'session_id' => session_id(),
'user_id' => $this->getUserId(),
'device_id' => $this->getDeviceId(),
'client_ip' => $this->request->getClientIp(),
Expand Down Expand Up @@ -273,6 +280,13 @@ public function getUserId()
: null;
}

public function setUserId($user_id)
{
$session = $this->dataRepositoryManager->sessionRepository->getCurrent();
$session->user_id = $user_id;
$session->save();
}

/**
* @param \Throwable $throwable
*/
Expand Down
1 change: 1 addition & 0 deletions src/Vendor/Laravel/Models/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Log extends Base

protected $fillable = [
'session_id',
'cookie_id',
'method',
'path_id',
'query_id',
Expand Down
1 change: 1 addition & 0 deletions src/Vendor/Laravel/Models/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Session extends Base

protected $fillable = [
'uuid',
'session_id',
'user_id',
'device_id',
'language_id',
Expand Down
10 changes: 10 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,21 @@
*/
'tracker_cookie_name' => 'please_change_this_cookie_name',

/*
* If you are storing cookies, set the lifetime.
*/
'tracker_cookie_lifetime' => 0,

/*
* Internal tracker session name.
*/
'tracker_session_name' => 'tracker_session',

/*
* The visit ends when no new events arrive from the visitor for a certain time.
*/
'tracker_visit_close' => 30,

/*
* ** IMPORTANT **
* Change the user model to your own.
Expand Down
46 changes: 46 additions & 0 deletions src/migrations/2021_12_14_311104_add_cookie_id_to_tracker_log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use PragmaRX\Tracker\Support\Migration;

class AddCookieIdToTrackerLog extends Migration
{
/**
* Table related to this migration.
*
* @var string
*/
private $table = 'tracker_log';

/**
* Run the migrations.
*
* @return void
*/
public function migrateUp()
{
try {
$this->builder->table(
$this->table,
function ($table) {
$table->bigInteger('cookie_id')->unsigned()->nullable()->index()->after('session_id');
}
);
} catch (\Exception $e) {
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function migrateDown()
{
$this->builder->table(
$this->table,
function ($table) {
$table->dropColumn('cookie_id');
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use PragmaRX\Tracker\Support\Migration;

class AddSessionIdToTrackerSessions extends Migration
{
/**
* Table related to this migration.
*
* @var string
*/
private $table = 'tracker_sessions';

/**
* Run the migrations.
*
* @return void
*/
public function migrateUp()
{
try {
$this->builder->table(
$this->table,
function ($table) {
$table->string('session_id')->after('uuid');
}
);
} catch (\Exception $e) {
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function migrateDown()
{
$this->builder->table(
$this->table,
function ($table) {
$table->dropColumn('session_id');
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use PragmaRX\Tracker\Support\Migration;

class ChangeTrackerCookiesUuidLength512 extends Migration
{
/**
* Table related to this migration.
*
* @var string
*/
private $table = 'tracker_cookies';

/**
* Run the migrations.
*
* @return void
*/
public function migrateUp()
{
try {
$this->builder->table(
$this->table,
function ($table) {
$table->string('uuid', 512)->change();
}
);
} catch (\Exception $e) {
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function migrateDown()
{
}
}