-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHome.php
More file actions
298 lines (245 loc) · 10.7 KB
/
Home.php
File metadata and controls
298 lines (245 loc) · 10.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class Home extends BaseController {
function __construct(){
helper('form'); // Load the form helper
}
public function index(): string {
// Pass the data to the view
return view('home');
}
// Check account status
private function check_email_exists($email){
// Connect to the database
$db = \Config\Database::connect();
$builder = $db->table('inbounds');
// Use query binding to prevent SQL injection
$query = $builder->select('settings')->get();
$results = $query->getResult();
if ($results) {
foreach ($results as $result) {
// Decode the JSON string in the 'settings' field
$settings = json_decode($result->settings);
// Ensure the JSON decoding was successful
if ($settings && isset($settings->clients)) {
foreach ($settings->clients as $client) {
if ($client->email == $email && $client->enable == true) {
return 1;
}
}
}
}
}
// Return 0 if no matching client found
return 0;
}
// Get CPU usage percentage
private function getCpuUsage() {
// Execute shell command to get CPU usage
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
// Windows command
$cpuUsage = shell_exec("wmic cpu get loadpercentage");
$cpuUsage = (float) trim($cpuUsage); // Convert to float and remove any extra spaces or newlines
} else {
// Unix/Linux command
$cpuUsage = sys_getloadavg()[0]; // Get the 1-minute load average
}
return number_format($cpuUsage, 2); // Round to two decimals
}
// Get RAM usage percentage
private function getRamUsage(){
$ramUsagePercent = 0;
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
// Windows command
$ramUsage = shell_exec("wmic os get freephysicalmemory /value");
} else {
// Linux/Unix command
$free = shell_exec("free -m");
$freeArr = explode("\n", $free);
$memory = explode(" ", preg_replace('/\s+/', ' ', $freeArr[1]));
$ramUsage = $memory[2] . 'MB used out of ' . $memory[1] . 'MB';
$ramUsagePercent = ($memory[2]/$memory[1])*100;
}
return [$ramUsage,$ramUsagePercent];
}
// Get disk space usage
private function getDiskSpace(){
// Execute shell command to get disk usage
$diskSpace = disk_free_space("/");
$totalSpace = disk_total_space("/");
$usedSpace = $totalSpace - $diskSpace;
return round(($usedSpace / $totalSpace) * 100, 2) . '% used';
}
// Get the server's overall status (could be more detailed in the future)
private function getServerStatus(){
return 'Online'; // Changed from 'Healthy' to match frontend display
}
// Check for uuid
private function isUuid($input) {
// Regular expression for validating UUID (v4)
return preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', $input);
}
// Get data usage for user
public function getusage(){
$data = new \stdClass(); // Use the global namespace explicitly
$data->success = FALSE;
$data->error = FALSE;
$data->error_msg = "";
$data->token = csrf_hash();
$post_data = $this->request->getPost(); // Using the correct CodeIgniter method to get POST data
// return $this->response->setJSON($data);
// Get the email from the GET request
$email = $post_data['email'];
$email = preg_replace('/^\s+|\s+$/u', '', $email);
// Validate if email is provided
if (!$email) {
$data->error = TRUE;
$data->error_msg = "User parameter is missing.";
return $this->response->setJSON($data);
}
$uuid = '';
$username = '';
// check input type (uuid or username)
if ($this->isUuid($email)) {
$uuid = $email;
}else{
$username = $email;
}
// Connect to the database
$db = \Config\Database::connect();
// Path to the panel.txt file
$get_panel_type = FCPATH . 'panel.txt'; // FCPATH points to the root directory of your CodeIgniter project
// Check if the panel type file exists
if (file_exists($get_panel_type)) {
// Read the file contents
// 1 = 3x-ui, 2 = marzban, 3 = hidify
$panel_number = file_get_contents($get_panel_type);
// 3x-ui
if($panel_number == 1){
// uuid
if($uuid != ''){
$builder = $db->table('inbounds');
// Use query binding to prevent SQL injection
$query = $builder->select('settings')->get();
$results = $query->getResult();
if ($results) {
foreach ($results as $result) {
// Decode the JSON string in the 'settings' field
$settings = json_decode($result->settings);
// Ensure the JSON decoding was successful
if ($settings && isset($settings->clients)) {
foreach ($settings->clients as $client) {
if ($client->id == $uuid) {
$username = $client->email;
$uuid = $client->id;
break 2; // Exit both loops when the match is found
}
}
}else{
// No data found for the given email
$data->error = TRUE;
$data->error_msg = "No data found for the given user.";
return $this->response->setJSON($data);
}
}
}else{
// No data found for the given email
$data->error = TRUE;
$data->error_msg = "No data found for the given user.";
return $this->response->setJSON($data);
}
}
$builder = $db->table('client_traffics');
// Use query binding to prevent SQL injection
$query = $builder->select('enable, email, up, down, expiry_time, total')
->where('email', $username)
->get();
}elseif ($panel_number == 2) {
$builder = $db->table('users');
// Use query binding to prevent SQL injection
$query = $builder->select('username, used_traffic, expire, data_limit')
->where('username', $username)
->get();
}
} else {
$data->error = TRUE;
$data->error_msg = "System error.";
return $this->response->setJSON($data);
}
// Get the result
$result = $query->getRow();
if ($result) {
// Data found
$enable = $result->enable == 0 ? 0 : $this->check_email_exists($username);
$email = $result->email;
$up = $result->up;
$down = $result->down;
$expiry_time = $result->expiry_time;
$total = $result->total;
// Convert bytes to GB
$up_gb = $up / 1073741824;
$down_gb = $down / 1073741824;
$total_gb = $total / 1073741824;
// Format the storage sizes
function formatStorage($size) {
if ($size >= 1000) {
$size_in_tb = $size / 1000;
return number_format($size_in_tb, 2) . ' TB';
} else {
return number_format($size, 2) . ' GB';
}
}
$formatted_up = formatStorage($up_gb);
$formatted_down = formatStorage($down_gb);
$formatted_total = formatStorage($total_gb);
$total_up_down = $up + $down;
$total_up_down_gb = $total_up_down / 1073741824;
$formatted_total_up_down = formatStorage($total_up_down_gb);
$percentage = 0; // Default to 0
// Check if total is not zero to avoid division by zero
if ($total > 0) {
$percentage = ($total_up_down / $total) * 100;
}
// Calculate the remaining time
if($expiry_time != 0){
$expiry_time_seconds = $expiry_time / 1000;
$expiry_date = new \DateTime();
$expiry_date->setTimestamp($expiry_time_seconds);
$current_date = new \DateTime();
$interval = $current_date->diff($expiry_date);
$remaining_days = $interval->format('%r%a');
$remaining_days = $remaining_days <= 0 ? 0 : $remaining_days;
}else{
$remaining_days = "∞";
}
$ram_usage = $this->getRamUsage();
// success data
$data->success = TRUE;
$data->enable = $enable;
$data->email = $email;
$data->uuid = $uuid;
$data->total = $total_gb == 0 ? "∞ GB" : $formatted_total;
$data->total_up_down = $formatted_total_up_down;
$data->percentage = number_format($percentage, 2);
$data->remaining_days = $remaining_days;
$data->uptime = shell_exec('uptime -p'); // Example output: "up 5 days, 3 hours"
$data->cpu_usage = $this->getCpuUsage();
$data->ram_usage = $ram_usage[0];
$data->ram_usage_percent = number_format((float)$ram_usage[1], 2, '.', '');
$data->disk_space = $this->getDiskSpace();
$data->server_status = $this->getServerStatus();
$data->upload_data = $formatted_up;
$data->download_data = $formatted_down;
$data->upload_bytes = $up;
$data->download_bytes = $down;
$data->total_bytes = $total;
return $this->response->setJSON($data);
} else {
// No data found for the given email
$data->error = TRUE;
$data->error_msg = "No data found for the given user.";
return $this->response->setJSON($data);
}
}
}