-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.php
205 lines (185 loc) · 8.5 KB
/
panel.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
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
<?php
session_start();
include __DIR__ . DIRECTORY_SEPARATOR . "common.php";
include __DIR__ . DIRECTORY_SEPARATOR . "telegram_api.class.php";
if (isset($_GET['logout'])) {
$_SESSION['logged'] = FALSE;
session_destroy();
header("Location: panel.php");
exit;
}
if (count($_POST) > 0) {
$action = trim(filter_var($_POST['action'], FILTER_SANITIZE_FULL_SPECIAL_CHARS));
switch ($action) {
case 'login':
$password = trim(filter_var($_POST['password'], FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$_SESSION['logged'] = (bool)($password === getenv('MASTER_PASSWORD'));
header("Location: panel.php");
exit;
break;
case 'send_message':
if (isset($_SESSION['logged']) && $_SESSION['logged'] === TRUE) {
$submit = trim(filter_var($_POST['submit'], FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$message = trim($_POST['message']); // To have all special chars in message, this must be not validated
$messages = array();
foreach (array_keys($lang) as $lang_code) {
$messages[$lang_code] = trim($_POST['message_' . $lang_code]); // To have all special chars in message, this must be not validated
}
$result_message = '';
if ($message && count($messages) > 0) {
$Telegram_API = new Telegram_API(getenv('TELEGRAM_API_KEY'));
$users = array();
$users_query = "SELECT
*
FROM
`users`
WHERE
`enabled` = TRUE
;";
$users_stmt = $db->prepare($users_query);
$users_stmt->execute();
$users_result = $users_stmt->get_result();
while ($row = $users_result->fetch_object()) {
$users[] = $row;
}
$users_stmt->close();
foreach ($users as $user) {
$telegram_message = isset($messages[$user->language_code]) ? $messages[$user->language_code] : $message;
$telegram_message = str_replace('\n', "\n", $telegram_message); // New lines with \n symbol
$result_message .= $user->first_name . ' ' . $user->language_code . '>' . substr(filter_var($telegram_message, FILTER_SANITIZE_FULL_SPECIAL_CHARS), 0, 50);
if ($submit === "Send") {
$sent = $Telegram_API->sendMessage($user->active_chat_id, $telegram_message, TRUE);
if ($sent->ok && $sent->result) {
$result_message .= ' sent';
} else {
$result_message .= ' not sent';
}
}
$result_message .= '<br/>';
}
$_SESSION['result'] = 'success';
} else {
$_SESSION['result'] = 'danger';
$result_message = 'No messages received';
}
$_SESSION['message'] = $result_message;
} else {
header("Location: panel.php");
exit;
}
break;
default:
header("Location: panel.php");
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>High Altitude Balloon APRS Tracker API</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>
<body>
<?php if (isset($_SESSION['logged']) && $_SESSION['logged'] === TRUE) { ?>
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<a class="navbar-brand col-md-3 col-lg-2 me-0 px-3 fs-6" href="<?= "panel.php"; ?>">
High Altitude Balloon APRS Tracker API
</a>
<div class="navbar-nav">
<div class="nav-item text-nowrap">
<a class="nav-link px-3" href="<?= "panel.php?logout"; ?>">Sign out</a>
</div>
</div>
</header>
<div class="container">
<?php if (isset($_SESSION['result']) && $_SESSION['result']) { ?>
<div class="alert my-2 text-center alert-<?= $_SESSION['result']; ?>">
<?= isset($_SESSION['message']) && $_SESSION['message'] ? $_SESSION['message'] : ucfirst($_SESSION['result']); ?>
</div>
<?php } ?>
<div class="row mt-3">
<div class="col">
<div class="card">
<form action="<?= "panel.php"; ?>" method="post">
<div class="card-header">
Send message to users with enabled notifications
</div>
<div class="card-body">
<input type="hidden" name="action" value="send_message">
<?php foreach (array_keys($lang) as $lang_code) { ?>
<div class="mb-3">
<label for="message_<?= $lang_code; ?>">
Message in <?= $lang_code; ?>:
</label>
<textarea class="form-control" name="message_<?= $lang_code; ?>"
id="message_<?= $lang_code; ?>"
required><?= isset($messages) && isset($messages[$lang_code]) ? $messages[$lang_code] : '' ?></textarea>
</div>
<?php } ?>
<div class="mb-3">
<label for="message">
Message for other languages:
</label>
<textarea class="form-control" name="message" id="message"
required><?= isset($message) ? $message : '' ?></textarea>
</div>
<div>
Hint: use <b>\n</b> as new line symbol
</div>
</div>
<div class="card-footer">
<input type="submit" name="submit" class="btn btn-primary" value="Send">
<input type="submit" name="submit" class="btn btn-secondary" value="Test">
</div>
</form>
</div>
</div>
</div>
<div class="mt-2 text-end">
© UR5WKM 2022
</div>
</div>
<?php } else { ?>
<div class="container">
<div class="row mt-5">
<div class="col mb-5 mb-md-0">
<div class="card">
<form action="<?= "panel.php"; ?>" method="post">
<div class="card-header">
Please log:
</div>
<div class="card-body">
<input type="hidden" name="action" value="login">
<div class="mb-3">
<label for="password" class="form-label">
Password:
</label>
<input type="password" class="form-control" name="password" value="" required>
</div>
</div>
<div class="card-footer">
<div class="text-center">
<input type="submit" class="btn btn-primary" value="Sing in">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php } ?>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa"
crossorigin="anonymous"></script>
</body>
</html>
<?php
if (isset($_SESSION['result']))
unset($_SESSION['result']);
if (isset($_SESSION['message']))
unset($_SESSION['message']);
?>