2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: counter.inc.php,v 1.19 2007/02/04 11:14:44 henoheno Exp $
5 // 2002-2005, 2007 PukiWiki Developers Team
6 // 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
9 // Counter plugin (per page)
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;
73 $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
74 pkwk_touch_file($file);
75 $fp = fopen($file, 'r+')
76 or die('counter.inc.php: Cannot open COUNTER_DIR/' . basename($file));
77 set_file_buffer($fp, 0);
82 foreach (array_keys($default) as $key) {
84 $counters[$page][$key] = rtrim(fgets($fp, 256));
89 if ($counters[$page]['date'] != $default['date']) {
91 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', UTIME - 24 * 60 * 60));
92 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
93 $counters[$page]['date'] = $default['date'];
94 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
95 $counters[$page]['today'] = 1;
96 $counters[$page]['total']++;
97 } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
100 $counters[$page]['ip'] = $_SERVER['REMOTE_ADDR'];
101 $counters[$page]['today']++;
102 $counters[$page]['total']++;
106 if ($modify && $vars['cmd'] == 'read') {
109 foreach (array_keys($default) as $key)
110 fputs($fp, $counters[$page][$key] . "\n");
117 return $counters[$page];