Skip to content

Commit

Permalink
Web action logs(discord webhook)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobsRKM committed Jun 23, 2024
1 parent 1fc0534 commit bc968cb
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ DB_PORT_SKINS=3306
## ==============END========================###
#--------------------------------------------------------------------------

DISCORD_WEBHOOK=''

172 changes: 172 additions & 0 deletions app/Helpers/CommonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use App\Models\Appeal\Appeal;
use App\Models\Report\Report;
use App\Models\SaBan;
use App\Models\SaMute;
use Carbon\Carbon;
use Carbon\CarbonInterval;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -141,4 +145,172 @@ public static function getCSRankImage($rank)

return '<img src="' . asset($imagePath) . '" class="cs2rating" alt="CS Rating">';
}

private static function getActionColor($action)
{
switch ($action) {
case 'ban':
return 0xFF0000; // Red
case 'unban':
return 0x00FF00; // Green
case 'mute':
return 0xFFFF00; // Yellow
case 'unmute':
return 0x00FFFF; // Cyan
default:
return 0xFFFFFF; // White
}
}

private static function getActionVerb($action)
{
switch ($action) {
case 'ban':
return 'banned';
case 'unban':
return 'unbanned';
case 'mute':
return 'muted';
case 'unmute':
return 'unmuted';
case 'appeal':
return 'appealed';
case 'report':
return 'reported';
default:
return 'acted upon';
}
}

public static function sendActionLog($action, $actionId)
{
if(!empty(env('DISCORD_WEBHOOK'))) {
switch ($action) {
case "ban":
$actionDetails = SaBan::where('id', $actionId)->first();
$username = $actionDetails->player_name;
$steamId = $actionDetails->player_steamid;
$admin = "[$actionDetails->admin_name](https://steamcommunity.com/profiles/{$actionDetails->admin_steamid})";
$reason = $actionDetails->reason;
break;
case "unban":
$actionDetails = SaBan::where('id', $actionId)->first();
$username = $actionDetails->player_name;
$steamId = $actionDetails->player_steamid;
$admin = "[$actionDetails->admin_name](https://steamcommunity.com/profiles/{$actionDetails->admin_steamid})";
break;
case "mute":
$actionDetails = SaMute::where('id', $actionId)->first();
$username = $actionDetails->player_name;
$steamId = $actionDetails->player_steamid;
$admin = "[$actionDetails->admin_name](https://steamcommunity.com/profiles/{$actionDetails->admin_steamid})";
$reason = $actionDetails->reason;
break;
case "unmute":
$actionDetails = SaMute::where('id', $actionId)->first();
$username = $actionDetails->player_name;
$steamId = $actionDetails->player_steamid;
$admin = "[$actionDetails->admin_name](https://steamcommunity.com/profiles/{$actionDetails->admin_steamid})";
break;
case "appeal":
$actionDetails = Appeal::where('id', $actionId)->first();
$username = $actionDetails->name;
$steamId = $actionDetails->steamid;
$admin = null;
$reason = $actionDetails->reason;
break;
case "report":
$actionDetails = Report::where('id', $actionId)->first();
$username = $actionDetails->nickname;
$steamId = $actionDetails->steamid;
$admin = null;
$reason = $actionDetails->comments;
break;
}
$appUrl = env('APP_URL');
$website = "[CSS-BANS]($appUrl)";
if($action == 'appeal'){
$website = "[View Appeal]($appUrl/appeal/$actionDetails->id)";
}else if($action == 'ban' || $action == 'unban') {
$website = "[View Bans]($appUrl/list/bans)";
}else if($action == 'mute' || $action == 'unmute') {
$website = "[View Mutes]($appUrl/list/mutes)";
}else if($action == 'report'){
$website = "[View Report]($appUrl/reports/show/$actionDetails->id)";
}

$actionVerb = CommonHelper::getActionVerb($action);

$client = new Client();

$embed = [
'title' => ucfirst($action) . ' Notification',
'description' => "User **{$username}** has been **{$actionVerb}**.",
'color' => CommonHelper::getActionColor($action),
'fields' => [
[
'name' => 'Username',
'value' => $username,
'inline' => true,
],
[
'name' => 'Steam Profile',
'value' => "[View Profile](https://steamcommunity.com/profiles/{$steamId})",
'inline' => true,
],
[
'name' => 'Steam ID',
'value' => $steamId,
'inline' => true,
],
[
'name' => 'Action',
'value' => ucfirst($action),
'inline' => true,
],
[
'name' => 'Reason',
'value' => $reason ?? 'No reason provided'
],
[
'name' => 'Website',
'value' => $website,
'inline' => true,
],
],
'thumbnail' => [
'url' => 'https://i.imgur.com/n2JVS9z.png'
],
'author' => [
'name' => 'CSS-BANS',
'url' => 'https://github.com/counterstrikesharp-panel/css-bans',
'icon_url' => 'https://i.imgur.com/n2JVS9z.png'
],
'footer' => [
'text' => 'Powered by CSS-BANS. Action performed at ' . Carbon::now()->format('Y-m-d H:i:s'),
'icon_url' => 'https://i.imgur.com/n2JVS9z.png'
],
];
if(!empty($admin)) {
$embed['fields'][] = [
'name' => 'Admin',
'value' => $admin,
'inline' => true,
];
}

$data = [
'content' => '',
'embeds' => [$embed],
];

$response = $client->post(env('DISCORD_WEBHOOK'), [
'json' => $data,
]);

Log::info("discord", [
"response" => $response->getBody()
]);
}
}
}
5 changes: 3 additions & 2 deletions app/Http/Controllers/Appeal/AppealController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace App\Http\Controllers\Appeal;

