From 7b24598f90815e093bda4239ee62a43b1aa8f827 Mon Sep 17 00:00:00 2001 From: sorenpeter Date: Sun, 12 Nov 2023 11:38:46 +0100 Subject: [PATCH] Fixing subfolder support --- .htaccess | 3 ++- index.php | 29 ++++++++++++++-------- libs/twtxt.php | 2 +- partials/base.php | 13 +++++----- partials/header.php | 34 ++++++++++++-------------- partials/lists.php | 43 +++++++++++++++++++++++++++++++++ partials/profile.php | 53 ++++++++++++++++++++++++++++++++++++++++ partials/timeline.php | 12 +++++----- views/following.php | 2 +- views/home.php | 56 +++++-------------------------------------- views/logout.php | 3 +-- views/new_twt.php | 2 +- views/post.php | 4 ++-- 13 files changed, 156 insertions(+), 100 deletions(-) create mode 100644 partials/lists.php create mode 100644 partials/profile.php diff --git a/.htaccess b/.htaccess index 4aee731..2e514f4 100644 --- a/.htaccess +++ b/.htaccess @@ -1,3 +1,4 @@ RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f -RewriteRule ^(.*)$ index.php \ No newline at end of file +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^(.*)$ index.php [NC,L] \ No newline at end of file diff --git a/index.php b/index.php index a18c429..fe826ce 100644 --- a/index.php +++ b/index.php @@ -1,15 +1,25 @@ '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 "

Oops! Page not found.

"; -echo __DIR__ . $viewDir . $action; + +//echo __DIR__ . $viewDir . $action; /* Credit: - PHP FOR BEGINNERS #4 - Create a dynamic Router: https://www.youtube.com/watch?v=eaHBK2XJ5Io diff --git a/libs/twtxt.php b/libs/twtxt.php index 8a1abfb..e5fbd4f 100644 --- a/libs/twtxt.php +++ b/libs/twtxt.php @@ -125,7 +125,7 @@ function replaceMentionsFromTwt(string $twtString): string { $pattern = '/@<([^ ]+)\s([^>]+)>/'; //$replacement = '@$1'; - $replacement = '@$1'; + $replacement = '@$1'; #$twtString = '@'; #$pattern = '/@<([^ ]+) ([^>]+)>/'; #$replacement = '@$1'; diff --git a/partials/base.php b/partials/base.php index ddc6751..bc3b6fe 100644 --- a/partials/base.php +++ b/partials/base.php @@ -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']); diff --git a/partials/header.php b/partials/header.php index f3ef67b..9794723 100644 --- a/partials/header.php +++ b/partials/header.php @@ -1,7 +1,6 @@ @@ -9,42 +8,39 @@ $profile = getTwtsFromTwtxtString($config['public_txt_url']); - - + + <?= $title ?>

- + Timeline for nick ?>@mainURL, PHP_URL_HOST); ?> - - - -

-
\ No newline at end of file +
+ + diff --git a/partials/lists.php b/partials/lists.php new file mode 100644 index 0000000..068de12 --- /dev/null +++ b/partials/lists.php @@ -0,0 +1,43 @@ + +
+ + + +
\ No newline at end of file diff --git a/partials/profile.php b/partials/profile.php new file mode 100644 index 0000000..54fc0b0 --- /dev/null +++ b/partials/profile.php @@ -0,0 +1,53 @@ + + +
+ + + + + +
+ + nick ?>@mainURL, PHP_URL_HOST); ?> + + +

description ?>

+ + + Posts | + Replies | + Gallery | + + + Following | + twtxt.txt | + How to follow + + + + +
+ +
+ + \ No newline at end of file diff --git a/partials/timeline.php b/partials/timeline.php index 54eb97b..462f309 100644 --- a/partials/timeline.php +++ b/partials/timeline.php @@ -1,5 +1,5 @@ - displayDate ?> + displayDate ?> diff --git a/views/following.php b/views/following.php index eb2396f..65396f3 100644 --- a/views/following.php +++ b/views/following.php @@ -19,7 +19,7 @@ include 'partials/header.php'; - + diff --git a/views/home.php b/views/home.php index 5e5e619..7c45db6 100644 --- a/views/home.php +++ b/views/home.php @@ -7,11 +7,9 @@ include 'partials/header.php'; ?> -mainURL] = $parsedTwtxtFile; include 'partials/profile.php'; } -} -*/ +} ?> -?> - -mainURL] = $parsedTwtxtFile; - } - } - } -} - - -$twts = []; - -# Combine all the followers twts -foreach ($parsedTwtxtFiles as $currentTwtFile) { - if (!is_null($currentTwtFile)) { - $twts += $currentTwtFile->twts; - } -} - -?> +} ?> diff --git a/views/logout.php b/views/logout.php index 9c3ca62..c44d92b 100644 --- a/views/logout.php +++ b/views/logout.php @@ -4,6 +4,5 @@ session_start(); session_unset(); session_destroy(); -header("Location: /"); +header("Location: ."); die(); - diff --git a/views/new_twt.php b/views/new_twt.php index 98c2073..5cf3d1b 100644 --- a/views/new_twt.php +++ b/views/new_twt.php @@ -20,7 +20,7 @@ require_once('libs/session.php'); // } if (!isset($_SESSION['password'])) { - header('Location: /login'); + header('Location: ./login'); exit(); } diff --git a/views/post.php b/views/post.php index 8560bdf..2dae8e2 100644 --- a/views/post.php +++ b/views/post.php @@ -7,12 +7,12 @@ if (!empty($id)) { }); } -$title = "Post: ".$id." - ".$title; +$title = "Post #".$id." - ".$title; include 'partials/header.php'; ?> -

Post: #

+