diff options
| author | Jonas Kohl | 2024-07-19 17:20:26 +0200 | 
|---|---|---|
| committer | Jonas Kohl | 2024-07-19 17:20:26 +0200 | 
| commit | 8a6897e3fd2efceacf8d020dfbf8b742a68f96cd (patch) | |
| tree | f43176d660ec727a72a2a8ac3a029c572a167ecb | |
| parent | 1e02dc1555f9d298f6f054834589a80c8f8d8273 (diff) | |
Add development server and refactor building
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | common.php | 29 | ||||
| -rw-r--r-- | dev-server.php | 60 | ||||
| -rw-r--r-- | expand.php | 21 | 
4 files changed, 97 insertions, 20 deletions
@@ -1,7 +1,7 @@  SRCFILES:=$(shell find src/pages/ -type f -name '*.php')  HTMLTARGETS:=$(SRCFILES:src/pages/%.php=build/%.html) -.PHONY: build/static publish +.PHONY: build/static publish dev-server  all: $(HTMLTARGETS) $(STATICFILES) build/static @@ -22,3 +22,8 @@ publish-http:  	rsync -avh ./build/ hozyro-srv:/var/www/hozyro --delete  publish-all: publish publish-http + +dev-server: +	@bash -c 'echo -e "\e[91mWARNING: This server should only be used for \ +	development purposes! IT IS NOT PRODUCTION-SAFE! USE WITH CAUTION!\e[0m"' +	php -S 127.0.0.1:19310 dev-server.php diff --git a/common.php b/common.php new file mode 100644 index 0000000..c505660 --- /dev/null +++ b/common.php @@ -0,0 +1,29 @@ +<?php + +define("SRCDIR", realpath(__DIR__ . "/src/")); + +function hzcom_expand(string $__page): string { +    $title = ""; +    $__page = preg_replace_callback( +        ';<title>(.*?)</title>;i', +        function(array $matches) use(&$title): string { +            $title = $matches[1]; +            return ""; +        }, +        $__page +    ); +    $page = SRCDIR . "/pages/" . bin2hex(random_bytes(8)) . ".tmp"; +    file_put_contents($page, $__page); +    unset($__page); + +    ob_start(); +    include SRCDIR . "/template.php"; +    $output = ob_get_clean(); +    if ($output === false) +        $output = ""; + +    if (is_file($page)) +        unlink($page); + +    return $output; +} diff --git a/dev-server.php b/dev-server.php new file mode 100644 index 0000000..dea55ab --- /dev/null +++ b/dev-server.php @@ -0,0 +1,60 @@ +<?php + +require_once __DIR__ . "/common.php"; + +const TEMPLATE_DIR = SRCDIR . "/pages/"; +const STATIC_DIR = SRCDIR . "/static/"; +const FALLBACK_FILE = TEMPLATE_DIR . "not_found.php"; +const DEFAULT_FILE = TEMPLATE_DIR . "index.php"; +const DEFAULT_MIME_TYPE = "application/octet-stream"; + +function get_mime(string $filename): string { +    static $mime_overrides = [ +        "css" => "text/css", +        "js" => "text/javascript", +    ]; +    $ext = pathinfo($filename, PATHINFO_EXTENSION); +    $ext = strtolower($ext); +    $mime = $mime_overrides[$ext] ?? mime_content_type($filename); +    if ($mime === false) +        $mime = DEFAULT_MIME_TYPE; +    return $mime; +} + +$name = $_SERVER["REQUEST_URI"]; +$is_static = false; + +$name = ltrim($name, "/"); +if (str_starts_with($name, "static/")) +    $is_static = true; +elseif (str_ends_with($name, ".html")) +    $name = preg_replace('/\.html$/', "", $name); + +$include_file = FALLBACK_FILE; + +if ($is_static) { +    $staticFile = STATIC_DIR . preg_replace(';^static/;', "", $name); +    if (!is_file($staticFile)) { +        $include_file = FALLBACK_FILE; +        http_response_code(404); +    } else { +        $content_type = get_mime($staticFile); +        $content_length = filesize($staticFile); +        header("Content-Type: $content_type"); +        header("Content-Length: $content_length"); +        readfile($staticFile); +        exit; +    } +} else { +    if ($name === "") +        $include_file = DEFAULT_FILE; +    else +        $include_file = TEMPLATE_DIR . $name . ".php"; +} + +if (!is_file($include_file)) { +    $include_file = FALLBACK_FILE; +    http_response_code(404); +} + +echo hzcom_expand(file_get_contents($include_file)); @@ -1,21 +1,4 @@  <?php -define("SRCDIR", realpath(__DIR__ . "/src/")); - -$__page = file_get_contents("php://stdin"); -$title = ""; -$__page = preg_replace_callback( -    ';<title>(.*?)</title>;i', -    function(array $matches) use(&$title): string { -        $title = $matches[1]; -        return ""; -    }, -    $__page -); -$page = SRCDIR . "/pages/" . bin2hex(random_bytes(8)) . ".tmp"; -file_put_contents($page, $__page); -unset($__page); - -include SRCDIR . "/template.php"; - -unlink($page); +require_once __DIR__ . "/common.php"; +echo hzcom_expand(file_get_contents("php://stdin"));  |