diff options
| author | Philip Wittamore <philip@wittamore.com> | 2026-07-12 11:08:50 +0200 |
|---|---|---|
| committer | Philip Wittamore <philip@wittamore.com> | 2026-07-12 11:08:50 +0200 |
| commit | 420ceb49b53a8fa9d8ba8443e42e50cd7bd7cea9 (patch) | |
| tree | 3009f0564d75e3f7d1ca38507e7ebb92deaded07 /scripts/getfeeds_http.php | |
update
Diffstat (limited to 'scripts/getfeeds_http.php')
| -rw-r--r-- | scripts/getfeeds_http.php | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/scripts/getfeeds_http.php b/scripts/getfeeds_http.php new file mode 100644 index 0000000..eaa03eb --- /dev/null +++ b/scripts/getfeeds_http.php @@ -0,0 +1,167 @@ +<?php +/* + Build rss feed pages + creates the necessary index.phps using + /feeds/feedname/feed and /feeds/feedname/title + feed contains the feed url, title is whatever title you edit + requires simplepie https://www.simplepie.org/ +*/ + +require_once('/srv/http/wittamore.fr/scripts/SimplePie.compiled.php'); + +$root = "/srv/http/wittamore.fr/feeds"; +$cache = "/srv/http/wittamore.fr/.cache"; +$fs = "feed"; +$ts = "title"; +$dir = glob($root . '/*' , GLOB_ONLYDIR); + +$dt = new DateTime("now", new DateTimeZone('Europe/Paris')); +$curdir = str_replace($_SERVER["DOCUMENT_ROOT"],"",__DIR__); + +// build header +$header = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n"; +$header .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" >\n"; +$header .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" >\n"; +$header .= "<meta name=\"description\" content=\"my rss feeds\" >\n"; +$header .= "<link href=\"/style.css\" rel=\"stylesheet\" type=\"text/css\" >\n"; +$header .= "<link rel=\"icon\" href=\"/favicon.ico\" type=\"image/x-icon\" >\n"; +$header .= "<title>My feeds</title>\n</head>\n<body>\n<nav>\n"; +$header .= "<header>\n<h1>Bloggings</h1>\n<nav>\n"; +$header .= "<p>\n<a + href=\"/index.php\">Blog</a> | <a + href=\"/static/index.php\">Static</a> | <a + href=\"/search.php\">Search</a> | <a + href=\"https://git.wittamore.fr\" target=\"_blank\" title=\"Git\">Git</a> | <a + href=\"https://social.wittamore.fr/philip\" target=\"_blank\" title=\"Fediverse\">Fedi</a> | <b + >Feeds</b> | <a + href=\"/rss.xml\">RSS</a>\n"; +$header .= "</p>\n</nav>\n<div class=\"banner\">\n<img src=\"/images/me.jpg\" alt=\"logo\">\n"; +$header .= "<p>Comments: <a href=\"https://social.wittamore.fr/philip\">@philip</a><br>"; +$header .= "Contact: <a href=\"https://i.delta.chat/#ED59E209CCA23D16F40EB7F4AC0F8F0CFE934057&v=3&i=6G6xPiCMaT--yhMyn4r_1q-G&s=GB9pE6SoqzUpITR3XffnKQ1J&a=scu9zmbso%40nine.testrun.org&n=Philip+Wittamore\">Deltachat</a>\n</p>\n</div>\n</header>"; + +// start main index +$headmi = "<h2>RSS FEEDS</h2>"; +$headmi .= "<h4>last update: ".$dt->format('Y-m-d H:i:s')."</h4>\n"; +$headmi .= "<table><thead><tr><th>Type</th><th>Feed</th></tr></thead><tbody>\n"; +file_put_contents("$root/index.php", $header.$headmi); + +// iterate through feed directories +foreach ($dir as $subdir) { + // get feed settings from directory + if (!file_exists("$subdir/$fs")) continue; + if (!file_exists("$subdir/$ts")) continue; + + $feeditemtitle = fgets(fopen("$subdir/$ts", 'r')); + $feeditemtitle = str_replace("\n", '', $feeditemtitle); + + $feedurl = fgets(fopen("$subdir/$fs", 'r')); + $feedurl = str_replace("\n", '', $feedurl); + + // new instance of simplepie + $feed = new SimplePie(); + + // simplepie can't recuperate a gopher url + // so we use curl in this case + $gopher = 0; + $rssstring = ""; + if (str_starts_with($feedurl, "gopher")) { + exec("curl $feedurl", $rssdata, $retval); + if (!empty($rssdata)) { + $rssstring=implode($rssdata); + $feed->set_raw_data($rssstring); + $gopher=1; + }else{ + $itemlist .= "<p>Error $retval retrieving $feedurl</p>\n"; + } + }else{ + $feed->set_feed_url($feedurl); + } + + // initialise simplepie + $feed->set_cache_location($cache); + $feed->enable_order_by_date(true); + $feed->handle_content_type(); + $feed->init(); + + $feedtitle = $feed->get_title(); + + // extract each feed item + $x=0; + $youtube=false; + $itemlist=""; + foreach ($feed->get_items(0, 30) as $item) { + + $itemlink = $item->get_permalink(); + $itemtitle = $item->get_title(); + $itemdate = $item->get_date("Y-m-d"); + + if (empty($itemtitle)) $itemtitle="No item title found"; + + // build item link + if ($gopher==1) { + $host = parse_url($itemlink, PHP_URL_HOST); + $port = parse_url($itemlink, PHP_URL_PORT); + if ((int)$port < 70) $port="70"; + $path = parse_url($itemlink, PHP_URL_PATH); + if (pathinfo($path, PATHINFO_EXTENSION)) { + $itemlist .= "<tr><td>".$itemdate."</td><td><a href=\"gopher://" . $host . ":" .$port ."/0".substr($path,2) . + "\">" .$itemtitle."</a></td></tr>\n"; + }else{ + $itemlist .= "<tr><td>".$itemdate."</td><td><a href=\"gopher://" . $host . ":" .$port ."/1".substr($path,2) . + "\">" .$itemtitle."</a></td></tr>\n"; + } + }else{ + // convert rss youtube url to youtu.be url + if (str_contains($itemlink,"youtu")) { + $youtube=true; + $itemlink = str_replace("www.youtube.com/watch?v=", "youtu.be/", $itemlink); + } + $itemlist .= "<tr><td>".$itemdate."</td><td><a href=\"".$itemlink."\">".$itemtitle."</a></td></tr>\n"; + } + $x++; + } + + // build list of feed items + if ($x==0) { + // gopher error tag + if ( str_starts_with($rssstring,"3")) { + $itemlist .= "<tr><td></td>Error retrieving gopher feed</td></tr>\n"; + }else{ + $itemlist .= $rssstring; + } + } + + // build rss_feeds/feed/index.php + $index = $header; + $index .= "<h2>".$feedtitle."</h2>"; + $index .= "<h4><a href=\"".$feedurl."\">feed link</a></h4>\n"; + $index .= "<table><thead><tr><th>Date </th><th>Article</th></tr></thead><tbody>\n"; + $index .= $itemlist; + $index .= "</tbody></table>\n<p> </p>\n</main>\n</body>\n</html>"; + file_put_contents("$subdir/index.php", $index); + + // build rss_feeds/index.php + $feedline = explode(" - ", $feeditemtitle); + $feedname = "<tr><td>".$feedline[0]."</td><td><a href=\"".basename($subdir)."\">".$feedline[1]."</a></td></tr>\n"; + file_put_contents("$root/index.php", $feedname, FILE_APPEND); + + // reset + unset($rssstring); + unset($rssdata); + unset($sfeed); + unset($itemlist); +} + +// finish rss_feeds/index.php +file_put_contents("$root/index.php", "</tbody>\n</table>\n<p> </p>\n</main>\n</body>\n</html>", FILE_APPEND); + +?> + + + + + + + + + |
