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
| <?
/**
* This plugin can be used to insert RSS Newsfeeds to your blog
* Based on http://readinged.com/articles/rssparser/
by ed@readinged.com
* History:
* v0.9 : initial plugin
* v0.91: minor bugfixes
* v0.92: added posibillity to show site meta information
* v0.93: added options to toggle meta info on or off
* added the option to open links from a feed in a named target (like __blank or _top)
* v0.93b: problems with 2 blogs on the same url, now the query string is in included in the cache file (again) sorry Trent
* v0.94: somewhere there was a " too much in the output.
* v0.95: added supportsFeature
* fixed ask 4 get 3 bug
* add option to control whether to display feed temp unava message
*
*/
class NP_NewsFeed extends NucleusPlugin {
function getName() {return 'NewsFeed: Import RSS / XML News feeds in your weblog.'; }
function getAuthor() {return '-=Xiffy=- | admun (Edmond Hui)'; }
function getURL() {return 'http://xiffy.nl/weblog/'; }
function getVersion() {return '0.95'; }
function getDescription() {
return 'Call this to import a newsfeed. Currently all feeds work with the same defaults.';
}
function supportsFeature($what) {
switch($what) {
case 'SqlTablePrefix':
return 1;
default:
return 0;
}
}
function install() {
// create some options
$this->createOption('Cachedir','Directory where to put the cached data (relative from MEDIADIR)','text','rsscache/');
$this->createOption('Titlediv','Classname for the titleDiv item-title','text','feedTitle');
$this->createOption('Linkdiv', 'Classname for the links when shown seperatly','text','feedLink');
$this->createOption('Descriptiondiv','Classname for the description in de feed','text','feedDesc');
$this->createOption('cacheTime','Time before cache gets refreshed','text','60');
$this->createOption('showLogoAndTitle','Should we include the sites meta data as logo and title?','yesno','yes');
$this->createOption('target','If filled with anyname all links will include target="anyname" (can be _blank)','text','');
$this->createOption('linktext','text visible for "read whole item" only for type 2 and 4 calls','text','[read on]');
$this->createOption('showFeedNotAvail','Should we give warning to a unavailbale feed?','yesno','yes');
}
function doSkinVar($skintype, $newsfeedURL, $what = 1, $amount = 10) {
global $manager, $blog, $CONF, $i;
// go get the requested newsfeed.
$feed = $this->readFeed($newsfeedURL);
$titlediv = $this->getOption(Titlediv);
$linkdiv = $this->getOption(Linkdiv);
$descriptiondiv = $this->getOption(Descriptiondiv);
$target = $this->getOption(target);
$linktext = $this->getOption(linktext);
if (!$feed)
{
if ($this->getOption('showFeedNotAvail') == 'yes')
echo "<div class='$titlediv'>Tijdelijk onbereikbaar</div>\n";
return;
}
// Now insert the newsfeed in your weblog
// what. 1 Headlines only; headline = link
// 2 Headlines + link apart
// 3 Headlines, headline = link + description
// 4 Headlines, link and description
if ($this->getOption(showLogoAndTitle) == "yes")
$i = 0;
else {
$i = 1;
$amount = $amount+1;
}
foreach ( $feed as $feeditem ) {
if ($i <= $amount) {
$linkUrl = "<a href=\"".$feeditem[ "link" ]."\"";
if (($what % 2) == 1) {
$linkUrl .= " title=\"".stripslashes(htmlspecialchars(strip_tags($feeditem[ "description" ])))."\"";
if ($target <> "" ) {
$linkUrl .= " target=\"".$target."\"";
}
$linkUrl .= ">" . stripslashes($feeditem[ "title" ]) ."</a>";
}
if (($what % 2) == 0) {
if ($target <> "" ) {
$linkUrl .= " target=\"" . $target . "\"";
}
$linkUrl .= "\">" .$linktext."</a>";
} // well we have the linkUrl at last ;-) all those options make it a mess !!
if ($i == 0 && $feeditem[ "sitetitle"] <> "" ) {
echo "<div class='title'>". stripslashes($feeditem[ "sitetitle" ]) ."</div>";
if ($feeditem[ "url" ] <> "") {
echo "<img class=\"centered\" src=\"" . $feeditem[ "url" ] ."\" alt=\"". stripslashes($feeditem[ "sitetitle" ]) . "\" title=\"". stripslashes($feeditem[ "sitetitle" ]) . "\" />";
}
} else if ($what == 1 || $what == 3) {
echo "<div class='$titlediv'>" .$linkUrl."</div>\n";
} else {
echo "<div class='$titlediv'>". $feeditem[ "title" ] ."</div>";
echo "<div class='$linkdiv'>" . $linkUrl ."</div>\n";
}
if ($what == 3 || $what == 4) {
echo "<div class='$descriptiondiv'>" . stripslashes($feeditem[ "description" ]) . "</div>\n";
}
$i++;
}
}
}
function isCurrent($filename, $minutes) {
return ceil((time() - filemtime($filename)) / 60) < $minutes;
}
function readFeed($feedURL) {
// which URL to get
global $manager, $blog, $saxparser, $CONF, $contents, $cache_age, $cache_time, $cache_path, $last_modified_time, $DIR_MEDIA;
// get the cache path
$cache_path = $this->getOption(Cachedir);
$cache_time = $this->getOption(cacheTime);
$feedURL_parts = parse_url($feedURL);
$path = isset($feedURL_parts["path"]) ? $feedURL_parts["path"] : "/";
$filename = isset($feedURL_parts["host"]) ? $feedURL_parts["host"] : "feedfile";
$unique = isset($feedURL_parts["query"]) ? $feedURL_parts["query"] : "";
$filename = $filename . $path . $unique;
$filename = str_replace("/","_",$filename);
$filename = $DIR_MEDIA.$cache_path.$filename;
$writedir = $DIR_MEDIA.$cache_path;
$contents = "";
$data = "";
// create cache dir if non-excistent
if (!@is_dir($writedir)){
if (!@mkdir($writedir, 0777))
return _ERROR_BADPERMISSIONS;
}
if (!file_exists($filename) ||
(file_exists($filename) && !$this->isCurrent($filename, $cache_time))) {
$tag = "";
$isItem = false;
$i = 0;
unset($saxparser);
$saxparser = xml_parser_create();
xml_parser_set_option($saxparser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($saxparser, 'sax_start', 'sax_end');
xml_set_character_data_handler($saxparser, 'sax_data');
$saxparser= mb_convert_encoding($saxparser, "UTF-8", "iso-8859-1");
if (!function_exists('sax_start')) {
function sax_start($parser, $name, $attribs) {
global $tag, $isItem, $i, $isChannel;
switch ($name){
case "channel":
$i++;
$isChannel = true;
break;
case "item":
$i++;
$isItem = true;
break;
case "image";
case "url";
case "docs";
case "language";
case "generator";
case "copyright";
case "title":
case "link":
case "pubDate";
case "description":
case "author";
case "category":
case "guid":
if ($isItem || $isChannel) $tag = $name;
break;
default:
$isItem = false;
$isChannel = false;
break;
}
}
}
if (!function_exists('sax_end')) {
function sax_end($parser, $name) {
}
}
if (!function_exists('sax_data')) {
function sax_data($parser, $data) {
global $tag, $isItem, $contents, $isChannel, $i;
if ($data != "\n" && $isItem) {
switch ($tag) {
case "title";
case "link":
case "description":
(!isset($contents[$i-1][$tag]) || !strlen($contents[$i-1][$tag])) ?
$contents[$i-1][$tag] = addslashes($data) :
$contents[$i-1][$tag].= addslashes($data);
break;
}
} else if ($data != "\n" && $isChannel) {
switch ($tag) {
case "title";
if ($tag == "title") {$tag = "sitetitle";}
(!isset($contents[$i-1][$tag]) || !strlen($contents[$i-1][$tag])) ?
$contents[$i-1][$tag] = addslashes($data) :
$contents[$i-1][$tag] = addslashes($data);
break;
case "url":
case "image":
if ($tag == "title") {$tag = "sitetitle";}
(!isset($contents[$i-1][$tag]) || !strlen($contents[$i-1][$tag])) ?
$contents[$i-1][$tag] = addslashes($data) :
$contents[$i-1][$tag] .= addslashes($data);
break;
}
}
}
}
$fp = fopen($feedURL, "r");
while ($data = fread($fp, 4096)) {
$parsedOkay = xml_parse($saxparser, $data, feof($fp));
if (!$parsedOkay && xml_get_error_code($saxparser) != XML_ERROR_NONE) {
die("XML Error in File: ".xml_error_string(xml_get_error_code($saxparser)).
" at line ".xml_get_current_line_number($saxparser));
}
}
xml_parser_free($saxparser);
fclose($fp);
$cache = @fopen($filename, "w");
if ($cache) {
fwrite($cache, serialize($contents));
fclose($cache);
}
$cache_age = 0;
}
else {
$cache_age = ceil((time() - filemtime($filename)) / 60);
$fp = @fopen($filename, "r");
if ($fp) {
$data = fread($fp, filesize($filename));
}
fclose($fp);
$contents = unserialize($data);
}
return $contents;
}
}
?> |