OSDN Git Service

Added spaces only
[pukiwiki/pukiwiki.git] / plugin / showrss.inc.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: showrss.inc.php,v 1.12 2004/06/20 04:20:18 henoheno Exp $
6 //
7 // Modified version by PANDA <panda@arino.jp>
8 //
9
10 /**
11  *
12  * showrss ¥×¥é¥°¥¤¥ó (Created by hiro_do3ob@yahoo.co.jp)
13  * 
14  * ¥é¥¤¥»¥ó¥¹¤Ï PukiWiki ËÜÂΤÈƱ¤¸¤¯ GNU General Public License (GPL) ¤Ç¤¹¡£
15  * http://www.gnu.org/licenses/gpl.txt
16  *
17  * pukiwikiÍѤΥץ饰¥¤¥ó¤Ç¤¹¡£
18  * pukiwiki1.3.2°Ê¾å¤ÇÆ°¤¯¤È»×¤¤¤Þ¤¹¡£
19  * 
20  * º£¤Î¤È¤³¤íÆ°ºî¤µ¤»¤ë¤¿¤á¤Ë¤ÏPHP ¤Î xml extension ¤¬É¬¿Ü¤Ç¤¹¡£PHP¤ËÁȤ߹þ¤Þ¤ì¤Æ¤Ê¤¤¾ì¹ç¤Ï¤½¤Ã¤±¤Ê¤¤¥¨¥é¡¼¤¬½Ð¤ë¤È»×¤¤¤Þ¤¹¡£
21  * Àµµ¬É½¸½ or Ê¸»úÎó´Ø¿ô¤Ç¤Ê¤ó¤È¤«¤Ê¤é¤Ê¤¯¤â¤Ê¤µ¤²¤Ê¤ó¤Ç¤¹¤¬¼ûÍפäƤɤ줯¤é¤¤¤¢¤ë¤Î¤«¤ï¤«¤é¤¤¤Î¤ÇÊÝα¤Ç¤¹¡£
22  * 
23  * version: Id:showrss.inc.php,v 1.40 2003/03/18 11:52:58 hiro Exp
24  * 
25  */
26
27 // showrss¥×¥é¥°¥¤¥ó¤¬»ÈÍѲÄǽ¤«¤É¤¦¤«¤òɽ¼¨
28 function plugin_showrss_action()
29 {
30         $xml_extension = extension_loaded('xml');
31         $mbstring_extension = extension_loaded('mbstring');
32
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}';
35
36         $showrss_info = '';
37         $showrss_info .= "| xml parser | $xml_msg |\n";
38         $showrss_info .= "| multibyte | $mbstring_msg |\n";
39
40         return array('msg' => 'showrss_info', 'body' => convert_html($showrss_info));
41 }
42
43 function plugin_showrss_convert()
44 {
45         if (func_num_args() == 0)
46         {
47                 // °ú¿ô¤¬¤Ê¤¤¾ì¹ç¤Ï¥¨¥é¡¼
48                 return "<p>showrss: no parameter(s).</p>\n";
49         }
50         if (!extension_loaded('xml'))
51         {
52                 // xml ³ÈÄ¥µ¡Ç½¤¬Í­¸ú¤Ç¤Ê¤¤¾ì¹ç¡£
53                 return "<p>showrss: xml extension is not loaded</p>\n";
54         }
55
56         $array = func_get_args();
57         $rssurl = $tmplname = $usecache = $usetimestamp = '';
58
59         switch (func_num_args())
60         {
61                 case 4:
62                         $usetimestamp = trim($array[3]);
63                 case 3:
64                         $usecache = $array[2];
65                 case 2:
66                         $tmplname = strtolower(trim($array[1]));
67                 case 1:
68                         $rssurl = trim($array[0]);
69         }
70
71         // RSS ¥Ñ¥¹¤ÎÃÍ¥Á¥§¥Ã¥¯
72         if (!is_url($rssurl))
73         {
74                 return '<p>showrss: syntax error. '.htmlspecialchars($rssurl)."</p>\n";
75         }
76
77         $class = "ShowRSS_html_$tmplname";
78         if (!class_exists($class))
79         {
80                 $class = 'ShowRSS_html';
81         }
82
83         list($rss,$time) = plugin_showrss_get_rss($rssurl,$usecache);
84         if ($rss === FALSE)
85         {
86                 return "<p>showrss: cannot get rss from server.</p>\n";
87         }
88
89         $obj = new $class($rss);
90
91         $timestamp = '';
92         if ($usetimestamp > 0)
93         {
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>";
96         }
97         return $obj->toString($timestamp);
98 }
99 // rssÇÛÎ󤫤éhtml¤òºî¤ë
100 class ShowRSS_html
101 {
102         var $items = array();
103         var $class = '';
104
105         function ShowRSS_html($rss)
106         {
107                 foreach ($rss as $date=>$items)
108                 {
109                         foreach ($items as $item)
110                         {
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);
116                         }
117                 }
118         }
119         function format_link($link)
120         {
121                 return "$link<br />\n";
122         }
123         function format_list($date,$str)
124         {
125                 return $str;
126         }
127         function format_body($str)
128         {
129                 return $str;
130         }
131         function toString($timestamp)
132         {
133                 $retval = '';
134                 foreach ($this->items as $date=>$items)
135                 {
136                         $retval .= $this->format_list($date,join('',$items));
137                 }
138                 $retval = $this->format_body($retval);
139                 return <<<EOD
140 <div{$this->class}>
141 $retval$timestamp
142 </div>
143 EOD;
144         }
145 }
146 class ShowRSS_html_menubar extends ShowRSS_html
147 {
148         var $class = ' class="small"';
149
150         function format_link($link)
151         {
152                 return "<li>$link</li>\n";
153         }
154         function format_body($str)
155         {
156                 return "<ul class=\"recent_list\">\n$str</ul>\n";
157         }
158 }
159 class ShowRSS_html_recent extends ShowRSS_html
160 {
161         var $class = ' class="small"';
162
163         function format_link($link)
164         {
165                 return "<li>$link</li>\n";
166         }
167         function format_list($date,$str)
168         {
169                 return "<strong>$date</strong>\n<ul class=\"recent_list\">\n$str</ul>\n";
170         }
171 }
172 // rss¤ò¼èÆÀ¤¹¤ë
173 function plugin_showrss_get_rss($target,$usecache)
174 {
175         $buf = '';
176         $time = NULL;
177         if ($usecache)
178         {
179                 // ´ü¸ÂÀÚ¤ì¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢
180                 plugin_showrss_cache_expire($usecache);
181
182                 // ¥­¥ã¥Ã¥·¥å¤¬¤¢¤ì¤Ð¼èÆÀ¤¹¤ë
183                 $filename = CACHE_DIR . encode($target) . '.tmp';
184                 if (is_readable($filename))
185                 {
186                         $buf = join('',file($filename));
187                         $time = filemtime($filename) - LOCALZONE;
188                 }
189         }
190         if ($time === NULL)
191         {
192                 // rssËÜÂΤò¼èÆÀ
193                 $data = http_request($target);
194                 if ($data['rc'] !== 200)
195                 {
196                         return array(FALSE,0);
197                 }
198                 $buf = $data['data'];
199                 $time = UTIME;
200                 // ¥­¥ã¥Ã¥·¥å¤òÊݸ
201                 if ($usecache)
202                 {
203                         $fp = fopen($filename, 'w');
204                         fwrite($fp,$buf);
205                         fclose($fp);
206                 }
207         }
208
209         // parse
210         $obj = new ShowRSS_XML();
211         return array($obj->parse($buf),$time);
212 }
213 // ´ü¸ÂÀÚ¤ì¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢
214 function plugin_showrss_cache_expire($usecache)
215 {
216         $expire = $usecache * 60 * 60; // Hour
217
218         $dh = dir(CACHE_DIR);
219         while (($file = $dh->read()) !== FALSE)
220         {
221                 if (substr($file,-4) != '.tmp')
222                 {
223                         continue;
224                 }
225                 $file = CACHE_DIR.$file;
226                 $last = time() - filemtime($file);
227
228                 if ($last > $expire)
229                 {
230                         unlink($file);
231                 }
232         }
233         $dh->close();
234 }
235 // rss¤ò¼èÆÀ¡¦ÇÛÎó²½
236 class ShowRSS_XML
237 {
238         var $items;
239         var $item;
240         var $is_item;
241         var $tag;
242
243         function parse($buf)
244         {
245                 // ½é´ü²½
246                 $this->items = array();
247                 $this->item = array();
248                 $this->is_item = FALSE;
249                 $this->tag = '';
250
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'));
254
255                 if (!xml_parse($xml_parser,$buf,1))
256                 {
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));
260                 }
261                 xml_parser_free($xml_parser);
262
263                 return $this->items;
264         }
265         function escape($str)
266         {
267                 // RSSÃæ¤Î "&lt; &gt; &amp;" ¤Ê¤É¤ò °ìö "< > &" ¤ËÌᤷ¡¢ ¡ã "&amp;" ¤¬ "&amp;amp;" ¤Ë¤Ê¤Ã¤Á¤ã¤¦¤ÎÂкö
268                 // ¤½¤Î¸å¤â¤Ã¤«¤¤"< > &"¤Ê¤É¤ò"&lt; &gt; &amp;"¤Ë¤¹¤ë  ¡ã XSSÂкö¡©
269                 $str = strtr($str, array_flip(get_html_translation_table(ENT_COMPAT)));
270                 $str = htmlspecialchars($str);
271
272                 // Ê¸»ú¥³¡¼¥ÉÊÑ´¹
273                 $str = mb_convert_encoding($str, SOURCE_ENCODING, 'auto');
274
275                 return trim($str);
276         }
277
278         // ¥¿¥°³«»Ï
279         function start_element($parser,$name,$attrs)
280         {
281                 if ($this->is_item)
282                 {
283                         $this->tag = $name;
284                 }
285                 else if ($name == 'ITEM')
286                 {
287                         $this->is_item = TRUE;
288                 }
289         }
290         // ¥¿¥°½ªÎ»
291         function end_element($parser,$name)
292         {
293                 if (!$this->is_item or $name != 'ITEM')
294                 {
295                         return;
296                 }
297                 $item = array_map(array(&$this,'escape'),$this->item);
298
299                 $this->item = array();
300
301                 if (array_key_exists('DC:DATE',$item))
302                 {
303                         $time = plugin_showrss_get_timestamp($item['DC:DATE']);
304                 }
305                 else if (array_key_exists('PUBDATE',$item))
306                 {
307                         $time = plugin_showrss_get_timestamp($item['PUBDATE']);
308                 }
309                 else if (array_key_exists('DESCRIPTION',$item)
310                         and ($description = trim($item['DESCRIPTION'])) != ''
311                         and ($time = strtotime($description)) != -1)
312                 {
313                         $time -= LOCALZONE;
314                 }
315                 else
316                 {
317                         $time = time() - LOCALZONE;
318                 }
319                 $item['_TIMESTAMP'] = $time;
320                 $date = get_date('Y-m-d',$item['_TIMESTAMP']);
321
322                 $this->items[$date][] = $item;
323                 $this->is_item = FALSE;
324         }
325         // ¥­¥ã¥é¥¯¥¿
326         function character_data($parser,$data)
327         {
328                 if (!$this->is_item)
329                 {
330                         return;
331                 }
332                 if (!array_key_exists($this->tag,$this->item))
333                 {
334                         $this->item[$this->tag] = '';
335                 }
336                 $this->item[$this->tag] .= $data;
337         }
338 }
339 function plugin_showrss_get_timestamp($str)
340 {
341         if (($str = trim($str)) == '')
342         {
343                 return UTIME;
344         }
345         if (!preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/',$str,$matches))
346         {
347                 $time = strtotime($str);
348                 return ($time == -1) ? UTIME : $time - LOCALZONE;
349         }
350         $str = $matches[1];
351         $time = strtotime($matches[1].' '.$matches[2]);
352         if (!empty($matches[3]))
353         {
354                 $diff = ($matches[5]*60+$matches[6])*60;
355                 $time += ($matches[4] == '-' ? $diff : -$diff);
356         }
357         return $time;
358 }
359 ?>