Skip to content
Merged
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 .github/workflows/php81.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
test:
if: ${{ !contains(github.event.head_commit.message, 'release-please--branches--main') }}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 10

env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php82.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
test:
if: ${{ !contains(github.event.head_commit.message, 'release-please--branches--main') }}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 10

env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php83.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
test:
if: ${{ !contains(github.event.head_commit.message, 'release-please--branches--main') }}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 10

env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php84.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
test:
if: ${{ !contains(github.event.head_commit.message, 'release-please--branches--main') }}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 10

env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php85.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
test:
if: ${{ !contains(github.event.head_commit.message, 'release-please--branches--main') }}
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 10

env:
Expand Down
81 changes: 67 additions & 14 deletions WebFiori/Framework/Session/CacheSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Ibrahim
* @since 3.1.0 (per-key interface)
*/
class CacheSessionStorage implements SessionStorage {
class CacheSessionStorage implements ExpiringSessionStorage {
private string $prefix;
private Storage $storage;
private int $ttl;
Expand All @@ -51,6 +51,7 @@
}

$this->storage->delete($this->indexKey($sessionId));
$this->storage->delete($this->expiryKey($sessionId));
}

/**
Expand All @@ -60,6 +61,15 @@
$this->storage->purgeExpired();
}

/**
* {@inheritdoc}
*/
public function getExpiry(string $sessionId): ?int {
$value = $this->storage->read($this->expiryKey($sessionId), null);

return is_int($value) ? $value : null;
}

public function getPrefix(): string {
return $this->prefix;
}
Expand Down Expand Up @@ -112,6 +122,38 @@
$this->removeFromIndex($sessionId, $key);
}

/**
* {@inheritdoc}
*
* The expiry is enforced through the cache TTL: every entry of the session
* is stored again with a TTL equal to the remaining lifetime, and later
* writes use the same TTL. Clearing the expiry leaves existing entries
* with the TTL they already have.
*/
public function setExpiry(string $sessionId, ?int $expiresAt): void {
if ($expiresAt === null) {
$this->storage->delete($this->expiryKey($sessionId));

return;
}
$ttl = max(1, $expiresAt - time());
$this->storeItem($this->expiryKey($sessionId), $expiresAt, $ttl);

$keys = $this->readIndex($sessionId);

foreach ($keys as $key) {
$entry = $this->read($sessionId, $key);

if ($entry !== null) {
$this->storeItem($this->keyFor($sessionId, $key), $entry, $ttl);
}
}

if ($keys !== []) {
$this->storeItem($this->indexKey($sessionId), $keys, $ttl);
}
}

public function setTTL(int $ttl): void {
$this->ttl = $ttl;
}
Expand Down Expand Up @@ -140,31 +182,28 @@
$entry = ['value' => $value, 'version' => $newVersion];
$cacheKey = $this->keyFor($sessionId, $key);

$item = new Item($cacheKey, $entry, $this->ttl);
$secConfig = new SecurityConfig();
$secConfig->setEncryptionEnabled(false);
$item->setSecurityConfig($secConfig);
$this->storage->store($item);
$ttl = $this->ttlFor($sessionId);

Check warning on line 185 in WebFiori/Framework/Session/CacheSessionStorage.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename "$ttl" which has the same name as the field declared at line 35.

See more on https://sonarcloud.io/project/issues?id=WebFiori_framework&issues=AaHlk30pCCpRAKyqEfI5&open=AaHlk30pCCpRAKyqEfI5&pullRequest=436
$this->storeItem($cacheKey, $entry, $ttl);

// Update the key index.
$this->addToIndex($sessionId, $key);
$this->addToIndex($sessionId, $key, $ttl);

return $newVersion;
}

