Filippo Bertilotti
2024-09-19 072cbba7a5c6aeb9dab04904b6d27312aa7fe6ac
app/Vola/Classes/Utils.php
@@ -3,6 +3,7 @@
namespace App\Vola\Classes;
use App\Models\MailTemplate;
use App\Vola\Services\VolaFakeHTTPResponder\VolaFakeHTTPResponder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
@@ -95,6 +96,16 @@
        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);
@@ -108,23 +119,39 @@
    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) {
            if (is_null(self::getLegacyAuthCookie($request))) {
                $reqParams = !empty($request->query()) ? $request->query() : [];
                if (isset($reqParams["t"])) {
                    $profile = $reqParams["t"];
                }
            } else {
                $profile = self::getLegacyAuthCookie($request);
        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 if ($picassoRequest && self::getPicassoAuthCookie($request)) {
        } 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 (isset($profile)) ? $profile : null;
        $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;
    }
}