This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
init.php
216 lines (175 loc) · 7.73 KB
/
init.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
<?php
class Af_Feedmod extends Plugin implements IHandler
{
private $host;
function about()
{
return array(
1.0, // version
'Replace feed contents by contents from the linked page', // description
'mbirth', // author
false, // is_system
);
}
function api_version()
{
return 2;
}
function init($host)
{
$this->host = $host;
$host->add_hook($host::HOOK_PREFS_TABS, $this);
# only allowed for system plugins: $host->add_handler('pref-feedmod', '*', $this);
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
}
function csrf_ignore($method)
{
$csrf_ignored = array("index", "edit");
return array_search($method, $csrf_ignored) !== false;
}
function before($method)
{
if ($_SESSION["uid"]) {
return true;
}
return false;
}
function after()
{
return true;
}
function hook_article_filter($article)
{
global $fetch_last_content_type;
$json_conf = $this->host->get($this, 'json_conf');
$owner_uid = $article['owner_uid'];
$data = json_decode($json_conf, true);
if (!is_array($data)) {
// no valid JSON or no configuration at all
return $article;
}
foreach ($data as $urlpart=>$config) {
if (strpos($article['link'], $urlpart) === false) continue; // skip this config if URL not matching
if (strpos($article['plugin_data'], "feedmod,$owner_uid:") !== false) {
// do not process an article more than once
if (isset($article['stored']['content'])) $article['content'] = $article['stored']['content'];
break;
}
switch ($config['type']) {
case 'xpath':
$doc = new DOMDocument();
$link = trim($article['link']);
if (version_compare(VERSION, '1.7.9', '>=')) {
$html = fetch_file_contents($link);
$content_type = $fetch_last_content_type;
} else {
// fallback to file_get_contents()
$html = file_get_contents($link);
// try to fetch charset from HTTP headers
$headers = $http_response_header;
$content_type = false;
foreach ($headers as $h) {
if (substr(strtolower($h), 0, 13) == 'content-type:') {
$content_type = substr($h, 14);
// don't break here to find LATEST (if redirected) entry
}
}
}
$charset = false;
if (!isset($config['force_charset'])) {
if ($content_type) {
preg_match('/charset=(\S+)/', $content_type, $matches);
if (isset($matches[1]) && !empty($matches[1])) $charset = $matches[1];
}
} else {
// use forced charset
$charset = $config['force_charset'];
}
if ($charset && isset($config['force_unicode']) && $config['force_unicode']) {
$html = iconv($charset, 'utf-8', $html);
$charset = 'utf-8';
}
if ($charset) {
$html = '<?xml encoding="' . $charset . '">' . $html;
}
@$doc->loadHTML($html);
if ($doc) {
$basenode = false;
$xpath = new DOMXPath($doc);
$entries = $xpath->query('(//'.$config['xpath'].')'); // find main DIV according to config
if ($entries->length > 0) $basenode = $entries->item(0);
if ($basenode) {
// remove nodes from cleanup configuration
if (isset($config['cleanup'])) {
if (!is_array($config['cleanup'])) {
$config['cleanup'] = array($config['cleanup']);
}
foreach ($config['cleanup'] as $cleanup) {
$nodelist = $xpath->query('//'.$cleanup, $basenode);
foreach ($nodelist as $node) {
if ($node instanceof DOMAttr) {
$node->ownerElement->removeAttributeNode($node);
}
else {
$node->parentNode->removeChild($node);
}
}
}
}
$article['content'] = $doc->saveXML($basenode);
$article['plugin_data'] = "feedmod,$owner_uid:" . $article['plugin_data'];
}
}
break;
default:
// unknown type or invalid config
continue;
}
break; // if we got here, we found the correct entry in $data, do not process more
}
return $article;
}
function hook_prefs_tabs($args)
{
print '<div id="feedmodConfigTab" dojoType="dijit.layout.ContentPane"
href="backend.php?op=af_feedmod"
title="' . __('FeedMod') . '"></div>';
}
function index()
{
$pluginhost = PluginHost::getInstance();
$json_conf = $pluginhost->get($this, 'json_conf');
print "<form dojoType=\"dijit.form.Form\">";
print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
evt.preventDefault();
if (this.validate()) {
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
if (transport.responseText.indexOf('error')>=0) notify_error(transport.responseText);
else notify_info(transport.responseText);
}
});
//this.reset();
}
</script>";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_feedmod\">";
print "<table width='100%'><tr><td>";
print "<textarea dojoType=\"dijit.form.SimpleTextarea\" name=\"json_conf\" style=\"font-size: 12px; width: 99%; height: 500px;\">$json_conf</textarea>";
print "</td></tr></table>";
print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".__("Save")."</button>";
print "</form>";
}
function save()
{
$json_conf = $_POST['json_conf'];
if (is_null(json_decode($json_conf))) {
echo __("error: Invalid JSON!");
return false;
}
$this->host->set($this, 'json_conf', $json_conf);
echo __("Configuration saved.");
}
}