2019-07-25 09:30:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
chdir(__DIR__);
|
|
|
|
|
2019-07-25 10:48:38 +02:00
|
|
|
$options = getopt("c:");
|
|
|
|
|
|
|
|
$config = parse_ini_file($options['c'] ?? 'config.ini', true);
|
2019-07-25 09:30:24 +02:00
|
|
|
|
|
|
|
$feedUrl = $config['feed']['url'];
|
|
|
|
$hookUrl = $config['mattermost']['hook_url'];
|
|
|
|
$cacheFile = $config['cache']['file'];
|
|
|
|
$template = $config['mattermost']['message_template'];
|
|
|
|
|
2019-07-25 10:48:38 +02:00
|
|
|
|
2019-07-25 09:30:24 +02:00
|
|
|
if (file_exists($cacheFile)) {
|
|
|
|
$cache = json_decode(file_get_contents($cacheFile), true);
|
|
|
|
|
|
|
|
if ($cache === null) {
|
|
|
|
$cache = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$feedContent = file_get_contents($feedUrl);
|
|
|
|
|
|
|
|
preg_match_all('#<title><!\[CDATA\[(.*)\]\]></title>#isU', $feedContent, $titles, PREG_SET_ORDER);
|
|
|
|
preg_match_all('#<link>(.*)</link>#isU', $feedContent, $links, PREG_SET_ORDER);
|
|
|
|
|
|
|
|
$commands = [];
|
|
|
|
|
|
|
|
foreach ($titles as $key => $title) {
|
|
|
|
$link = $links[$key] ?? null;
|
|
|
|
|
|
|
|
if (!$link) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $title[1];
|
|
|
|
$link = $link[1];
|
|
|
|
|
|
|
|
$hash = md5($title.$link);
|
|
|
|
|
|
|
|
if (!isset($cache[$hash])) {
|
|
|
|
$cache[$hash] = true;
|
|
|
|
|
|
|
|
$json = json_encode([
|
|
|
|
'text' => sprintf($template, $title, $link),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$commands[] = sprintf(
|
|
|
|
'curl -i -X POST -H "Content-Type: application/json" -d %s %s',
|
|
|
|
escapeshellarg($json),
|
|
|
|
escapeshellarg($hookUrl)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($cacheFile, json_encode($cache));
|
|
|
|
|
|
|
|
foreach ($commands as $command) {
|
|
|
|
shell_exec($command);
|
|
|
|
}
|