forked from bloopletech/rss2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·342 lines (305 loc) · 13.5 KB
/
index.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
<?
set_time_limit(240);
ini_set("default_socket_timeout", 120);
if(isset($_GET["url"])) {
function eschtml($str) {
return str_replace(array(">", "<", "\""), array(">", "<", """), $str);
}
function remtags($str) {
$str = html_entity_decode($str, ENT_COMPAT, "UTF-8");
return strip_tags($str);
}
$url = $_GET["url"];
$detail = (isset($_GET["detail"]) ? intval($_GET["detail"]) : 2147483647);
$limit = (isset($_GET["limit"]) ? $_GET["limit"] : 2147483647);
$striphtml = (isset($_GET["striphtml"]) ? ($_GET["striphtml"] == "true") : false);
$showtitle = (isset($_GET["showtitle"]) ? ($_GET["showtitle"] == "true") : true);
$showtitledesc = (isset($_GET["showtitledesc"]) ? ($_GET["showtitledesc"] == "true") : false);
$titleprefix = (isset($_GET["titleprefix"]) ? $_GET["titleprefix"] : "");
$titlereplacement = (isset($_GET["titlereplacement"]) ? $_GET["titlereplacement"] : "");
$titledescprefix = (isset($_GET["titledescprefix"]) ? $_GET["titledescprefix"] : "");
$itemtitleprefix = (isset($_GET["itemtitleprefix"]) ? $_GET["itemtitleprefix"] : "");
$itemdescprefix = (isset($_GET["itemdescprefix"]) ? $_GET["itemdescprefix"] : "");
$showicon = (isset($_GET["showicon"]) ? ($_GET["showicon"] == "true") : false);
$showempty = (isset($_GET["showempty"]) ? ($_GET["showempty"] == "true") : false);
$type = (isset($_GET["type"]) ? $_GET["type"] : "php");
$fixbugs = (isset($_GET["fixbugs"]) ? ($_GET["fixbugs"] == "true") : false);
$forceutf8 = (isset($_GET["forceutf8"]) ? ($_GET["forceutf8"] == "true") : false);
if($type == "html") {
header("Content-Type: text/html; charset=utf-8");
echo "<html>\n<head>\n<title></title>\n</head>\n<body>\n";
}
else if($type == "js") {
header("Content-Type: text/javascript; charset=utf-8");
ob_start();
}
$headers = "Connection: close\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-Language: en-us,en;q=0.5\r\n".
"Referer: /\r\n";
$http_context = array(
'protocol_version' => 1.1,
'method' => 'GET',
'header' => $headers
);
$feedtext = file_get_contents($url, false, stream_context_create(array('http' => $http_context)));
$feedtext = trim($feedtext);
if($fixbugs) {
$feedtext = str_replace("& ", " & ", $feedtext);
$feedtext = str_replace("&x80;", "€", $feedtext);
$feedtext = str_replace("&x81;", "‘", $feedtext);
$feedtext = str_replace("&x85;", "…", $feedtext);
$feedtext = str_replace("&x86;", "†", $feedtext);
$feedtext = str_replace("&x87;", "‡", $feedtext);
$feedtext = str_replace("&x88;", "ˆ", $feedtext);
$feedtext = str_replace("&x89;", "‰", $feedtext);
$feedtext = str_replace("&x8A;", "Š", $feedtext);
$feedtext = str_replace("&x8B;", "‹", $feedtext);
$feedtext = str_replace("&x8C;", "Œ", $feedtext);
$feedtext = str_replace("&x8E", "", $feedtext);
$feedtext = str_replace("&x91;", "‘", $feedtext);
$feedtext = str_replace("&x92;", "’", $feedtext);
$feedtext = str_replace("&x93;", "“", $feedtext);
$feedtext = str_replace("&x94;", "”", $feedtext);
$feedtext = str_replace("&x95;", "•", $feedtext);
$feedtext = str_replace("&x96;", "-", $feedtext);
$feedtext = str_replace("&x97;", "—", $feedtext);
$feedtext = str_replace("&x98;", "˜", $feedtext);
$feedtext = str_replace("&x99;", "™", $feedtext);
$feedtext = str_replace("&x9A;", "š", $feedtext);
$feedtext = str_replace("&x9B;", "›", $feedtext);
$feedtext = str_replace("&x9C;", "&eolig;", $feedtext);
$feedtext = str_replace("&x9E;", "", $feedtext);
$feedtext = str_replace("&x9F;", "Ÿ", $feedtext);
}
if($forceutf8) {
$feedtext = preg_replace("/<\?xml(.*?)encoding=['\"].*?['\"](.*?)\?>/m", "<?xml$1encoding=\"utf-8\"$2?>", $feedtext);
}
$doc = new DOMDocument();
$doc->loadXML($feedtext);
function search($tag, $context, $single = true) {
$nodes = $context->getElementsByTagName($tag);
if($nodes->length > 0) return $single ? $nodes->item(0) : $nodes;
return NULL;
}
if($showtitle == true) {
$channel = search("channel", $doc);
$title = search("title", $channel);
$title = eschtml((isset($title) ? $titleprefix.$title->textContent : "(No feed title)"));
if($titlereplacement) $title = $titlereplacement;
if($striphtml) $title = remtags($title);
$link = search("link", $channel);
$link = ($link ? (isset($eschtml) ? eschtml($link->textContent) : $link->textContent) : "");
if($link != "") $title = "<a href=\"$link\" target=\"_blank\">$title</a>";
if($striphtml) $link = remtags($link);
$desc = search("description", $channel);
$desc = eschtml(isset($desc) ? $desc->textContent : "");
if($striphtml) $desc = remtags($desc);
$image = search("url", $channel);
$image = (isset($image) ? $image->textContent : "");
if($showicon && $image != "") $title = "<img class=\"feed-title-image\" src=\"$image\">$title";
if($showempty || (!$showempty && $title != "")) echo "<h3 class=\"feed-title\">$title</h3>\n";
if($showtitledesc && ($showempty || (!$showempty && $desc != ""))) echo "<p class=\"feed-desc\">$titledescprefix$desc</p>\n";
}
$items = search("item", $doc, false);
foreach($items as $i => $item) {
if($i == $limit) break;
$title = search("title", $item);
$title = (isset($title) ? eschtml($title->textContent) : "(No title)");
$link = search("link", $item);
$link = (isset($link) ? eschtml($link->textContent) : "");
if($link != "") $title = "<a href=\"$link\" target=\"_blank\">$title</a>";
$desc = search("description", $item);
$desc = (isset($desc) ? $desc->textContent : "");
if($striphtml) $desc = remtags($desc);
if($showempty || (!$showempty && $title != "")) echo "<h4 class=\"feed-item-title\">$itemtitleprefix$title</h4>\n";
if(($detail > 0) && ($showempty || (!$showempty && $desc != ""))) {
$words = explode(" ", $desc);
if(count($words) > $detail) {
$words = array_slice($words, 0, $detail);
$desc = implode(" ", $words)."...";
}
echo "<p class=\"feed-item-desc\">$itemdescprefix$desc</p>\n";
}
}
echo "<div class=\"rss2html-note\" style=\"float: right;\"><a href=\"https://rss.bloople.net/\" style=\"color: #000000;\">Powered by rss2html</a></div>\n<div class=\"rss2html-note-clear\" style=\"clear: right; height: 0;\"></div>\n";
if($type == "html") {
echo "</body>\n</html>";
}
else if($type == "js") {
$text = ob_get_contents();
ob_end_clean();
$text = str_replace("\n", "", str_replace("\"", "\\\"", $text));
?>
var container = document.createElement("div");
container.innerHTML = "<?= $text ?>";
var nodes = [].slice.call(container.childNodes);
var script = document.scripts[document.scripts.length - 1];
var parent = script.parentNode;
while(nodes.length > 0) parent.insertBefore(nodes.shift(), script);
parent.removeChild(script);
<?
}
}
else {
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>RSS 2 HTML</title>
<link rel="StyleSheet" type="text/css" href="style.css">
<script type="text/javascript" src="scripts/parse.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
</head>
<body id="body">
<header>
<h1>RSS 2 HTML</h1>
</header>
<div id="mainbody">
<h3>Welcome to RSS 2 HTML!</h3>
<p class="mast">This page offers an easy way to embed RSS feeds in HTML webpages. One line of code in your webpage, and easily-styled HTML will be generated, with no advertisements or other restrictions.</p>
<p class="mast">Use the form below to generate the code to include in your webpage:</p>
<form id="form">
<table>
<tr>
<td class="l">URL of RSS feed:</td>
<td><input class="text" type="text" name="url" id="url"></td>
</tr>
<tr class="advanced" id="advtitle">
<td class="l"></td>
<td>
<label for="showtitle">
<input type="checkbox" name="showtitle" id="showtitle">
Show feed title
</label>
</td>
</tr>
<tr class="advanced" id="advicon">
<td class="l"></td>
<td>
<label for="showicon">
<input type="checkbox" name="showicon" id="showicon">
Show feed icon
</label>
</td>
</tr>
<tr>
<td class="l">Length of feed item descriptions:</td>
<td>
<label for="detailhide">
<input type="radio" name="detail" id="detailhide" value="-1">
Hide descriptions
</label>
<label for="detailshow">
<input type="radio" name="detail" id="detailshow" value="0">
Show up to <input class="num" type="text" id="detailnum"> words
</label>
<label id="detailnolimit">
<input type="radio" name="detail" id="detailnolimit" value="1">
Don't limit length
</label>
</td>
</tr>
<tr class="advanced" id="advshow">
<td class="l"></td>
<td>
<label for="showempty">
<input type="checkbox" name="showempty" id="showempty">
Output HTML for empty feed titles and descriptions
</label>
</td>
</tr>
<tr class="advanced" id="advstrip">
<td class="l"></td>
<td>
<label for="striphtml">
<input type="checkbox" name="striphtml" id="striphtml">
Strip HTML from feed item descriptions
</label>
</td>
</tr>
<tr class="advanced" id="advforce">
<td class="l"></td>
<td>
<label for="forceutf8">
<input type="checkbox" name="forceutf8" id="forceutf8">
Assume RSS feed is UTF-8, ignoring XML prolog
</label>
</td>
</tr>
<tr class="advanced" id="advfix">
<td class="l"></td>
<td>
<label for="fixbugs">
<input type="checkbox" name="fixbugs" id="fixbugs">
Attempt to convert Windows-1252 -> UTF-8
</label>
</td>
</tr>
<tr>
<td class="l"></td>
<td>
<label for="limit">
<input type="checkbox" name="limit" id="limit">
Limit to the first <input class="num" type="text" id="limitnum"> items in the feed
</label>
</td>
</tr>
<tr class="advanced" id="advembed">
<td class="l">How should the feed be embedded in your webpage:</td>
<td>
<label for="codejs">
<input type="radio" name="codegen" value="js" id="codejs" checked="checked">
JavaScript code - this option is best if you want to style the embedded feed.
</label>
<label for="codehtml">
<input type="radio" name="codegen" id="codehtml" value="html">
<iframe> embed - this option is best if the people using your website don't have JavaScript enabled.
</label>
<label for="codephp">
<input type="radio" name="codegen" id="codephp" value="php">
PHP code - this option will only work if your webpage is already coded in PHP.
</label>
</td>
</tr>
<tr class="results">
<td class="l">Embed this code in your webpage:</td>
<td><textarea id="codeout"></textarea></td>
</tr>
<tr class="results">
<td class="l">Live example:</td>
<td>
<iframe id="live-example"></iframe>
</td>
</tr>
<tr>
<td colspan="2" class="actions">
<input type="button" id="showopt" value="More options" onclick="showAdvanced();">
<input type="submit" id="submit" value="Submit & get code">
</td>
</tr>
</table>
</form>
<h3>Styling the output</h3>
<p>
The HTML generated by this code contains CSS hooks so you can style the output in your stylesheet.<br>
The title of the feed is displayed in a <code><h3></code> tag, with the class <code>feed-title</code>.<br>
The feed description is displayed in a <code><p></code> tag with class <code>feed-desc</code>.<br>
The feed icon is displayed in an <code><img></code> tag with class <code>feed-title-image</code>.<br>
Each feed item title is displayed in a <code><h4></code> tag with class <code>feed-item-title</code>.<br>
Each feed item description is displayed in a <code><p></code> tag with class <code>feed-item-desc</code>.
</p>
<p>
Note that this code does not generate HTML for empty feed titles, descriptions, etc.<br>
If you want empty HTML to be generated, click "more options" and tick the "Output HTML for empty..." checkbox.
</p>
<hr>
<div class="footer">© Brenton Fletcher. Comments? e-mail me: <a href="mailto:i@bloople.net">i@bloople.net</a>.</div>
</div>
</body>
</html>
<?
}
?>