-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.php
150 lines (126 loc) · 4.83 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
<?php
include_once 'simplehtmldom/simple_html_dom.php';
include_once 'simplehtmldom/HtmlWeb.php';
include_once 'functions.php';
//$doc = file_get_html($_GET['url']);
$n = new simplehtmldom\HtmlWeb();
$doc = $n->load($_GET['url']);
//$doc = $n->load_fopen($_GET['url']);
// setup feed output
$rss = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"></rss>');
$rss->addAttribute('version', '2.0');
$channel = $rss->addChild('channel');
$channel->addChild('title', htmlentities2(strip_tags($doc->find("title", 0))) ?? "Feed");
$channel->addChild('link', $_GET['url']);
// remove scripts
foreach ($doc->find("script") as $el) $el->remove();
$scriptattrs = [
"onload", "onunload", "onclick", "ondblclick", "onmousedown", "onmouseup",
"onmouseover", "onmousemove", "onmouseout", "onfocus", "onblur", "onkeypress",
"onkeydown", "onkeyup", "onsubmit", "onreset", "onselect", "onchange"
];
$scriptattrstr = implode(", ", array_map(function ($x) { return "*[$x]"; }, $scriptattrs));
foreach ($doc->find($scriptattrstr) as $el) {
if ($el->attrs)
$el->attrs = array_diff_key($el->attrs, array_flip($scriptattrs));
}
// find container element
foreach ($doc->find($_GET['containersel']) as $container) {
$item = $channel->addChild('item');
// make links and image sources absolute
foreach ($container->find("a, img") as $el) {
foreach (["href", "src"] as $attr) {
if ($el->hasAttribute($attr)) {
$el->setAttribute($attr, getAbsoluteUrl($_GET['url'], $el->getAttribute($attr)));
}
}
}
// find title
$title = $container->find($_GET['titlesel'], 0);
$titlestr = $title != null ? htmlentities2(strip_tags($title->innertext)) : "No title";
$item->addChild('title', $titlestr);
// find link
if ($title) {
if ($title->hasAttribute("href")) {
$link = $title;
} else if ($_GET['usefirstlink']) {
$link = $container->find("a", 0);
} else {
$link = $title->find("a", 0);
}
if ($link != null && $link->hasAttribute("href")) {
//$actualurl = getAbsoluteUrl($_GET['url'], $link->getAttribute("href"));
$actualurl = $link->getAttribute("href");
} else {
$actualurl = $_GET['url'];
}
} else {
$actualurl = $_GET['url'];
}
$item->addChild('link', htmlentities2($actualurl));
// find date
$date = $container->find($_GET['datesel'], 0);
if ($date) {
if ($date->tag == "time" && $date->hasAttribute("datetime")) {
$datestr = $date->getAttribute("datetime");
} else {
$datestr = trim(strip_tags($date->innertext));
}
$dateval = date_create_from_format($_GET['datefmt'], $datestr);
$maybe_error = date_get_last_errors();
if ($maybe_error && $maybe_error['error_count'] > 0) {
print_r($maybe_error);
die();
}
$date_rfc = gmdate(DATE_RFC2822, $dateval->getTimestamp());
$item->addChild('pubDate', $date_rfc);
}
if ($title) $title->remove();
if ($link) $link->remove();
if ($date) $date->remove();
// remove other things we want gone
if (isset($_GET['removesel']) && is_array($_GET['removesel'])) {
foreach ($_GET['removesel'] as $removesel) {
foreach ($container->find($removesel) as $el) {
$el->remove();
}
}
}
// is the content the remainder or a specific thing?
$contentstr = "";
if (isset($_GET['contentsel']) && !empty($_GET['contentsel'])) {
foreach ($container->find($_GET['contentsel']) as $el) {
$contentstr .= $el->outertext;
}
} else {
$contentstr = $container->innertext;
}
$item->addChild('description', htmlentities2($contentstr));
// create guid
$guid = $item->addChild('guid', sha1($item->asXML()));
$guid->addAttribute('isPermaLink', 'false');
}
unset($doc);
$xmldoc = new DomDocument("1.0", "UTF-8");
$xmldoc->appendChild($xmldoc->importNode(dom_import_simplexml($rss), true));
if (isset($_GET['preview'])) {
// setup XSL
$xsl = new XSLTProcessor;
$xsl->registerPHPFunctions();
$xsldoc = new DomDocument();
$xsldoc->load("rss2html.xsl");
$xsl->importStyleSheet($xsldoc);
// convert rss to DOM
//$xmldoc = new DomDocument();
//$xmldoc->appendChild($xmldoc->importNode(dom_import_simplexml($rss), true));
// transform
//echo $xsl->transformToXml($rss);
echo $xsl->transformToXml($xmldoc);
} else {
header('Content-Type: text/xml; charset=utf-8', true);
//echo $rss->asXML();
echo $xmldoc->saveXML();
}
function htmlentities2($text) {
return htmlentities($text, ENT_XML1 | ENT_DISALLOWED, "UTF-8");
}