-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibrarian.php
471 lines (394 loc) · 13.9 KB
/
librarian.php
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/*
* RSS-Librarian - A read-it-later service for RSS purists
*
* https://github.com/thefranke/rss-librarian
*
*/
use fivefilters\Readability\Readability;
use fivefilters\Readability\Configuration;
use fivefilters\Readability\ParseException;
// Global configuration
$g_max_items = 100;
$g_url_base = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'];
$g_url_librarian = $g_url_base . $_SERVER["PHP_SELF"];
$g_dir_feeds = "feeds";
// Fetch parameters given to librarian
function fetch_param($param)
{
$params = $_GET;
foreach($params as $k => $v)
{
if ($k == $param)
return $v;
}
return "";
}
// Produce path for local feed file
function get_local_feed_file($param_id)
{
global $g_dir_feeds;
return $g_dir_feeds . "/" . $param_id . ".xml";
}
// Check if feed file exists
function feed_file_exists($param_id)
{
return file_exists(get_local_feed_file($param_id));
}
// Update feed files with new header
function update_feed_file($param_id)
{
global $g_dir_feeds;
global $g_url_librarian;
// check for subs dir
if (!is_dir($g_dir_feeds))
mkdir($g_dir_feeds);
$personal_url = $g_url_librarian . '?id=' . $param_id;
// recreate base file so changes in the header are put in with every new release
$new_rss_base_text = '<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>RSS-Librarian (' . substr($param_id, 0, 4) . ')</title>
<description>A read-it-later service for RSS purists</description>
<link>' . $personal_url . '</link>
<atom:link href="' . $personal_url . '" rel="self" type="application/rss+xml" />
</channel>
</rss>
';
$rss_xml = simplexml_load_string($new_rss_base_text);
$local_feed_file = get_local_feed_file($param_id);
// try to open local subscriptions and copy items over
$local_feed_text = @file_get_contents($local_feed_file);
if ($local_feed_text != "")
{
$old_rss_xml = simplexml_load_string($local_feed_text);
foreach($old_rss_xml->channel->item as $item)
sxml_append($rss_xml->channel, $item);
}
return $rss_xml;
}
// Helper to attach new XML element to existing one
function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from)
{
$toDom = dom_import_simplexml($to);
$fromDom = dom_import_simplexml($from);
$new_node = $toDom->ownerDocument->importNode($fromDom, true);
$firstSibling = $toDom->getElementsByTagName('item')->item(0);
$toDom->insertBefore($new_node, $firstSibling);
}
// Create XML element for an RSS item
function make_feed_item($url, $title, $author, $content)
{
$pub_date = date("D, d M Y H:i:s T");
$xmlstr = '<item>
<link>' . $url . '</link>
<title>' . $title . '</title>
<guid isPermaLink="true">' . $url .'</guid>
<description>'
. htmlspecialchars($content) .
'</description>
<author>' . $author . '</author>
<pubDate>' . $pub_date . '</pubDate>
</item>';
return new SimpleXMLElement($xmlstr);
}
// Extract content by piping through Readability.php
function extract_readability($url)
{
$autoload = __DIR__ . '/vendor/autoload.php';
$html = "";
if (file_exists($autoload))
{
require $autoload;
// Pretend to be a browser to have an increased success rate of
// downloading the contents compared to a simple `file_get_contents`.
// See https://stackoverflow.com/a/11680776.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$html = curl_exec($ch);
curl_close($ch);
}
// No local Readability.php installed, use FiveFilters
if ($html == "")
{
$feed_url = "https://ftr.fivefilters.net/makefulltextfeed.php?url=" . urlencode($url);
$feed_item = file_get_contents($feed_url);
// error handling remove everything until first <
$start = strpos($feed_item, "<");
$feed_item = substr($feed_item, $start);
$xml = simplexml_load_string($feed_item);
$ff_item = $xml->channel->item[0];
$title = $ff_item->title;
$content = $ff_item->description;
$author = "";
return make_feed_item($url, $title, $author, $content);
}
if (function_exists('tidy_parse_string'))
{
$tidy = tidy_parse_string($html, array(), 'UTF8');
$tidy->cleanRepair();
$html = $tidy->value;
}
$readability = new Readability(new Configuration([
'fixRelativeURLs' => true,
'originalURL' => $url,
]));
$item = "";
try
{
$readability->parse($html);
$title = $readability->getTitle();
$content = $readability->getContent();
$author = $readability->getAuthor();
$item = make_feed_item($url, $title, $author, $content);
}
catch (ParseException $e)
{
$item = make_feed_item($url, $url, $url, "Could not extract content: " . $e->getMessage());
}
return $item;
}
// Remove URL from personal feed
function remove_url($user_id, $param_url)
{
$local_feed_file = get_local_feed_file($user_id);
$local_feed_text = @file_get_contents($local_feed_file);
if ($local_feed_text != "")
{
$rss_xml = simplexml_load_string($local_feed_text);
$i = 0;
foreach($rss_xml->channel->item as $item)
{
if ($item->guid == $param_url)
{
unset($rss_xml->channel->item[$i]);
break;
}
$i++;
}
// write to rss file
file_put_contents($local_feed_file, $rss_xml->asXml());
return '<a href="' . $param_url . '">' . $param_url . '</a> removed';
}
return 'Url did not exist';
}
// Add URL to personal feed
function add_url($param_id, $param_url)
{
global $g_max_items;
$local_feed_file = get_local_feed_file($param_id);
$xml = update_feed_file($param_id);
if ($xml->channel->item)
{
// check max item count, remove anything beyond
$c = $xml->channel->item->count();
while ($c > $g_max_items)
{
unset($xml->channel->item[$c - 1]);
$c--;
}
// check if item already exists
foreach($xml->channel->item as $item)
{
if ($item->link == $param_url)
return "URL already added";
}
}
// fetch rss content and add
$item = extract_readability($param_url);
sxml_append($xml->channel, $item);
// write to rss file
file_put_contents($local_feed_file, $xml->asXml());
return '<a href="' . $param_url . '">' . $param_url . '</a> added';
}
// Count number of feeds in feed directory
function count_feeds()
{
global $g_dir_feeds;
$filecount = count(glob($g_dir_feeds . "/*.xml"));
return $filecount;
}
$param_url = fetch_param("url");
$param_id = fetch_param("id");
$param_delete = fetch_param("delete");
$user_id = $param_id;
if ($param_id != "" && $param_url != "")
{
if ($param_delete == "1")
{
$result = remove_url($param_id, $param_url);
http_response_code(302);
header('Location: ' .$g_url_librarian. '?id=' .$param_id);
die;
}
else
{
$result = add_url($param_id, $param_url);
http_response_code(302);
header('Location: ' .$g_url_librarian. '?id=' .$param_id);
die;
}
}
// Fetch personal URL string
function get_personal_url($param_id)
{
global $g_url_librarian;
return $g_url_librarian . '?id=' . $param_id;
}
// Print message containing RSS url and personal url
function show_user_urls($param_id, $show_header)
{
global $g_url_librarian;
if ($param_id == "")
return;
$personal_url = get_personal_url($param_id);
$local_feed_file = get_local_feed_file($param_id);
if ($show_header)
print_r('<h4>Managing articles of ' . substr($param_id, 0, 4) . '...' . substr($param_id, -4) .':</h4>');
print_r('Your <a href="'. $personal_url .'">personal URL</a> and <a href="' . $local_feed_file . '">personal RSS feed</a><br><br><br>');
}
function show_saved_urls($param_id)
{
if ($param_id == "")
return;
$local_feed_text = @file_get_contents(get_local_feed_file($param_id));
// try to open local subscriptions for printing
if ($local_feed_text != "")
{
$rss_xml = simplexml_load_string($local_feed_text);
if (count($rss_xml->channel->item) == 0)
return;
print_r('<div id="feed-items">Feed Items:<ol>');
foreach($rss_xml->channel->item as $item)
print_r('<li><a href="?id=' .$param_id. '&delete=1&url=' .urlencode($item->guid). '" onclick="return confirm(\'Delete?\')">❌</a> <a href="' .$item->guid. '" target="_blank">' .$item->title. '</a></li>');
print_r('</ol></div>');
}
}
// Print message with tools for RSS feed management and preview
function show_tools($param_id)
{
global $g_url_base;
if ($param_id == "")
return;
$personal_url = get_personal_url($param_id);
$local_feed_file = get_local_feed_file($param_id);
print_r('<br><br><br>
<h4>More tools:</h4>
<a href="javascript:window.location.href=\'' . $personal_url . '&url=\' + window.location.href">Feed boomarklet</a>,
<a href="https://feedreader.xyz/?url=' . urlencode($g_url_base . '/' . $local_feed_file) . '">Feed preview</a>');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>RSS-Librarian</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="https://raw.githubusercontent.com/Warhammer40kGroup/wh40k-icon/master/src/svgs/librarius-02.svg">
<?php
$param_url = fetch_param("url");
$param_id = fetch_param("id");
// User exists?
if ($param_id != "")
print('<link rel="alternate" type="application/rss+xml" title="RSS Librarian (' . substr($param_id, 0, 4) . ')" href="' . get_local_feed_file($param_id) . '">');
?>
<style>
html {
font-family: monospace;
font-size: 12pt;
color: #66397C;
text-align: center;
}
#main {
text-align: center;
margin: auto;
max-width: 75%;
display: inline-block;
}
hr {
border: 1px dashed;
}
a:link, a:visited {
color: #66397C;
}
ul {
margin: 10px;
}
#header {
text-align: center;
margin: 24pt;
}
h1, h2, h3, h4 {
margin: 0;
}
img {
width: 120pt;
}
#feed-items {
text-align: left;
}
@media (prefers-color-scheme: dark) {
html {
background-color: #000;
color: #99c683;
}
a:link, a:visited {
color: #99c683;
}
img {
filter:invert(1);
}
}
</style>
</head>
<body>
<div id="main">
<div id="header">
<img alt="" src="https://raw.githubusercontent.com/Warhammer40kGroup/wh40k-icon/master/src/svgs/librarius-02.svg">
<h1>RSS-Librarian</h1>
<h4>[<a href="https://github.com/thefranke/rss-librarian">Github</a>]</h4>
<br>
<hr>
<h4>Instance managing <?php echo count_feeds() ?> feeds</h4>
</div>
<?php
// Adding URL for the first time, make sure user has saved their URLs!
if ($param_id == "" && $param_url != "")
{
// Create new user id
$param_id = hash('sha256', random_bytes(18));
print_r('You are about to create a new feed.
<br><br>
<h4>Please confirm that you have saved the following two URLs before continuing!</h4>
<br>');
show_user_urls($param_id, false);
print_r('<form action="' . $g_url_librarian . '">
<input type="hidden" id="url" name="url" value="' . $param_url . '">
<input type="hidden" id="id" name="id" value="' . $param_id . '">
<input type="submit" value="Confirm">
</form>');
}
// Existing user adding a URL or fresh start
else
{
print_r('<h4>Paste a new URL here:</h4>
<form action="' . $g_url_librarian . '">
<input type="text" id="url" name="url">
<input type="hidden" id="id" name="id" value="' . $param_id . '">
<br>
<input type="submit" value="Add to feed">
</form>
<br><br>');
show_saved_urls($param_id);
if ($param_url != "")
{
$result = add_url($param_id, $param_url);
print_r($result);
}
show_tools($param_id);
}
?>
</div>
</body>
</html>