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
|
<?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);
?>
|