Repository navigation
Expand file tree
/
Copy pathwebhooks.php
More file actions
198 lines (187 loc) · 8.21 KB
/
Copy pathwebhooks.php
File metadata and controls
198 lines (187 loc) · 8.21 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
<?php
if (!defined('REFACTORED_LEGACY_ROOT')) {
define('REFACTORED_LEGACY_ROOT', __DIR__);
}
@chdir(__DIR__);
require_once 'config.php';
require_once 'botapi.php';
require_once 'panels.php';
require_once 'function.php';
$ManagePanel = new ManagePanel();
$headers = getallheaders();
$webhook_secret = isset($headers['X-Webhook-Secret']) ? $headers['X-Webhook-Secret'] : '';
$reportcron = select("topicid","idreport","report","reportcron","select")['idreport'];
$textservice = select("textbot","text","id_text","text_Purchased_services","select")['text'];
$setting = select("setting", "*");
$decodedSecret = base64_decode($webhook_secret, true);
if (!is_string($decodedSecret) || strlen($decodedSecret) < 8) {
if (function_exists('rx_log_event')) {
rx_log_event('WEBHOOK_REJECT', 'Webhook secret missing or too short', [
'remote_ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
'has_secret' => $webhook_secret !== '' ? 'yes' : 'no',
]);
} else {
error_log('[webhook] rejected: missing/short secret');
}
http_response_code(401);
return;
}
$secret_key = select("admin", "*", "password", $decodedSecret, "count");
if ($secret_key == 0) {
if (function_exists('rx_log_event')) {
rx_log_event('WEBHOOK_AUTH_FAIL', 'Webhook secret did not match any admin row', [
'remote_ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
]);
} else {
error_log('[webhook] auth failed');
}
http_response_code(401);
return;
}
$rawWebhookBody = file_get_contents("php://input");
$decodedBody = json_decode($rawWebhookBody, true);
if (!is_array($decodedBody) || !isset($decodedBody[0]) || !is_array($decodedBody[0])) {
if (function_exists('rx_log_event')) {
rx_log_event('WEBHOOK_BAD_BODY', 'Webhook body was not the expected [object] shape', [
'remote_ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
]);
}
http_response_code(400);
return;
}
$data = $decodedBody[0];
if (!isset($data['action']) || !is_string($data['action'])) {
http_response_code(400);
return;
}
if($data['action'] == "reached_usage_percent"){
$line = $data['username'];
$invoice = select("invoice","*","username",$line,"select");
if($invoice == false)return;
if($invoice['name_product'] == "سرویس تست")return;
$user = select("user","*","id",$invoice['id_user'],"select");
$data = $data['user'];
$output = $data['data_limit'] - $data['used_traffic'];
$RemainingVolume = formatBytes($output);
$data_limit = formatBytes($data['data_limit']);
$Response = json_encode([
'inline_keyboard' => [
[
['text' => "💊 تمدید سرویس", 'callback_data' => 'extend_' . $invoice['id_invoice']],
],
]
]);
$text = "با سلام خدمت شما کاربر گرامی 👋
🚨 از حجم سرویس $line تنها $RemainingVolume باقی مانده است. لطفاً در صورت تمایل برای تمدید سرویستون از طریق بخش «{$textservice}» اقدام بفرمایین";
if(intval($user['status_cron']) != 0){
sendmessage($invoice['id_user'], $text, $Response, 'HTML');
}
$text_report = "📌 اطلاعیه کرون حجم
<blockquote>نام کاربری سرویس : <code>$line</code></blockquote>
<blockquote>آیدی عددی کاربر : <code>{$invoice['id_user']}</code></blockquote>
<blockquote>وضعیت سرویس : {$data['status']}</blockquote>
<blockquote>حجم باقی مانده : $RemainingVolume</blockquote>
<blockquote>حجم کل سرویس : $data_limit</blockquote>";
if (strlen($setting['Channel_Report']) > 0) {
telegram('sendmessage',[
'chat_id' => $setting['Channel_Report'],
'message_thread_id' => $reportcron,
'text' => $text_report,
'parse_mode' => "HTML"
]);
}
if($invoice['Status'] === "end_of_volume"){
update("invoice","Status","sendedwarn", "username",$invoice['username']);
}else{
update("invoice","Status","end_of_volume", "username",$invoice['username']);
}
}
elseif ($data['action'] == "reached_days_left"){
$line = $data['username'];
$invoice = select("invoice","*","username",$line,"select");
if($invoice == false)return;
if($invoice['name_product'] == "سرویس تست")return;
$user = select("user","*","id",$invoice['id_user'],"select");
$data = $data['user'];
$timeservice = $data['expire'] - time();
$day = intval($timeservice / 86400);
if($day <=0){
$day = intval($timeservice / 3600) . "ساعت";
}else{
$day = $day. "روز";
}
$Response = json_encode([
'inline_keyboard' => [
[
['text' => "💊 تمدید سرویس", 'callback_data' => 'extend_' . $invoice['id_invoice']],
],
]
]);
$text = "با سلام خدمت شما کاربر گرامی 👋
📌 از مهلت زمانی استفاده از سرویس {$invoice['username']} فقط $day باقی مانده است. لطفاً در صورت تمایل برای تمدید این سرویس، از طریق بخش «{$textservice}» اقدام بفرمایین. با تشکر از همراهی شما";
if(intval($user['status_cron']) != 0){
sendmessage($invoice['id_user'], $text, $Response, 'HTML');
}
$text_report = "📌 اطلاعیه کرون زمان
<blockquote>نام کاربری سرویس : <code>{$data['username']}</code></blockquote>
<blockquote>آیدی عددی کاربر : <code>{$invoice['id_user']}</code></blockquote>
<blockquote>وضعیت سرویس : {$data['status']}</blockquote>
<blockquote>تعداد روز باقی مانده :$day</blockquote>";
if (strlen($setting['Channel_Report']) > 0) {
telegram('sendmessage',[
'chat_id' => $setting['Channel_Report'],
'message_thread_id' => $reportcron,
'text' => $text_report,
'parse_mode' => "HTML"
]);
}
if($invoice['Status'] === "end_of_volume"){
update("invoice","Status","sendedwarn", "username",$invoice['username']);
}else{
update("invoice","Status","end_of_time", "username",$invoice['username']);
}
}
elseif(in_array($data['action'],["user_expired","user_limited"])){
$line = $data['username'];
$invoice = select("invoice","*","username",$line,"select");
if($invoice == false)return;
if($invoice['name_product'] == "سرویس تست")return;
$panel = select("marzban_panel","*","name_panel",$invoice['Service_location'],"select");
$actionName = $data['action'];
$data = $data['user'];
if($actionName === "user_expired"){
update("invoice","Status","end_of_time","username",$line);
}elseif($actionName === "user_limited"){
update("invoice","Status","end_of_volume","username",$line);
}
try {
update("invoice","user_info",json_encode($data, JSON_UNESCAPED_UNICODE),"username",$line);
} catch (Throwable $e) {
}
if($panel['inboundstatus'] == "oninbounddisable"){
if($data['data_limit_reset_strategy'] == "no_reset"){
$inbound = explode("*", $panel['inbound_deactive']);
$proxies = [];
$proxies[$inbound[0]] = new stdClass();;
$inbounds[$inbound[0]][] = $inbound[1];
$configs = array(
"proxies" => $proxies,
"inbounds" => $inbounds
);
$modifyResult = $ManagePanel->Modifyuser($line,$panel['name_panel'],$configs);
if (!$modifyResult['status']) {
if (function_exists('rx_log_event')) {
rx_log_event('WEBHOOK_MODIFYUSER_FAIL', 'Modifyuser call failed, invoice uuid not updated', [
'username' => $line,
'name_panel' => $panel['name_panel'],
'msg' => $modifyResult['msg'] ?? '',
]);
} else {
error_log('[webhook] Modifyuser failed for ' . $line . ': ' . ($modifyResult['msg'] ?? ''));
}
} else {
update("invoice","uuid",json_encode($data['proxies']), "username",$line);
}
}
}
}