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 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 26
c 1
b 0
f 0
nc 49
nop 1
dl 0
loc 36
rs 7.3166

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