diff --git a/public/index.php b/public/index.php index 9d40da8..07670b0 100644 --- a/public/index.php +++ b/public/index.php @@ -1,4 +1,16 @@ 404 Not Found'; + exit; +} + // Define all the important paths define('APP_ROOT', dirname(dirname(__FILE__))); define('SRC_DIR', APP_ROOT . '/src'); @@ -34,18 +46,6 @@ Session::start(); Session::generateCsrfToken(); $config = Config::load(); -// Get request data -$method = $_SERVER['REQUEST_METHOD']; -$request = $_SERVER['REQUEST_URI']; -$path = parse_url($request, PHP_URL_PATH); - -// return a 404 if a request for a .php file gets this far. -if (preg_match('/\.php$/', $path)) { - http_response_code(404); - echo '

404 Not Found

'; - exit; -} - // Remove the base path from the URL // and strip the trailing slash from the resulting route if (strpos($path, $config->basePath) === 0) { @@ -100,6 +100,7 @@ $routeHandlers = [ ['mood', 'MoodController@handleMood', ['POST']], ['feed/rss', 'FeedController@rss'], ['feed/atom', 'FeedController@atom'], + ['tick/{y}/{m}/{d}/{h}/{i}/{s}', 'TickController'], ]; // Set content type diff --git a/src/Controller/Admin/Admin.php b/src/Controller/AdminController/AdminController.php similarity index 97% rename from src/Controller/Admin/Admin.php rename to src/Controller/AdminController/AdminController.php index db188be..1a880d7 100644 --- a/src/Controller/Admin/Admin.php +++ b/src/Controller/AdminController/AdminController.php @@ -18,8 +18,8 @@ class AdminController extends Controller { // POST handler // save updated settings public function handleSave(){ - $isLoggedIn = isset($_SESSION['user_id']); - if (!$isLoggedIn){ + //$isLoggedIn = isset($_SESSION['user_id']); + if (!Session::isLoggedIn()){ header('Location: ' . $config->basePath . 'login.php'); exit; } diff --git a/src/Controller/Auth/Auth.php b/src/Controller/AuthController/AuthController.php similarity index 100% rename from src/Controller/Auth/Auth.php rename to src/Controller/AuthController/AuthController.php diff --git a/src/Controller/Feed/Feed.php b/src/Controller/FeedController/FeedController.php similarity index 100% rename from src/Controller/Feed/Feed.php rename to src/Controller/FeedController/FeedController.php diff --git a/src/Controller/Home/Home.php b/src/Controller/HomeController/HomeController.php similarity index 91% rename from src/Controller/Home/Home.php rename to src/Controller/HomeController/HomeController.php index 554d7ac..1cc7061 100644 --- a/src/Controller/Home/Home.php +++ b/src/Controller/HomeController/HomeController.php @@ -6,7 +6,6 @@ class HomeController extends Controller { // renders the homepage view. public function index(){ $page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1; - $isLoggedIn = isset($_SESSION['user_id']); $config = Config::load(); $user = User::load(); @@ -18,10 +17,9 @@ class HomeController extends Controller { $tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit); $vars = [ - 'isLoggedIn' => $isLoggedIn, 'config' => $config, 'user' => $user, - 'tickList' => $tickList, + 'tickList' => $tickList, ]; $this->render("home.php", $vars); diff --git a/src/Controller/Mood/Mood.php b/src/Controller/MoodController/MoodController.php similarity index 100% rename from src/Controller/Mood/Mood.php rename to src/Controller/MoodController/MoodController.php diff --git a/src/Controller/TickController/TickController.php b/src/Controller/TickController/TickController.php new file mode 100644 index 0000000..b1f842c --- /dev/null +++ b/src/Controller/TickController/TickController.php @@ -0,0 +1,11 @@ +get($year, $month, $day, $hour, $minute, $second); + $this->render('tick.php', $tick); + } +} \ No newline at end of file diff --git a/src/Framework/Session/Session.php b/src/Framework/Session/Session.php index cc7b85e..251aebf 100644 --- a/src/Framework/Session/Session.php +++ b/src/Framework/Session/Session.php @@ -24,6 +24,10 @@ class Session { return $_SESSION['csrf_token']; } + public static function isLoggedIn(): bool { + return isset($_SESSION['user_id']); + } + public static function end(): void { $_SESSION = []; session_destroy(); diff --git a/src/Framework/Util/Util.php b/src/Framework/Util/Util.php index 9602c83..fa8c3fe 100644 --- a/src/Framework/Util/Util.php +++ b/src/Framework/Util/Util.php @@ -104,6 +104,17 @@ class Util { }; } + public static function tick_time_to_tick_path($tickTime){ + [$date, $time] = explode(' ', $tickTime); + $dateParts = explode('-', $date); + $timeParts = explode(':', $time); + + [$year, $month, $day] = $dateParts; + [$hour, $minute, $second] = $timeParts; + + return "$year/$month/$day/$hour/$minute/$second"; + } + // TODO: Move to model base class? public static function get_db(): PDO { Util::verify_data_dir(DATA_DIR, true); diff --git a/src/Model/Tick/Tick.php b/src/Model/Tick/Tick.php index 7ceb537..dc81c0f 100644 --- a/src/Model/Tick/Tick.php +++ b/src/Model/Tick/Tick.php @@ -1,6 +1,7 @@ strcmp($b, $a)); // sort filenames in reverse chronological order @@ -73,5 +74,30 @@ class Tick { // write the tick to the file (the file will be created if it doesn't exist) $content = $time . "|" . $tick . "\n"; file_put_contents($filename, $content, FILE_APPEND); - } + } + + public static function get(string $y, string $m, string $d, string $H, string $i, string $s): array{ + $tickTime = new DateTime("$y-$m-$d $H:$i:$s"); + $timestamp = "$H:$i:$s"; + $file = TICKS_DIR . "/$y/$m/$d.txt"; + + if (!file_exists($file)) { + http_response_code(404); + echo "Tick not found: $file."; + exit; + } + + $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + foreach ($lines as $line) { + if (str_starts_with($line, $timestamp)) { + $tick = Util::escape_and_linkify(explode('|', $line)[1]); + + return [ + 'tickTime' => $tickTime, + 'tick' => $tick, + 'config' => Config::load(), + ]; + } + } + } } diff --git a/src/View/Home/Home.php b/src/View/HomeView/HomeView.php similarity index 100% rename from src/View/Home/Home.php rename to src/View/HomeView/HomeView.php diff --git a/src/View/Mood/Mood.php b/src/View/MoodView/MoodView.php similarity index 100% rename from src/View/Mood/Mood.php rename to src/View/MoodView/MoodView.php diff --git a/templates/admin.php b/templates/admin.php index b4abf78..0ae2da5 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -1,17 +1,14 @@ - - + - <?= $config->siteTitle ?> - - - + + +

