Filippo Bertilotti
2024-09-19 072cbba7a5c6aeb9dab04904b6d27312aa7fe6ac
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
<?php
 
namespace App\Vola\Classes;
 
use App\Models\MailTemplate;
use App\Vola\Services\VolaFakeHTTPResponder\VolaFakeHTTPResponder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
 
class Utils
{
 
    public static function genericAppError($methodName,$msg)
    {
        \Log::channel('default')->debug(" * Error [$methodName]:\r".$msg);
        return [
            "code" => "internal-error",
            "desc" => "Error processing request"
        ];
    }
 
    /**
     * @return void
     */
    public static function logResponse($result, $params)
    {
        if (isset($result["code"])) {
            if ($result["code"] != "OK") {
                \Log::channel('requests_failed')->debug(" * Error managing request:\r" . print_r($params, 1));
                \Log::channel('requests_failed')->debug(" * Response returned:\r" . print_r($result, 1) . str_repeat("***", 30) . "\r");
 
            } else {
                \Log::channel('requests_managed')->debug(print_r($params, 1) . "\r" . str_repeat("***", 30) . "\r");
            }
        }
    }
 
    /**
     * @param $filename string nome/path del file da verificare
     * @param string [optional] $extension estenzione che vogliamo controllare(default: ".pdf")
     * @param bool [optional] $modify se true modifica l'estenzione del file passato(default: true)
     */
    public static function hasExtension(string $filename, $extension = ".pdf", $modify = true)
    {
        $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
 
        if ($file_extension !== $extension && $modify) {
            if ($file_extension !== ""){
               $filename = substr($filename, 0, strrpos($filename, "."));
            }
            $filename .= $extension;
        }
 
        return $filename;
    }
 
    public static function convertToXML($data, $root = null)
    {
        if ($root !== null && $root !== '<isLogged/>') {
            $root = "<" . $root . "/>";
        } else {
            $root = "<isLogged></isLogged>";
        }
 
        $xml = new \SimpleXMLElement($root);
        self::array_to_xml($data, $xml);
 
        return $xml->asXML();
    }
 
    public static function array_to_xml($data, &$xml)
    {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $subnode = $xml->addChild($key);
                self::array_to_xml($value, $subnode);
            } else {
                $xml->addChild("$key", "$value");
            }
        }
    }
 
    public static function convertXMLStrToArray(string $xml): array
    {
        $xmlObject = @simplexml_load_string($xml);
        $jsonString = json_encode($xmlObject);
        return json_decode($jsonString,1);
    }
 
    public static function getDomain($url){
        $pieces = parse_url($url);
        $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
        if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)){
            return $regs['domain'];
        }
        return FALSE;
    }
 
    public static function get_string_between(string $string, string $start, string $end): string
    {
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
 
    public static function getLegacyAuthCookie(Request $request): ?string
    {
        return $request->cookie('CAuthCookie', null);
    }
 
    public static function getPicassoAuthCookie(Request $request): ?string
    {
        return $request->cookie('SSOSESSIONID', null);
    }
 
    public static function getRequestedUser(Request $request): ?string
    {
        $picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
        $isLoggedRequest = str_contains( self::get_string_between($request->getRequestUri(), '/', '?'), 'islogged' );
        $profile = null;
 
        if ((!$picassoRequest) && (!$isLoggedRequest)) {
            // la rotta sso islogged usa solo il cookie per identificare l'utente
            $reqParams = !empty($request->query()) ? $request->query() : [];
            if (isset($reqParams["t"])) {
                $profile = $reqParams["t"];
            }
        } else {
            // tutte le chiamate che non sono islogged usano il parametro t (token) per identificare l'utente
            $profile = self::getLegacyAuthCookie($request);
        }
 
        if ($picassoRequest) {
            // picasso usa sempre il cookie per identifcare l'utente
            $profile = self::getPicassoAuthCookie($request);
        }
 
        $profile = (is_null($profile)) ? null : intval(str_replace("xno:", "", $profile));
        return $profile;
    }
 
    public static function getLoggedUser(Request $request): array
    {
        $cookie = self::getLegacyAuthCookie($request);
        $profile = (is_null($cookie)) ? null : intval(str_replace("xno:", "", $cookie));
        $isLogged = (new VolaFakeHTTPResponder())->getLoggedLegacy($profile);
        $xml = self::convertXMLStrToArray($isLogged);
        if (is_string($xml) || is_bool($xml)) {
            return ["logged" => '0', 'message' => $isLogged];
        }
        return $xml;
    }
}