]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blobdiff - server-bans/index.php
Logs: show search pane (on desktop)
[irc/unrealircd/unrealircd-webpanel.git] / server-bans / index.php
index 688b7936759d522fc03d6a5f24aa3a060ae42c56..a83205de1e8fd31bf118ca7834c6bc7b23efc996 100644 (file)
@@ -1,18 +1,20 @@
 <?php
-require_once "../common.php";
-
-require_once "../header.php";
+require_once "../inc/common.php";
+require_once "../inc/header.php";
 
 if (!empty($_POST))
 {
+       require_once "../inc/connection.php";
 
-       do_log($_POST);
-
-       if (isset($_POST['tklch']) && !empty($_POST['tklch'])) // User has asked to delete these tkls
+       if (!empty($_POST['tklch'])) // User has asked to delete these tkls
        {
-               foreach ($_POST as $key => $value) {
-                       foreach ($value as $tok) {
-                               $tok = explode(",", $tok);
+               if (!current_user_can(PERMISSION_SERVER_BAN_DEL))
+               {
+                       Message::Fail("Could not delete: Permission denied");
+               }
+               else {
+                       foreach ($_POST['tklch'] as $key => $value) {
+                               $tok = explode(",", $value);
                                $ban = base64_decode($tok[0]);
                                $type = base64_decode($tok[1]);
                                $success = false;
@@ -31,60 +33,58 @@ if (!empty($_POST))
                        }
                }
        }
-       elseif (isset($_POST['tkl_add']) && !empty($_POST['tkl_add']))
+       elseif (isset($_POST['do_add_ban']))
        {
-               if (!($iphost = $_POST['tkl_add']))
-                       Message::Fail("No mask was specified");
-               else if (!($bantype = (isset($_POST['bantype'])) ? $_POST['bantype'] : false))
+               if (!current_user_can(PERMISSION_SERVER_BAN_ADD))
                {
-                       Message::Fail("Unable to add Server Ban: No ban type selected");
-               } else /* It did */{
-
-                       if (
-                               (
-                                       $bantype == "gline" ||
-                                       $bantype == "gzline" ||
-                                       $bantype == "shun" ||
-                                       $bantype == "eline"
-                               ) && strpos($iphost, "@") == false
-                       ) // doesn't have full mask
-                               $iphost = "*@" . $iphost;
-
-                       $soft = ($_POST['soft']) ? true : false;
-
-                       if ($soft)
-                               $iphost = "%" . $iphost;
-                       /* duplicate code for now [= */
-                       $banlen_w = (isset($_POST['banlen_w'])) ? $_POST['banlen_w'] : NULL;
-                       $banlen_d = (isset($_POST['banlen_d'])) ? $_POST['banlen_d'] : NULL;
-                       $banlen_h = (isset($_POST['banlen_h'])) ? $_POST['banlen_h'] : NULL;
-                       $duration = "";
-                       if (!$banlen_d && !$banlen_h && !$banlen_w)
-                               $duration .= "0";
-                       else {
-                               if ($banlen_w)
-                                       $duration .= $banlen_w;
-                               if ($banlen_d)
-                                       $duration .= $banlen_d;
-                               if ($banlen_h)
-                                       $duration .= $banlen_h;
-                       }
-                       $msg_msg = ($duration == "0" || $duration == "0w0d0h") ? "permanently" : "for " . rpc_convert_duration_string($duration);
-                       $reason = (isset($_POST['ban_reason'])) ? $_POST['ban_reason'] : "No reason";
-                       if ($bantype == "qline") {
-                               if ($rpc->nameban()->add($iphost, $reason, $duration))
-                                       Message::Success("Name Ban set against \"$iphost\": $reason");
-                               else
-                                       Message::Fail("Name Ban could not be set against \"$iphost\": $rpc->error");
-                       } elseif ($bantype == "except") {
-                               if ($rpc->serverbanexception()->add($iphost, "", $duration, $reason))
-                                       Message::Success("Exception set for \"$iphost\": $reason");
-                               else
-                                       Message::Fail("Exception could not be set \"$iphost\": $rpc->error");
-                       } else if ($rpc->serverban()->add($iphost, $bantype, $duration, $reason)) {
-                               Message::Success("Host / IP: $iphost has been $bantype" . "d $msg_msg: $reason");
+                       Message::Fail("Could not add: Permission denied");
+               }
+               else
+               {
+                       if (empty($_POST['ban_host']) || empty($_POST['ban_type']))
+                       {
+                               Message::Fail("Unable to add Server Ban: No host or ban type selected");
                        } else
-                               Message::Fail("The $bantype against \"$iphost\" could not be added: $rpc->error");
+                       {
+                               $ban_host = $_POST['ban_host'];
+                               $ban_type = $_POST['ban_type'];
+                               $ban_soft = empty($_POST['ban_soft']) ? false : true;
+                               $ban_duration = $_POST['ban_duration'] ?? 0;
+                               $ban_reason = $_POST['ban_reason'] ?? '';
+                               if (!str_contains($ban_host, "@"))
+                                       $ban_host = "*@$ban_host"; // prefix ban with *@ if no @ present
+                               if ($ban_soft)
+                                       $ban_host = "%$ban_host"; // prefix ban with % if soft-ban
+                               if ($rpc->serverban()->add($ban_host, $ban_type, $ban_duration, $ban_reason))
+                               {
+                                       Message::Success("Ban added on ".htmlspecialchars($ban_host));
+                               } else {
+                                       $success = false;
+                                       if (($rpc->errno == -1001) && !empty($_POST['edit_existing']))
+                                       {
+                                               // existing one = del + add
+                                               // and yeah we do this after add() fails because then we now
+                                               // at least the syntax and fields and everything are OK.
+                                               // This so we don't accidentally remove a ban and the add fails
+                                               // causing the edit to result in a deletion.
+                                               $e = explode(":", $_POST['edit_existing'], 2);
+                                               if (count($e) == 2)
+                                               {
+                                                       if ($rpc->serverban()->delete($e[1], $e[0]))
+                                                       {
+                                                               /* Good, now try the add operation */
+                                                               if ($rpc->serverban()->add($ban_host, $ban_type, $ban_duration, $ban_reason))
+                                                               {
+                                                                       Message::Success("Ban successfully modified: ".htmlspecialchars($ban_host));
+                                                                       $success = true;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                                       if (!$success)
+                                               Message::Fail("The ".htmlspecialchars($ban_type)." on ".htmlspecialchars($ban_host)." could not be added: $rpc->error / $rpc->errno");
+                               }
+                       }
                }
        }
        elseif (isset($_POST['search_types']) && !empty($_POST['search_types']))
@@ -93,137 +93,86 @@ if (!empty($_POST))
        }
 }
 
-$tkl = $rpc->serverban()->getAll();
 ?>
 <h4>Server Bans Overview</h4>
 Here are all your network bans, from K-Lines to G-Lines, it's all here.<br><br>
-<p><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
-                       Add entry
-       </button></p></table>
-       <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
+Click on an entry to edit it.
+<!-- Top add button -->
+<p><div class="btn btn-primary" onclick="add_ban()" <?php echo (current_user_can(PERMISSION_SERVER_BAN_ADD)) ? "" : "disabled"; ?>>
+Add Ban</div></p></table>
+
+<!-- Add/edit ban -->
+       <div class="modal fade" id="ban_add" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
-               <div class="modal-content">
-               <div class="modal-header">
-                       <h5 class="modal-title" id="myModalLabel">Add new Server Ban</h5>
-                       <button type="button" class="close" data-dismiss="modal" aria-label="Close">
-                       <span aria-hidden="true">&times;</span>
-                       </button>
-               </div>
-               <div class="modal-body">
-               
-               <form  method="post">
-                       <div class="align_label">IP / Host: </div> <input class="curvy" type="text" id="tkl_add" name="tkl_add"><br>
-                       <div class="align_label">Ban Type: </div> <select class="curvy" name="bantype" id="bantype">
-                               <option value=""></option>
-                               <optgroup label="Bans">
-                                       <option value="kline">Kill Line (KLine)</option>
-                                       <option value="gline">Global Kill Line (GLine)</option>
-                                       <option value="zline">Zap Line (ZLine)</option>
-                                       <option value="gzline">Global Zap Line (GZLine)</option>
-                                       
-                               </optgroup>
-                               <optgroup label="Restrictions">
-                                       <option value="local-qline">Reserve Nick Locally(QLine)</option>
-                                       <option value="qline">Reserve Nick Globally (QLine)</option>
-                                       <option value="shun">Shun</option>
-
-                               </optgroup>
-                               <optgroup label="Settings">
-                                       <option value="except">Global Exception (ELine)</option>
-                                       <option value="local-exception">Local Exception (ELine)</option>
-                               </optgroup>
-                       </select><br>
-                       <div class="align_label"><label for="banlen_w">Duration: </label></div>
-                                       <select class="curvy" name="banlen_w" id="banlen_w">
-                                                       <?php
-                                                       for ($i = 0; $i <= 56; $i++)
-                                                       {
-                                                               if (!$i)
-                                                                       echo "<option value=\"0w\"></option>";
-                                                               else
-                                                               {
-                                                                       $w = ($i == 1) ? "week" : "weeks";
-                                                                       echo "<option value=\"$i" . "w\">$i $w" . "</option>";
-                                                               }
-                                                       }
-                                                       ?>
-                                       </select>
-                                       <select class="curvy" name="banlen_d" id="banlen_d">
-                                                       <?php
-                                                       for ($i = 0; $i <= 31; $i++)
-                                                       {
-                                                               if (!$i)
-                                                                       echo "<option value=\"0d\"></option>";
-                                                               else
-                                                               {
-                                                                       $d = ($i == 1) ? "day" : "days";
-                                                                       echo "<option value=\"$i" . "d\">$i $d" . "</option>";
-                                                               }
-                                                       }
-                                                       ?>
-                                       </select>
-                                       <select class="curvy" name="banlen_h" id="banlen_h">
-                                                       <?php
-                                                       for ($i = 0; $i <= 24; $i++)
-                                                       {
-                                                               if (!$i)
-                                                                       echo "<option value=\"0d\"></option>";
-                                                               else
-                                                               {
-                                                                       $h = ($i == 1) ? "hour" : "hours";
-                                                                       echo "<option value=\"$i" . "h\">$i $h" . "</option>";
-                                                               }
-                                                       }
-                                                       ?>
-                                       </select>
-                                       <br><div class="align_label"><label for="ban_reason">Reason: </label></div>
-                                       <input class="curvy input_text" type="text" id="ban_reason" name="ban_reason"><br>
-                                       <input class="curvy input_text" type="checkbox" id="soft" name="soft">Don't affect logged-in users (soft)
-                               
+               <form method="post">
+                       <input name="edit_existing" type="hidden" id="edit_existing" value="">
+                       <div class="modal-content">
+                               <div class="modal-header">
+                                       <h5 class="modal-title" id="ban_add_title">Add server ban</h5>
+                                       <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                                       <span aria-hidden="true">&times;</span></button>                
+                               </div>
+                               <div class="modal-body">
+                                       <div class="form-group">
+                                               <label for="ban_host">IP / Host</label>
+                                               <input name="ban_host" type="text" class="form-control" id="ban_host" aria-describedby="ban_host_help" value="" required>
+                                               <small id="ban_host_help" class="form-text text-muted">IP or host on which the ban is applied.</small>
+                                       </div>
+                                       <div class="form-group">
+                                               <label for="ban_type">Type</label><br>
+                                               <select class="curvy" name="ban_type" id="ban_type">
+                                                       <option value=""></option>
+                                                       <optgroup label="Bans">
+                                                       <option value="kline">Local Kill (K-Line)</option>
+                                                       <option value="gline">Global Kill (G-Line)</option>
+                                                       <option value="zline">Local Z-Line</option>
+                                                       <option value="gzline">Global Z-line</option>
+                                                       </optgroup>
+                                               </select>
+                                               <small id="ban_type_help" class="form-text text-muted">Usually K-Line or G-Line. Use Z-Lines with care.</small>
+                                       </div>
+                                       <div class="form-group">
+                                               <input class="curvy input_text" type="checkbox" id="ban_soft" name="ban_soft"><label for="ban_soft">Soft-ban</label><br>
+                                               <small id="ban_soft_help" class="form-text text-muted">Ban does not affect logged in users</small>
+                                       </div>
+                                       <div class="form-group">
+                                               <label for="ban_duration">Duration</label>
+                                               <input name="ban_duration" type="text" class="form-control" id="ban_duration" aria-describedby="ban_duration_help" value="" placeholder="(empty means permanent ban)">
+                                               <small id="ban_duration_help" class="form-text text-muted">Duration of the ban in seconds, or in a format like 1d for 1 day. Leave empty for permanent ban</small>
+                                       </div>
+                                       <div class="form-group">
+                                               <label for="ban_reason">Reason</label>
+                                               <input name="ban_reason" type="text" class="form-control" id="ban_reason" aria-describedby="ban_reason_help" value="">
+                                               <small id="ban_reason_help" class="form-text text-muted">Reason of the ban (shown to the banned user)</small>
+                                       </div>
+                               </div>
+                                                               
+                               <div class="modal-footer">
+                                       <button id="CloseButton" type="button" id="cancel_add_ban" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
+                                       <button type="submit" name="do_add_ban" id="do_add_ban" class="btn btn-primary">Add Ban</button>
+                               </div>
                        </div>
-                       
-               <div class="modal-footer">
-                       <button id="CloseButton" type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
-                       <button type="submit" action="post" class="btn btn-danger">Add Ban</button>
-                       </form>
-               </div>
-               </div>
+               </form>
        </div>
        </div>
 
-       <table class="container-xxl table table-sm table-responsive caption-top table-striped">
-       <thead class="table-primary">
+       <!-- The banlist table -->
        <form method="post">
-       <th scope="col"><input type="checkbox" label='selectall' onClick="toggle_tkl(this)" /></th>
-       <th scope="col">Mask</th>
-       <th scope="col">Type</th>
-       <th scope="col">Duration</th>
-       <th scope="col">Reason</th>
-       <th scope="col">Set By</th>
-       <th scope="col">Set On</th>
-       <th scope="col">Expires</th>
+       <table id="data_list" class="table-striped display responsive nowrap" style="width:100%">
+       <thead class="table-primary">
+               <th scope="col"><input type="checkbox" label='selectall' onClick="toggle_tkl(this)" /></th>
+               <th scope="col">Mask</th>
+               <th scope="col">Type</th>
+               <th scope="col">Duration</th>
+               <th scope="col">Reason</th>
+               <th scope="col">Set By</th>
+               <th scope="col">Set On</th>
+               <th scope="col">Expires</th>
        </thead>
-       <tbody>
-       <?php
-               foreach($tkl as $tkl)
-               {
-                       $set_in_config = ((isset($tkl->set_in_config) && $tkl->set_in_config) || ($tkl->set_by == "-config-")) ? true : false;
-                       echo "<tr scope='col'>";
-                       if ($set_in_config)
-                               echo "<td scope=\"col\"></td>";
-                       else
-                               echo "<td scope=\"col\"><input type=\"checkbox\" value='" . base64_encode($tkl->name).",".base64_encode($tkl->type) . "' name=\"tklch[]\"></td>";
-                       echo "<td scope=\"col\">".$tkl->name."</td>";
-                       echo "<td scope=\"col\">".$tkl->type_string."</td>";
-                       echo "<td scope=\"col\">".$tkl->duration_string."</td>";
-                       echo "<td scope=\"col\">".$tkl->reason."</td>";
-                       $set_by = $set_in_config ? "<span class=\"badge rounded-pill badge-secondary\">Config</span>" : show_nick_only($tkl->set_by);
-                       echo "<td scope=\"col\">".$set_by."</td>";
-                       echo "<td scope=\"col\">".$tkl->set_at_string."</td>";
-                       echo "<td scope=\"col\">".$tkl->expire_at_string."</td>";
-                       echo "</tr>";
-               }
-       ?></tbody></table><p><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal2">
+       </table>
+
+       <!-- Delete button -->
+       <p><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal2" <?php echo (current_user_can(PERMISSION_SERVER_BAN_DEL)) ? "" : "disabled"; ?>>
        Delete selected
        </button></p>
        <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="confirmModalCenterTitle" aria-hidden="true">
@@ -248,4 +197,114 @@ Here are all your network bans, from K-Lines to G-Lines, it's all here.<br><br>
        </div>
        </div></form></div></div>
 
-<?php require_once 'footer.php'; ?>
+<script>
+let data_list_table = null;
+
+$(document).ready( function () {
+       args = {
+               'responsive': true,
+               'fixedHeader': {
+                       header: true,
+                       headerOffset: 53
+               },
+               'ajax': {
+                       'url': '<?php echo get_config("base_url"); ?>api/server-bans.php',
+                       dataSrc: ''
+               },
+               'columns': [
+                       { 'data': 'Select', 'responsivePriority': 1 },
+                       { 'data': 'Mask', 'responsivePriority': 2, 'className':'virtuallink' },
+                       { 'data': 'Type', 'responsivePriority': 3 },
+                       { 'data': 'Duration', 'responsivePriority': 4 },
+                       { 'data': 'Reason', 'responsivePriority': 5, 'render': DataTable.render.ellipsis(50, false) },
+                       { 'data': 'Set By', 'responsivePriority': 6 },
+                       { 'data': 'Set On', 'responsivePriority': 7 },
+                       { 'data': 'Expires', 'responsivePriority': 8 },
+               ],
+               'pageLength':100,
+               'order':[[1,'asc']],
+               createdRow: function(row) {
+                       var td = jQuery(row).find(".truncate");
+                       td.each(function(index, el) {
+                               jQuery(this).attr("title", jQuery(this).html());
+                               });
+                       },
+       };
+       /* Only show filter pane on desktop */
+       if (window.innerWidth > 800)
+       {
+               args['dom'] = 'Pfrtip';
+               args['searchPanes'] = {
+                       'initCollapsed': 'true',
+                       'columns': [2,3,5],
+                       'dtOpts': {
+                               select: { style: 'multi'},
+                               order: [[ 1, "desc" ]]
+                       },
+               }
+       }
+
+       data_list_table = $('#data_list').DataTable(args);
+
+       $('#data_list').on( 'click', 'td', function () {
+               edit_ban(this);
+       } );
+} );
+
+       function edit_ban(e)
+       {
+               /* The first column is the 'Select' column */
+               if (data_list_table.cell(e).index().column == 0)
+                       return;
+               /* For all the other columns we try to popup and edit screen */
+               var data = data_list_table.row(e).data();
+               $host = data['Mask'];
+               if ($host.startsWith('%'))
+               {
+                       $('#ban_host').val($host.substring(1));
+                       $('#ban_soft').prop('checked', true);
+               } else {
+                       $('#ban_host').val($host);
+                       $('#ban_soft').prop('checked', false);
+               }
+               $type = data['Type'].replace('Soft ','');
+               if ($type == 'Global Z-Line')
+                       $type = 'gzline';
+               else if ($type == 'Z-Line')
+                       $type = 'zline';
+               else if ($type == 'G-Line')
+                       $type = 'gline';
+               else
+                       $type = 'kline';
+               $('#ban_type').val($type);
+               if (data['Duration'] == 'permanent')
+                       $('#ban_duration').val();
+               else
+                       $('#ban_duration').val(data['Duration']);
+               $('#ban_reason').val(data['Reason']);
+               $('#do_del_ban').show();
+               $('#ban_add_title').html("Edit server ban");
+               $('#do_add_ban').html("Modify Ban");
+               $('#edit_existing').val($type+':'+data['Mask']);
+               $('#ban_add').modal('show');
+       }
+
+       // This is in a function because a canceled edit_rpc_server otherwise causes a prefilled effect
+       function add_ban()
+       {
+               $('#edit_existing').val("");
+               $('#ban_host').val("");
+               $('#ban_type').val("");
+               $('#ban_duration').val("");
+               $('#ban_reason').val("");
+               $('#ban_soft').prop('checked', false);
+               $('#do_del_ban').hide();
+               $('#ban_add_title').html("Add server ban");
+               $('#do_add_ban').html("Add Ban");
+               $('#ban_add').modal('show');
+       }
+
+
+</script>
+
+<?php require_once '../inc/footer.php'; ?>