-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathfeed.php
175 lines (145 loc) · 5.44 KB
/
feed.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
<?php
declare(strict_types=1);
namespace spouts\rss;
use helpers\FeedReader;
use helpers\HtmlString;
use helpers\Image;
use Monolog\Logger;
use SimplePie;
use spouts\Item;
use spouts\Parameter;
/**
* Spout for fetching an rss feed
*
* @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>
*
* @extends \spouts\spout<SimplePie\Item>
*/
class feed extends \spouts\spout {
public string $name = 'RSS Feed';
public string $description = 'Get posts from plain RSS/Atom feed.';
public array $params = [
'url' => [
'title' => 'URL',
'type' => Parameter::TYPE_URL,
'default' => '',
'required' => true,
'validation' => [Parameter::VALIDATION_NONEMPTY],
],
];
/** URL of the source */
protected ?string $htmlUrl = null;
/** Title of the source */
protected ?string $title = null;
/** @var SimplePie\Item[] current fetched items */
private array $items = [];
private Logger $logger;
private FeedReader $feed;
private Image $imageHelper;
public function __construct(FeedReader $feed, Image $imageHelper, Logger $logger) {
$this->imageHelper = $imageHelper;
$this->logger = $logger;
$this->feed = $feed;
}
//
// Source Methods
//
public function load(array $params): void {
$feedData = $this->feed->load(htmlspecialchars_decode($params['url']));
$this->items = $feedData['items'];
$this->htmlUrl = $feedData['htmlUrl'];
$this->title = $feedData['title'];
}
public function getTitle(): ?string {
return $this->title;
}
public function getXmlUrl(array $params): ?string {
return isset($params['url']) ? html_entity_decode($params['url']) : null;
}
public function getHtmlUrl(): ?string {
return $this->htmlUrl;
}
/**
* @return \Generator<Item<SimplePie\Item>> list of items
*/
public function getItems(): iterable {
foreach ($this->items as $item) {
$id = (string) $item->get_id();
if (strlen($id) > 255) {
$id = md5($id);
}
$title = (string) $item->get_title();
// Atom feeds can contain HTML in titles, strip tags and convert to text.
$title = HtmlString::fromPlainText(htmlspecialchars_decode(strip_tags($title)));
$content = HtmlString::fromRaw((string) $item->get_content());
$thumbnail = null;
$icon = null;
$link = htmlspecialchars_decode((string) $item->get_link(), ENT_COMPAT); // SimplePie sanitizes URLs
$unixDate = $item->get_date('U');
$date = $unixDate !== null ? new \DateTimeImmutable('@' . $unixDate) : new \DateTimeImmutable();
$author = $this->getAuthorString($item);
yield new Item(
$id,
$title,
$content,
$thumbnail,
$icon,
$link,
$date,
$author,
$item
);
}
}
private function getAuthorString(SimplePie\Item $item): ?string {
$author = $item->get_author();
if (isset($author)) {
// Both are sanitized using SimplePie::CONSTRUCT_TEXT
// so they are plain text strings with escaped HTML special characters.
$name = $author->get_name();
$email = $author->get_email();
if ($name !== null) {
return htmlspecialchars_decode($name);
} elseif ($email !== null) {
return htmlspecialchars_decode($author->get_email() ?? '');
}
}
return null;
}
public function getIcon(): ?string {
// Try to use feed logo first
$feedLogoUrl = $this->feed->getImageUrl();
if ($feedLogoUrl && ($iconData = $this->imageHelper->fetchFavicon($feedLogoUrl)) !== null) {
[$faviconUrl, $iconBlob] = $iconData;
$aspectRatio = $iconBlob->getWidth() / $iconBlob->getHeight();
if (0.8 < $aspectRatio && $aspectRatio < 1.3) {
$this->logger->debug('icon: using feed logo: ' . $faviconUrl);
return $faviconUrl;
} else {
$this->logger->debug('icon: feed logo “' . $faviconUrl . '” not square enough with aspect ratio ' . $aspectRatio . '. Not using it.');
}
}
// else fallback to the favicon of the associated web page
$htmlUrl = $this->getHtmlUrl();
if ($htmlUrl && ($iconData = $this->imageHelper->fetchFavicon($htmlUrl, true)) !== null) {
[$faviconUrl, $iconBlob] = $iconData;
$this->logger->debug('icon: using feed homepage favicon: ' . $faviconUrl);
return $faviconUrl;
}
// else fallback to the favicon of the feed effective domain
$feedUrl = $this->feed->getFeedUrl();
if ($feedUrl && ($iconData = $this->imageHelper->fetchFavicon($feedUrl, true)) !== null) {
[$faviconUrl, $iconBlob] = $iconData;
$this->logger->debug('icon: using feed homepage favicon: ' . $faviconUrl);
return $faviconUrl;
}
return null;
}
public function destroy(): void {
$this->feed->__destruct();
unset($this->items);
$this->items = [];
}
}