126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?php
|
|
|
|
class Vodia_Api {
|
|
private $log;
|
|
private $baseurl;
|
|
private $admin_user;
|
|
private $admin_pass;
|
|
private $session_name;
|
|
private $session_id;
|
|
|
|
|
|
public function __construct($baseurl, $admin_user, $admin_pass) {
|
|
$this->log = mfLoghandler::singleton();
|
|
|
|
$this->baseurl = rtrim($baseurl, '/');
|
|
$this->admin_user = $admin_user;
|
|
$this->admin_pass = $admin_pass;
|
|
|
|
if(!$this->baseurl || !$this->admin_user || !$this->admin_pass) {
|
|
throw new Exception("Invalid Arguments");
|
|
}
|
|
}
|
|
|
|
private function _authenticate($domain) {
|
|
$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,
|
|
"method" => "POST",
|
|
"headers" => [
|
|
"Accept: application/json",
|
|
"Content-Type: application/json",
|
|
],
|
|
"content" => json_encode([
|
|
"name" => $this->session_name,
|
|
"value" => "$this->admin_user " . md5($this->admin_pass),
|
|
"admin" => true,
|
|
"domain" => $domain,
|
|
]),
|
|
]];
|
|
|
|
$url = $this->baseurl . VODIA_API_EP_SYSTEM_SESSION;
|
|
$ctx = stream_context_create($ctx_options);
|
|
$output = file_get_contents($url, false, $ctx);
|
|
|
|
$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;
|
|
}
|
|
|
|
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);
|
|
|
|
$ctx_options = [
|
|
"http" => [
|
|
"ignore_errors" => true,
|
|
"method" => "POST",
|
|
"header" => [
|
|
"Cookie: session=".$this->session_id,
|
|
"Accept: application/json",
|
|
"Content-Type: application/json",
|
|
],
|
|
"content" => json_encode($user_settings),
|
|
]
|
|
];
|
|
|
|
$ctx = stream_context_create($ctx_options);
|
|
file_get_contents($url, false, $ctx);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getUsersetting($domain, $user, $key = false) {
|
|
$settings = $this->_get(VODIA_API_EP_GET_USER_SETTINGS, $domain, $user);
|
|
|
|
return $key ? ($settings[$key] ?? false) : $settings;
|
|
}
|
|
|
|
public function getActiveCalls($domain, $user) {
|
|
return $this->_get(VODIA_API_EP_GET_USER_CALLS, $domain, $user);
|
|
}
|
|
|
|
} |