OSDN Git Service

BugTrack/791: Fix typo 0,91 => 0.91
[pukiwiki/pukiwiki.git] / plugin / counter.inc.php
1 <?php
2 /*
3  * PukiWiki counter plugin
4  * $Id: counter.inc.php,v 1.15 2004/12/04 06:54:34 henoheno Exp $
5  *
6  * (C) 2004 PukiWiki Developer Team
7  * (C) 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
8  */
9
10 // Counter file's suffix
11 define('PLUGIN_COUNTER_SUFFIX', '.count');
12
13 function plugin_counter_inline()
14 {
15         global $vars;
16
17         $arg = strtolower(array_shift(func_get_args()));
18         switch ($arg) {
19         case ''     : $arg = 'total'; /*FALLTHROUGH*/
20         case 'total': /*FALLTHROUGH*/
21         case 'today': /*FALLTHROUGH*/
22         case 'yesterday':
23                 $counter = plugin_counter_get_count($vars['page']);
24                 return $counter[$arg];
25         default:
26                 return '&counter([total|today|yesterday]);';
27         }
28 }
29
30 function plugin_counter_convert()
31 {
32         global $vars;
33
34         $counter = plugin_counter_get_count($vars['page']);
35         return <<<EOD
36 <div class="counter">
37 Counter:   {$counter['total']},
38 today:     {$counter['today']},
39 yesterday: {$counter['yesterday']}
40 </div>
41 EOD;
42 }
43
44 function plugin_counter_get_count($page)
45 {
46         global $vars;
47         static $counters = array();
48         static $default;
49
50         if (! isset($default))
51                 $default = array(
52                         'total'     => 0,
53                         'date'      => get_date('Y/m/d'),
54                         'today'     => 0,
55                         'yesterday' => 0,
56                         'ip'        => '');
57
58         if (! is_page($page)) return $default;
59         if (isset($counters[$page])) return $counters[$page];
60
61         // Set default
62         $counters[$page] = $default;
63         $modify = FALSE;
64
65         $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
66         $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
67                 or die_message('counter.inc.php: Cannot open ' . $file);
68         set_file_buffer($fp, 0);
69         flock($fp, LOCK_EX);
70         rewind($fp);
71         foreach ($default as $key=>$val) {
72                 // Update
73                 $counters[$page][$key] = rtrim(fgets($fp, 256));
74                 if (feof($fp)) break;
75         }
76         if ($counters[$page]['date'] != $default['date']) {
77                 // New day
78                 $modify = TRUE;
79                 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', strtotime('yesterday', UTIME)));
80                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
81                 $counters[$page]['date']      = $default['date'];
82                 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
83                 $counters[$page]['today']     = 1;
84                 $counters[$page]['total']++;
85
86         } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
87                 // Not the same host
88                 $modify = TRUE;
89                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
90                 $counters[$page]['today']++;
91                 $counters[$page]['total']++;
92         }
93         // Modify
94         if ($modify && $vars['cmd'] == 'read') {
95                 rewind($fp);
96                 ftruncate($fp, 0);
97                 foreach (array_keys($default) as $key)
98                         fputs($fp, $counters[$page][$key] . "\n");
99         }
100         flock($fp, LOCK_UN);
101         fclose($fp);
102
103         return $counters[$page];
104 }
105 ?>