OSDN Git Service

* Enable old design #ref(filename,BrackettedBracketName)
[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.14 2004/07/31 03:09:20 henoheno Exp $
9  */
10
11 // counter file
12 if (!defined('COUNTER_EXT'))
13 {
14         define('COUNTER_EXT','.count');
15 }
16
17 function plugin_counter_inline()
18 {
19         global $vars;
20
21         $arg = '';
22         if (func_num_args() > 0)
23         {
24                 $args = func_get_args();
25                 $arg = strtolower($args[0]);
26         }
27
28         $counter = plugin_counter_get_count($vars['page']);
29
30         switch ($arg)
31         {
32                 case 'today':
33                 case 'yesterday':
34                         $count = $counter[$arg];
35                         break;
36                 default:
37                         $count = $counter['total'];
38         }
39         return $count;
40 }
41
42 function plugin_counter_convert()
43 {
44         global $vars;
45
46         $counter = plugin_counter_get_count($vars['page']);
47
48         return <<<EOD
49 <div class="counter">
50 Counter: {$counter['total']},
51 today: {$counter['today']},
52 yesterday: {$counter['yesterday']}
53 </div>
54 EOD;
55 }
56
57 function plugin_counter_get_count($page)
58 {
59         global $vars;
60         static $counters = array();
61         static $default;
62
63         // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃÍ
64         if (!isset($default))
65         {
66                 $default = array(
67                         'total'     => 0,
68                         'date'      => get_date('Y/m/d'),
69                         'today'     => 0,
70                         'yesterday' => 0,
71                         'ip'        => ''
72                 );
73         }
74         if (!is_page($page))
75         {
76                 return $default;
77         }
78         if (array_key_exists($page,$counters))
79         {
80                 return $counters[$page];
81         }
82
83         // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤò¥»¥Ã¥È
84         $counters[$page] = $default;
85
86         // ¥«¥¦¥ó¥¿¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¾ì¹ç¤ÏÆɤ߹þ¤à
87         $file = COUNTER_DIR.encode($page).COUNTER_EXT;
88         $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
89                 or die_message('counter.inc.php:cannot open '.$file);
90         set_file_buffer($fp, 0);
91         flock($fp,LOCK_EX);
92         rewind($fp);
93
94         foreach ($default as $key=>$val)
95         {
96                 $counters[$page][$key] = rtrim(fgets($fp,256));
97                 if (feof($fp)) { break; }
98         }
99         // ¥Õ¥¡¥¤¥ë¹¹¿·¤¬É¬Íפ«?
100         $modify = FALSE;
101
102         // ÆüÉÕ¤¬ÊѤï¤Ã¤¿
103         if ($counters[$page]['date'] != $default['date'])
104         {
105                 $modify = TRUE;
106                 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d',strtotime('yesterday',UTIME)));
107                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
108                 $counters[$page]['date']      = $default['date'];
109                 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
110                 $counters[$page]['today']     = 1;
111                 $counters[$page]['total']++;
112         }
113         // IP¥¢¥É¥ì¥¹¤¬°Û¤Ê¤ë
114         else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR'])
115         {
116                 $modify = TRUE;
117                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
118                 $counters[$page]['today']++;
119                 $counters[$page]['total']++;
120         }
121
122         //¥Ú¡¼¥¸Æɤ߽Ф·»þ¤Î¤ß¥Õ¥¡¥¤¥ë¤ò¹¹¿·
123         if ($modify and $vars['cmd'] == 'read')
124         {
125                 // ¥Õ¥¡¥¤¥ë¤ò´Ý¤á¤ë
126                 rewind($fp);
127                 ftruncate($fp,0);
128                 // ½ñ¤­½Ð¤¹
129                 foreach (array_keys($default) as $key)
130                 {
131                         fputs($fp,$counters[$page][$key]."\n");
132                 }
133         }
134         // ¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë
135         flock($fp,LOCK_UN);
136         fclose($fp);
137
138         return $counters[$page];
139 }
140 ?>