2 // $Id: online.inc.php,v 1.12 2007/02/10 06:21:53 henoheno Exp $
4 // 2002-2005, 2007 PukiWiki Developers Team
5 // 2001-2002 Originally written by yu-ji
6 // License: GPL v2 or (at your option) any later version
8 // Online plugin -- Just show the number 'users-on-line'
10 define('PLUGIN_ONLINE_TIMEOUT', 60 * 5); // Count users in N seconds
14 // List of 'IP-address|last-access-time(seconds)'
15 define('PLUGIN_ONLINE_USER_LIST', COUNTER_DIR . 'user.dat');
17 // Regex of 'IP-address|last-access-time(seconds)'
18 define('PLUGIN_ONLINE_LIST_REGEX', '/^([^\|]+)\|([0-9]+)$/');
20 function plugin_online_convert()
22 return plugin_online_itself(0);
25 function plugin_online_inline()
27 return plugin_online_itself(1);
30 function plugin_online_itself($type = 0)
32 static $count, $result, $base;
34 if (! isset($count)) {
35 if (isset($_SERVER['REMOTE_ADDR'])) {
36 $host = & $_SERVER['REMOTE_ADDR'];
42 if (plugin_online_check_online($count, $host)) {
46 $result = plugin_online_sweep_records($host);
51 return $count; // Integer
53 if (! isset($base)) $base = basename(PLUGIN_ONLINE_USER_LIST);
54 $error = '"COUNTER_DIR/' . $base . '" not writable';
56 $error = '#online: ' . $error . '<br />' . "\n";
58 $error = '&online: ' . $error . ';';
60 return $error; // String
64 // Check I am already online (recorded and not time-out)
65 // & $count == Number of online users
66 function plugin_online_check_online(& $count, $host = '')
68 if (! pkwk_touch_file(PLUGIN_ONLINE_USER_LIST)) return FALSE;
71 $fp = @fopen(PLUGIN_ONLINE_USER_LIST, 'r');
72 if ($fp == FALSE) return FALSE;
73 set_file_buffer($fp, 0);
84 $line = fgets($fp, 512);
85 if ($line === FALSE) continue;
87 // Ignore invalid-or-outdated lines
88 if (! preg_match(PLUGIN_ONLINE_LIST_REGEX, $line, $matches) ||
89 ($matches[2] + PLUGIN_ONLINE_TIMEOUT) <= UTIME ||
90 $matches[2] > UTIME) continue;
93 if (! $found && $matches[1] == $host) $found = TRUE;
98 if(! fclose($fp)) return FALSE;
100 if (! $found && $host != '') ++$count; // About you
105 // Cleanup outdated records, Add/Replace new record, Return the number of 'users in N seconds'
106 // NOTE: Call this when plugin_online_check_online() returnes FALSE
107 function plugin_online_sweep_records($host = '')
110 $fp = @fopen(PLUGIN_ONLINE_USER_LIST, 'r+');
111 if ($fp == FALSE) return FALSE;
112 set_file_buffer($fp, 0);
117 $lines = @file(PLUGIN_ONLINE_USER_LIST);
118 if ($lines === FALSE) $lines = array();
121 $line_count = $count = count($lines);
124 for ($i = 0; $i < $line_count; $i++) {
125 if (! preg_match(PLUGIN_ONLINE_LIST_REGEX, $lines[$i], $matches) ||
126 ($matches[2] + PLUGIN_ONLINE_TIMEOUT) <= UTIME ||
127 $matches[2] > UTIME ||
128 $matches[1] == $host) {
129 unset($lines[$i]); // Invalid or outdated or invalid date
135 // Add new, at the top of the record
136 array_unshift($lines, strtr($host, "\n", '') . '|' . UTIME . "\n");
143 if (! ftruncate($fp, 0)) return FALSE;
145 fputs($fp, join('', $lines));
150 if(! fclose($fp)) return FALSE;
152 return $count; // Number of lines == Number of users online