Skip to content

Commit

Permalink
Merge pull request #244 from CleanTalk/spbc_widget.ag
Browse files Browse the repository at this point in the history
Security dashboard widget
  • Loading branch information
alexandergull authored Sep 15, 2023
2 parents b678627 + 686c1ce commit 9d74acb
Show file tree
Hide file tree
Showing 13 changed files with 865 additions and 23 deletions.
1 change: 1 addition & 0 deletions css/spbc-dashboard-widget.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions css/src/spbc-dashboard-widget.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#spbc_widget_wrapper {
position: relative;
width: 100%;
height: 100%;
}
.spbc_widget_top_links {
text-align: right;
padding: 0 12px;
height: 32px;
}
.spbc_widget_settings_link{margin: 0 0 0 10px;}
.spbc_widget_refresh_link{}

.spbc_preloader{
display: none;
float: left;
width: 20px; height: 20px;
margin: 0 10px;
}

.spbc_widget_hr{
width: 100%;
}

.spbc_widget_block_header{
font-size: 18px !important;
margin-left: 12px !important;
}

.spbc_widget_block{
display: block; position: relative;
padding: 12px;
}
.spbc_widget_chart_wrapper{
margin-right: 10px;
height: 300px;
}

.spbc_widget_block table{
width: 100%;
text-align: left;
}
.spbc_widget_block table tr{margin-bottom: 10px;}
.spbc_widget_block table th{
text-align: left;
padding: 10px 0 5px 10px;;
border-bottom: 2px solid gray;

}
.spbc_widget_block table td{
text-align: left;
padding: 10px 0 5px 10px;;
border-bottom: 1px solid gray;
}
#spbc_widget_wrapper .spbc_widget_block table td.spbc_widget_block__country_cell img {
width: 16px !important;
height: auto;
}

.spbc_widget_button{
display: block;
margin: 10px auto;
}
.spbc_widget_activate_button{
display: block;
margin: 10px auto; padding: 7px 15px;
font-weight: 600;
border-radius: 3px;
border: 2px solid #aaa;
background: rgba(250,50,50,0.9);
}
.spbc_widget_resolve_button{
background: rgba(50,250,50,0.9);
}
.spbc_widget_activate_header{
display: inline-block;
width: 100%;
text-align: center;
font-size: 18px !important;
}
.spbc_widget_wprapper_total_blocked{
padding: 10px 0 10px 10px;
background: #f1f1f1;
}
.spbc_widget_wprapper_total_blocked span{
position: relative;
top: 2px;
}
.spbc_widget_small_logo{
margin-right: 1em;
vertical-align: middle;
}
#spbc_widget_wrapper .spbc_widget_wprapper_total_blocked img.spbc_widget_small_logo {
width: 24px !important;
height: 24px !important;
}
#spbc_widget_button_view_all{
cursor: pointer;
border: 1px solid #0074a2;
-webkit-appearance: none;
-webkit-border-radius: 2px;
border-radius: 2px;
white-space: nowrap;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
background: #0085ba;
-webkit-box-shadow: inset 0 1px 0 rgba(120,200,230,.5), 0 1px 0 rgba(0,0,0,.15);
box-shadow: inset 0 1px 0 rgba(120,200,230,.5), 0 1px 0 rgba(0,0,0,.15);
color: #fff;
}
#spbc_widget_button_view_all:hover{color: #000 !important;}
184 changes: 184 additions & 0 deletions inc/spbc-admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use CleantalkSP\SpbctWP\Scanner\Cure;
use CleantalkSP\SpbctWP\Escape;
use CleantalkSP\SpbctWP\Scanner\FileMonitoringModule\FileMonitoringTabData;
use CleantalkSP\Variables\Post;
use CleantalkSP\Variables\Server;
Expand Down Expand Up @@ -987,3 +988,186 @@ function spbc_admin__change_plugin_description($all_plugins)
}
return $all_plugins;
}


/**
* Add the statistics widget to the dashboard.
* @return void
* @psalm-suppress UndefinedFunction
*/
function spbc_dashboard_statistics_widget()
{
global $spbc;

$actual_plugin_name = SPBC_NAME;
if (isset($spbc->data['wl_brandname']) && $spbc->data['wl_brandname'] !== SPBC_NAME) {
$actual_plugin_name = $spbc->data['wl_brandname'];
}

if ( current_user_can('activate_plugins') ) {
wp_add_dashboard_widget(
'spbc_dashboard_statistics_widget',
$actual_plugin_name,
'spbc_dashboard_statistics_widget_output'
);
}
}

