Webfinger lookup is now part of add_feed

This commit is contained in:
sørenpeter 2024-03-16 21:11:48 +01:00
parent a65727ccd4
commit 35e7d96780
6 changed files with 89 additions and 19 deletions

View file

@ -190,7 +190,8 @@ article.post-entry {
} }
article .twt-msg { article .twt-msg {
padding: 0.5rem 0; padding: 0.5rem 0.5rem 0.5rem 0 ;
overflow-wrap: anywhere;
} }
article .twt-msg > blockquote { article .twt-msg > blockquote {

View file

@ -13,7 +13,7 @@
// Private lists // Private lists
echo "<option disabled>Private Lists:</option>"; echo "<option disabled>Private Lists:</option>";
foreach (glob("private/twtxt-*.txt") as $filename) { foreach (glob("private/twtxt-*.txt") as $filename) {
if($filename == $_GET['lists']) $attr="selected"; if($filename == $_GET['list']) $attr="selected";
else $attr = ""; else $attr = "";
$listName = $filename; $listName = $filename;
$listName = str_replace("private/twtxt-", "", $listName); $listName = str_replace("private/twtxt-", "", $listName);
@ -28,7 +28,7 @@
} }
foreach (glob("twtxt-*.txt") as $filename) { foreach (glob("twtxt-*.txt") as $filename) {
if($filename == $_GET['lists']) $attr="selected"; if($filename == $_GET['list']) $attr="selected";
else $attr = ""; else $attr = "";
$listName = $filename; $listName = $filename;
$listName = str_replace("twtxt-", "", $listName); $listName = str_replace("twtxt-", "", $listName);

View file

@ -0,0 +1,48 @@
<?php
function splitNickDomain($str) {
$str = trim($str);
$str = str_replace("acct:", "", $str); // remove "acct:" prefix
$str = ltrim($str, "@"); // remove leading "@"
if (filter_var($str, FILTER_VALIDATE_EMAIL) ) {
return array("nick" => explode("@",$str)[0], "domain" => explode("@",$str)[1] );
} else {
return FALSE;
}
}
$wf_acct = "";
$wf_nick = "";
$wf_url = "";
$wf_error = "";
if(isset($_POST['submit'])) {
$wf_request = $_POST["webfinger"];
$wf_acct = splitNickDomain($wf_request);
if($wf_acct === FALSE) { $wf_error = "ERROR: ".$wf_request." does not look like a valid webfinger handle"; }
$wf_json = file_get_contents("https://".$wf_acct["domain"]."/.well-known/webfinger?resource=acct:".$wf_acct["nick"]."@".$wf_acct["domain"]);
//if($wf_json === FALSE) { $wf_error = "ERROR: Chould not find a twtxt feed at webfinger endpoint ".$wf_request; }
$wf_array = json_decode($wf_json, true);
$wf_domain = splitNickDomain($wf_array["subject"])["domain"];
foreach( $wf_array["links"] as $link ) {
if ( $link["rel"] == "self" AND $link["type"] == "text/plain") {
if ( filter_var($link["href"], FILTER_VALIDATE_URL) ) {
$wf_url = $link["href"];
$wf_nick = splitNickDomain($wf_array["subject"])["nick"];
$wf_error = "URL to twtxt.txt found for ".$wf_nick ."@".$wf_domain;
break;
}
} else { $wf_error = "ERROR: Chould not find a twtxt feed at webfinger endpoint ".$wf_acct["domain"]; }
}
}
//else { $wf_error = "Chould not find webfinger endpoint at ".$wf_acct["domain"]; }

View file

@ -1,5 +1,6 @@
<?php <?php
require_once('partials/base.php'); require_once('partials/base.php');
require_once('partials/webfinger_lookup.php');
// TODO: Give a warning if the file is not found // TODO: Give a warning if the file is not found
$config = parse_ini_file('private/config.ini'); $config = parse_ini_file('private/config.ini');
@ -72,17 +73,25 @@ $title = "Add feed - ".$title;
include 'partials/header.php'; include 'partials/header.php';
?> ?>
<h2>Webfinger lookup</h2>
<form method="post" action="">
<label>Check if a webfinger handle has a link to a twtxt.txt feed</label>
<input type="text" name="webfinger" size="50" autocomplete="off" required placeholder="name@example.com" value="<?= $wf_request; ?>">
<br>
<input type="submit" name="submit" value="Lookup"><br>
</form>
<?= $wf_error; ?>
<h1>Add a new feed to follow</h1> <h1>Add a new feed to follow</h1>
<form method="POST" class="column"> <form method="POST" class="column">
<div id="follow"> <div id="follow">
<label for="url">URL to follow</label>
<br>
<input type="url" id="url" name="url" class="input" size="50" autocomplete="off" required>
<br>
<label for="nick">Nick</label> <label for="nick">Nick</label>
<br> <input type="text" id="nick" name="nick" class="input" size="50" autocomplete="off" required value="<?= $wf_nick; ?>">
<input type="text" id="nick" name="nick" class="input" size="50" autocomplete="off" required> <label for="url">URL to follow</label>
<input type="url" id="url" name="url" class="input" size="50" autocomplete="off" required value="<?= $wf_url; ?>">
<br> <br>
<input type="submit" value="Follow" class="btn"> <input type="submit" value="Follow" class="btn">
</div> </div>

View file

@ -15,7 +15,9 @@ include 'partials/header.php';
<!-- <th></th> --> <!-- <th></th> -->
<th>Nick</th> <th>Nick</th>
<th>URL</th> <th>URL</th>
<th>Time ago</th> <?php if($_SESSION['password']=="$password") { ?>
<th>Time ago</th>
<?php } ?>
</tr> </tr>
<?php foreach ($twtFollowingList as $currentFollower) { ?> <?php foreach ($twtFollowingList as $currentFollower) { ?>
@ -28,12 +30,13 @@ include 'partials/header.php';
<!-- <a href="?remove_url=<?= $currentFollower[1] ?>">Remove</a> --> <!-- <a href="?remove_url=<?= $currentFollower[1] ?>">Remove</a> -->
<!-- <?php // } ?> --> <!-- <?php // } ?> -->
</td> </td>
<?php if($_SESSION['password']=="$password") { ?>
<td> <td>
<?php <?php
// Test first if URL is a valid feed: // Test first if URL is a valid feed:
if (is_array(getTwtsFromTwtxtString($currentFollower[1])->twts)) { if (is_array(getTwtsFromTwtxtString($currentFollower[1])->twts)) {
// Then test if latest twt is at top or bottom of file: // Then test if latest twt is at start or end of file:
$resetVar = reset(getTwtsFromTwtxtString($currentFollower[1])->twts); $resetVar = reset(getTwtsFromTwtxtString($currentFollower[1])->twts);
$endVar = end(getTwtsFromTwtxtString($currentFollower[1])->twts); $endVar = end(getTwtsFromTwtxtString($currentFollower[1])->twts);
if ($resetVar->timestamp < $endVar->timestamp) { // TODO: this can be swapped to get time of first twt if ($resetVar->timestamp < $endVar->timestamp) { // TODO: this can be swapped to get time of first twt
@ -45,6 +48,8 @@ include 'partials/header.php';
?> ?>
</td> </td>
<?php } ?>
</tr> </tr>
<?php } ?> <?php } ?>

View file

@ -55,16 +55,18 @@ if (isset($_POST['submit'])) {
$contents = file_get_contents($txt_file_path); $contents = file_get_contents($txt_file_path);
if (!date_default_timezone_set($timezone)) { /*if (!date_default_timezone_set($timezone)) {
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
} }*/ // Turned this off, so now the server need to have set the right timezone, seem to work for CET
//$datetime = gmdate('Y-m-d\TH:i:s\Z', $date->format('U')); //$datetime = gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));
//$twt = $datetime . "\t$new_post\n"; //$twt = $datetime . "\t$new_post\n";
$twt = date('c') . "\t$new_post\n"; //$twt = date('c') . "\t$new_post\n";
$datetime = date('Y-m-d\TH:i:sp'); // abracting to be used for webmentions
$twt = "\n" . $datetime . "\t" .$new_post; // NB: only works with PHP 8
// TODO: Turn off append at top?!
if (strpos($contents, NEW_TWT_MARKER) !== false) { /*if (strpos($contents, NEW_TWT_MARKER) !== false) {
// Add the previous marker // Add the previous marker
// Take note that doesn't not work if twtxt file has CRLF line ending // Take note that doesn't not work if twtxt file has CRLF line ending
// (which is wrong anyway) // (which is wrong anyway)
@ -73,15 +75,20 @@ if (isset($_POST['submit'])) {
} else { } else {
// Fall back if the marker is not found. // Fall back if the marker is not found.
$contents .= $twt; $contents .= $twt;
} }*/
// Append twt at the end of file
$contents .= $twt;
// TODO: Add error handling if write to the file fails // TODO: Add error handling if write to the file fails
// For example due to permissions problems // For example due to permissions problems
// https://www.w3docs.com/snippets/php/how-can-i-handle-the-warning-of-file-get-contents-function-in-php.html // https://www.w3docs.com/snippets/php/how-can-i-handle-the-warning-of-file-get-contents-function-in-php.html
$file_write_result = file_put_contents($txt_file_path, $contents); $file_write_result = file_put_contents($txt_file_path, $contents);
header('Refresh:0; url=.'); //header('Refresh:0; url=.');
exit; header("Location: refresh?url=".$public_txt_url); // Trying to fix issue with douple posting
exit; //
} else { } else {
require_once("partials/base.php"); require_once("partials/base.php");