PHP Classes

File: src/static-cache.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   CMS Airship   src/static-cache.php   Download  
File: src/static-cache.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CMS Airship
Content management system with security features
Author: By
Last change: Repeat ourselves less.
Date: 6 years ago
Size: 1,853 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
Airship\Engine\{
   
Cache\File as FileCache,
   
Cache\SharedMemory as MemoryCache
};
use
ParagonIE\ConstantTime\Binary;

if (empty(
$_POST)) {
   
/**
     * Let's get rid of trailing slashes in URLs without POST data
     */
   
$sliceAt = Binary::safeStrlen($_SERVER['REQUEST_URI']) - 1;
    if (
$sliceAt > 0 && $_SERVER['REQUEST_URI'][$sliceAt] === '/') {
        \
Airship\redirect(
           
'/' . \trim($_SERVER['REQUEST_URI'], '/')
        );
    }

   
/**
     * Let's handle static content caching
     */
   
if (\extension_loaded('apcu')) {
       
$staticCache = (new MemoryCache())
            ->
personalize('staticPage:');
       
$cspCache = (new MemoryCache())
            ->
personalize('contentSecurityPolicy:');
    } else {
        if (!\
is_dir(ROOT . '/tmp/cache/static')) {
            require_once
ROOT . '/tmp_dirs.php';
        }
       
$staticCache = new FileCache(ROOT . '/tmp/cache/static');
       
$cspCache = new FileCache(ROOT . '/tmp/cache/csp_static');
    }
   
$port = $_SERVER['HTTP_PORT'] ?? '';
   
$lookup = $_SERVER['HTTP_HOST'] . ':' . $port . '/' . $_SERVER['REQUEST_URI'];
   
$staticPage = $staticCache->get($lookup);
    if (!empty(
$staticPage)) {
        if (!\
headers_sent()) {
            foreach (\
Airship\get_standard_headers('text/plain;charset=UTF-8') as $left => $right) {
                \
header($left . ': ' . $right);
            }
        }
       
$csp = $cspCache->get($lookup);
        if (!empty(
$csp)) {
            foreach (\
json_decode($csp, true) as $cspHeader) {
                \
header($cspHeader);
            }
        }

        echo
$staticPage;
       
// This is just for benchmarking purposes:
       
echo '<!-- Load time: ' . \round(\microtime(true) - $start, 5) . ' s (static page) -->';
        exit;
    }
    unset(
$staticCache);
}