Admin

-
Back to home
diff --git a/templates/feed/atom.php b/templates/feed/atom.php index dabb21e..0f5815a 100644 --- a/templates/feed/atom.php +++ b/templates/feed/atom.php @@ -27,9 +27,9 @@ echo '' . "\n"; [$hour, $minute, $second] = $timeParts; $tickPath = "$year/$month/$day/$hour/$minute/$second"; - $tickUrl = htmlspecialchars($basePath . "tick.php?path=" . $tickPath); + $tickUrl = htmlspecialchars($basePath . "tick/$tickPath"); $tickTime = date(DATE_ATOM, strtotime($tick['timestamp'])); - $tickText = htmlspecialchars($tick['tick']); + $tickText = htmlspecialchars($tick['tick']); ?> <?= $tickText ?> diff --git a/templates/feed/rss.php b/templates/feed/rss.php index ee5f5f9..0b4e995 100644 --- a/templates/feed/rss.php +++ b/templates/feed/rss.php @@ -15,7 +15,6 @@ echo '' . "\n"; en-us ' . "\n"; ?> <?php echo htmlspecialchars($tick['tick']); ?> - basePath . "tick.php?path=$tickPath"); ?> + basePath . "tick/$tickPath"); ?> diff --git a/templates/home.php b/templates/home.php index fca3a70..720c603 100644 --- a/templates/home.php +++ b/templates/home.php @@ -2,26 +2,13 @@ - - <?= $config->siteTitle ?> - - - + -
- rss - atom - - login - - admin - logout - -
+