-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathRss.php
More file actions
107 lines (88 loc) · 3.17 KB
/
Rss.php
File metadata and controls
107 lines (88 loc) · 3.17 KB
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
<?php
declare(strict_types=1);
namespace Selfoss\controllers;
use FeedWriter\RSS2;
use Selfoss\daos;
use Selfoss\daos\ItemOptions;
use Selfoss\helpers\Authentication;
use Selfoss\helpers\Configuration;
use Selfoss\helpers\View;
/**
* Controller for rss access
*
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
final readonly class Rss {
public function __construct(
private Authentication $authentication,
private Configuration $configuration,
private RSS2 $feedWriter,
private daos\Items $itemsDao,
private daos\Sources $sourcesDao,
private View $view
) {
}
/**
* rss feed
*/
public function rss(): void {
$this->authentication->ensureCanRead();
$this->feedWriter->setTitle($this->configuration->rssTitle);
$this->feedWriter->setChannelElement('description', '');
$this->feedWriter->setSelfLink($this->view->getBaseUrl() . 'feed');
$this->feedWriter->setLink($this->view->getBaseUrl());
// get sources
$lastSourceId = 0;
$lastSourceName = '';
$options = new ItemOptions($_GET);
// get items
$newestEntryDate = null;
$itemsToMark = [];
foreach ($this->itemsDao->get($options) as $item) {
if ($newestEntryDate === null) {
$newestEntryDate = $item['datetime'];
}
$newItem = $this->feedWriter->createNewItem();
// get Source Name
if ($item['source'] != $lastSourceId) {
foreach ($this->sourcesDao->getAll() as $source) {
if ($source['id'] == $item['source']) {
$lastSourceId = $source['id'];
$lastSourceName = $source['title'];
break;
}
}
}
$newItem->setTitle($this->sanitizeTitle($item['title'] . ' (' . $lastSourceName . ')'));
@$newItem->setLink($item['link']);
@$newItem->setId($item['link']);
$newItem->setDate($item['datetime']);
$newItem->setDescription(str_replace('"', '"', $item['content']));
// add tags in category node
foreach ($item['tags'] as $tag) {
$tag = trim($tag);
if (strlen($tag) > 0) {
$newItem->addElement('category', $tag);
}
}
$this->feedWriter->addItem($newItem);
$itemsToMark[] = $item['id'];
}
// mark as read
if ($this->configuration->rssMarkAsRead && count($itemsToMark) > 0) {
$this->itemsDao->mark($itemsToMark);
}
if ($newestEntryDate === null) {
$newestEntryDate = new \DateTime();
}
$this->feedWriter->setDate($newestEntryDate);
$this->feedWriter->printFeed();
}
private function sanitizeTitle(string $title): string {
$title = strip_tags($title);
$title = html_entity_decode($title, ENT_HTML5, 'UTF-8');
return $title;
}
}