1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* XOOPS Kernel Class |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
13
|
|
|
* @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) |
14
|
|
|
* @package kernel |
15
|
|
|
* @since 2.0.0 |
16
|
|
|
* @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/ |
17
|
|
|
*/ |
18
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A handler for "Who is Online?" information |
22
|
|
|
* |
23
|
|
|
* @package kernel |
24
|
|
|
* |
25
|
|
|
* @author Kazumi Ono <[email protected]> |
26
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
27
|
|
|
*/ |
28
|
|
|
class XoopsOnlineHandler |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Database connection |
32
|
|
|
* |
33
|
|
|
* @var object |
34
|
|
|
* @access private |
35
|
|
|
*/ |
36
|
|
|
public $db; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This should be here, since this really should be a XoopsPersistableObjectHandler |
40
|
|
|
* Here, we fake it for future compatibility |
41
|
|
|
* |
42
|
|
|
* @var string table name |
43
|
|
|
*/ |
44
|
|
|
public $table; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor |
48
|
|
|
* |
49
|
|
|
* @param XoopsDatabase $db {@link XoopsHandlerFactory} |
50
|
|
|
*/ |
51
|
|
|
public function __construct(XoopsDatabase $db) |
52
|
|
|
{ |
53
|
|
|
$this->db = $db; |
54
|
|
|
$this->table = $this->db->prefix('online'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Write online information to the database |
59
|
|
|
* |
60
|
|
|
* @param int $uid UID of the active user |
61
|
|
|
* @param string $uname Username |
62
|
|
|
* @param int $time Timestamp |
63
|
|
|
* @param int $module Current module id |
64
|
|
|
* @param string $ip User's IP address |
65
|
|
|
* |
66
|
|
|
* @internal param string $timestamp |
67
|
|
|
* @return bool TRUE on success |
68
|
|
|
*/ |
69
|
|
|
public function write($uid, $uname, $time, $module, $ip) |
70
|
|
|
{ |
71
|
|
|
$uid = (int) $uid; |
72
|
|
|
$uname = $this->db->quote($uname); |
73
|
|
|
$time = (int) $time; |
74
|
|
|
$module = (int) $module; |
75
|
|
|
$ip = $this->db->quote($ip); |
76
|
|
|
|
77
|
|
|
if ($uid > 0) { |
78
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('online') . " WHERE online_uid={$uid}"; |
79
|
|
|
} else { |
80
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('online') |
81
|
|
|
. " WHERE online_uid={$uid} AND online_ip={$ip}"; |
82
|
|
|
} |
83
|
|
|
list($count) = $this->db->fetchRow($this->db->queryF($sql)); |
84
|
|
|
if ($count > 0) { |
85
|
|
|
$sql = 'UPDATE ' . $this->db->prefix('online') |
86
|
|
|
. " SET online_updated = {$time}, online_module = {$module} WHERE online_uid = {$uid}"; |
87
|
|
|
if ($uid === 0) { |
88
|
|
|
$sql .= " AND online_ip={$ip}"; |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
if ($uid != 0) { |
92
|
|
|
// this condition (no entry for a real user) exists when a user first signs in |
93
|
|
|
// first, cleanup the uid == 0 row the user generated before signing in |
94
|
|
|
$loginSql = sprintf('DELETE FROM %s WHERE online_uid = 0 AND online_ip=%s', $this->db->prefix('online'), $ip); |
95
|
|
|
$this->db->queryF($loginSql); |
96
|
|
|
} |
97
|
|
|
$sql = sprintf( |
98
|
|
|
'INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_module)' |
99
|
|
|
. ' VALUES (%u, %s, %u, %s, %u)', |
100
|
|
|
$this->db->prefix('online'), |
101
|
|
|
$uid, |
102
|
|
|
$uname, |
103
|
|
|
$time, |
104
|
|
|
$ip, |
105
|
|
|
$module |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
if (!$this->db->queryF($sql)) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Delete online information for a user |
117
|
|
|
* |
118
|
|
|
* @param int $uid UID |
119
|
|
|
* |
120
|
|
|
* @return bool TRUE on success |
121
|
|
|
*/ |
122
|
|
|
public function destroy($uid) |
123
|
|
|
{ |
124
|
|
|
$sql = sprintf('DELETE FROM %s WHERE online_uid = %u', $this->db->prefix('online'), $uid); |
125
|
|
|
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Garbage Collection |
134
|
|
|
* |
135
|
|
|
* Delete all online information that has not been updated for a certain time |
136
|
|
|
* |
137
|
|
|
* @param int $expire Expiration time in seconds |
138
|
|
|
*/ |
139
|
|
|
public function gc($expire) |
140
|
|
|
{ |
141
|
|
|
$sql = sprintf( |
142
|
|
|
'DELETE FROM %s WHERE online_updated < %u', |
143
|
|
|
$this->db->prefix('online'), |
144
|
|
|
time() - (int)$expire |
145
|
|
|
); |
146
|
|
|
$this->db->queryF($sql); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get an array of online information |
151
|
|
|
* |
152
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} |
153
|
|
|
* @return array Array of associative arrays of online information |
154
|
|
|
*/ |
155
|
|
|
public function getAll(CriteriaElement $criteria = null) |
156
|
|
|
{ |
157
|
|
|
$ret = array(); |
158
|
|
|
$limit = $start = 0; |
159
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('online'); |
160
|
|
|
if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
161
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
|
|
|
162
|
|
|
$limit = $criteria->getLimit(); |
163
|
|
|
$start = $criteria->getStart(); |
164
|
|
|
} |
165
|
|
|
$result = $this->db->query($sql, $limit, $start); |
166
|
|
|
if (!$result) { |
167
|
|
|
return false; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
170
|
|
|
$ret[] = $myrow; |
171
|
|
|
unset($myrow); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $ret; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Count the number of online users |
179
|
|
|
* |
180
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
public function getCount(CriteriaElement $criteria = null) |
185
|
|
|
{ |
186
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('online'); |
187
|
|
|
if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
188
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
189
|
|
|
} |
190
|
|
|
if (!$result = $this->db->query($sql)) { |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
list($ret) = $this->db->fetchRow($result); |
194
|
|
|
|
195
|
|
|
return $ret; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|