OSDN Git Service

BugTrack/191
[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.10 2003/04/23 08:26:19 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'), // PukiWiki/1.4
74 //                      'date'      => date('Y/m/d'), // PukiWiki/1.3.x
75                         'today'     => 0,
76                         'yesterday' => 0,
77                         'ip'        => ''
78                 );
79         }
80         if (!is_page($page))
81         {
82                 return $default;
83         }
84         if (array_key_exists($page,$counters))
85         {
86                 return $counters[$page];
87         }
88         
89         // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤò¥»¥Ã¥È
90         $counters[$page] = $default;
91         
92         // ¥«¥¦¥ó¥¿¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¾ì¹ç¤ÏÆɤ߹þ¤à
93         $fp = NULL;
94         $file = COUNTER_DIR.encode($page).COUNTER_EXT;
95         if (file_exists($file))
96         {
97                 $fp = fopen($file, 'r+')
98                         or die_message('counter.inc.php:cannot read '.$file);
99                 flock($fp,LOCK_EX);
100                 foreach ($default as $key=>$val)
101                 {
102                         $counters[$page][$key] = rtrim(fgets($fp,256));
103                 }
104         }
105         // ¥Õ¥¡¥¤¥ë¹¹¿·¤¬É¬Íפ«?
106         $modify = FALSE;
107         
108         // ÆüÉÕ¤¬ÊѤï¤Ã¤¿
109         if ($counters[$page]['date'] != $default['date'])
110         {
111                 $modify = TRUE;
112                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
113                 $counters[$page]['date']      = $default['date'];
114                 $counters[$page]['yesterday'] = $counters[$page]['today'];
115                 $counters[$page]['today']     = 1;
116                 $counters[$page]['total']++;
117         }
118         // IP¥¢¥É¥ì¥¹¤¬°Û¤Ê¤ë
119         else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR'])
120         {
121                 $modify = TRUE;
122                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
123                 $counters[$page]['today']++;
124                 $counters[$page]['total']++;
125         }
126         
127         //¥Ú¡¼¥¸Æɤ߽Ф·»þ¤Î¤ß¥Õ¥¡¥¤¥ë¤ò¹¹¿·
128 //      for PukiWiki/1.3.x
129 //      $is_read = !(arg_check('add') || arg_check('edit') || arg_check('preview') ||
130 //              $vars['preview'] != '' || $vars['write'] != '');
131 //      for PukiWiki/1.4
132         $is_read = ($vars['cmd'] == 'read');
133         
134         if ($modify and $is_read)
135         {
136                 // ¥Õ¥¡¥¤¥ë¤¬³«¤¤¤Æ¤¤¤ë
137                 if ($fp)
138                 {
139                         // ¥Õ¥¡¥¤¥ë¤ò´Ý¤á¤ë
140                         ftruncate($fp,0);
141                         rewind($fp);
142                 }
143                 else
144                 {
145                         // ¥Õ¥¡¥¤¥ë¤ò³«¤¯
146                         $fp = fopen($file, 'w')
147                                 or die_message('counter.inc.php:cannot write '.$file);
148                         flock($fp,LOCK_EX);
149                 }
150                 // ½ñ¤­½Ð¤¹
151                 foreach (array_keys($default) as $key)
152                 {
153                         fputs($fp,$counters[$page][$key]."\n");
154                 }
155         }
156         // ¥Õ¥¡¥¤¥ë¤¬³«¤¤¤Æ¤¤¤ë
157         if ($fp)
158         {
159                 // ¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë
160                 flock($fp,LOCK_UN);
161                 fclose($fp);
162         }
163         
164         return $counters[$page];
165 }
166 ?>