OSDN Git Service

Cleanup.
authorhenoheno <henoheno>
Tue, 2 May 2006 08:15:43 +0000 (17:15 +0900)
committerhenoheno <henoheno>
Tue, 2 May 2006 08:15:43 +0000 (17:15 +0900)
* Show usage and error detail
* Simplify
PKWK_SAFE_MODE prohibits action
* plugin_showrss_get_timestamp(): Check if strtotime() returns -1

plugin/showrss.inc.php

index 455fae5..2eedec7 100644 (file)
@@ -1,67 +1,75 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
-// $Id: showrss.inc.php,v 1.18 2006/05/02 01:28:01 henoheno Exp $
+// $Id: showrss.inc.php,v 1.19 2006/05/02 08:15:43 henoheno Exp $
 //  Id:showrss.inc.php,v 1.40 2003/03/18 11:52:58 hiro Exp
 // Copyright (C):
-//     2002-2005 PukiWiki Developers Team
+//     2002-2006 PukiWiki Developers Team
 //     2002      PANDA <panda@arino.jp>
 //     (Original)hiro_do3ob@yahoo.co.jp
 // License: GPL, same as PukiWiki
 //
 // Show RSS (of remote site) plugin
-// NOTE: This plugin needs 'PHP xml extension'
+// NOTE:
+//    * This plugin needs 'PHP xml extension'
+//    * Cache data will be stored as CACHE_DIR/*.tmp
 
-// Show this plugin is enable or not for this PukiWiki
+define('PLUGIN_SHOWRSS_USAGE', '#showrss(URI-to-RSS[,default|menubar|recent[,Cache-lifetime[,Show-timestamp]]])');
+
+// Show related extensions are found or not
 function plugin_showrss_action()
 {
-       $xml_extension      = extension_loaded('xml');
-       $mbstring_extension = extension_loaded('mbstring');
-
-       $xml_msg      = $xml_extension      ? 'xml extension is loaded'      : 'COLOR(RED){xml extension is not loaded}';
-       $mbstring_msg = $mbstring_extension ? 'mbstring extension is loaded' : 'COLOR(RED){mbstring extension is not loaded}';
-       $showrss_info  =
-               '| xml parser | ' . $xml_msg      . ' |' . "\n" .
-               '| multibyte  | ' . $mbstring_msg . ' |' . "\n";
-
-       return array('msg' => 'showrss_info', 'body' => convert_html($showrss_info));
+       if (PKWK_SAFE_MODE) die_message('PKWK_SAFE_MODE prohibit this');
+
+       $body = '';
+       foreach(array('xml', 'mbstring') as $extension){
+               $$extension = extension_loaded($extension) ?
+                       '&color(green){Found};' :
+                       '&color(red){Not found};';
+               $body .= '| ' . $extension . ' extension | ' . $$extension . ' |' . "\n";
+       }
+       return array('msg' => 'showrss_info', 'body' => convert_html($body));
 }
 
 function plugin_showrss_convert()
 {
-       if (func_num_args() == 0) {
-               return '<p>showrss: no parameter(s).</p>' . "\n";
-       } else if (! extension_loaded('xml')) {
-               return '<p>showrss: xml extension is not loaded</p>' . "\n";
+       static $_xml;
+
+       if (! isset($_xml)) $_xml = extension_loaded('xml');
+       if (! $_xml) return '#showrss: xml extension is not found<br />' . "\n";
+
+       $num = func_num_args();
+       if ($num == 0) return PLUGIN_SHOWRSS_USAGE . '<br />' . "\n";
+
+       $argv = func_get_args();
+       $timestamp = FALSE;
+       $cachehour = 0;
+       $template = $uri = '';
+       switch ($num) {
+       case 4: $timestamp = (trim($argv[3]) == '1');   /*FALLTHROUGH*/
+       case 3: $cachehour = trim($argv[2]);            /*FALLTHROUGH*/
+       case 2: $template  = strtolower(trim($argv[1]));/*FALLTHROUGH*/
+       case 1: $uri       = trim($argv[0]);
        }
 
-       $array  = func_get_args();
-       $rssurl = $tmplname = $usecache = $usetimestamp = '';
-
-       switch (func_num_args()) {
-       case 4: $usetimestamp = trim($array[3]);
-       case 3: $usecache     = $array[2];
-       case 2: $tmplname     = strtolower(trim($array[1]));
-       case 1: $rssurl       = trim($array[0]);
+       $class = ($template == '' || $template == 'default') ? 'ShowRSS_html' : 'ShowRSS_html_' . $template;
+       if (! is_numeric($cachehour))
+               return '#showrss: Cache-lifetime seems not numeric: ' . htmlspecialchars($cachehour) . '<br />' . "\n";
+       if (! class_exists($class))
+               return '#showrss: Template not found: ' . htmlspecialchars($template) . '<br />' . "\n";
+       if (! is_url($uri))
+               return '#showrss: Seems not URI: ' . htmlspecialchars($uri) . '<br />' . "\n";
+
+       list($rss, $time) = plugin_showrss_get_rss($uri, $cachehour);
+       if ($rss === FALSE) return '#showrss: Failed fetching RSS from the server<br />' . "\n";
+
+       $time = '';
+       if ($timestamp > 0) {
+               $time = '<p style="font-size:10px; font-weight:bold">Last-Modified:' .
+                       get_date('Y/m/d H:i:s', $time) .  '</p>';
        }
 
-       if (! is_url($rssurl))
-               return '<p>showrss: syntax error. ' . htmlspecialchars($rssurl) . '</p>' . "\n";
-
-       $class = 'ShowRSS_html_' . $tmplname;
-       if (! class_exists($class)) $class = 'ShowRSS_html';
-
-       list($rss, $time) = plugin_showrss_get_rss($rssurl, $usecache);
-       if ($rss === FALSE)
-               return '<p>showrss: cannot get rss from server.</p>' . "\n";
-
        $obj = new $class($rss);
-
-       $timestamp = '';
-       if ($usetimestamp > 0) {
-               $time = get_date('Y/m/d H:i:s',$time);
-               $timestamp = '<p style="font-size:10px; font-weight:bold">Last-Modified:' . $time . '</p>';
-       }
-       return $obj->toString($timestamp);
+       return $obj->toString($time);
 }
 
 // Create HTML from RSS array()
@@ -140,14 +148,14 @@ class ShowRSS_html_recent extends ShowRSS_html
        }
 }
 
