davide.cucurnia@vola.it
2024-02-07 fcb09380736bcbd8609ada51df5423d428552932
commit | author | age
9f6455 1 <?php
DC 2
3 namespace App\Vola\Classes;
4
5 use App\Models\MailTemplate;
68b84d 6 use Illuminate\Http\Request;
9f6455 7 use Illuminate\Support\Facades\Log;
DC 8
9 class Utils
10 {
11
12     public static function genericAppError($methodName,$msg)
13     {
14         \Log::channel('default')->debug(" * Error [$methodName]:\r".$msg);
15         return [
16             "code" => "internal-error",
17             "desc" => "Error processing request"
18         ];
19     }
20
21     /**
22      * @return void
23      */
24     public static function logResponse($result, $params)
25     {
26         if (isset($result["code"])) {
27             if ($result["code"] != "OK") {
28                 \Log::channel('requests_failed')->debug(" * Error managing request:\r" . print_r($params, 1));
29                 \Log::channel('requests_failed')->debug(" * Response returned:\r" . print_r($result, 1) . str_repeat("***", 30) . "\r");
30
31             } else {
32                 \Log::channel('requests_managed')->debug(print_r($params, 1) . "\r" . str_repeat("***", 30) . "\r");
33             }
34         }
35     }
36
37     /**
38      * @param $filename string nome/path del file da verificare
39      * @param string [optional] $extension estenzione che vogliamo controllare(default: ".pdf")
40      * @param bool [optional] $modify se true modifica l'estenzione del file passato(default: true)
41      */
42     public static function hasExtension(string $filename, $extension = ".pdf", $modify = true)
43     {
44         $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
45
46         if ($file_extension !== $extension && $modify) {
47             if ($file_extension !== ""){
48                $filename = substr($filename, 0, strrpos($filename, "."));
49             }
50             $filename .= $extension;
51         }
52
53         return $filename;
54     }
55
56     public static function convertToXML($data, $root = null)
57     {
58         if ($root !== null && $root !== '<isLogged/>') {
59             $root = "<" . $root . "/>";
60         } else {
61             $root = "<isLogged></isLogged>";
62         }
63
64         $xml = new \SimpleXMLElement($root);
65         self::array_to_xml($data, $xml);
66
67         return $xml->asXML();
68     }
69
70     public static function array_to_xml($data, &$xml)
71     {
72         foreach ($data as $key => $value) {
73             if (is_array($value)) {
74                 $subnode = $xml->addChild($key);
75                 self::array_to_xml($value, $subnode);
76             } else {
77                 $xml->addChild("$key", "$value");
78             }
79         }
80     }
81
82     public static function convertXMLStrToArray(string $xml): array
83     {
84         $xmlObject = @simplexml_load_string($xml);
85         $jsonString = json_encode($xmlObject);
86         return json_decode($jsonString,1);
87     }
88
89     public static function getDomain($url){
90         $pieces = parse_url($url);
91         $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
92         if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)){
93             return $regs['domain'];
94         }
95         return FALSE;
96     }
97
68b84d 98     public static function getLegacyAuthCookie(Request $request): ?string
D 99     {
100         return $request->cookie('CAuthCookie', null);
101     }
102
103     public static function getPicassoAuthCookie(Request $request): ?string
104     {
105         return $request->cookie('SSOSESSIONID', null);
106     }
107
108     public static function getRequestedUser(Request $request): ?string
109     {
110         $picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
111
112         if (!$picassoRequest && self::getLegacyAuthCookie($request)) {
113             $profile = intval(str_replace("xno:", "", self::getLegacyAuthCookie($request)));
114         } else if ($picassoRequest && self::getPicassoAuthCookie($request)) {
115             $profile = intval(str_replace("xno:", "", self::getPicassoAuthCookie($request)));
116         }
117
118         return (isset($profile)) ? $profile : null;
119     }
120
9f6455 121 }
DC 122