2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2018-11-11 17:38:32 +00:00
|
|
|
|
2020-02-23 18:48:25 +00:00
|
|
|
class StaticFiles extends Extension
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
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)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$theme_file = "themes/$theme_name/static/$f_pagename";
|
2020-02-23 18:48:25 +00:00
|
|
|
$static_file = "ext/static_files/static/$f_pagename";
|
2018-11-11 17:38:32 +00:00
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
2019-05-28 16:59:38 +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);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_data(file_get_contents($filename));
|
2020-05-28 15:05:20 +00:00
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
$page->set_mime(MimeType::get_for_file($filename));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 17:38:32 +00:00
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 98;
|
|
|
|
} // before 404
|
2018-11-11 17:38:32 +00:00
|
|
|
}
|