1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cApiOnlineUserCollection extends ItemCollection {
25:
26: 27: 28: 29: 30:
31: public function __construct($select = false) {
32: global $cfg;
33: parent::__construct($cfg['tab']['online_user'], 'user_id');
34: $this->_setItemClass('cApiOnlineUser');
35: if ($select !== false) {
36: $this->select($select);
37: }
38: }
39:
40: 41: 42: 43: 44: 45: 46: 47:
48: public function startUsersTracking($userId = null) {
49: global $auth;
50:
51: $userId = (string) $userId;
52: if (empty($userId)) {
53: $userId = $auth->auth['uid'];
54: }
55:
56:
57: $this->deleteInactiveUser();
58:
59: $bResult = $this->findUser($userId);
60: if ($bResult) {
61:
62: $this->updateUser($userId);
63: } else {
64:
65: $this->insertOnlineUser($userId);
66: }
67: }
68:
69: 70: 71: 72: 73: 74:
75: public function insertOnlineUser($userId) {
76: $oItem = parent::createNewItem((string) $userId);
77: if ($oItem) {
78: $created = date('Y-m-d H:i:s');
79: $oItem->set('lastaccessed', $created);
80: $oItem->store();
81: }
82: return ($oItem)? true : false;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function findUser($userId) {
92: $oUser = new cApiOnlineUser((string) $userId);
93: return ($oUser->isLoaded());
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function findAllUser() {
104:
105: $aAllUser = array();
106: $aUser = array();
107: $sClientName = '';
108:
109:
110: $this->select();
111: while (($oItem = $this->next()) !== false) {
112: $aUser[] = $oItem->get('user_id');
113: }
114:
115: $oClientColl = new cApiClientCollection();
116:
117:
118: $where = "user_id IN ('" . implode("', '", $aUser) . "')";
119: $oUserColl = new cApiUserCollection();
120: $oUserColl->select($where);
121: while (($oItem = $oUserColl->next()) !== false) {
122: $sClientNames = '';
123: $userId = $oItem->get('user_id');
124: $aAllUser[$userId]['realname'] = $oItem->get('realname');
125: $aAllUser[$userId]['username'] = $oItem->get('username');
126: $aPerms = explode(',', $oItem->get('perms'));
127:
128: if (in_array('sysadmin', $aPerms)) {
129: $aAllUser[$userId]['perms'] = 'Systemadministrator';
130: } else {
131: $bIsAdmin = false;
132: $iCounter = 0;
133: foreach ($aPerms as $sPerm) {
134: $aResults = array();
135: if (preg_match('/^admin\[(\d+)\]$/', $sPerm, $aResults)) {
136: $iClientId = $aResults[1];
137: $bIsAdmin = true;
138: $sClientName = $oClientColl->getClientname((int) $iClientId);
139: if ($iCounter == 0 && $sClientName != '') {
140: $sClientNames .= $sClientName;
141: } elseif ($sClientName != '') {
142: $sClientNames .= ', ' . $sClientName;
143: }
144:
145: $aAllUser[$userId]['perms'] = 'Administrator (' . $sClientNames . ')';
146: $iCounter++;
147: } elseif (preg_match('/^client\[(\d+)\]$/', $sPerm, $aResults) && !$bIsAdmin) {
148: $iClientId = $aResults[1];
149: $sClientName = $oClientColl->getClientname((int) $iClientId);
150: if ($iCounter == 0 && $sClientName != '') {
151: $sClientNames .= $sClientName;
152: } elseif ($sClientName != '') {
153: $sClientNames .= ', ' . $sClientName;
154: }
155:
156: $aAllUser[$userId]['perms'] = '(' . $sClientNames . ')';
157: $iCounter++;
158: }
159: }
160: }
161: }
162:
163: return $aAllUser;
164: }
165:
166: 167: 168: 169: 170: 171:
172: public function updateUser($userId) {
173: $oUser = new cApiOnlineUser((string) $userId);
174: if ($oUser->isLoaded()) {
175: $now = date('Y-m-d H:i:s');
176: $oUser->set('lastaccessed', $now);
177: return $oUser->store();
178: }
179: return false;
180: }
181:
182: 183: 184: 185: 186: 187:
188: public function deleteInactiveUser() {
189: global $cfg;
190: include_once ($cfg['path']['contenido_config'] . 'config.misc.php');
191: $iSetTimeOut = (int) $cfg['backend']['timeout'];
192: if ($iSetTimeOut <= 0) {
193: $iSetTimeOut = 10;
194: }
195:
196:
197:
198:
199: $where = "DATE_SUB(NOW(), INTERVAL '$iSetTimeOut' Minute) >= `lastaccessed`";
200: $result = $this->deleteByWhereClause($where);
201: return ($result > 0)? true : false;
202: }
203:
204: 205: 206: 207: 208:
209: public function getNumberOfUsers() {
210: $sql = 'SELECT COUNT(*) AS cnt FROM `%s`';
211: $result = $this->db->query($sql, $this->table);
212: $this->_lastSQL = $sql;
213: if ($result) {
214: $this->db->nextRecord();
215: return (int) $this->db->f('cnt');
216: }
217: return 0;
218: }
219:
220: 221: 222: 223: 224: 225:
226: public function deleteUser($userId) {
227: return $this->delete((string) $userId);
228: }
229: }
230:
231: 232: 233: 234: 235: 236:
237: class cApiOnlineUser extends Item {
238:
239: 240: 241: 242: 243:
244: public function __construct($mId = false) {
245: global $cfg;
246: parent::__construct($cfg['tab']['online_user'], 'user_id');
247: $this->setFilters(array(), array());
248: if ($mId !== false) {
249: $this->loadByPrimaryKey($mId);
250: }
251: }
252:
253: }
254: