Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Add simple file logging
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
#
# Webmention Emailer
# ------------------
#
# An incremental evolution of Jeremy Keith's "minimum viable" webmention,
# I've added some minimal code to have webmentions emailed to an address
# of your choosing. Also logs the webmention with the sender's IP.
if (!isset($_POST['source']) || !isset($_POST['target'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
ob_start();
$ch = curl_init($_POST['source']);
curl_setopt($ch,CURLOPT_USERAGENT,'mydomain (webmention.org)');
curl_setopt($ch,CURLOPT_HEADER,0);
$ok = curl_exec($ch);
curl_close($ch);
$source = ob_get_contents();
ob_end_clean();
header($_SERVER['SERVER_PROTOCOL'] . ' 202 Accepted');
if (stristr($source, $_POST['target'])) {
/* Configure these variables.
* If you're sending "from" yourself to yourself then there
* will be a some duplication :)
*/
$to_address = 'you@yourdomain.net';
$from_address = 'sender@sender.com';
$from_name = 'Sender Real Name';
$reply_to = 'Real Name <reply@sender.com>';
$subject = 'Webmention from '.$_POST['source'];
$logfile = '/full/path/to/webmentions.log'; /* Make sure file is writeable */
$charset = 'UTF-8';
$sep = "\r\n"; /* Change to '\n' for Windows */
$log = "[".date("c")."] User: ".$_SERVER['REMOTE_ADDR']
.' - Valid webmention on: <'.$_POST['source']
.'> (target: <'.$_POST['target'].'>)'.PHP_EOL;
file_put_contents($logfile, $log, FILE_APPEND);
$headers = "From: $from_name <$from_address>".
$sep.'Reply-To: ' . $reply_to .
$sep.'X-Mailer: Mydomain (webmention.org)'.
$sep.'Content-Transfer-Encoding: 8bit'.
$sep.'Content-Type: text/plain; charset="'.$charset.'"'.
$sep;
$body = 'Valid webmention received!'.
$sep.
$sep.'- Mention found at: '.$_POST['source'].
$sep.
$sep.'- Page mentioned : '.$_POST['target'];
mail($to_address, $subject, $body, $headers, '-f'.$from_address);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment