Issues (17)

modules/tuic.php (1 issue)

1
<?php
2
3
function parseTuic ($config_str) {
4
    $parsedUrl = parse_url($config_str);
5
6
    // Extract the parameters from the query string
7
    $params = [];
8
    if (isset($parsedUrl["query"])) {
9
        parse_str($parsedUrl["query"], $params);
10
    }
11
12
    // Construct the output object
13
    $output = [
14
        "protocol" => "tuic",
15
        "username" => isset($parsedUrl["user"]) ? $parsedUrl["user"] : "",
16
        "pass" => isset($parsedUrl["pass"]) ? $parsedUrl["pass"] : "",
17
        "hostname" => isset($parsedUrl["host"]) ? $parsedUrl["host"] : "",
18
        "port" => isset($parsedUrl["port"]) ? $parsedUrl["port"] : "",
19
        "params" => $params,
20
        "hash" => isset($parsedUrl["fragment"]) ? $parsedUrl["fragment"] : "",
21
    ];
22
23
    return $output;
24
25
}
26
27
function buildTuic($obj)
28
{
29
    $url = "tuic://";
30
    $url .= addUsernameAndPassword($obj);
31
    $url .= $obj["hostname"];
32
    $url .= addPort($obj);
33
    $url .= addParams($obj);
34
    $url .= addHash($obj);
35
    return $url;
36
}
37
38
function remove_duplicate_tuic($input)
39
{
40
    $array = explode("\n", $input);
41
42
    foreach ($array as $item) {
43
        $parts = parseTuic($item);
44
        $part_hash = $parts["hash"];
45
        unset($parts["hash"]);
46
        ksort($parts["params"]);
47
        $part_serialize = base64_encode(serialize($parts));
48
        $result[$part_serialize][] = $part_hash ?? "";
49
    }
50
51
    $finalResult = [];
52
    foreach ($result as $url => $parts) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 42. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
53
        $partAfterHash = $parts[0] ?? "";
54
        $part_serialize = unserialize(base64_decode($url));
55
        $part_serialize["hash"] = $partAfterHash;
56
        $finalResult[] = buildTuic($part_serialize);
57
    }
58
59
    $output = "";
60
    foreach ($finalResult as $config) {
61
        $output .= $output == "" ? $config : "\n" . $config;
62
    }
63
    return $output;
64
}
65