Webmentions now working

This commit is contained in:
sørenpeter 2024-03-17 01:01:16 +01:00
parent 3dd338ab38
commit f3a6500d9b
2 changed files with 62 additions and 16 deletions

View file

@ -65,7 +65,7 @@ if (isset($_POST['submit'])) {
$datetime = date('Y-m-d\TH:i:sp'); // abracting to be used for webmentions $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 $twt = "\n" . $datetime . "\t" .$new_post; // NB: only works with PHP 8
// TODO: Turn off append at top?! // TODO: Delete?
/*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
@ -80,15 +80,43 @@ if (isset($_POST['submit'])) {
// Append twt at the end of file // Append twt at the end of file
$contents .= $twt; $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);
// TODO: replace with file_put_contents($logfile, $log, FILE_APPEND) -- https://www.w3schools.com/php/func_filesystem_file_put_contents.asp
// Send webmentions (TODO: move to it own file?)
$new_mentions = getMentionsFromTwt($twt);
foreach ($new_mentions as $mention) {
//print_r(getMentionsFromTwt($twt));
//echo $mention["nick"] . " from " . $mention["url"]."<br>";
// TODO: detect endpoint via $mention["url"]
$targets_webmention_endpoint = "https://darch.dk/timeline/webmention";
$your_url = "https://darch.dk/twtxt.txt#:~:text=".$datetime;
//$your_url = "https://darch.dk/twtxt.txt#:~:text=2024-03-16T20:38:31Z";
$target_url = $mention["url"];
$payload = "source=".$your_url."&target=".$target_url;
echo $payload;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $targets_webmention_endpoint);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
$data = curl_exec($curl);
curl_close($curl);
}
//header('Refresh:0; url=.'); //header('Refresh:0; url=.');
header("Location: refresh?url=".$public_txt_url); // Trying to fix issue with douple posting header("Location: refresh?url=".$public_txt_url); // Trying to fix issue with douple posting
exit; // exit;
} else { } else {
require_once("partials/base.php"); require_once("partials/base.php");

View file

@ -4,9 +4,9 @@
# http://creativecommons.org/publicdomain/zero/1.0/ # http://creativecommons.org/publicdomain/zero/1.0/
if (!isset($_POST['source']) || !isset($_POST['target'])) { if (!isset($_POST['source']) || !isset($_POST['target'])) {
print('Please send a propper webmention to this endpoint'); print('Please send a propper webmention to this endpoint');
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit; exit;
} }
ob_start(); ob_start();
@ -19,17 +19,35 @@ $source = ob_get_contents();
ob_end_clean(); ob_end_clean();
if (stristr($source, $_POST['target'])) { if (stristr($source, $_POST['target'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 202 Accepted'); header($_SERVER['SERVER_PROTOCOL'] . ' 202 Accepted');
# Now do something with $source e.g. parse it for h-entry and h-card and store what you find. # Now do something with $source e.g. parse it for h-entry and h-card and store what you find.
$logfile = './mentions.txt'; /* Make sure file is writeable */ // TODO: test if $datetime is to be found in $source
// and then write the $twt to the $log
$datetime = explode( ":~:text=", $_POST['source'] );
$pattern = '/^'.$datetime[1].'\t(.*?)$/m';
preg_match($pattern, $source, $twt); // $twt[1] contains your line.
//preg_match('/^'.$datetime[1].'\t(.*?)$/m', $source, $twt);
$logfile = './mentions.txt'; /* Make sure file is writeable */
$log = date("Y-m-d\TH:i:s\Z") . "\t"
."You were mentioned in: ".$_POST['source']
.""
."> ". $twt[1]
//."Recived webmention from ".$_POST['source']
//." mentioning ".$_POST['target']
//." (IP: ".$_SERVER['REMOTE_ADDR'].")"
.PHP_EOL;
file_put_contents($logfile, $log, FILE_APPEND);
# Send email fork: https://gist.github.com/otherjoel/9301d985622f0d3d1a09
$log = date("Y-m-d\TH:i:s\Z") . "\t"
."Recived webmention from ".$_POST['source']
." mentioning ".$_POST['target']
." (IP: ".$_SERVER['REMOTE_ADDR'].")".PHP_EOL;
file_put_contents($logfile, $log, FILE_APPEND);
} }
else { else {