Эх сурвалжийг харах

[add] init stuff
[add] read data
[add] 404 page (missing styles)
[add] project structure

isundil 9 жил өмнө
parent
commit
21421dab5e

+ 1 - 0
data/.gitignore

@@ -0,0 +1 @@
+*

+ 31 - 0
src/Flammenkuchen.php

@@ -0,0 +1,31 @@
+<?php
+
+class Flammenkuchen
+{
+    private $title;
+    private $descr;
+    private $img;
+
+    public function __construct($data)
+    {
+        $this->title = $data->title;
+        $this->descr = $data->desc;
+        $this->img = $data->img;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function getDescription()
+    {
+        return $this->descr;
+    }
+
+    public function getImageB64()
+    {
+        return $this->img;
+    }
+}
+

+ 102 - 0
src/Router.php

@@ -0,0 +1,102 @@
+<?php
+
+require_once("Flammenkuchen.php");
+
+class Router
+{
+    protected $request;
+    protected $requestingDate;
+    protected $data;
+
+    protected $LATEST = -1;
+
+    function __construct()
+    {
+        $this->request = $_SERVER["REQUEST_URI"];
+        $this->data = null;
+
+        if ($this->request[0] == '/')
+            $this->request = substr($this->request, 1);
+
+        if ($this->request == "")
+            $this->requestingDate = $this->checkFileExists($this->LATEST);
+        else if (strlen($this->request) == 6)
+        {
+            $isNumeric = true;
+
+            for ($i =0; $i < 6; $i++)
+            {
+                if (!is_numeric($this->request[$i]))
+                {
+                    $isNumeric = false;
+                    break;
+                }
+            }
+            if ($isNumeric)
+                $this->requestingDate = $this->checkFileExists($this->request);
+        }
+    }
+
+    private function checkFileExists($yymmdd)
+    {
+        if ($yymmdd != $this->LATEST)
+        {
+            // TODO use index cache
+            if (file_exists("../data/${yymmdd}.json"))
+                return $yymmdd;
+            return null;
+        }
+        else
+        {
+            $now = (int) (new \Datetime())->format("ymd");
+            $min = 0;
+
+            // obvious today-is-the-day
+            if ($this->checkFileExists($now))
+                return $now;
+
+            //TODO use index cache
+            $datadir = opendir("../data/");
+            while (($file = readdir($datadir)) !== false)
+            {
+                if (is_dir("../data/".$file)
+                    || strlen($file) != 11
+                    || substr($file, 6) !== ".json")
+                    continue;
+
+                $current = (int) substr($file, 0, 6);
+
+                if ($current > $now)
+                    continue;
+                if ($now - $current < $now - $min)
+                    $min = $current;
+            }
+            closedir($datadir);
+            return $min === 0 ? null : $min;
+        }
+    }
+
+    public function isLost()
+    {
+        if ($this->requestingDate === null)
+            return true;
+        if ($this->data === null)
+            $this->readData();
+        return ($this->data === false);
+    }
+
+    public function getData()
+    {
+        if ($this->data === null)
+            $this->data = $this->readData();
+        return $this->data;
+    }
+
+    private function readData()
+    {
+        $req = $this->requestingDate;
+
+        return new Flammenkuchen(json_decode(file_get_contents("../data/${req}.json")));
+    }
+}
+

+ 4 - 0
src/templates/bonjour.php

@@ -0,0 +1,4 @@
+<?php include ("./header.php"); ?>
+<title>Bonjour Flammenkuchen !</title></head><body>
+<?php var_dump($data); ?>
+</body></html>

+ 2 - 0
src/templates/header.php

@@ -0,0 +1,2 @@
+<!DOCTYPE html5>
+<html><head><link rel="stylesheet" href="/css/style.css"><script src="/js/scripts.js"></script>

+ 6 - 0
src/templates/lost.php

@@ -0,0 +1,6 @@
+<?php header("404 Not Found"); include ("./header.php"); ?>
+    <title>Not found</title>
+</head><body>
+    <h1>Oops</h1>
+    <p>Look like you lost your bacon in here...</p>
+</body></html>

+ 4 - 0
www/.htaccess

@@ -0,0 +1,4 @@
+RewriteEngine on
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule . index.php [L]
+

+ 0 - 0
www/css/style.css


+ 16 - 0
www/index.php

@@ -0,0 +1,16 @@
+<?php
+
+require_once("../src/Router.php");
+
+$router = new Router();
+
+    $data = $router->getData();
+if ($router->isLost())
+    include("../src/templates/lost.php");
+else
+{
+    include("../src/templates/bonjour.php");
+}
+
+?>
+

+ 0 - 0
www/js/scripts.js