2 // PukiWiki - Yet another WikiWikiWeb clone
5 // 2002-2017 PukiWiki Development Team
6 // 2002 PANDA <panda@arino.jp>
7 // (Original)hiro_do3ob@yahoo.co.jp
8 // License: GPL, same as PukiWiki
10 // Show RSS (of remote site) plugin
12 // * This plugin needs 'PHP xml extension'
13 // * Cache data will be stored as CACHE_DIR/*.tmp
15 define('PLUGIN_SHOWRSS_USAGE', '#showrss(URI-to-RSS[,default|menubar|recent[,Cache-lifetime[,Show-timestamp]]])');
17 // Show related extensions are found or not
18 function plugin_showrss_action()
20 if (PKWK_SAFE_MODE) die_message('PKWK_SAFE_MODE prohibit this');
23 foreach(array('xml', 'mbstring') as $extension){
24 $$extension = extension_loaded($extension) ?
25 '&color(green){Found};' :
26 '&color(red){Not found};';
27 $body .= '| ' . $extension . ' extension | ' . $$extension . ' |' . "\n";
29 return array('msg' => 'showrss_info', 'body' => convert_html($body));
32 function plugin_showrss_convert()
36 if (! isset($_xml)) $_xml = extension_loaded('xml');
37 if (! $_xml) return '#showrss: xml extension is not found<br />' . "\n";
39 $num = func_num_args();
40 if ($num == 0) return PLUGIN_SHOWRSS_USAGE . '<br />' . "\n";
42 $argv = func_get_args();
45 $template = $uri = '';
47 case 4: $timestamp = (trim($argv[3]) == '1'); /*FALLTHROUGH*/
48 case 3: $cachehour = trim($argv[2]); /*FALLTHROUGH*/
49 case 2: $template = strtolower(trim($argv[1]));/*FALLTHROUGH*/
50 case 1: $uri = trim($argv[0]);
53 $class = ($template == '' || $template == 'default') ? 'ShowRSS_html' : 'ShowRSS_html_' . $template;
54 if (! is_numeric($cachehour))
55 return '#showrss: Cache-lifetime seems not numeric: ' . htmlsc($cachehour) . '<br />' . "\n";
56 if (! class_exists($class))
57 return '#showrss: Template not found: ' . htmlsc($template) . '<br />' . "\n";
59 return '#showrss: Seems not URI: ' . htmlsc($uri) . '<br />' . "\n";
61 // Remove old caches in 5% rate
62 if (mt_rand(1, 20) === 1) {
63 plugin_showrss_cache_expire(24);
65 list($rss, $time) = plugin_showrss_get_rss($uri, $cachehour);
66 if ($rss === FALSE) return '#showrss: Failed fetching RSS from the server<br />' . "\n";
67 if (! is_array($rss)) {
68 // Show XML error message
69 return '#showrss: Error - ' . htmlsc($rss) . '<br />' . "\n";
73 $time_display = '<p style="font-size:10px; font-weight:bold">Last-Modified:' .
74 get_date('Y/m/d H:i:s', $time) . '</p>';
77 $obj = new $class($rss);
78 return $obj->toString($time_display);
81 // Create HTML from RSS array()
87 function ShowRSS_html($rss)
89 $this->__construct($rss);
91 function __construct($rss)
93 foreach ($rss as $date=>$items) {
94 foreach ($items as $item) {
95 $link = $item['LINK'];
96 $title = $item['TITLE'];
97 $passage = get_passage($item['_TIMESTAMP']);
98 $link = '<a href="' . $link . '" title="' . $title . ' ' .
99 $passage . '" rel="nofollow">' . $title . '</a>';
100 $this->items[$date][] = $this->format_link($link);
105 function format_link($link)
107 return $link . '<br />' . "\n";
110 function format_list($date, $str)
115 function format_body($str)
120 function toString($timestamp)
123 foreach ($this->items as $date=>$items)
124 $retval .= $this->format_list($date, join('', $items));
125 $retval = $this->format_body($retval);
134 class ShowRSS_html_menubar extends ShowRSS_html
136 var $class = ' class="small"';
138 function format_link($link) {
139 return '<li>' . $link . '</li>' . "\n";
142 function format_body($str) {
143 return '<ul class="recent_list">' . "\n" . $str . '</ul>' . "\n";
147 class ShowRSS_html_recent extends ShowRSS_html
149 var $class = ' class="small"';
151 function format_link($link) {
152 return '<li>' . $link . '</li>' . "\n";
155 function format_list($date, $str) {
156 return '<strong>' . $date . '</strong>' . "\n" .
157 '<ul class="recent_list">' . "\n" . $str . '</ul>' . "\n";
162 function plugin_showrss_get_rss($target, $cachehour)
167 $filename = CACHE_DIR . encode($target) . '.tmp';
168 // Remove expired cache
169 plugin_showrss_cache_expire_file($filename, $cachehour);
170 // Get the cache not expired
171 if (is_readable($filename)) {
172 $buf = join('', file($filename));
173 $time = filemtime($filename) - LOCALZONE;
177 if (is_null($time)) {
179 $data = pkwk_http_request($target);
180 if ($data['rc'] !== 200) {
181 return array(FALSE, 0);
183 $buf = $data['data'];
186 // Save RSS into cache
188 $fp = fopen($filename, 'w');
194 $obj = new ShowRSS_XML();
195 $obj->modified_date = (is_null($time) ? UTIME : $time);
196 return array($obj->parse($buf),$time);
199 // Remove cache if expired limit exeed
200 function plugin_showrss_cache_expire($cachehour)
202 $expire = $cachehour * 60 * 60; // Hour
203 $dh = dir(CACHE_DIR);
204 while (($file = $dh->read()) !== FALSE) {
205 if (substr($file, -4) != '.tmp') continue;
206 $file = CACHE_DIR . $file;
207 $last = time() - filemtime($file);
208 if ($last > $expire) unlink($file);
214 * Remove single file cache if expired limit exeed
218 function plugin_showrss_cache_expire_file($filename, $cachehour)
220 $expire = $cachehour * 60 * 60; // Hour
221 $last = time() - filemtime($filename);
222 if ($last > $expire) {
227 // Get RSS and array() them
239 $this->items = array();
240 $this->item = array();
241 $this->is_item = FALSE;
244 $this->encoding = $utf8;
247 if(preg_match('/<\?xml [^>]*\bencoding="([a-z0-9-_]+)"/i', $buf, $matches)) {
248 $encoding = $matches[1];
249 if (strtoupper($encoding) !== $utf8) {
250 // xml_parse() fails on non UTF-8 encoding attr in XML decLaration
251 $buf = preg_replace('/<\?xml ([^>]*)\bencoding="[a-z0-9-_]+"/i', '<?xml $1', $buf);
252 // xml_parse() requires UTF-8 compatible encoding
253 $buf = mb_convert_encoding($buf, $utf8, $encoding);
257 $xml_parser = xml_parser_create($utf8);
258 xml_set_element_handler($xml_parser, array(& $this, 'start_element'), array(& $this, 'end_element'));
259 xml_set_character_data_handler($xml_parser, array(& $this, 'character_data'));
260 if (! xml_parse($xml_parser, $buf, 1)) {
261 return sprintf('XML error: %s at line %d in %s',
262 xml_error_string(xml_get_error_code($xml_parser)),
263 xml_get_current_line_number($xml_parser),
264 (strlen($buf) < 500 ? $buf : substr($buf, 0, 500) . '...'));
266 xml_parser_free($xml_parser);
270 function escape($str)
272 // Unescape already-escaped chars (<, >, &, ...) in RSS body before htmlsc()
273 $str = strtr($str, array_flip(get_html_translation_table(ENT_COMPAT)));
276 // Encoding conversion
277 $str = mb_convert_encoding($str, SOURCE_ENCODING, $this->encoding);
282 function start_element($parser, $name, $attrs)
284 if ($this->is_item) {
286 } else if ($name == 'ITEM') {
287 $this->is_item = TRUE;
292 function end_element($parser, $name)
294 if (! $this->is_item || $name != 'ITEM') return;
296 $item = array_map(array(& $this, 'escape'), $this->item);
297 $this->item = array();
299 if (isset($item['DC:DATE'])) {
300 $time = plugin_showrss_get_timestamp($item['DC:DATE'], $this->modified_date);
302 } else if (isset($item['PUBDATE'])) {
303 $time = plugin_showrss_get_timestamp($item['PUBDATE'], $this->modified_date);
305 $time_from_desc = FALSE;
306 if (isset($item['DESCRIPTION']) &&
307 (($description = trim($item['DESCRIPTION'])) != '')) {
308 $time_from_desc = strtotime($description);
310 if ($time_from_desc !== FALSE && $time_from_desc !== -1) {
311 $time = $time_from_desc - LOCALZONE;
313 $time = time() - LOCALZONE;
316 $item['_TIMESTAMP'] = $time;
317 $date = get_date('Y-m-d', $item['_TIMESTAMP']);
319 $this->items[$date][] = $item;
320 $this->is_item = FALSE;
323 function character_data($parser, $data)
325 if (! $this->is_item) return;
326 if (! isset($this->item[$this->tag])) $this->item[$this->tag] = '';
327 $this->item[$this->tag] .= $data;
331 function plugin_showrss_get_timestamp($str, $default_date)
334 if ($str == '') return UTIME;
337 if (preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/', $str, $matches)) {
338 $time = strtotime($matches[1] . ' ' . $matches[2]);
339 if ($time === FALSE || $time === -1) {
340 $time = $default_date;
341 } else if (isset($matches[3])) {
342 $diff = ($matches[5] * 60 + $matches[6]) * 60;
343 $time += ($matches[4] == '-' ? $diff : -$diff);
347 $time = strtotime($str);
348 return ($time === FALSE || $time === -1) ? $default_date : $time - LOCALZONE;