-// Get RSS
-function plugin_showrss_get_rss($target, $usecache)
+// Get and save RSS
+function plugin_showrss_get_rss($target, $cachehour)
 {
-       $buf = '';
+       $buf  = '';
        $time = NULL;
-       if ($usecache) {
+       if ($cachehour) {
                // Remove expired cache
-               plugin_showrss_cache_expire($usecache);
+               plugin_showrss_cache_expire($cachehour);
 
                // Get the cache not expired
                $filename = CACHE_DIR . encode($target) . '.tmp';
@@ -167,7 +175,7 @@ function plugin_showrss_get_rss($target, $usecache)
                $time = UTIME;
 
                // Save RSS into cache
-               if ($usecache) {
+               if ($cachehour) {
                        $fp = fopen($filename, 'w');
                        fwrite($fp, $buf);
                        fclose($fp);
@@ -180,9 +188,9 @@ function plugin_showrss_get_rss($target, $usecache)
 }
 
 // Remove cache if expired limit exeed
-function plugin_showrss_cache_expire($usecache)
+function plugin_showrss_cache_expire($cachehour)
 {
-       $expire = $usecache * 60 * 60; // Hour
+       $expire = $cachehour * 60 * 60; // Hour
        $dh = dir(CACHE_DIR);
        while (($file = $dh->read()) !== FALSE) {
                if (substr($file, -4) != '.tmp') continue;
@@ -215,10 +223,11 @@ class ShowRSS_XML
                        $buf = mb_convert_encoding($buf, 'utf-8', $this->encoding);
                        $this->encoding = 'utf-8';
                }
+
+               // Parsing
                $xml_parser = xml_parser_create($this->encoding);
                xml_set_element_handler($xml_parser, array(& $this, 'start_element'), array(& $this, 'end_element'));
                xml_set_character_data_handler($xml_parser, array(& $this, 'character_data'));
-
                if (! xml_parse($xml_parser, $buf, 1)) {
                        return(sprintf('XML error: %s at line %d in %s',
                                xml_error_string(xml_get_error_code($xml_parser)),
@@ -289,20 +298,22 @@ class ShowRSS_XML
 
 function plugin_showrss_get_timestamp($str)
 {
-       if (($str = trim($str)) == '')
-               return UTIME;
+       $str = trim($str);
+       if ($str == '') return UTIME;
 
        $matches = array();
-       if (! preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/', $str, $matches)) {
+       if (preg_match('/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})(([+-])(\d{2}):(\d{2}))?/', $str, $matches)) {
+               $time = strtotime($matches[1] . ' ' . $matches[2]);
+               if ($time == -1) {
+                       $time = UTIME;
+               } else if ($matches[3]) {
+                       $diff = ($matches[5] * 60 + $matches[6]) * 60;
+                       $time += ($matches[4] == '-' ? $diff : -$diff);
+               }
+               return $time;
+       } else {
                $time = strtotime($str);
                return ($time == -1) ? UTIME : $time - LOCALZONE;
        }
-       $str  = $matches[1];
-       $time = strtotime($matches[1] . ' ' . $matches[2]);
-       if (! empty($matches[3])) {
-               $diff = ($matches[5] * 60 + $matches[6]) * 60;
-               $time += ($matches[4] == '-' ? $diff : -$diff);
-       }
-       return $time;
 }
 ?>