-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathextension.php
More file actions
223 lines (189 loc) · 5.53 KB
/
extension.php
File metadata and controls
223 lines (189 loc) · 5.53 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
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
<?php
require_once __DIR__ . "/vendor/autoload.php";
use \fivefilters\Readability\Readability;
use \fivefilters\Readability\Configuration;
class Af_ReadabilityExtension extends Minz_Extension
{
/** @var array<int,FreshRSS_Feed> */
private array $feeds;
/** @var array<int,FreshRSS_Category> */
private array $categories;
/** @var array<int,bool> */
private array $configFeeds = [];
/** @var array<int,bool> */
private array $configCategories = [];
public function init()
{
$this->registerHook('entry_before_insert', array($this, 'processArticle'));
Minz_View::appendStyle($this->getFileUrl('style.css'));
}
/**
* @throws Minz_PermissionDeniedException
*/
public function processArticle(FreshRSS_Entry $article): FreshRSS_Entry
{
$this->loadConfigValues();
$feedId = $article->feedId();
$categoryId = $article->feed()?->category()?->id();
if (!array_key_exists($feedId, $this->configFeeds)
&& (null === $categoryId || !array_key_exists($categoryId, $this->configCategories))
) {
return $article;
}
$extractedContent = $this->extractContent($article->link());
$contentTest = is_string($extractedContent) ? trim(strip_tags($extractedContent)) : null;
if (!empty($contentTest)) {
$article->_content((string)$extractedContent);
}
return $article;
}
/** @return array<int,FreshRSS_Feed> */
public function getFeeds(): array
{
return $this->feeds;
}
/** @return array<int,FreshRSS_Category> */
public function getCategories(): array
{
return $this->categories;
}
/**
* @throws Minz_PermissionDeniedException
*/
private function loadConfigValues(): void
{
if (!class_exists('FreshRSS_Context', false)) {
echo "Failed data";
return;
}
try {
$userConf = FreshRSS_Context::userConf();
}
catch(\Throwable $t) {
Minz_Log::warning('af-readability: ' . $t->getMessage());
return;
}
$this->configFeeds = $this->readConfigValue($userConf, 'ext_af_readability_feeds');
$this->configCategories = $this->readConfigValue($userConf, 'ext_af_readability_categories');
}
/** @return array<int,bool> */
private function readConfigValue(FreshRSS_UserConfiguration $userConf, string $configKey): array
{
if('' === $configKey) {
return [];
}
$value = $userConf->attributeString($configKey);
if ($value == '') {
return [];
}
$decoded = (array)json_decode($value, true);
$result = [];
foreach($decoded as $key => $param) {
$result[(int)$key] = (bool) $param;
}
return $result;
}
public function getConfigFeeds(int $id): bool
{
return array_key_exists($id, $this->configFeeds);
}
public function getConfigCategories(int $id): bool
{
return array_key_exists($id, $this->configCategories);
}
/**
* @throws FreshRSS_Context_Exception
* @throws Minz_ConfigurationNamespaceException
* @throws Minz_PDOConnectionException
* @throws Minz_PermissionDeniedException
*/
public function handleConfigureAction()
{
$feedDAO = FreshRSS_Factory::createFeedDao();
$catDAO = FreshRSS_Factory::createCategoryDao();
$this->feeds = $feedDAO->listFeeds();
$this->categories = $catDAO->listCategories(true,false);
if (Minz_Request::isPost()) {
$configFeeds = [];
foreach ($this->feeds as $f) {
if (Minz_Request::paramBoolean("feed_".$f->id())){
$configFeeds[$f->id()] = true;
}
}
$configCategories = [];
foreach ($this->categories as $c) {
if (Minz_Request::paramBoolean("cat_".$c->id())){
$configCategories[$c->id()] = true;
}
}
FreshRSS_Context::userConf()->_attribute('ext_af_readability_feeds', (string)json_encode($configFeeds));
FreshRSS_Context::userConf()->_attribute('ext_af_readability_categories', (string)json_encode($configCategories));
FreshRSS_Context::userConf()->save();
}
$this->loadConfigValues();
}
/**
* @throws Minz_PermissionDeniedException
*/
private function extractContent(string $url): bool|string|null
{
if(empty($url)) {
return false;
}
$ch = curl_init();
if(false === $ch) {
return false;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, FRESHRSS_USERAGENT);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: text/*',
'Content-Type: text/html'
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
$redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
if (!empty($redirectUrl)) {
$url = $redirectUrl;
}
curl_close($ch);
if (!is_string($response) || mb_strlen($response) > 1024 * 500) {
return false;
}
$document = new DOMDocument("1.0", "UTF-8");
libxml_use_internal_errors(true);
if (!$document->loadHTML('<?xml encoding="UTF-8">' . $response)) {
libxml_clear_errors();
return false;
}
libxml_clear_errors();
if (null === $document->encoding || strtolower($document->encoding) !== 'utf-8') {
$responseReplaced = preg_replace("/<meta.*?charset.*?\/?>/i", "", $response);
$response = null !== $responseReplaced ? $responseReplaced : $response;
if (empty($document->encoding)) {
$response = mb_convert_encoding($response, 'utf-8');
} else {
$response = mb_convert_encoding($response, 'utf-8', $document->encoding);
}
}
try {
$r = new Readability(new Configuration([
'FixRelativeURLs' => true,
'OriginalURL' => $url,
'ExtraIgnoredElements' => ['template'],
]));
if ($r->parse($response)) {
return $r->getContent();
}
}
catch(\Throwable $t) {
Minz_Log::warning('af-readability: ' . $t->getMessage());
return false;
}
return false;
}
}