OSDN Git Service

2ch.net/PukiWiki/1/188 XHTML needs '&' everywhere, not '&'
[pukiwiki/pukiwiki.git] / plugin / counter.inc.php
index 7e22104..702a7b1 100644 (file)
  * CopyRight 2002 Y.MASUI GPL2
  * http://masui.net/pukiwiki/ masui@masui.net
  *
- * $Id: counter.inc.php,v 1.7 2003/04/02 04:16:01 panda Exp $
+ * $Id: counter.inc.php,v 1.14 2004/07/31 03:09:20 henoheno Exp $
  */
 
 // counter file
-if (!defined('COUNTER_DIR'))
-{
-       define('COUNTER_DIR', './counter/');
-}
-
 if (!defined('COUNTER_EXT'))
 {
        define('COUNTER_EXT','.count');
 }
 
-function plugin_counter_convert()
-{
-       list($count,$today_count,$yesterday_count) = plugin_counter_count();
-       return "<div class=\"counter\">Counter: $count, today: $today_count, yesterday: $yesterday_count</div>";
-}
-
 function plugin_counter_inline()
 {
+       global $vars;
+
        $arg = '';
        if (func_num_args() > 0)
        {
-               list($arg) = func_get_args();
+               $args = func_get_args();
+               $arg = strtolower($args[0]);
        }
-       
-       list($count,$today_count,$yesterday_count) = plugin_counter_count();
-       
-       if ($arg == 'today')
-       {
-               return $today_count;
-       }
-       else if ($arg == 'yesterday')
+
+       $counter = plugin_counter_get_count($vars['page']);
+
+       switch ($arg)
        {
-               return $yesterday_count;
+               case 'today':
+               case 'yesterday':
+                       $count = $counter[$arg];
+                       break;
+               default:
+                       $count = $counter['total'];
        }
        return $count;
 }
-function plugin_counter_count()
+
+function plugin_counter_convert()
 {
-       global $vars,$HTTP_SERVER_VARS;
-       static $counters;
-       
-       $page = $vars['page'];
-       
-       if (!is_page($page))
+       global $vars;
+
+       $counter = plugin_counter_get_count($vars['page']);
+
+       return <<<EOD
+<div class="counter">
+Counter: {$counter['total']},
+today: {$counter['today']},
+yesterday: {$counter['yesterday']}
+</div>
+EOD;
+}
+
+function plugin_counter_get_count($page)
+{
+       global $vars;
+       static $counters = array();
+       static $default;
+
+       // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃÍ
+       if (!isset($default))
        {
-               return array(0,0,0);
+               $default = array(
+                       'total'     => 0,
+                       'date'      => get_date('Y/m/d'),
+                       'today'     => 0,
+                       'yesterday' => 0,
+                       'ip'        => ''
+               );
        }
-       if (!isset($counters))
+       if (!is_page($page))
        {
-               $counters = array();
+               return $default;
        }
        if (array_key_exists($page,$counters))
        {
                return $counters[$page];
        }
-       
+
+       // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤò¥»¥Ã¥È
+       $counters[$page] = $default;
+
+       // ¥«¥¦¥ó¥¿¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¾ì¹ç¤ÏÆɤ߹þ¤à
        $file = COUNTER_DIR.encode($page).COUNTER_EXT;
-       if (!file_exists($file))
+       $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
+               or die_message('counter.inc.php:cannot open '.$file);
+       set_file_buffer($fp, 0);
+       flock($fp,LOCK_EX);
+       rewind($fp);
+
+       foreach ($default as $key=>$val)
        {
-               $nf = fopen($file, 'w');
-               flock($nf,LOCK_EX);
-               fputs($nf,"0\n0\n0\n0\n\n");
-               flock($nf,LOCK_UN);
-               fclose($nf);
+               $counters[$page][$key] = rtrim(fgets($fp,256));
+               if (feof($fp)) { break; }
        }
-       
-       $array = file($file);
-       $count = rtrim($array[0]);
-       $today = rtrim($array[1]);
-       $today_count = rtrim($array[2]);
-       $yesterday_count = rtrim($array[3]);
-       $ip = rtrim($array[4]);
-       
-       //Á°²ó¤ÈIP¥¢¥É¥ì¥¹¤¬°Û¤Ê¤Ã¤¿¾ì¹ç¤Ë¥«¥¦¥ó¥¿¤ò²ó¤¹
-       if ($ip != $HTTP_SERVER_VARS['REMOTE_ADDR'])
+       // ¥Õ¥¡¥¤¥ë¹¹¿·¤¬É¬Íפ«?
+       $modify = FALSE;
+
+       // ÆüÉÕ¤¬ÊѤï¤Ã¤¿
+       if ($counters[$page]['date'] != $default['date'])
        {
-               $t = get_date('Y/m/d');
-               if ($t != $today)
-               {
-                       $yesterday_count = $today_count;
-                       $today_count = 0;
-                       $today = $t;
-               }
-               ++$count;
-               ++$today_count;
+               $modify = TRUE;
+               $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d',strtotime('yesterday',UTIME)));
+               $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
+               $counters[$page]['date']      = $default['date'];
+               $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
+               $counters[$page]['today']     = 1;
+               $counters[$page]['total']++;
+       }
+       // IP¥¢¥É¥ì¥¹¤¬°Û¤Ê¤ë
+       else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR'])
+       {
+               $modify = TRUE;
+               $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
+               $counters[$page]['today']++;
+               $counters[$page]['total']++;
        }
+
        //¥Ú¡¼¥¸Æɤ߽Ф·»þ¤Î¤ß¥Õ¥¡¥¤¥ë¤ò¹¹¿·
-       if ($vars['cmd'] == 'read')
+       if ($modify and $vars['cmd'] == 'read')
        {
-               $ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
-               $nf = fopen($file, 'w');
-               flock($nf,LOCK_EX);
-               fputs($nf,"$count\n");
-               fputs($nf,"$today\n");
-               fputs($nf,"$today_count\n");
-               fputs($nf,"$yesterday_count\n");
-               fputs($nf,"$ip\n");
-               flock($nf,LOCK_UN);
-               fclose($nf);
+               // ¥Õ¥¡¥¤¥ë¤ò´Ý¤á¤ë
+               rewind($fp);
+               ftruncate($fp,0);
+               // ½ñ¤­½Ð¤¹
+               foreach (array_keys($default) as $key)
+               {
+                       fputs($fp,$counters[$page][$key]."\n");
+               }
        }
-       
-       $counters[$page] = array($count,$today_count,$yesterday_count);
-       
+       // ¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë
+       flock($fp,LOCK_UN);
+       fclose($fp);
+
        return $counters[$page];
 }
 ?>