<?php
// AuthApiP.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Authentication;use Docuform\Api\ApiUser;use Docuform\Lib\ApiUuid;class ServerState{public const NOERROR=0;public const EXPIRED=2;public const DUPLICATE_LICENSE=4;public const INVALID_LICENSE=8;public const MAINTENANCE=32;}class LicenseState{public const LICSTATE_NONE=0;public const LICSTATE_ACCOUNTING=0x00000008;public const LICSTATE_CASCADE_SERVER=0x00000010;public const LICSTATE_CASCADE_CLIENT=0x00000020;public const LICSTATE_HOSTING=0x00000040;public const LICSTATE_ESHOP=0x00000100;public const LICSTATE_XMLIMPORT=0x00000200;public const LICSTATE_XMLEXPORT=0x00000400;public const LICSTATE_STATISTICS=0x00000800;public const LICSTATE_NPSSTATISTICS=0x00001000;public const LICSTATE_LABELCF=0x00002000;public const LICSTATE_CONTROLLING=0x00004000;public const LICSTATE_CONTRACTS=0x00008000;public const LICSTATE_MAINTENANCE=0x00010000;}class AuthApiP extends AuthApi{public function CheckServerState(string & $serverApiUrl,string & $error):bool{$result=false;$serverApiUrl='';$error='';$database=$this->getContainer()->get('database');if($database){$cursor=null;$sql="SELECT sValue as ServerState,"." (SELECT sValue FROM ACCSYSTEM WHERE sSection='System' AND sKey='LicenseState') as LicenseState,"." (SELECT sValue FROM ACCSYSTEM WHERE sSection='Restapi' AND sKey='ServerUrl') as ServerApiUrl"." FROM ACCSYSTEM WHERE sSection='System' AND sKey='ServerStateEx'";$row=$database->FetchFirst($cursor,$sql);if($row!==false){$serverState=(int)($row['ServerState']);$licenseState=(int)($row['LicenseState']);$serverApiUrl=$row['ServerApiUrl'];if(($serverState &(ServerState::EXPIRED|ServerState::DUPLICATE_LICENSE|ServerState::INVALID_LICENSE|ServerState::MAINTENANCE))==0){if($licenseState & LicenseState::LICSTATE_CASCADE_SERVER){if(!empty($serverApiUrl)){$result=true;}else{$error='Missing server api base url';}}else{$error='Invalid license state ('.$licenseState.')';}}else{$error='Invalid server state ('.$serverState.')';}}$database->FetchClose($cursor);}else{$error='Database error';}if(!$result&&empty($error)){$error="Server error";}if(!empty($error)){$this->getLogger()->error($error);$result=false;}return $result;}public function getClientApiUrl($user){global $apiSettings;$clientApiUrl='';if(!empty($user->restApiClientUuid)){$restApiClientUuidHex=ApiUuid::toHex($user->restApiClientUuid);$database=$this->getContainer()->get('database');if($database){$cursor=null;$sql='SELECT RestApiUrl FROM ACCSUBMITTERCLIENTS WHERE sUUID=?';$params=[$restApiClientUuidHex];$row=$database->FetchFirst($cursor,$sql,$params);if($row!==false){$clientApiUrl=$row['RestApiUrl'];}}if(empty($clientApiUrl)){if(isset($apiSettings[$restApiClientUuidHex]['apiBasePath']))$clientApiUrl=$apiSettings[$restApiClientUuidHex]['apiBasePath'];elseif(isset($apiSettings[strtolower($restApiClientUuidHex)]['apiBasePath']))$clientApiUrl=$apiSettings[strtolower($restApiClientUuidHex)]['apiBasePath'];elseif(isset($apiSettings[$user->restApiClientUuid]['apiBasePath']))$clientApiUrl=$apiSettings[$user->restApiClientUuid]['apiBasePath'];elseif(isset($apiSettings[strtoupper($user->restApiClientUuid)]['apiBasePath']))$clientApiUrl=$apiSettings[strtoupper($user->restApiClientUuid)]['apiBasePath'];if(empty($clientApiUrl))$clientApiUrl=$this->getContainer()->get('server_api_url');}}return $clientApiUrl;}}