-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
263 lines (213 loc) · 6.55 KB
/
Copy pathdb.php
File metadata and controls
263 lines (213 loc) · 6.55 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
require_once "env.php";
Class f2poolbot_db {
private $db;
private $logkey = "log_count";
private $lastaccessedkey = "last_accessed";
private $previouslastaccessedkey = "previous_last_accessed";
private $KEYS = [
"batchSwitch" => "batchSwitch",
];
/*
private $LOCAL_ENV = [
"DEFAULT_BATCHRUNINTERVAL" => 7200, // 2 hours
"MIN_BATCHRUNINTERVAL" => 3600, // 1 hour
"MAX_BATCHRUNINTERVAL" => 86400, // 24 hours
];
*/
public function __construct() {
global $ENV;
$this->db = new Redis();
try {
$this->db->connect("localhost");
$this->db->select($ENV["REDIS_DB"]);
} catch( Exception $e ){
throw $e;
}
}
public function turnBatchSwitchOn() {
$this->set($this->KEYS["batchSwitch"], 1);
}
public function turnBatchSwitchOff() {
$this->set($this->KEYS["batchSwitch"], 0);
}
public function isBatchSwitchOn() {
return $this->get($this->KEYS["batchSwitch"]) == 1;
}
public function exists($key) {
return $this->db->exists($key);
}
public function set($key, $value) {
return $this->db->set($key, $value);
}
public function get($key) {
if ($this->exists($key)) {
return $this->db->get($key);
} else {
return "";
}
}
public function incr($key) {
return $this->db->incr($key);
}
public function addLog() {
$ts = date('Y/m/d H:i:s');
if ($this->exists($this->logkey)) {
$this->incr($this->logkey);
} else {
$this->set($this->logkey, 1);
}
if (!$this->exists($this->lastaccessedkey)) {
$this->set($this->lastaccessedkey,$ts);
}
$this->set($this->previouslastaccessedkey, $this->get($this->lastaccessedkey));
$this->set($this->lastaccessedkey, $ts);
}
public function getLogCount() {
return $this->get($this->logkey);
}
public function getLastAccessed() {
return $this->get($this->lastaccessedkey);
}
public function getPrevLastAccessed() {
return $this->get($this->previouslastaccessedkey);
}
public function setTelegramUsername($uid, $uname) {
$this->db->set("username_for_userid_" . $uid, $uname);
}
public function getTelegramUsername($uid) {
return $this->get("username_for_userid_" . $uid);
}
public function isAutoMonitorModeSet($uid) {
$this->db->exists("automonitormode_for_userid_" . $uid);
}
public function isAutoMonitorModeOn($uid) {
return $this->getAutoMonitorMode($uid) == 1;
}
public function setAutoMonitorModeOn($uid) {
$this->set("automonitormode_for_userid_" . $uid, 1);
}
public function setAutoMonitorModeOff($uid) {
$this->set("automonitormode_for_userid_" . $uid, 0);
}
public function getAutoMonitorMode($uid) {
return $this->get("automonitormode_for_userid_" . $uid);
}
public function getAutoMonitorModeOnUserIds() {
$result = Array();
$keys = $this->db->keys("automonitormode_for_userid_*");
foreach ($keys as $key) {
if ($this->get($key)) $result[] = substr($key, strlen("automonitormode_for_userid_"));
}
return $result;
}
public function isF2UsernameSet($uid) {
return $this->exists("f2username_for_userid_" . $uid);
}
public function setF2Username($uid, $f2_uname) {
$this->db->set("f2username_for_userid_" . $uid, $f2_uname);
}
public function getF2Username($uid) {
return $this->get("f2username_for_userid_" . $uid);
}
public function setBatchRunInterval($uid, $i) {
global $LOCAL_ENV;
if ($i == "" ||
$i == 0 ||
$i < $LOCAL_ENV["MIN_BATCHRUNINTERVAL"] ||
$i > $LOCAL_ENV["MAX_BATCHRUNINTERVAL"]
) {
$i = $LOCAL_ENV["DEFAULT_BATCHRUNINTERVAL"];
}
$this->set("automonitor_interval_for_userid_" . $uid, $i);
}
public function getBatchRunInterval($uid) {
return $this->get("automonitor_interval_for_userid_" . $uid);
}
public function isBatchRunIntervalSet($uid) {
return $this->getBatchRunInterval($uid) != "";
}
public function setNextBatchRunTime($uid) {
$this->set("nextbatchruntime_for_userid_" . $uid, time() + $this->getBatchRunInterval($uid));
}
public function getNextBatchRunTime($uid) {
return $this->get("nextbatchruntime_for_userid_" . $uid);
}
public function getNextBatchRunTimeDate($uid) {
// TYPE 1
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp($this->getNextBatchRunTime($uid));
//return $date->format('Y-m-d H:i') . " UTC";
// TYPE 2
$diff = $this->getNextBatchRunTime($uid) - time();
$m = floor($diff/(60*60)) . " hour(s) " . floor(($diff % (60*60))/60) . " minute(s)";
return $m;
}
public function setChatId($uid, $chatId) {
$this->set("chatid_for_userid_" . $uid, $chatId);
}
public function getChatId($uid) {
return $this->get("chatid_for_userid_" . $uid);
}
public function setOfflineAlertOn($uid) {
$this->set("offlinealertflag_for_userid_" . $uid, 1);
}
public function setOfflineAlertOff($uid) {
$this->set("offlinealertflag_for_userid_" . $uid, 0);
}
public function isOfflineAlertOn($uid) {
return $this->get("offlinealertflag_for_userid_" . $uid) == 1;
}
public function getOfflineAlertOnUserIds() {
$result = Array();
$keys = $this->db->keys("offlinealertflag_for_userid_*");
foreach ($keys as $key) {
if ($this->get($key)) $result[] = substr($key, strlen("offlinealertflag_for_userid_"));
}
return $result;
}
public function isDebugModeOn() {
return $this->get("debugmodeflag");
}
public function setDebugModeOn() {
$this->set("debugmodeflag", 1);
}
public function setDebugModeOff() {
$this->set("debugmodeflag", 0);
}
public function setBatchRunningStatusOff(){
$this->set("batchRunning", 0);
}
public function setBatchRunningStatusOn() {
$this->set("batchRunning", 1);
}
public function isBatchRunning() {
return $this->get("batchRunning") == 1;
}
public function setShowStatModeOn() {
$this->set("showstatmode", 1);
}
public function setShowStatModeOff() {
$this->set("showstatmode", 0);
}
public function isShowStatMode() {
return $this->get("showstatmode") == 1;
}
// ==============================================================
// ADMIN
// ==============================================================
public function getTelegramUsernames() {
$result = Array();
$keys = $this->db->keys("username_for_userid_*");
foreach ($keys as $key) {
if ($this->get($key)) $result[] = substr($key, strlen("username_for_userid_"));
}
return $result;
}
public function getNumOfTelegramUsernames() {
$usernames = $this->getTelegramUsernames();
return count($usernames);
}
}
?>