/**
* Generate statistics widget output HTML.
* @param $_post
* @param $_callback_args
* @return void
*/
function spbc_dashboard_statistics_widget_output($_post, $_callback_args)
{
global $spbc, $current_user;

$actual_plugin_name = SPBC_NAME;
if (isset($spbc->data['wl_brandname']) && $spbc->data['wl_brandname'] !== SPBC_NAME) {
$actual_plugin_name = $spbc->data['wl_brandname'];
}

echo "<div id='spbc_widget_wrapper'>";
?>
<div class='spbc_widget_top_links'>
<img src="<?php echo Escape::escUrl(SPBC_PATH . '/images/preloader.gif'); ?>" class='spbc_preloader'>
<?php
echo sprintf(
__("%sRefresh%s", 'security-malware-firewall'),
"<a href='#spbc_widget' class='spbc_widget_refresh_link'>",
"</a>"
); ?>
<?php
echo sprintf(
__("%sConfigure%s", 'security-malware-firewall'),
"<a href='{$spbc->settings_link}' class='spbc_widget_settings_link'>",
"</a>"
); ?>
</div>
<form id='spbc_refresh_form' method='POST' action='#spbc_widget'>
<input type='hidden' name='spbc_brief_refresh' value='1'>
</form>
<h4 class='spbc_widget_block_header' style='margin-left: 12px;'><?php
_e('7 days Security FireWall and Bruteforce stats', 'security-malware-firewall'); ?></h4>
<div class='spbc_widget_block spbc_widget_chart_wrapper'>
<canvas id='spbc_widget_chart' ></canvas>
</div>
<h4 class='spbc_widget_block_header'><?php
_e('10 last actions in WP Dashboard', 'security-malware-firewall'); ?></h4>
<hr class='spbc_widget_hr'>
<?php
// todo if new brand brief method will be implemented, adopt this scum
if (
! spbc_api_key__is_correct() ||
(isset($spbc->data['brief_data']['error_no']) && $spbc->data['brief_data']['error_no'] == 6)
) {
?>
<div class='spbc_widget_block'>
<form action='<?php
echo $spbc->settings_link; ?>' method='POST'>
<h2 class='spbc_widget_activate_header'><?php
_e('Get Access key to activate Security protection!', 'security-malware-firewall'); ?></h2>
<input class='spbc_widget_button spbc_widget_activate_button' type='submit' name='get_apikey_auto'
value='ACTIVATE'/>
</form>
</div>
<?php
} elseif ( ! empty($spbc->data['brief_data']['error']) ) {
echo '<div class="spbc_widget_block">'
. '<h2 class="spbc_widget_activate_header">'
. sprintf(
__('Something went wrong! Error: "%s".', 'security-malware-firewall'),
"<u>{$spbc->brief_data['error']}</u>"
)
. '</h2>';
if ( $spbc->user_token && ! $spbc->white_label ) {
echo '<h2 class="spbc_widget_activate_header">'
. __('Please, visit your Dashboard.', 'security-malware-firewall')
. '</h2>'
. '<a target="_blank" href="https://cleantalk.org/my?user_token=' . $spbc->user_token . '&cp_mode=spbc">'
. '<input class="spbc_widget_button spbc_widget_activate_button spbc_widget_resolve_button" type="button" value="VISIT CONTROL PANEL">'
. '</a>';
}
echo '</div>';
}

if ( spbc_api_key__is_correct() && empty($spbc->data['brief_data']['error']) ) {
?>
<div class='spbc_widget_block'>
<table cellspacing="0">
<tr>
<th><?php
_e('Date', 'security-malware-firewall'); ?></th>
<th><?php
_e('IP', 'security-malware-firewall'); ?></th>
<th><?php
_e('Login', 'security-malware-firewall'); ?></th>
<th><?php
_e('Action', 'security-malware-firewall'); ?></th>
</tr>
<?php
foreach ( $spbc->brief_data['last_actions'] as $val ) { ?>
<tr>
<td><?php
echo Escape::escHtml($val['date']); ?></td>

<td><?php
echo Escape::escHtml($val['ip']); ?></td>

<td><?php
echo Escape::escHtml($val['login']); ?></td>

<td><a class="spbc_auto_link" title="<?php echo Escape::escHtml($val['action_url']) ?>"><?php
echo Escape::escHtml($val['parsed_action']); ?></td>
</tr>
<?php
} ?>
</table>
<?php
if ( $spbc->user_token && ! $spbc->data["wl_mode_enabled"] ) { ?>
<a target='_blank' href='https://cleantalk.org/my?user_token=<?php
echo Escape::escHtml($spbc->user_token); ?>&cp_mode=security'>
<input class='spbc_widget_button' id='spbc_widget_button_view_all' type='button' value='View all'>
</a>
<?php
} ?>
</div>

<?php
}
// Notice at the bottom
if ( isset($current_user) && in_array('administrator', $current_user->roles) ) {
$brief_data_total_count = $spbc->data['brief_data']['total_count'];
if ( !empty($brief_data_total_count) && $brief_data_total_count > 0 ) {
echo '<div class="spbc_widget_wprapper_total_blocked">'
. ($spbc->data["wl_mode_enabled"] ? '' : '<img src="' . SPBC_PATH . '/images/logo_small.png" class="spbc_widget_small_logo"/>')
. '<span title="'
. sprintf(
/* translators: %s: Number of spam messages */
__(
'%s%s%s has blocked %s attacks for all time. The statistics are automatically updated every 24 hours.',
'security-malware-firewall'
),
! $spbc->data["wl_mode_enabled"] ? '<a href="https://cleantalk.org/my/?user_token=' . $spbc->user_token . '&utm_source=wp-backend&utm_medium=dashboard_widget&cp_mode=spbc" target="_blank">' : '',
$actual_plugin_name,
! $spbc->data["wl_mode_enabled"] ? '</a>' : '',
'<b>' . number_format($brief_data_total_count, 0, ',', ' ') . '</b>'
)
. '</span>'
. (! $spbc->white_label && ! $spbc->data["wl_mode_enabled"]
? '<br><br>'
. '<b style="font-size: 16px;">'
. sprintf(
__('Do you like CleanTalk? %sPost your feedback here%s.', 'security-malware-firewall'),
'<u><a href="https://wordpress.org/support/plugin/security-malware-firewall/reviews/#new-post" target="_blank">',
'</a></u>'
)
. '</b>'
: ''
)
. '</div>';
}
}
echo '</div>';
}
32 changes: 9 additions & 23 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,11 @@ function spbc_settings__register()
'description' => __('Shows the firewall counters in the admin bar. Counts only the local database.', 'security-malware-firewall'),
'parent' => 'admin_bar__show',
),
'wp__dashboard_widget__show' => array(
'type' => 'field',
'title' => __('Security brief report widget', 'security-malware-firewall'),
'description' => __('Shows the brief widget on the main admin page', 'security-malware-firewall'),
),
)
),

