timeline/partials/base.php
2023-12-28 11:42:23 -06:00

110 lines
2.7 KiB
PHP

<?php
# Shows the timeline for a user
declare (strict_types = 1);
require_once "libs/session.php";
require_once 'libs/twtxt.php';
require_once 'libs/hash.php';
require_once 'libs/Slimdown.php';
const TWTS_PER_PAGE = 50;
// TODO: Move twts per page to config.ini
// Add a fallback if the number is invalid (it should be between 1 and 999)
$config = parse_ini_file('private/config.ini');
// TODO: Take the title from the config.ini
$title = "Timeline"; // Fallback, should be set in all views
// HACKED by sp@darch.dk
if (!empty($_GET['list'])) {
$url = "https://darch.dk/twtxt-lists/" . $_GET['list'];
} else {
$url = $config['public_txt_url'];
}
date_default_timezone_set('UTC');
if (!empty($_GET['url'])) {
$url = $_GET['url'];
}
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}
$cacheRefreshTime = $config['cache_refresh_time'];
$fileContent = getCachedFileContentsOrUpdate($url, $cacheRefreshTime);
if ($fileContent === false) {
die("$url couldn't be retrieved.");
}
$fileContent = mb_convert_encoding($fileContent, 'UTF-8');
$fileLines = explode("\n", $fileContent);
$twtFollowingList = [];
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');
}
$parsedTwtxtFile = getTwtsFromTwtxtString($twtsURL);
if (!is_null($parsedTwtxtFile)) {
$parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile;
}
} else { // Show timeline for the URL
$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;
}
}
if (!empty($_GET['hash'])) {
$hash = $_GET['hash'];
$twts = array_filter($twts, function ($twt) use ($hash) {
return $twt->hash === $hash || $twt->replyToHash === $hash;
});
}
krsort($twts, SORT_NUMERIC);
if (!empty($_GET['hash'])) {
$twts = array_reverse($twts, true);
}
$page = 1;
if (!empty($_GET['page'])) {
$page = intval($_GET['page']);
}
// If we should paginate our twts list
if (!empty($paginateTwts)) {
$startingTwt = (($page - 1) * TWTS_PER_PAGE);
$twts = array_slice($twts, $startingTwt, TWTS_PER_PAGE);
}
$baseURL = str_replace("/index.php", "", $_SERVER['SCRIPT_NAME']);