mirror of
https://github.com/sorenpeter/timeline.git
synced 2025-12-15 19:07:01 +00:00
Routing implemented
This commit is contained in:
parent
0e079a4ba3
commit
bf5c678f66
17 changed files with 198 additions and 94 deletions
|
|
@ -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>
|
|
||||||
65
index.php
65
index.php
|
|
@ -2,45 +2,46 @@
|
||||||
|
|
||||||
//require_once("router.php");
|
//require_once("router.php");
|
||||||
//require_once("views/home.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>
|
$viewDir = '/views/';
|
||||||
<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';?>
|
// Define your routes using regular expressions
|
||||||
|
|
||||||
<main>
|
$routes = [
|
||||||
<!-- PHP: GET PROFILE CARD -->
|
'/' => 'home.php',
|
||||||
<?php
|
'/admin' => 'admin.php',
|
||||||
if (!empty($_GET['twts'])) { // Show twts for some user
|
'/about' => 'about.php',
|
||||||
$twtsURL = $_GET['twts'];
|
'/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
|
// Loop through the defined routes and try to match the request URI
|
||||||
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
|
foreach ($routes as $pattern => $action) {
|
||||||
die('Not a valid URL');
|
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);
|
// Load the corresponding action (view)
|
||||||
if (!is_null($parsedTwtxtFile)) {
|
require __DIR__ . $viewDir . $action;
|
||||||
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
|
exit; // Stop processing further routes
|
||||||
include 'partials/profile.php';
|
}
|
||||||
}
|
}
|
||||||
} ?>
|
|
||||||
|
|
||||||
|
// 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'?>
|
/* Credit:
|
||||||
|
- PHP FOR BEGINNERS #4 - Create a dynamic Router: https://www.youtube.com/watch?v=eaHBK2XJ5Io
|
||||||
</main>
|
- https://chat.openai.com/c/3082a22a-d70e-4740-891c-9872f5da2180
|
||||||
|
*/
|
||||||
<!-- PHP: GET FOOTER --><?php include 'partials/footer.php';?>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
38
index_old.php
Normal file
38
index_old.php
Normal 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>
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<header>
|
<header>
|
||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<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 -->
|
<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>
|
<li><?php include 'partials/listSelect.php'; ?></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
|
|
@ -28,12 +28,15 @@
|
||||||
|
|
||||||
<small>
|
<small>
|
||||||
<?php if($twt->replyToHash) { ?>
|
<?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 } ?>
|
<?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 ?>">Reply</a>
|
||||||
(<a href="new_twt.php?hash=<?= $twt->hash ?>">via email</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='/?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>
|
</small>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
|
||||||
41
views/conv.php
Normal file
41
views/conv.php
Normal 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
40
views/home.php
Normal 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
34
views/post.php
Normal 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>
|
||||||
Loading…
Reference in a new issue