aboutsummaryrefslogtreecommitdiffstats
path: root/rss.php
blob: 14caeaa13fbdd91a0d64b752d19def8a21f7b65a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php define("VERSION", "0.0.1");
/* --- FEEDS - EDIT AS NEEDED --- */

$feeds["OTW News"]       ["url"]  = "https://www.transformativeworks.org/category/announcement/feed/";
//$feeds["OTW News"]       ["home"] = "https://www.transformativeworks.org/";

$feeds["Dreamwidth News"]["url"]  = "https://dw-news.dreamwidth.org/data/rss";
$feeds["Dreamwidth News"]["home"] = "https://dw-news.dreamwidth.org/";

/* --- CONFIG - EDIT AS NEEDED --- */


/// Directory to store RSS cache.
///
/// Multiple instances can share one dir.
$config["cache_dir"] = "/tmp/rss_dot_php";


/// Custom CSS
$config["custom_css"] = <<<'EOC'

/* custom CSS goes here! */

EOC;


/// Document Language
$config["lang"] = "en";


/// Date Format
///
/// Displayed under every article, see
/// <https://www.php.net/manual/en/datetime.format.php>
/// for documentation.
$config["date_fmt"] = "l, M jS, Y, H:i T";


/// Timezone
///
/// A value of type DateTimeZone, see
/// <https://www.php.net/manual/en/class.datetimezone.php>
/// for documentation.
$config["timezone"] = new DateTimeZone('UTC');


/// <a target=? >
///
/// What to set for the target= attribute on generated links.
/// _top will redirect the main tab, _blank wil make a nwe tab
$config["link_target"] = "_top";


/* --- CODE - DO NOT TOUCH --- */

function load_rss(string $uri, string $linkrel = "alternate"): array {
  global $config;

  $xml = file_get_contents($uri);

  // if the file doesn't contain an encoding, attempt to read it from http headers and re-encode
  if (!preg_match("/^[^>]+encoding/", $xml) && str_starts_with($uri, "http")) {
    foreach ($http_response_header as $header) {
      if (!str_starts_with(strtolower($header), "content-type")) continue;
      if (preg_match("/(?<=charset=)[a-z0-9_-]+/i", $header, $matches)) {
        $xml = iconv($matches[0], "UTF-8", $xml);
        $doc = new DOMDocument(encoding: "UTF-8");
      }
      break;
    }
  }

  $doc ??= new DOMDocument();
  $doc->loadXML($xml);

  if ($doc->documentElement->nodeName == "rss") {
    // TODO: better rss / atom sniffing
    foreach ($doc->getElementsByTagName("item") as $node) {
      $data["title"] = $node->getElementsByTagName("title")
                            ?->item(0)?->textContent;
      $data["link"] ??= $node->getElementsByTagName("link")
                             ?->item(0)?->textContent;

      $data["date"] = new DateTime($node->getElementsByTagName("pubDate")
                                        ->item(0)->textContent);
      $data["date"]->setTimezone($config["timezone"]);

      $parsed[] = $data;
    }
  } else {
    // assume atom
    foreach ($doc->getElementsByTagName("entry") as $node) {
      $data["title"] = $node->getElementsByTagName("title")
                          ?->item(0)?->textContent;
      $data["links"] = [];
      foreach ($node->getElementsByTagName("link")->getIterator() as $link) {
        $date["links"][] = ["rel" => $link->getAttribute("rel"),
                          "href" => $link->getAttribute("href")];
        if ($link->getAttribute("rel") === $linkrel) {
          $data["link"] ??= $link->getAttribute("href");
        }
      }
      $data["link"] ??= @$data["links"][0];

      $data["date"] = $node->getElementsByTagName("published")
                           ?->item(0)?->textContent;
      $data["date"] ??= $node->getElementsByTagName("updated")
                             ?->item(0)?->textContent;
      $data["date"] = new DateTime($data["date"]);
      $data["date"]->setTimezone($config["timezone"]);

      $parsed[] = $data;
    }
  }

  return $parsed??[];
}

