-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
268 lines (233 loc) · 8.61 KB
/
index.php
File metadata and controls
268 lines (233 loc) · 8.61 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
session_start();
// Load configuration
$config = parse_ini_file(__DIR__ . '/config.ini', true);
if (!$config) {
die('Configuration file not found or invalid');
}
define('MATRIX_SERVER', $config['matrix']['server']);
define('MATRIX_DOMAIN', $config['matrix']['domain']);
// Check if admin is logged in
$isLoggedIn = isset($_SESSION['admin_token']) && !empty($_SESSION['admin_token']);
// Handle logout
if (isset($_GET['logout'])) {
session_destroy();
header('Location: index.php');
exit;
}
// Handle login
if (($_POST['action'] ?? '') === 'login' && !$isLoggedIn) {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if ($username && $password) {
$loginData = [
'type' => 'm.login.password',
'user' => $username,
'password' => $password
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MATRIX_SERVER . '/_matrix/client/r0/login');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($loginData));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$data = json_decode($response, true);
$token = $data['access_token'] ?? '';
// Check if user is admin
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MATRIX_SERVER . '/_synapse/admin/v1/users/@' . $username . ':' . MATRIX_DOMAIN . '/admin');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $token]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$adminResponse = curl_exec($ch);
$adminHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($adminHttpCode === 200) {
$adminData = json_decode($adminResponse, true);
if ($adminData['admin'] === true) {
$_SESSION['admin_token'] = $token;
$_SESSION['admin_user'] = '@' . $username . ':' . MATRIX_DOMAIN;
header('Location: index.php');
exit;
} else {
$error = 'Access denied: Admin privileges required';
}
} else {
$error = 'Failed to verify admin status';
}
} else {
$error = 'Invalid username or password';
}
} else {
$error = 'Please enter username and password';
}
}
// Handle user creation
if (($_POST['action'] ?? '') === 'create_user' && $isLoggedIn) {
$newUsername = $_POST['new_username'] ?? '';
$newPassword = $_POST['new_password'] ?? '';
$isAdmin = isset($_POST['is_admin']) ? true : false;
if ($newUsername && $newPassword) {
$userData = [
'password' => $newPassword,
'admin' => $isAdmin
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MATRIX_SERVER . '/_synapse/admin/v2/users/@' . $newUsername . ':' . MATRIX_DOMAIN);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $_SESSION['admin_token']
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 || $httpCode === 201) {
$success = 'User created successfully';
} else {
$error = 'Failed to create user: ' . $response;
}
} else {
$error = 'Please enter username and password';
}
}
// Handle user deactivation
if (($_POST['action'] ?? '') === 'deactivate_user' && $isLoggedIn) {
$userId = $_POST['user_id'] ?? '';
if ($userId) {
$deactivateData = ['deactivated' => true];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MATRIX_SERVER . '/_synapse/admin/v2/users/' . urlencode($userId));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($deactivateData));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $_SESSION['admin_token']
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$success = 'User deactivated successfully';
} else {
$error = 'Failed to deactivate user: ' . $response;
}
}
}
// Get users list if logged in
$users = [];
if ($isLoggedIn) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MATRIX_SERVER . '/_synapse/admin/v2/users');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $_SESSION['admin_token']]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$data = json_decode($response, true);
$users = $data['users'] ?? [];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matrix Admin Panel - <?= MATRIX_DOMAIN ?></title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 100%);
color: #00ff00;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
margin-bottom: 30px;
padding: 20px;
background: rgba(0, 255, 0, 0.1);
border-radius: 10px;
border: 1px solid #00ff00;
}
.header h1 {
font-size: 2.5rem;
text-shadow: 0 0 10px #00ff00;
margin-bottom: 10px;
}
.card {
background: rgba(0, 0, 0, 0.8);
border: 1px solid #00ff00;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 0 20px rgba(0, 255, 0, 0.2);
}
.btn {
background: linear-gradient(45deg, #00ff00, #00cc00);
color: #000;
border: none;
padding: 12px 24px;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
transition: all 0.3s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0, 255, 0, 0.3);
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
background: rgba(0, 0, 0, 0.7);
border: 1px solid #00ff00;
border-radius: 5px;
color: #00ff00;
margin: 10px 0;
}
label {
color: #00cc00;
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Matrix Admin Panel</h1>
<p><?= MATRIX_DOMAIN ?> - User Management System</p>
</div>
<div class="card">
<h2>Welcome to Matrix Admin Panel</h2>
<p>This application allows authorized administrators to manage Matrix users on <?= MATRIX_DOMAIN ?> server.</p>
<p>Features:</p>
<ul>
<li>Create new users</li>
<li>Deactivate existing users</li>
<li>View user statistics</li>
<li>Admin authentication required</li>
</ul>
<br>
<a href="admin.php" class="btn">Access Admin Panel</a>
</div>
<div style="text-align: center; margin-top: 30px; opacity: 0.7;">
<p>Created with ❤️ by <a href="https://www.easypro.tech" style="color: #00aa00;">www.easypro.tech</a></p>
</div>
</div>
</body>
</html>