This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/static_files/main.php

53 lines
1.8 KiB
PHP
Raw Normal View History

2020-01-26 13:19:35 +00:00
<?php declare(strict_types=1);
2018-11-11 17:38:32 +00:00
class StaticFiles extends Extension
{
public function onPageRequest(PageRequestEvent $event)
{
global $config, $page;
// hax.
2019-06-19 01:58:28 +00:00
if ($page->mode == PageMode::PAGE && (!isset($page->blocks) || $this->count_main($page->blocks) == 0)) {
$h_pagename = html_escape(implode('/', $event->args));
$f_pagename = preg_replace("/[^a-z_\-\.]+/", "_", $h_pagename);
2019-08-02 19:40:03 +00:00
$theme_name = $config->get_string(SetupConfig::THEME, "default");
2018-11-11 17:38:32 +00:00
$theme_file = "themes/$theme_name/static/$f_pagename";
$static_file = "ext/static_files/static/$f_pagename";
2018-11-11 17:38:32 +00:00
if (file_exists($theme_file) || file_exists($static_file)) {
$filename = file_exists($theme_file) ? $theme_file : $static_file;
2018-11-11 17:38:32 +00:00
$page->add_http_header("Cache-control: public, max-age=600");
$page->add_http_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT');
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::DATA);
$page->set_data(file_get_contents($filename));
if (endsWith($filename, ".ico")) {
$page->set_type("image/x-icon");
}
if (endsWith($filename, ".png")) {
$page->set_type("image/png");
}
if (endsWith($filename, ".txt")) {
$page->set_type("text/plain");
}
}
}
}
2018-11-11 17:38:32 +00:00
private function count_main($blocks)
{
$n = 0;
foreach ($blocks as $block) {
if ($block->section == "main" && $block->is_content) {
$n++;
} // more hax.
}
return $n;
}
2018-11-11 17:38:32 +00:00
public function get_priority(): int
{
return 98;
} // before 404
2018-11-11 17:38:32 +00:00
}