forked from cogdog/feed2js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed2php.inc
355 lines (250 loc) · 11.1 KB
/
feed2php.inc
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
<?php
/* Feed2inc : RSS feed to PHP include file
ABOUT
This PHP code can be used as an include to provide the
same functionality of our Feed2JS concept, but without
the need to generate content via JavaScript
Developed by Alan Levine
http://cogdogblog.com/
This is a modified version of Fee22JS and merely replaces
the output of Javascript to that for a PHP. See the original
feed2js.php for change history.
USAGE:
See http://feed2js.org/index.php?s=php
CODE:
http://code.google.com/p/feed2js/
This makes use of the Magpie RSS parser from
http://magpierss.sourceforge.net/
------------- small print ---------------------------------------
GNU General Public License
Copyright (C) 2004-2010 Alan Levine
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details
http://www.gnu.org/licenses/gpl.html
------------- small print ---------------------------------------
*/
require_once('feed2js_config.php');
/// trap for missing src param for the feed, use a dummy one so it gets displayed.
if (!$src or strpos($src, 'http://')!=0) $src= 'http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']) . '/nosource.php';
// check for utf encoding type
if (!isset($utf)) $utf = 'n';
if ($utf == 'y') {
define('MAGPIE_CACHE_DIR', MAGPIE_DIR . 'cache_utf8/');
// character encoding
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
} else {
define('MAGPIE_CACHE_DIR', MAGPIE_DIR . 'cache/');
define('MAGPIE_OUTPUT_ENCODING', 'ISO-8859-1');
}
// GET VARIABLES ---------------------------------------------
// retrieve values from posted variables
// flag to show channel info
if (!isset($chan)) $chan = 'n';
// variable to limit number of displayed items; default = 0 (show all, 20 is a safe bet to list a big list of feeds)
if (!isset($num)) $num = 0;
if ($num==0) $num = 100;
// indicator to show item description, 0 = no; 1=all; n>1 = characters to display
// values of -1 indicate to display item without the title as a link
// (default=0)
if (!isset($desc)) $desc = 0;
// flag to show author of items, values: no/yes (default=no)
$auth = (isset($_GET['au'])) ? 'y' : 'n';
// flag to show date of posts, values: no/yes (default=no)
$date = (isset($date)) ? $date : 'n';
// time zone offset for making local time,
// e.g. +7, =-10.5; 'feed' = print the time string in the RSS w/o conversion
$tz = (isset($tz)) ? $tz : 'feed';
// flag to open target window in new window; n = same window, y = new window,
// other = targeted window, 'popup' = call JavaScript function popupfeed to display
// in new window
// (default is n)
if (!isset($targ)) $targ = 'n';
if ($targ == 'n') {
$target_window = ' target="_self"';
} elseif ($targ == 'y' ) {
$target_window = ' target="_blank"';
} elseif ($targ == 'popup') {
$target_window = ' onClick="popupfeed(this.href);return false"';
} else {
$target_window = ' target="' . $targ . '"';
}
// flag to show feed as full html output rather than JavaScript, used for alternative
// views for JavaScript-less users.
// y = display html only for non js browsers
// n = default (JavaScript view)
// a = display javascript output but allow HTML
// p = display text only items but convert linefeeds to BR tags
// default setting for no conversion of linebreaks
if (!isset($html)) $html = 'n';
$br = ' ';
if ($html == 'a') {
$desc = 1;
} elseif ($html == 'p') {
$br = '<br />';
}
// optional parameter to use different class for the CSS container
if (isset($css)) {
$rss_box_id = '-' . $css;
} else {
$rss_box_id = '';
}
if (isset($pc)) {
$play_podcast = $pc;
} else {
$play_podcast = 'n';
}
// PARSE FEED and GENERATE OUTPUT -------------------------------
// This is where it all happens!
// Fetch the data, thanks Magpie
$rss = @fetch_rss( $src );
// begin javascript output string for channel info
$str = "<div class=\"rss-box" . $rss_box_id . "\">\n";
// no feed found by magpie, return error statement
if (!$rss) {
// error, nothing grabbed
$str.= "<p class=\"rss-item\"><em>Error:</em> Feed failed! Causes may be (1) No data found for RSS feed $src; (2) There are no items are available for this feed; (3) The RSS feed does not validate.<br /><br /> Please verify that the URL <a href=\"$src\">$src</a> works first in your browser and that the feed passes a <a href=\"http://feedvalidator.org/check.cgi?url=" . urlencode($src) . "\">validator test</a>.</p>\n";
} else {
// we have a feed, so let's process
if ($chan == 'y') {
// output channel title linked and description
$str.= "document.write('<p class=\"rss-title\"><a class=\"rss-title\" href=\"" . trim($rss->channel['link']) . '"' . $target_window . ">" . strip_returns($rss->channel['title']) . "</a><br /><span class=\"rss-item\">" . strip_returns(strip_tags($rss->channel['description'])) . "</span></p>');\n";
} elseif ($chan == 'title') {
// output title only with link
$str.= "document.write('<p class=\"rss-title\"><a class=\"rss-title\" href=\"" . trim($rss->channel['link']) . '"' . $target_window . ">" . strip_returns($rss->channel['title']) . "</a></p>');\n";
} elseif ($chan == 'titlelinkno') {
// output title only with no link
$str.= "document.write('<p class=\"rss-title\">" . strip_returns($rss->channel['title']) . "</p>');\n";
} elseif ($chan == 'linkno') {
// output title and descript only with no link
$str.= "document.write('<p class=\"rss-title\">" . strip_returns($rss->channel['title']) . "<br /><span class=\"rss-item\">" . strip_returns(strip_tags($rss->channel['description'])) . "</span></p>');\n";
}
// begin item listing
$str.= "<ul class=\"rss-items\">\n";
// Walk the items and process each one
$all_items = array_slice($rss->items, 0, $num);
foreach ( $all_items as $item ) {
// create output for item author
$author_str = '';
if ($auth == 'y') {
if (isset($item['dc']['creator'])) {
$author_str = ' <span class="rss-item-auth">(' . addslashes(strip_tags($item['dc']['creator'])) . ')</span>';
} else {
if (isset($item['author_name'])) {
$author_str = ' <span class="rss-item-auth">(' . addslashes(strip_tags($item['author_name'])) . ')</span>';
}
}
}
if ($item['link']) {
// link url
$my_url = $item['link'];
} elseif ($item['guid']) {
// feeds lacking item -> link
$my_url = ($item['guid']);
}
if ($desc < 0) {
$str.= "<li class=\"rss-item\">\n";
} elseif ($item['title']) {
// format item title
$my_title = strip_returns($item['title']);
// create a title attribute. thanks Seb!
$title_str = substr(strip_returns(htmlspecialchars(strip_tags($item['summary']))), 0, 60) . '...';
// write the item with a title attribute
$str.= "<li class=\"rss-item\"><a class=\"rss-item\" href=\"" . trim($my_url) . "\" title=\"$title_str\"" . $target_window . ">" . $my_title . '</a> ' . $author_str . "<br />\n";
} else {
// if no title, build a link to tag on the description
$str.= "<li class=\"rss-item\">\n";
$more_link = " <a class=\"rss-item\" href=\"" .trim($my_url) . '"' . $target_window . ">«details»</a>";
}
// print out date if option indicated and feed returns a value.
// Use the new date_timestamp function in Magpie 0.71
if ($tz == 'feed') {
// echo the date/time stamp reported in the feed
if ($item['pubdate'] != '') {
// RSS 2.0 is already formatted, so just use it
$pretty_date = $item['pubdate'];
} elseif ($item['published'] != "") {
// ATOM 1.0 format, remove the "T" and "Z" and the time zone offset
$pretty_date = str_replace("T", " ", $item['published']);
$pretty_date= str_replace("Z", " ", $pretty_date);
} elseif ($item['issued'] != "") {
// ATOM 0.3 format, remove the "T" and "Z" and the time zone offset
$pretty_date = str_replace("T", " ", $item['issued']);
$pretty_date= str_replace("Z", " ", $pretty_date);
} elseif ( $item['dc']['date'] != "") {
// RSS 1.0, remove the "T" and the time zone offset
$pretty_date = str_replace("T", " ", $item['dc']['date']);
$pretty_date = substr($pretty_date, 0,-6);
} else {
// no time/date stamp,
$pretty_date = 'n/a';
}
} else {
// convert to local time via conversion to GMT + offset
// adjust local server time to GMT and then adjust time according to user
// entered offset.
// let's see what kind of timestamps we can pull...
if ($item['date_timestamp'] != "") {
$ts = $item['date_timestamp'];
} elseif ($item['published'] != "") {
$ts = strtotime($item['published']);
} elseif ($item['issued'] != "") {
$ts = strtotime($item['issued']);
} elseif ( $item['dc']['date'] != "") {
$ts = strtotime($item['dc']['date']);
} else {
$ts = time();
}
$pretty_date = date($date_format, $ts - $tz_offset + $tz * 3600);
}
$str.= "<span class=\"rss-date\">$pretty_date</span><br />\n";
}
// link to podcast media if available
if ($play_podcast == 'y' and is_array($item['enclosure'])) {
$str.= "<div class=\"pod-play-box\">Media: ";
for ($i = 0; $i < count($item['enclosure']); $i++) {
// display only if enclosure is a valid URL
//if (strpos($item['enclosure'][$i]['url'], 'http://')!=0) {
$str.= "<a class=\"pod-play\"><a href=\"" . trim($item['enclosure'][$i]['url']) . "\" title=\"Play Now\" target=\"_blank\"><em>Play</em> <span>" . substr(trim($item['enclosure'][$i]['url']), -3) . "</span></a> ";
//}
}
$str.= "</div>";
}
// output description of item if desired
if ($desc) {
if ($item['atom_content']) {
// Atom content - note that wordpress.com feeds return bad data here "A"
// so revert to description if this is the case.
$my_blurb = ($item['atom_content'] == "A") ? $item['description'] : html_entity_decode ( $item['atom_content'], ENT_NOQUOTES, MAGPIE_OUTPUT_ENCODING);
} else if ($item['content']) {
// Atom/encocded content support (thanks David Carter-Tod)
$my_blurb = html_entity_decode ( $item['content'], ENT_NOQUOTES, MAGPIE_OUTPUT_ENCODING);
} else {
$my_blurb = $item['summary'];
}
// strip html
if ($html != 'a') $my_blurb = strip_tags($my_blurb);
// trim descriptions
if ($desc > 1) {
// display specified substring numbers of chars;
// html is stripped to prevent cut off tags
// make sure we dont chop UTF-8 characters
if ($utf == 'y') {
$my_blurb = mb_substr($my_blurb, 0, $desc, 'UTF-8') . '...';
} else {
$my_blurb = substr($my_blurb, 0, $desc) . '...';
}
}
$str.= strip_returns($my_blurb, $br) . "\n";
}
$str.= "$more_link</li>\n";
}
}
$str .= "</ul></div>\n";
echo $str;
?>