forked from BobCoderS9/SSPanel-Metron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_job.php
172 lines (161 loc) · 5.49 KB
/
clean_job.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
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
#!/usr/bin/env php
<?php
declare(strict_types=1);
use App\Services\Boot;
require __DIR__ . '/../app/predefine.php';
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../config/.config.php';
Boot::setTime();
Boot::bootDb();
$processed = [];
$renew = [];
$renew_c = function ($ids) use ($processed) {
echo 'Renew Process START.';
foreach ($ids as $id) {
$bought = \App\Models\Bought::find($id);
if ($bought == null) {
echo 'Bought not found:' . $id . PHP_EOL;
unlink(__DIR__ . '/../storage/' . $id . '.renew');
$processed['renew'] = $id;
} else {
$bought->is_notified = true;
if ($bought->save() == true) {
unlink(__DIR__ . '/../storage/' . $id . '.renew');
echo 'Renew Process successed for bought' . $id . PHP_EOL;
$processed['renew'] = $id;
}
}
}
echo 'Renew Process END.' . PHP_EOL . PHP_EOL;
};
$offline = [];
$offline_c = function ($ids) use ($processed) {
echo 'Offline Process START.';
foreach ($ids as $id) {
$node = \App\Models\Node::find($id);
if ($node == null) {
echo 'Node not found:' . $id . PHP_EOL;
unlink(__DIR__ . '/../storage/' . $id . '.offline');
$processed['offline'] = $id;
} else {
$node->online = false;
if ($node->save() == true) {
unlink(__DIR__ . '/../storage/' . $id . '.offline');
echo 'Offline Process successed for node' . $id . PHP_EOL;
$processed['offline'] = $id;
}
}
}
echo 'Offline Process END.' . PHP_EOL . PHP_EOL;
};
$expire = [];
$expire_c = function ($ids) use ($processed) {
echo 'Expire Process START.';
foreach ($ids as $id) {
$user = \App\Models\User::find($id);
if ($user == null) {
echo 'User not found:' . $id . PHP_EOL;
unlink(__DIR__ . '/../storage/' . $id . '.expire_in');
$processed['expire'] = $id;
} else {
$user->expire_notified = true;
if ($user->save() == true) {
unlink(__DIR__ . '/../storage/' . $id . '.expire_in');
echo 'Expire Process successed for user' . $id . PHP_EOL;
$processed['expire'] = $id;
}
}
}
echo 'Expire Process END.' . PHP_EOL . PHP_EOL;
};
$gfw = [];
$gfw_c = function ($ids) use ($processed) {
echo 'GFW Process START.';
foreach ($ids as $id) {
$node = \App\Models\Node::find($id);
if ($node == null) {
echo 'Node not found:' . $id . PHP_EOL;
unlink(__DIR__ . '/../storage/' . $id . '.gfw');
$processed['gfw'] = $id;
} else {
$node->gfw_block = true;
if ($node->save() == true) {
unlink(__DIR__ . '/../storage/' . $id . '.gfw');
echo 'GFW Process successed for node' . $id . PHP_EOL;
$processed['gfw'] = $id;
}
}
}
echo 'GFW Process END.' . PHP_EOL . PHP_EOL;
};
$files = scandir(__DIR__ . '/../storage');
foreach ($files as $origin_file) {
$file = explode('.', $origin_file);
if (count($file) == 2 && is_numeric($file[0])) {
switch ($file[1]) {
case 'renew':
$renew[] = $file[0];
break;
case 'offline':
$offline[] = $file[0];
break;
case 'expire_in':
$expire[] = $file[0];
break;
case 'gfw':
$gfw[] = $file[0];
break;
default:
echo 'Unrecognized file: ' . $origin_file . PHP_EOL;
}
} else {
continue;
}
}
$renew_c($renew);
$offline_c($offline);
$expire_c($expire);
$gfw_c($gfw);
if (file_exists(__DIR__ . '/../storage/traffic_notified') == true) {
$files = scandir(__DIR__ . '/../storage/traffic_notified');
if ($files != false) {
foreach ($files as $origin_file) {
$file = explode('.', $origin_file);
if (count($file) == 2 && is_numeric($file[0] && $file[1] == 'userid')) {
$notified[] = $file[0];
} else {
echo 'Unrecognized file: ' . $origin_file . PHP_EOL;
}
}
file_put_contents(__DIR__ . '/notified.json', json_encode($file));
}
$notified_c = function ($ids) use ($processed) {
echo 'Notified Process START.';
foreach ($ids as $id) {
$user = \App\Models\User::find($id);
if ($user == null) {
echo 'User not found:' . $id . PHP_EOL;
unlink(__DIR__ . '/../storage/traffic_notified/' . $id . '.userid');
$processed['notified'] = $id;
} else {
$user->traffic_notified = true;
if ($user->save() == true) {
unlink(__DIR__ . '/../storage/traffic_notified/' . $id . '.userid');
echo 'Notified Process successed for node' . $id . PHP_EOL;
$processed['notified'] = $id;
}
}
}
echo 'Notified Process END.' . PHP_EOL . PHP_EOL;
};
} else {
echo 'Notified Process Nothing to do.' . PHP_EOL . PHP_EOL;
}
file_put_contents(__DIR__ . '/processed.json', json_encode($processed));
file_put_contents(__DIR__ . '/raw.json', json_encode([
'renew' => $renew,
'offline' => $offline,
'expire' => $expire,
'gfw' => $gfw,
]));
exit(0);