2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: counter.inc.php,v 1.18 2006/05/27 13:31:11 henoheno Exp $
5 // 2002-2005 PukiWiki Developers Team
6 // 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
11 // Counter file's suffix
12 define('PLUGIN_COUNTER_SUFFIX', '.count');
15 function plugin_counter_inline()
19 // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
20 $args = func_get_args(); // with array_shift()
22 $arg = strtolower(array_shift($args));
24 case '' : $arg = 'total'; /*FALLTHROUGH*/
25 case 'total': /*FALLTHROUGH*/
26 case 'today': /*FALLTHROUGH*/
28 $counter = plugin_counter_get_count($vars['page']);
29 return $counter[$arg];
31 return '&counter([total|today|yesterday]);';
36 function plugin_counter_convert()
40 $counter = plugin_counter_get_count($vars['page']);
43 Counter: {$counter['total']},
44 today: {$counter['today']},
45 yesterday: {$counter['yesterday']}
51 function plugin_counter_get_count($page)
54 static $counters = array();
57 if (! isset($default))
60 'date' => get_date('Y/m/d'),
65 if (! is_page($page)) return $default;
66 if (isset($counters[$page])) return $counters[$page];
69 $counters[$page] = $default;
72 $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
73 $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
74 or die('counter.inc.php: Cannot open COUTER_DIR/' . basename($file));
75 set_file_buffer($fp, 0);
78 foreach ($default as $key=>$val) {
80 $counters[$page][$key] = rtrim(fgets($fp, 256));
83 if ($counters[$page]['date'] != $default['date']) {
86 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', strtotime('yesterday', UTIME)));
87 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
88 $counters[$page]['date'] = $default['date'];
89 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
90 $counters[$page]['today'] = 1;
91 $counters[$page]['total']++;
93 } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
96 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
97 $counters[$page]['today']++;
98 $counters[$page]['total']++;
101 if ($modify && $vars['cmd'] == 'read') {
104 foreach (array_keys($default) as $key)
105 fputs($fp, $counters[$page][$key] . "\n");
110 return $counters[$page];