-
Notifications
You must be signed in to change notification settings - Fork 2
/
sysop.php
185 lines (167 loc) · 6.27 KB
/
sysop.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
<?php
require("ii-functions.php");
ini_set("session.gc_maxlifetime", $session_lifetime);
ini_set("session.cookie_lifetime", $session_lifetime);
session_set_cookie_params($session_lifetime);
session_start();
// проверяет наличие данных от юзера
function checkData($str, $post=false) {
if($post) $arr=$_POST;
else $arr=$_GET;
if (
isset($arr[$str]) &&
!empty ($arr[$str])
) return true;
else return false;
}
function generate_csrf_token() {
return $_SESSION['sysop_csrf_token'] = substr(str_shuffle('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'), 0, 20);
}
class SysopAdm {
function __construct($tpldir, $transport, $access) {
$this->transport=$transport;
$this->access=$access;
$htmltop=file_get_contents($tpldir."/top.html");
$msgeditform=file_get_contents($tpldir."/msgedit.html");
$authform=file_get_contents($tpldir."/auth.html");
$htmlmain=file_get_contents($tpldir."/sysop.html");
$htmlbottom=file_get_contents($tpldir."/bottom.html");
$html=$htmltop;
$links=[];
$header=$title="Панель сисопа";
$remote=$this->fetchUserData();
if ($remote["authorized"]) {
$links[]="<a class='toplink' href='?logout'>Выйти</a>";
if ($remote["delmessages"]) {
foreach ($remote["delmessages"] as $msgid) {
$transport->deleteMessage($msgid, $withecho=true);
$remote["debug-messages"].="deleted msgid ".$msgid."<br />\n";
}
$html.=$htmlmain;
} elseif ($remote["delechoarea"]) {
$msgids=$access->getMsgList($remote["delechoarea"]);
$transport->deleteMessages($msgids, $withecho=true);
$remote["debug-messages"].="Deleted ".$remote["delechoarea"]
." (".count($msgids).")<br />\n";
$html.=$htmlmain;
} elseif ($remote["clearblacklist"]) {
$msgids=$access->blacklist;
$transport->deleteMessages($msgids, $withecho=true);
$remote["debug-messages"].="Clear (".count($msgids).")<br />\n";
$html.=$htmlmain;
} elseif ($remote["editmessage"]) {
if ($remote["updatemessage-text"]) {
$readableMsg=$transport->makeReadable($remote["updatemessage-text"]);
$transport->updateMessage($remote["editmessage"], $readableMsg);
}
$msg=$transport->getRawMessage($remote["editmessage"]);
$html.=$msgeditform;
$html=str_replace("{message}", $msg, $html);
$html=str_replace("{msgid}", $remote["editmessage"], $html);
$links[]="<a class='toplink' href='?'>Назад</a>";
} elseif ($remote["blacklist-add"]) {
//blacklist-add
file_put_contents($access->blacklist_file, "\n".$remote["blacklist-add"], FILE_APPEND);
$access->blacklist[]=$remote["blacklist-add"];
$html.=$htmlmain;
} elseif ($remote["updateblacklist-text"]) {
file_put_contents($access->blacklist_file, $remote["updateblacklist-text"]);
$access->blacklist=$access->getBlackList($access->blacklist_file);
$html.=$htmlmain;
} else {
// главная страница
$html.=$htmlmain;
}
} else {
// не авторизован - вводи пароль
$html.=$authform;
}
$html.=$htmlbottom;
$menu_links="";
foreach($links as $link) { $menu_links.=$link; }
$html=str_replace("{links}", $menu_links, $html);
$html=str_replace("{header}", $header, $html);
$html=str_replace("{title}", $title, $html);
$html=str_replace("{errors}", $remote["debug-messages"], $html);
$html=str_replace("{blacklist}", implode("\n", $access->blacklist), $html);
$html=str_replace("{token}", '<input name="sysop_csrf_token" type="hidden" value="'.generate_csrf_token().'" />', $html);
$echolist=$transport->fullEchoList();
$selects="";
foreach ($echolist as $echo) {
$selects.="<option>$echo</option>\n";
}
$html=str_replace("{options}", $selects, $html);
echo $html;
}
function fetchUserData() {
// это обработчик всех внешних данных (форм и сессий тоже!)
global $pushpassword;
$userDataArray=[
"authorized" => false,
"debug-messages" => "",
"editmessage" => null,
"updatemessage-text" => null,
"delmessages" => null,
"delechoarea" => null,
"clearblacklist" => false,
"blacklist-add" => null,
"updateblacklist-text" => null
];
if (isset($_GET["logout"])) {
unset($_SESSION["sysop-auth"]);
$userDataArray["authorized"]=false;
}
if (!isset($_SESSION["sysop-auth"])) {
if (checkData("authstr", true)) {
if ($_POST["authstr"] == $pushpassword) {
$sysop_auth=true;
$userDataArray["authorized"]=true;
} else {
$sysop_auth=false;
$userDataArray["debug-messages"].="Неверный пароль<br />\n";
}
} else {
$sysop_auth=false;
}
if ($sysop_auth) $_SESSION["sysop-auth"]=$sysop_auth;
} else $sysop_auth=$_SESSION["sysop-auth"];
$userDataArray["authorized"]=$sysop_auth;
if (!$userDataArray["authorized"]) return $userDataArray;
if (!(isset($_SESSION['sysop_csrf_token']) && isset($_POST['sysop_csrf_token']) && $_SESSION['sysop_csrf_token'] == @$_POST['sysop_csrf_token'])) {
// csrf не отправлен или неверный, так что прерываем обработку формы
return $userDataArray;
}
if (checkData("blacklist-clear", true)) {
$userDataArray["clearblacklist"]=true;
}
elseif (checkData("messages-clear", true)) {
$userDataArray["delmessages"]=explode("\r\n", $_POST["messages-clear"]);
}
elseif (checkData("echoarea-delete", true)) {
$echo=$_POST["echoarea-delete"];
if ($this->access->checkEcho($echo)) {
$userDataArray["delechoarea"]=$echo;
} else $userDataArray["debug-messages"].="Неправильная эха<br />\n";
}
elseif (checkData("edit-message", true)) {
$msgid=$_POST["edit-message"];
$userDataArray["editmessage"]=$msgid;
if (!$this->access->checkHash($msgid)) $userDataArray["debug-messages"].="Неправильный msgid!<br />\n";
else {
if (checkData("edit-text", true)) {
$userDataArray["updatemessage-text"]=str_replace("\r", "", $_POST["edit-text"]);
}
}
}
elseif (checkData("blacklist-add", true)) {
$msgid=$_POST["blacklist-add"];
$userDataArray["blacklist-add"]=$msgid;
}
elseif (checkData("blacklist", true)) {
$userDataArray["updateblacklist-text"]=str_replace("\r", "", $_POST["blacklist"]);
}
return $userDataArray;
}
}
$adm=new SysopAdm("iiweb-material/", $access->transport, $access);
?>