1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* NewBB 5.0x, the forum module for XOOPS project |
4
|
|
|
* |
5
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
6
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
7
|
|
|
* @author Taiwen Jiang (phppp or D.J.) <[email protected]> |
8
|
|
|
* @since 4.00 |
9
|
|
|
* @package module::newbb |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Xmf\IPAddress; |
13
|
|
|
|
14
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.'); |
|
|
|
|
15
|
|
|
|
16
|
|
|
include_once __DIR__ . '/../include/functions.config.php'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class NewbbOnlineHandler |
20
|
|
|
*/ |
21
|
|
|
class NewbbOnlineHandler |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
public $db; |
24
|
|
|
public $forum_id; |
25
|
|
|
public $forumObject; |
26
|
|
|
public $topic_id; |
27
|
|
|
public $user_ids = []; |
28
|
|
|
|
29
|
|
|
public function __construct(XoopsDatabase $db) |
30
|
|
|
{ |
31
|
|
|
$this->db = $db; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param null|\NewbbForum $forum |
36
|
|
|
* @param null|Topic $forumtopic |
37
|
|
|
*/ |
38
|
|
|
public function init($forum = null, $forumtopic = null) |
39
|
|
|
{ |
40
|
|
View Code Duplication |
if (is_object($forum)) { |
|
|
|
|
41
|
|
|
$this->forum_id = $forum->getVar('forum_id'); |
42
|
|
|
$this->forumObject = $forum; |
43
|
|
|
} else { |
44
|
|
|
$this->forum_id = (int)$forum; |
45
|
|
|
$this->forumObject = $forum; |
46
|
|
|
} |
47
|
|
View Code Duplication |
if (is_object($forumtopic)) { |
|
|
|
|
48
|
|
|
$this->topic_id = $forumtopic->getVar('topic_id'); |
49
|
|
|
if (empty($this->forum_id)) { |
50
|
|
|
$this->forum_id = $forumtopic->getVar('forum_id'); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$this->topic_id = (int)$forumtopic; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->update(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function update() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
global $xoopsModule; |
|
|
|
|
62
|
|
|
|
63
|
|
|
mt_srand((double)microtime() * 1000000); |
64
|
|
|
// set gc probabillity to 10% for now.. |
65
|
|
|
if (mt_rand(1, 100) < 60) { |
66
|
|
|
$this->gc(150); |
67
|
|
|
} |
68
|
|
|
if (is_object($GLOBALS['xoopsUser'])) { |
69
|
|
|
$uid = $GLOBALS['xoopsUser']->getVar('uid'); |
70
|
|
|
$uname = $GLOBALS['xoopsUser']->getVar('uname'); |
71
|
|
|
$name = $GLOBALS['xoopsUser']->getVar('name'); |
72
|
|
|
} else { |
73
|
|
|
$uid = 0; |
74
|
|
|
$uname = ''; |
75
|
|
|
$name = ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$xoops_onlineHandler = xoops_getHandler('online'); |
79
|
|
|
$xoopsupdate = $xoops_onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), Xmf\IPAddress::fromRequest()->asReadable()); |
80
|
|
|
if (!$xoopsupdate) { |
|
|
|
|
81
|
|
|
//xoops_error("newbb online upate error"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$uname = (empty($GLOBALS['xoopsModuleConfig']['show_realname']) || empty($name)) ? $uname : $name; |
85
|
|
|
$this->write($uid, $uname, time(), $this->forum_id, IPAddress::fromRequest()->asReadable(), $this->topic_id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $xoopsTpl |
90
|
|
|
*/ |
91
|
|
|
public function render(Smarty $xoopsTpl) |
92
|
|
|
{ |
93
|
|
|
include_once __DIR__ . '/../include/functions.render.php'; |
94
|
|
|
include_once __DIR__ . '/../include/functions.user.php'; |
95
|
|
|
$criteria = null; |
96
|
|
View Code Duplication |
if ($this->topic_id) { |
|
|
|
|
97
|
|
|
$criteria = new Criteria('online_topic', $this->topic_id); |
98
|
|
|
} elseif ($this->forum_id) { |
99
|
|
|
$criteria = new Criteria('online_forum', $this->forum_id); |
100
|
|
|
} |
101
|
|
|
$users = $this->getAll($criteria); |
102
|
|
|
$num_total = count($users); |
103
|
|
|
|
104
|
|
|
$num_user = 0; |
105
|
|
|
$users_id = []; |
106
|
|
|
$users_online = []; |
107
|
|
View Code Duplication |
for ($i = 0; $i < $num_total; ++$i) { |
|
|
|
|
108
|
|
|
if (empty($users[$i]['online_uid'])) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
$users_id[] = $users[$i]['online_uid']; |
112
|
|
|
$users_online[$users[$i]['online_uid']] = [ |
113
|
|
|
'link' => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'], |
114
|
|
|
'uname' => $users[$i]['online_uname'] |
115
|
|
|
]; |
116
|
|
|
++$num_user; |
117
|
|
|
} |
118
|
|
|
$num_anonymous = $num_total - $num_user; |
119
|
|
|
$online = []; |
120
|
|
|
$online['image'] = newbbDisplayImage('whosonline'); |
121
|
|
|
$online['num_total'] = $num_total; |
122
|
|
|
$online['num_user'] = $num_user; |
123
|
|
|
$online['num_anonymous'] = $num_anonymous; |
124
|
|
|
$administrator_list = newbbIsModuleAdministrators($users_id); |
125
|
|
|
$moderator_list = []; |
126
|
|
View Code Duplication |
if ($member_list = array_diff(array_keys($administrator_list), $users_id)) { |
|
|
|
|
127
|
|
|
if (is_object($this->forumObject)) { |
128
|
|
|
$moderator_list = $this->forumObject->getVar('forum_moderator'); |
129
|
|
|
} else { |
130
|
|
|
$moderator_list = newbbIsForumModerators($member_list); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
foreach ($users_online as $uid => $user) { |
134
|
|
|
if (!empty($administrator_list[$uid])) { |
135
|
|
|
$user['level'] = 2; |
136
|
|
|
} elseif (!empty($moderator_list[$uid])) { |
137
|
|
|
$user['level'] = 1; |
138
|
|
|
} else { |
139
|
|
|
$user['level'] = 0; |
140
|
|
|
} |
141
|
|
|
$online['users'][] = $user; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$xoopsTpl->assign_by_ref('online', $online); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Deprecated |
149
|
|
|
*/ |
150
|
|
|
public function showOnline() |
151
|
|
|
{ |
152
|
|
|
include_once __DIR__ . '/../include/functions.render.php'; |
153
|
|
|
include_once __DIR__ . '/../include/functions.user.php'; |
154
|
|
|
$criteria = null; |
155
|
|
View Code Duplication |
if ($this->topic_id) { |
|
|
|
|
156
|
|
|
$criteria = new Criteria('online_topic', $this->topic_id); |
157
|
|
|
} elseif ($this->forum_id) { |
158
|
|
|
$criteria = new Criteria('online_forum', $this->forum_id); |
159
|
|
|
} |
160
|
|
|
$users = $this->getAll($criteria); |
161
|
|
|
$num_total = count($users); |
162
|
|
|
|
163
|
|
|
$num_user = 0; |
164
|
|
|
$users_id = []; |
165
|
|
|
$users_online = []; |
166
|
|
View Code Duplication |
for ($i = 0; $i < $num_total; ++$i) { |
|
|
|
|
167
|
|
|
if (empty($users[$i]['online_uid'])) { |
168
|
|
|
continue; |
169
|
|
|
} |
170
|
|
|
$users_id[] = $users[$i]['online_uid']; |
171
|
|
|
$users_online[$users[$i]['online_uid']] = [ |
172
|
|
|
'link' => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'], |
173
|
|
|
'uname' => $users[$i]['online_uname'] |
174
|
|
|
]; |
175
|
|
|
++$num_user; |
176
|
|
|
} |
177
|
|
|
$num_anonymous = $num_total - $num_user; |
178
|
|
|
$online = []; |
179
|
|
|
$online['image'] = newbbDisplayImage('whosonline'); |
180
|
|
|
$online['statistik'] = newbbDisplayImage('statistik'); |
181
|
|
|
$online['num_total'] = $num_total; |
182
|
|
|
$online['num_user'] = $num_user; |
183
|
|
|
$online['num_anonymous'] = $num_anonymous; |
184
|
|
|
$administrator_list = newbbIsModuleAdministrators($users_id); |
185
|
|
|
$moderator_list = []; |
186
|
|
View Code Duplication |
if ($member_list = array_diff($users_id, array_keys($administrator_list))) { |
|
|
|
|
187
|
|
|
if (is_object($this->forumObject)) { |
188
|
|
|
$moderator_list = $this->forumObject->getVar('forum_moderator'); |
189
|
|
|
} else { |
190
|
|
|
$moderator_list = newbbIsForumModerators($member_list); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
foreach ($users_online as $uid => $user) { |
195
|
|
|
if (in_array($uid, $administrator_list)) { |
196
|
|
|
$user['level'] = 2; |
197
|
|
|
} elseif (in_array($uid, $moderator_list)) { |
198
|
|
|
$user['level'] = 1; |
199
|
|
|
} else { |
200
|
|
|
$user['level'] = 0; |
201
|
|
|
} |
202
|
|
|
$online['users'][] = $user; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $online; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Write online information to the database |
210
|
|
|
* |
211
|
|
|
* @param int $uid UID of the active user |
212
|
|
|
* @param string $uname Username |
213
|
|
|
* @param $time |
214
|
|
|
* @param string $forum_id Current forum_id |
215
|
|
|
* @param string $ip User's IP adress |
216
|
|
|
* @param $topic_id |
217
|
|
|
* @return bool TRUE on success |
218
|
|
|
* @internal param string $timestamp |
219
|
|
|
*/ |
220
|
|
|
public function write($uid, $uname, $time, $forum_id, $ip, $topic_id) |
221
|
|
|
{ |
222
|
|
|
global $xoopsModule, $xoopsDB; |
|
|
|
|
223
|
|
|
|
224
|
|
|
$uid = (int)$uid; |
225
|
|
|
if ($uid > 0) { |
226
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid; |
227
|
|
|
} else { |
228
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid . " AND online_ip='" . $ip . "'"; |
229
|
|
|
} |
230
|
|
|
list($count) = $this->db->fetchRow($this->db->queryF($sql)); |
231
|
|
|
if ($count > 0) { |
232
|
|
|
$sql = 'UPDATE ' . $this->db->prefix('newbb_online') . " SET online_updated= '" . $time . "', online_forum = '" . $forum_id . "', online_topic = '" . $topic_id . "' WHERE online_uid = " . $uid; |
233
|
|
|
if (0 == $uid) { |
234
|
|
|
$sql .= " AND online_ip='" . $ip . "'"; |
235
|
|
|
} |
236
|
|
|
} else { |
237
|
|
|
$sql = sprintf('INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_forum, online_topic) VALUES (%u, %s, %u, %s, %u, %u)', $this->db->prefix('newbb_online'), $uid, $this->db->quote($uname), $time, $this->db->quote($ip), $forum_id, $topic_id); |
238
|
|
|
} |
239
|
|
|
if (!$this->db->queryF($sql)) { |
240
|
|
|
//xoops_error($this->db->error()); |
|
|
|
|
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** @var XoopsOnlineHandler $xoops_onlineHandler */ |
245
|
|
|
$xoops_onlineHandler = xoops_getHandler('online'); |
246
|
|
|
$xoopsOnlineTable = $xoops_onlineHandler->table; |
247
|
|
|
|
248
|
|
|
$sql = 'DELETE FROM ' |
249
|
|
|
. $this->db->prefix('newbb_online') |
250
|
|
|
. ' WHERE' |
251
|
|
|
. ' ( online_uid > 0 AND online_uid NOT IN ( SELECT online_uid FROM ' |
252
|
|
|
. $xoopsOnlineTable |
253
|
|
|
. ' WHERE online_module =' |
254
|
|
|
. $xoopsModule->getVar('mid') |
255
|
|
|
. ' ) )' |
256
|
|
|
. ' OR ( online_uid = 0 AND online_ip NOT IN ( SELECT online_ip FROM ' |
257
|
|
|
. $xoopsOnlineTable |
258
|
|
|
. ' WHERE online_module =' |
259
|
|
|
. $xoopsModule->getVar('mid') |
260
|
|
|
. ' AND online_uid = 0 ) )'; |
261
|
|
|
|
262
|
|
|
if ($result = $this->db->queryF($sql)) { |
|
|
|
|
263
|
|
|
return true; |
264
|
|
|
} else { |
265
|
|
|
//xoops_error($this->db->error()); |
|
|
|
|
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Garbage Collection |
272
|
|
|
* |
273
|
|
|
* Delete all online information that has not been updated for a certain time |
274
|
|
|
* |
275
|
|
|
* @param int $expire Expiration time in seconds |
276
|
|
|
*/ |
277
|
|
|
public function gc($expire) |
278
|
|
|
{ |
279
|
|
|
global $xoopsModule; |
|
|
|
|
280
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_updated < ' . (time() - (int)$expire); |
281
|
|
|
$this->db->queryF($sql); |
282
|
|
|
|
283
|
|
|
$xoops_onlineHandler = xoops_getHandler('online'); |
284
|
|
|
$xoops_onlineHandler->gc($expire); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get an array of online information |
289
|
|
|
* |
290
|
|
|
* @param CriteriaElement $criteria {@link CriteriaElement} |
|
|
|
|
291
|
|
|
* @return array Array of associative arrays of online information |
292
|
|
|
*/ |
293
|
|
|
public function getAll(CriteriaElement $criteria = null) |
294
|
|
|
{ |
295
|
|
|
$ret = []; |
296
|
|
|
$limit = $start = 0; |
297
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('newbb_online'); |
298
|
|
|
if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
299
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
300
|
|
|
$limit = $criteria->getLimit(); |
301
|
|
|
$start = $criteria->getStart(); |
302
|
|
|
} |
303
|
|
|
$result = $this->db->query($sql, $limit, $start); |
304
|
|
|
if (!$result) { |
305
|
|
|
return $ret; |
306
|
|
|
} |
307
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
308
|
|
|
$ret[] = $myrow; |
309
|
|
|
if ($myrow['online_uid'] > 0) { |
310
|
|
|
$this->user_ids[] = $myrow['online_uid']; |
311
|
|
|
} |
312
|
|
|
unset($myrow); |
313
|
|
|
} |
314
|
|
|
$this->user_ids = array_unique($this->user_ids); |
315
|
|
|
|
316
|
|
|
return $ret; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param $uids |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
|
|
public function checkStatus($uids) |
324
|
|
|
{ |
325
|
|
|
$online_users = []; |
326
|
|
|
$ret = []; |
327
|
|
|
if (!empty($this->user_ids)) { |
328
|
|
|
$online_users = $this->user_ids; |
329
|
|
|
} else { |
330
|
|
|
$sql = 'SELECT online_uid FROM ' . $this->db->prefix('newbb_online'); |
331
|
|
|
if (!empty($uids)) { |
332
|
|
|
$sql .= ' WHERE online_uid IN (' . implode(', ', array_map('intval', $uids)) . ')'; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$result = $this->db->query($sql); |
336
|
|
|
if (!$result) { |
337
|
|
|
return $ret; |
338
|
|
|
} |
339
|
|
|
while (list($uid) = $this->db->fetchRow($result)) { |
340
|
|
|
$online_users[] = $uid; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
foreach ($uids as $uid) { |
344
|
|
|
if (in_array($uid, $online_users)) { |
345
|
|
|
$ret[$uid] = 1; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return $ret; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Count the number of online users |
354
|
|
|
* |
355
|
|
|
* @param CriteriaElement $criteria {@link CriteriaElement} |
|
|
|
|
356
|
|
|
* @return bool |
357
|
|
|
*/ |
358
|
|
|
public function getCount(CriteriaElement $criteria = null) |
359
|
|
|
{ |
360
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online'); |
361
|
|
|
if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) { |
|
|
|
|
362
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
363
|
|
|
} |
364
|
|
|
if (!$result = $this->db->query($sql)) { |
365
|
|
|
return false; |
366
|
|
|
} |
367
|
|
|
list($ret) = $this->db->fetchRow($result); |
368
|
|
|
|
369
|
|
|
return $ret; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.