diff --git a/composer.json b/composer.json index 5242e7e3..6993f9ad 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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", diff --git a/src/Data/Repositories/Cookie.php b/src/Data/Repositories/Cookie.php index f7227062..4e7be1fb 100644 --- a/src/Data/Repositories/Cookie.php +++ b/src/Data/Repositories/Cookie.php @@ -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]); diff --git a/src/Data/Repositories/Session.php b/src/Data/Repositories/Session.php index 34c4ed4c..9318d8c6 100644 --- a/src/Data/Repositories/Session.php +++ b/src/Data/Repositories/Session.php @@ -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; @@ -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'))); diff --git a/src/Support/Exceptions/Handler.php b/src/Support/Exceptions/Handler.php index f95cf6b8..b3ae8cf6 100644 --- a/src/Support/Exceptions/Handler.php +++ b/src/Support/Exceptions/Handler.php @@ -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); diff --git a/src/Tracker.php b/src/Tracker.php index 7fde4dd0..b7ae377c 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -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(); @@ -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(), @@ -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(), @@ -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 */ diff --git a/src/Vendor/Laravel/Models/Log.php b/src/Vendor/Laravel/Models/Log.php index 1b62cd10..bb66f137 100644 --- a/src/Vendor/Laravel/Models/Log.php +++ b/src/Vendor/Laravel/Models/Log.php @@ -8,6 +8,7 @@ class Log extends Base protected $fillable = [ 'session_id', + 'cookie_id', 'method', 'path_id', 'query_id', diff --git a/src/Vendor/Laravel/Models/Session.php b/src/Vendor/Laravel/Models/Session.php index 2047b554..8dc63213 100644 --- a/src/Vendor/Laravel/Models/Session.php +++ b/src/Vendor/Laravel/Models/Session.php @@ -8,6 +8,7 @@ class Session extends Base protected $fillable = [ 'uuid', + 'session_id', 'user_id', 'device_id', 'language_id', diff --git a/src/config/config.php b/src/config/config.php index 38ca17fc..90ec15e7 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -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. diff --git a/src/migrations/2021_12_14_311104_add_cookie_id_to_tracker_log.php b/src/migrations/2021_12_14_311104_add_cookie_id_to_tracker_log.php new file mode 100644 index 00000000..e934a552 --- /dev/null +++ b/src/migrations/2021_12_14_311104_add_cookie_id_to_tracker_log.php @@ -0,0 +1,46 @@ +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'); + } + ); + } +} diff --git a/src/migrations/2021_12_14_311105_add_session_id_to_tracker_sessions.php b/src/migrations/2021_12_14_311105_add_session_id_to_tracker_sessions.php new file mode 100644 index 00000000..17094055 --- /dev/null +++ b/src/migrations/2021_12_14_311105_add_session_id_to_tracker_sessions.php @@ -0,0 +1,46 @@ +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'); + } + ); + } +} diff --git a/src/migrations/2021_12_14_311106_change_tracker_cookies_uuid_length_512.php b/src/migrations/2021_12_14_311106_change_tracker_cookies_uuid_length_512.php new file mode 100644 index 00000000..9072ccf7 --- /dev/null +++ b/src/migrations/2021_12_14_311106_change_tracker_cookies_uuid_length_512.php @@ -0,0 +1,40 @@ +builder->table( + $this->table, + function ($table) { + $table->string('uuid', 512)->change(); + } + ); + } catch (\Exception $e) { + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function migrateDown() + { + } +}