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