2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: showrss.inc.php,v 1.11 2003/12/03 12:30:02 arino Exp $
7 // modified by PANDA <panda@arino.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 ʸ»úÎó´Ø¿ô¤Ç¤Ê¤ó¤È¤«¤Ê¤é¤Ê¤¯¤â¤Ê¤µ¤²¤Ê¤ó¤Ç¤¹¤¬¼ûÍפäƤɤ줯¤é¤¤¤¢¤ë¤Î¤«¤ï¤«¤é¤¤¤Î¤ÇÊÝα¤Ç¤¹¡£
22 * mbstring ¤â¤¢¤ë¤Û¤¦¤¬¤¤¤¤¤Ç¤¹¡£
24 * ¤Ê¤¤¾ì¹ç¤Ï¡¢ jcode.phps ¤ò¤Á¤ç¤³¤Ã¤È¤¤¤¸¤Ã¤Æ mb_convert_encoding ¤È¤¤¤¦´Ø¿ô¤òÀë¸À¤·¤Æ¤ª¤±¤Ð¤È¤ê¤¢¤¨¤º¤½¤ì¤Ã¤Ý¤¯ÊÑ´¹¤Ç¤¤ë¤«¤â¤Ç¤¹¡£
25 * http://www.spencernetwork.org/
28 * do3ob wiki -> http://do3ob.com/
29 * email -> hiro_do3ob@yahoo.co.jp
31 * ÈòÆñ½ê -> http://do3ob.s20.xrea.com/
33 * version: Id:showrss.inc.php,v 1.40 2003/03/18 11:52:58 hiro Exp
37 // showrss¥×¥é¥°¥¤¥ó¤¬»ÈÍѲÄǽ¤«¤É¤¦¤«¤òɽ¼¨
38 function plugin_showrss_action()
40 $xml_extension = extension_loaded('xml');
41 $mbstring_extension = extension_loaded('mbstring');
43 $xml_msg = $xml_extension ? 'xml extension is loaded' : 'COLOR(RED){xml extension is not loaded}';
44 $mbstring_msg = $mbstring_extension ? 'mbstring extension is loaded' : 'COLOR(RED){mbstring extension is not loaded}';
47 $showrss_info .= "| xml parser | $xml_msg |\n";
48 $showrss_info .= "| multibyte | $mbstring_msg |\n";
50 return array('msg' => 'showrss_info', 'body' => convert_html($showrss_info));
53 function plugin_showrss_convert()
55 if (func_num_args() == 0)
57 // °ú¿ô¤¬¤Ê¤¤¾ì¹ç¤Ï¥¨¥é¡¼
58 return "<p>showrss: no parameter(s).</p>\n";
60 if (!extension_loaded('xml'))
62 // xml ³ÈÄ¥µ¡Ç½¤¬Í¸ú¤Ç¤Ê¤¤¾ì¹ç¡£
63 return "<p>showrss: xml extension is not loaded</p>\n";
66 $array = func_get_args();
67 $rssurl = $tmplname = $usecache = $usetimestamp = '';
69 switch (func_num_args())
72 $usetimestamp = trim($array[3]);
74 $usecache = $array[2];
76 $tmplname = strtolower(trim($array[1]));
78 $rssurl = trim($array[0]);
81 // RSS ¥Ñ¥¹¤ÎÃÍ¥Á¥§¥Ã¥¯
84 return '<p>showrss: syntax error. '.htmlspecialchars($rssurl)."</p>\n";
87 $class = "ShowRSS_html_$tmplname";
88 if (!class_exists($class))
90 $class = 'ShowRSS_html';
93 list($rss,$time) = plugin_showrss_get_rss($rssurl,$usecache);
96 return "<p>showrss: cannot get rss from server.</p>\n";
99 $obj = new $class($rss);
102 if ($usetimestamp > 0)
104 $time = get_date('Y/m/d H:i:s',$time);
105 $timestamp = "<p style=\"font-size:10px; font-weight:bold\">Last-Modified:$time</p>";
107 return $obj->toString($timestamp);
109 // rssÇÛÎ󤫤éhtml¤òºî¤ë
112 var $items = array();
115 function ShowRSS_html($rss)
117 foreach ($rss as $date=>$items)
119 foreach ($items as $item)
121 $link = $item['LINK'];
122 $title = $item['TITLE'];
123 $passage = get_passage($item['_TIMESTAMP']);
124 $link = "<a href=\"$link\" title=\"$title $passage\">$title</a>";
125 $this->items[$date][] = $this->format_link($link);
129 function format_link($link)
131 return "$link<br />\n";
133 function format_list($date,$str)
137 function format_body($str)
141 function toString($timestamp)
144 foreach ($this->items as $date=>$items)
146 $retval .= $this->format_list($date,join('',$items));
148 $retval = $this->format_body($retval);
156 class ShowRSS_html_menubar extends ShowRSS_html
158 var $class = ' class="small"';
160 function format_link($link)
162 return "<li>$link</li>\n";
164 function format_body($str)
166 return "<ul class=\"recent_list\">\n$str</ul>\n";
169 class ShowRSS_html_recent extends ShowRSS_html
171 var $class = ' class="small"';
173 function format_link($link)
175 return "<li>$link</li>\n";
177 function format_list($date,$str)
179 return "<strong>$date</strong>\n<ul class=\"recent_list\">\n$str</ul>\n";
183 function plugin_showrss_get_rss($target,$usecache)
189 // ´ü¸ÂÀÚ¤ì¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢
190 plugin_showrss_cache_expire($usecache);
192 // ¥¥ã¥Ã¥·¥å¤¬¤¢¤ì¤Ð¼èÆÀ¤¹¤ë
193 $filename = CACHE_DIR . encode($target) . '.tmp';
194 if (is_readable($filename))
196 $buf = join('',file($filename));
197 $time = filemtime($filename) - LOCALZONE;
203 $data = http_request($target);
204 if ($data['rc'] !== 200)
206 return array(FALSE,0);
208 $buf = $data['data'];
213 $fp = fopen($filename, 'w');
220 $obj = new ShowRSS_XML();
221 return array($obj->parse($buf),$time);
223 // ´ü¸ÂÀÚ¤ì¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢
224 function plugin_showrss_cache_expire($usecache)
226 $expire = $usecache * 60 * 60; // Hour
228 $dh = dir(CACHE_DIR);
229 while (($file = $dh->read()) !== FALSE)
231 if (substr($file,-4) != '.tmp')
235 $file = CACHE_DIR.$file;
236 $last = time() - filemtime($file);
256 $this->items = array();
257 $this->item = array();
258 $this->is_item = FALSE;
261 $xml_parser = xml_parser_create();
262 xml_set_element_handler($xml_parser,array(&$this,'start_element'),array(&$this,'end_element'));
263 xml_set_character_data_handler($xml_parser,array(&$this,'character_data'));
265 if (!xml_parse($xml_parser,$buf,1))
267 return(sprintf('XML error: %s at line %d in %s',
268 xml_error_string(xml_get_error_code($xml_parser)),
269 xml_get_current_line_number($xml_parser),$buf));
271 xml_parser_free($xml_parser);
275 function escape($str)
277 // RSSÃæ¤Î "< > &" ¤Ê¤É¤ò °ìö "< > &" ¤ËÌᤷ¡¢ ¡ã "&" ¤¬ "&amp;" ¤Ë¤Ê¤Ã¤Á¤ã¤¦¤ÎÂкö
278 // ¤½¤Î¸å¤â¤Ã¤«¤¤"< > &"¤Ê¤É¤ò"< > &"¤Ë¤¹¤ë ¡ã XSSÂкö¡©
279 $str = strtr($str, array_flip(get_html_translation_table(ENT_COMPAT)));
280 $str = htmlspecialchars($str);
283 $str = mb_convert_encoding($str, SOURCE_ENCODING, 'auto');
289 function start_element($parser,$name,$attrs)
295 else if ($name == 'ITEM')
297 $this->is_item = TRUE;
301 function end_element($parser,$name)
303 if (!$this->is_item or $name != 'ITEM')
307 $item = array_map(array(&$this,'escape'),$this->item);
309 $this->item = array();
311 if (array_key_exists('DC:DATE',$item))
313 $time = plugin_showrss_get_timestamp($item['DC:DATE']);
315 else if (array_key_exists('PUBDATE',$item))
317 $time = plugin_showrss_get_timestamp($item['PUBDATE']);
319 else if (array_key_exists('DESCRIPTION',$item)
320 and ($description = trim($item['DESCRIPTION'])) != ''
321 and ($time = strtotime($description)) != -1)
327 $time = time() - LOCALZONE;
329 $item['_TIMESTAMP'] = $time;
330 $date = get_date('Y-m-d',$item['_TIMESTAMP']);
332 $this->items[$date][] = $item;
333 $this->is_item = FALSE;
336 function character_data($parser,$data)
342 if (!array_key_exists($this->tag,$this->item))
344 $this->item[$this->tag] = '';
346 $this->item[$this->tag] .= $data;
349 function plugin_showrss_get_timestamp($str)
351 if (($str = trim($str)) == '')
355 if (!preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/',$str,$matches))
357 $time = strtotime($str);
358 return ($time == -1) ? UTIME : $time - LOCALZONE;
361 $time = strtotime($matches[1].' '.$matches[2]);
362 if (!empty($matches[3]))
364 $diff = ($matches[5]*60+$matches[6])*60;
365 $time += ($matches[4] == '-' ? $diff : -$diff);