This commit is contained in:
Ismo Vuorinen
2021-11-12 17:46:17 +02:00
parent f5a0a5ab4d
commit 59e6717fc2
10 changed files with 286 additions and 52 deletions

View File

@@ -1,35 +1,53 @@
<?php
/**
* xkcd-Mailer
*
* Sends HTML-email containing hotlinked latest xkcd
* Sends HTML-email containing hotlinked the latest xkcd
* strip with alt/title-text underneath the image.
*
* @category Default
* @package Default
* @author Ismo Vuorinen <ivuorinen@me.com>
* @author wojas <https://github.com/wojas>
* @author Raam Dev <https://github.com/raamdev>
* @link https://github.com/ivuorinen/xkcd-Mailer
* @license The MIT License http://www.opensource.org/licenses/mit-license.php
* @version 1.0.20140525
* @link https://github.com/ivuorinen/xkcd-Mailer
**/
*/
// Use config.example.php as base for your configurations.
$lastfile = "last.txt";
$here = dirname(__FILE__);
$here = __DIR__;
if (! is_readable($here . '/config.php')) {
die("Please configure me. I don't know where I should sent the comic. (Config file {$here}/config.php missing.)");
}
require_once $here . '/config.php';
$feed = "http://xkcd.com/atom.xml";
if (! isset($mail, $from) || empty($mail) || empty($from)) {
die('Configuration values $mail and $from cannot be empty');
}
$lastfile = $lastfile ?? 'last.txt';
$debug = $debug ?? false;
$send = $send ?? false;
$feed = "https://xkcd.com/atom.xml";
/**
* Get xml feed and parse it.
* Checks if http(s):// wrapper is allowed.
*
* @param string $feed Feed to fetch and parse.
*
* @return \SimpleXMLElement
*/
function getFeed(string $feed): SimpleXMLElement
{
if (ini_get('allow_url_fopen')) {
// https://bugs.php.net/bug.php?id=62577
return simplexml_load_string(file_get_contents($feed));
}
// Check if http:// wrapper is allowed
if (ini_get('allow_url_fopen')) {
$data = simplexml_load_file($feed);
} else {
// If http:// wrapper is disabled (by allow_url_fopen=0, for example), then fall back on cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
@@ -40,19 +58,17 @@ if (ini_get('allow_url_fopen')) {
$result = curl_exec($ch);
curl_close($ch);
$data = simplexml_load_string($result);
return simplexml_load_string($result);
}
$data = getFeed($feed);
$item = $data->entry[0];
$last = 0;
if (file_exists($lastfile)) {
$f = fopen($lastfile, 'r');
$last = (int) fread($f, 1024);
fclose($f);
}
$last = file_exists($lastfile)
? file_get_contents($lastfile)
: 0;
$parts = explode('/', $item->id);
$parts = explode('/', $item->id, 4);
$current = (int) $parts[3];
if ($current > $last) {
@@ -60,25 +76,45 @@ if ($current > $last) {
preg_match("#title=\"(.+)\"#iU", $item->summary, $t);
// To send HTML mail, the Content-type header must be set
//$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: '. $from . "\r\n";
$headers = "Content-type: text/html; charset=UTF-8\r\n" .
sprintf("From: %s\r\n", $from);
$subject = "xkcd {$date}: {$item->title}";
$punchline = $t[1];
$subject = sprintf('xkcd %s: %s', $date, $item->title);
$punchline = $t[1];
$msg = "<html><body><h1><a href=\"{$item->id}\">{$item->title}</a></h1>\n"
. "<small>Posted {$date}</small><br />\n"
. $item->summary."<br />\n"
. "<p>{$punchline}</p></body></html>\n";
$title = sprintf('<h1><a href="%s">%s</a></h1>', $item->id, $item->title);
$body = sprintf(
'<small>Posted %s</small><br>%s<br><p>%s</p>',
$date,
$item->summary,
$punchline
);
mail($mail, $subject, $msg, $headers);
$msg = sprintf(
"<html lang='en'><body>%s\n%s</body></html>",
$title,
$body
);
$f = fopen($lastfile, 'w');
fwrite($f, $current);
fclose($f);
if ($send) {
mail($mail, $subject, $msg, $headers);
} else {
echo $msg . "\n\n";
}
echo "New last is $current (was $last)\n";
} else {
echo "No new XKCD: last=$last current=$current\n";
$file_write_result = file_put_contents($lastfile, $current);
if (! $file_write_result) {
echo "Error writing to file: $lastfile\n";
exit(1);
}
if ($debug) {
echo "New last is $current (was $last)\n";
}
exit(0);
}
if ($debug) {
echo sprintf("No new XKCD: last=%d current=%d\n", $last, $current);
}