-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.php
209 lines (176 loc) · 6.35 KB
/
extension.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
<?php
class ReadableExtension extends Minz_Extension {
private $readHost;
private $mercHost;
private $fiveHost;
private $feeds;
private $cats;
private $mStore;
private $rStore;
private $fStore;
private $cStore;
public function init() {
$this->registerHook('entry_before_insert', array($this, 'fetchStuff'));
Minz_View::appendStyle($this->getFileUrl('style.css', 'css'));
}
public function fetchStuff($entry) {
$this->loadConfigValues();
$host = '';
if (empty($entry->toArray()['id_feed'])){
$id = $entry->feed(false);
} else {
$id = $entry->toArray()['id_feed'];
}
$catid = $entry->feed()->category()->id();
if ( array_key_exists($id, $this->mStore) || array_key_exists($catid, $this->cStore["merc"]) ) {
$host = $this->mercHost."/parser?url=".$entry->link();
$c = curl_init($host);
}
if ( array_key_exists($id, $this->rStore) || array_key_exists($catid, $this->cStore["read"]) ) {
$host = $this->readHost;
$c = curl_init($host);
$data = "{\"url\": \"" . $entry->link() ."\"}";
$headers[] = 'Content-Type: application/json';
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
}
if ( array_key_exists($id, $this->fStore) || array_key_exists($catid, $this->cStore["ff"]) ) {
$host = $this->fiveHost."/extract.php?url=".$entry->link();
$c = curl_init($host);
}
if ($host === '')
return $entry;
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
$c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
//$c_error = curl_error($c);
curl_close($c);
if ($c_status !== 200) {
return $entry;
}
$val = json_decode($result, true);
if (empty($val) || empty($val["content"])) {
return $entry;
}
$entry->_content($val["content"]);
return $entry;
}
/*
* These are called from configure.phtml, which is controlled by handleConfigureAction(),
* thus values are already fetched from userconfig and FeedDAO.
*/
public function getReadHost() {
return $this->readHost;
}
public function getMercHost() {
return $this->mercHost;
}
public function getFiveHost() {
return $this->fiveHost;
}
public function getFeeds() {
return $this->feeds;
}
public function getCategories() {
return $this->cats;
}
/*
Loading basic variables from user storage
*/
public function loadConfigValues(){
if (!class_exists('FreshRSS_Context', false) || null === FreshRSS_Context::$user_conf) {
echo "Failed data";
return;
}
if (FreshRSS_Context::$user_conf->read_ext_read_host != '') {
$this->readHost = FreshRSS_Context::$user_conf->read_ext_read_host;
}
if (FreshRSS_Context::$user_conf->read_ext_merc_host != '') {
$this->mercHost = FreshRSS_Context::$user_conf->read_ext_merc_host;
}
if (FreshRSS_Context::$user_conf->read_ext_five_host != '') {
$this->fiveHost = FreshRSS_Context::$user_conf->read_ext_five_host;
}
if (FreshRSS_Context::$user_conf->read_ext_mercury != '') {
$this->mStore = json_decode(FreshRSS_Context::$user_conf->read_ext_mercury, true);
} else {
$this->mStore = [];
}
if (FreshRSS_Context::$user_conf->read_ext_readability != '') {
$this->rStore = json_decode(FreshRSS_Context::$user_conf->read_ext_readability, true);
} else {
$this->rStore = [];
}
if (FreshRSS_Context::$user_conf->read_ext_five != '') {
$this->fStore = json_decode(FreshRSS_Context::$user_conf->read_ext_five, true);
} else {
$this->fStore = [];
}
if (FreshRSS_Context::$user_conf->read_ext_cat != '') {
$this->cStore = json_decode(FreshRSS_Context::$user_conf->read_ext_cat, true);
} else {
$this->cStore = ["ff" => [], "merc" => [], "read" => []];
}
}
public function getConfStoreR($id ) {
return array_key_exists($id, $this->rStore);
}
public function getConfStoreM($id ) {
return array_key_exists($id, $this->mStore);
}
public function getConfStoreF($id ) {
return array_key_exists($id, $this->fStore);
}
public function getConfStoreCat($str, $id ) {
return $id != 'all' ? array_key_exists($id, $this->cStore[$str]) :
array_key_exists($id, $this->cStore["ff"]) ||
array_key_exists($id, $this->cStore["read"]) ||
array_key_exists($id, $this->cStore["merc"]);
}
/*
* handleConfigureAction() is only executed on loading and saving the extenstion's configuration page.
* If the Request type is POST, values are being saved. It looks weird, but I copied it from another example and it works flawlessly.
*/
public function handleConfigureAction()
{
$feedDAO = FreshRSS_Factory::createFeedDao();
$catDAO = FreshRSS_Factory::createCategoryDao();
$this->feeds = $feedDAO->listFeeds();
$this->cats = $catDAO->listCategories(true,false);
if (Minz_Request::isPost()) {
$mstore = [];
$rstore = [];
$fstore = [];
$cstore = ["ff" => [], "merc" => [], "read" => []];
foreach ( $this->feeds as $f ) {
//I rather encode only a few 'true' entries, than 400+ false entries + the few 'true' entries
if ((bool)Minz_Request::param("read_".$f->id(), 0)){
$rstore[$f->id()] = true;
}
if ((bool)Minz_Request::param("merc_".$f->id(), 0) ) {
$mstore[$f->id()] = true;
}
if ((bool)Minz_Request::param("ff_".$f->id(), 0) ) {
$fstore[$f->id()] = true;
}
}
foreach ( $this->cats as $c ) {
foreach ( array_keys($cstore) as $v ) {
if ((bool)Minz_Request::param($v . "_cat_".$c->id(), 0)){
$cstore[$v][$c->id()] = true;
}
}
}
// Json encoded, so you can easily view and debug in the user config file
FreshRSS_Context::$user_conf->read_ext_mercury = (string)json_encode($mstore);
FreshRSS_Context::$user_conf->read_ext_readability = (string)json_encode($rstore);
FreshRSS_Context::$user_conf->read_ext_five = (string)json_encode($fstore);
FreshRSS_Context::$user_conf->read_ext_cat = (string)json_encode($cstore);
FreshRSS_Context::$user_conf->read_ext_merc_host = (string)Minz_Request::param('read_mercury_host');
FreshRSS_Context::$user_conf->read_ext_read_host = (string)Minz_Request::param('read_readability_host');
FreshRSS_Context::$user_conf->read_ext_five_host = (string)Minz_Request::param('read_fivefilters_host');
FreshRSS_Context::$user_conf->save();
}
$this->loadConfigValues();
}
}