Skip to content
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
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/SparxstarUserEnvironmentCheck.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* SPARXSTAR User Environment Check
*
Expand All @@ -22,7 +23,6 @@
use Starisian\SparxstarUEC\helpers\StarLogger;
use Starisian\SparxstarUEC\core\SparxstarUECKernel;
use Starisian\SparxstarUEC\api\SparxstarUECRESTController;
use Starisian\SparxstarUEC\StarUserEnv;

/**
* Orchestrates plugin services and exposes shared dependencies.
Expand Down
157 changes: 79 additions & 78 deletions src/StarUserEnv.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* SPARXSTAR User Environment Check
*
Expand Down Expand Up @@ -122,7 +123,7 @@ public static function getDeviceHash(): string
/**
* Retrieve the latest stored snapshot for a user/session (public).
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @return array<string, mixed>|null Snapshot payload when available.
*/
Expand All @@ -134,60 +135,60 @@ public static function get_snapshot(?int $user_id = null, ?string $session_id =
/**
* Internal engine fetching snapshot from runtime cache/session/cache/DB.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @return array<string, mixed>|null Snapshot payload when available.
*/
private static function fetch_snapshot(?int $user_id, ?string $session_id): ?array
{
// --- 1. CHECK RUNTIME CACHE (Fastest) ---
if (self::$snapshot_cache !== null) {
return self::$snapshot_cache;
}
// --- 2. CHECK SESSION (Primary fix for REST API) ---
// If we're in an AJAX/REST context, the session will have the data if
// the user loaded the page immediately prior.
$session_stored = self::getEnvironmentSnapshot();
if (!empty($session_stored['snapshot'])) {
self::$snapshot_cache = $session_stored['snapshot'];
return self::$snapshot_cache;
}
// --- 3. CHECK DB/CACHE (Original UEC Logic) ---
$fingerprint = self::getFingerprint();
$device_hash = self::getDeviceHash();
$resolved_user_id = $user_id ?? (get_current_user_id() ?: null);
// Build cache key using BOTH fingerprint AND device_hash
$cache_key = SparxstarUECCacheHelper::make_key(
$resolved_user_id,
$session_id,
$fingerprint . ':' . $device_hash
);
$cached = SparxstarUECCacheHelper::get($cache_key);
if ($cached !== null) {
self::$snapshot_cache = $cached;
return $cached;
}
// Query database using v2.0 identity model
$from_db = SparxstarUECSnapshotRepository::get($fingerprint, $device_hash);
if ($from_db !== null) {
SparxstarUECCacheHelper::set($cache_key, $from_db);
self::$snapshot_cache = $from_db;
}
return $from_db;
}
private static function fetch_snapshot(?int $user_id, ?string $session_id): ?array
{
// --- 1. CHECK RUNTIME CACHE (Fastest) ---
if (self::$snapshot_cache !== null) {
return self::$snapshot_cache;
}

// --- 2. CHECK SESSION (Primary fix for REST API) ---
// If we're in an AJAX/REST context, the session will have the data if
// the user loaded the page immediately prior.
$session_stored = self::getEnvironmentSnapshot();
if (!empty($session_stored['snapshot'])) {
self::$snapshot_cache = $session_stored['snapshot'];
return self::$snapshot_cache;
}

// --- 3. CHECK DB/CACHE (Original UEC Logic) ---
$fingerprint = self::getFingerprint();
$device_hash = self::getDeviceHash();
$resolved_user_id = $user_id ?? (get_current_user_id() ?: null);

// Build cache key using BOTH fingerprint AND device_hash
$cache_key = SparxstarUECCacheHelper::make_key(
$resolved_user_id,
$session_id,
$fingerprint . ':' . $device_hash
);

$cached = SparxstarUECCacheHelper::get($cache_key);
if ($cached !== null) {
self::$snapshot_cache = $cached;
return $cached;
}

// Query database using v2.0 identity model
$from_db = SparxstarUECSnapshotRepository::get($fingerprint, $device_hash);
if ($from_db !== null) {
SparxstarUECCacheHelper::set($cache_key, $from_db);
self::$snapshot_cache = $from_db;
}

return $from_db;
}

/**
* Generic dot-path accessor into snapshot structure.
*
* @param string $path Dot-path into snapshot array.
* @param mixed $default Default value when missing.
* @param int|null $user_id Optional user scope.
* @param string $path Dot-path into snapshot array.
* @param mixed $default Default value when missing.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @return mixed Resolved value or default.
*/
Expand Down Expand Up @@ -217,7 +218,7 @@ private static function get_value_from_snapshot(
/**
* Flushes the cache for a given user, forcing the next getter call to fetch fresh data.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function flush_cache(?int $user_id = null, ?string $session_id = null): void
Expand All @@ -239,7 +240,7 @@ public static function flush_cache(?int $user_id = null, ?string $session_id = n
/**
* Retrieves the entire raw snapshot for debugging or full-data use cases.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @return array<string, mixed>|null Snapshot payload when available.
*/
Expand All @@ -258,7 +259,7 @@ public static function get_full_snapshot(?int $user_id = null, ?string $session_
* Get the user's stable, anonymous browser fingerprint ID from the snapshot.
* This is the primary key for tracking anonymous users.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_visitor_id(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -276,7 +277,7 @@ public static function get_visitor_id(?int $user_id = null, ?string $session_id
/**
* Retrieve effective network type from the latest snapshot.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_network_type(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -292,7 +293,7 @@ public static function get_network_type(?int $user_id = null, ?string $session_i
/**
* Determine whether data-saver mode is enabled in snapshot data.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function is_data_saver_enabled(?int $user_id = null, ?string $session_id = null): bool
Expand All @@ -308,7 +309,7 @@ public static function is_data_saver_enabled(?int $user_id = null, ?string $sess
/**
* Get normalized device type from snapshot.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_device(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -324,7 +325,7 @@ public static function get_user_device(?int $user_id = null, ?string $session_id
/**
* Get GPU identifier from snapshot fingerprinting data.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_gpu(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -340,7 +341,7 @@ public static function get_user_gpu(?int $user_id = null, ?string $session_id =
/**
* Get operating system label from snapshot.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_os_name(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -356,7 +357,7 @@ public static function get_os_name(?int $user_id = null, ?string $session_id = n
/**
* Get browser name from snapshot.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_browser_name(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -374,7 +375,7 @@ public static function get_browser_name(?int $user_id = null, ?string $session_i
/**
* Get server-observed user IP from snapshot.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_ip(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -390,7 +391,7 @@ public static function get_user_ip(?int $user_id = null, ?string $session_id = n
/**
* Get user country from snapshot geolocation block.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_country(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -406,7 +407,7 @@ public static function get_user_country(?int $user_id = null, ?string $session_i
/**
* Get user region/state from snapshot geolocation block.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_state(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -422,7 +423,7 @@ public static function get_user_state(?int $user_id = null, ?string $session_id
/**
* Get user city from snapshot geolocation block.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_city(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -438,7 +439,7 @@ public static function get_user_city(?int $user_id = null, ?string $session_id =
/**
* Get two-letter language code from snapshot context language.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_user_language(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -458,7 +459,7 @@ public static function get_user_language(?int $user_id = null, ?string $session_
/**
* Get UTC timestamp of the stored snapshot.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function get_snapshot_timestamp(?int $user_id = null, ?string $session_id = null): string
Expand All @@ -474,7 +475,7 @@ public static function get_snapshot_timestamp(?int $user_id = null, ?string $ses
/**
* Determine if geolocation data flagged VPN/proxy usage.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
*/
public static function is_on_vpn(?int $user_id = null, ?string $session_id = null): bool
Expand All @@ -500,7 +501,7 @@ public static function get_current_user_session_id(): string
/**
* Get full geolocation payload from snapshot server block.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @return array<string, mixed> Geolocation map.
*/
Expand All @@ -519,9 +520,9 @@ public static function get_geolocation(?int $user_id = null, ?string $session_id
/**
* Geolocation convenience accessor for city.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @param string $default Default when value is missing.
* @param string $default Default when value is missing.
*/
public static function get_city(
?int $user_id = null,
Expand All @@ -539,9 +540,9 @@ public static function get_city(
/**
* Geolocation convenience accessor for state.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @param string $default Default when value is missing.
* @param string $default Default when value is missing.
*/
public static function get_state(
?int $user_id = null,
Expand All @@ -559,9 +560,9 @@ public static function get_state(
/**
* Geolocation convenience accessor for postal code.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @param string $default Default when value is missing.
* @param string $default Default when value is missing.
*/
public static function get_postal_code(
?int $user_id = null,
Expand All @@ -579,9 +580,9 @@ public static function get_postal_code(
/**
* Geolocation convenience accessor for region.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @param string $default Default when value is missing.
* @param string $default Default when value is missing.
*/
public static function get_region(
?int $user_id = null,
Expand All @@ -599,9 +600,9 @@ public static function get_region(
/**
* Geolocation convenience accessor for country.
*
* @param int|null $user_id Optional user scope.
* @param int|null $user_id Optional user scope.
* @param string|null $session_id Optional session scope.
* @param string $default Default when value is missing.
* @param string $default Default when value is missing.
*/
public static function get_country(
?int $user_id = null,
Expand Down Expand Up @@ -746,7 +747,7 @@ public static function get_current_visitor_ip(): string
* Persist an arbitrary value in the session namespace.
*
* @param string $key Session key.
* @param mixed $value Value to persist.
* @param mixed $value Value to persist.
*/
public static function setSessionValue(string $key, mixed $value): void
{
Expand All @@ -758,7 +759,7 @@ public static function setSessionValue(string $key, mixed $value): void
* Retrieve a value from the session namespace.
*
* @param string $key Session key.
* @param mixed $default Default return when missing.
* @param mixed $default Default return when missing.
* @return mixed Resolved session value.
*/
public static function getSessionValue(string $key, mixed $default = null): mixed
Expand Down
Loading