seprate_by_country()   B
last analyzed

Complexity

Conditions 11
Paths 49

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 26
nc 49
nop 1
dl 0
loc 36
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

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
header("Content-type: application/json;"); // Set response content type as JSON
3
4
include "modules/get_data.php"; // Include the get_data module
5
include "modules/config.php"; // Include the config module
6
include "modules/ranking.php"; // Include the ranking module
7
include "modules/singbox.php"; // Include the singbox module
8
9
function addHeader ($subscription, $subscriptionName) {
10
    $headerText = "#profile-title: base64:" . base64_encode($subscriptionName) . "
11
#profile-update-interval: 1
12
#subscription-userinfo: upload=0; download=0; total=10737418240000000; expire=2546249531
13
#support-url: https://t.me/v2raycollector
14
#profile-web-page-url: https://github.com/yebekhe/TelegramV2rayCollector
15
16
";
17
18
    return $headerText . $subscription;
19
}
20
21
function deleteFolder($folder) {
22
    if (!is_dir($folder)) {
23
        return;
24
    }
25
    $files = glob($folder . '/*');
26
    foreach ($files as $file) {
27
        is_dir($file) ? deleteFolder($file) : unlink($file);
28
    }
29
    rmdir($folder);
30
}
31
32
function seprate_by_country($configs){
33
    $configsArray = explode("\n", $configs);
34
    $configLocation = "";
35
    $output = [];
36
    foreach ($configsArray as $config) {
37
        $configType = detect_type($config);
38
39
        if ($configType === "vmess") {
40
            $configName = parse_config($config, "vmess", true)['ps'];
41
        } elseif ($configType === "vless" || $configType === "trojan" ){
42
            $configName = parse_config($config, $configType)['hash'];
43
        } elseif ($configType === "ss"){
44
            $configName = parse_config($config, "ss")['name'];
45
        } elseif ($configType === "tuic"){
46
            $configName = parse_config($config, "tuic")['hash'];
47
        } elseif ($configType === "hy2"){
48
            $configName = parse_config($config, "hy2")['hash'];
49
        }
50
51
        if (stripos($configName, "RELAY🚩")){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $configName does not seem to be defined for all execution paths leading up to this point.
Loading history...
52
            $configLocation = "RELAY";
53
        } else {
54
            $pattern = '/\b([A-Z]{2})\b[\x{1F1E6}-\x{1F1FF}]{2}/u';
55
            preg_match_all($pattern, $configName, $matches);
56
            $configLocation = $matches[1][0];
57
        }
58
59
        if (!isset($output[$configLocation])){
60
            $output[$configLocation] = [];
61
        }
62
        
63
        if (!in_array($config, $output[$configLocation])) {
64
            $output[$configLocation][] = $config;
65
        }
66
    }
67
    return $output;
68
}
69
70
function process_mix_json($input, $name)
71
{
72
    $mix_data_json = json_encode($input, JSON_PRETTY_PRINT); // Encode input array to JSON with pretty printing
73
    $mix_data_decode = json_decode($mix_data_json); // Decode the JSON into an object or array
74
    usort($mix_data_decode, "compare_time"); // Sort the decoded data using the "compare_time" function
75
    $mix_data_json = json_encode(
76
        $mix_data_decode,
77
        JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
78
    ); // Re-encode the sorted data to JSON with pretty printing and Unicode characters not escaped
79
    $mix_data_json = urldecode($mix_data_json);
80
    $mix_data_json = str_replace("amp;", "", $mix_data_json); // Replace HTML-encoded ampersands with regular ampersands
81
    $mix_data_json = str_replace("\\", "", $mix_data_json); // Remove backslashes from the JSON string
82
    file_put_contents("json/" . $name, $mix_data_json); // Save the JSON data to a file in the "json/" directory with the specified name
83
}
84
85
function fast_fix($input){
86
    $input = urldecode($input);
87
    $input = str_replace("amp;", "", $input);
88
    return $input;
89
}
90
91
function config_array($input){
92
    return array_map(function ($object) {
93
    return $object["config"];
94
}, $input);
95
}
96
97
$raw_url_base =
98
    "https://raw.githubusercontent.com/yebekhe/TelegramV2rayCollector/main"; // Define the base URL for fetching raw data
99
100
$mix_data = []; // Initialize an empty array for mix data
101
$vmess_data = []; // Initialize an empty array for vmess data
102
$trojan_data = []; // Initialize an empty array for trojan data
103
$vless_data = []; // Initialize an empty array for vless data
104
$shadowsocks_data = []; // Initialize an empty array for shadowsocks data
105
$tuic_data = []; // Initialize an empty array for tuic data
106
$hy2_data = []; // Initialize an empty array for hy2 data
107
108
109
//Get data from channels
110
foreach ($Types as $channelUsername => $type_array) {
111
    $count = count($type_array);
112
    for ($type_count = $count - 1; $type_count >= 0; $type_count--) {
113
        $current_type = $type_array[$type_count];
114
        if ($current_type === "vmess") {
115
                // Merge the results of `get_config` function with $vmess_data array
116
                $vmess_data = array_merge(
117
                    $vmess_data,
118
                    /** @scrutinizer ignore-call */ 
119
                    get_config($channelUsername, $current_type)
120
                );
121
        } 
122
        if ($current_type === "vless") {
123
                // Merge the results of `get_config` function with $vless_data array
124
                $vless_data = array_merge(
125
                    $vless_data,
126
                    /** @scrutinizer ignore-call */
127
                    get_config($channelUsername, $current_type)
128
                );
129
        } 
130
        if ($current_type === "trojan") {
131
                // Merge the results of `get_config` function with $trojan_data array
132
                $trojan_data = array_merge(
133
                    $trojan_data,
134
                    /** @scrutinizer ignore-call */
135
                    get_config($channelUsername, $current_type)
136
                );
137
        } 
138
        if($current_type === "ss") {
139
                // Merge the results of `get_config` function with $shadowsocks_data array
140
                $shadowsocks_data = array_merge(
141
                    $shadowsocks_data,
142
                    /** @scrutinizer ignore-call */
143
                    get_config($channelUsername, $current_type)
144
                );
145
        } 
146
        if ($current_type === "tuic") {
147
                // Merge the results of `get_config` function with $tuic_data array
148
                $tuic_data = array_merge(
149
                    $tuic_data,
150
                    /** @scrutinizer ignore-call */
151
                    get_config($channelUsername, $current_type)
152
                );
153
        }
154
        if ($current_type === "hy2") {
155
            // Merge the results of `get_config` function with $tuic_data array
156
            $hy2_data = array_merge(
157
                $hy2_data,
158
                /** @scrutinizer ignore-call */
159
                get_config($channelUsername, $current_type)
160
            );
161
    }
162
    }
163
}
164
165
$donated_vmess_data = []; // Initialize an empty array for vmess data
166
$donated_trojan_data = []; // Initialize an empty array for trojan data
167
$donated_vless_data = []; // Initialize an empty array for vless data
168
$donated_shadowsocks_data = []; // Initialize an empty array for shadowsocks data
169
$donated_tuic_data = []; // Initialize an empty array for tuic data
170
$donated_hy2_data = []; // Initialize an empty array for tuic data
171
172
$base_donated_url = "https://yebekhe.000webhostapp.com/donate/donated_servers/";
173
174
$processed_subscription = [];
175
$usernames = [];
176
foreach ($donated_subscription as $url){
177
    $max_attempts = 3;
178
    $attempts = 0;
179
    while ($attempts < $max_attempts) {
180
        try {
181
            $usernames = json_decode(file_get_contents($url), true);
182
            break; // Success, so break out of the loop
183
        } catch (Exception $e) {
184
            // Handle the error here, e.g. by logging it
185
            $attempts++;
186
            if ($attempts == $max_attempts) {
187
             // Reached max attempts, so throw an exception to indicate failure
188
                throw new Exception('Failed to retrieve data after ' . $max_attempts . ' attempts.');
189
            }
190
        sleep(1); // Wait for 1 second before retrying
191
        }
192
    }
193
    foreach ($usernames as $username){
194
        $subscription_data = file_get_contents($base_donated_url . $username);
195
        $processed_subscription = /** @scrutinizer ignore-call */ process_subscription($subscription_data, $username);
196
        foreach ($processed_subscription as $donated_type => $donated_data){
197
            if ($donated_type === "vmess") {
198
                    $donated_vmess_data = array_merge(
199
                        $donated_vmess_data,
200
                        $donated_data
201
                    );
202
            } 
203
            if ($donated_type === "vless") {
204
                    $donated_vless_data = array_merge(
205
                        $donated_vless_data,
206
                        $donated_data
207
                    );
208
            } 
209
            if ($donated_type === "ss") {
210
                    $donated_shadowsocks_data = array_merge(
211
                        $donated_shadowsocks_data,
212
                        $donated_data
213
                    );
214
            } 
215
            if ($donated_type === "trojan") {
216
                    $donated_trojan_data = array_merge(
217
                        $donated_trojan_data,
218
                        $donated_data
219
                    );
220
            } 
221
            if ($donated_type === "tuic") {
222
                    $donated_tuic_data = array_merge(
223
                        $donated_tuic_data,
224
                        $donated_data
225
                    );
226
            }
227
            if ($donated_type === "hy2") {
228
                $donated_hy2_data = array_merge(
229
                    $donated_hy2_data,
230
                    $donated_data
231
                );
232
        }
233
        }
234
    }
235
}
236
237
$string_donated_vmess = $donated_vmess_data !== [] ? remove_duplicate_vmess(implode("\n", config_array($donated_vmess_data))) : "";
238
$string_donated_vless = $donated_vless_data !== [] ? remove_duplicate_xray(fast_fix(implode("\n", config_array($donated_vless_data))), "vless") : "";
239
$string_donated_trojan = $donated_trojan_data !== [] ? remove_duplicate_xray(fast_fix(implode("\n", config_array($donated_trojan_data))), "trojan") : "";
240
$string_donated_shadowsocks = $donated_shadowsocks_data !== [] ? remove_duplicate_ss(fast_fix(implode("\n", config_array($donated_shadowsocks_data)))) : "";
241
$string_donated_tuic = $donated_tuic_data !== [] ? remove_duplicate_tuic(fast_fix(implode("\n", config_array($donated_tuic_data)))) : "";
242
$string_donated_hy2 = $donated_tuic_hy2 !== [] ? remove_duplicate_hy2(fast_fix(implode("\n", config_array($donated_hy2_data)))) : "";
243
$string_donated_reality = get_reality($string_donated_vless);
244
245
$donated_mix =
246
    $string_donated_vmess .
