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
|
<?php
/*
Build rss feed pages for gopher
creates the necessary gophermaps 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('/root/bin/SimplePie.compiled.php');
$root = "/srv/gopher/feeds";
$cache= "/srv/gopher/cache";
$fs = "feed";
$ts = "title";
$hs = "header";
$dir = glob($root . '/*' , GLOB_ONLYDIR);
if (file_exists("$root/$hs")) {
copy("$root/$hs", "$root/gophermap");
$dt = new DateTime("now", new DateTimeZone('Europe/Paris'));
file_put_contents("$root/gophermap", "last update: ".$dt->format('Y-m-d H:i:s')."\n\n", FILE_APPEND);
}else{
echo "$root/$hs not found, aborting";
exit;
}
// iterate through feed directories
// each directory contains the feed url and title
// and receives the constructed gophermap
foreach ($dir as $subdir) {
# if config files missing go to next 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);
// feed list header
$itemlist = "RSS feed : " . $feedurl."\n\n";
// 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 .= "Error $retval retrieving $feedurl\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();
// extract each feed item
$x=0;
$youtube=false;
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 .= "0".$itemdate." ".$itemtitle."\t".substr($path,2)."\t".$host."\t".$port."\n";
}else{
$itemlist .= "1".$itemdate." ".$itemtitle."\t".substr($path,2)."\t".$host."\t".$port."\n";
}
}else{
//
if (str_contains($itemlink,"youtu")) {
// convert rss youtube url to youtu.be url
$youtube=true;
$itemlink = str_replace("www.youtube.com/watch?v=", "youtu.be/", $itemlink);
}
$itemlist .= "U".$itemdate." ".$itemtitle."\tURL:".$itemlink."\n";
}
$x++;
}
// build list of feed items
if ($x==0) {
// gopher error tag
if ( str_starts_with($rssstring,"3")) {
$itemlist .= "$rssstring\t/errors.txt\tspike.nagatha.fr\t70\n";
}else{
$itemlist .= $rssstring;
}
}
file_put_contents("$subdir/gophermap", $itemlist);
file_put_contents("$subdir/gophermap", str_repeat("\n ", 30), FILE_APPEND);
// build list of feeds
$feedname = "1".$feeditemtitle."\t".basename($subdir)."\n";
file_put_contents("$root/gophermap", $feedname, FILE_APPEND);
// reset
unset($rssstring);
unset($rssdata);
unset($sfeed);
unset($itemlist);
}
// add empty lines to gophermap
file_put_contents("$root/gophermap", str_repeat("\n ", 30), FILE_APPEND);
?>
|