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 XOOPS Project (http://xoops.org)
13: * @license GNU GPL 2 or later (http://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: * @version $Id$
18: */
19:
20: namespace Xoops\Core\Kernel\Handlers;
21:
22: use Xoops\Core\Database\Connection;
23: use Xoops\Core\Kernel\Criteria;
24: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
25:
26: /**
27: * A handler for "Who is Online?" information
28: *
29: * @category Xoops\Core\Kernel\Handlers\XoopsOnlineHandler
30: * @package Xoops\Core\Kernel
31: * @author Kazumi Ono <onokazu@xoops.org>
32: * @copyright 2000-2015 XOOPS Project (http://xoops.org)
33: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
34: * @link http://xoops.org
35: */
36: class XoopsOnlineHandler extends XoopsPersistableObjectHandler
37: {
38: /**
39: * Constructor
40: *
41: * @param Connection|null $db database
42: */
43: public function __construct(Connection $db = null)
44: {
45: parent::__construct(
46: $db,
47: 'system_online',
48: '\Xoops\Core\Kernel\Handlers\XoopsOnline',
49: 'online_uid',
50: 'online_uname'
51: );
52: }
53:
54: /**
55: * Write online information to the database
56: *
57: * @param int $uid UID of the active user
58: * @param string $uname Username
59: * @param string $time time
60: * @param string $module Current module
61: * @param string $ip User's IP address
62: *
63: * @return bool TRUE on success
64: */
65: public function write($uid, $uname, $time, $module, $ip)
66: {
67: $criteria = array();
68: $criteria['online_uid'] = $uid;
69: if ($uid == 0) {
70: $criteria['online_ip'] = $ip;
71: }
72: $rows = $this->db2->updatePrefix(
73: 'system_online',
74: array(
75: 'online_uname' => $uname,
76: 'online_updated' => $time,
77: 'online_module' => $module,
78: ),
79: $criteria
80: );
81: if ($rows === false) {
82: return false;
83: }
84: if ($rows == 0) {
85: $rows = $this->db2->insertPrefix(
86: 'system_online',
87: array(
88: 'online_uid' => $uid,
89: 'online_uname' => $uname,
90: 'online_updated' => $time,
91: 'online_ip' => $ip,
92: 'online_module' => $module,
93: )
94: );
95: }
96: if ($rows === false) {
97: return false;
98: }
99: return ($rows>0);
100: }
101:
102: /**
103: * Delete online information for a user
104: *
105: * @param int $uid UID
106: *
107: * @return bool TRUE on success
108: */
109: public function destroy($uid)
110: {
111: $criteria = new Criteria('online_uid', (int)($uid));
112: if (false === $this->deleteAll($criteria)) {
113: return false;
114: }
115: return true;
116: }
117:
118: /**
119: * Garbage Collection
120: *
121: * Delete all online information that has not been updated for a certain time
122: *
123: * @param int $expire Expiration time in seconds
124: *
125: * @return bool
126: */
127: public function gc($expire)
128: {
129: $criteria = new Criteria('online_updated', time() - (int)($expire), '<');
130: if (false === $this->deleteAll($criteria)) {
131: return false;
132: }
133: return true;
134: }
135: }
136: