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