OSDN Git Service

Cleanup. 'and'/'or' for boorean => '&&'/'||'.
[pukiwiki/pukiwiki.git] / plugin / showrss.inc.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: showrss.inc.php,v 1.14 2004/09/19 14:05:30 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         var $encoding;
243
244         function parse($buf)
245         {
246                 // ½é´ü²½
247                 $this->items = array();
248                 $this->item = array();
249                 $this->is_item = FALSE;
250                 $this->tag = '';
251
252                 // Ê¸»ú¥³¡¼¥É¸¡½Ð
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';
257                 }
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'));
261
262                 if (!xml_parse($xml_parser,$buf,1))
263                 {
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));
267                 }
268                 xml_parser_free($xml_parser);
269
270                 return $this->items;
271         }
272         function escape($str)
273         {
274                 // RSSÃæ¤Î "&lt; &gt; &amp;" ¤Ê¤É¤ò °ìö "< > &" ¤ËÌᤷ¡¢ ¡ã "&amp;" ¤¬ "&amp;amp;" ¤Ë¤Ê¤Ã¤Á¤ã¤¦¤ÎÂкö
275                 // ¤½¤Î¸å¤â¤Ã¤«¤¤"< > &"¤Ê¤É¤ò"&lt; &gt; &amp;"¤Ë¤¹¤ë  ¡ã XSSÂкö¡©
276                 $str = strtr($str, array_flip(get_html_translation_table(ENT_COMPAT)));
277                 $str = htmlspecialchars($str);
278
279                 // Ê¸»ú¥³¡¼¥ÉÊÑ´¹
280                 $str = mb_convert_encoding($str, SOURCE_ENCODING, $this->encoding);
281
282                 return trim($str);
283         }
284
285         // ¥¿¥°³«»Ï
286         function start_element($parser,$name,$attrs)
287         {
288                 if ($this->is_item)
289                 {
290                         $this->tag = $name;
291                 }
292                 else if ($name == 'ITEM')
293                 {
294                         $this->is_item = TRUE;
295                 }
296         }
297         // ¥¿¥°½ªÎ»
298         function end_element($parser,$name)
299         {
300                 if (!$this->is_item or $name != 'ITEM')
301                 {
302                         return;
303                 }
304                 $item = array_map(array(&$this,'escape'),$this->item);
305
306                 $this->item = array();
307
308                 if (array_key_exists('DC:DATE',$item))
309                 {
310                         $time = plugin_showrss_get_timestamp($item['DC:DATE']);
311                 }
312                 else if (array_key_exists('PUBDATE',$item))
313                 {
314                         $time = plugin_showrss_get_timestamp($item['PUBDATE']);
315                 }
316                 else if (array_key_exists('DESCRIPTION',$item)
317                         and ($description = trim($item['DESCRIPTION'])) != ''
318                         and ($time = strtotime($description)) != -1)
319                 {
320                         $time -= LOCALZONE;
321                 }
322                 else
323                 {
324                         $time = time() - LOCALZONE;
325                 }
326                 $item['_TIMESTAMP'] = $time;
327                 $date = get_date('Y-m-d',$item['_TIMESTAMP']);
328
329                 $this->items[$date][] = $item;
330                 $this->is_item = FALSE;
331         }
332         // ¥­¥ã¥é¥¯¥¿
333         function character_data($parser,$data)
334         {
335                 if (!$this->is_item)
336                 {
337                         return;
338                 }
339                 if (!array_key_exists($this->tag,$this->item))
340                 {
341                         $this->item[$this->tag] = '';
342                 }
343                 $this->item[$this->tag] .= $data;
344         }
345 }
346 function plugin_showrss_get_timestamp($str)
347 {
348         if (($str = trim($str)) == '')
349         {
350                 return UTIME;
351         }
352         if (!preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/',$str,$matches))
353         {
354                 $time = strtotime($str);
355                 return ($time == -1) ? UTIME : $time - LOCALZONE;
356         }
357         $str = $matches[1];
358         $time = strtotime($matches[1].' '.$matches[2]);
359         if (!empty($matches[3]))
360         {
361                 $diff = ($matches[5]*60+$matches[6])*60;
362                 $time += ($matches[4] == '-' ? $diff : -$diff);
363         }
364         return $time;
365 }
366 ?>