function load_cached(int $ttl, string $uri, string $linkrel = "alternate"): array {
  global $config;
  $path = $config["cache_dir"]."/".md5($uri);
//  echo $path."\n";
  if ((@filemtime($path) ?? 0) + $ttl < time()) {
//    echo "cache miss, loading over network\n";
    $data = load_rss($uri, $linkrel);
    file_put_contents($path, serialize($data));
    return $data;
  } else {
//    echo "cache hit, loading from file\n";
    return unserialize(file_get_contents($path));
  }
}

@mkdir($config["cache_dir"], recursive: true);

foreach ($_GET["disabled"]??[] as $idx => $feed) {
  if (!array_key_exists($feed, $feeds)) {
    unset($_GET["disabled"][$idx]);
    continue;
  }
  $off_feeds[$feed] = @$feeds[$feed];
  unset($feeds[$feed]);
}

$combined = [];
// Real Feed Processing Happens Here
foreach ($feeds as $name => $data) {
  if (!isset($data["url"])) {
    error_log("Feed \"$name\" missing url. Ignoring.");
    continue;
  }
  if (!isset($data["ttl"])) $data["ttl"] = 3600;
  if (!isset($data["linkrel"])) $data["linkrel"] = "alternate";

  foreach(load_cached($data["ttl"], $data["url"], $data["linkrel"]) as $entry) {
    $entry["source"] = $name;
    $entry["home"] = @$data["home"];
    $combined[] = $entry;
  }
}

// reverse-chronological by default
usort($combined, fn($a, $b) => $b["date"]->getTimestamp() <=> $a["date"]->getTimestamp());

if (isset($_GET["reverse"]))
  $combined = array_reverse($combined);

$base = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);

?>
<!doctype html>
<html lang="<?= $config['lang'] ?>">
<head>
  <meta charset="utf-8">
  <style><?= $config['custom_css'] ?></style>
</head>
<body>
  <nav>
    <div>
      <b>Toggle Feeds</b>:
<?php foreach ($feeds??[] as $name => $data):
  $query = $_GET;
  $query["disabled"][] = $name;
  $uri = $base."?".http_build_query($query);
?>
        <span class="source" data-source="<?= htmlentities($name) ?>">
          <a href="<?= htmlentities($uri) ?>"><?= htmlentities($name) ?></a>
        </span>
<?php endforeach; ?>
<?php foreach ($off_feeds??[] as $name => $data):
  $query = $_GET;
  $query["disabled"] = array_filter($query["disabled"], fn($x) => $x !== $name);
  $uri = $base."?".http_build_query($query);
?>
        <span class="source disabled" data-source="<?= htmlentities($name) ?>">
          <a href="<?= htmlentities($uri) ?>"><?= htmlentities($name) ?></a>
        </span>
<?php endforeach; ?>
    </div>
  </nav>
  <main>
<?php if (!count($combined) && isset($_GET['disabled'])): ?>
<h1>Looks like you filtered out everything...</h1>
<p>Try unfiltering some feeds!</p>
<?php endif;
      foreach ($combined as $entry): ?>
    <article>
      <h1><a target="<?= htmlentities($config['link_target']) ?>" href="<?= htmlentities($entry['link']) ?>"><?= htmlentities($entry['title'] ?? "[[[No Title]]]") ?></a></h1>
      <span class="source" data-source="<?= htmlentities($entry['source']) ?>">
        <?php if ($entry['home']): ?>
        <a target="<?= htmlentities($config['link_target']) ?>" href="<?= htmlentities($entry['home']) ?>"><?= htmlentities($entry['source']) ?></a>
        <?php else: ?>
        <?= htmlentities($entry['source']) ?>
        <?php endif; ?>
      </span>
      &bullet;
      <time datetime="<?= htmlentities($entry['date']->format(DateTime::ISO8601)) ?>">
        <?= htmlentities($entry['date']->format($config['date_fmt'])) ?>
      </time>
    </article>
<?php endforeach; ?>
  </main>
  <!-- generated by rss_dot_php <?= VERSION ?>
       https://git.aleteoryx.me/cgit/rss_dot_php -->
</body>
</html>