387 lines
11 KiB
PHP
387 lines
11 KiB
PHP
<?php
|
|
|
|
$REDIS_HOST='redis-server';
|
|
$SHARED_DIR = "/var/tmp/shared";
|
|
$INTERFACE = "directory"; // redis OR directory
|
|
//$INTERFACE = "redis";
|
|
|
|
function ping_redis() {
|
|
|
|
global $REDIS_HOST;
|
|
|
|
$redis = new Redis();
|
|
$redis->connect($REDIS_HOST);
|
|
if ($redis->ping()) return true;
|
|
else return false;
|
|
}
|
|
|
|
function check_install() { // is install in progress
|
|
|
|
$arr = check_request("install");
|
|
if (!empty($arr)) return "install"; // install in progress
|
|
else return "";
|
|
}
|
|
|
|
function check_deploy($application) { // is an application deploy in progress
|
|
|
|
$arr = check_request("deployment");
|
|
if (!empty($arr)) return "deploy-".$application; // deploy in progress install has started
|
|
else {
|
|
$arr = check_response("deploy-".$application); // application install in progress PID exists
|
|
if (!empty($arr)) return $arr;
|
|
else return 0;
|
|
}
|
|
}
|
|
|
|
function check_redis($group="web_out", $key="") {
|
|
|
|
global $REDIS_HOST;
|
|
|
|
$redis = new Redis();
|
|
$redis->connect($REDIS_HOST);
|
|
if ($redis->ping()) {
|
|
$members = $redis->sMembers($group); // redis-cli -h redis-server smembers $group
|
|
//print_r($members);
|
|
|
|
$result = array();
|
|
foreach ($members as $member) {
|
|
if ($key!="" && $member!=$key) continue; // find a specific key in a group
|
|
|
|
$value = $redis->get($member);
|
|
$json_data = base64_decode($value);
|
|
$data = json_decode($json_data,true);
|
|
if ($data === null) {
|
|
echo "JSON read error...";
|
|
// TODO json error
|
|
}
|
|
else {
|
|
$result["$member"] = $data;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
function redis_get($key) {
|
|
|
|
global $REDIS_HOST;
|
|
|
|
$redis = new Redis();
|
|
$redis->connect($REDIS_HOST);
|
|
if ($redis->ping()) {
|
|
//$arList = $redis->keys("*"); // ? redis-cli -h redis-server keys "*"
|
|
//echo "Stored keys in redis:";
|
|
//print_r($arList);
|
|
if ($redis->exists($key)) {
|
|
$value = $redis->get($key);
|
|
//redis-cli -h redis-server get $key
|
|
return base64_decode($value);
|
|
} else {
|
|
echo "Key does not exist: $key";
|
|
// TODO
|
|
}
|
|
}
|
|
}
|
|
|
|
function redis_set($key, $value) {
|
|
|
|
global $REDIS_HOST;
|
|
|
|
$redis = new Redis();
|
|
$redis->connect($REDIS_HOST);
|
|
// $redis->auth('password');
|
|
if ($redis->ping()) {
|
|
if (!$redis->exists($key)) {
|
|
//redis-cli -h redis set $key "$value"
|
|
//redis-cli -h redis sadd web_in $key
|
|
//redis-cli -h redis smembers web_in
|
|
$redis->set($key, base64_encode($value));
|
|
$redis->sAdd('web_in', $key);
|
|
} else {
|
|
//echo "Key already exist: $key";
|
|
}
|
|
}
|
|
}
|
|
|
|
function redis_remove($key) {
|
|
|
|
global $REDIS_HOST;
|
|
|
|
$redis = new Redis();
|
|
$redis->connect($REDIS_HOST);
|
|
// $redis->auth('password');
|
|
if ($redis->ping()) {
|
|
//redis-cli -h redis srem web_out $key
|
|
//redis-cli -h redis del $key
|
|
$redis->srem("web_out", $key);
|
|
$redis->del($key);
|
|
}
|
|
}
|
|
|
|
function get_vpn_url($domain,$passkey) {
|
|
|
|
$date = gmdate("Ymd");
|
|
$hash = hash("sha256",$passkey*$date);
|
|
$url = $domain."/".$passkey."/secret";
|
|
return $url;
|
|
}
|
|
|
|
function show_service($name, $containers) {
|
|
$str = '<table id="service_'.$name.'">';
|
|
$str .= "<tr><th>{$name}</th></tr>";
|
|
$containers = trim($containers);
|
|
$arr = explode("|",$containers);
|
|
foreach ($arr as $container) {
|
|
$c_arr = explode("#",$container);
|
|
$str .= "<tr><td>".$c_arr[0]."</td><td>".$c_arr[1]."</td><td>".$c_arr[2]."</td><td>RESTART</td></tr>";
|
|
}
|
|
$str .= '</table>';
|
|
|
|
echo $str;
|
|
}
|
|
|
|
function show_service_update($name, $update, $uptodate, $error) {
|
|
|
|
$str = "";
|
|
$update = trim($update);
|
|
if (!empty($update)) {
|
|
$arr = explode(" ",$update);
|
|
foreach ($arr as $container) {
|
|
$str .= "<tr><td> </td><td>".$container."</td><td><div id=\"status_".$name."\">UPDATE AVAILABLE</div></td><td> </td></tr>";
|
|
}
|
|
$update_str = "<a href=\"javascript:void(0)\" onclick=\"upgrade('{$name}')\">UPDATE</a>";
|
|
}
|
|
|
|
$uptodate = trim($uptodate);
|
|
if (!empty($uptodate)) {
|
|
$arr = explode(" ",$uptodate);
|
|
foreach ($arr as $container) {
|
|
$str .= "<tr><td> </td><td>".$container."</td><td><div id=\"status_".$name."\">Already up to date</div></td><td> </td></tr>";
|
|
}
|
|
$update_str = "<a href=\"javacript:void(0)\" onclick=\"upgrade('{$name}')\">FORCE UPDATE</a>";
|
|
}
|
|
|
|
$error = trim($error);
|
|
if (!empty($error)) {
|
|
$arr = explode(" ",$error);
|
|
foreach ($arr as $container) {
|
|
//$str .= "<tr><td> </td><td>".$container."</td><td>N/A</td><td></td></tr>";
|
|
$str .= "<tr><td> </td><td>".$container."</td><td><div id=\"status_".$name."\">N/A</div></td><td> </td></tr>";
|
|
}
|
|
$update_str = "<a href=\"javascript:void(0)\" onclick=\"upgrade('{$name}')\">TRY UPDATE</a>";
|
|
}
|
|
|
|
echo '<table id="update_'.$name.'">';
|
|
echo "<tr><th>{$name}</th><th> </th><th> </th><th>{$update_str}</th></tr>";
|
|
echo $str;
|
|
echo '</table>';
|
|
}
|
|
|
|
// not in use
|
|
function put_install_envs() {
|
|
|
|
// TEMP
|
|
putenv('HOME=/home/hael');
|
|
putenv('USER=hael');
|
|
|
|
putenv('DOCKER_REGISTRY_URL='.$_POST["registry"]);
|
|
|
|
putenv('SMARTHOST_PROXY='.$_POST["smarthost"]);
|
|
if ($_POST["smarthost"]=="Y") {
|
|
if ($_POST["domain"]=="") $_POST["domain"] = "localhost";
|
|
putenv('DOMAIN='.$_POST["domain"]);
|
|
# if not FQDN
|
|
$arr = explode(".",$_POST["DOMAIN"]);
|
|
if (count($arr)==1) {
|
|
echo "Warning! It seems DOMAAIN is not an FQDN. Self-signed certificate will be created only.";
|
|
putenv('SELF_SIGNED_CERTIFICATE=true');
|
|
}
|
|
|
|
}
|
|
|
|
putenv('LOCAL_PROXY='.$_POST["localproxy"]);
|
|
putenv('VPN_PROXY='.$_POST["vpn"]);
|
|
if ($_POST["vpn"]=="yes") {
|
|
putenv('VPN_DOMAIN='.$_POST["vpn_domain"]);
|
|
putenv('VPN_PASS='.$_POST["vpn_pass"]);
|
|
|
|
putenv('LETSENCRYPT_MAIL='.$_POST["letsencrypt_mail"]);
|
|
putenv('LETSENCRYPT_SERVERNAME='.$_POST["letsencrypt_servername"]);
|
|
}
|
|
putenv('CRON='.$_POST["cron"]);
|
|
putenv('DISCOVERY='.$_POST["discovery"]);
|
|
|
|
if ($_POST["discovery"]=="yes") {
|
|
if ($_POST["DISCOVERY_DIR"] == "" ) $_POST["DISCOVERY_DIR"]="/usr/local/bin/";
|
|
if (substr($_POST["DISCOVERY_DIR"],0,1)!="/") {
|
|
echo "The path must be absolute, for example /usr/local/bin/. Please type it again.";
|
|
exit;
|
|
}
|
|
if ($_POST["DISCOVERY_CONFIG_FILE"] == "" ) $_POST["DISCOVERY_CONFIG_FILE"]="discovery.conf";
|
|
putenv('DISCOVERY_DIR='.$_POST["discovery_dir"]);
|
|
putenv('DISCOVERY_CONFIG_FILE='.$_POST["discovery_config_file"]);
|
|
}
|
|
|
|
|
|
putenv('ADDITIONALS='.$_POST["additionals"]);
|
|
if ($_POST["additionals"]=="yes") {
|
|
if ($_POST["SERVICE_DIR"] == "" ) $_POST["SERVICE_DIR"]="/etc/user/config/services";
|
|
putenv('SERVICE_DIR='.$_POST["service_dir"]);
|
|
|
|
putenv('NEXTCLOUD='.$_POST["nextcloud"]);
|
|
putenv('BITWARDEN='.$_POST["bitwarden"]);
|
|
putenv('GUACAMOLE='.$_POST["guacamole"]);
|
|
putenv('SMTP='.$_POST["smtp_server"]);
|
|
putenv('ROUNDCUBE='.$_POST["roundcube"]);
|
|
|
|
if ($_POST["nextcloud"]=="yes") {
|
|
putenv('NEXTCLOUD_DOMAIN='.$_POST["nextcloud_domain"]);
|
|
putenv('NEXTCLOUD_USERNAME='.$_POST["nextcloud_username"]);
|
|
putenv('NEXTCLOUD_PASSWORD='.$_POST["nextcloud_password"]);
|
|
}
|
|
if ($_POST["bitwarden"]=="yes") {
|
|
putenv('BITWARDEN_DOMAIN='.$_POST["bitwarden_domain"]);
|
|
putenv('SMTP_SERVER='.$_POST["bitwarden_smtp_server"]);
|
|
putenv('SMTP_HOST='.$_POST["bitwarden_smtp_host"]);
|
|
putenv('SMTP_PORT='.$_POST["bitwarden_smtp_port"]);
|
|
putenv('SMTP_SECURITY='.$_POST["bitwarden_smtp_security"]);
|
|
putenv('SMTP_FROM='.$_POST["bitwarden_smtp_from"]);
|
|
putenv('SMTP_USERNAME='.$_POST["bitwarden_smtp_username"]);
|
|
putenv('SMTP_PASSWORD='.$_POST["bitwarden_smtp_password"]);
|
|
putenv('DOMAINS_WHITELIST='.$_POST["bitwarden_domains_whitelist"]);
|
|
}
|
|
if ($_POST["guacamole"]=="yes") {
|
|
putenv('GUACAMOLE_DOMAIN='.$_POST["bitwarden_domain"]);
|
|
putenv('GUACAMOLE_ADMIN_NAME='.$_POST["bitwarden_smtp_username"]);
|
|
putenv('GUACAMOLE_ADMIN_PASSWORD='.$_POST["bitwarden_smtp_password"]);
|
|
if ($_POST["bitwarden_totp"]=="yes") putenv('TOTP_USE=true');
|
|
if ($_POST["bitwarden_ban_duration"]=="") $_POST["bitwarden_ban_duration"]="5";
|
|
putenv('BAN_DURATION='.$_POST["bitwarden_ban_duration"]);
|
|
}
|
|
if ($_POST["roundcube"]=="yes") {
|
|
if ($_POST["roundcube_imap_port"]=="") $_POST["roundcube_imap_port"]="143";
|
|
if ($_POST["roundcube_smtp_port"]=="") $_POST["roundcube_smtp_port"]="25";
|
|
if ($_POST["roundcube_upload"]=="") $_POST["roundcube_smtp_port"]="50M";
|
|
putenv('ROUNDCUBE_IMAP_HOST='.$_POST["roundcube_imap_host"]);
|
|
putenv('ROUNDCUBE_IMAP_PORT='.$_POST["roundcube_imap_port"]);
|
|
putenv('ROUNDCUBE_SMTP_HOST='.$_POST["roundcube_smtp_host"]);
|
|
putenv('ROUNDCUBE_SMTP_PORT='.$_POST["roundcube_smtp_port"]);
|
|
putenv('ROUNDCUBE_UPLOAD_MAX_FILESIZE='.$_POST["roundcube_upload"]);
|
|
putenv('ROUNDCUBE_DOMAIN='.$_POST["roundcube_domain"]);
|
|
}
|
|
}
|
|
}
|
|
|
|
function set_output($op,$output) {
|
|
|
|
global $INTERFACE, $SHARED_DIR;
|
|
|
|
if ($INTERFACE=="redis") {
|
|
redis_set($op,$output);
|
|
}
|
|
else {
|
|
if (file_exists($SHARED_DIR."/input/".$op.".json")) return false;
|
|
else file_put_contents($SHARED_DIR."/input/".$op.".json",$output);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function check_files($dir,$key) {
|
|
|
|
global $SHARED_DIR;
|
|
|
|
$input_file = $SHARED_DIR."/{$dir}/".$key.".json";
|
|
if (file_exists($input_file)) {
|
|
$json_data = file_get_contents($input_file);
|
|
$data = json_decode($json_data,true);
|
|
if ($data === null) {
|
|
echo "JSON read error...";
|
|
// TODO json error
|
|
}
|
|
else {
|
|
$result["$key"] = $data;
|
|
}
|
|
}
|
|
else $result = "";
|
|
|
|
return $result;
|
|
}
|
|
|
|
function show_letsencrypt($letsencrypt, $domain) {
|
|
if (!empty($letsencrypt[$domain])) {
|
|
echo "LETSENCRYPT: ".$letsencrypt[$domain]["status"]." - ".$letsencrypt[$domain]["date"];
|
|
echo " - <a href=\"letsencrypt_log.php?domain={$domain}\" target=\"_blank\">LOG</a>";
|
|
if (date("Y-m-d",time()-60*24*3600)>substr($letsencrypt[$domain]["date"],0,10) || $letsencrypt[$domain]["status"]=="failed") {
|
|
echo " - <a href=\"#\" onclick=\"request_letsencrypt('{$domain}')\">Request new certificate</a>";
|
|
}
|
|
echo "<br><br>";
|
|
}
|
|
else echo "LETSENCRYPT in progress for {$domain}.<script>check_letsencrypt('{$domain}')</script>";
|
|
}
|
|
|
|
function check_letsencrypt() {
|
|
|
|
global $SHARED_DIR;
|
|
|
|
$input_file = $SHARED_DIR."/output/letsencrypt.json";
|
|
if (file_exists($input_file)) {
|
|
$json_data = file_get_contents($input_file);
|
|
$data = json_decode($json_data,true);
|
|
if ($data === null) {
|
|
return "ERROR";
|
|
}
|
|
else {
|
|
foreach ($data as $domain => $domain_data) {
|
|
$result[$domain] = $domain_data;
|
|
}
|
|
}
|
|
}
|
|
else $result = "";
|
|
|
|
return $result;
|
|
}
|
|
|
|
function check_request($key="") {
|
|
|
|
global $INTERFACE;
|
|
|
|
if ($INTERFACE=="redis") {
|
|
$arr = check_redis("web_in",$key);
|
|
}
|
|
else {
|
|
$arr = check_files("input",$key);
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
function check_response($key="") {
|
|
|
|
global $INTERFACE;
|
|
|
|
if ($INTERFACE=="redis") {
|
|
$arr = check_redis("web_out",$key);
|
|
}
|
|
else {
|
|
$arr = check_files("output",$key);
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
function remove_response($key) {
|
|
|
|
global $INTERFACE, $SHARED_DIR;
|
|
|
|
if ($INTERFACE=="redis") {
|
|
redis_remove("$key");
|
|
}
|
|
else {
|
|
copy($SHARED_DIR."/output/".$key.".json",$SHARED_DIR."/".$key.".json"); // DEBUG - last json
|
|
if (!unlink($SHARED_DIR."/output/".$key.".json")) echo "UNLINK ERROR";
|
|
}
|
|
}
|
|
|
|
?>
|