OSDN Git Service

BugTrack/292:rewrite counter update routine.
[pukiwiki/pukiwiki.git] / plugin / counter.inc.php
1 <?php
2 /*
3  * PukiWiki ¥«¥¦¥ó¥¿¡¼¥×¥é¥°¥¤¥ó
4  *
5  * CopyRight 2002 Y.MASUI GPL2
6  * http://masui.net/pukiwiki/ masui@masui.net
7  *
8  * $Id: counter.inc.php,v 1.8 2003/04/13 04:51:46 arino Exp $
9  */
10
11 // counter file
12 if (!defined('COUNTER_DIR'))
13 {
14         define('COUNTER_DIR', './counter/');
15 }
16
17 if (!defined('COUNTER_EXT'))
18 {
19         define('COUNTER_EXT','.count');
20 }
21
22 function plugin_counter_inline()
23 {
24         global $vars;
25         
26         $arg = '';
27         if (func_num_args() > 0)
28         {
29                 $args = func_get_args();
30                 $arg = strtolower($args[0]);
31         }
32         
33         $counter = plugin_counter_get_count($vars['page']);
34         
35         switch ($arg)
36         {
37                 case 'today':
38                 case 'yesterday':
39                         $count = $counter[$arg];
40                         break;
41                 default:
42                         $count = $counter['total'];
43         }
44         return $count;
45 }
46
47 function plugin_counter_convert()
48 {
49         global $vars;
50         
51         $counter = plugin_counter_get_count($vars['page']);
52         
53         return <<<EOD
54 <div class="counter">
55 Counter: {$counter['total']},
56 today: {$counter['today']},
57 yesterday: {$counter['yesterday']}
58 </div>
59 EOD;
60 }
61
62 function plugin_counter_get_count($page)
63 {
64         global $vars;
65         static $counters = array();
66         static $default;
67         
68         // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃÍ
69         if (!isset($default))
70         {
71         $default = array(
72                 'total'     => 0,
73                 'date'      => get_date('Y/m/d'),
74                 'today'     => 0,
75                 'yesterday' => 0,
76                 'ip'        => ''
77         );
78         }
79         if (!is_page($page))
80         {
81                 return $default;
82         }
83         if (array_key_exists($page,$counters))
84         {
85                 return $counters[$page];
86         }
87         
88         // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤò¥»¥Ã¥È
89         $counters[$page] = $default;
90         
91         // ¥«¥¦¥ó¥¿¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¾ì¹ç¤ÏÆɤ߹þ¤à
92         $fp = NULL;
93         $file = COUNTER_DIR.encode($page).COUNTER_EXT;
94         if (file_exists($file))
95         {
96                 $fp = fopen($file, 'r+')
97                         or die_message('counter.inc.php:cannot read '.$file);
98                 flock($fp,LOCK_EX);
99         foreach ($default as $key=>$val)
100         {
101                 $counters[$page][$key] = rtrim(fgets($fp));
102         }
103         }
104         // ¥Õ¥¡¥¤¥ë¹¹¿·¤¬É¬Íפ«?
105         $modify = FALSE;
106         
107         // ÆüÉÕ¤¬ÊѤï¤Ã¤¿
108         if ($counters[$page]['date'] != $default['date'])
109         {
110                 $modify = TRUE;
111                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
112                 $counters[$page]['date']      = $default['date'];
113                 $counters[$page]['yesterday'] = $counters[$page]['today'];
114                 $counters[$page]['today']     = 1;
115                 $counters[$page]['total']++;
116         }
117         // IP¥¢¥É¥ì¥¹¤¬°Û¤Ê¤ë
118         else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR'])
119         {
120                 $modify = TRUE;
121                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
122                 $counters[$page]['today']++;
123                 $counters[$page]['total']++;
124         }
125         
126         //¥Ú¡¼¥¸Æɤ߽Ф·»þ¤Î¤ß¥Õ¥¡¥¤¥ë¤ò¹¹¿·
127         if ($modify and $vars['cmd'] == 'read')
128         {
129                 // ¥Õ¥¡¥¤¥ë¤¬³«¤¤¤Æ¤¤¤ë
130                 if ($fp)
131                 {
132                         // ¥Õ¥¡¥¤¥ë¤ò´Ý¤á¤ë
133                         ftruncate($fp,0);
134                         rewind($fp);
135                 }
136                 else
137                 {
138                         // ¥Õ¥¡¥¤¥ë¤ò³«¤¯
139                 $fp = fopen($file, 'w')
140                         or die_message('counter.inc.php:cannot write '.$file);
141                 flock($fp,LOCK_EX);
142                 }
143                 // ½ñ¤­½Ð¤¹
144                 foreach (array_keys($default) as $key)
145                 {
146                         fputs($fp,$counters[$page][$key]."\n");
147                 }
148         }
149         // ¥Õ¥¡¥¤¥ë¤¬³«¤¤¤Æ¤¤¤ë
150         if ($fp)
151         {
152                 // ¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë
153                 flock($fp,LOCK_UN);
154                 fclose($fp);
155         }
156         
157         return $counters[$page];
158 }
159 ?>