Skip to content

Instantly share code, notes, and snippets.

@thinkier
Last active April 26, 2020 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkier/26d5531f98b4cac541e3933eb0cf83a5 to your computer and use it in GitHub Desktop.
Save thinkier/26d5531f98b4cac541e3933eb0cf83a5 to your computer and use it in GitHub Desktop.
PHP Script to fetch the RSS feed to debian security and send a webhook to discord when it has a new one. (This generates dsaCache where it's ran.) Prerequisite: PHP>= 5.6, php5-cron, php5-cli, read and write access to local directory.
<?php
/**
* Usage: php dsa-notification.php
* In cron (hourly): 0 * * * * cd /absolute/path/of/your/directory && php dsa-notification.php
*/
$hooks = ["https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}"];
if (php_sapi_name() == "cli") {
$knownDSA = json_decode(file_get_contents("dsaCache"), true);
$rssFeed = simplexml_load_string(file_get_contents("https://www.debian.org/security/dsa"));
$webhookPayload = array();
foreach ($rssFeed->item as $item) {
$title = ($item->title) . "";
if (!isset($knownDSA[$title])) {
$knownDSA[$title] = time();
$webhookPayload[$title] = [
'title' => $title,
'url' => ($item->link) . "",
'description' => ($item->description) . "",
'timestamp' => date("Y-m-d\\TH:i:s", strtotime(($item->children('dc', true)->date) . ""))
];
}
}
if (!empty($webhookPayload)) {
if (count($webhookPayload) > 10) {
$webhookPayload = array_slice($webhookPayload, 0, 10);
}
postWebhook(array("embeds" => array_reverse(array_values($webhookPayload))), $hooks);
}
file_put_contents("dsaCache", json_encode($knownDSA));
}
function postWebhook($dataArray = array(), $hooks) {
if (!empty($dataArray['embeds']) || !empty($dataArray['content'])) {
$dataJson = json_encode($dataArray);
foreach ($hooks as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($dataJson))
);
curl_exec($ch);
curl_close($ch);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment