From 446d3b9317b88b3113e3083bca7bbdc2741ae210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?s=C3=B8renpeter?= Date: Sun, 24 Mar 2024 23:35:49 +0100 Subject: [PATCH] Webmention: Detect webmention endpoint define in twtxt.txt as --- partials/header.php | 2 +- partials/profile.php | 1 + views/conv.php | 2 + views/new_twt.php | 78 ++++++++++++++++++++++------------- views/webmention_endpoint.php | 8 ++-- 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/partials/header.php b/partials/header.php index 2ff1158..787a3ba 100644 --- a/partials/header.php +++ b/partials/header.php @@ -46,7 +46,7 @@ $profile = getTwtsFromTwtxtString($config['public_txt_url']);
  • Following
  • Add feed
  • Log Out
  • -
  • +
  • Profile
  • Gallery
  • diff --git a/partials/profile.php b/partials/profile.php index e31dbc4..e450929 100644 --- a/partials/profile.php +++ b/partials/profile.php @@ -7,6 +7,7 @@ if (!empty($_GET['profile'])) { $profile = getTwtsFromTwtxtString($url); $profileURL = $baseURL . '/?profile=' . $profile->mainURL; +$textareaValue = "@<$profile->nick $profile->mainURL> "; ?>
    diff --git a/views/conv.php b/views/conv.php index 2b9211f..289d01b 100644 --- a/views/conv.php +++ b/views/conv.php @@ -23,7 +23,9 @@ include_once 'partials/header.php'; + "; - // 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; + // Detect webmention endpoint define in twtxt.txt as `# webmention = URL` + $targets_webmention_endpoint = getSingleParameter("webmention", file_get_contents($mention["url"])); - $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); + if (!isset($targets_webmention_endpoint)) { + echo "

    No endpoint found in: ".$mention["url"]."

    "; + + } else { + + $new_twt_url = $public_txt_url."#:~:text=".$datetime; + //$target_url = $mention["url"]; + $payload = "source=".$new_twt_url."&target=".$mention["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); + $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); + curl_close($curl); + + echo "

    A webmention was send to: ".$targets_webmention_endpoint." (Status: $status)

    "; + } } //header('Refresh:0; url=.'); - header("Location: refresh?url=".$public_txt_url); // Trying to fix issue with douple posting - exit; + //header("Location: refresh?url=".$public_txt_url); // Trying to fix issue with douple posting + //exit; } else { require_once("partials/base.php"); - $title = "New post - ".$title; - include_once 'partials/header.php'; + + +if (!isset($textareaValue)) { + $textareaValue = ''; +} + +if (isset($_GET['hash'])) { + $hash = $_GET['hash']; + $textareaValue = "(#$hash) "; + + // COPY from conv.php + // TODO: make into a partial or global function + // Get the hashes (both post and replies) as $hash from the router and return an inverted list + $twt_op = array_filter($twts, function($twt) use ($hash) { + return $twt->hash === $hash; //|| $twt->replyToHash === $hash; + }); + //$twts = array_reverse($twts, true); + + //$textareaValue .= print_r($twts); + //$textareaValue .= $twts["nick"]; + + include_once 'partials/timeline.php'; +} + ?>
    @@ -138,7 +160,7 @@ if (isset($_POST['submit'])) {
    - + diff --git a/views/webmention_endpoint.php b/views/webmention_endpoint.php index 26296a9..bf8dfce 100644 --- a/views/webmention_endpoint.php +++ b/views/webmention_endpoint.php @@ -35,9 +35,9 @@ if (stristr($source, $_POST['target'])) { $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] + .'You were mentioned in: '.$_POST['source'].'' // "%0A" means new line + ."
" // add a line break before blockquote + ."> " . $twt[1] //."Recived webmention from ".$_POST['source'] //." mentioning ".$_POST['target'] //." (IP: ".$_SERVER['REMOTE_ADDR'].")" @@ -45,10 +45,8 @@ if (stristr($source, $_POST['target'])) { file_put_contents($logfile, $log, FILE_APPEND); - # Send email fork: https://gist.github.com/otherjoel/9301d985622f0d3d1a09 - } else { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');