-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.php
executable file
·102 lines (90 loc) · 2 KB
/
users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* add new user in users.json
* @return string
*
*/
function addUser($id, $obj) {
if(isset($_SESSION['user']) && empty($id)) return $_SESSION['user'];
$users = getJsonFromFile('users.json');
$newUser = null;
if (isset($id)) {
if($users === null) $users = new stdClass();
$users->lastId = (int) substr($id, 6);
$users->count += 1;
$users->$id = $obj;
}
else {
$users->lastId = $users->lastId === null ? 0 : $users->lastId+1;
$users->count += 1;
$newUser = 'guest_'.$users->lastId;
$_SESSION['user'] = $newUser;
$_SESSION['status'] = 0;
$_SESSION['chat'] = null;
$users->$newUser = getDefaultData();
}
setJsonToFile('users.json', $users);
return $newUser;
}
/**
* drop user from users.json
* @param array &$users
* @param string $user
* @return array
*
*/
function dropUser(&$users, $user) {
if (empty($users) || empty($users->$user)) return false;
$users->count -= 1;
unset($users->$user);
return $users;
}
/**
* get user online
* @return bool
*
*/
function isUserOnline($arr, $user) {
return $arr->$user->online;
}
/**
* drops users from users.json by timeout
* @param int $timeout
* @return execute record to file
*
*/
function dropUsersBySession($timeout) {
$users = getJsonFromFile('users.json');
foreach($users as $id => $user) {
if($id[0] == 'g')
if( (time() - $user->visited) > $timeout && $user->online == false)
dropUser($users, $id);
}
return setJsonToFile('users.json', $users);
}
/**
* set new value by key, example: updateUserData($users, 'visited', time());
* @param array &$arr
* @param string $key
* @param string $value
* @return array
*
*/
function updateUserData(&$arr, $key, $value) {
$arr->$key = $value;
return $arr;
}
/**
* get object new user with default stats
* @return object
*
*/
function getDefaultData() {
$obj = null;
$obj->mmr = 1;
$obj->chat = null;
$obj->online = true;
$obj->visited = time();
return $obj;
}
?>