Commit b824f1cf authored by Rolf Forst's avatar Rolf Forst
Browse files

Fix script logic; add namespace

parent b0aeae64
<?php
if (!isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] != 'GET') {
return false;
namespace RRZE\Cache;
defined('ABSPATH') || exit;
if (!skipCache()) {
getCache();
}
if (!empty($_COOKIE)) {
foreach ($_COOKIE as $k => $v) {
if (preg_match('/^(wp-postpass|wordpress_logged_in|comment_author)_/', $k)) {
return false;
function skipCache(): bool
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != 'GET') {
return true;
}
if (!empty($_SERVER['QUERY_STRING'])) {
return true;
}
if (!empty($_COOKIE)) {
foreach ($_COOKIE as $k => $v) {
if (preg_match('/^(wp-postpass|wordpress_logged_in|comment_author)_/', $k)) {
return true;
}
}
}
return false;
}
$timeout_file = WP_CONTENT_DIR . '/advanced-cache/timeout';
$expires_seconds = 10 * MINUTE_IN_SECONDS;
function getCache()
{
$timeout_file = WP_CONTENT_DIR . '/advanced-cache/timeout';
$expires_seconds = 10 * MINUTE_IN_SECONDS;
if (is_readable($timeout_file) && filesize($timeout_file) > 0) {
$expires_seconds = abs(intval(file_get_contents($timeout_file))) * MINUTE_IN_SECONDS;
}
if (is_readable($timeout_file) && filesize($timeout_file) > 0) {
$expires_seconds = abs(intval(file_get_contents($timeout_file))) * MINUTE_IN_SECONDS;
}
$expires_seconds = $expires_seconds >= (1 * MINUTE_IN_SECONDS) ? $expires_seconds : (10 * MINUTE_IN_SECONDS);
$path = WP_CONTENT_DIR . '/advanced-cache' .
DIRECTORY_SEPARATOR .
parse_url('https://' . strtolower($_SERVER['HTTP_HOST']), PHP_URL_HOST) .
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = rtrim($path, '/\\') . '/';
$cache_file = $path . 'index.html';
// Check if the cache file exists and is readable.
if (is_readable($cache_file) && ($content = file_get_contents($cache_file)) !== false && (filesize($cache_file) > 0)) {
// Check if the cache file is not stale.
if (($filemtime = filemtime($cache_file) + $expires_seconds) > time()) {
// Not stale: Output the content of the cache file.
header('Last-Modified: ' . date('r', $filemtime), true);
header('Cache-Control: max-age=' . $expires_seconds . ', must-revalidate', true);
echo $content;
exit;
} else {
// Stale: Remove the cache file.
unlink($cache_file);
$expires_seconds = $expires_seconds >= (1 * MINUTE_IN_SECONDS) ? $expires_seconds : (10 * MINUTE_IN_SECONDS);
$path = WP_CONTENT_DIR . '/advanced-cache' .
DIRECTORY_SEPARATOR .
parse_url('https://' . strtolower($_SERVER['HTTP_HOST']), PHP_URL_HOST) .
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = rtrim($path, '/\\') . '/';
$cache_file = $path . 'index.html';
// Check if the cache file exists and is readable.
if (is_readable($cache_file) && ($content = file_get_contents($cache_file)) !== false && (filesize($cache_file) > 0)) {
// Check if the cache file is not stale.
if (($filemtime = filemtime($cache_file) + $expires_seconds) > time()) {
// Not stale: Output the content of the cache file.
header('Last-Modified: ' . date('r', $filemtime), true);
header('Cache-Control: max-age=' . $expires_seconds . ', must-revalidate', true);
echo $content;
exit;
} else {
// Stale: Remove the cache file.
unlink($cache_file);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment