added new session handling

This commit is contained in:
2025-08-12 16:17:04 +02:00
parent 8045a80b50
commit b4a84c2340

View File

@@ -2,7 +2,6 @@
class Vodia_Api {
private $log;
private $baseurl;
private $admin_user;
private $admin_pass;
@@ -23,11 +22,14 @@ class Vodia_Api {
}
private function _authenticate($domain) {
$url = $this->baseurl.VODIA_API_EP_SYSTEM_SESSION;
$session_id = false;
//$this->session_name = sprintf("%x", crc32(uniqid("thetool", true)));
$this->session_name = "auth";
$session_key = "vodia.$domain.auth.sessionid";
$session = new mfConfig($session_key);
if ($session->value() && (time() - $session->edit) < 3000) {
$this->session_id = $session->value();
return true;
}
$ctx_options = ["http" => [
"ignore_errors" => true,
@@ -42,37 +44,52 @@ class Vodia_Api {
"admin" => true,
"domain" => $domain,
]),
//"header" => $headers,
]];
//$this->log->debug(__METHOD__.": authenticating to $url for domain $domain, user $this->admin_user");
//$this->log->debug(__METHOD__.": ".print_r($ctx_options, true));
$url = $this->baseurl . VODIA_API_EP_SYSTEM_SESSION;
$ctx = stream_context_create($ctx_options);
$output = file_get_contents($url, false, $ctx);
//$this->log->debug(__METHOD__.": auth output: $output");
$m = [];
if(preg_match('/^"([^"]+)"$/', $output, $m)) {
if($m[1]) {
$session_id = $m[1];
}
}
$session_id = preg_match('/^"([^"]+)"$/', $output, $matches) ? ($matches[1] ?? null) : null;
if (!$session_id) {
throw new Exception("Authentication failed for domain $domain");
}
$session->value($session_id);
$session->save();
$this->session_id = $session_id;
return true;
}
public function setUsersettings($domain, $user, Array $user_settings) {
private function _get($endpoint, $domain, $user): ?array {
if (!$this->session_id) {
$this->_authenticate($domain);
}
$url = str_replace(['{DOMAIN}', '{EXT}'], [$domain, $user], $this->baseurl . $endpoint);
$context = stream_context_create([
'http' => [
'ignore_errors' => true,
'method' => 'GET',
'header' => [
'Cookie: session=' . $this->session_id,
'Accept: application/json',
'Content-Type: application/json',
],
]
]);
$output = file_get_contents($url, false, $context);
return json_decode($output, true);
}
public function setUsersettings($domain, $user, Array $user_settings) {
if(!$this->session_id) $this->_authenticate($domain);
$url = $this->baseurl.VODIA_API_EP_POST_USER_SETTINGS;
$url = str_replace("{DOMAIN}", $domain, $url);
$url = str_replace("{EXT}", $user, $url);
@@ -91,86 +108,19 @@ class Vodia_Api {
];
$ctx = stream_context_create($ctx_options);
$output = file_get_contents($url, false, $ctx);
//$this->log->debug(__METHOD__.": output: $output");
file_get_contents($url, false, $ctx);
return true;
}
/**
* Get user settings for a specific user in a domain.
*
* @param string $domain The domain to query.
* @param string $user The user extension to query.
* @param string|bool $key Optional. If provided, only the value for this key will be returned.
* @return mixed Returns the user settings as an associative Array or a specific key's value as string if $key is provided or false if $key is not available.
*/
public function getUsersetting($domain, $user, $key = false) {
if(!$this->session_id) {
$this->_authenticate($domain);
}
$url = $this->baseurl.VODIA_API_EP_GET_USER_SETTINGS;
$url = str_replace("{DOMAIN}", $domain, $url);
$url = str_replace("{EXT}", $user, $url);
$ctx_options = [
"http" => [
"ignore_errors" => true,
"method" => "GET",
"header" => [
"Cookie: session=".$this->session_id,
"Accept: application/json",
"Content-Type: application/json",
],
]
];
$ctx = stream_context_create($ctx_options);
$output = file_get_contents($url, false, $ctx);
//$this->log->debug(__METHOD__.": output: $output");
$user_settings = json_decode($output, true);
if($key) {
if(array_key_exists($key, $user_settings)) {
return $user_settings[$key];
} else {
return false;
}
}
return $user_settings;
$settings = $this->_get(VODIA_API_EP_GET_USER_SETTINGS, $domain, $user);
return $key ? ($settings[$key] ?? false) : $settings;
}
public function getActiveCalls($domain, $user) {
if(!$this->session_id) {
$this->_authenticate($domain);
}
$url = $this->baseurl.VODIA_API_EP_GET_USER_CALLS;
$url = str_replace("{DOMAIN}", $domain, $url);
$url = str_replace("{EXT}", $user, $url);
$ctx_options = [
"http" => [
"ignore_errors" => true,
"method" => "GET",
"header" => [
"Cookie: session=".$this->session_id,
"Accept: application/json",
"Content-Type: application/json",
],
]
];
$ctx = stream_context_create($ctx_options);
$output = file_get_contents($url, false, $ctx);
return json_decode($output, true);
return $this->_get(VODIA_API_EP_GET_USER_CALLS, $domain, $user);
}
}