process_subscription()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 70
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 54
nc 11
nop 2
dl 0
loc 70
rs 7.7591
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
include "flag.php";
3
include "ipinfo.php";
4
include "shadowsocks.php";
5
include "vmess.php";
6
include "xray.php";
7
include "tuic.php";
8
include "hysteria2.php";
9
include "ping.php";
10
11
function numberToEmoji($number)
12
{
13
    $map = [
14
        "0" => "0️⃣",
15
        "1" => "1️⃣",
16
        "2" => "2️⃣",
17
        "3" => "3️⃣",
18
        "4" => "4️⃣",
19
        "5" => "5️⃣",
20
        "6" => "6️⃣",
21
        "7" => "7️⃣",
22
        "8" => "8️⃣",
23
        "9" => "9️⃣",
24
    ];
25
26
    $emoji = "";
27
    $digits = str_split($number);
28
29
    foreach ($digits as $digit) {
30
        if (count($digits) === 1) {
31
            $emoji = $map["0"];
32
        }
33
        if (isset($map[$digit])) {
34
            $emoji .= $map[$digit];
35
        }
36
    }
37
38
    return $emoji;
39
}
40
41
function openLink($url)
42
{
43
    $ch = curl_init();
44
    curl_setopt_array($ch, [
45
        CURLOPT_URL => $url,
46
        CURLOPT_RETURNTRANSFER => 1,
47
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
48
        CURLOPT_FOLLOWLOCATION => true,
49
    ]);
50
    return curl_exec($ch);
51
}
52
53
function convert_to_iran_time($utc_timestamp)
54
{
55
    $utc_datetime = new DateTime($utc_timestamp);
56
    $utc_datetime->setTimezone(new DateTimeZone("Asia/Tehran"));
57
    return $utc_datetime->format("Y-m-d H:i:s");
58
}
59
60
function get_config_time($type, $input)
61
{
62
    preg_match_all(
63
        "/" . $type . ':\/\/[^"]+(?:[^<]+<[^<]+)*<time datetime="([^"]+)"/',
64
        $input,
65
        $times
66
    );
67
    return $times;
68
}
69
70
function get_config_items($type, $input)
71
{
72
    preg_match_all("#>" . $type . "://(.*?)<#", $input, $items);
73
    return $items;
74
}
75
76
function is_valid($input)
77
{
78
    if (stripos($input, "…") !== false or stripos($input, "...") !== false) {
79
        return false;
80
    }
81
    return true;
82
}
83
84
function is_reality($input, $type)
85
{
86
    switch ($type) {
87
        case "vmess":
88
            return false;
89
        case "vless":
90
            if (stripos($input, "reality") !== false) {
91
                return true;
92
            } else {
93
                return false;
94
            }
95
        case "trojan":
96
            return false;
97
        case "tuic":
98
            return false;
99
        case "hy2":
100
            return false;
101
        case "ss":
102
            return false;
103
    }
104
}
105
106
function check_pbk($input)
107
{
108
    if (stripos($input, "pbk=&") !== false) {
109
        return false;
110
    } else {
111
        return true;
112
    }
113
}
114
115
function get_ip($input, $type, $is_reality)
116
{
117
    switch ($type) {
118
        case "vmess":
119
            return get_vmess_ip($input);
120
        case "vless":
121
            return get_vless_ip($input, $is_reality);
122
        case "trojan":
123
            return get_trojan_ip($input);
124
        case "ss":
125
            return get_ss_ip($input);
126
        case "tuic":
127
            return get_tuic_ip($input);
128
        case "hy2":
129
            return get_hy2_ip($input);
130
    }
131
}
132
133
function get_address($input, $type)
134
{
135
    switch ($type) {
136
        case "vmess":
137
            return $input["add"];
138
        case "vless":
139
        case "trojan":
140
            return $input["hostname"];
141
        case "tuic":
142
            return $input["hostname"];
143
        case "hy2":
144
            return $input["hostname"];
145
        case "ss":
146
            return $input["server_address"];
147
    }
148
}
149
150
function is_number_with_dots($s)
151
{
152
    /*
153
     * Returns true if the given string contains only digits and dots, and false otherwise.
154
     */
155
    for ($i = 0; $i < strlen($s); $i++) {
156
        $c = $s[$i];
157
        if (!ctype_digit($c) && $c != ".") {
158
            return false;
159
        }
160
    }
161
    return true;
162
}
163
164
function is_valid_address($address)
165
{
166
    $ipv4_pattern = '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/';
167
    $ipv6_pattern = '/^[0-9a-fA-F:]+$/'; // matches any valid IPv6 address
168
169
    if (
170
        preg_match($ipv4_pattern, $address) ||
171
        preg_match($ipv6_pattern, $address)
172
    ) {
173
        return true;
174
    } elseif (is_number_with_dots($address) === false) {
175
        if (
176
            substr($address, 0, 8) === "https://" ||
177
            substr($address, 0, 7) === "http://"
178
        ) {
179
            $url = filter_var($address, FILTER_VALIDATE_URL);
180
        } else {
181
            $url = filter_var("https://" . $address, FILTER_VALIDATE_URL);
182
        }
183
        if ($url !== false) {
184
            return true;
185
        } else {
186
            return false;
187
        }
188
    }
189
    return false;
190
}
191
192
function get_vmess_ip($input)
193
{
194
    return !empty($input["sni"])
195
        ? $input["sni"]
196
        : (!empty($input["host"])
197
            ? $input["host"]
198
            : $input["add"]);
199
}
200
201
function get_vless_ip($input, $is_reality)
202
{
203
    return $is_reality
204
        ? $input["hostname"]
205
        : (!empty($input["params"]["sni"])
206
            ? $input["params"]["sni"]
207
            : (!empty($input["params"]["host"])
208
                ? $input["params"]["host"]
209
                : $input["hostname"]));
210
}
211
212
function get_trojan_ip($input)
213
{
214
    return !empty($input["params"]["sni"])
215
        ? $input["params"]["sni"]
216
        : (!empty($input["params"]["host"])
217
            ? $input["params"]["host"]
218
            : $input["hostname"]);
219
}
220
221
function get_tuic_ip($input)
222
{
223
    return $input["hostname"];
224
}
225
226
function get_hy2_ip($input)
227
{
228
    return $input["hostname"];
229
}
230
231
function get_ss_ip($input)
232
{
233
    return $input["server_address"];
234
}
235
236
function get_port($input, $type)
237
{
238
    switch ($type) {
239
        case "vmess":
240
            return $input["port"];
241
        case "vless":
242
        case "trojan":
243
            return $input["port"];
244
        case "tuic":
245
            return $input["port"];
246
        case "hy2":
247
            return $input["port"];
248
        case "ss":
249
            return $input["server_port"];
250
    }
251
}
252
253
function get_flag($ip)
254
{
255
    $flag = "";
256
    $ip_info = ip_info($ip);
257
    if ($ip_info->country != "XX") {
258
        $location = $ip_info->country;
259
        $flag = $location . getFlags($location);
260
    } else {
261
        $flag = "RELAY🚩";
262
    }
263
    return $flag;
264
}
265
266
function get_channels_assets()
267
{
268
    return json_decode(
269
        file_get_contents("modules/channels/channels_assets.json"),
270
        true
271
    );
272
}
273
274
function generate_name($channel, $flag, $is_reality, $number, $type)
275
{
276
    $name = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
277
    if ($channel === "iP_CF") {
278
        $channel = "FAKEOFTVC";
279
    }
280
    switch ($is_reality) {
281
        case true:
282
            return
283
                "رایگان | REALITY | " .
284
                "@" .
285
                $channel .
286
                " | " .
287
                $flag .
288
                " | " .
289
                numberToEmoji($number);
290
        case false:
291
            return
292
                "رایگان | " .
293
                $type .
294
                " | @" .
295
                $channel .
296
                " | " .
297
                $flag .
298
                " | " .
299
                numberToEmoji($number);
300
    }
301
}
302
303
function parse_config($input, $type, $is_sub = false)
304
{
305
    switch ($type) {
306
        case "vmess":
307
            return $is_sub
308
                ? decode_vmess($input)
309
                : decode_vmess($type . "://" . $input);
310
        case "vless":
311
        case "trojan":
312
            return $is_sub
313
                ? parseProxyUrl($input, $type)
314
                : parseProxyUrl($type . "://" . $input, $type);
315
        case "tuic":
316
            return $is_sub
317
                ? parseTuic($input)
318
                : parseTuic($type . "://" . $input);
319
        case "hy2":
320
            return $is_sub
321
                ? parseHy2($input)
322
                : parseHy2($type . "://" . $input);
323
        case "ss":
324
            return $is_sub
325
                ? ParseShadowsocks($input)
326
                : ParseShadowsocks($type . "://" . $input);
327
    }
328
}
329
330
function build_config($input, $type)
331
{
332
    switch ($type) {
333
        case "vmess":
334
            return encode_vmess($input);
335
        case "vless":
336
        case "trojan":
337
            return buildProxyUrl($input, $type);
338
        case "tuic":
339
            return buildTuic($input);
340
        case "hy2":
341
            return buildHy2($input);
342
        case "ss":
343
            return BuildShadowsocks($input);
344
    }
345
}
346
347
function get_config($channel, $type)
348
{
349
    $name_array = [
350
        "vmess" => "ps",
351
        "vless" => "hash",
352
        "trojan" => "hash",
353
        "ss" => "name",
354
        "tuic" => "hash",
355
        "hy2" => "hash"
356
    ];
357
    // Fetch the content of the Telegram channel URL
358
    $get = file_get_contents("https://t.me/s/" . $channel);
359
360
    // Load channels_assets JSON data
361
    $channels_assets = get_channels_assets();
362
363
    $matches = get_config_time($type, $get);
364
    $configs = get_config_items($type, $get);
365
366
    $final_data = [];
367
    if ($channel === "V2rayCollectorDonate") {
368
        $key_limit = count($configs[1]) - 20;
369
    } else {
370
        $key_limit = count($configs[1]) - 7;
371
    }
372
    $config_number = 1;
373
374
    foreach (array_reverse($configs[1]) as $key => $config) {
375
        if ($key >= $key_limit) {
376
            if (is_valid($config)) {
377
                if (strpos($config, "<br/>") !== false) {
378
                    $config = substr($config, 0, strpos($config, "<br/>"));
379
                }
380
381
                $is_reality = is_reality($config, $type);
382
383
                $the_config = parse_config($config, $type);
384
                $check_pbk = $is_reality ? check_pbk($config) : true;
385
386
                $address = get_address($the_config, $type);
387
                if ($check_pbk) {
388
                    if (is_valid_address($address)) {
389
                        $ip = get_ip($the_config, $type, $is_reality);
390
                        $port = get_port($the_config, $type);
391
392
                        @$ping_data = ping($ip, $port);
393
                        if ($ping_data !== "unavailable" || $type === "tuic") {
394
                            $flag = get_flag($ip);
395
396
                            $name_key = $name_array[$type];
397
                            $the_config[$name_key] = generate_name(
398
                                $channel,
399
                                $flag,
400
                                $is_reality,
401
                                $config_number,
402
                                strtoupper($type)
403
                            );
404
405
                            $final_config = build_config($the_config, $type);
406
407
                            $final_data[$key]["channel"]["username"] = $channel === "iP_CF" ? "FAKEOFTVC" : $channel;
408
                            $final_data[$key]["channel"]["title"] =
409
                                $channels_assets[$channel]["title"];
410
                            $final_data[$key]["channel"]["logo"] =
411
                                $channels_assets[$channel]["logo"];
412
                            $final_data[$key]["type"] = $is_reality
413
                                ? "reality"
414
                                : $type;
415
                            $final_data[$key]["config"] = $final_config;
416
                            $final_data[$key]["ping"] = $ping_data;
417
                            $final_data[$key]["time"] = convert_to_iran_time(
418
                                $matches[1][$key]
419
                            );
420
                            $config_number++;
421
                        }
422
                    }
423
                }
424
            }
425
        }
426
    }
427
    // Return the final data array
428
    return $final_data;
429
}
430
431
function detect_type($input)
432
{
433
    if (substr($input, 0, 8) === "vmess://") {
434
        return "vmess";
435
    } elseif (substr($input, 0, 8) === "vless://") {
436
        return "vless";
437
    } elseif (substr($input, 0, 9) === "trojan://") {
438
        return "trojan";
439
    } elseif (substr($input, 0, 5) === "ss://") {
440
        return "ss";
441
    } elseif (substr($input, 0, 7) === "tuic://") {
442
        return "tuic";
443
    } elseif (substr($input, 0, 6) === "hy2://") {
444
        return "hy2";
445
    } 
446
    
447
448
}
449
450
function process_subscription($input, $channel)
451
{
452
    $name_array = [
453
        "vmess" => "ps",
454
        "vless" => "hash",
455
        "trojan" => "hash",
456
        "ss" => "name",
457
        "tuic" => "hash",
458
        "hy2" => "hash",
459
    ];
460
461
    $final_data = [];
462
    $configs = explode("\n", $input);
463
    $array_helper_vmess = 0;
464
    $array_helper_vless = 0;
465
    $array_helper_ss = 0;
466
    $array_helper_trojan = 0;
467
    $array_helper_tuic = 0;
468
    $array_helper_hy2 = 0;
469
    $config_number = 1;
470
    $i = 0;
471
    $channel = $channel . " | Donated";
472
    foreach ($configs as $config) {
473
        $type = detect_type($config);
474
        $is_reality = is_reality($config, $type);
475
476
        $the_config = parse_config($config, $type, true);
477
        $check_pbk = $is_reality ? check_pbk($config) : true;
478
479
        $address = get_address($the_config, $type);
480
        if ($check_pbk) {
481
            if (is_valid_address($address)) {
482
                $ip = get_ip($the_config, $type, $is_reality);
483
                $port = get_port($the_config, $type);
484
485
                @$ping_data = ping($ip, $port);
486
                if ($ping_data !== "unavailable" || $type === "tuic") {
487
                    $flag = get_flag($ip);
488
489
                    $name_key = $name_array[$type];
490
                    $the_config[$name_key] = generate_name(
491
                        $channel,
492
                        $flag,
493
                        $is_reality,
494
                        $config_number,
495
                        strtoupper($type)
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

495
                        strtoupper(/** @scrutinizer ignore-type */ $type)
Loading history...
496
                    );
497
                    $final_config = build_config($the_config, $type);
498
499
                    $key = ${"array_helper_$type"};
500
501
                    $final_data[$type][$key]["channel"]["username"] = $channel;
502
                    $final_data[$type][$key]["channel"]["title"] = $channel;
503
                    $final_data[$type][$key]["channel"]["logo"] = "null";
504
                    $final_data[$type][$key]["type"] = $is_reality
505
                        ? "reality"
506
                        : $type;
507
                    $final_data[$type][$key]["config"] = $final_config;
508
                    $final_data[$type][$key]["ping"] = $ping_data;
509
                    $final_data[$type][$key]["time"] = tehran_time();
510
511
                    $key++;
512
                    ${"array_helper_$type"} = $key;
513
                    $config_number++;
514
                }
515
            }
516
        }
517
    }
518
    $i++;
519
    return $final_data;
520
}
521