Skip to content

Commit

Permalink
AdvancedBan integration
Browse files Browse the repository at this point in the history
  • Loading branch information
samerton committed May 9, 2020
1 parent 31a79d3 commit eab8551
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
The Infractions module for Nameless v2 allows you to display a list of ingame punishments on your website.

### Supported Plugins
- AdvancedBan
- LiteBans

If you would like to request plugin support, please open an issue on [GitHub](https://github.com/samerton/Nameless-Infractions/issues).
Expand All @@ -13,4 +14,4 @@ If you would like to request plugin support, please open an issue on [GitHub](ht
### Installation
- Upload the contents of the **upload** directory straight into your NamelessMC installation's directory
- Activate the module in the StaffCP -> Modules tab
- Configure your Infractions plugin information in the StaffCP -> Infractions tab
- Configure your Infractions plugin information in the StaffCP -> Infractions tab
6 changes: 4 additions & 2 deletions upload/custom/panel_templates/Default/infractions/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@
<div class="form-group">
<label for="inputPlugin">{$PLUGIN}</label>
<select class="form-control" id="inputPlugin" name="plugin">
<option value="litebans"{if $PLUGIN_VALUE eq litebans} selected{/if}>LiteBans</option>
{foreach from=$PLUGIN_OPTIONS item=item}
<option value="{$item.value}"{if $PLUGIN_VALUE eq $item.value} selected{/if}>{$item.name}</option>
{/foreach}
</select>
</div>
<hr>
<hr />
<strong>{$DATABASE_SETTINGS}</strong>
<div class="form-group">
<label for="inputHost">{$ADDRESS}</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<span class="ui label">{$infraction.action}</span>
{/if}

{if $infraction.action_id eq 1 OR $infraction.action_id eq 3}
{if $infraction.action_id lte 4}
{if $infraction.revoked == 1}
<span data-toggle="tooltip" title="{$infraction.expires_full}" class="badge badge-success">{$infraction.revoked_full}</span>
{else}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<span class="ui label">{$infraction.action}</span>
{/if}

{if $infraction.action_id eq 1 OR $infraction.action_id eq 3}
{if $infraction.action_id lte 4}
{if $infraction.revoked == 1}
<span data-toggle="tooltip" title="{$infraction.expires_full}" class="badge badge-success">{$infraction.revoked_full}</span>
{else}
Expand Down
136 changes: 136 additions & 0 deletions upload/modules/Infractions/classes/AdvancedBan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/*
* Made by Samerton
* https://github.com/samerton/Nameless-Infractions
* NamelessMC version 2.0.0-pr7
*
* License: MIT
*
* AdvancedBan class
*/

class AdvancedBan extends Infractions {

// Variables
protected $_extra;

// Constructor
public function __construct($inf_db, $language) {
parent::__construct($inf_db, $language);

if(file_exists(ROOT_PATH . '/modules/Infractions/extra.php'))
require_once(ROOT_PATH . '/modules/Infractions/extra.php');
else {
$inf_extra = array('advancedban');

$inf_extra['advancedban'] = array(
'punishments_table' => 'Punishments',
'punishment_history_table' => 'PunishmentHistory'
);
}

$this->_extra = $inf_extra['advancedban'];
}

// Retrieve a list of all infractions, either from cache or database
public function listInfractions($page, $limit){
// Cached?
$cache = $this->_cache;
$cache->setCache('infractions_infractions');
if($cache->isCached('infractions' . $page)){
$mapped_punishments = $cache->retrieve('infractions' . $page);
} else {
$this->initDB();

$total = $this->getTotal()->first()->total;
$infractions = $this->listAll($page, $limit)->results();

$mapped_punishments = array();
$staff_usernames = array();

$mapped_punishments['total'] = $total;

if(count($infractions)){
foreach($infractions as $punishment){
$staff_uuid = $punishment->operator;

if(!isset($staff_usernames[$punishment->operator])){
$staff_query = DB::getInstance()->query('SELECT uuid FROM nl2_users WHERE username = ?', array($punishment->operator));
if($staff_query->count()){
$staff_uuid = $staff_query->first()->uuid;
$staff_usernames[$punishment->operator] = $staff_uuid;
}
} else {
$staff_uuid = $staff_usernames[$punishment->operator];
}

$mapped_punishments[] = (object) array(
'id' => $punishment->id,
'name' => $punishment->name,
'uuid' => $punishment->uuid,
'reason' => $punishment->reason,
'banned_by_uuid' => $staff_uuid,
'banned_by_name' => $punishment->operator,
'removed_by_uuid' => '',
'removed_by_name' => '',
'removed_by_date' => '',
'time' => $punishment->start,
'until' => $punishment->end > 0 ? $punishment->end : null,
'ipban' => '',
'active' => $punishment->pstart ? 1 : 0,
'type' => $this->mapType($punishment->punishmentType)
);
}
}

$cache->store('infractions' . $page, $mapped_punishments, 120);
}

return $mapped_punishments;
}

// List all infractions
public function listAll($page, $limit){
$start = ($page - 1) * $limit;

return $this->_db->query(
$this->getPunishmentQuery() . ' LIMIT ?,?',
array($start, $limit)
);
}

// Get total rows
protected function getTotal(){
return $this->_db->query(
'SELECT (SELECT COUNT(*) FROM ' . $this->_extra['punishment_history_table'] . ') AS total', array()
);
}

// Get bans query
private function getPunishmentQuery(){
return 'SELECT ph.id, ph.name, ph.uuid, ph.reason, ph.operator, ph.punishmentType, p.start as pstart, ph.start, ph.end' .
' FROM ' . $this->_extra['punishments_table'] . ' AS p' .
' RIGHT JOIN (SELECT id, name, uuid, reason, operator, punishmentType, start, end FROM ' . $this->_extra['punishment_history_table'] . ') AS ph ON p.start = ph.start' .
' WHERE ph.punishmentType <> \'IP_BAN\' ORDER BY ph.start DESC';
}

// Map punishment type
private function mapType($type){
switch($type){
case 'BAN':
case 'IP_BAN':
return 'ban';

case 'KICK':
return 'kick';

case 'MUTE':
return 'mute';

case 'WARNING':
return 'warning';
}

return 'unknown';
}
}
8 changes: 6 additions & 2 deletions upload/modules/Infractions/extra.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
'mutes_table' => 'litebans_mutes',
'warnings_table' => 'litebans_warnings',
'history_table' => 'litebans_history'
)
);
),
'advancedban' => array(
'punishments_table' => 'Punishments',
'punishment_history_table' => 'PunishmentHistory'
)
);
4 changes: 2 additions & 2 deletions upload/modules/Infractions/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton and Partydragen
* https://github.com/samerton/Nameless-Infractions
* NamelessMC version 2.0.0-pr6
* NamelessMC version 2.0.0-pr7
*
* License: MIT
*
Expand All @@ -19,7 +19,7 @@ public function __construct($language, $infractions_language, $pages, $cache){

$name = 'Infractions';
$author = '<a href="https://samerton.me" target="_blank" rel="nofollow noopener">Samerton</a>';
$module_version = '1.0.0';
$module_version = '1.1.0';
$nameless_version = '2.0.0-pr7';

parent::__construct($this, $name, $author, $module_version, $nameless_version);
Expand Down
5 changes: 5 additions & 0 deletions upload/modules/Infractions/pages/infractions.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
require_once(ROOT_PATH . '/modules/Infractions/classes/LiteBans.php');
$infractions = new LiteBans($inf_db, $infractions_language);
break;
case 'advancedban':
// AdvancedBan integration
require_once(ROOT_PATH . '/modules/Infractions/classes/AdvancedBan.php');
$infractions = new AdvancedBan($inf_db, $infractions_language);
break;
default:
die('Plugin not supported!');
break;
Expand Down
16 changes: 14 additions & 2 deletions upload/modules/Infractions/pages/panel/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
die();
} else {
if(!$user->hasPermission('admincp.infractions.settings')){
require_once(ROOT_PATH . '/404.php');
require_once(ROOT_PATH . '/403.php');
die();
}
}
Expand Down Expand Up @@ -149,6 +149,18 @@
'ERRORS_TITLE' => $language->get('general', 'error')
));

