50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
const NEWS_PNG_DIRECTORY = __DIR__ . '/news/png';
|
|
|
|
function isValidNewsFileBase(string $fileBase): bool
|
|
{
|
|
return preg_match('/^\d{4}-\d{2}-\d{2}-[A-Za-z0-9_-]+$/D', $fileBase) === 1;
|
|
}
|
|
|
|
function deleteNewsFiles(string $fileBase): bool
|
|
{
|
|
if (!isValidNewsFileBase($fileBase)) {
|
|
return false;
|
|
}
|
|
|
|
$success = true;
|
|
|
|
foreach (glob(NEWS_PNG_DIRECTORY . '/' . $fileBase . '_page_*.png') ?: [] as $file) {
|
|
if (!unlink($file)) {
|
|
$success = false;
|
|
}
|
|
}
|
|
|
|
return $success;
|
|
}
|
|
|
|
function cleanupExpiredNews(): void
|
|
{
|
|
$today = new DateTimeImmutable('today');
|
|
|
|
foreach (glob(NEWS_PNG_DIRECTORY . '/*_page_1.png') ?: [] as $file) {
|
|
$fileBase = basename($file, '_page_1.png');
|
|
|
|
if (!isValidNewsFileBase($fileBase)) {
|
|
continue;
|
|
}
|
|
|
|
$dateString = substr($fileBase, 0, 10);
|
|
$expiryDate = DateTimeImmutable::createFromFormat('!Y-m-d', $dateString);
|
|
|
|
if (
|
|
$expiryDate !== false &&
|
|
$expiryDate->format('Y-m-d') === $dateString &&
|
|
$today > $expiryDate
|
|
) {
|
|
deleteNewsFiles($fileBase);
|
|
}
|
|
}
|
|
}
|