247
    "\n" .
248
    $string_donated_vless .
249
    "\n" .
250
    $string_donated_trojan .
251
    "\n" .
252
    $string_donated_shadowsocks .
253
    "\n" .
254
    $string_donated_tuic .
255
    "\n" .
256
    $string_donated_hy2;
257
$donated_array = explode("\n", $donated_mix);
258
259
foreach ($donated_array as $key => $donated_config){
260
    if ($donated_config === ""){
261
        unset($donated_array[$key]);
262
    }
263
}
264
265
$donated_mix = implode("\n", $donated_array);
266
267
file_put_contents("sub/normal/donated", addHeader($donated_mix, "TVC | DONATED"));
268
file_put_contents("sub/base64/donated", base64_encode(addHeader($donated_mix, "TVC | DONATED")));
269
270
// Extract the "config" value from each object in $type_data and store it in $type_array
271
$vmess_array = config_array($vmess_data);
272
$vless_array = config_array($vless_data);
273
$trojan_array = config_array($trojan_data);
274
$shadowsocks_array = config_array($shadowsocks_data);
275
$tuic_array = config_array($tuic_data);
276
$hy2_array = config_array($hy2_data);
277
278
$fixed_string_vmess = remove_duplicate_vmess(implode("\n", $vmess_array));
279
$fixed_string_vmess_array = explode("\n", $fixed_string_vmess);
280
$json_vmess_array = [];
281
282
// Iterate over $vmess_data and $fixed_string_vmess_array to find matching configurations
283
foreach ($vmess_data as $vmess_config_data) {
284
    foreach ($fixed_string_vmess_array as $vmess_config) {
285
        if (
286
            decode_vmess($vmess_config)["ps"] ===
287
            decode_vmess($vmess_config_data["config"])["ps"]
288
        ) {
289
            // Add matching configuration to $json_vmess_array
290
            $json_vmess_array[] = $vmess_config_data;
291
        }
292
    }
293
}
294
295
$string_vless = fast_fix(implode("\n", $vless_array));
296
$fixed_string_vless = remove_duplicate_xray($string_vless, "vless");
297
$fixed_string_vless_array = explode("\n", $fixed_string_vless);
298
$json_vless_array = [];
299
300
// Iterate over $vless_data and $fixed_string_vless_array to find matching configurations
301
foreach ($vless_data as $vless_config_data) {
302
    foreach ($fixed_string_vless_array as $vless_config) {
303
        if (
304
            parseProxyUrl($vless_config, "vless")["hash"] ===
305
            parseProxyUrl($vless_config_data["config"], "vless")["hash"]
306
        ) {
307
            // Add matching configuration to $json_vless_array
308
            $json_vless_array[] = $vless_config_data;
309
        }
310
    }
311
}
312
313
$string_trojan = fast_fix(implode("\n", $trojan_array));
314
$fixed_string_trojan = remove_duplicate_xray($string_trojan, "trojan");
315
$fixed_string_trojan_array = explode("\n", $fixed_string_trojan);
316
$json_trojan_array = [];
317
318
// Iterate over $trojan_data and $fixed_string_trojan_array to find matching configurations
319
foreach ($trojan_data as $trojan_config_data) {
320
    foreach ($fixed_string_trojan_array as $key => $trojan_config) {
321
        if (
322
            parseProxyUrl($trojan_config)["hash"] ===
323
            parseProxyUrl($trojan_config_data["config"])["hash"]
324
        ) {
325
            // Add matching configuration to $json_trojan_array
326
            $json_trojan_array[$key] = $trojan_config_data;
327
        }
328
    }
329
}
330
331
$string_shadowsocks = fast_fix(implode("\n", $shadowsocks_array));
332
$fixed_string_shadowsocks = remove_duplicate_ss($string_shadowsocks);
333
$fixed_string_shadowsocks_array = explode("\n", $fixed_string_shadowsocks);
334
$json_shadowsocks_array = [];
335
336
// Iterate over $shadowsocks_data and $fixed_string_shadowsocks_array to find matching configurations
337
foreach ($shadowsocks_data as $shadowsocks_config_data) {
338
    foreach ($fixed_string_shadowsocks_array as $shadowsocks_config) {
339
        if (
340
            ParseShadowsocks($shadowsocks_config)["name"] ===
341
            ParseShadowsocks($shadowsocks_config_data["config"])["name"]
342
        ) {
343
            // Add matching configuration to $json_shadowsocks_array
344
            $json_shadowsocks_array[] = $shadowsocks_config_data;
345
        }
346
    }
347
}
348
349
$string_tuic = fast_fix(implode("\n", $tuic_array));
350
$fixed_string_tuic = remove_duplicate_tuic($string_tuic);
351
$fixed_string_tuic_array = explode("\n", $fixed_string_tuic);
352
$json_tuic_array = [];
353
354
// Iterate over $tuic_data and $fixed_string_tuic_array to find matching configurations
355
foreach ($tuic_data as $tuic_config_data) {
356
    foreach ($fixed_string_tuic_array as $key => $tuic_config) {
357
        if (
358
            parseTuic($tuic_config)["hash"] ===
359
            parseTuic($tuic_config_data["config"])["hash"]
360
        ) {
361
            // Add matching configuration to $json_tuic_array
362
            $json_tuic_array[$key] = $tuic_config_data;
363
        }
364
    }
365
}
366
367
$string_hy2 = fast_fix(implode("\n", $hy2_array));
368
$fixed_string_hy2 = remove_duplicate_hy2($string_hy2);
369
$fixed_string_hy2_array = explode("\n", $fixed_string_hy2);
370
$json_hy2_array = [];
371
372
// Iterate over $hy2_data and $fixed_string_hy2_array to find matching configurations
373
foreach ($hy2_data as $hy2_config_data) {
374
    foreach ($fixed_string_hy2_array as $key => $hy2_config) {
375
        if (
376
            parsehy2($hy2_config)["hash"] ===
377
            parsehy2($hy2_config_data["config"])["hash"]
378
        ) {
379
            // Add matching configuration to $json_hy2_array
380
            $json_hy2_array[$key] = $hy2_config_data;
381
        }
382
    }
383
}
384
385
$fixed_string_reality = get_reality($fixed_string_vless);
386
387
$mix =
388
    $fixed_string_vmess .
