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