davide.cucurnia@vola.it
2024-02-08 d5d253a98cabfe8f57335a2805120335d5ab265d
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
68b84d 99     public static function getLegacyAuthCookie(Request $request): ?string
D 100     {
101         return $request->cookie('CAuthCookie', null);
102     }
103
104     public static function getPicassoAuthCookie(Request $request): ?string
105     {
106         return $request->cookie('SSOSESSIONID', null);
107     }
108
109     public static function getRequestedUser(Request $request): ?string
110     {
111         $picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
112
71688b 113         if (!$picassoRequest) {
D 114             if (is_null(self::getLegacyAuthCookie($request))) {
115                 $reqParams = !empty($request->query()) ? $request->query() : [];
116                 if (isset($reqParams["t"])) {
117                     $profile = $reqParams["t"];
118                 }
119             } else {
120                 $profile = self::getLegacyAuthCookie($request);
121             }
68b84d 122         } else if ($picassoRequest && self::getPicassoAuthCookie($request)) {
71688b 123             $profile = self::getPicassoAuthCookie($request);
68b84d 124         }
71688b 125         $profile = (is_null($profile)) ? null : intval(str_replace("xno:", "", $profile));
d5d253 126         return $profile;
68b84d 127     }
D 128
d5d253 129     public static function getLoggedUser(Request $request): array
D 130     {
131         $cookie = self::getLegacyAuthCookie($request);
132         $profile = (is_null($cookie)) ? null : intval(str_replace("xno:", "", $cookie));
133         $isLogged = (new VolaFakeHTTPResponder())->getLoggedLegacy($profile);
134         $xml = self::convertXMLStrToArray($isLogged);
135         if (is_string($xml) || is_bool($xml)) {
136             return ["logged" => '0', 'message' => $isLogged];
137         }
138         return $xml;
139     }
9f6455 140 }
DC 141