added new session handling
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
class Vodia_Api {
|
class Vodia_Api {
|
||||||
private $log;
|
private $log;
|
||||||
|
|
||||||
private $baseurl;
|
private $baseurl;
|
||||||
private $admin_user;
|
private $admin_user;
|
||||||
private $admin_pass;
|
private $admin_pass;
|
||||||
@@ -23,11 +22,14 @@ class Vodia_Api {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function _authenticate($domain) {
|
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";
|
$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" => [
|
$ctx_options = ["http" => [
|
||||||
"ignore_errors" => true,
|
"ignore_errors" => true,
|
||||||
@@ -38,41 +40,56 @@ class Vodia_Api {
|
|||||||
],
|
],
|
||||||
"content" => json_encode([
|
"content" => json_encode([
|
||||||
"name" => $this->session_name,
|
"name" => $this->session_name,
|
||||||
"value" => "$this->admin_user ".md5($this->admin_pass),
|
"value" => "$this->admin_user " . md5($this->admin_pass),
|
||||||
"admin" => true,
|
"admin" => true,
|
||||||
"domain" => $domain,
|
"domain" => $domain,
|
||||||
]),
|
]),
|
||||||
//"header" => $headers,
|
|
||||||
]];
|
]];
|
||||||
|
|
||||||
//$this->log->debug(__METHOD__.": authenticating to $url for domain $domain, user $this->admin_user");
|
$url = $this->baseurl . VODIA_API_EP_SYSTEM_SESSION;
|
||||||
//$this->log->debug(__METHOD__.": ".print_r($ctx_options, true));
|
|
||||||
|
|
||||||
|
|
||||||
$ctx = stream_context_create($ctx_options);
|
$ctx = stream_context_create($ctx_options);
|
||||||
$output = file_get_contents($url, false, $ctx);
|
$output = file_get_contents($url, false, $ctx);
|
||||||
//$this->log->debug(__METHOD__.": auth output: $output");
|
|
||||||
|
|
||||||
$m = [];
|
$session_id = preg_match('/^"([^"]+)"$/', $output, $matches) ? ($matches[1] ?? null) : null;
|
||||||
if(preg_match('/^"([^"]+)"$/', $output, $m)) {
|
|
||||||
if($m[1]) {
|
|
||||||
$session_id = $m[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$session_id) {
|
if (!$session_id) {
|
||||||
throw new Exception("Authentication failed for domain $domain");
|
throw new Exception("Authentication failed for domain $domain");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$session->value($session_id);
|
||||||
|
$session->save();
|
||||||
|
|
||||||
$this->session_id = $session_id;
|
$this->session_id = $session_id;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUsersettings($domain, $user, Array $user_settings) {
|
private function _get($endpoint, $domain, $user): ?array {
|
||||||
if(!$this->session_id) {
|
if (!$this->session_id) {
|
||||||
$this->_authenticate($domain);
|
$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 = $this->baseurl.VODIA_API_EP_POST_USER_SETTINGS;
|
||||||
$url = str_replace("{DOMAIN}", $domain, $url);
|
$url = str_replace("{DOMAIN}", $domain, $url);
|
||||||
$url = str_replace("{EXT}", $user, $url);
|
$url = str_replace("{EXT}", $user, $url);
|
||||||
@@ -91,86 +108,19 @@ class Vodia_Api {
|
|||||||
];
|
];
|
||||||
|
|
||||||
$ctx = stream_context_create($ctx_options);
|
$ctx = stream_context_create($ctx_options);
|
||||||
$output = file_get_contents($url, false, $ctx);
|
file_get_contents($url, false, $ctx);
|
||||||
|
|
||||||
//$this->log->debug(__METHOD__.": output: $output");
|
|
||||||
|
|
||||||
return true;
|
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) {
|
public function getUsersetting($domain, $user, $key = false) {
|
||||||
if(!$this->session_id) {
|
$settings = $this->_get(VODIA_API_EP_GET_USER_SETTINGS, $domain, $user);
|
||||||
$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;
|
|
||||||
|
|
||||||
|
return $key ? ($settings[$key] ?? false) : $settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getActiveCalls($domain, $user) {
|
public function getActiveCalls($domain, $user) {
|
||||||
if(!$this->session_id) {
|
return $this->_get(VODIA_API_EP_GET_USER_CALLS, $domain, $user);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user