3 * PukiWiki counter plugin
4 * $Id: counter.inc.php,v 1.15 2004/12/04 06:54:34 henoheno Exp $
6 * (C) 2004 PukiWiki Developer Team
7 * (C) 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
10 // Counter file's suffix
11 define('PLUGIN_COUNTER_SUFFIX', '.count');
13 function plugin_counter_inline()
17 $arg = strtolower(array_shift(func_get_args()));
19 case '' : $arg = 'total'; /*FALLTHROUGH*/
20 case 'total': /*FALLTHROUGH*/
21 case 'today': /*FALLTHROUGH*/
23 $counter = plugin_counter_get_count($vars['page']);
24 return $counter[$arg];
26 return '&counter([total|today|yesterday]);';
30 function plugin_counter_convert()
34 $counter = plugin_counter_get_count($vars['page']);
37 Counter: {$counter['total']},
38 today: {$counter['today']},
39 yesterday: {$counter['yesterday']}
44 function plugin_counter_get_count($page)
47 static $counters = array();
50 if (! isset($default))
53 'date' => get_date('Y/m/d'),
58 if (! is_page($page)) return $default;
59 if (isset($counters[$page])) return $counters[$page];
62 $counters[$page] = $default;
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);
71 foreach ($default as $key=>$val) {
73 $counters[$page][$key] = rtrim(fgets($fp, 256));
76 if ($counters[$page]['date'] != $default['date']) {
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']++;
86 } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
89 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
90 $counters[$page]['today']++;
91 $counters[$page]['total']++;
94 if ($modify && $vars['cmd'] == 'read') {
97 foreach (array_keys($default) as $key)
98 fputs($fp, $counters[$page][$key] . "\n");
103 return $counters[$page];