forked from thefranke/rss-librarian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
librarian.php
560 lines (463 loc) · 15.2 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?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";
// echo "<pre>";
// echo $g_url_base."<br>";
// echo $g_url_librarian."<br>";
// echo $g_dir_feeds."<br>";
// echo "</pre>";
// Fetch parameters given to librarian
function fetch_param($param)
{
$params = $_GET;
foreach($params as $k => $v)
{
if ($k == $param)
return $v;
}
return "";
}
// Turn user ID into feed ID
function get_feed_id($user_id)
{
return $user_id;
// Later feature: Disconnect feed from user
//return hash('sha256', $user_id);
}
// Produce path for local feed file
function get_local_feed_file($param_id)
{
global $g_dir_feeds;
$file_hash = get_feed_id($param_id);
return $g_dir_feeds . "/" . $file_hash . ".xml";
}
// Update feed files with new header
function update_feed_file($user_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=' . $user_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($user_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($user_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://darch.dk/read/Readability.php?url=" . urlencode($url);
//echo "using ftr";
$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;
}
// Add URL to personal feed
function add_url($user_id, $param_url)
{
global $g_max_items;
$local_feed_file = get_local_feed_file($user_id);
$xml = update_feed_file($user_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--;
}
// TODO: Shoud these two checks not be swapped?
// If a URL already on the list then there is no need to check if it list is maxed out?
// check if item already exists
foreach($xml->channel->item as $item)
{
if ($item->link == $param_url)
return '<p class="notice">' . $param_url . ' already added</p>';
}
}
// 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 '<p class="notice"><a href="' . $param_url . '">' . $param_url . '</a> added</p>';
}
// Remove URL from personal feed
function delete_url($user_id, $param_remove)
{
$local_feed_file = get_local_feed_file($user_id);
$xml = update_feed_file($user_id);
if ($xml->channel->item)
{
// Find and remove item
$c = $xml->channel->item->count();
for( $i = 0; $i < $c; $i++)
{
$item = $xml->channel->item[$i];
if ($item->link == $param_remove)
{
//echo "<p>Remove [" . $i . "]" . $param_remove ."</p>";
unset($xml->channel->item[$i]);
} /*else
return '<p class="notice">Cannot delete link to <code>' . $param_remove .'</code>, since it was not fond in the list</p>';
*/
}
}
// write to rss file
file_put_contents($local_feed_file, $xml->asXml());
return '<p class="notice">Link to <a href="' . $param_remove . '">' . $param_remove . '</a> was removed</p>';
}
// Count number of feeds in feed directory
function count_feeds()
{
global $g_dir_feeds;
$filecount = count(glob($g_dir_feeds . "/*.xml"));
return $filecount;
}
?>
<!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="favicon.png">
<?php
$param_url = fetch_param("url");
$param_remove = fetch_param("remove");
$param_id = fetch_param("id");
$user_id = $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) . '">');
else
$user_id = hash('sha256', random_bytes(18));
?>
<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;
}
*/
html {
font-family: sans-serif;
line-height: 1.5;
margin: 1rem;
}
body {
max-width: 900px;
margin: 1rem auto;
}
header {
text-align: center;
}
header h1 {
/*border-bottom: thin dashed;*/
margin-bottom: 0.75rem;
}
header img {
height: 1.5rem;
}
header .nav {
border-top: thin dashed;
padding: 0.75rem 1.5rem;
}
.notice {
background-color: #ffdd55;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
}
form {
margin-top: 1rem;
margin-bottom: 1.5rem;
margin: 1.5rem 0;
text-align: center;
}
fieldset {
border-radius: 0.5rem;
margin: 0;
}
legend {
font-weight: bold;
padding: 0 0.5rem;
}
input[type=text] {
width: 100%;
moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
box-sizing:border-box;
margin-bottom: 0.5rem;
}
details {
margin: 1rem 0;
border: thin solid lightgray;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
}
details img {
max-width: 100%;
margin: auto;
}
.title {
font-weight: bold;
}
a.remove {
color: tomato;
text-decoration: none;
float: right;
}
.content {
margin-top: 0.5rem;
/*
border: thin solid lightgray;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
*/
}
.date {
font-size: small;
color: gray;
border-bottom: thin dashed;
display: block;
padding-bottom: 0.25rem;
margin-bottom: 0.25rem;
}
footer {
font-size: small;
border-top: thin dashed;
text-align: center;
padding-top: 0.5rem;
}
@media (prefers-color-scheme: dark) {
html {
background-color: #000;
color: #99c683;
}
a:link, a:visited {
color: #99c683;
}
img {
filter:invert(1);
}
}
</style>
</head>
<body>
<main>
<header>
<h1><img alt="logo" src="https://raw.githubusercontent.com/Warhammer40kGroup/wh40k-icon/master/src/svgs/librarius-02.svg"> RSS-Librarian</h1>
<?php
// Check if parameter was supplied to distinguish new users from existing ones
if ($param_id != "")
{
$personal_url = $g_url_librarian . '?id=' . $user_id;
$feed_id = get_feed_id($user_id);
$local_feed_file = get_local_feed_file($user_id);
//print_r('<h4>Managing articles of ' . substr($user_id, 0, 4) . '...' . substr($user_id, -4) .':</h4>');
print_r('<span class="nav">Your <a href="'. $personal_url .'">personal URL</a> and <a href="' . $local_feed_file . '">personal RSS feed</a></span>');
}
?> </header>
<form action="<?= $g_url_librarian ?>">
<fieldset>
<legend>Add URL to reading list</legend>
<input type="text" id="url" name="url">
<input type="hidden" id="id" name="id" value="<?= $user_id ?>">
<input type="submit" value="Add to feed">
</fieldset>
</form>
<?php
if ($param_url != "")
{
$result = add_url($user_id, $param_url);
print_r($result);
}
if ($param_remove != "")
{
$result = delete_url($user_id, $param_remove);
print_r($result);
}
// Build-in Reader
// based on: https://gist.github.com/mburst/5230448
if ($param_id) {
$feed_url = $g_url_base . '/read/' . $local_feed_file; // TODO: Make not hardcoded by detecting propper path
//echo '<p>'.$feed_url;
$entries = array();
$xml = simplexml_load_file($feed_url);
$entries = array_merge($entries, $xml->xpath("//item"));
//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});
}
//echo "<ol>";
//Print all the entries
foreach($entries as $entry)
{
?>
<!-- <li> -->
<details>
<summary>
<span class="title"><?= $entry->title ?></span>
(<a href="<?= $entry->link ?>" target="_blank"><?= parse_url($entry->link)['host'] ?></a>)
<a href="<?= $personal_url . "&remove=" .$entry->link ?>" class="remove">[x]</a>
</summary>
<div class="content">
<span class="date"><?= strftime('%d/%m/%Y %H:%M', strtotime($entry->pubDate)) ?></span>
<?= $entry->description ?>
</div>
</details>
<!-- </li> -->
<?php
}
// echo "</ol>";
?>
<footer>
<a href="https://github.com/thefranke/rss-librarian">RSS-Librarian on Github</a>
| Instance managing <?php echo count_feeds() ?> feeds
<?php
if ($param_id)
{
print_r(' | ');
print_r('More tools: <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>');
// BUG: Not working if rss-lib is in a folder like: domain.tld/reader/
}
?>
</footer>
</main>
</body>
</html>