2 // $Id: counter.inc.php,v 1.17 2005/10/04 14:31:22 henoheno Exp $
4 // PukiWiki counter plugin
6 // (C) 2002-2005 PukiWiki Developers Team
7 // (C) 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
9 // Counter file's suffix
10 define('PLUGIN_COUNTER_SUFFIX', '.count');
13 function plugin_counter_inline()
17 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
18 $args = func_get_args(); // with array_shift()
20 $arg = strtolower(array_shift($args));
22 case '' : $arg = 'total'; /*FALLTHROUGH*/
23 case 'total': /*FALLTHROUGH*/
24 case 'today': /*FALLTHROUGH*/
26 $counter = plugin_counter_get_count($vars['page']);
27 return $counter[$arg];
29 return '&counter([total|today|yesterday]);';
34 function plugin_counter_convert()
38 $counter = plugin_counter_get_count($vars['page']);
41 Counter: {$counter['total']},
42 today: {$counter['today']},
43 yesterday: {$counter['yesterday']}
49 function plugin_counter_get_count($page)
52 static $counters = array();
55 if (! isset($default))
58 'date' => get_date('Y/m/d'),
63 if (! is_page($page)) return $default;
64 if (isset($counters[$page])) return $counters[$page];
67 $counters[$page] = $default;
70 $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
71 $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
72 or die('counter.inc.php: Cannot open COUTER_DIR/' . basename($file));
73 set_file_buffer($fp, 0);
76 foreach ($default as $key=>$val) {
78 $counters[$page][$key] = rtrim(fgets($fp, 256));
81 if ($counters[$page]['date'] != $default['date']) {
84 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', strtotime('yesterday', UTIME)));
85 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
86 $counters[$page]['date'] = $default['date'];
87 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
88 $counters[$page]['today'] = 1;
89 $counters[$page]['total']++;
91 } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
94 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
95 $counters[$page]['today']++;
96 $counters[$page]['total']++;
99 if ($modify && $vars['cmd'] == 'read') {
102 foreach (array_keys($default) as $key)
103 fputs($fp, $counters[$page][$key] . "\n");
108 return $counters[$page];