2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: showrss.inc.php,v 1.14 2004/09/19 14:05:30 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);
247 $this->items = array();
248 $this->item = array();
249 $this->is_item = FALSE;
253 $this->encoding = mb_detect_encoding($buf);
254 if (!in_array(strtolower($this->encoding),array('us-ascii','iso-8859-1','utf-8'))) {
255 $buf = mb_convert_encoding($buf,'utf-8',$this->encoding);
256 $this->encoding = 'utf-8';
258 $xml_parser = xml_parser_create($this->encoding);
259 xml_set_element_handler($xml_parser,array(&$this,'start_element'),array(&$this,'end_element'));
260 xml_set_character_data_handler($xml_parser,array(&$this,'character_data'));
262 if (!xml_parse($xml_parser,$buf,1))
264 return(sprintf('XML error: %s at line %d in %s',
265 xml_error_string(xml_get_error_code($xml_parser)),
266 xml_get_current_line_number($xml_parser),$buf));
268 xml_parser_free($xml_parser);
272 function escape($str)
274 // RSSÃæ¤Î "< > &" ¤Ê¤É¤ò °ìö "< > &" ¤ËÌᤷ¡¢ ¡ã "&" ¤¬ "&amp;" ¤Ë¤Ê¤Ã¤Á¤ã¤¦¤ÎÂкö
275 // ¤½¤Î¸å¤â¤Ã¤«¤¤"< > &"¤Ê¤É¤ò"< > &"¤Ë¤¹¤ë ¡ã XSSÂкö¡©
276 $str = strtr($str, array_flip(get_html_translation_table(ENT_COMPAT)));
277 $str = htmlspecialchars($str);
280 $str = mb_convert_encoding($str, SOURCE_ENCODING, $this->encoding);
286 function start_element($parser,$name,$attrs)
292 else if ($name == 'ITEM')
294 $this->is_item = TRUE;
298 function end_element($parser,$name)
300 if (!$this->is_item or $name != 'ITEM')
304 $item = array_map(array(&$this,'escape'),$this->item);
306 $this->item = array();
308 if (array_key_exists('DC:DATE',$item))
310 $time = plugin_showrss_get_timestamp($item['DC:DATE']);
312 else if (array_key_exists('PUBDATE',$item))
314 $time = plugin_showrss_get_timestamp($item['PUBDATE']);
316 else if (array_key_exists('DESCRIPTION',$item)
317 and ($description = trim($item['DESCRIPTION'])) != ''
318 and ($time = strtotime($description)) != -1)
324 $time = time() - LOCALZONE;
326 $item['_TIMESTAMP'] = $time;
327 $date = get_date('Y-m-d',$item['_TIMESTAMP']);
329 $this->items[$date][] = $item;
330 $this->is_item = FALSE;
333 function character_data($parser,$data)
339 if (!array_key_exists($this->tag,$this->item))
341 $this->item[$this->tag] = '';
343 $this->item[$this->tag] .= $data;
346 function plugin_showrss_get_timestamp($str)
348 if (($str = trim($str)) == '')
352 if (!preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/',$str,$matches))
354 $time = strtotime($str);
355 return ($time == -1) ? UTIME : $time - LOCALZONE;
358 $time = strtotime($matches[1].' '.$matches[2]);
359 if (!empty($matches[3]))
361 $diff = ($matches[5]*60+$matches[6])*60;
362 $time += ($matches[4] == '-' ? $diff : -$diff);