Filippo Bertilotti
2024-07-31 7e64cc10b90638803aa7e6b1b78c76825a8da866
commit | author | age
9f6455 1 <?php
DC 2
3 namespace App\Vola\Classes;
4
5 use App\Models\MailTemplate;
d5d253 6 use App\Vola\Services\VolaFakeHTTPResponder\VolaFakeHTTPResponder;
68b84d 7 use Illuminate\Http\Request;
9f6455 8 use Illuminate\Support\Facades\Log;
DC 9
10 class Utils
11 {
12
13     public static function genericAppError($methodName,$msg)
14     {
15         \Log::channel('default')->debug(" * Error [$methodName]:\r".$msg);
16         return [
17             "code" => "internal-error",
18             "desc" => "Error processing request"
19         ];
20     }
21
22     /**
23      * @return void
24      */
25     public static function logResponse($result, $params)
26     {
27         if (isset($result["code"])) {
28             if ($result["code"] != "OK") {
29                 \Log::channel('requests_failed')->debug(" * Error managing request:\r" . print_r($params, 1));
30                 \Log::channel('requests_failed')->debug(" * Response returned:\r" . print_r($result, 1) . str_repeat("***", 30) . "\r");
31
32             } else {
33                 \Log::channel('requests_managed')->debug(print_r($params, 1) . "\r" . str_repeat("***", 30) . "\r");
34             }
35         }
36     }
37
38     /**
39      * @param $filename string nome/path del file da verificare
40      * @param string [optional] $extension estenzione che vogliamo controllare(default: ".pdf")
41      * @param bool [optional] $modify se true modifica l'estenzione del file passato(default: true)
42      */
43     public static function hasExtension(string $filename, $extension = ".pdf", $modify = true)
44     {
45         $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
46
47         if ($file_extension !== $extension && $modify) {
48             if ($file_extension !== ""){
49                $filename = substr($filename, 0, strrpos($filename, "."));
50             }
51             $filename .= $extension;
52         }
53
54         return $filename;
55     }
56
57     public static function convertToXML($data, $root = null)
58     {
59         if ($root !== null && $root !== '<isLogged/>') {
60             $root = "<" . $root . "/>";
61         } else {
62             $root = "<isLogged></isLogged>";
63         }
64
65         $xml = new \SimpleXMLElement($root);
66         self::array_to_xml($data, $xml);
67
68         return $xml->asXML();
69     }
70
71     public static function array_to_xml($data, &$xml)
72     {
73         foreach ($data as $key => $value) {
74             if (is_array($value)) {
75                 $subnode = $xml->addChild($key);
76                 self::array_to_xml($value, $subnode);
77             } else {
78                 $xml->addChild("$key", "$value");
79             }
80         }
81     }
82
83     public static function convertXMLStrToArray(string $xml): array
84     {
85         $xmlObject = @simplexml_load_string($xml);
86         $jsonString = json_encode($xmlObject);
87         return json_decode($jsonString,1);
88     }
89
90     public static function getDomain($url){
91         $pieces = parse_url($url);
92         $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
93         if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)){
94             return $regs['domain'];
95         }
96         return FALSE;
97     }
98
3c5355 99     public static function get_string_between(string $string, string $start, string $end): string
D 100     {
101         $string = ' ' . $string;
102         $ini = strpos($string, $start);
103         if ($ini == 0) return '';
104         $ini += strlen($start);
105         $len = strpos($string, $end, $ini) - $ini;
106         return substr($string, $ini, $len);
107     }
108
68b84d 109     public static function getLegacyAuthCookie(Request $request): ?string
D 110     {
111         return $request->cookie('CAuthCookie', null);
112     }
113
114     public static function getPicassoAuthCookie(Request $request): ?string
115     {
116         return $request->cookie('SSOSESSIONID', null);
117     }
118
119     public static function getRequestedUser(Request $request): ?string
120     {
121         $picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
3c5355 122         $isLoggedRequest = str_contains( self::get_string_between($request->getRequestUri(), '/', '?'), 'islogged' );
D 123         $profile = null;
68b84d 124
3c5355 125         if ((!$picassoRequest) && (!$isLoggedRequest)) {
D 126             // la rotta sso islogged usa solo il cookie per identificare l'utente
127             $reqParams = !empty($request->query()) ? $request->query() : [];
128             if (isset($reqParams["t"])) {
129                 $profile = $reqParams["t"];
71688b 130             }
3c5355 131         } else {
D 132             // tutte le chiamate che non sono islogged usano il parametro t (token) per identificare l'utente
133             $profile = self::getLegacyAuthCookie($request);
134         }
135
136         if ($picassoRequest) {
137             // picasso usa sempre il cookie per identifcare l'utente
71688b 138             $profile = self::getPicassoAuthCookie($request);
68b84d 139         }
3c5355 140
71688b 141         $profile = (is_null($profile)) ? null : intval(str_replace("xno:", "", $profile));
d5d253 142         return $profile;
68b84d 143     }
D 144
d5d253 145     public static function getLoggedUser(Request $request): array
D 146     {
147         $cookie = self::getLegacyAuthCookie($request);
148         $profile = (is_null($cookie)) ? null : intval(str_replace("xno:", "", $cookie));
149         $isLogged = (new VolaFakeHTTPResponder())->getLoggedLegacy($profile);
150         $xml = self::convertXMLStrToArray($isLogged);
151         if (is_string($xml) || is_bool($xml)) {
152             return ["logged" => '0', 'message' => $isLogged];
153         }
154         return $xml;
155     }
9f6455 156 }
DC 157