Expand Down Expand Up @@ -1918,29 +1923,10 @@ function spbc_field_security_logs__prepare_data(&$table)
$page = "<a href='" . $url . "' target='_blank'>" . $url . "</a>";
}

$action = '';
$is_add_time = true;
if (!is_null($url)) {
switch ($url) {
case '/wp-admin/edit.php':
$action = __('Viewing the posts list', 'security-malware-firewall');
break;
case '/wp-admin/edit.php?post_type=page':
$action = __('Viewing the pages list', 'security-malware-firewall');
break;
case (preg_match('#/wp-admin/post.php\?post=[\d\w]+&action=edit#', $url) ? true : false):
$action = __('Editing post', 'security-malware-firewall');
break;
case (preg_match('#/wp-admin/plugins.php\?action=activate#', $url) ? true : false):
$action = __('Activate plugin', 'security-malware-firewall');
$is_add_time = false;
break;
case (preg_match('#/wp-admin/plugins.php\?action=deactivate#', $url) ? true : false):
$action = __('Deactivate plugin', 'security-malware-firewall');
$is_add_time = false;
break;
}
}

$action = spbc_parse_action_from_admin_page_uri($url);
$action = !empty($action['parsed_action']) ? $action['parsed_action'] : '';
$is_add_time = !empty($action['add_time']) ? $action['add_time'] : false;

switch ($row->event) {
case 'view':
Expand Down
Loading

0 comments on commit 9d74acb

Please sign in to comment.