-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.php
149 lines (128 loc) · 3.52 KB
/
util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// backup logpath
$logPath = "td-agent-bit-backup.conf";
// logPath for local
// $tdBitConf = "td-agent-bit.conf";
// logPath for ami
// $tdBitConf = "/etc/td-agent-bit/td-agent-bit.conf";
// logPath for docker
$tdBitConf = "/reactivesearch-data/td-agent-bit.conf";
// env file path for local
// $filePath = "env.sample";
// env file path for AMI
// $filePath = "/etc/systemd/system/arc.env";
// env file path for docker images
$filePath = "/reactivesearch-data/.env";
function getEnvVars() {
global $filePath;
$file = fopen($filePath, "r");
$fileLines = array();
while(!feof($file)) {
$result = fgets($file);
if (!empty($result)) {
$splitResult = explode("=", $result);
$fileLines[$splitResult[0]] = trim($splitResult[1]);
}
}
fclose($file);
return $fileLines;
}
function upsertEnvVars($data) {
$finalData = getEnvVars();
foreach ($data as $key => $val) {
$finalData[$key] = $val;
}
$fileContent = "";
foreach ($finalData as $key => $value) {
$trimValue = trim($value);
if (!empty($trimValue)) {
$fileContent = $fileContent.$key."=".$value."\n";
}
}
global $filePath;
$file = fopen($filePath, "w") or die("Unable to open file!");
fwrite($file, $fileContent);
}
function splitURL($url) {
$username = "";
$password = "";
$tls = "On";
$host = "";
$port = "443";
$url = rtrim($url, "/");
$hostSplit = explode("://", $url);
if (count($hostSplit) === 2) {
$host = $hostSplit[1];
if ($hostSplit[0] == "http") {
$tls = "Off";
$port = "80";
}
} else {
$host = $hostSplit[0];
$tls = "Off";
$port = "80";
}
$authSplit = explode( "@", $host);
if (count($authSplit) === 2) {
$credentialSplit = explode(":", $authSplit[0]);
$username = $credentialSplit[0];
$password = $credentialSplit[1];
$host = $authSplit[1];
}
$portSplit = explode(":", $host);
if (count($portSplit) === 2) {
$port = $portSplit[1];
$host = $portSplit[0];
}
return [
$host,
$port,
$tls,
$username,
$password
];
}
function upsertLogFile($data) {
global $logPath;
global $tdBitConf;
$url = $data["ES_CLUSTER_URL"];
if (isset($url) && $url != "") {
$contents = file_get_contents($logPath);
$splitRes = splitURL($url);
$host = $splitRes[0];
$port = $splitRes[1];
$tls = $splitRes[2];
$username = $splitRes[3];
$password = $splitRes[4];
$contents = str_replace('__ES_URL__', $host, $contents);
$contents = str_replace('__ES_PORT__', $port, $contents);
$contents = str_replace('__ES_TLS__', $tls, $contents);
if (trim($username) !== "") {
$contents = str_replace('__ES_USERNAME__', $username, $contents);
} else {
$contents = str_replace('HTTP_User __ES_USERNAME__', '', $contents);
}
if (trim($password) !== "") {
$contents = str_replace('__ES_PASSWORD__', $password, $contents);
} else {
$contents = str_replace('HTTP_Passwd __ES_PASSWORD__', '', $contents);
}
$res = file_put_contents($tdBitConf, trim($contents));
}
}
function checkAuth() {
$currentTime = time();
if (!isset($_SESSION["password"]) || !isset($_SESSION["username"]) || !isset($_SESSION["EXPIRES"]) || (time() - $_SESSION['EXPIRES'] > 900)) {
logout("?error=Please login");
header("Location: index.php?error=Please login");
}
}
function logout($data="") {
unset($_SESSION["username"]);
unset($_SESSION["password"]);
unset($_SESSION["EXPIRES"]);
session_destroy();
session_unset();
header("Location: index.php".$data);
}
?>