317 lines
9.4 KiB
PHP
317 lines
9.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Page cache
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
function cache_should_bypass(array $config): bool
|
||
|
|
{
|
||
|
|
if (empty($config['cache']['enabled'])) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'GET') {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if (isset($_GET['q'])) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
$hasSession = !empty($_COOKIE[session_name()]);
|
||
|
|
$hasRememberMe = !empty($_COOKIE[get_remember_me_cookie_name()]);
|
||
|
|
|
||
|
|
if ($hasSession || $hasRememberMe) {
|
||
|
|
start_admin_session();
|
||
|
|
maybe_restore_admin_from_cookie();
|
||
|
|
if (is_admin_logged_in()) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
function get_cache_file_path(string $key, string $ext = 'html'): string
|
||
|
|
{
|
||
|
|
return PUREBLOG_CACHE_PATH . '/' . md5($key) . '.' . $ext;
|
||
|
|
}
|
||
|
|
|
||
|
|
function cache_read(string $key, int $ttl = 0, string $ext = 'html'): ?string
|
||
|
|
{
|
||
|
|
$path = get_cache_file_path($key, $ext);
|
||
|
|
if (!is_file($path)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
if ($ttl > 0 && (time() - filemtime($path)) > $ttl) {
|
||
|
|
@unlink($path);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
$content = file_get_contents($path);
|
||
|
|
return $content !== false ? $content : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
function cache_write(string $key, string $content, string $ext = 'html'): void
|
||
|
|
{
|
||
|
|
if (!is_dir(PUREBLOG_CACHE_PATH)) {
|
||
|
|
mkdir(PUREBLOG_CACHE_PATH, 0755, true);
|
||
|
|
}
|
||
|
|
$timestamp = gmdate('Y-m-d H:i:s') . ' UTC';
|
||
|
|
if ($ext === 'xml') {
|
||
|
|
$content = str_replace('</rss>', '<!-- Cached at ' . $timestamp . " -->\n</rss>", $content);
|
||
|
|
} else {
|
||
|
|
$content .= "\n<!-- Cached at " . $timestamp . ' -->';
|
||
|
|
}
|
||
|
|
file_put_contents(get_cache_file_path($key, $ext), $content, LOCK_EX);
|
||
|
|
}
|
||
|
|
|
||
|
|
function cache_clear(): void
|
||
|
|
{
|
||
|
|
if (!is_dir(PUREBLOG_CACHE_PATH)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
$files = glob(PUREBLOG_CACHE_PATH . '/*') ?: [];
|
||
|
|
foreach ($files as $file) {
|
||
|
|
if (is_file($file)) {
|
||
|
|
@unlink($file);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$ogDir = PUREBLOG_CACHE_PATH . '/og';
|
||
|
|
if (is_dir($ogDir)) {
|
||
|
|
$ogFiles = glob($ogDir . '/*') ?: [];
|
||
|
|
foreach ($ogFiles as $file) {
|
||
|
|
if (is_file($file)) {
|
||
|
|
@unlink($file);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Version helpers
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
function detect_current_pureblog_version(): string
|
||
|
|
{
|
||
|
|
$versionFile = PUREBLOG_BASE_PATH . '/VERSION';
|
||
|
|
if (is_file($versionFile)) {
|
||
|
|
$raw = @file_get_contents($versionFile);
|
||
|
|
if (is_string($raw)) {
|
||
|
|
$fromFile = trim($raw);
|
||
|
|
if ($fromFile !== '') {
|
||
|
|
return $fromFile;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (defined('PUREBLOG_VERSION') && is_string(PUREBLOG_VERSION) && PUREBLOG_VERSION !== '' && strtolower(PUREBLOG_VERSION) !== 'unknown') {
|
||
|
|
return PUREBLOG_VERSION;
|
||
|
|
}
|
||
|
|
|
||
|
|
return 'unknown';
|
||
|
|
}
|
||
|
|
|
||
|
|
function normalize_version_label(string $version): string
|
||
|
|
{
|
||
|
|
$trimmed = trim($version);
|
||
|
|
if ($trimmed === '') {
|
||
|
|
return 'unknown';
|
||
|
|
}
|
||
|
|
|
||
|
|
return ltrim($trimmed, "vV");
|
||
|
|
}
|
||
|
|
|
||
|
|
function versions_match(string $current, string $latest): bool
|
||
|
|
{
|
||
|
|
$a = strtolower(trim($current));
|
||
|
|
$b = strtolower(trim($latest));
|
||
|
|
|
||
|
|
if ($a === '' || $b === '') {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$a = ltrim($a, 'v');
|
||
|
|
$b = ltrim($b, 'v');
|
||
|
|
|
||
|
|
return $a === $b;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// HTTP helpers
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
function pureblog_http_get(string $url, int $timeout = 5, array $headers = []): array
|
||
|
|
{
|
||
|
|
if (function_exists('curl_init')) {
|
||
|
|
$ch = curl_init($url);
|
||
|
|
if ($ch === false) {
|
||
|
|
return ['ok' => false, 'error' => 'curl_init failed'];
|
||
|
|
}
|
||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||
|
|
if ($headers) {
|
||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||
|
|
}
|
||
|
|
$raw = curl_exec($ch);
|
||
|
|
$status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||
|
|
$err = curl_error($ch);
|
||
|
|
|
||
|
|
if (!is_string($raw) || $status < 200 || $status >= 300) {
|
||
|
|
return ['ok' => false, 'error' => $err !== '' ? $err : "HTTP {$status}"];
|
||
|
|
}
|
||
|
|
return ['ok' => true, 'body' => $raw];
|
||
|
|
}
|
||
|
|
|
||
|
|
$ctx = stream_context_create(['http' => [
|
||
|
|
'method' => 'GET',
|
||
|
|
'timeout' => $timeout,
|
||
|
|
'header' => implode("\r\n", $headers),
|
||
|
|
'ignore_errors' => true,
|
||
|
|
]]);
|
||
|
|
$raw = @file_get_contents($url, false, $ctx);
|
||
|
|
if (!is_string($raw)) {
|
||
|
|
return ['ok' => false, 'error' => 'Network request failed'];
|
||
|
|
}
|
||
|
|
return ['ok' => true, 'body' => $raw];
|
||
|
|
}
|
||
|
|
|
||
|
|
function pureblog_http_download(string $url, string $destination, int $timeout = 20, array $headers = []): ?string
|
||
|
|
{
|
||
|
|
if (function_exists('curl_init')) {
|
||
|
|
$ch = curl_init($url);
|
||
|
|
if ($ch === false) {
|
||
|
|
return 'curl_init failed';
|
||
|
|
}
|
||
|
|
$fp = @fopen($destination, 'wb');
|
||
|
|
if ($fp === false) {
|
||
|
|
return 'Could not open destination file for writing';
|
||
|
|
}
|
||
|
|
curl_setopt($ch, CURLOPT_FILE, $fp);
|
||
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
||
|
|
if ($headers) {
|
||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||
|
|
}
|
||
|
|
$ok = curl_exec($ch);
|
||
|
|
$status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||
|
|
$err = curl_error($ch);
|
||
|
|
fclose($fp);
|
||
|
|
|
||
|
|
if ($ok !== true || $status < 200 || $status >= 300) {
|
||
|
|
return $err !== '' ? $err : "HTTP {$status}";
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
$ctx = stream_context_create(['http' => [
|
||
|
|
'method' => 'GET',
|
||
|
|
'timeout' => $timeout,
|
||
|
|
'header' => implode("\r\n", $headers),
|
||
|
|
]]);
|
||
|
|
$raw = @file_get_contents($url, false, $ctx);
|
||
|
|
if (!is_string($raw)) {
|
||
|
|
return 'Network request failed';
|
||
|
|
}
|
||
|
|
if (@file_put_contents($destination, $raw) === false) {
|
||
|
|
return 'Could not write to destination file';
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Backup & file helpers
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return list<string>
|
||
|
|
*/
|
||
|
|
function collect_relative_files(string $root): array
|
||
|
|
{
|
||
|
|
$files = [];
|
||
|
|
$iterator = new RecursiveIteratorIterator(
|
||
|
|
new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS)
|
||
|
|
);
|
||
|
|
|
||
|
|
foreach ($iterator as $item) {
|
||
|
|
if (!$item->isFile()) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$fullPath = str_replace('\\', '/', $item->getPathname());
|
||
|
|
$prefix = rtrim(str_replace('\\', '/', $root), '/') . '/';
|
||
|
|
if (!str_starts_with($fullPath, $prefix)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$relative = substr($fullPath, strlen($prefix));
|
||
|
|
if ($relative === '' || str_starts_with($relative, '.git/')) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$files[] = $relative;
|
||
|
|
}
|
||
|
|
|
||
|
|
sort($files);
|
||
|
|
return $files;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a full-site zip archive in the system temp directory (excluding /cache, /backup, and /.git).
|
||
|
|
*
|
||
|
|
* @return array{ok:bool,path?:string,filename?:string,error?:string}
|
||
|
|
*/
|
||
|
|
function create_site_backup_zip(): array
|
||
|
|
{
|
||
|
|
if (!class_exists('ZipArchive')) {
|
||
|
|
return ['ok' => false, 'error' => t('admin.settings.updates.error_ziparchive')];
|
||
|
|
}
|
||
|
|
|
||
|
|
$tmpZip = rtrim(sys_get_temp_dir(), '/') . '/pureblog-site-backup-' . bin2hex(random_bytes(8)) . '.zip';
|
||
|
|
$zip = new ZipArchive();
|
||
|
|
if ($zip->open($tmpZip, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||
|
|
return ['ok' => false, 'error' => t('admin.settings.site.error_backup_zip')];
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$files = collect_relative_files(PUREBLOG_BASE_PATH);
|
||
|
|
$excludedTop = ['cache' => true, 'backup' => true];
|
||
|
|
$added = 0;
|
||
|
|
|
||
|
|
foreach ($files as $relative) {
|
||
|
|
$top = (string) strtok($relative, '/');
|
||
|
|
if (isset($excludedTop[$top])) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$fullPath = PUREBLOG_BASE_PATH . '/' . $relative;
|
||
|
|
if (!is_readable($fullPath)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (!$zip->addFile($fullPath, $relative)) {
|
||
|
|
$zip->close();
|
||
|
|
@unlink($tmpZip);
|
||
|
|
return ['ok' => false, 'error' => t('admin.settings.site.error_backup_zip')];
|
||
|
|
}
|
||
|
|
$added++;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!$zip->close() || $added === 0 || !is_file($tmpZip)) {
|
||
|
|
@unlink($tmpZip);
|
||
|
|
return ['ok' => false, 'error' => t('admin.settings.site.error_backup_zip')];
|
||
|
|
}
|
||
|
|
|
||
|
|
$versionRaw = detect_current_pureblog_version();
|
||
|
|
$versionSlug = preg_replace('/[^a-zA-Z0-9._-]+/', '-', $versionRaw);
|
||
|
|
if (!is_string($versionSlug) || $versionSlug === '') {
|
||
|
|
$versionSlug = 'unknown';
|
||
|
|
}
|
||
|
|
$filename = 'pureblog-backup-' . date('Ymd-His') . '-' . $versionSlug . '.zip';
|
||
|
|
|
||
|
|
return [
|
||
|
|
'ok' => true,
|
||
|
|
'path' => $tmpZip,
|
||
|
|
'filename' => $filename,
|
||
|
|
];
|
||
|
|
} catch (Throwable $e) {
|
||
|
|
@$zip->close();
|
||
|
|
@unlink($tmpZip);
|
||
|
|
return ['ok' => false, 'error' => t('admin.settings.site.error_backup_zip')];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|