OSDN Git Service

BugTrack/2565 Fix read_auth and edit_auth apply condition
[pukiwiki/pukiwiki.git] / plugin / online.inc.php
1 <?php
2 // $Id: online.inc.php,v 1.12 2007/02/10 06:21:53 henoheno Exp $
3 // Copyright (C)
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
7 //
8 // Online plugin -- Just show the number 'users-on-line'
9
10 define('PLUGIN_ONLINE_TIMEOUT', 60 * 5); // Count users in N seconds
11
12 // ----
13
14 // List of 'IP-address|last-access-time(seconds)'
15 define('PLUGIN_ONLINE_USER_LIST', COUNTER_DIR . 'user.dat');
16
17 // Regex of 'IP-address|last-access-time(seconds)'
18 define('PLUGIN_ONLINE_LIST_REGEX', '/^([^\|]+)\|([0-9]+)$/');
19
20 function plugin_online_convert()
21 {
22         return plugin_online_itself(0);
23 }
24
25 function plugin_online_inline()
26 {
27         return plugin_online_itself(1);
28 }
29
30 function plugin_online_itself($type = 0)
31 {
32         static $count, $result, $base;
33
34         if (! isset($count)) {
35                 if (isset($_SERVER['REMOTE_ADDR'])) {
36                         $host  = & $_SERVER['REMOTE_ADDR'];
37                 } else {
38                         $host  = '';
39                 }
40
41                 // Try read
42                 if (plugin_online_check_online($count, $host)) {
43                         $result = TRUE;
44                 } else {
45                         // Write
46                         $result = plugin_online_sweep_records($host);
47                 }
48         }
49
50         if ($result) {
51                 return $count; // Integer
52         } else {
53                 if (! isset($base)) $base = basename(PLUGIN_ONLINE_USER_LIST);
54                 $error = '"COUNTER_DIR/' . $base . '" not writable';
55                 if ($type == 0) {
56                         $error = '#online: ' . $error . '<br />' . "\n";
57                 } else {
58                         $error = '&online: ' . $error . ';';
59                 }
60                 return $error; // String
61         }
62 }
63
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 = '')
67 {
68         if (! pkwk_touch_file(PLUGIN_ONLINE_USER_LIST)) return FALSE;
69
70         // Open
71         $fp = @fopen(PLUGIN_ONLINE_USER_LIST, 'r');
72         if ($fp == FALSE) return FALSE;
73         set_file_buffer($fp, 0);
74
75         // Init
76         $count   = 0;
77         $found   = FALSE;
78         $matches = array();
79
80         flock($fp, LOCK_SH);
81
82         // Read
83         while (! feof($fp)) {
84                 $line = fgets($fp, 512);
85                 if ($line === FALSE) continue;
86
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;
91
92                 ++$count;
93                 if (! $found && $matches[1] == $host) $found = TRUE;
94         }
95
96         flock($fp, LOCK_UN);
97
98         if(! fclose($fp)) return FALSE;
99
100         if (! $found && $host != '') ++$count; // About you
101
102         return $found;
103 }
104
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 = '')
108 {
109         // Open
110         $fp = @fopen(PLUGIN_ONLINE_USER_LIST, 'r+');
111         if ($fp == FALSE) return FALSE;
112         set_file_buffer($fp, 0);
113
114         flock($fp, LOCK_EX);
115
116         // Read to check
117         $lines = @file(PLUGIN_ONLINE_USER_LIST);
118         if ($lines === FALSE) $lines = array();
119
120         // Need modify?
121         $line_count = $count = count($lines);
122         $matches = array();
123         $dirty   = FALSE;
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
130                         --$count;
131                         $dirty = TRUE;
132                 }
133         }
134         if ($host != '' ) {
135                 // Add new, at the top of the record
136                 array_unshift($lines, strtr($host, "\n", '') . '|' . UTIME . "\n");
137                 ++$count;
138                 $dirty = TRUE;
139         }
140
141         if ($dirty) {
142                 // Write
143                 if (! ftruncate($fp, 0)) return FALSE;
144                 rewind($fp);
145                 fputs($fp, join('', $lines));
146         }
147
148         flock($fp, LOCK_UN);
149
150         if(! fclose($fp)) return FALSE;
151
152         return $count; // Number of lines == Number of users online
153 }
154 ?>