Fixing subfolder support

This commit is contained in:
sorenpeter 2023-11-12 11:38:46 +01:00
parent 0846df5ed0
commit 7b24598f90
13 changed files with 156 additions and 100 deletions

View file

@ -1,3 +1,4 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,L]

View file

@ -1,15 +1,25 @@
<?php
// Adds support for installation in subfolder
$base = dirname($_SERVER['PHP_SELF']) ;
if ($base === '/' || $base === '\\'){
$base = '';
function getBaseURI() // https://github.com/taniarascia/comments/issues/26#issuecomment-1458121921
{
$basePath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
// Get the current Request URI and remove rewrite base path from it
// (= allows one to run the router in a sub folder)
$uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen($basePath));
// Don't take query params into account on the URL
if (strstr($uri, '?')) {
$uri = substr($uri, 0, strpos($uri, '?'));
}
// Remove trailing slash + enforce a slash at the start
return '/' . trim($uri, '/');
}
$request = $_SERVER['REQUEST_URI'];
$request = getBaseURI();
//$request = $_SERVER['REQUEST_URI'];
$path = parse_url($request, PHP_URL_PATH);
// $query = parse_url($request, PHP_URL_QUERY);
$viewDir = '/views/';
// Define your routes using regular expressions
@ -23,16 +33,14 @@ $routes = [
'/refresh' => 'load_twt_files.php',
'/login' => 'login.php',
'/logout' => 'logout.php',
'/profile' => 'profile.php',
//'/profile/([a-zA-Z0-9_-]+)' => 'profile.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
];
// Loop through the defined routes and try to match the request URI
foreach ($routes as $pattern => $action) {
if (preg_match('#^' . $base.$pattern . '$#', $path, $matches)) {
if (preg_match('#^' . $pattern . '$#', $path, $matches)) {
// Extract any matched parameters (e.g., username)
if(!empty($matches[1])) {
@ -49,7 +57,8 @@ foreach ($routes as $pattern => $action) {
// If no matching route is found, handle as a 404
http_response_code(404);
echo "<h1>Oops! Page not found.</h1>";
echo __DIR__ . $viewDir . $action;
//echo __DIR__ . $viewDir . $action;
/* Credit:
- PHP FOR BEGINNERS #4 - Create a dynamic Router: https://www.youtube.com/watch?v=eaHBK2XJ5Io

View file

@ -125,7 +125,7 @@ function replaceMentionsFromTwt(string $twtString): string {
$pattern = '/@<([^ ]+)\s([^>]+)>/';
//$replacement = '<a href="/?url=$2">@$1</a>';
$replacement = '<a href="/?twts=$2">@$1</a>';
$replacement = '<a href="'.str_replace("/index.php", "", $_SERVER["SCRIPT_NAME"]).'/?profil=$2">@$1</a>';
#$twtString = '@<nick https://eapl.mx/twtxt.txt>';
#$pattern = '/@<([^ ]+) ([^>]+)>/';
#$replacement = '@$1';

View file

@ -78,11 +78,8 @@ $fileContent = mb_convert_encoding($fileContent, 'UTF-8');
$fileLines = explode("\n", $fileContent);
$twtFollowingList = [];
// Show twts only for URL in query request, else show user timeline
if (!empty($_GET['twts'])) { // Show twts for some user --> /profile
$twtsURL = $_GET['twts'];
if (!empty($_GET['profile'])) { // Show profile for some user
$twtsURL = $_GET['profile'];
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}
@ -92,7 +89,7 @@ if (!empty($_GET['twts'])) { // Show twts for some user --> /profile
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
}
} else { // Show timeline for the URL --> / (home)
} else { // Show timeline for the URL
$parsedTwtxtFiles = [];
foreach ($fileLines as $currentLine) {
if (str_starts_with($currentLine, '#')) {
@ -120,7 +117,6 @@ foreach ($parsedTwtxtFiles as $currentTwtFile) {
}
}
# Show individual posts
if (!empty($_GET['hash'])) {
$hash = $_GET['hash'];
$twts = array_filter($twts, function($twt) use ($hash) {
@ -128,6 +124,7 @@ if (!empty($_GET['hash'])) {
});
}
krsort($twts, SORT_NUMERIC);
if (!empty($_GET['hash'])) {
@ -141,3 +138,5 @@ if (!empty($_GET['page'])) {
$startingTwt = (($page - 1) * TWTS_PER_PAGE);
$twts = array_slice($twts, $startingTwt, TWTS_PER_PAGE);
$baseURL = str_replace("/index.php", "", $_SERVER['SCRIPT_NAME']);

View file

@ -1,7 +1,6 @@
<?php
$profile = getTwtsFromTwtxtString($config['public_txt_url']);
// $profile = getTwtsFromTwtxtString("http://darch.dk/twtxt.txt");
?>
@ -9,42 +8,39 @@ $profile = getTwtsFromTwtxtString($config['public_txt_url']);
<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" href="/libs/simple.css">
<link rel="stylesheet" type="text/css" href="/style.css">
<link rel="stylesheet" href="<?= $baseURL ?>/libs/simple.css">
<link rel="stylesheet" type="text/css" href="<?= $baseURL ?>/style.css">
<title><?= $title ?></title>
</head>
<body>
<header>
<p>
<a href="/">
<a href="<?= $baseURL ?>">
<img class="avatar" src="<?= $profile->avatar ?>" alt="" loading="lazy">
Timeline for <?= $profile->nick ?>@<?= parse_url($profile->mainURL, PHP_URL_HOST); ?>
</a>
<!-- <a href="/">🧶 Timeline for <?= $url ?> </a> -->
<!-- for <a href="http://localhost:8000/?twts=http://darch.dk/twtxt.txt">sorenpeter</a>@darch.dk -->
<!-- (<a href="http://localhost:8000/twtxt.txt">twtxt.txt</a>) -->
</p>
<nav>
<!-- TODO: make automatic via PHP and show avatar as well -->
<ul class="secondary">
<li><?php include 'partials/lists.php'; ?></li>
<li><a href="<?= $baseURL ?>/following">Following <?php // echo count($twtFollowingList); ?></a></li>
<?php //if ($validSession) { // TODO: Make login seqcure ?>
<?php if( isset($_SESSION['password'])) { /*
if($_SESSION['password']=="$password") {*/ // Hacky login ?>
<li><a href="/refresh?url=<?= $url ?>">Refresh</a></li>
<li><a href="/following">Following <?php // echo count($twtFollowingList); ?></a></li>
<!-- <li><a href="/new">New post</a></li> -->
<li><a href="/add">Add feed</a></li>
<!-- <li><a href="/admin">Settings</a></li> -->
<li><a href="/logout">Log Out</a></li>
<li><a href="<?= $baseURL ?>/refresh?url=<?= $url ?>">Refresh</a></li>
<li><a href="<?= $baseURL ?>/add">Add feed</a></li>
<li><a href="<?= $baseURL ?>/logout">Log Out</a></li>
<?php /*}*/ } else { ?>
<li><a href="/login">Log in</a></li>
<li><a href="<?= $baseURL ?>/login">Log in</a></li>
<?php } ?>
<!-- <li><?php //include 'partials/listSelect.php'; ?></li> -->
<!-- <li><a href="<?= $baseURL ?>/?profile=<?=$profile->mainURL ?>">Profile</a></li> -->
<li><a href="<?= $baseURL ?>/?profile=<?=$config["public_txt_url"] ?>">Profile</a></li>
</ul>
</nav>
</header>
<main>

43
partials/lists.php Normal file
View file

@ -0,0 +1,43 @@
<!-- List UI -->
<form action="" method="get">
<!-- Select a list: -->
<select name="list" onchange="this.form.submit()">
<option value="twtxt.txt" selected>twtxt.txt (Main)</option>
<?php
// TODO: fix it so if List -> Selected for both public and private lists
if( isset($_SESSION['password'])) {
if($_SESSION['password']=="$password") { // Hacky login
// Private lists
echo "<option disabled>Private Lists:</option>";
foreach (glob("private/twtxt-*.txt") as $filename) {
if($filename == $_GET['lists']) $attr="selected";
else $attr = "";
$listName = $filename;
$listName = str_replace("private/twtxt-", "", $listName);
$listName = str_replace("_", " ", $listName);
$listName = str_replace(".txt", "", $listName);
echo "<option value='{$filename}' {$attr}>$listName</option>";
}
// Public Lists
echo "<option disabled>Public Lists:</option>";
}
}
foreach (glob("twtxt-*.txt") as $filename) {
if($filename == $_GET['lists']) $attr="selected";
else $attr = "";
$listName = $filename;
$listName = str_replace("twtxt-", "", $listName);
$listName = str_replace("_", " ", $listName);
$listName = str_replace(".txt", "", $listName);
echo "<option value='{$filename}' {$attr}>$listName</option>";
}
?>
</select>
<noscript><button type="submit">View list</button></noscript>
</form>

53
partials/profile.php Normal file
View file

@ -0,0 +1,53 @@
<?php
// Get info about profile from URL as an objects
if (!empty($_GET['profile'])) {
$url = $twtsURL;
}
$profile = getTwtsFromTwtxtString($url);
?>
<div class="profile">
<a href="<?= $profile->mainURL ?>">
<img class="avatar" src="<?= $profile->avatar ?>" alt="" loading="lazy">
</a>
<div>
<a href="<?= $profile->mainURL ?>" class="author">
<strong><?= $profile->nick ?></strong>@<?= parse_url($profile->mainURL, PHP_URL_HOST); ?>
</a>
<p><?= $profile->description ?></p>
<small>
<a href="">Posts</a> |
<a href="">Replies</a> |
<a href="">Gallery</a> |
<!-- <span class="right"> -->
<a href="following.php">Following <?php echo count($twtFollowingList); ?></a> |
<a target="_blank" href="<?= $profile->mainURL ?>"></i>twtxt.txt</a> |
<a href="https://yarn.social">How to follow</a>
<!-- </span> -->
</small>
</div>
</div>
<!-- <nav>
<ul>
<li><a href="">Posts</a></li>
<li><a href="">Replies</a></li>
<li><a href="">Gallery</a></li>
</ul>
<ul class="right">
<li><a href="https://yarn.social" class="button">How to follow...</a></li>
<li><a href="following.php">Following <?php echo count($twtFollowingList); ?></a></li>
<li><a target="_blank" href="<?= $profile->mainURL ?>"></i>twtxt.txt</a></li>
</ul>
</nav> -->

View file

@ -1,5 +1,5 @@
<!-- <center>
<?php if (!empty($_GET['twts'])) { ?>
<?php if (!empty($_GET['profile'])) { ?>
<em>Twts for <a href="<?= $twtsURL ?>"><?= $twtsURL ?></a></em>
<?php } else { ?>
<em>Timeline for <a href="<?= $url ?>"><?= $url ?></a></em>
@ -8,11 +8,11 @@
<?php foreach ($twts as $twt) { ?>
<article class="post-entry">
<a href="/?twts=<?= $twt->mainURL ?>">
<a href="<?= $baseURL ?>/?profile=<?= $twt->mainURL ?>">
<img src='<?= $twt->avatar ?>' class="avatar" onerror="this.onerror=null;this.src='imgs/image_not_found.png';">
</a>
<div>
<a href="/?twts=<?= $twt->mainURL ?>" class="author">
<a href="<?= $baseURL ?>/?profile=<?= $twt->mainURL ?>" class="author">
<strong><?= $twt->nick ?></strong>@<?= parse_url($twt->mainURL, PHP_URL_HOST); ?>
</a>
@ -30,7 +30,7 @@
<?php
if($twt->replyToHash) {
echo 'In reply to: <a href="/conv/'.$twt->replyToHash.'">#'.$twt->replyToHash.'</a>';
echo 'In reply to: <a href="'.$baseURL.'/conv/'.$twt->replyToHash.'">#'.$twt->replyToHash.'</a>';
//echo '<a href="/conv/'.$twt->replyToHash.'">Convesation</a>';
}
@ -39,12 +39,12 @@
}
if (isset($_SESSION['password'])) {
echo '<a href="/new?hash='.$twt->hash.'">Reply</a>';
echo '<a href="'.$baseURL.'/new?hash='.$twt->hash.'">Reply</a>';
}
?>
<!-- (<a href="new_twt.php?hash=<?= $twt->hash ?>">via email</a>) TODO: mailto-link -->
<a href='/post/<?= $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>

View file

@ -19,7 +19,7 @@ include 'partials/header.php';
<?php foreach ($twtFollowingList as $currentFollower) { ?>
<tr>
<!-- <td></td> -->
<td><a href="/?twts=<?= $currentFollower[1] ?>"><?= $currentFollower[0] ?></a></td>
<td><a href="<?= $baseURL ?>/?profile=<?= $currentFollower[1] ?>"><?= $currentFollower[0] ?></a></td>
<!-- <td><a href="/?twt=<?= $currentFollower[1] ?>"><?= $currentFollower[0] ?></a></td> -->
<td><?= $currentFollower[1] ?>
<!-- <?php if ($validSession) { ?> -->

View file

@ -8,10 +8,8 @@ include 'partials/header.php';
<!-- PHP: PROFILE CARD -->
<?php
/*
if (!empty($_GET['twts'])) { // Show profile for some user
$twtsURL = $_GET['twts'];
if (!empty($_GET['profile'])) { // Show twts for some user
$twtsURL = $_GET['profile'];
// TODO: Give a propper error if feed is not valid
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
@ -23,56 +21,14 @@ if (!empty($_GET['twts'])) { // Show profile for some user
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
include 'partials/profile.php';
}
}
*/
} ?>
?>
<!-- PHP: NEW POST BOX -->
<?php
if (isset($_SESSION['password'])) {
if( isset($_SESSION['password'])) {
include 'views/new_twt.php'; // TODO: Split up new_twt into a view and a partial
}
/*else {
$twtsURL = $config['public_txt_url'];
// $twtsURL = "http://darch.dk/twtxt.txt";
header("Location: profile?url=".$twtsURL);
// die();
}*/
?>
<?php // Load user timeline
$parsedTwtxtFiles = [];
foreach ($fileLines as $currentLine) {
if (str_starts_with($currentLine, '#')) {
if (!is_null(getDoubleParameter('follow', $currentLine))) {
$follow = getDoubleParameter('follow', $currentLine);
$twtFollowingList[] = $follow;
// Read the parsed files if in Cache
$followURL = $follow[1];
$parsedTwtxtFile = getTwtsFromTwtxtString($followURL);
if (!is_null($parsedTwtxtFile)) {
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
}
}
}
}
$twts = [];
# Combine all the followers twts
foreach ($parsedTwtxtFiles as $currentTwtFile) {
if (!is_null($currentTwtFile)) {
$twts += $currentTwtFile->twts;
}
}
?>
} ?>
<!-- PHP: TIMELINE --><?php include 'partials/timeline.php'?>

View file

@ -4,6 +4,5 @@ session_start();
session_unset();
session_destroy();
header("Location: /");
header("Location: .");
die();

View file

@ -20,7 +20,7 @@ require_once('libs/session.php');
// }
if (!isset($_SESSION['password'])) {
header('Location: /login');
header('Location: ./login');
exit();
}

View file

@ -7,12 +7,12 @@ if (!empty($id)) {
});
}
$title = "Post: ".$id." - ".$title;
$title = "Post #".$id." - ".$title;
include 'partials/header.php';
?>
<h2>Post: #<?= $id ?></h2>
<!-- <h2>Post: #<?= $id ?></h2> -->
<!-- PHP: GET TIMELIE --><?php include 'partials/timeline.php'?>