forked from vizplus/my-viz-plus
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.php
More file actions
186 lines (168 loc) · 7.71 KB
/
Copy pathadmin.php
File metadata and controls
186 lines (168 loc) · 7.71 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
<?php
include('autoloader.php');
// Сессия админа (Q1): серверная сессия вместо cookie = md5(пароль). Куки — HttpOnly/Secure/SameSite.
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'httponly' => true,
'secure' => !empty($_SERVER['HTTPS']),
'samesite' => 'Strict',
]);
session_start();
ob_start();
$esc = function ($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); };
$admin = isset($_SESSION['admin_user']);
$user = $admin ? $_SESSION['admin_user'] : '';
$menu_addon = $admin ? '<a class="menu-el color1" href="/admin.php?action=logout">Выход</a>' : '';
// CSRF-токен (Q3): на сессию; обязателен для изменяющих действий (hide/show).
if (empty($_SESSION['csrf'])) {
$_SESSION['csrf'] = bin2hex(random_bytes(16));
}
$csrf = $_SESSION['csrf'];
function csrf_ok($csrf) {
return isset($_GET['token']) && is_string($_GET['token']) && hash_equals($csrf, $_GET['token']);
}
// $ip уже вычислен в autoloader (X-Forwarded-For / REMOTE_ADDR); выводим только экранированным.
$title = 'Администрирование';
$action = isset($_GET['action']) ? $_GET['action'] : '';
if ($admin) {
// Карта таблиц маркетплейсов: action => [таблица, ключевое поле].
$tables = [
'paid_subscriptions' => ['paid_subscriptions', 'creator'],
'accounts_on_sale' => ['accounts_on_sale', 'account'],
'subaccounts_on_sale' => ['subaccounts_on_sale', 'account'],
];
if (isset($tables[$action])) {
list($table, $keyfield) = $tables[$action];
$titles = [
'paid_subscriptions' => 'Записи в таблице «Платные подписки»',
'accounts_on_sale' => 'Записи в таблице «Аккаунты на продаже»',
'subaccounts_on_sale' => 'Записи в таблице «Субаккаунты на продаже»',
];
$title = $titles[$action];
// Мутации hide/show — только с валидным CSRF-токеном.
if (isset($_GET['hide']) && '' !== $_GET['hide']) {
if (!csrf_ok($csrf)) { header('location:/admin.php?action=' . rawurlencode($action)); exit; }
$db->sql('UPDATE `' . $table . '` SET `status`=1 WHERE `' . $keyfield . '`=\'' . $db->prepare($_GET['hide']) . '\' LIMIT 1');
header('location:/admin.php?action=' . rawurlencode($action) . '&caption=ok');
exit;
}
if (isset($_GET['show']) && '' !== $_GET['show']) {
if (!csrf_ok($csrf)) { header('location:/admin.php?action=' . rawurlencode($action)); exit; }
$db->sql('UPDATE `' . $table . '` SET `status`=0 WHERE `' . $keyfield . '`=\'' . $db->prepare($_GET['show']) . '\' LIMIT 1');
header('location:/admin.php?action=' . rawurlencode($action) . '&caption=ok');
exit;
}
print '<a class="right" href="/admin.php">← вернуться</a>';
if ('ok' == ($_GET['caption'] ?? '')) {
print '<p class="green">Операция успешно выполнена</p>';
}
$is_subs = ('paid_subscriptions' == $action);
$noun = $is_subs ? 'подписок' : ('accounts_on_sale' == $action ? 'аккаунтов' : 'субаккаунтов');
// helper: строка списка с кнопкой (hide/show), всё экранировано + CSRF-токен в ссылке.
$render = function ($m) use ($esc, $csrf, $action, $keyfield, $is_subs) {
$key = $m[$keyfield];
$hidden = (1 == $m['status']);
$verb = $hidden ? 'show' : 'hide';
$label = $hidden ? 'показывать' : 'скрыть';
$cls = $hidden ? '' : ' red';
$note = '';
if ($is_subs) {
if (!empty($m['sub_count'])) $note = '<span class="green"> — подписчиков: ' . (int)$m['sub_count'] . '</span>';
} else {
$len = (int)$m['length'];
if ($hidden ? $len < 8 : $len > 8) {
$ccls = $hidden ? 'green' : 'red';
$note = '<span class="' . $ccls . '"> — длина ' . $len . ' символов</span>';
}
}
return '<div style="margin-top:5px;line-height:25px;">'
. '<a href="/admin.php?action=' . rawurlencode($action) . '&' . $verb . '=' . rawurlencode($key) . '&token=' . $esc($csrf) . '"'
. ' class="inline-button small no-margin' . $cls . '" style="font-size:14px;margin-right:10px !important;">' . $label . '</a> '
. $esc($key) . $note . '</div>';
};
print 'Список скрытых ' . $noun . ':';
$order = $is_subs ? '`creator` ASC' : '`account` ASC';
$q = $db->sql('SELECT * FROM `' . $table . '` WHERE `status`=1 ORDER BY ' . $order);
while ($m = $db->row($q)) { print $render($m); }
print '<hr>Список отображаемых ' . $noun . ':';
$q = $db->sql('SELECT * FROM `' . $table . '` WHERE `status`=0 ORDER BY ' . $order);
while ($m = $db->row($q)) { print $render($m); }
}
else if ('logout' == $action) {
$_SESSION = [];
if (ini_get('session.use_cookies')) {
$p = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
}
session_destroy();
header('location:/admin.php');
exit;
}
else {
print '<p>Привет, ' . $esc($user) . ', ваш IP: ' . $esc($ip) . '</p>';
print '<p><a href="/admin.php?action=paid_subscriptions">Записи в таблице «Платные подписки»</a></p>';
print '<p><a href="/admin.php?action=accounts_on_sale">Записи в таблице «Аккаунты на продаже»</a></p>';
print '<p><a href="/admin.php?action=subaccounts_on_sale">Записи в таблице «Субаккаунты на продаже»</a></p>';
}
}
else {
$error = false;
if (isset($_POST['my_login'])) {
$login = (string)$_POST['my_login'];
$pass = isset($_POST['my_password']) ? (string)$_POST['my_password'] : '';
// password_verify против ХЕША из config (password_hash). Единое сообщение — не раскрываем, есть ли логин.
if (isset($users_arr[$login]) && is_string($users_arr[$login]) && password_verify($pass, $users_arr[$login])) {
session_regenerate_id(true);
$_SESSION['admin_user'] = $login;
header('location:/admin.php');
exit;
}
$error = 'Неверный логин или пароль';
}
$title = 'Вход';
print '<form action="/admin.php" method="POST">';
if ($error) print '<p class="red">Ошибка: ' . $esc($error) . '</p>';
print '<p><input type="text" name="my_login"> — логин</p>';
print '<p><input type="password" name="my_password"> — пароль</p>';
print '<p><input type="submit" value="Выполнить вход"></p>';
print '</form>';
}
$content = ob_get_contents();
ob_end_clean();
print '<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>' . $esc($title) . ' — VIZ World</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/app.css?' . filemtime('app.css') . '">
</head>
<body>
<div class="header shadow unselectable">
<div class="horizontal-view">
<div class="menu-button menu-button-action"><img class="menu-button-action" src="/icons/menu.svg"></div>
<div class="logo">
<a data-href="/" class="prefix">wallet</a><a href="https://promo.viz.world/"><img src="https://viz.world/logo-viz-simple.svg" alt="VIZ World"></a>
</div>
<div class="menu-list captions">
<div class="menu-bg">
<a class="menu-el color1 selected" href="/admin.php">Администрирование</a>
' . $menu_addon . '
</div>
</div>
</div>
</div>
<div class="horizontal-view vertical-view">
<div class="cards-view">
<div class="cards-container">
<div class="card captions">
<h3>' . $esc($title) . '</h3>';
print $content;
print '
</div>
</div>
</div>
</div>
';