// Plugin options
$plugin_options = array(
array(
'name' => 'LiteBans',
'value' => 'litebans'
),
array(
'name' => 'AdvancedBan',
'value' => 'advancedban'
)
);

$smarty->assign(array(
'PARENT_PAGE' => PARENT_PAGE,
'PAGE' => PANEL_PAGE,
Expand All @@ -158,6 +170,7 @@
'DATABASE_SETTINGS' => $infractions_language->get('infractions', 'database_settings'),
'PLUGIN' => $infractions_language->get('infractions', 'plugin'),
'PLUGIN_VALUE' => (!empty($inf_config['plugin']) ? Output::getClean($inf_config['plugin']) : 'litebans'),
'PLUGIN_OPTIONS' => $plugin_options,
'LINK_LOCATION' => $infractions_language->get('infractions', 'link_location'),
'LINK_LOCATION_VALUE' => $link_location,
'LINK_NAVBAR' => $language->get('admin', 'page_link_navbar'),
Expand All @@ -173,7 +186,6 @@
'PORT' => $infractions_language->get('infractions', 'database_port'),
'PORT_VALUE' => (!empty($inf_db['port']) ? Output::getClean($inf_db['port']) : '3306'),
'PASSWORD' => $infractions_language->get('infractions', 'database_password'),
'INFO' => $language->get('general', 'info'),
'PASSWORD_HIDDEN' => $language->get('admin', 'email_password_hidden'),
'TOKEN' => Token::get(),
'SUBMIT' => $language->get('general', 'submit')
Expand Down

0 comments on commit eab8551

Please sign in to comment.