2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2023-07-03 15:03:34 +00:00
|
|
|
use MicroHTML\HTMLElement;
|
|
|
|
|
2024-01-04 13:57:45 +00:00
|
|
|
use function MicroHTML\{emptyHTML,rawHTML,HTML,HEAD,BODY};
|
|
|
|
|
2020-01-27 18:35:36 +00:00
|
|
|
require_once "core/event.php";
|
|
|
|
|
2022-10-28 00:45:35 +00:00
|
|
|
enum PageMode: string
|
2019-06-21 08:12:44 +00:00
|
|
|
{
|
2022-10-28 00:45:35 +00:00
|
|
|
case REDIRECT = 'redirect';
|
|
|
|
case DATA = 'data';
|
|
|
|
case PAGE = 'page';
|
|
|
|
case FILE = 'file';
|
|
|
|
case MANUAL = 'manual';
|
2019-06-19 01:58:28 +00:00
|
|
|
}
|
2009-07-21 03:18:40 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-29 05:33:03 +00:00
|
|
|
* Class Page
|
|
|
|
*
|
2009-07-19 07:38:13 +00:00
|
|
|
* A data structure for holding all the bits of data that make up a page.
|
|
|
|
*
|
|
|
|
* The various extensions all add whatever they want to this structure,
|
2014-04-29 05:33:03 +00:00
|
|
|
* then Layout turns it into HTML.
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2020-01-27 18:35:36 +00:00
|
|
|
class BasePage
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2022-10-28 00:45:35 +00:00
|
|
|
public PageMode $mode = PageMode::PAGE;
|
2021-03-14 23:43:50 +00:00
|
|
|
private string $mime;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set what this page should do; "page", "data", or "redirect".
|
|
|
|
*/
|
2022-10-28 00:45:35 +00:00
|
|
|
public function set_mode(PageMode $mode): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->mode = $mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the page's MIME type.
|
|
|
|
*/
|
2020-06-14 16:05:55 +00:00
|
|
|
public function set_mime(string $mime): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-06-14 16:05:55 +00:00
|
|
|
$this->mime = $mime;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 00:06:04 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2020-06-14 16:05:55 +00:00
|
|
|
$this->mime = MimeType::add_parameters(MimeType::HTML, MimeType::CHARSET_UTF8);
|
2019-12-16 00:06:04 +00:00
|
|
|
if (@$_GET["flash"]) {
|
|
|
|
$this->flash[] = $_GET['flash'];
|
|
|
|
unset($_GET["flash"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// ==============================================
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $data = ""; // public only for unit test
|
|
|
|
private ?string $file = null;
|
|
|
|
private bool $file_delete = false;
|
|
|
|
private ?string $filename = null;
|
|
|
|
private ?string $disposition = null;
|
2019-06-25 18:50:52 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Set the raw data to be sent.
|
|
|
|
*/
|
2019-05-29 17:23:29 +00:00
|
|
|
public function set_data(string $data): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
2020-02-07 22:05:27 +00:00
|
|
|
public function set_file(string $file, bool $delete = false): void
|
2019-06-25 18:50:52 +00:00
|
|
|
{
|
|
|
|
$this->file = $file;
|
2020-02-07 22:05:27 +00:00
|
|
|
$this->file_delete = $delete;
|
2019-06-25 18:50:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Set the recommended download filename.
|
|
|
|
*/
|
2019-06-25 18:50:52 +00:00
|
|
|
public function set_filename(string $filename, string $disposition = "attachment"): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-07-03 14:09:38 +00:00
|
|
|
$max_len = 250;
|
|
|
|
if(strlen($filename) > $max_len) {
|
|
|
|
// remove extension, truncate filename, apply extension
|
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION);
|
|
|
|
$filename = substr($filename, 0, $max_len - strlen($ext) - 1) . '.' . $ext;
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->filename = $filename;
|
2019-06-25 18:50:52 +00:00
|
|
|
$this->disposition = $disposition;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ==============================================
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $redirect = "";
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the URL to redirect to (remember to use make_link() if linking
|
|
|
|
* to a page in the same site).
|
|
|
|
*/
|
2019-05-29 17:23:29 +00:00
|
|
|
public function set_redirect(string $redirect): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->redirect = $redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==============================================
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public int $code = 200;
|
|
|
|
public string $title = "";
|
|
|
|
public string $heading = "";
|
|
|
|
public string $subheading = "";
|
2023-12-15 21:23:26 +00:00
|
|
|
public bool $left_enabled = true;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/** @var string[] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $html_headers = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/** @var string[] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $http_headers = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/** @var string[][] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $cookies = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/** @var Block[] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $blocks = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-12-15 19:47:18 +00:00
|
|
|
/** @var string[] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $flash = [];
|
2019-12-15 19:47:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Set the HTTP status code
|
|
|
|
*/
|
|
|
|
public function set_code(int $code): void
|
|
|
|
{
|
|
|
|
$this->code = $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set_title(string $title): void
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set_heading(string $heading): void
|
|
|
|
{
|
|
|
|
$this->heading = $heading;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set_subheading(string $subheading): void
|
|
|
|
{
|
|
|
|
$this->subheading = $subheading;
|
|
|
|
}
|
|
|
|
|
2019-12-15 19:47:18 +00:00
|
|
|
public function flash(string $message): void
|
|
|
|
{
|
|
|
|
$this->flash[] = $message;
|
|
|
|
}
|
|
|
|
|
2023-12-15 21:23:26 +00:00
|
|
|
public function disable_left()
|
|
|
|
{
|
|
|
|
$this->left_enabled = false;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Add a line to the HTML head section.
|
|
|
|
*/
|
2019-06-25 18:50:52 +00:00
|
|
|
public function add_html_header(string $line, int $position = 50): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
while (isset($this->html_headers[$position])) {
|
|
|
|
$position++;
|
|
|
|
}
|
|
|
|
$this->html_headers[$position] = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a http header to be sent to the client.
|
|
|
|
*/
|
2019-06-25 18:50:52 +00:00
|
|
|
public function add_http_header(string $line, int $position = 50): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
while (isset($this->http_headers[$position])) {
|
|
|
|
$position++;
|
|
|
|
}
|
|
|
|
$this->http_headers[$position] = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The counterpart for get_cookie, this works like php's
|
|
|
|
* setcookie method, but prepends the site-wide cookie prefix to
|
|
|
|
* the $name argument before doing anything.
|
|
|
|
*/
|
|
|
|
public function add_cookie(string $name, string $value, int $time, string $path): void
|
|
|
|
{
|
2019-06-25 18:50:52 +00:00
|
|
|
$full_name = COOKIE_PREFIX . "_" . $name;
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->cookies[] = [$full_name, $value, $time, $path];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_cookie(string $name): ?string
|
|
|
|
{
|
2019-06-25 18:50:52 +00:00
|
|
|
$full_name = COOKIE_PREFIX . "_" . $name;
|
2019-05-28 16:59:38 +00:00
|
|
|
if (isset($_COOKIE[$full_name])) {
|
|
|
|
return $_COOKIE[$full_name];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the HTML headers that are currently set and return as a string.
|
|
|
|
*/
|
|
|
|
public function get_all_html_headers(): string
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
ksort($this->html_headers);
|
|
|
|
foreach ($this->html_headers as $line) {
|
|
|
|
$data .= "\t\t" . $line . "\n";
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a Block of data to the page.
|
|
|
|
*/
|
2019-05-29 17:23:29 +00:00
|
|
|
public function add_block(Block $block): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->blocks[] = $block;
|
|
|
|
}
|
|
|
|
|
2020-01-28 21:19:59 +00:00
|
|
|
/**
|
|
|
|
* Find a block which contains the given text
|
|
|
|
* (Useful for unit tests)
|
|
|
|
*/
|
2020-01-29 00:49:21 +00:00
|
|
|
public function find_block(string $text): ?Block
|
|
|
|
{
|
|
|
|
foreach ($this->blocks as $block) {
|
|
|
|
if ($block->header == $text) {
|
2020-01-28 21:19:59 +00:00
|
|
|
return $block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// ==============================================
|
|
|
|
|
2020-05-28 14:18:09 +00:00
|
|
|
public function send_headers(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (!headers_sent()) {
|
2023-02-07 13:56:44 +00:00
|
|
|
header("HTTP/1.1 {$this->code} Shimmie");
|
2020-06-14 16:05:55 +00:00
|
|
|
header("Content-type: " . $this->mime);
|
2020-05-28 14:18:09 +00:00
|
|
|
header("X-Powered-By: Shimmie-" . VERSION);
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
foreach ($this->http_headers as $head) {
|
|
|
|
header($head);
|
|
|
|
}
|
|
|
|
foreach ($this->cookies as $c) {
|
|
|
|
setcookie($c[0], $c[1], $c[2], $c[3]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print "Error: Headers have already been sent to the client.";
|
|
|
|
}
|
2020-05-28 14:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the page according to the mode and data given.
|
|
|
|
*/
|
|
|
|
public function display(): void
|
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($this->mode != PageMode::MANUAL) {
|
2020-05-28 14:18:09 +00:00
|
|
|
$this->send_headers();
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
switch ($this->mode) {
|
2020-05-28 14:18:09 +00:00
|
|
|
case PageMode::MANUAL:
|
|
|
|
break;
|
2019-06-19 01:58:28 +00:00
|
|
|
case PageMode::PAGE:
|
2023-01-10 22:44:09 +00:00
|
|
|
usort($this->blocks, "Shimmie2\blockcmp");
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->add_auto_html_headers();
|
2020-02-01 23:23:23 +00:00
|
|
|
$this->render();
|
2019-05-28 16:59:38 +00:00
|
|
|
break;
|
2019-06-19 01:58:28 +00:00
|
|
|
case PageMode::DATA:
|
2019-06-25 18:50:52 +00:00
|
|
|
header("Content-Length: " . strlen($this->data));
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!is_null($this->filename)) {
|
2019-06-25 18:50:52 +00:00
|
|
|
header('Content-Disposition: ' . $this->disposition . '; filename=' . $this->filename);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
print $this->data;
|
|
|
|
break;
|
2019-06-25 18:50:52 +00:00
|
|
|
case PageMode::FILE:
|
|
|
|
if (!is_null($this->filename)) {
|
|
|
|
header('Content-Disposition: ' . $this->disposition . '; filename=' . $this->filename);
|
|
|
|
}
|
2023-01-11 10:12:19 +00:00
|
|
|
assert($this->file, "file should not be null with PageMode::FILE");
|
2019-06-25 18:50:52 +00:00
|
|
|
|
2020-02-01 21:20:32 +00:00
|
|
|
// https://gist.github.com/codler/3906826
|
2019-06-25 18:50:52 +00:00
|
|
|
$size = filesize($this->file); // File size
|
|
|
|
$length = $size; // Content length
|
|
|
|
$start = 0; // Start byte
|
|
|
|
$end = $size - 1; // End byte
|
|
|
|
|
2020-01-26 22:58:59 +00:00
|
|
|
header("Content-Length: " . $size);
|
2019-06-25 18:50:52 +00:00
|
|
|
header('Accept-Ranges: bytes');
|
|
|
|
|
|
|
|
if (isset($_SERVER['HTTP_RANGE'])) {
|
|
|
|
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
|
2020-10-25 19:31:58 +00:00
|
|
|
if (str_contains($range, ',')) {
|
2019-06-25 18:50:52 +00:00
|
|
|
header('HTTP/1.1 416 Requested Range Not Satisfiable');
|
|
|
|
header("Content-Range: bytes $start-$end/$size");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($range == '-') {
|
2020-02-01 11:58:05 +00:00
|
|
|
$c_start = $size - (int)substr($range, 1);
|
2020-02-01 21:20:32 +00:00
|
|
|
$c_end = $end;
|
2019-06-25 18:50:52 +00:00
|
|
|
} else {
|
|
|
|
$range = explode('-', $range);
|
2020-02-01 11:58:05 +00:00
|
|
|
$c_start = (int)$range[0];
|
|
|
|
$c_end = (isset($range[1]) && is_numeric($range[1])) ? (int)$range[1] : $size;
|
2019-06-25 18:50:52 +00:00
|
|
|
}
|
|
|
|
$c_end = ($c_end > $end) ? $end : $c_end;
|
|
|
|
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
|
|
|
|
header('HTTP/1.1 416 Requested Range Not Satisfiable');
|
|
|
|
header("Content-Range: bytes $start-$end/$size");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$start = $c_start;
|
|
|
|
$end = $c_end;
|
|
|
|
$length = $end - $start + 1;
|
|
|
|
header('HTTP/1.1 206 Partial Content');
|
|
|
|
}
|
|
|
|
header("Content-Range: bytes $start-$end/$size");
|
|
|
|
header("Content-Length: " . $length);
|
|
|
|
|
2020-02-07 22:05:27 +00:00
|
|
|
try {
|
|
|
|
stream_file($this->file, $start, $end);
|
|
|
|
} finally {
|
|
|
|
if ($this->file_delete === true) {
|
|
|
|
unlink($this->file);
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 18:50:52 +00:00
|
|
|
break;
|
2019-06-19 01:58:28 +00:00
|
|
|
case PageMode::REDIRECT:
|
2019-12-15 19:47:18 +00:00
|
|
|
if ($this->flash) {
|
2020-10-25 19:31:58 +00:00
|
|
|
$this->redirect .= str_contains($this->redirect, "?") ? "&" : "?";
|
2019-12-15 19:47:18 +00:00
|
|
|
$this->redirect .= "flash=" . url_escape(implode("\n", $this->flash));
|
|
|
|
}
|
2019-06-25 18:50:52 +00:00
|
|
|
header('Location: ' . $this->redirect);
|
|
|
|
print 'You should be redirected to <a href="' . $this->redirect . '">' . $this->redirect . '</a>';
|
2019-05-28 16:59:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print "Invalid page mode";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function grabs all the CSS and JavaScript files sprinkled throughout Shimmie's folders,
|
|
|
|
* concatenates them together into two large files (one for CSS and one for JS) and then stores
|
|
|
|
* them in the /cache/ directory for serving to the user.
|
|
|
|
*
|
|
|
|
* Why do this? Two reasons:
|
|
|
|
* 1. Reduces the number of files the user's browser needs to download.
|
|
|
|
* 2. Allows these cached files to be compressed/minified by the admin.
|
|
|
|
*/
|
|
|
|
public function add_auto_html_headers(): void
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$data_href = get_base_href();
|
2019-08-02 19:40:03 +00:00
|
|
|
$theme_name = $config->get_string(SetupConfig::THEME, 'default');
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$this->add_html_header("<script type='text/javascript'>base_href = '$data_href';</script>", 40);
|
|
|
|
|
2020-02-23 18:48:25 +00:00
|
|
|
# static handler will map these to themes/foo/static/bar.ico or ext/static_files/static/bar.ico
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->add_html_header("<link rel='icon' type='image/x-icon' href='$data_href/favicon.ico'>", 41);
|
|
|
|
$this->add_html_header("<link rel='apple-touch-icon' href='$data_href/apple-touch-icon.png'>", 42);
|
|
|
|
|
|
|
|
//We use $config_latest to make sure cache is reset if config is ever updated.
|
|
|
|
$config_latest = 0;
|
|
|
|
foreach (zglob("data/config/*") as $conf) {
|
|
|
|
$config_latest = max($config_latest, filemtime($conf));
|
|
|
|
}
|
|
|
|
|
2023-08-18 13:15:44 +00:00
|
|
|
$css_cache_file = $this->get_css_cache_file($theme_name, $config_latest);
|
|
|
|
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css_cache_file' type='text/css'>", 43);
|
|
|
|
|
2024-01-01 20:03:50 +00:00
|
|
|
$initjs_cache_file = $this->get_initjs_cache_file($theme_name, $config_latest);
|
|
|
|
$this->add_html_header("<script src='$data_href/$initjs_cache_file' type='text/javascript'></script>", 44);
|
|
|
|
|
2023-08-18 13:15:44 +00:00
|
|
|
$js_cache_file = $this->get_js_cache_file($theme_name, $config_latest);
|
|
|
|
$this->add_html_header("<script defer src='$data_href/$js_cache_file' type='text/javascript'></script>", 44);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_css_cache_file(string $theme_name, int $config_latest): string
|
|
|
|
{
|
2019-05-28 16:59:38 +00:00
|
|
|
$css_latest = $config_latest;
|
|
|
|
$css_files = array_merge(
|
2019-08-07 19:53:59 +00:00
|
|
|
zglob("ext/{" . Extension::get_enabled_extensions_as_string() . "}/style.css"),
|
2021-05-05 16:40:25 +00:00
|
|
|
zglob("themes/$theme_name/{" . implode(",", $this->get_theme_stylesheets()) . "}")
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
foreach ($css_files as $css) {
|
|
|
|
$css_latest = max($css_latest, filemtime($css));
|
|
|
|
}
|
|
|
|
$css_md5 = md5(serialize($css_files));
|
|
|
|
$css_cache_file = data_path("cache/style/{$theme_name}.{$css_latest}.{$css_md5}.css");
|
|
|
|
if (!file_exists($css_cache_file)) {
|
2024-01-01 15:00:54 +00:00
|
|
|
$mcss = new \MicroBundler\MicroBundler();
|
|
|
|
foreach($css_files as $css) {
|
|
|
|
$mcss->addSource($css, file_get_contents($css));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-01-01 15:00:54 +00:00
|
|
|
$mcss->save($css_cache_file);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 13:15:44 +00:00
|
|
|
return $css_cache_file;
|
|
|
|
}
|
|
|
|
|
2024-01-01 20:03:50 +00:00
|
|
|
private function get_initjs_cache_file(string $theme_name, int $config_latest): string
|
|
|
|
{
|
|
|
|
$js_latest = $config_latest;
|
|
|
|
$js_files = array_merge(
|
|
|
|
zglob("ext/{" . Extension::get_enabled_extensions_as_string() . "}/init.js"),
|
|
|
|
zglob("themes/$theme_name/init.js")
|
|
|
|
);
|
|
|
|
foreach ($js_files as $js) {
|
|
|
|
$js_latest = max($js_latest, filemtime($js));
|
|
|
|
}
|
|
|
|
$js_md5 = md5(serialize($js_files));
|
|
|
|
$js_cache_file = data_path("cache/initscript/{$theme_name}.{$js_latest}.{$js_md5}.js");
|
|
|
|
if (!file_exists($js_cache_file)) {
|
|
|
|
$mcss = new \MicroBundler\MicroBundler();
|
|
|
|
foreach($js_files as $js) {
|
|
|
|
$mcss->addSource($js, file_get_contents($js));
|
|
|
|
}
|
|
|
|
$mcss->save($js_cache_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $js_cache_file;
|
|
|
|
}
|
|
|
|
|
2023-08-18 13:15:44 +00:00
|
|
|
private function get_js_cache_file(string $theme_name, int $config_latest): string
|
|
|
|
{
|
2019-05-28 16:59:38 +00:00
|
|
|
$js_latest = $config_latest;
|
|
|
|
$js_files = array_merge(
|
|
|
|
[
|
|
|
|
"vendor/bower-asset/jquery/dist/jquery.min.js",
|
|
|
|
"vendor/bower-asset/jquery-timeago/jquery.timeago.js",
|
|
|
|
"vendor/bower-asset/js-cookie/src/js.cookie.js",
|
|
|
|
],
|
2019-08-07 19:53:59 +00:00
|
|
|
zglob("ext/{" . Extension::get_enabled_extensions_as_string() . "}/script.js"),
|
2021-05-05 16:40:25 +00:00
|
|
|
zglob("themes/$theme_name/{" . implode(",", $this->get_theme_scripts()) . "}")
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
foreach ($js_files as $js) {
|
|
|
|
$js_latest = max($js_latest, filemtime($js));
|
|
|
|
}
|
|
|
|
$js_md5 = md5(serialize($js_files));
|
|
|
|
$js_cache_file = data_path("cache/script/{$theme_name}.{$js_latest}.{$js_md5}.js");
|
|
|
|
if (!file_exists($js_cache_file)) {
|
2024-01-01 15:00:54 +00:00
|
|
|
$mcss = new \MicroBundler\MicroBundler();
|
|
|
|
foreach($js_files as $js) {
|
|
|
|
$mcss->addSource($js, file_get_contents($js));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-01-01 15:00:54 +00:00
|
|
|
$mcss->save($js_cache_file);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2023-08-18 13:15:44 +00:00
|
|
|
|
|
|
|
return $js_cache_file;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-02-01 18:11:00 +00:00
|
|
|
|
2021-05-05 16:40:25 +00:00
|
|
|
/**
|
|
|
|
* @return array A list of stylesheets relative to the theme root.
|
|
|
|
*/
|
|
|
|
protected function get_theme_stylesheets(): array
|
|
|
|
{
|
|
|
|
return ["style.css"];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array A list of script files relative to the theme root.
|
|
|
|
*/
|
|
|
|
protected function get_theme_scripts(): array
|
|
|
|
{
|
|
|
|
return ["script.js"];
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
protected function get_nav_links(): array
|
2020-02-01 23:23:23 +00:00
|
|
|
{
|
|
|
|
$pnbe = send_event(new PageNavBuildingEvent());
|
|
|
|
|
|
|
|
$nav_links = $pnbe->links;
|
|
|
|
|
|
|
|
$active_link = null;
|
|
|
|
// To save on event calls, we check if one of the top-level links has already been marked as active
|
|
|
|
foreach ($nav_links as $link) {
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($link->active === true) {
|
2020-02-01 23:23:23 +00:00
|
|
|
$active_link = $link;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sub_links = null;
|
|
|
|
// If one is, we just query for sub-menu options under that one tab
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($active_link !== null) {
|
2020-02-01 23:23:23 +00:00
|
|
|
$psnbe = send_event(new PageSubNavBuildingEvent($active_link->name));
|
|
|
|
$sub_links = $psnbe->links;
|
|
|
|
} else {
|
|
|
|
// Otherwise we query for the sub-items under each of the tabs
|
|
|
|
foreach ($nav_links as $link) {
|
|
|
|
$psnbe = send_event(new PageSubNavBuildingEvent($link->name));
|
|
|
|
|
|
|
|
// Now we check for a current link so we can identify the sub-links to show
|
|
|
|
foreach ($psnbe->links as $sub_link) {
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($sub_link->active === true) {
|
2020-02-01 23:23:23 +00:00
|
|
|
$sub_links = $psnbe->links;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the active link has been detected, we break out
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($sub_links !== null) {
|
2020-02-01 23:23:23 +00:00
|
|
|
$link->active = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$sub_links = $sub_links ?? [];
|
2023-12-16 11:04:52 +00:00
|
|
|
|
|
|
|
usort($nav_links, fn (NavLink $a, NavLink $b) => $a->order - $b->order);
|
|
|
|
usort($sub_links, fn (NavLink $a, NavLink $b) => $a->order - $b->order);
|
2020-02-01 23:23:23 +00:00
|
|
|
|
|
|
|
return [$nav_links, $sub_links];
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:11:00 +00:00
|
|
|
/**
|
|
|
|
* turns the Page into HTML
|
|
|
|
*/
|
2020-02-01 23:23:23 +00:00
|
|
|
public function render()
|
2020-02-01 18:11:00 +00:00
|
|
|
{
|
2024-01-04 14:07:52 +00:00
|
|
|
global $config, $user;
|
|
|
|
|
2024-01-04 13:57:45 +00:00
|
|
|
$head = $this->head_html();
|
|
|
|
$body = $this->body_html();
|
2020-02-01 18:11:00 +00:00
|
|
|
|
2024-01-04 14:07:52 +00:00
|
|
|
$body_attrs = [
|
|
|
|
"data-userclass" => $user->class->name,
|
|
|
|
];
|
|
|
|
|
2024-01-04 13:57:45 +00:00
|
|
|
print emptyHTML(
|
|
|
|
rawHTML("<!doctype html>"),
|
|
|
|
HTML(
|
|
|
|
["lang" => "en"],
|
|
|
|
HEAD(rawHTML($head)),
|
2024-01-04 14:07:52 +00:00
|
|
|
BODY($body_attrs, rawHTML($body))
|
2024-01-04 13:57:45 +00:00
|
|
|
)
|
|
|
|
);
|
2020-02-01 18:11:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 21:20:32 +00:00
|
|
|
protected function head_html(): string
|
|
|
|
{
|
2020-02-01 18:11:00 +00:00
|
|
|
$html_header_html = $this->get_all_html_headers();
|
|
|
|
|
|
|
|
return "
|
2024-01-04 13:57:45 +00:00
|
|
|
<title>{$this->title}</title>
|
|
|
|
$html_header_html
|
2020-02-01 18:11:00 +00:00
|
|
|
";
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:20:32 +00:00
|
|
|
protected function body_html(): string
|
|
|
|
{
|
2020-02-01 18:11:00 +00:00
|
|
|
$left_block_html = "";
|
|
|
|
$main_block_html = "";
|
|
|
|
$sub_block_html = "";
|
|
|
|
|
|
|
|
foreach ($this->blocks as $block) {
|
|
|
|
switch ($block->section) {
|
|
|
|
case "left":
|
|
|
|
$left_block_html .= $block->get_html(true);
|
|
|
|
break;
|
|
|
|
case "main":
|
|
|
|
$main_block_html .= $block->get_html(false);
|
|
|
|
break;
|
|
|
|
case "subheading":
|
|
|
|
$sub_block_html .= $block->get_html(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print "<p>error: {$block->header} using an unknown section ({$block->section})";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$footer_html = $this->footer_html();
|
|
|
|
$flash_html = $this->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $this->flash)))."</b>" : "";
|
|
|
|
return "
|
2024-01-04 13:57:45 +00:00
|
|
|
<header>
|
2024-01-08 21:24:41 +00:00
|
|
|
<h1>{$this->heading}</h1>
|
2024-01-04 13:57:45 +00:00
|
|
|
$sub_block_html
|
|
|
|
</header>
|
|
|
|
<nav>
|
|
|
|
$left_block_html
|
|
|
|
</nav>
|
|
|
|
<article>
|
|
|
|
$flash_html
|
|
|
|
$main_block_html
|
|
|
|
</article>
|
|
|
|
<footer>
|
|
|
|
$footer_html
|
|
|
|
</footer>
|
2020-02-01 18:11:00 +00:00
|
|
|
";
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:20:32 +00:00
|
|
|
protected function footer_html(): string
|
|
|
|
{
|
2020-02-01 18:11:00 +00:00
|
|
|
$debug = get_debug_info();
|
|
|
|
$contact_link = contact_link();
|
|
|
|
$contact = empty($contact_link) ? "" : "<br><a href='$contact_link'>Contact</a>";
|
|
|
|
|
|
|
|
return "
|
2020-10-26 15:08:58 +00:00
|
|
|
Media © their respective owners,
|
2020-02-01 18:11:00 +00:00
|
|
|
<a href=\"https://code.shishnet.org/shimmie2/\">Shimmie</a> ©
|
|
|
|
<a href=\"https://www.shishnet.org/\">Shish</a> &
|
|
|
|
<a href=\"https://github.com/shish/shimmie2/graphs/contributors\">The Team</a>
|
2024-01-03 14:47:58 +00:00
|
|
|
2007-2024,
|
2020-02-01 18:11:00 +00:00
|
|
|
based on the Danbooru concept.
|
|
|
|
$debug
|
|
|
|
$contact
|
|
|
|
";
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
|
|
|
|
class PageNavBuildingEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $links = [];
|
2019-08-02 19:54:48 +00:00
|
|
|
|
|
|
|
public function add_nav_link(string $name, Link $link, string $desc, ?bool $active = null, int $order = 50)
|
|
|
|
{
|
|
|
|
$this->links[] = new NavLink($name, $link, $desc, $active, $order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PageSubNavBuildingEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $parent;
|
2019-08-02 19:54:48 +00:00
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $links = [];
|
2019-08-02 19:54:48 +00:00
|
|
|
|
|
|
|
public function __construct(string $parent)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2023-11-11 21:49:12 +00:00
|
|
|
$this->parent = $parent;
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 15:03:34 +00:00
|
|
|
public function add_nav_link(string $name, Link $link, string|HTMLElement $desc, ?bool $active = null, int $order = 50)
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
$this->links[] = new NavLink($name, $link, $desc, $active, $order);
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NavLink
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $name;
|
|
|
|
public Link $link;
|
2023-07-03 15:03:34 +00:00
|
|
|
public string|HTMLElement $description;
|
2021-03-14 23:43:50 +00:00
|
|
|
public int $order;
|
|
|
|
public bool $active = false;
|
2019-08-02 19:54:48 +00:00
|
|
|
|
2023-07-03 15:03:34 +00:00
|
|
|
public function __construct(string $name, Link $link, string|HTMLElement $description, ?bool $active = null, int $order = 50)
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$this->name = $name;
|
|
|
|
$this->link = $link;
|
|
|
|
$this->description = $description;
|
|
|
|
$this->order = $order;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($active == null) {
|
2019-08-02 19:54:48 +00:00
|
|
|
$query = ltrim(_get_query(), "/");
|
|
|
|
if ($query === "") {
|
|
|
|
// This indicates the front page, so we check what's set as the front page
|
|
|
|
$front_page = trim($config->get_string(SetupConfig::FRONT_PAGE), "/");
|
|
|
|
|
|
|
|
if ($front_page === $link->page) {
|
|
|
|
$this->active = true;
|
|
|
|
} else {
|
|
|
|
$this->active = self::is_active([$link->page], $front_page);
|
|
|
|
}
|
2023-11-11 21:49:12 +00:00
|
|
|
} elseif ($query === $link->page) {
|
2019-08-02 19:54:48 +00:00
|
|
|
$this->active = true;
|
2019-09-29 13:30:55 +00:00
|
|
|
} else {
|
2019-08-02 19:54:48 +00:00
|
|
|
$this->active = self::is_active([$link->page]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->active = $active;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function is_active(array $pages_matched, string $url = null): bool
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Woo! We can actually SEE THE CURRENT PAGE!! (well... see it highlighted in the menu.)
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
$url = $url ?? ltrim(_get_query(), "/");
|
2019-08-02 19:54:48 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$re1 = '.*?';
|
|
|
|
$re2 = '((?:[a-z][a-z_]+))';
|
2019-08-02 19:54:48 +00:00
|
|
|
|
|
|
|
if (preg_match_all("/".$re1.$re2."/is", $url, $matches)) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$url = $matches[1][0];
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$count_pages_matched = count($pages_matched);
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
for ($i = 0; $i < $count_pages_matched; $i++) {
|
2019-08-02 19:54:48 +00:00
|
|
|
if ($url == $pages_matched[$i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|