389
    "\n" .
390
    $fixed_string_vless .
391
    "\n" .
392
    $fixed_string_trojan .
393
    "\n" .
394
    $fixed_string_shadowsocks .
395
    "\n" .
396
    $fixed_string_tuic .
397
    "\n" .
398
    $fixed_string_hy2 .
399
    "\n" .
400
    $donated_mix;
401
402
$mix_data = array_merge(
403
    $vmess_data,
404
    $vless_data,
405
    $trojan_data,
406
    $shadowsocks_data,
407
    $tuic_data,
408
    $hy2_data
409
);
410
411
$mix_data_deduplicate = array_merge(
412
    $json_vmess_array,
413
    $json_vless_array,
414
    $json_trojan_array,
415
    $json_shadowsocks_array,
416
    $json_tuic_array,
417
    $json_hy2_array
418
);
419
420
$subscription_types = [
421
    "mix" => base64_encode(addHeader($mix, "TVC | MIX")),
422
    "vmess" => base64_encode(addHeader($fixed_string_vmess, "TVC | VMESS")),
423
    "vless" => base64_encode(addHeader($fixed_string_vless, "TVC | VLESS")),
424
    "reality" => base64_encode(addHeader($fixed_string_reality, "TVC | REALITY")),
425
    "trojan" => base64_encode(addHeader($fixed_string_trojan, "TVC | TROJAN")),
426
    "shadowsocks" => base64_encode(addHeader($fixed_string_shadowsocks, "TVC | SHADOWSOCKS")),
427
    "tuic" => base64_encode(addHeader($fixed_string_tuic, "TVC | TUIC")),
428
    "hysteria2" => base64_encode(addHeader($fixed_string_hy2, "TVC | HYSTERIA2")),
429
];
430
431
// Write subscription data to files
432
foreach ($subscription_types as $subscription_type => $subscription_data) {
433
    file_put_contents(
434
        "sub/normal/" . $subscription_type,
435
        base64_decode($subscription_data)
436
    );
437
    file_put_contents(
438
        "sub/base64/" . $subscription_type,
439
        $subscription_data
440
    );
441
}
442
443
$countryBased = seprate_by_country($mix);
444
deleteFolder("country");
445
mkdir("country");
446
foreach ($countryBased as $country => $configsArray) {
447
    if (!is_dir("country/". $country)) {
448
        mkdir("country/". $country);
449
    }
450
    $configsSub = implode("\n", $configsArray);
451
    file_put_contents("country/". $country . "/normal", $configsSub);
452
    file_put_contents("country/". $country . "/base64", base64_encode($configsSub));
453
}
454
455
process_mix_json($mix_data, "configs.json");
456
process_mix_json($mix_data_deduplicate, "configs_deduplicate.json");
457
458
$singboxTypes = [
459
    "mix" => $mix,
460
    "vmess" => $fixed_string_vmess,
461
    "vless" => $fixed_string_vless,
462
    "trojan" => $fixed_string_trojan,
463
    "shadowsocks" => $fixed_string_shadowsocks,
464
    "tuic" => $fixed_string_tuic,
465
    "hysteria2" => $fixed_string_hy2,
466
];
467
468
foreach ($singboxTypes as $singboxType => $subContents) {
469
    file_put_contents("singbox/nekobox/118/" . $singboxType . ".json", GenerateConfig($subContents, "nnew", $singboxType));
470
    file_put_contents("singbox/nekobox/117/" . $singboxType . ".json", GenerateConfig($subContents, "nold", $singboxType));
471
    file_put_contents("singbox/sfasfi/" . $singboxType . ".json", GenerateConfig($subContents, "sfia", $singboxType));
472
    file_put_contents("singbox/nekobox/118/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "nnew", $singboxType));
473
    file_put_contents("singbox/nekobox/117/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "nold", $singboxType));
474
    file_put_contents("singbox/sfasfi/" . $singboxType . "Lite.json", GenerateConfigLite($subContents, "sfia", $singboxType));
475
}
476
477
$the_string_reality_singbox = $fixed_string_reality . "\n" . $string_donated_reality ;
478
$string_reality_singbox = remove_duplicate_xray($the_string_reality_singbox, "vless");
479
480
file_put_contents("singbox/nekobox/117/reality.json", GenerateConfig($string_reality_singbox, "nold", "reality"));
481
file_put_contents("singbox/nekobox/118/reality.json", GenerateConfig($string_reality_singbox, "nnew", "reality"));
482
file_put_contents("singbox/sfasfi/reality.json", GenerateConfig($string_reality_singbox,"sfia", "reality"));
483
file_put_contents("singbox/nekobox/117/realityLite.json", GenerateConfigLite($string_reality_singbox, "nold", "reality"));
484
file_put_contents("singbox/nekobox/118/realityLite.json", GenerateConfigLite($string_reality_singbox, "nnew", "reality"));
485
file_put_contents("singbox/sfasfi/realityLite.json", GenerateConfigLite($string_reality_singbox,"sfia", "reality"));
486
487
$data = [
488
    [
489
        "data" => $vmess_data,
490
        "filename" => "channel_ranking_vmess.json",
491
        "type" => "vmess",
492
    ],
493
    [
494
        "data" => $vless_data,
495
        "filename" => "channel_ranking_vless.json",
496
        "type" => "vless",
497
    ],
498
    [
499
        "data" => $trojan_data,
500
        "filename" => "channel_ranking_trojan.json",
501
        "type" => "trojan",
502
    ],
503
    [
504
        "data" => $shadowsocks_data,
505
        "filename" => "channel_ranking_ss.json",
506
        "type" => "ss",
507
    ],
508
    [
509
        "data" => $tuic_data,
510
        "filename" => "channel_ranking_tuic.json",
511
        "type" => "tuic",
512
    ],
513
    [
514
        "data" => $hy2_data,
515
        "filename" => "channel_ranking_hy2.json",
516
        "type" => "hy2",
517
    ],
518
];
519
520
// Process each item in the data array
521
foreach ($data as $item) {
522
    // Calculate ranking for the specific type of data
523
    $channel_ranking = ranking($item["data"], $item["type"]);
524
525
    // Convert the ranking to JSON format
526
    $json_content = json_encode($channel_ranking, JSON_PRETTY_PRINT);
527
528
    // Write the JSON content to a file in the "ranking" directory
529
    file_put_contents("ranking/{$item["filename"]}", $json_content);
530
}
531