Added Vodia Identity Switcher for Xinon Users
This commit is contained in:
148
lib/Vodia/Api.php
Normal file
148
lib/Vodia/Api.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?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) {
|
||||
$url = $this->baseurl.VODIA_API_EP_SYSTEM_SESSION;
|
||||
|
||||
$session_id = false;
|
||||
//$this->session_name = sprintf("%x", crc32(uniqid("thetool", true)));
|
||||
$this->session_name = "auth";
|
||||
|
||||
$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,
|
||||
]),
|
||||
//"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));
|
||||
|
||||
|
||||
$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];
|
||||
}
|
||||
}
|
||||
|
||||
if(!$session_id) {
|
||||
throw new Exception("Authentication failed for domain $domain");
|
||||
}
|
||||
|
||||
$this->session_id = $session_id;
|
||||
return 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);
|
||||
$output = file_get_contents($url, false, $ctx);
|
||||
|
||||
$this->log->debug(__METHOD__.": output: $output");
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user