2 // $Id: online.inc.php,v 1.9 2005/06/25 10:03:20 henoheno Exp $
4 // 2002-2005 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 (! file_exists(PLUGIN_ONLINE_USER_LIST) &&
69 ! @touch(PLUGIN_ONLINE_USER_LIST))
73 $fp = @fopen(PLUGIN_ONLINE_USER_LIST, 'r');
74 if ($fp == FALSE) return FALSE;
75 set_file_buffer($fp, 0);
86 $line = fgets($fp, 512);
87 if ($line === FALSE) continue;
89 // Ignore invalid-or-outdated lines
90 if (! preg_match(PLUGIN_ONLINE_LIST_REGEX, $line, $matches) ||
91 ($matches[2] + PLUGIN_ONLINE_TIMEOUT) <= UTIME ||
92 $matches[2] > UTIME) continue;
95 if (! $found && $matches[1] == $host) $found = TRUE;
100 if(! fclose($fp)) return FALSE;
102 if (! $found && $host != '') ++$count; // About you
107 // Cleanup outdated records, Add/Replace new record, Return the number of 'users in N seconds'
108 // NOTE: Call this when plugin_online_check_online() returnes FALSE
109 function plugin_online_sweep_records($host = '')
112 $fp = @fopen(PLUGIN_ONLINE_USER_LIST, 'r+');
113 if ($fp == FALSE) return FALSE;
114 set_file_buffer($fp, 0);
119 $lines = @file(PLUGIN_ONLINE_USER_LIST);
120 if ($lines === FALSE) $lines = array();
123 $line_count = $count = count($lines);
126 for ($i = 0; $i < $line_count; $i++) {
127 if (! preg_match(PLUGIN_ONLINE_LIST_REGEX, $lines[$i], $matches) ||
128 ($matches[2] + PLUGIN_ONLINE_TIMEOUT) <= UTIME ||
129 $matches[2] > UTIME ||
130 $matches[1] == $host) {
131 unset($lines[$i]); // Invalid or outdated or invalid date
137 // Add new, at the top of the record
138 array_unshift($lines, strtr($host, "\n", '') . '|' . UTIME . "\n");
145 if (! ftruncate($fp, 0)) return FALSE;
147 fputs($fp, join('', $lines));
152 if(! fclose($fp)) return FALSE;
154 return $count; // Number of lines == Number of users online