From b4a84c2340fe9f8af8449f588b52e97dde575099 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Tue, 12 Aug 2025 16:17:04 +0200 Subject: [PATCH] added new session handling --- lib/Vodia/Api.php | 134 +++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 92 deletions(-) diff --git a/lib/Vodia/Api.php b/lib/Vodia/Api.php index baaa0a5fe..6dfe73775 100644 --- a/lib/Vodia/Api.php +++ b/lib/Vodia/Api.php @@ -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, @@ -38,41 +40,56 @@ class Vodia_Api { ], "content" => json_encode([ "name" => $this->session_name, - "value" => "$this->admin_user ".md5($this->admin_pass), + "value" => "$this->admin_user " . md5($this->admin_pass), "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) { + 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) { - if(!$this->session_id) { + 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); } } \ No newline at end of file