2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: showrss.inc.php,v 1.12 2004/06/20 04:20:18 henoheno Exp $
7 // Modified version by PANDA <panda@arino.jp>
12 * showrss ¥×¥é¥°¥¤¥ó (Created by hiro_do3ob@yahoo.co.jp)
14 * ¥é¥¤¥»¥ó¥¹¤Ï PukiWiki ËÜÂΤÈƱ¤¸¤¯ GNU General Public License (GPL) ¤Ç¤¹¡£
15 * http://www.gnu.org/licenses/gpl.txt
17 * pukiwikiÍѤΥץ饰¥¤¥ó¤Ç¤¹¡£
18 * pukiwiki1.3.2°Ê¾å¤ÇÆ°¤¯¤È»×¤¤¤Þ¤¹¡£
20 * º£¤Î¤È¤³¤íÆ°ºî¤µ¤»¤ë¤¿¤á¤Ë¤ÏPHP ¤Î xml extension ¤¬É¬¿Ü¤Ç¤¹¡£PHP¤ËÁȤ߹þ¤Þ¤ì¤Æ¤Ê¤¤¾ì¹ç¤Ï¤½¤Ã¤±¤Ê¤¤¥¨¥é¡¼¤¬½Ð¤ë¤È»×¤¤¤Þ¤¹¡£
21 * Àµµ¬É½¸½ or ʸ»úÎó´Ø¿ô¤Ç¤Ê¤ó¤È¤«¤Ê¤é¤Ê¤¯¤â¤Ê¤µ¤²¤Ê¤ó¤Ç¤¹¤¬¼ûÍפäƤɤ줯¤é¤¤¤¢¤ë¤Î¤«¤ï¤«¤é¤¤¤Î¤ÇÊÝα¤Ç¤¹¡£
23 * version: Id:showrss.inc.php,v 1.40 2003/03/18 11:52:58 hiro Exp
27 // showrss¥×¥é¥°¥¤¥ó¤¬»ÈÍѲÄǽ¤«¤É¤¦¤«¤òɽ¼¨
28 function plugin_showrss_action()
30 $xml_extension = extension_loaded('xml');
31 $mbstring_extension = extension_loaded('mbstring');
33 $xml_msg = $xml_extension ? 'xml extension is loaded' : 'COLOR(RED){xml extension is not loaded}';
34 $mbstring_msg = $mbstring_extension ? 'mbstring extension is loaded' : 'COLOR(RED){mbstring extension is not loaded}';
37 $showrss_info .= "| xml parser | $xml_msg |\n";
38 $showrss_info .= "| multibyte | $mbstring_msg |\n";
40 return array('msg' => 'showrss_info', 'body' => convert_html($showrss_info));
43 function plugin_showrss_convert()
45 if (func_num_args() == 0)
47 // °ú¿ô¤¬¤Ê¤¤¾ì¹ç¤Ï¥¨¥é¡¼
48 return "<p>showrss: no parameter(s).</p>\n";
50 if (!extension_loaded('xml'))
52 // xml ³ÈÄ¥µ¡Ç½¤¬Í¸ú¤Ç¤Ê¤¤¾ì¹ç¡£
53 return "<p>showrss: xml extension is not loaded</p>\n";
56 $array = func_get_args();
57 $rssurl = $tmplname = $usecache = $usetimestamp = '';
59 switch (func_num_args())
62 $usetimestamp = trim($array[3]);
64 $usecache = $array[2];
66 $tmplname = strtolower(trim($array[1]));
68 $rssurl = trim($array[0]);
71 // RSS ¥Ñ¥¹¤ÎÃÍ¥Á¥§¥Ã¥¯
74 return '<p>showrss: syntax error. '.htmlspecialchars($rssurl)."</p>\n";
77 $class = "ShowRSS_html_$tmplname";
78 if (!class_exists($class))
80 $class = 'ShowRSS_html';
83 list($rss,$time) = plugin_showrss_get_rss($rssurl,$usecache);
86 return "<p>showrss: cannot get rss from server.</p>\n";
89 $obj = new $class($rss);
92 if ($usetimestamp > 0)
94 $time = get_date('Y/m/d H:i:s',$time);
95 $timestamp = "<p style=\"font-size:10px; font-weight:bold\">Last-Modified:$time</p>";
97 return $obj->toString($timestamp);
99 // rssÇÛÎ󤫤éhtml¤òºî¤ë
102 var $items = array();
105 function ShowRSS_html($rss)
107 foreach ($rss as $date=>$items)
109 foreach ($items as $item)
111 $link = $item['LINK'];
112 $title = $item['TITLE'];
113 $passage = get_passage($item['_TIMESTAMP']);
114 $link = "<a href=\"$link\" title=\"$title $passage\">$title</a>";
115 $this->items[$date][] = $this->format_link($link);
119 function format_link($link)
121 return "$link<br />\n";
123 function format_list($date,$str)
127 function format_body($str)
131 function toString($timestamp)
134 foreach ($this->items as $date=>$items)
136 $retval .= $this->format_list($date,join('',$items));
138 $retval = $this->format_body($retval);
146 class ShowRSS_html_menubar extends ShowRSS_html
148 var $class = ' class="small"';
150 function format_link($link)
152 return "<li>$link</li>\n";
154 function format_body($str)
156 return "<ul class=\"recent_list\">\n$str</ul>\n";
159 class ShowRSS_html_recent extends ShowRSS_html
161 var $class = ' class="small"';
163 function format_link($link)
165 return "<li>$link</li>\n";
167 function format_list($date,$str)
169 return "<strong>$date</strong>\n<ul class=\"recent_list\">\n$str</ul>\n";
173 function plugin_showrss_get_rss($target,$usecache)
179 // ´ü¸ÂÀÚ¤ì¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢
180 plugin_showrss_cache_expire($usecache);
182 // ¥¥ã¥Ã¥·¥å¤¬¤¢¤ì¤Ð¼èÆÀ¤¹¤ë
183 $filename = CACHE_DIR . encode($target) . '.tmp';
184 if (is_readable($filename))
186 $buf = join('',file($filename));
187 $time = filemtime($filename) - LOCALZONE;
193 $data = http_request($target);
194 if ($data['rc'] !== 200)
196 return array(FALSE,0);
198 $buf = $data['data'];
203 $fp = fopen($filename, 'w');
210 $obj = new ShowRSS_XML();
211 return array($obj->parse($buf),$time);
213 // ´ü¸ÂÀÚ¤ì¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢
214 function plugin_showrss_cache_expire($usecache)
216 $expire = $usecache * 60 * 60; // Hour
218 $dh = dir(CACHE_DIR);
219 while (($file = $dh->read()) !== FALSE)
221 if (substr($file,-4) != '.tmp')
225 $file = CACHE_DIR.$file;
226 $last = time() - filemtime($file);
246 $this->items = array();
247 $this->item = array();
248 $this->is_item = FALSE;
251 $xml_parser = xml_parser_create();
252 xml_set_element_handler($xml_parser,array(&$this,'start_element'),array(&$this,'end_element'));
253 xml_set_character_data_handler($xml_parser,array(&$this,'character_data'));
255 if (!xml_parse($xml_parser,$buf,1))
257 return(sprintf('XML error: %s at line %d in %s',
258 xml_error_string(xml_get_error_code($xml_parser)),
259 xml_get_current_line_number($xml_parser),$buf));
261 xml_parser_free($xml_parser);
265 function escape($str)
267 // RSSÃæ¤Î "< > &" ¤Ê¤É¤ò °ìö "< > &" ¤ËÌᤷ¡¢ ¡ã "&" ¤¬ "&amp;" ¤Ë¤Ê¤Ã¤Á¤ã¤¦¤ÎÂкö
268 // ¤½¤Î¸å¤â¤Ã¤«¤¤"< > &"¤Ê¤É¤ò"< > &"¤Ë¤¹¤ë ¡ã XSSÂкö¡©
269 $str = strtr($str, array_flip(get_html_translation_table(ENT_COMPAT)));
270 $str = htmlspecialchars($str);
273 $str = mb_convert_encoding($str, SOURCE_ENCODING, 'auto');
279 function start_element($parser,$name,$attrs)
285 else if ($name == 'ITEM')
287 $this->is_item = TRUE;
291 function end_element($parser,$name)
293 if (!$this->is_item or $name != 'ITEM')
297 $item = array_map(array(&$this,'escape'),$this->item);
299 $this->item = array();
301 if (array_key_exists('DC:DATE',$item))
303 $time = plugin_showrss_get_timestamp($item['DC:DATE']);
305 else if (array_key_exists('PUBDATE',$item))
307 $time = plugin_showrss_get_timestamp($item['PUBDATE']);
309 else if (array_key_exists('DESCRIPTION',$item)
310 and ($description = trim($item['DESCRIPTION'])) != ''
311 and ($time = strtotime($description)) != -1)
317 $time = time() - LOCALZONE;
319 $item['_TIMESTAMP'] = $time;
320 $date = get_date('Y-m-d',$item['_TIMESTAMP']);
322 $this->items[$date][] = $item;
323 $this->is_item = FALSE;
326 function character_data($parser,$data)
332 if (!array_key_exists($this->tag,$this->item))
334 $this->item[$this->tag] = '';
336 $this->item[$this->tag] .= $data;
339 function plugin_showrss_get_timestamp($str)
341 if (($str = trim($str)) == '')
345 if (!preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/',$str,$matches))
347 $time = strtotime($str);
348 return ($time == -1) ? UTIME : $time - LOCALZONE;
351 $time = strtotime($matches[1].' '.$matches[2]);
352 if (!empty($matches[3]))
354 $diff = ($matches[5]*60+$matches[6])*60;
355 $time += ($matches[4] == '-' ? $diff : -$diff);