]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - spamfilter.php
Spamfilter: Fix a user permission check
[irc/unrealircd/unrealircd-webpanel.git] / spamfilter.php
CommitLineData
0f627622
VP
1<?php
2require_once "common.php";
0f627622
VP
3require_once "header.php";
4
6bc0b9a0 5$spamfilter_target_info = Array(
6bc0b9a0
BM
6 "p"=>Array("short_text" => "usermsg", "long_text" => "User message"),
7 "n"=>Array("short_text" => "usernotice", "long_text" => "User notice"),
c17704e4 8 "c"=>Array("short_text" => "chanmsg", "long_text" => "Channel message"),
6bc0b9a0
BM
9 "N"=>Array("short_text" => "channotice", "long_text" => "Channel notice"),
10 "P"=>Array("short_text" => "part", "long_text" => "Part message"),
11 "q"=>Array("short_text" => "quit", "long_text" => "Quit message"),
12 "d"=>Array("short_text" => "dcc", "long_text" => "DCC Filename"),
13 "a"=>Array("short_text" => "away", "long_text" => "Away message"),
14 "t"=>Array("short_text" => "topic", "long_text" => "Channel topic"),
15 "T"=>Array("short_text" => "message-tag", "long_text" => "Message tag"),
c17704e4 16 "u"=>Array("short_text" => "usermask", "long_text" => "User mask (nick!user@host:realname)"),
6bc0b9a0
BM
17);
18
19function spamfilter_targets_to_string($targets)
20{
88cae748 21 global $spamfilter_target_info;
6bc0b9a0
BM
22
23 $ret = '';
24 for ($i = 0, $targs = ""; $i < strlen($targets); $i++)
25 {
26 $c = $targets[$i];
27 if (isset($spamfilter_target_info[$c]))
28 $ret .= $spamfilter_target_info[$c]["short_text"].", ";
29 else
30 $ret .= "??, ";
31 }
32 $ret = rtrim($ret,", ");
33 return $ret;
34}
35
36function spamfilter_targets_to_string_with_info($targets)
37{
88cae748 38 global $spamfilter_target_info;
6bc0b9a0
BM
39
40 $ret = '';
41 for ($i = 0, $targs = ""; $i < strlen($targets); $i++)
42 {
43 $c = $targets[$i];
44 if (isset($spamfilter_target_info[$c]))
45 $ret .= "<span data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"".$spamfilter_target_info[$c]["long_text"]."\" style=\"border-bottom: 1px dotted #000000\">".$spamfilter_target_info[$c]["short_text"]."</span>, ";
46 else
47 $ret .= "??, ";
48 }
49 $ret = rtrim($ret,", ");
50 return $ret;
51}
52
c17704e4
BM
53function spamfilter_target_name_to_char($name)
54{
88cae748 55 global $spamfilter_target_info;
c17704e4
BM
56
57 foreach ($spamfilter_target_info as $char=>$e)
58 {
59 if ($e["short_text"] == $name)
60 return $char;
61 }
62 return false;
63}
64
65function spamfilter_targets_from_array_to_chars($ar)
66{
67 $ret = '';
68 foreach ($ar as $name)
69 {
70 $c = spamfilter_target_name_to_char($name);
71 if ($c !== false)
72 $ret .= $c;
73 }
74 return $ret;
75}
6bc0b9a0 76
0f627622
VP
77if (!empty($_POST))
78{
79
80 do_log($_POST);
81
c9bbb0e6 82 if (($sf = (isset($_POST['sf_add'])) ? $_POST['sf_add'] : false)) // if it was a spamfilter entry
0f627622 83 {
88cae748
VP
84 if (!current_user_can(PERMISSION_SPAMFILTER_ADD))
85 Message::Fail("Could not add Spamfilter entry: Permission denied");
0f627622
VP
86 else
87 {
0f627622 88
88cae748
VP
89 /* get targets */
90 $targets = []; // empty arrae
91 foreach($_POST as $key => $value)
0f627622 92 {
88cae748
VP
93 if (substr($key, 0, 7) == "target_")
94 $targets[] = str_replace(["target_", "_"], ["", "-"], $key);
0f627622 95 }
88cae748
VP
96 if (empty($targets))
97 Message::Fail("No target was specified");
98
99 if (!isset($_POST['sf_bantype']))
100 Message::Fail("No action was chosen");
101
102 else
103 {
104
105 $bantype = $_POST['sf_bantype'];
106 $targ_chars = spamfilter_targets_from_array_to_chars($targets);
107 /* duplicate code for now [= */
108 $banlen_w = (isset($_POST['banlen_w'])) ? $_POST['banlen_w'] : NULL;
109 $banlen_d = (isset($_POST['banlen_d'])) ? $_POST['banlen_d'] : NULL;
110 $banlen_h = (isset($_POST['banlen_h'])) ? $_POST['banlen_h'] : NULL;
111 $duration = "";
112 if (!$banlen_d && !$banlen_h && !$banlen_w)
113 $duration .= "0";
114
0f627622 115 else
88cae748
VP
116 {
117 if ($banlen_w)
118 $duration .= $banlen_w;
119 if ($banlen_d)
120 $duration .= $banlen_d;
121 if ($banlen_h)
122 $duration .= $banlen_h;
123 }
124 $match_type = $_POST['matchtype']; // should default to 'simple'
125 $reason = isset($_POST['ban_reason']) ? $_POST['ban_reason'] : "No reason";
126 $soft = (isset($_POST['soft'])) ? true : false;
127 if ($soft)
128 $bantype = "soft-$bantype";
129 if ($rpc->spamfilter()->add($sf, $match_type, $targ_chars, $bantype, $duration, $reason))
130 Message::Success("Added spamfilter entry \"$sf\" [match type: $match_type] [targets: $targ_chars] [reason: $reason]");
131 else
132 Message::Fail("Could not add spamfilter entry \"$sf\" [match type: $match_type] [targets: $targ_chars] [reason: $reason]: $rpc->error");
133 }
0f627622
VP
134 }
135 }
136 else if (!empty($_POST['sf']))
88cae748
VP
137 {
138 if (!current_user_can(PERMISSION_SPAMFILTER_DEL))
139 Message::Fail("Could not delete Spamfilter entry or entries: Permission denied");
140 else
141 foreach ($_POST['sf'] as $key => $value)
0f627622 142 {
88cae748 143 $tok = explode(",", $value);
0f627622
VP
144 $name = base64_decode($tok[0]);
145 $match_type = base64_decode($tok[1]);
146 $spamfilter_targets = base64_decode($tok[2]);
147 $ban_action = base64_decode($tok[3]);
148 if ($rpc->spamfilter()->delete($name, $match_type, $spamfilter_targets, $ban_action))
149 Message::Success("Spamfilter on $name has been removed");
150 else
151 Message::Fail("Unable to remove spamfilter on $name: $rpc->error");
152 }
88cae748 153 }
0f627622
VP
154
155}
156
d3697d8f 157$spamfilter = $rpc->spamfilter()->getAll();
0f627622 158?>
c88945a1
VP
159
160<h4>Spamfilter Overview</h4><br>
88cae748 161<p><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" <?php echo (current_user_can(PERMISSION_SPAMFILTER_ADD)) ? "" : "disabled"; ?>>
c88945a1
VP
162 Add entry
163 </button></p>
164 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
165 <div class="modal-dialog modal-dialog-centered" role="document">
166 <div class="modal-content">
167 <div class="modal-header">
168 <h5 class="modal-title" id="myModalLabel">Add new Spamfilter Entry</h5>
169 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
170 <span aria-hidden="true">&times;</span>
171 </button>
172 </div>
173 <div class="modal-body">
0f627622
VP
174
175 <form action="spamfilter.php" method="post">
a741bd50 176 <div class="align_label curvy">Match&nbsp;type: </div> <select name="matchtype" id="matchtype">
0f627622
VP
177 <option value="simple">Simple</option>
178 <option value="regex">Regular Expression</option>
179 </select><br>
a741bd50
BM
180 <div class="align_label curvy">Entry: </div> <input class="curvy" type="text" id="sf_add" name="sf_add"><br>
181
182 <div class="align_label curvy"><label for="banlen_w">Targets: </label></div>
c17704e4
BM
183<?php
184 $first = true;
185 foreach ($spamfilter_target_info as $letter=>$e)
186 {
187 $shortname = $e['short_text'];
188 $longname = $e['long_text'];
189 if (!$first)
190 echo "<div class=\"align_label curvy\"><label></label></div>";
191 $first = false;
192 echo "<input type=\"checkbox\" class=\"curvy\" id=\"target_$shortname\" name=\"target_$shortname\">$longname<br>\n";
193 }
194?>
c88945a1 195 <div class="align_label curvy">Action: </div> <select name="sf_bantype" id="sf_bantype">
0f627622
VP
196 <option value=""></option>
197 <optgroup label="Bans">
198 <option value="kline">Kill Line (KLine)</option>
199 <option value="gline">Global Kill Line (GLine)</option>
200 <option value="zline">Zap Line (ZLine)</option>
201 <option value="gzline">Global Zap Line (GZLine)</option>
202
203 </optgroup>
204 <optgroup label="Restrictions">
205 <option value="tempshun">Temporary Shun (Session only)</option>
206 <option value="shun">Shun</option>
207 <option value="block">Block</option>
208 <option value="dccblock">DCC Block</option>
209 <option value="viruschan">Send to "Virus Chan"</option>
210 </optgroup>
211 <optgroup label="Other">
212 <option value="warn">Warn the user</option>
213 </optgroup>
214 </select><br>
c88945a1 215 <div class="align_label curvy"><label for="banlen_w">Duration: </label></div>
0f627622
VP
216 <select name="banlen_w" id="banlen_w">
217 <?php
218 for ($i = 0; $i <= 56; $i++)
219 {
220 if (!$i)
221 echo "<option value=\"0w\"></option>";
222 else
223 {
224 $w = ($i == 1) ? "week" : "weeks";
225 echo "<option value=\"$i" . "w\">$i $w" . "</option>";
226 }
227 }
228 ?>
229 </select>
230 <select name="banlen_d" id="banlen_d">
231 <?php
232 for ($i = 0; $i <= 31; $i++)
233 {
234 if (!$i)
235 echo "<option value=\"0d\"></option>";
236 else
237 {
238 $d = ($i == 1) ? "day" : "days";
239 echo "<option value=\"$i" . "d\">$i $d" . "</option>";
240 }
241 }
242 ?>
243 </select>
244 <select name="banlen_h" id="banlen_h">
245 <?php
246 for ($i = 0; $i <= 24; $i++)
247 {
248 if (!$i)
249 echo "<option value=\"0d\"></option>";
250 else
251 {
252 $h = ($i == 1) ? "hour" : "hours";
253 echo "<option value=\"$i" . "h\">$i $h" . "</option>";
254 }
255 }
256 ?>
a741bd50
BM
257 </select><br>
258 <input class="curvy" type="checkbox" id="soft" name="soft">Don't affect logged-in users (soft)
c88945a1
VP
259 <br><div class="align_label curvy"><label for="ban_reason">Reason: </label></div>
260 <input class="curvy" type="text" id="ban_reason" name="ban_reason"><br>
c88945a1
VP
261 </div>
262
263 <div class="modal-footer">
264 <button id="CloseButton" type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
265 <button type="submit" action="post" class="btn btn-danger">Add Spamfilter Entry</button>
266 </form>
267 </div></div></div></div>
268
269
ce3de101 270 <table class="container-xxl table-sm table-responsive caption-top table-striped">
c88945a1 271 <thead class="table-primary"><form action="spamfilter.php" method="post">
44e93f67 272 <th><input type="checkbox" label='selectall' onClick="toggle_sf(this)" /></th>
0f627622 273 <th>Match Type</th>
a448002a
BM
274 <th>Mask</th>
275 <th>Target</th>
0f627622
VP
276 <th>Action</th>
277 <th>Action Duration</th>
0f627622 278 <th>Reason</th>
a448002a
BM
279 <th>Set By</th>
280 <th>Set On</th>
d1d9caa9 281 </thead>
0f627622
VP
282
283 <?php
d3697d8f 284 foreach($spamfilter as $sf)
0f627622
VP
285 {
286 echo "<tr>";
287 echo "<td><input type=\"checkbox\" value='" . base64_encode($sf->name).",".base64_encode($sf->match_type).",".base64_encode($sf->spamfilter_targets).",".base64_encode($sf->ban_action) . "' name=\"sf[]\"></td>";
0f627622 288 echo "<td>".$sf->match_type."</td>";
a448002a 289 echo "<td>".$sf->name."</td>";
6bc0b9a0 290 echo "<td>".spamfilter_targets_to_string_with_info($sf->spamfilter_targets)."</td>";
84cebc21 291 echo "<td><span class=\"badge rounded-pill badge-info\">".$sf->ban_action."</span></td>";
a448002a 292 echo "<td>".$sf->ban_duration_string."</td>";
0f627622 293 echo "<td>".$sf->reason."</td>";
c9c3cd87 294 echo "<td>".show_nick_only($sf->set_by)."</td>";
a448002a 295 echo "<td>".$sf->set_at_string."</td>";
0f627622
VP
296
297 }
250c1a03 298 ?></table><p><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal2" <?php echo (current_user_can(PERMISSION_SPAMFILTER_DEL)) ? "" : "disabled"; ?>>
c88945a1
VP
299 Delete selected
300 </button></p>
301 <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
302 <div class="modal-dialog modal-dialog-centered" role="document">
303 <div class="modal-content">
304 <div class="modal-header">
e3f034ee 305 <h5 class="modal-title" id="myModalLabel">Confirm deletion</h5>
c88945a1
VP
306 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
307 <span aria-hidden="true">&times;</span>
308 </button>
309 </div>
310 <div class="modal-body">
311 Are you sure you want to do this?<br>
312 This cannot be undone.
313 </div>
314 <div class="modal-footer">
315 <button id="CloseButton" type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
316 <button type="submit" action="post" class="btn btn-danger">Delete</button>
317
318 </div>
319 </div>
320 </div>
321 </div>
322</form></div></div>
0f627622
VP
323
324
325<?php require_once 'footer.php'; ?>