private function addToIndex(string $sessionId, string $key): void {
private function addToIndex(string $sessionId, string $key, int $ttl): void {
$keys = $this->readIndex($sessionId);

if (!in_array($key, $keys, true)) {
$keys[] = $key;
$item = new Item($this->indexKey($sessionId), $keys, $this->ttl);
$secConfig = new SecurityConfig();
$secConfig->setEncryptionEnabled(false);
$item->setSecurityConfig($secConfig);
$this->storage->store($item);
$this->storeItem($this->indexKey($sessionId), $keys, $ttl);
}
}

private function expiryKey(string $sessionId): string {
return $this->prefix.$sessionId.':_expires';
}

private function indexKey(string $sessionId): string {
return $this->prefix.$sessionId.':_keys';
}
Expand All @@ -183,10 +222,24 @@
private function removeFromIndex(string $sessionId, string $key): void {
$keys = $this->readIndex($sessionId);
$keys = array_values(array_filter($keys, fn ($k) => $k !== $key));
$item = new Item($this->indexKey($sessionId), $keys, $this->ttl);
$this->storeItem($this->indexKey($sessionId), $keys, $this->ttlFor($sessionId));
}

private function storeItem(string $cacheKey, mixed $data, int $ttl): void {
$item = new Item($cacheKey, $data, $ttl);
$secConfig = new SecurityConfig();
$secConfig->setEncryptionEnabled(false);
$item->setSecurityConfig($secConfig);
$this->storage->store($item);
}

/**
* TTL for new entries of a session: the remaining lifetime if an expiry
* is recorded, the configured default otherwise.
*/
private function ttlFor(string $sessionId): int {
$expiry = $this->getExpiry($sessionId);

return $expiry === null ? $this->ttl : max(1, $expiry - time());
}
}
16 changes: 15 additions & 1 deletion WebFiori/Framework/Session/DatabaseSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author Ibrahim
* @since 2.1.0 (original), 3.1.0 (per-key interface)
*/
class DatabaseSessionStorage implements SessionStorage {
class DatabaseSessionStorage implements ExpiringSessionStorage {
private SessionDB $dbController;

/**
Expand Down Expand Up @@ -76,6 +76,13 @@ public function getController(): SessionDB {
return $this->dbController;
}

/**
* {@inheritdoc}
*/
public function getExpiry(string $sessionId): ?int {
return $this->dbController->getExpiry($sessionId);
}

/**
* {@inheritdoc}
*/
Expand All @@ -97,6 +104,13 @@ public function remove(string $sessionId, string $key): void {
$this->dbController->removeSessionKey($sessionId, $key);
}

/**
* {@inheritdoc}
*/
public function setExpiry(string $sessionId, ?int $expiresAt): void {
$this->dbController->setExpiry($sessionId, $expiresAt);
}

/**
* {@inheritdoc}
*/
Expand Down
74 changes: 69 additions & 5 deletions WebFiori/Framework/Session/DefaultSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
* @author Ibrahim
* @since 3.1.0 (per-key interface)
*/
class DefaultSessionStorage implements SessionStorage {
class DefaultSessionStorage implements ExpiringSessionStorage {
private const EXPIRY_SUFFIX = '.exp';
private string $storeLoc;

public function __construct() {
Expand Down Expand Up @@ -68,6 +69,11 @@ public function destroy(string $sessionId): void {
if (file_exists($path)) {
unlink($path);
}
$expiryPath = $this->expiryPath($sessionId);

if (file_exists($expiryPath)) {
unlink($expiryPath);
}
}

/**
Expand All @@ -81,22 +87,57 @@ public function gc(string $olderThan, int $maxCount = 0): void {
$files = array_diff(scandir($this->storeLoc), ['.', '..']);
$removed = 0;
$threshold = strtotime($olderThan);
$now = time();

foreach ($files as $file) {
$filePath = $this->storeLoc.DS.$file;

if (substr($file, -strlen(self::EXPIRY_SUFFIX)) === self::EXPIRY_SUFFIX) {
// Remove expiry files whose session file is gone.
if (!file_exists(substr($filePath, 0, -strlen(self::EXPIRY_SUFFIX))) && file_exists($filePath)) {
unlink($filePath);
}

continue;
}

if ($maxCount > 0 && $removed >= $maxCount) {
break;
}

$filePath = $this->storeLoc.DS.$file;
$mtime = filemtime($filePath);
$expiry = $this->getExpiry($file);

if ($expiry !== null) {
$expired = $expiry <= $now;
} else {
$mtime = filemtime($filePath);
$expired = $mtime !== false && $mtime < $threshold;
}

if ($mtime !== false && $mtime < $threshold) {
unlink($filePath);
if ($expired) {
$this->destroy($file);
$removed++;
}
}
}

/**
* {@inheritdoc}
*
* The expiry is kept in a sidecar file next to the session file, because
* the modification time of the session file changes on every write.
*/
public function getExpiry(string $sessionId): ?int {
$path = $this->expiryPath($sessionId);

if (!file_exists($path)) {
return null;
}
$raw = @file_get_contents($path);

return $raw !== false && ctype_digit(trim($raw)) ? (int) trim($raw) : null;
}

/**
* Checks if the storage directory exists and is writable.
*/
Expand Down Expand Up @@ -186,6 +227,25 @@ public function remove(string $sessionId, string $key): void {
fclose($handle);
}

/**
* {@inheritdoc}
*/
public function setExpiry(string $sessionId, ?int $expiresAt): void {
if (!$this->canWrite()) {
return;
}
$path = $this->expiryPath($sessionId);

if ($expiresAt === null) {
if (file_exists($path)) {
unlink($path);
}

return;
}
file_put_contents($path, (string) $expiresAt, LOCK_EX);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -272,6 +332,10 @@ private function decode(string $raw, string $sessionId): array {
return [];
}

private function expiryPath(string $sessionId): string {
return $this->filePath($sessionId).self::EXPIRY_SUFFIX;
}

private function filePath(string $sessionId): string {
return $this->storeLoc.DS.$sessionId;
}
Expand Down
Loading
Loading