diff --git a/README.md b/README.md
index c42ab29..2e9eb9f 100644
--- a/README.md
+++ b/README.md
@@ -15,6 +15,8 @@ $ cp config.ini.dist config.ini
Example of `config.ini` :
```ini
+debug = false
+
[feed]
url = "https://www.example.com/feed/"
diff --git a/index.php b/index.php
index c841660..9f0e161 100644
--- a/index.php
+++ b/index.php
@@ -2,48 +2,65 @@
chdir(__DIR__);
-$options = getopt("c:");
+$options = getopt('c:');
$config = parse_ini_file($options['c'] ?? 'config.ini', true);
+$debug = (bool) ($config['debug'] ?? false);
$feedUrl = $config['feed']['url'];
$hookUrl = $config['mattermost']['hook_url'];
$cacheFile = $config['cache']['file'];
$template = $config['mattermost']['message_template'];
-
+$cache = [];
+$commands = [];
if (file_exists($cacheFile)) {
$cache = json_decode(file_get_contents($cacheFile), true);
- if ($cache === null) {
+ if (null === $cache) {
$cache = [];
}
}
-$feedContent = file_get_contents($feedUrl);
+$xml = simplexml_load_file($feedUrl);
+$entries = array_merge($xml->xpath('//entry'), $xml->xpath('//item'));
-preg_match_all('#
#isU', $feedContent, $titles, PREG_SET_ORDER);
-preg_match_all('#(.*)#isU', $feedContent, $links, PREG_SET_ORDER);
+foreach ($entries as $entry) {
+ $title = (string) $entry->title;
+ $url = null;
-$commands = [];
+ foreach ($entry->link as $link) {
+ if (null !== $url) {
+ continue;
+ }
-foreach ($titles as $key => $title) {
- $link = $links[$key] ?? null;
+ $attributes = $link->attributes();
+ $content = (string) $link;
+ $href = (string) ($attributes['href'] ?? '');
+ $type = (string) ($attributes['type'] ?? '');
- if (!$link) {
+ if ('' !== $type && 'text/html' !== $type) {
+ continue;
+ }
+
+ if ('' !== $href) {
+ $url = $href;
+ } elseif ('' !== $content) {
+ $url = $content;
+ }
+ }
+
+ if (empty($url)) {
continue;
}
- $title = $title[1];
- $link = $link[1];
-
- $hash = md5($title.$link);
+ $hash = md5($title.$url);
if (!isset($cache[$hash])) {
$cache[$hash] = true;
$json = json_encode([
- 'text' => sprintf($template, $title, $link),
+ 'text' => sprintf($template, $title, $url),
]);
$commands[] = sprintf(
@@ -54,8 +71,14 @@ foreach ($titles as $key => $title) {
}
}
-file_put_contents($cacheFile, json_encode($cache));
+if (!$debug) {
+ file_put_contents($cacheFile, json_encode($cache));
+}
foreach ($commands as $command) {
- shell_exec($command);
+ if ($debug) {
+ echo $command, "\n";
+ } else {
+ shell_exec($command);
+ }
}