use App\Helpers\CommonHelper;
use App\Http\Controllers\Controller;
use App\Mail\AppealApproved;
use App\Models\Appeal\Appeal;
Expand Down Expand Up @@ -37,8 +38,8 @@ public function store(Request $request)
$data = $request->only(['ban_type', 'steamid', 'ip', 'name', 'reason', 'email']);
$data['status'] = 'PENDING';
// Save the appeal
Appeal::create($data);

$appeal = Appeal::create($data);
CommonHelper::sendActionLog('appeal', $appeal->id);
return redirect()->route('appeals.create')->with('success', 'Appeal submitted successfully.');
}
else {
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/BansController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ public function unban(Request $request, $dataId)
$ban->status = 'UNBANNED';
$ban->ends = now();
$ban->save();
CommonHelper::sendActionLog('unban', $ban->id);
}

// If all unbans are successful, commit the transaction
DB::commit();

return response()->json(['success' => true, 'message' => __('admins.bansSuccess')]);
} catch (\Exception $e) {
// If any error occurs, rollback the transaction
Expand Down Expand Up @@ -199,6 +199,7 @@ public function store(Request $request)
$saban->ends = !empty($minutesDifference) ? CommonHelper::formatDate($validatedData['duration']): Carbon::now();
$saban->save();
$bansAdded = true;
CommonHelper::sendActionLog('ban', $saban->id);
}
DB::commit();
} catch(\Exception $e) {
Expand Down Expand Up @@ -246,6 +247,7 @@ public function update(Request $request, $id)
}
$ban->status = 'ACTIVE';
$ban->save();
CommonHelper::sendActionLog('ban', $ban->id);
return redirect()->route('list.bans')->with('success', __('admins.banUpdateSuccess'));
} catch(\Exception $e) {
Log::error('ban.update.error: ' . $e->getMessage());
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/MutesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function store(Request $request)
$samute->ends = !empty($minutesDifference) ? CommonHelper::formatDate($validatedData['duration']): Carbon::now();
$samute->save();
$mutesAdded = true;
CommonHelper::sendActionLog('mute', $samute->id);
}
DB::commit();
} catch(\Exception $e) {
Expand Down Expand Up @@ -227,6 +228,7 @@ public function update(Request $request, $id)
$mute->status = 'ACTIVE';
$mute->type = $validatedData['type'];
$mute->save();
CommonHelper::sendActionLog('unmute', $mute->id);
return redirect()->route('list.mutes')->with('success', __('admins.muteUpdate'));
} catch(\Exception $e) {
Log::error('mute.update.error: ' . $e->getMessage());
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Report/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Report;

use App\Helpers\CommonHelper;
use App\Http\Controllers\Controller;
use App\Models\Report\Report;
use App\Models\SaServer;
Expand Down Expand Up @@ -30,7 +31,8 @@ public function store(Request $request)
]);

$data = $request->only(['ban_type', 'steamid', 'ip', 'nickname', 'comments', 'name', 'email', 'server_id', 'media_link']);
Report::create($data);
$report = Report::create($data);
CommonHelper::sendActionLog('report', $report->id);

return redirect()->route('reports.create')->with('success', 'Report submitted successfully.');
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function showSettings()
'DB_DATABASE' => env('DB_DATABASE'),
'DB_USERNAME' => env('DB_USERNAME'),
'DB_PASSWORD' => env('DB_PASSWORD'),
'DISCORD_WEBHOOK' => env('DISCORD_WEBHOOK')
],
'Modules' => [
'RANKS' => env('RANKS'),
Expand Down
4 changes: 4 additions & 0 deletions resources/scss/dark/assets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4995,3 +4995,7 @@ body.layout-dark #sidebar ul.menu-categories li.menu ul.submenu>li a:before {
left: auto;
border-radius: 0;
}
.sidebar-label {
position: absolute;
margin-left: 6px;
}
18 changes: 18 additions & 0 deletions resources/views/components/menu/vertical-menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ class="feather feather-message-circle badge-icon">
</div>
</a>
</li>
<li class="{{ Request::is('*reports*') ? 'active' : '' }}">
<a href="{{ getAppSubDirectoryPath() }}/reports/list" aria-expanded="false" class="dropdown-toggle">
<div class="">
<i class="fas fa-list-alt fa-fw me-3"></i> <span>Reports</span>
@if(CommonHelper::reportCheck() > 0)
<span class="badge badge-primary sidebar-label">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round"
class="feather feather-message-circle badge-icon">
<path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path>
</svg>
{{CommonHelper::reportCheck()}}
</span>
@endif
</div>
</a>
</li>
@endif
</ul>
</li>
Expand Down

0 comments on commit bc968cb

Please sign in to comment.