Routing implemented

This commit is contained in:
sorenpeter 2023-10-08 20:31:49 +02:00
parent 0e079a4ba3
commit bf5c678f66
17 changed files with 198 additions and 94 deletions

View file

@ -1,53 +0,0 @@
<?php require_once("partials/base.php"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="../style.css">
<title><?=$title?> - Timeline</title>
</head>
<body >
<nav class="edit">
<?php
// $config = parse_ini_file('private/config.ini');
// $password = $config['password'];
if($_SESSION['password']=="$password")
{
?>
<small>You are loggged in</small>
<form method="post" action="" id="logout_form">
<input type="submit" name="page_logout" value="Log out" class="right">
</form>
<?php
} else {
?>
<small><a href="/login">Log in</a></small>
<?php } ?>
</nav>
<!-- PHP: GET HEADER --><?php include 'partials/header.php';?>
<!-- PHP: GET PROFILE CARD --><?php include 'partials/profile.php';?>
<main class="timeline">
<center><h3>
<?php if (!empty($_GET['twts'])) { ?>
<em>Twts for <a href="<?= $twtsURL ?>"><?= $twtsURL ?></a></em>
<?php } else { ?>
<em>Timeline for <a href="<?= $url ?>"><?= $url ?></a></em>
<?php } ?>
</h3></center>
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>
</main>
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
</body>
</html>

View file

@ -2,45 +2,46 @@
//require_once("router.php");
//require_once("views/home.php");
require_once("partials/base.php");
//require_once("partials/base.php");
?>
$request = $_SERVER['REQUEST_URI'];
$path = parse_url($request, PHP_URL_PATH);
// $query = parse_url($request, PHP_URL_QUERY);
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="style.css">
<title>Timeline</title>
</head>
<body >
$viewDir = '/views/';
<!-- PHP: GET HEADER --><?php include 'partials/header.php';?>
// Define your routes using regular expressions
<main>
<!-- PHP: GET PROFILE CARD -->
<?php
if (!empty($_GET['twts'])) { // Show twts for some user
$twtsURL = $_GET['twts'];
$routes = [
'/' => 'home.php',
'/admin' => 'admin.php',
'/about' => 'about.php',
'/profile/([a-zA-Z0-9_-]+)' => 'profile.php',
'/conv/([a-zA-Z0-9]{7})' => 'conv.php', // matches only twtHash of exactly 7 alphanumeric characters
'/post/([a-zA-Z0-9]{7})' => 'post.php', // matches only twtHash of exactly 7 alphanumeric characters
];
// TODO: Give a propper error if feed is not valid
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
// Loop through the defined routes and try to match the request URI
foreach ($routes as $pattern => $action) {
if (preg_match('#^' . $pattern . '$#', $path, $matches)) {
// Extract any matched parameters (e.g., username)
if(!empty($matches[1])) {
//array_shift($matches);
$id = $matches[1];
}
// $parsedTwtxtFile = getTwtsFromTwtxtString($twtsURL);
if (!is_null($parsedTwtxtFile)) {
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
include 'partials/profile.php';
// Load the corresponding action (view)
require __DIR__ . $viewDir . $action;
exit; // Stop processing further routes
}
} ?>
}
// If no matching route is found, handle as a 404
http_response_code(404);
echo "<h1>Oops! Page not found.</h1>";
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>
</main>
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
</body>
</html>
/* Credit:
- PHP FOR BEGINNERS #4 - Create a dynamic Router: https://www.youtube.com/watch?v=eaHBK2XJ5Io
- https://chat.openai.com/c/3082a22a-d70e-4740-891c-9872f5da2180
*/

38
index_old.php Normal file
View file

@ -0,0 +1,38 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="style.css">
<title>Timeline</title>
</head>
<body >
<!-- PHP: GET HEADER --><?php include 'partials/header.php';?>
<main>
<!-- PHP: GET PROFILE CARD -->
<?php
if (!empty($_GET['twts'])) { // Show twts for some user
$twtsURL = $_GET['twts'];
// TODO: Give a propper error if feed is not valid
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}
// $parsedTwtxtFile = getTwtsFromTwtxtString($twtsURL);
if (!is_null($parsedTwtxtFile)) {
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
include 'partials/profile.php';
}
} ?>
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>
</main>
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
</body>
</html>

View file

@ -1,7 +1,7 @@
<header>
<nav>
<ul>
<li>🧶 <a href=".">Timeline</a> for
<li>🧶 <a href="/">Timeline</a> for
<a href="http://localhost:8000/?twts=http://darch.dk/twtxt.txt">sorenpeter</a>@darch.dk</li> <!-- TODO: make automatic via PHP and show avatar as well -->
<li><?php include 'partials/listSelect.php'; ?></li>
</ul>

View file

@ -28,12 +28,15 @@
<small>
<?php if($twt->replyToHash) { ?>
<a href="?hash=<?= $twt->replyToHash?>">Conversation</a> |
<!-- <a href="?hash=<?= $twt->replyToHash?>">Conversation</a> | -->
<a href="/conv/<?= $twt->replyToHash?>">Conversation: #<?= $twt->replyToHash?></a> |
<?php } ?>
<!-- TODO make depending on session or nor -->
<!-- TODO: make depending on session or nor -->
<a href="new_twt.php?hash=<?= $twt->hash ?>">Reply</a>
(<a href="new_twt.php?hash=<?= $twt->hash ?>">via email</a>)
<a href='?hash=<?= $twt->hash ?>' class="right"><span title="<?= $twt->fullDate ?> "><?= $twt->displayDate ?></span></a>
(<a href="new_twt.php?hash=<?= $twt->hash ?>">via email</a>) <!-- TODO: mailto-link -->
<!-- <a href='/?hash=<?= $twt->hash ?>' class="right"><span title="<?= $twt->fullDate ?> "><?= $twt->displayDate ?></span></a> -->
<a href='/post/<?= $twt->hash ?>' class="right"><span title="<?= $twt->fullDate ?> "><?= $twt->displayDate ?></span></a>
</small>
</div>
</article>

41
views/conv.php Normal file
View file

@ -0,0 +1,41 @@
<?php require_once("partials/base.php");
// Get the hashes (both post and replies) as $id from the router and return an inverted list
if (!empty($id)) {
$twts = array_filter($twts, function($twt) use ($id) {
return $twt->hash === $id || $twt->replyToHash === $id;
});
$twts = array_reverse($twts, true);
}
// && !preg_match('/conv/', $request)
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="/style.css">
<title>Conversation: <?= $id ?></title>
</head>
<body >
<!-- PHP: GET HEADER --><?php include 'partials/header.php';?>
<main>
<h2>Conversation</h2>
<p>Recent twts in reply to <a href="/post/<?= $id ?>">#<?= $id ?></a></p>
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>
</main>
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
</body>
</html>

40
views/home.php Normal file
View file

@ -0,0 +1,40 @@
<?php require_once("partials/base.php"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="style.css">
<title>Timeline</title>
</head>
<body >
<!-- PHP: GET HEADER --><?php include 'partials/header.php';?>
<main>
<!-- PHP: GET PROFILE CARD -->
<?php
if (!empty($_GET['twts'])) { // Show twts for some user
$twtsURL = $_GET['twts'];
// TODO: Give a propper error if feed is not valid
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}
// $parsedTwtxtFile = getTwtsFromTwtxtString($twtsURL);
if (!is_null($parsedTwtxtFile)) {
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
include 'partials/profile.php';
}
} ?>
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>
</main>
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
</body>
</html>

34
views/post.php Normal file
View file

@ -0,0 +1,34 @@
<?php require_once("partials/base.php");
// Get the hash (only post, not replies) as $id from the router
if (!empty($id)) {
$twts = array_filter($twts, function($twt) use ($id) {
return $twt->hash === $id;
});
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="/style.css">
<title>Post: <?= $id ?></title>
</head>
<body >
<!-- PHP: GET HEADER --><?php include 'partials/header.php';?>
<main>
<h2>Post: #<?= $id ?></h2>
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>
</main>
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
</body>
</html>