forked from wltb/ff_feedcleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.php
427 lines (367 loc) · 12.4 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
class ff_FeedCleaner extends Plugin
{
private $host;
private $debug;
function about()
{
return array(
0.9, // version
'Applies regular expressions to a feed', // description
'feader', // author
false, // is_system
);
}
function api_version() {
return 2;
}
function init($host) {
$this->host = $host;
$host->add_hook($host::HOOK_PREFS_TABS, $this);
$host->add_hook($host::HOOK_FEED_FETCHED, $this);
$host->add_hook($host::HOOK_FEED_PARSED, $this);
}
//implement fetch hooks
function hook_feed_fetched($feed_data, $fetch_url)
{
$json_conf = $this->host->get($this, 'json_conf');
$debug_conf = sql_bool_to_bool($this->host->get($this, 'debug', bool_to_sql_bool(FALSE)));
$this->debug = $debug_conf; // || $this->host->get_debug();
try {
list($feed_data, $this->feed_parsed) = self::hook1($feed_data, $fetch_url, $json_conf, $this->debug);
} catch (Exception $e) {
user_error($e->getMessage(), E_USER_WARNING);
}
return $feed_data;
}
static function hook1($feed_data, $fetch_url, $json_conf, $debug=False) {
$data = json_decode($json_conf, true);
if (!$data || !is_array($data)) throw new Exception('No or malformed configuration stored');
$later = array();
foreach($data as $config) {
$test = false;
if(array_key_exists('URL', $config))
$test = (strpos($fetch_url, $config['URL']) !== false);
elseif(array_key_exists('URL_re', $config))
$test = (preg_match($config['URL_re'], $fetch_url) === 1);
else
user_error('For ' . json_encode($config) . ': Neither URL nor URL_re key is present', E_USER_WARNING);
if( $test ){
if($debug)
user_error('Modifying ' . $fetch_url . ' with ' . json_encode($config), E_USER_NOTICE);
switch (strtolower($config["type"])) {
case "regex":
$feed_data = self::apply_regex($feed_data, $config, $debug);
break;
case "xpath_regex":
case "link_regex":
$later [] = $config;
break;
case "utf-8":
$feed_data = self::enc_utf8($feed_data, $config, $debug);
break;
default:
continue 2;
}
}
}
return array($feed_data, $later);
}
function hook_feed_parsed($rss) {
if(!$this->feed_parsed)
return;
self::hook2($rss, $this->feed_parsed, $this->debug);
}
static function hook2($rss, $config_data, $debug=False) {
static $p_xpath;
if(!$p_xpath) { #initialize reflection
$ref = new ReflectionClass('FeedParser');
$p_xpath = $ref->getProperty('xpath');
$p_xpath->setAccessible(true);
}
$xpath = $p_xpath->getValue($rss);
//$xpath->registerNamespace('rssfake', "http://purl.org/rss/1.0/");
foreach($config_data as $config) {
//var_dump($config);
switch (strtolower($config["type"])) {
case "xpath_regex":
self::apply_xpath_regex($xpath, $config, $debug);
break;
case "link_regex":
self::link_regex($rss, $config, $debug);
break;
}
}
}
static function link_regex($rss, $config, $debug) {
static $p_elem;
if(!$p_elem) {
$ref = new ReflectionClass('FeedItem_Common');
$p_elem = $ref->getProperty('elem');
$p_elem->setAccessible(true);
}
foreach($rss->get_items() as $item_caps) {
$url = $item_caps->get_link();
$new_url = self::apply_regex($url, $config, $debug);
if($new_url !== $url) {
$item = $p_elem->getValue($item_caps);
$link = $item->ownerDocument->createElementNS("http://www.w3.org/2005/Atom", 'link');
$link->setAttribute('href', $new_url);
$item->insertBefore($link, $item->firstChild);
if($new_url !== $item_caps->get_link())
user_error("$new_url wasn't set for " . json_encode($config)
. ". File an issue please.");
}
}
}
static function enc_utf8($feed_data, $config, $debug) {
$decl_regex =
'/^(<\?xml
[\t\n\r ]+version[\t\n\r ]*=[\t\n\r ]*["\']1\.[0-9]+["\']
[\t\n\r ]+encoding[\t\n\r ]*=[\t\n\r ]*["\'])([A-Za-z][A-Za-z0-9._-]*)(["\']
(?:[\t\n\r ]+standalone[\t\n\r ]*=[\t\n\r ]*["\'](?:yes|no)["\'])?
[\t\n\r ]*\?>)/x';
if (preg_match($decl_regex, $feed_data, $matches) === 1 && strtoupper($matches[2]) != 'UTF-8') {
mb_substitute_character("none");
$data = mb_convert_encoding($feed_data, 'UTF-8', $matches[2]);
if($data !== false)
{
$feed_data = preg_replace($decl_regex, $matches[1] . "UTF-8" . $matches[3], $data);
if($debug)
user_error('Encoding conversion to UTF-8 was successful', E_USER_NOTICE);
}
else
user_error('For ' . json_encode($config) . ": Couldn't convert the encoding", E_USER_WARNING);
}
else {
if($debug)
user_error('No encoding declared or encoding is UTF-8 already', E_USER_NOTICE);
}
return $feed_data;
}
static function apply_regex($feed_data, $config, $debug=false)
{
$pat = $config["pattern"];
$rep = $config["replacement"];
$feed_data_mod = preg_replace($pat, $rep, $feed_data, -1, $count);
if($feed_data_mod !== NULL)
$feed_data = $feed_data_mod;
else {
$count = 0;
}
if($debug)
user_error('Applied (pattern "' . $pat . '", replacement "' . $rep . '") ' . $count . ' times', E_USER_NOTICE);
return $feed_data;
}
static function apply_xpath_regex($xpath, $config, $debug=false) {
if(isset($config['namespaces']) && is_array($config['namespaces']))
foreach($config['namespaces'] as $prefix => $URI)
$xpath->registerNamespace($prefix, $URI);
else { //TODO remove this
$DNS = array(
"http://www.w3.org/2005/Atom",
"http://purl.org/rss/1.0/",
'http://purl.org/atom/ns#',
);
foreach($DNS as $URI) {
if($xpath->document->isDefaultNamespace($URI)) {
$xpath->registerNamespace("DNS", $URI);
break;
}
}
}
$node_list = $xpath->query($config['xpath']);
$pat = $config["pattern"];
$rep = $config["replacement"];
if($debug)
user_error('Found ' . $node_list->length . ' nodes with XPath "' . $config['xpath'] . '"', E_USER_NOTICE);
$preg_rep_func = function($node) use ($pat, $rep, &$counter) {
if( $node->nodeType == XML_TEXT_NODE) {
$text_mod = preg_replace($pat, $rep, $node->textContent, -1, $count);
if($text_mod !== NULL) {
$node->nodeValue = $text_mod;
$counter += $count;
}
}
};
$counter = 0;
foreach($node_list as $node) {
$preg_rep_func($node);
if($node->hasChildNodes())
// This also works for DOMAttributes because apparently,
// their nodeValue is stored in a TextNode child.
foreach($node->childNodes as $child)
$preg_rep_func($child);
}
if($debug)
user_error('Applied (pattern "' . $pat . '", replacement "' . $rep . '") ' . $counter . ' times', E_USER_NOTICE);
}
//gui hook stuff
function hook_prefs_tabs($args)
{
print '<div id="' . strtolower(get_class()) . '_ConfigTab" dojoType="dijit.layout.ContentPane"
href="backend.php?op=pluginhandler&plugin=' . strtolower(get_class()) . '&method=index"
title="' . __('FeedCleaner') . '"></div>';
}
function index()
{
$pluginhost = PluginHost::getInstance();
$json_conf = $pluginhost->get($this, 'json_conf');
$debug = sql_bool_to_bool($this->host->get($this, "debug", bool_to_sql_bool(FALSE)));
if ($debug) {
$debugChecked = "checked=\"1\"";
} else {
$debugChecked = "";
}
?>
<div data-dojo-type="dijit/layout/AccordionContainer" style="height:100%;">
<div data-dojo-type="dijit/layout/ContentPane" title="<?php print __('Preferences'); ?>" selected="true">
<form dojoType="dijit.form.Form" accept-charset="UTF-8" style="overflow:auto;"
id="feedcleaner_settings">
<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault();
if (this.validate()) {
var values = this.getValues();
values.op = "pluginhandler";
values.method = "save";
values.plugin = "<?php print strtolower(get_class());?>";
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(values),
onComplete: function(transport) {
if (transport.responseText.indexOf('error')>=0) notify_error(transport.responseText);
else notify_info(transport.responseText);
}
});
//this.reset();
}
</script>
<table width='100%'><tr><td>
<textarea dojoType="dijit.form.SimpleTextarea" name="json_conf"
style="font-size: 12px; width: 99%; height: 500px;"
><?php echo htmlspecialchars($json_conf, ENT_NOQUOTES, 'UTF-8');?></textarea>
</td></tr></table>
<table width='30%' style="border:3px ridge grey;"><tr>
<td width="95%">
<label for="debug_id"><?php echo __("Enable extended logging");?></label>
</td>
<td class="prefValue">
<input dojoType="dijit.form.CheckBox" type="checkbox" name="debug" id="debug_id"
<?php print $debugChecked;?>>
</td>
</tr>
</table>
<p><button dojoType="dijit.form.Button" type="submit"><?php print __("Save");?></button></p>
</form>
</div>
<div data-dojo-type="dijit/layout/ContentPane" title="<?php print __('Show Diff'); ?>">
<form dojoType="dijit.form.Form">
<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault();
if (this.validate()) {
var values = this.getValues();
values.json_conf = dijit.byId("feedcleaner_settings").value.json_conf;
values.op = "pluginhandler";
values.method = "preview";
values.plugin = "<?php print strtolower(get_class());?>";
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(values),
onComplete: function(transport) {
if (transport.responseText.indexOf('error:')==0) notify_error(transport.responseText);
else {
var preview = document.getElementById("preview");
preview.innerHTML = transport.responseText;//textContent
}
}
});
//this.reset();
}
</script>
URL: <input dojoType="dijit.form.TextBox" name="url"> <button dojoType="dijit.form.Button" type="submit"><?php print __("Preview"); ?></button>
</form>
<div id="preview" style="border:2px solid grey; min-height:2cm; max-width: 30cm;"><?php print __("Preview"); ?></div>
</div>
</div>
<?php
}
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);
$this->host->set($this, 'debug', checkbox_to_sql_bool($_POST["debug"]));
echo __("Configuration saved.");
}
// diff stuff
static function format_diff_array_html($ar) {
// TODO should make sure that everything is indeed utf-8.
// This may be not the case if below, the feed data is not loaded into a xml doc.
// (and not even then?)
$func = function($var) {return htmlspecialchars($var, ENT_QUOTES, "UTF-8");};//ENT_XML1 would be better?
$diff = array_map($func, $ar);
return implode("<br/>", $diff);
}
function preview()
{
$url = $_POST['url'];
$conf = $_POST['json_conf'];
// TODO The output should be better structured, the check with 'error:' is a bit clumsy
try {
$diff = self::compute_diff($url, $conf);
print self::format_diff_array_html($diff);
} catch (Exception $e) {
print "error: " . $e->getMessage();
}
}
static function format_save_tmp($data, $format=True) {
if($format) {
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$res = $xml->loadXML($data);
}
$filename = tempnam(sys_get_temp_dir(), '');
if($format && $res) $outXML = $xml->save($filename);
else {
$handle = fopen($filename, "w");
fwrite($handle, $data);
fclose($handle);
}
return array($filename, $format && $res);
}
const diff_cmd = 'diff -U 0 -s -w ';
static function compute_diff($url, $json_data) {
//TODO maybe use a library for computing the diff,
// see http://stackoverflow.com/questions/321294/ or https://github.com/chrisboulton/php-diff
$con = fetch_file_contents($url);
if(!$con) throw new Exception("Couldn't fetch $url");
try {
list($feed_data, $config_data) = self::hook1($con, $url, $json_data);
} catch (Exception $e) {
throw $e;
}
$rss = new FeedParser($feed_data);
$rss->init();
if(!$rss->error()) {
self::hook2($rss, $config_data);
$ref = new ReflectionClass('FeedParser');
$p_doc = $ref->getProperty('doc');
$p_doc->setAccessible(true);
$doc = $p_doc->getValue($rss);
$new_feed_data = $doc->saveXML();
list($old_file, $xml) = self::format_save_tmp($con);
list($new_file, $xml) = self::format_save_tmp($new_feed_data, $xml);
$diff = array();
$res = 2;
exec(self::diff_cmd . " $old_file $new_file", $diff, $res);
unlink($old_file);
unlink($new_file);
//var_dump($diff);
if($res <= 1) {
return $diff;
} else throw new Exception("Error computing diff");
} else throw new Exception("XML errors: {$rss->error()}");
}
}