| 1 |
<?php defined('ABSPATH') or die("Protected By WT!"); |
| 2 |
|
| 3 |
add_action('admin_menu', 'wtsec_add_menu_link'); |
| 4 |
add_action('wp_ajax_change_status', 'wtsec_ajax_changeStatus'); |
| 5 |
add_action('admin_post_change_status', 'wtsec_changeStatus'); |
| 6 |
add_action('wp_ajax_cmd', 'wtsec_cmd_ajax'); |
| 7 |
add_action('admin_post_login_form', 'wtsec_login_form'); |
| 8 |
add_action('admin_post_ajax_cmd', 'wtsec_cmd_ajax'); |
| 9 |
|
| 10 |
function wtsec_add_menu_link() |
| 11 |
{ |
| 12 |
add_menu_page( |
| 13 |
WTSEC_PAGE_TITLE, |
| 14 |
WTSEC_MENU_TITLE, |
| 15 |
'manage_options', |
| 16 |
wtsec_getRoute('dashboard'), |
| 17 |
'wtsec_index_page', |
| 18 |
'', |
| 19 |
'1' |
| 20 |
); |
| 21 |
$parent = wtsec_getRoute('dashboard'); |
| 22 |
if (WTSEC_LIBRARY_App::authorized()) { |
| 23 |
$capability = 'manage_options'; |
| 24 |
foreach (wtsec_pages() as $page => $arguments) { |
| 25 |
add_submenu_page($parent, $arguments['page_title'], $arguments['menu_title'], $capability, wtsec_getRoute($page), $arguments['function']); |
| 26 |
} |
| 27 |
add_submenu_page(null, WTSEC_LIBRARY_Localization::lmsg('sign_in'), WTSEC_LIBRARY_Localization::lmsg('sign_in'), 'manage_options', wtsec_getRoute('login'), 'wtsec_login'); |
| 28 |
add_submenu_page($parent, WTSEC_LIBRARY_Localization::lmsg('logout'), WTSEC_LIBRARY_Localization::lmsg('logout'), 'manage_options', wtsec_getRoute('logout'), 'wtsec_logout'); |
| 29 |
} else { |
| 30 |
$capability = 'manage_options'; |
| 31 |
foreach (wtsec_pages() as $page => $arguments) { |
| 32 |
add_submenu_page(null, $arguments['page_title'], $arguments['menu_title'], $capability, wtsec_getRoute($page), function () { |
| 33 |
wp_safe_redirect(wtsec_getUrl('login')); |
| 34 |
}); |
| 35 |
} |
| 36 |
add_submenu_page($parent, WTSEC_LIBRARY_Localization::lmsg('sign_in'), WTSEC_LIBRARY_Localization::lmsg('sign_in'), 'manage_options', wtsec_getRoute('login'), 'wtsec_login'); |
| 37 |
add_submenu_page(null, WTSEC_LIBRARY_Localization::lmsg('logout'), WTSEC_LIBRARY_Localization::lmsg('logout'), 'manage_options', wtsec_getRoute('logout'), 'wtsec_logout'); |
| 38 |
} |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
function wtsec_index_page() |
| 43 |
{ |
| 44 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 45 |
$services = []; |
| 46 |
if (!empty($host)) { |
| 47 |
$checks = WTSEC_LIBRARY_WT::getAllChecks($host['id']); |
| 48 |
foreach ($checks['data'] as $service => $site) { |
| 49 |
if (empty($site) || !is_array($site)) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
$site = $site[0]; |
| 53 |
switch ($service) { |
| 54 |
case "waServiceChecks": |
| 55 |
$services["wa"] = [ |
| 56 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 57 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "wa"), |
| 58 |
"response_time" => $site['responseTime']['avg'] . ' ' . WTSEC_LIBRARY_Localization::lmsg('ms'), |
| 59 |
"availability" => ceil($site['average']['uptimePercent'] * 100) . '%', |
| 60 |
"availability_percent" => ceil($site['average']['uptimePercent'] * 100), |
| 61 |
"downtime" => ceil($site['average']['totalDown'] / 1000) . ' ' . WTSEC_LIBRARY_Localization::lmsg('sec'), |
| 62 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.availability') |
| 63 |
]; |
| 64 |
break; |
| 65 |
case "sslServiceChecks": |
| 66 |
$services["ssl"] = [ |
| 67 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 68 |
"information" => wtsec_getInformation($service, $site['status']), |
| 69 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "ssl"), |
| 70 |
"days_left" => $site['daysLeft'], |
| 71 |
"issue_date" => date("Y-m-d H:i", $site['issued'] / 1000), |
| 72 |
"expiry_date" => date("Y-m-d H:i", $site['expires'] / 1000), |
| 73 |
"tls" => $site['tls'], |
| 74 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.ssl') |
| 75 |
]; |
| 76 |
break; |
| 77 |
case "decServiceChecks": |
| 78 |
$services["dec"] = [ |
| 79 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 80 |
"registrar" => $site['registrar'], |
| 81 |
"owner" => $site['owner'], |
| 82 |
"information" => wtsec_getInformation($service, $site['status']), |
| 83 |
"email" => $site['email'], |
| 84 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "dec"), |
| 85 |
"days_left" => $site['daysLeft'], |
| 86 |
"created" => date("Y-m-d", $site['created'] / 1000), |
| 87 |
"expiry_date" => date("Y-m-d", $site['expires'] / 1000), |
| 88 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.domain') |
| 89 |
]; |
| 90 |
break; |
| 91 |
case "avServiceChecks": |
| 92 |
$services["av"] = [ |
| 93 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 94 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "av"), |
| 95 |
"time_of_the_last_test" => date("Y-m-d H:i", $site['lastTestTime'] / 1000), |
| 96 |
"blacklists_entries" => $site['count'], |
| 97 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.reputation') |
| 98 |
]; |
| 99 |
break; |
| 100 |
case "cmsServiceChecks": |
| 101 |
$services["cms"] = [ |
| 102 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 103 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "cms"), |
| 104 |
"time_of_the_last_test" => date("Y-m-d H:i", $site['lastTestTime'] / 1000), |
| 105 |
"detected_keywords" => $site['count'], |
| 106 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.malicious') |
| 107 |
]; |
| 108 |
break; |
| 109 |
case "dcServiceChecks": |
| 110 |
$services["dc"] = [ |
| 111 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 112 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "dc"), |
| 113 |
"time_of_the_last_test" => date("Y-m-d H:i", $site['lastTestTime'] / 1000), |
| 114 |
"number" => $site['count'], |
| 115 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.deface') |
| 116 |
]; |
| 117 |
break; |
| 118 |
case "psServiceChecks": |
| 119 |
$services["ps"] = [ |
| 120 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 121 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "ps"), |
| 122 |
"ip" => $site['ip'], |
| 123 |
"time_of_the_last_test" => date("Y-m-d H:i", $site['lastTestTime'] / 1000), |
| 124 |
"number" => $site['count'], |
| 125 |
"tcp" => !empty($site['openTCPs']) ? implode(",", $site['openTCPs']) : '', |
| 126 |
"udp" => !empty($site['openUDPs']) ? implode(",", $site['openUDPs']) : '', |
| 127 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.port') |
| 128 |
]; |
| 129 |
break; |
| 130 |
case "wafServiceChecks": |
| 131 |
$service = "WAF"; |
| 132 |
$_service = strtolower($service); |
| 133 |
$domain = $host['hostname']; |
| 134 |
$uid = $host['id']; |
| 135 |
$status = wtsec_checkStatus($uid, $service); |
| 136 |
$services["waf"] = [ |
| 137 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $uid), |
| 138 |
"site_address" => $domain, |
| 139 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "waf"), |
| 140 |
"time_of_the_last_check" => empty($site['lastTestTime']) ? '' : date("Y-m-d H:i", $site['lastTestTime'] / 1000), |
| 141 |
"signatures" => $site['count'], |
| 142 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.firewall'), |
| 143 |
"actions" => wtsec_generateButtons($uid, $_service, $service, $status, WTSEC_SITE_URL), |
| 144 |
"chart" => json_encode((array)generateChart($site['chart']), true) |
| 145 |
]; |
| 146 |
break; |
| 147 |
case "vcServiceChecks": |
| 148 |
$service = "VC"; |
| 149 |
$_service = strtolower($service); |
| 150 |
$domain = $host['hostname']; |
| 151 |
$uid = $host['id']; |
| 152 |
$status = wtsec_checkStatus($uid, $service); |
| 153 |
$services["vc"] = [ |
| 154 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $uid), |
| 155 |
"site_address" => $domain, |
| 156 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "vc"), |
| 157 |
"signatures" => $site['signaturesCount'], |
| 158 |
"changes" => $site['fileChangesCount'], |
| 159 |
"list" => $site["list"], |
| 160 |
"actions" => wtsec_generateButtons($uid, $_service, $service, $status, WTSEC_SITE_URL), |
| 161 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.antivirus') |
| 162 |
]; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
wtsec_layout("dashboard", $services); |
| 168 |
} |
| 169 |
|
| 170 |
function generateChart($charts) |
| 171 |
{ |
| 172 |
$d = date("d"); |
| 173 |
$m = date("m"); |
| 174 |
$c = []; |
| 175 |
$first_day = $d - 6; |
| 176 |
if ($first_day < 1) { |
| 177 |
$lastmonth = (int)date('d', strtotime('last day of previous month')); |
| 178 |
$lastmonth_day = $lastmonth; |
| 179 |
$lastmonth_month = (int)date('m', strtotime('last day of previous month')); |
| 180 |
$lastmonth = $lastmonth - ($first_day * -1); |
| 181 |
for ($i = $lastmonth; $i <= $lastmonth_day; $i++) { |
| 182 |
$c[$i] = [ |
| 183 |
'day' => "{$i}/$lastmonth_month", |
| 184 |
'count' => 0, |
| 185 |
]; |
| 186 |
} |
| 187 |
$first_day = 1; |
| 188 |
} |
| 189 |
for ($i = $first_day; $i <= $d; $i++) { |
| 190 |
$c[$i] = [ |
| 191 |
'day' => "{$i}/$m", |
| 192 |
'count' => 0, |
| 193 |
]; |
| 194 |
} |
| 195 |
foreach ($charts as $chart) { |
| 196 |
$d = (int)date("d", strtotime($chart['date'])); |
| 197 |
$c[$d]['count'] = $chart['count']; |
| 198 |
} |
| 199 |
|
| 200 |
//unset keys, because the js parser sorting by key in integer |
| 201 |
$result = []; |
| 202 |
foreach ($c as $d) { |
| 203 |
$result[] = $d; |
| 204 |
} |
| 205 |
return $result; |
| 206 |
} |
| 207 |
|
| 208 |
function wtsec_getInformation($service, $status) |
| 209 |
{ |
| 210 |
$information = [ |
| 211 |
"sslServiceChecks" => [ |
| 212 |
WTSEC_LIBRARY_Localization::lmsg("statuses.ok"), |
| 213 |
WTSEC_LIBRARY_Localization::lmsg("statuses.invalid"), |
| 214 |
WTSEC_LIBRARY_Localization::lmsg("statuses.expired"), |
| 215 |
WTSEC_LIBRARY_Localization::lmsg("statuses.expires"), |
| 216 |
"unknown_status" => WTSEC_LIBRARY_Localization::lmsg("statuses.missing"), |
| 217 |
], |
| 218 |
"decServiceChecks" => [ |
| 219 |
WTSEC_LIBRARY_Localization::lmsg("statuses.ok"), |
| 220 |
WTSEC_LIBRARY_Localization::lmsg("statuses.expires"), |
| 221 |
"unknown_status" => WTSEC_LIBRARY_Localization::lmsg("statuses.error"), |
| 222 |
] |
| 223 |
]; |
| 224 |
return isset($information[$service][$status]) ? $information[$service][$status] : $information[$service]["unknown_status"]; |
| 225 |
} |
| 226 |
function wtsec_getStatusStartPauseIcon($status, $config_id, $host_id) |
| 227 |
{ |
| 228 |
$icon = ''; |
| 229 |
switch ($status) { |
| 230 |
case "1": |
| 231 |
$icon = '<div class="v-pause ww-icon ww-icon--pause" style="border:none" data-config_id="' . $config_id . '" data-host_id="' . $host_id . '"></div>'; |
| 232 |
break; |
| 233 |
case "0": |
| 234 |
$icon = '<div class="v-pause ww-icon ww-icon--play" style="border:none" data-config_id="' . $config_id . '" data-host_id="' . $host_id . '"></div>'; |
| 235 |
break; |
| 236 |
} |
| 237 |
return $icon; |
| 238 |
} |
| 239 |
|
| 240 |
function wtsec_options_page() |
| 241 |
{ |
| 242 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 243 |
$result = WTSEC_LIBRARY_WT::getOptions($host['id']); |
| 244 |
$options = []; |
| 245 |
if (isset($result['data']['userHost']['services'])) { |
| 246 |
foreach ($result['data']['userHost']['services'] as $service) { |
| 247 |
$configs = $service['configs'][0]; |
| 248 |
$options[$service['name']] = ['config_id' => $configs['id'], 'is_active' => $configs['isActive']]; |
| 249 |
} |
| 250 |
} |
| 251 |
$options['host_id'] = $host['id']; |
| 252 |
wtsec_layout("options", $options); |
| 253 |
} |
| 254 |
|
| 255 |
function wtsec_services_page() |
| 256 |
{ |
| 257 |
wtsec_layout("services"); |
| 258 |
} |
| 259 |
|
| 260 |
function wtsec_antivirus_page() |
| 261 |
{ |
| 262 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 263 |
$site = WTSEC_LIBRARY_WT::getAntivirus($host['id']); |
| 264 |
if (isset($site['data']['vcServiceChecks'][0])) { |
| 265 |
$site = $site['data']['vcServiceChecks'][0]; |
| 266 |
$service = "VC"; |
| 267 |
$_service = strtolower($service); |
| 268 |
$signatures = []; |
| 269 |
$changes = []; |
| 270 |
$errors = []; |
| 271 |
$sign_count = 0; |
| 272 |
$changes_count = 0; |
| 273 |
$domain = $host['hostname']; |
| 274 |
if (!empty($site['list'])) { |
| 275 |
foreach ($site['list'] as $list) { |
| 276 |
if ($list['event'] > -1 && empty($list['matches'])) { |
| 277 |
$changes[$host['id']][] = $list; |
| 278 |
$changes_count++; |
| 279 |
} elseif ($list['event'] > -1 && !empty($list['matches'])) { |
| 280 |
$signatures[$host['id']][] = $list; |
| 281 |
$sign_count++; |
| 282 |
} else { |
| 283 |
$errors[$host['id']][] = $list; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
$uid = $host['id']; |
| 288 |
$status = wtsec_checkStatus($uid, $service); |
| 289 |
$services["vc"] = [ |
| 290 |
"pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $uid), |
| 291 |
"site_address" => $domain, |
| 292 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status'], "vc"), |
| 293 |
"signatures" => $sign_count, |
| 294 |
"changes" => $changes_count, |
| 295 |
"list" => $site["list"], |
| 296 |
"actions" => $buttons = wtsec_generateButtons($uid, $_service, $service, $status, WTSEC_SITE_URL), |
| 297 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.antivirus') |
| 298 |
]; |
| 299 |
} |
| 300 |
wtsec_layout("antivirus", $services); |
| 301 |
} |
| 302 |
|
| 303 |
function wtsec_login_form() |
| 304 |
{ |
| 305 |
if (wtsec_request()->method === "POST") { |
| 306 |
$result = WTSEC_LIBRARY_WT::auth(wtsec_request()->key); |
| 307 |
if (isset($result['data']['apiServiceMutation']['auth']['value'])) { |
| 308 |
WTSEC_LIBRARY_Session::setNotification("success", WTSEC_LIBRARY_Localization::lmsg('successfully_activated')); |
| 309 |
$token = $result['data']['apiServiceMutation']['auth']['value']; |
| 310 |
WTSEC_LIBRARY_App::login($token); |
| 311 |
wtsec_app()->set("api_key", wtsec_request()->key); |
| 312 |
wp_safe_redirect(wtsec_getUrl('dashboard')); |
| 313 |
exit; |
| 314 |
} else { |
| 315 |
WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg("form.incorrect")); |
| 316 |
} |
| 317 |
} |
| 318 |
wp_safe_redirect(wtsec_getUrl('login')); |
| 319 |
} |
| 320 |
|
| 321 |
function wtsec_cmd_ajax() |
| 322 |
{ |
| 323 |
$service = strtoupper(wtsec_request()->service); |
| 324 |
$_service = strtolower(wtsec_request()->service); |
| 325 |
$uid = wtsec_request()->uid; |
| 326 |
$cmd = wtsec_request()->cmd; |
| 327 |
$status = wtsec_checkStatus($uid, $service); |
| 328 |
global $wp_filesystem; |
| 329 |
if (mb_stripos($cmd, "install") === false) { |
| 330 |
wtsec_cmd($wp_filesystem, $cmd, $service, $_service, $uid, $status, WTSEC_SITE_URL); |
| 331 |
wp_safe_redirect(wp_get_referer()); |
| 332 |
} else { |
| 333 |
$redirect = wtsec_getUrl("dashboard"); |
| 334 |
$form_url = wp_nonce_url(admin_url('admin-post.php?cmd=' . $cmd . '&action=ajax_cmd&service=' . $service . '&uid=' . $uid), "ajax_cmd"); |
| 335 |
if (wtsec_filesystem_init($form_url, '', false, false)) { |
| 336 |
wtsec_cmd($wp_filesystem, $cmd, $service, $_service, $uid, $status, WTSEC_SITE_URL); |
| 337 |
wp_safe_redirect($redirect); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
function wtsec_login() |
| 343 |
{ |
| 344 |
wtsec_layout("login"); |
| 345 |
} |
| 346 |
|
| 347 |
function wtsec_logout() |
| 348 |
{ |
| 349 |
WTSEC_LIBRARY_App::logout(); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
function wtsec_layout($template, $arguments = [], $faq = "faq") |
| 354 |
{ |
| 355 |
$body = WTSEC_PLUGIN_PATH . "includes/" . $template . ".php"; |
| 356 |
$faq = WTSEC_PLUGIN_PATH . "includes/" . $faq . ".php"; |
| 357 |
require_once WTSEC_PLUGIN_PATH . "includes/layout.php"; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
function wtsec_ajax_changeStatus() |
| 362 |
{ |
| 363 |
if (wtsec_request()->method === "POST") { |
| 364 |
$host_id = wtsec_request()->host_id; |
| 365 |
$config_id = wtsec_request()->config_id; |
| 366 |
$result = WTSEC_LIBRARY_WT::changeStatus($config_id, $host_id); |
| 367 |
echo json_encode($result); |
| 368 |
} |
| 369 |
wp_die(); |
| 370 |
} |
| 371 |
|
| 372 |
function wtsec_changeStatus() |
| 373 |
{ |
| 374 |
if (wtsec_request()->method === "POST") { |
| 375 |
$host_id = wtsec_request()->host_id; |
| 376 |
$config_id = wtsec_request()->config_id; |
| 377 |
WTSEC_LIBRARY_WT::changeStatus($config_id, $host_id); |
| 378 |
} |
| 379 |
wp_safe_redirect(wp_get_referer()); |
| 380 |
} |
| 381 |
|
| 382 |
function wtsec_page($page) |
| 383 |
{ |
| 384 |
return wtsec_pages()[$page]; |
| 385 |
} |
| 386 |
|
| 387 |
function wtsec_getUrl($page) |
| 388 |
{ |
| 389 |
return admin_url('admin.php?page=' . WTSEC_PAGE_PREFIX . $page); |
| 390 |
} |
| 391 |
|
| 392 |
function wtsec_getRoute($page) |
| 393 |
{ |
| 394 |
return WTSEC_PAGE_PREFIX . $page; |
| 395 |
} |
| 396 |
|
| 397 |
function wtsec_pages() |
| 398 |
{ |
| 399 |
return [ |
| 400 |
"options" => [ |
| 401 |
"page_title" => WTSEC_LIBRARY_Localization::lmsg('options'), |
| 402 |
"menu_title" => WTSEC_LIBRARY_Localization::lmsg('options'), |
| 403 |
"function" => "wtsec_options_page", |
| 404 |
], |
| 405 |
"services" => [ |
| 406 |
"page_title" => WTSEC_LIBRARY_Localization::lmsg('services'), |
| 407 |
"menu_title" => WTSEC_LIBRARY_Localization::lmsg('services'), |
| 408 |
"function" => "wtsec_services_page", |
| 409 |
], |
| 410 |
"vc" => [ |
| 411 |
"page_title" => WTSEC_LIBRARY_Localization::lmsg('remote_antivirus'), |
| 412 |
"menu_title" => WTSEC_LIBRARY_Localization::lmsg('remote_antivirus'), |
| 413 |
"function" => "wtsec_antivirus_page", |
| 414 |
"vc_action" => "vc-action", |
| 415 |
"vc_function" => "wtsec_vc_function" |
| 416 |
], |
| 417 |
]; |
| 418 |
} |