]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - misc/channel-lookup-misc.php
Fix setting zlines on idents, reported by Jellis
[irc/unrealircd/unrealircd-webpanel.git] / misc / channel-lookup-misc.php
1 <?php
2 function generate_chanbans_table($channel)
3 {
4 global $rpc;
5 $channel = $rpc->channel()->get($channel->name);
6 ?><p><table class="container-xxl table table-sm table-responsive caption-top table-striped">
7 <button class="btn btn-primary mr-1 btn-sm" data-toggle="modal" data-target="#ban">Add New</button>
8 <form method="post">
9 <button class="btn btn-info btn-sm" type="submit" name="delete_sel_ban">Delete</button>
10 </p>
11
12 <thead class="table-info">
13 <th><input type="checkbox" label='selectall' onClick="toggle_chanbans(this)" /></th>
14 <th>Name</th>
15 <th>Set by</th>
16 <th>Set at</th>
17 <th></th>
18 </thead>
19 <tbody>
20 <?php
21 foreach ($channel->bans as $ban) {
22 echo "<tr>";
23 echo "<td scope=\"row\"><input type=\"checkbox\" value='$ban->name' name=\"cb_checkboxes[]\"></td>";
24 echo "<td><code>".htmlspecialchars($ban->name)."</code></td>";
25 $set_by = htmlspecialchars($ban->set_by);
26 echo "<td>$set_by</td>";
27 $set_at = $ban->set_at;
28 echo "<td>$set_at</td>";
29 echo "<td></td>";
30 echo "</tr>";
31 }
32
33 ?>
34 </tbody>
35 </table>
36 </form>
37 <?php
38 }
39 function generate_chaninvites_table($channel)
40 {
41 global $rpc;
42 $channel = $rpc->channel()->get($channel->name);
43 ?><p><table class="table table-sm table-responsive table-hover caption-top table-striped">
44 <button class="btn btn-primary btn-sm mr-1" data-toggle="modal" data-target="#invite">Add New</button>
45 <form method="post">
46 <button class="btn btn-info btn-sm" type="submit" name="delete_sel_inv">Delete</button>
47 </p>
48
49 <thead class="table-info">
50 <th><input type="checkbox" label='selectall' onClick="toggle_chaninvs(this)" /></th>
51 <th>Name</th>
52 <th>Set by</th>
53 <th>Set at</th>
54 <th></th>
55 </thead>
56 <tbody>
57 <?php
58 foreach ($channel->invite_exceptions as $inv) {
59 echo "<tr>";
60 echo "<td scope=\"row\"><input type=\"checkbox\" value='$inv->name' name=\"ci_checkboxes[]\"></td>";
61 echo "<td><code>".htmlspecialchars($inv->name)."</code></td>";
62 $set_by = htmlspecialchars($inv->set_by);
63 echo "<td>$set_by</td>";
64 $set_at = $inv->set_at;
65 echo "<td>$set_at</td>";
66 echo "<td></td>";
67 echo "</tr>";
68 }
69
70 ?>
71 </tbody>
72 </table>
73 </form>
74 <?php
75 }
76
77
78 function generate_chanexcepts_table($channel)
79 {
80 global $rpc;
81
82 $channel = $rpc->channel()->get($channel->name);
83 ?><p><table class="table table-sm table-responsive table-hover caption-top table-striped">
84 <button class="btn btn-primary mr-1 btn-sm" data-toggle="modal" data-target="#except">Add New</button>
85 <form method="post">
86 <button class="btn btn-info btn-sm" type="submit" name="delete_sel_ex">Delete</button>
87 </p>
88
89 <thead class="table-info">
90 <th><input type="checkbox" label='selectall' onClick="toggle_chanexs(this)" /></th>
91 <th>Name</th>
92 <th>Set by</th>
93 <th>Set at</th>
94 <th></th>
95 </thead>
96 <tbody>
97 <?php
98 foreach ($channel->ban_exemptions as $ex) {
99 echo "<tr>";
100 echo "<td scope=\"row\"><input type=\"checkbox\" value='$ex->name' name=\"ce_checkboxes[]\"></td>";
101 echo "<td><code>".htmlspecialchars($ex->name)."</code></td>";
102 $set_by = htmlspecialchars($ex->set_by);
103 echo "<td>$set_by</td>";
104 $set_at = $ex->set_at;
105 echo "<td>$set_at</td>";
106 echo "<td></td>";
107 echo "</tr>";
108 }
109
110 ?>
111 </tbody>
112 </table>
113 </form>
114 <?php
115 }
116
117 /**
118 * Generate the user list of a channel
119 *
120 * Why is it called chan occupants? o.o
121 * For the code, to avoid mixups
122 * It's called "User List" on the website
123 * @param mixed $channel
124 * @return void
125 */
126 function generate_chan_occupants_table($channel)
127 {
128 global $rpc;
129 ?>
130 <form method="post"><p>
131
132 </p>
133 <table class="container-xxl table table-sm table-responsive caption-top table-striped">
134 <thead class="table-info">
135 <th><input type="checkbox" label='selectall' onClick="toggle_checkbox(this)" /></th>
136 <th>Name</th>
137 <th>Status</th>
138 <th>Host</th>
139 </thead>
140 <tbody>
141 <?php
142 $m = sort_user_list($channel->members);
143
144 $i = 0;
145 foreach ($m as $member)
146 {
147 $lvlstring = "";
148
149 if (isset($member->level))
150 {
151 for ($i = 0; isset($member->level[$i]) && $m = $member->level[$i]; $i++)
152 {
153 switch ($m)
154 {
155 case "v":
156 $lvlstring .= "<div class='badge rounded-pill badge-primary'>Voice</div> ";
157 break;
158 case "h":
159 $lvlstring .= "<div class='badge rounded-pill badge-secondary'>Half-Op</div> ";
160 break;
161 case "o":
162 $lvlstring .= "<div class='badge rounded-pill badge-warning'>Op</div> ";
163 break;
164 case "a":
165 $lvlstring .= "<div class='badge rounded-pill badge-danger'>Admin</div> ";
166 break;
167 case "q":
168 $lvlstring .= "<div class='badge rounded-pill badge-success'>Owner</div> ";
169 break;
170
171 // providing support third/ojoin
172 case "Y":
173 $lvlstring .= "<div class='badge rounded-pill'>OJOIN</div> ";
174 break;
175 }
176 }
177 }
178
179 echo "<tr>";
180 ?><form method="post" action=""><?php
181 $disabled = (current_user_can(PERMISSION_EDIT_CHANNEL_USER)) ? "" : "disabled";
182 $disabledcolor = ($disabled) ? "btn-secondary" : "btn-primary";
183 echo "<td scope=\"row\"><input type=\"checkbox\" value='$member->id' name=\"checkboxes[]\"></td>";
184 echo "<td><a href=\"".get_config("base_url")."users/details.php?nick=$member->id\">".htmlspecialchars($member->name)."</a></td>";
185 echo "<td class='text-right'>$lvlstring</td>";
186 echo "<td><code>".((property_exists($member, 'hostname')) ? htmlspecialchars($member->hostname) : "")."</code></td>";
187 echo "</tr>";
188 }
189
190 ?>
191 </tbody>
192 </table>
193 </form>
194
195 <?php
196 }
197
198 function generate_html_chansettings($channel)
199 {
200 ?>
201
202 <table class="table-sm table-responsive caption-top table-hover">
203 <tbody>
204 <?php
205 if (BadPtr($channel->modes))
206 {
207 echo "No modes set";
208 return;
209 }
210 $fmodes = $channel->modes;
211 $tok = split($fmodes);
212 $modes = $tok[0];
213 $params = rparv($fmodes);
214 $paramed_modes = sort_paramed_modes($modes, $params);
215
216 for ($i=0; ($mode = (isset($modes[$i])) ? $modes[$i] : NULL); $i++)
217 {
218 $modeinfo = IRCList::lookup($mode);
219 if (!$modeinfo)
220 continue;
221 ?>
222 <tr>
223 <th><?php echo $modeinfo['name']; ?></th>
224 <td><code><?php echo isset($paramed_modes[$mode]) ? $paramed_modes[$mode] : ""; ?></code></td>
225 <td>
226 <?php echo $modeinfo['description']; ?>
227 </td>
228 </tr>
229 <?php
230 }
231
232
233 ?>
234 </tbody>
235 </table>
236
237 <?php
238 }
239
240 /** Return user-friendly mode group names as an array like:
241 * "Join restrictions"=>"kliRzOL", ...
242 * The group "Other" has a number of preset ones PLUS
243 * will be automatically filled with any remaining modes
244 * that are detected and that we forgot to put in Other.
245 */
246 function group_modes()
247 {
248 $modes = '';
249 foreach(IRCList::$uplink as $mode_groups)
250 $modes .= $mode_groups;
251
252 $grouping = IRCList::$grouping;
253
254 foreach (str_split($modes) as $letter)
255 {
256 $found = false;
257 foreach($grouping as $groupname=>$value)
258 {
259 if (str_contains($value, $letter))
260 {
261 $found = true;
262 break;
263 }
264 }
265 if (!$found)
266 $grouping["Other"] .= $letter;
267
268 }
269 return $grouping;
270 }
271
272 function generate_edit_chmodes($chan)
273 {
274 ?>
275 <?php
276 if (!isset($chan->modes))
277 {
278 echo "No modes set";
279 return;
280 }
281 $fmodes = $chan->modes;
282 $tok = split($fmodes);
283 $modes = $tok[0];
284 $params = rparv($fmodes);
285 $paramed_modes = sort_paramed_modes($modes, $params);
286
287 $all_modes = IRCList::$uplink;
288 $groups = group_modes();
289
290 foreach ($groups as $group_name=>$m)
291 {
292 echo "<fieldset class=\"border p-1 col-sm-6\">\n";
293 echo "<legend class=\"w-auto\" style=\"font-size: 16px\">$group_name</legend>\n";
294 for ($i=0; ($mode = (isset($m[$i])) ? $m[$i] : NULL); $i++)
295 {
296 $group = get_chmode_group($mode);
297 if (!$group || $group == 1)
298 continue;
299 $modeinfo = IRCList::lookup($mode);
300 $checked = (strstr($modes,$mode)) ? "checked " : " ";
301
302 $disabled = "";
303 if (isset($modeinfo) && $modeinfo['requires'] == "Server")
304 $disabled = "disabled";
305
306 ?>
307 <div class="form-group row">
308 <div class="col-sm-5">
309 <input <?php echo $checked.$disabled; ?> type="checkbox" value='<?php echo $mode; ?>' name="newmodes[]">
310 <span data-toggle="tooltip" data-placement="top" title="<?php echo htmlspecialchars($modeinfo['description']); ?>"><?php echo htmlspecialchars($modeinfo['name'])." (<code>+$mode</code>)"; ?>
311 </div>
312 <div class="col-sm-2">
313 <?php
314
315 if ($group == 2 || $group == 3)
316 {
317 ?><input type="text" class="input-group-sm" name="paramed_modes[<?php echo $mode; ?>]"
318 id="<?php echo $mode; ?>" value="<?php echo ($checked)
319 ?
320 htmlspecialchars(
321 isset($paramed_modes[$mode])
322 ?
323 $paramed_modes[$mode]
324 :
325 ""
326 )
327 :
328 ""; ?>"><?php
329 }
330 ?>
331 </div>
332 </div>
333 <?php
334 }
335 echo "</fieldset>\n";
336 }
337 ?>
338 <?php
339 }
340
341 function get_chmode_group(string $mode) : int
342 {
343 foreach(IRCList::$uplink as $i => $grp)
344 if (strstr($grp,$mode))
345 return $i + 1;
346
347 return 0; // invalid
348 }
349
350 /**
351 * Force loading of a particular modal by name
352 */
353 function chlkup_autoload_modal($name)
354 {
355 ?>
356 <script>
357 $(document).ready(function () {
358 $("#<?php echo $name; ?>").modal("show");
359 });
360 </script>
361 <?php
362 }
363
364 /**
365 * Returns an array of parametered modes and their params
366 */
367 function sort_paramed_modes($modes, $params) : array
368 {
369 $m = [];
370 for ($i=0; isset($modes[$i]) && $mode = $modes[$i]; $i++)
371 {
372 $group = get_chmode_group($mode);
373 if ($group != 2 && $group != 3)
374 continue;
375 $p = split($params);
376 $m[$mode] = $p[0];
377 $p[0] = NULL;
378 $params = glue($p);
379 }
380 return $m;
381 }
382
383 function _do_chan_item_delete($chan, string $type, array $list, array &$errors) : bool
384 {
385 global $rpc;
386 $n = "";
387 $str = "";
388
389 if ($type == "invite")
390 $char = "I";
391 elseif ($type == "ban")
392 $char = "b";
393 elseif ($type == "except")
394 $char = "e";
395 else
396 return false;
397
398 foreach($list as $l)
399 {
400 // check if the string needs therapy
401 if ((strlen($n) + 1) + (strlen($str) + strlen($l)) > 150)
402 {
403 // send what we've got already and prepare for the next lot
404 if (!$rpc->channel()->set_mode($chan->name, htmlspecialchars("-$n"), htmlspecialchars($str)))
405 $errors[] = $rpc->error . " ($rpc->errno)";
406
407 // prepare for the next set
408 $n = "";
409 $str = "";
410 }
411 $n .= $char;
412 $str .= " ".$l;
413 }
414 // if multiple, this will be the last one so we return here
415 if ($rpc->channel()->set_mode($chan->name, htmlspecialchars("-$n"), htmlspecialchars($str)))
416 {
417 if (!empty($errors))
418 Message::Fail("One or more errors occurred:", $errors);
419 Message::Success("Deleted successfully");
420 return true;
421 }
422 $errors[] = $rpc->error . " ($rpc->errno)";
423 Message::Fail("An error occurred:", $errors);
424 return false;
425 }
426
427 function do_delete_invite($chan, $list)
428 {
429 $errs = [];
430 _do_chan_item_delete($chan, "invite", $list, $errs);
431 }
432
433 function do_delete_chanban($chan, $list)
434 {
435 $errs = [];
436 _do_chan_item_delete($chan, "ban", $list, $errs);
437 }
438
439 function do_delete_chanex($chan, $list)
440 {
441 $errs = [];
442 _do_chan_item_delete($chan, "except", $list, $errs);
443 }
444
445
446
447 /**
448 * Sort the channels user list:
449 */
450 function sort_user_list($list) : array
451 {
452 if (empty($list))
453 return $list;
454
455 $new = [];
456 foreach($list as $k => $user)
457 {
458 if (!property_exists($user,"level"))
459 {
460 $new["rest"][] = $user;
461 $list[$k] = NULL;
462 }
463 else if (strstr($user->level,"Y"))
464 {
465 $new["Y"][] = $user;
466 $list[$k] = NULL;
467 }
468 else if (strstr($user->level,"q"))
469 {
470 $new["q"][] = $user;
471 $list[$k] = NULL;
472 }
473 else if (strstr($user->level,"a"))
474 {
475 $new["a"][] = $user;
476 $list[$k] = NULL;
477 }
478 else if (strstr($user->level,"o"))
479 {
480 $new["o"][] = $user;
481 $list[$k] = NULL;
482 }
483 else if (strstr($user->level,"h"))
484 {
485 $new["h"][] = $user;
486 $list[$k] = NULL;
487 }
488 else if (strstr($user->level,"v"))
489 {
490 $new["v"][] = $user;
491 $list[$k] = NULL;
492 }
493
494 }
495
496 unset($list);
497 $list = [];
498 if (isset($new["q"]))
499 foreach($new["q"] as $u)
500 $list[] = $u;
501
502 if (isset($new["a"]))
503 foreach($new["a"] as $u)
504 $list[] = $u;
505
506 if (isset($new["o"]))
507 foreach($new["o"] as $u)
508 $list[] = $u;
509
510 if (isset($new["h"]))
511 foreach($new["h"] as $u)
512 $list[] = $u;
513
514 if (isset($new["v"]))
515 foreach($new["v"] as $u)
516 $list[] = $u;
517
518 if (isset($new["rest"]))
519 foreach($new["rest"] as $u)
520 $list[] = $u;
521
522 return $list;
523 }