<?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;
|
}
|
}
|