<?php
|
|
namespace App\Vola\Classes;
|
|
use App\Models\MailTemplate;
|
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 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/',));
|
|
if (!$picassoRequest && self::getLegacyAuthCookie($request)) {
|
$profile = intval(str_replace("xno:", "", self::getLegacyAuthCookie($request)));
|
} else if ($picassoRequest && self::getPicassoAuthCookie($request)) {
|
$profile = intval(str_replace("xno:", "", self::getPicassoAuthCookie($request)));
|
}
|
|
return (isset($profile)) ? $profile : null;
|
}
|
|
}
|