| 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('admin_post_login_form', 'wtsec_login_form'); |
| 7 |
add_action('wp_ajax_cmd', 'wtsec_cmd_ajax'); |
| 8 |
add_action('admin_post_ajax_cmd', 'wtsec_cmd_ajax'); |
| 9 |
add_action('wp_ajax_ajax_firewall', 'wtsec_ajax_firewall'); |
| 10 |
add_action('wp_ajax_ajax_antivirus', 'wtsec_ajax_antivirus'); |
| 11 |
add_action('wp_ajax_chart_filter', 'wtsec_chart_filter'); |
| 12 |
|
| 13 |
|
| 14 |
function wtsec_add_menu_link() |
| 15 |
{ |
| 16 |
add_menu_page( |
| 17 |
WTSEC_PAGE_TITLE, |
| 18 |
WTSEC_MENU_TITLE, |
| 19 |
'manage_options', |
| 20 |
wtsec_getRoute('dashboard'), |
| 21 |
'wtsec_index_page', |
| 22 |
'', |
| 23 |
'1' |
| 24 |
); |
| 25 |
$parent = wtsec_getRoute('dashboard'); |
| 26 |
if (WTSEC_LIBRARY_App::authorized()) { |
| 27 |
$capability = 'manage_options'; |
| 28 |
foreach (wtsec_pages() as $page => $arguments) { |
| 29 |
add_submenu_page($parent, $arguments['page_title'], $arguments['menu_title'], $capability, wtsec_getRoute($page), $arguments['function']); |
| 30 |
} |
| 31 |
add_submenu_page(null, WTSEC_LIBRARY_Localization::lmsg('sign_in'), WTSEC_LIBRARY_Localization::lmsg('sign_in'), 'manage_options', wtsec_getRoute('login'), 'wtsec_login'); |
| 32 |
add_submenu_page($parent, WTSEC_LIBRARY_Localization::lmsg('logout'), WTSEC_LIBRARY_Localization::lmsg('logout'), 'manage_options', wtsec_getRoute('logout'), 'wtsec_logout'); |
| 33 |
} else { |
| 34 |
$capability = 'manage_options'; |
| 35 |
foreach (wtsec_pages() as $page => $arguments) { |
| 36 |
add_submenu_page(null, $arguments['page_title'], $arguments['menu_title'], $capability, wtsec_getRoute($page), function () { |
| 37 |
wp_safe_redirect(wtsec_getUrl('login')); |
| 38 |
}); |
| 39 |
} |
| 40 |
add_submenu_page($parent, WTSEC_LIBRARY_Localization::lmsg('sign_in'), WTSEC_LIBRARY_Localization::lmsg('sign_in'), 'manage_options', wtsec_getRoute('login'), 'wtsec_login'); |
| 41 |
add_submenu_page(null, WTSEC_LIBRARY_Localization::lmsg('logout'), WTSEC_LIBRARY_Localization::lmsg('logout'), 'manage_options', wtsec_getRoute('logout'), 'wtsec_logout'); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
function dd($vl) |
| 46 |
{ |
| 47 |
echo '<pre>'; |
| 48 |
print_r($vl); |
| 49 |
echo '</pre>'; |
| 50 |
die(); |
| 51 |
} |
| 52 |
|
| 53 |
function myGrading($score) |
| 54 |
{ |
| 55 |
if ($score < 0 || $score > 100) return ['grade' => '','color' => '']; |
| 56 |
$scores = [100 => 'A+', 90 => 'A', 80 => 'A-', 70 => 'B+', 60 => 'B', 50 => 'B-', 35 => 'C+', 20 => 'C', 0 => 'C-' ]; |
| 57 |
foreach ($scores as $key => $value){ |
| 58 |
if($score >= $key){ |
| 59 |
$grade = $value; |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$color = ($score >= 80) ? 'green' : ($score >= 50) ? 'orange' : 'red'; |
| 65 |
|
| 66 |
return ['grade' => $grade,'color' => $color]; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
function convertTimeToReadable($date) |
| 71 |
{ |
| 72 |
$lang = get_locale(); |
| 73 |
//enable russian language |
| 74 |
if ($lang === "ru_RU") { |
| 75 |
$lang .= ".UTF-8"; |
| 76 |
setlocale(LC_ALL, $lang); |
| 77 |
} |
| 78 |
if(strpos($date, ".") !== false){ |
| 79 |
$date = substr($date, 0, strpos($date, ".")); |
| 80 |
} |
| 81 |
return strftime("%B %e, %Y / %R", strtotime($date." UTC")); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
function convertToCurrentTime($date) |
| 86 |
{ |
| 87 |
$lang = get_locale(); |
| 88 |
//enable russian language |
| 89 |
if ($lang === "ru_RU") { |
| 90 |
$lang .= ".UTF-8"; |
| 91 |
setlocale(LC_ALL, $lang); |
| 92 |
} |
| 93 |
if(strpos($date, ".") !== false){ |
| 94 |
$date = substr($date, 0, strpos($date, ".")); |
| 95 |
} |
| 96 |
$date .= " UTC"; |
| 97 |
$current_time = strtotime($date); |
| 98 |
return ['date' => date("Y-m-d", $current_time),'time' => date("H:i:s",$current_time)]; |
| 99 |
} |
| 100 |
|
| 101 |
function wtsec_index_page() |
| 102 |
{ |
| 103 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 104 |
|
| 105 |
$services = []; |
| 106 |
if (!empty($host)) { |
| 107 |
$services = [ |
| 108 |
"settings" => [ |
| 109 |
"token" => WTSEC_LIBRARY_App::getToken(), |
| 110 |
"site_id" => $host['id'], |
| 111 |
], |
| 112 |
"score" => [ |
| 113 |
"total_score" => 0, |
| 114 |
"tested_on" => "", |
| 115 |
"server_ip" => "", |
| 116 |
"location" => "", |
| 117 |
"description" => wtsec_locale("score.description"), |
| 118 |
"information" => "", |
| 119 |
"grade" => "", |
| 120 |
], |
| 121 |
]; |
| 122 |
|
| 123 |
$score = json_decode(WTSEC_LIBRARY_WT::getScore(), true); |
| 124 |
if (!empty($score)) { |
| 125 |
$total_score = round($score['totalScore']); |
| 126 |
$my_grading = myGrading($total_score); |
| 127 |
$services["score"] = [ |
| 128 |
"total_score" => $total_score."%", |
| 129 |
"tested_on" => convertTimeToReadable($score['date']), |
| 130 |
"server_ip" => $score['ip'], |
| 131 |
"location" => $score['country'], |
| 132 |
"description" => wtsec_locale("score.description"), |
| 133 |
"information" => wtsec_locale("score.higher_than_percent", ['percent' => $score['ishigherthan']]), |
| 134 |
"grade" => $my_grading['grade'], |
| 135 |
"color" => $my_grading['color'], |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
$checks = WTSEC_LIBRARY_WT::getAllChecks($host['id']); |
| 140 |
foreach ($checks as $service => $site) { |
| 141 |
if (empty($site) || !is_array($site)) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
switch ($service) { |
| 145 |
case "availability": |
| 146 |
$services["wa"] = [ |
| 147 |
// "pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']), |
| 148 |
"pause" => "", |
| 149 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 150 |
"response_time" => ceil($site['responseTime'] / 1000000) . ' ' . WTSEC_LIBRARY_Localization::lmsg('ms'), |
| 151 |
"availability" => $site['percent'] . '%', |
| 152 |
"availability_percent" => $site['percent'], |
| 153 |
"last_test" => convertTimeToReadable($site['lastTest']['time']), |
| 154 |
"downtime" => ceil($site['downTime'] / 1000000) . ' ' . WTSEC_LIBRARY_Localization::lmsg('ms'), |
| 155 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.availability') |
| 156 |
]; |
| 157 |
break; |
| 158 |
case "ssl": |
| 159 |
if ((int)$site['expiryDate'] === 0) { |
| 160 |
$days_left = 0; |
| 161 |
} else { |
| 162 |
$earlier = new DateTime(); |
| 163 |
$later = new DateTime($site['expiryDate']); |
| 164 |
$days_left = $later->diff($earlier)->format("%a"); |
| 165 |
} |
| 166 |
$services["ssl"] = [ |
| 167 |
"pause" => "", |
| 168 |
"information" => wtsec_getInformation($service, $site['status']), |
| 169 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 170 |
"days_left" => $days_left, |
| 171 |
"issue_date" => convertTimeToReadable($site['issueDate']), |
| 172 |
"expiry_date" => convertTimeToReadable($site['expiryDate']), |
| 173 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.ssl'), |
| 174 |
"solve_it" => in_array($site['status'],['clean','pending']) ? '' : wtsec_locale('solve_it'), |
| 175 |
]; |
| 176 |
break; |
| 177 |
case "domain": |
| 178 |
if ((int)$site['expiredDate'] === 0) { |
| 179 |
$days_left = 0; |
| 180 |
} else { |
| 181 |
$earlier = new DateTime(); |
| 182 |
$later = new DateTime($site['expiredDate']); |
| 183 |
$days_left = $later->diff($earlier)->format("%a"); |
| 184 |
} |
| 185 |
|
| 186 |
$services["dec"] = [ |
| 187 |
"pause" => "", |
| 188 |
"registrar" => $site['registrar'], |
| 189 |
"owner" => $site['owner'], |
| 190 |
"information" => wtsec_getInformation($service, $site['status']), |
| 191 |
"email" => $site['email'], |
| 192 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 193 |
"days_left" => $days_left, |
| 194 |
"created" => convertTimeToReadable($site['createdDate']), |
| 195 |
"expiry_date" => convertTimeToReadable($site['expiredDate']), |
| 196 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.domain') |
| 197 |
]; |
| 198 |
break; |
| 199 |
case "reputation": |
| 200 |
$count = 0; |
| 201 |
if ($site['status'] != "clean") { |
| 202 |
foreach ($site['virusList'] as &$list) { |
| 203 |
if (!empty($list['viruses'])) { |
| 204 |
$count++; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
$services["av"] = [ |
| 209 |
"pause" => "", |
| 210 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 211 |
"time_of_the_last_test" => convertTimeToReadable($site['lastTest']['time']), |
| 212 |
"blacklists_entries" => $count, |
| 213 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.reputation'), |
| 214 |
"reputation" => $site['status'] === "infected" ? wtsec_locale('bad_reputation') : wtsec_locale('good_reputation'), |
| 215 |
]; |
| 216 |
break; |
| 217 |
case "maliciousScript": |
| 218 |
$services["cms"] = [ |
| 219 |
"pause" => "", |
| 220 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 221 |
"time_of_the_last_test" => $site['lastTest']['time'], |
| 222 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.malicious') |
| 223 |
]; |
| 224 |
break; |
| 225 |
case "deface": |
| 226 |
$services["dc"] = [ |
| 227 |
"pause" => "", |
| 228 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 229 |
"time_of_the_last_test" => convertTimeToReadable($site['lastTest']['time']), |
| 230 |
"number" => $site['count'], |
| 231 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.deface') |
| 232 |
]; |
| 233 |
break; |
| 234 |
case "ports": |
| 235 |
$services["ps"] = [ |
| 236 |
"pause" => "", |
| 237 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 238 |
"ip" => $site['ip'], |
| 239 |
"last_test" => convertTimeToReadable($site['lastTest']['time']), |
| 240 |
"number" => count($site['tcp']), |
| 241 |
"tcp" => !empty($site['tcp']) ? implode(",", $site['tcp']) : '', |
| 242 |
"udp" => "", |
| 243 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.port') |
| 244 |
]; |
| 245 |
break; |
| 246 |
case "firewall": |
| 247 |
$service = "WAF"; |
| 248 |
$_service = strtolower($service); |
| 249 |
$domain = $host['hostname']; |
| 250 |
$uid = $host['id']; |
| 251 |
|
| 252 |
$chart = generateChart($site['chart'], 7); |
| 253 |
// $status = wtsec_checkStatus($uid, $service); |
| 254 |
|
| 255 |
$data = WTSEC_LIBRARY_WT::getFirewall($host['id']); |
| 256 |
$data = $data['data']['auth']['viewer']['sites']['one']['firewall']; |
| 257 |
|
| 258 |
$countryAttacks = getCountryAttacks($data['map'], $chart['count_attacks']); |
| 259 |
$edges = $data['logs']['edges']; |
| 260 |
|
| 261 |
$services["waf"] = [ |
| 262 |
"pause" => "", |
| 263 |
"site_address" => $domain, |
| 264 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 265 |
"_status" => $site['status'], |
| 266 |
"installed_status" => in_array($site['status'], ["not_installed", "error", "down", "not_supported", "not_registered"]) ? false : true, |
| 267 |
"time_of_the_last_check" => convertTimeToReadable($site['lastTest']['time']), |
| 268 |
"attacks" => $chart['count_attacks'], |
| 269 |
"blocking" => $chart['count_blocks'], |
| 270 |
"non-blocking" => $chart['count_attacks'] - $chart['count_blocks'], |
| 271 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.firewall'), |
| 272 |
"actions" => wtsec_generateButtons($uid, $_service, $service, $site['status'], WTSEC_SITE_URL), |
| 273 |
"chart" => json_encode($chart['chart'], true), |
| 274 |
"edges" => $edges, |
| 275 |
"countryAttacks" => $countryAttacks, |
| 276 |
]; |
| 277 |
break; |
| 278 |
case "antivirus": |
| 279 |
$service = "AV"; |
| 280 |
$_service = strtolower($service); |
| 281 |
$domain = $host['hostname']; |
| 282 |
$uid = $host['id']; |
| 283 |
// $status = wtsec_checkStatus($uid, $service); |
| 284 |
$services["vc"] = [ |
| 285 |
"pause" => "", |
| 286 |
"site_address" => $domain, |
| 287 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 288 |
"installed_status" => in_array($site['status'], ["not_installed", "error", "down", "not_supported", "not_registered"]) ? false : true, |
| 289 |
"_status" => $site['status'], |
| 290 |
"signatures" => (int)$site['stats']['infected'], |
| 291 |
"changes" => (int)$site['stats']['changed'], |
| 292 |
"scanned" => (int)$site['stats']['scaned'], |
| 293 |
"deleted" => (int)$site['stats']['deleted'], |
| 294 |
"list" => $site['stats']["infected"], |
| 295 |
"actions" => wtsec_generateButtons($uid, $_service, $service, $site['status'], WTSEC_SITE_URL), |
| 296 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.antivirus') |
| 297 |
]; |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
//check AM, if not installed , run install |
| 303 |
$process = "installing"; |
| 304 |
$check_am_file = wtsec_checkInstalledFile("AM"); |
| 305 |
$am_installed = $check_am_file["status"]; |
| 306 |
$am_installed_status = wtsec_app()->get('am_installed'); |
| 307 |
|
| 308 |
if (!$am_installed || !$am_installed_status) { |
| 309 |
$am_is_installed = wtsec_cmd("am_install", "AM", "am", $host['id'], [], WTSEC_SITE_URL); |
| 310 |
if (!$am_is_installed) { |
| 311 |
$process = "failed"; |
| 312 |
wtsec_app()->set('am_installed', false); |
| 313 |
} else { |
| 314 |
wtsec_app()->set('am_installed', true); |
| 315 |
} |
| 316 |
} elseif ($services["vc"]["_status"] === "not_installed" || $services["waf"]["_status"] === "not_installed") { |
| 317 |
$process = "installing"; |
| 318 |
} else { |
| 319 |
$process = "installed"; |
| 320 |
} |
| 321 |
|
| 322 |
$services["am"] = [ |
| 323 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($am_installed ? "working" : "not_installed"), |
| 324 |
"actions" => wtsec_generateButtons($host['id'], "am", "AM", ["active" => $check_am_file], WTSEC_SITE_URL), |
| 325 |
"process_status" => $process, |
| 326 |
"installed" => $am_installed, |
| 327 |
]; |
| 328 |
} |
| 329 |
|
| 330 |
wtsec_layout("dashboard", $services); |
| 331 |
} |
| 332 |
|
| 333 |
function getCountryAttacks($map, $countAttacks){ |
| 334 |
|
| 335 |
if($map){ |
| 336 |
$attackKey = array_search(max(array_column($map, 'attacks')), array_column($map, 'attacks')); |
| 337 |
$map[$attackKey]['percent'] = round($map[$attackKey]['attacks'] / $countAttacks * 100); |
| 338 |
$map[$attackKey]['country'] = wtsec_locale("countries.".$map[$attackKey]['country']); |
| 339 |
return $map[$attackKey]; |
| 340 |
} |
| 341 |
|
| 342 |
return ['percent' => 0, 'country' => false]; |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
function generateChart($charts, $days = 7) |
| 347 |
{ |
| 348 |
|
| 349 |
if(is_array($days)) { |
| 350 |
$beginDate = new DateTime( $days['begin'] ); |
| 351 |
$endDate = new DateTime( $days['end'] ); |
| 352 |
$dateDiff = strtotime($days['end'] - strtotime($days['begin'])); |
| 353 |
$days = floor($dateDiff / (60 * 60 * 24)); |
| 354 |
} |
| 355 |
|
| 356 |
if($days <= 1){ |
| 357 |
$begin = new DateTime( date('Y-m-d 00:00:00') ); |
| 358 |
$end = new DateTime( date('Y-m-d 23:59:59') ); |
| 359 |
$step = 'hour'; |
| 360 |
} elseif($days <= 31){ |
| 361 |
$begin = new DateTime( date('Y-m-d', strtotime('-'.($days - 1).' day')) ); |
| 362 |
$end = new DateTime( date('Y-m-d') ); |
| 363 |
$step = 'day'; |
| 364 |
} else { |
| 365 |
$begin = new DateTime( date('Y-m-01', strtotime('- 11 month')) ); |
| 366 |
$end = new DateTime( date('Y-m-01', time()) ); |
| 367 |
$step = 'month'; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
$count_attacks = 0; |
| 372 |
$count_blocks = 0; |
| 373 |
$c = []; |
| 374 |
|
| 375 |
for($i = $begin; $i <= $end; $i->modify('+1 '.$step)){ |
| 376 |
|
| 377 |
$c[$i->format('U')] = [ |
| 378 |
'date' => ($days <= 1) ? $i->format("Y-m-d H:00:00") : $i->format("Y-m-d"), |
| 379 |
'count' => 0, |
| 380 |
'attacks' => 0, |
| 381 |
'blocked' => 0, |
| 382 |
]; |
| 383 |
} |
| 384 |
|
| 385 |
foreach ($charts as $chart) { |
| 386 |
$d = strtotime($chart['time']); |
| 387 |
$c[$d]['count'] = $chart['blocked']; |
| 388 |
$c[$d]['attacks'] = $chart['attacks']; |
| 389 |
$c[$d]['blocked'] = $chart['blocked']; |
| 390 |
$count_attacks += $chart['attacks']; |
| 391 |
$count_blocks += $chart['blocked']; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
//unset keys, because the js parser sorting by key in integer |
| 396 |
$result = []; |
| 397 |
foreach ($c as $d) { |
| 398 |
$result[] = $d; |
| 399 |
} |
| 400 |
|
| 401 |
return ['chart' => $result, 'count_attacks' => $count_attacks, 'count_blocks' => $count_blocks]; |
| 402 |
} |
| 403 |
|
| 404 |
function wtsec_getInformation($service, $status) |
| 405 |
{ |
| 406 |
$information = [ |
| 407 |
"ssl" => [ |
| 408 |
WTSEC_LIBRARY_Localization::lmsg("statuses.ok"), |
| 409 |
WTSEC_LIBRARY_Localization::lmsg("statuses.invalid"), |
| 410 |
WTSEC_LIBRARY_Localization::lmsg("statuses.expired"), |
| 411 |
WTSEC_LIBRARY_Localization::lmsg("statuses.expires"), |
| 412 |
"unknown_status" => WTSEC_LIBRARY_Localization::lmsg("statuses.missing"), |
| 413 |
], |
| 414 |
"domain" => [ |
| 415 |
WTSEC_LIBRARY_Localization::lmsg("statuses.ok"), |
| 416 |
WTSEC_LIBRARY_Localization::lmsg("statuses.expires"), |
| 417 |
"unknown_status" => WTSEC_LIBRARY_Localization::lmsg("statuses.error"), |
| 418 |
] |
| 419 |
]; |
| 420 |
return isset($information[$service][$status]) ? $information[$service][$status] : $information[$service]["unknown_status"]; |
| 421 |
} |
| 422 |
|
| 423 |
function wtsec_getStatusStartPauseIcon($status, $config_id, $host_id) |
| 424 |
{ |
| 425 |
$icon = ''; |
| 426 |
switch ($status) { |
| 427 |
case "1": |
| 428 |
$icon = '<div class="v-pause ww-icon ww-icon--pause" style="border:none" data-config_id="' . $config_id . '" data-host_id="' . $host_id . '"></div>'; |
| 429 |
break; |
| 430 |
case "0": |
| 431 |
$icon = '<div class="v-pause ww-icon ww-icon--play" style="border:none" data-config_id="' . $config_id . '" data-host_id="' . $host_id . '"></div>'; |
| 432 |
break; |
| 433 |
} |
| 434 |
return $icon; |
| 435 |
} |
| 436 |
|
| 437 |
function wtsec_options_page() |
| 438 |
{ |
| 439 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 440 |
$result = WTSEC_LIBRARY_WT::getOptions($host['id']); |
| 441 |
$options = []; |
| 442 |
if (isset($result['data']['userHost']['services'])) { |
| 443 |
foreach ($result['data']['userHost']['services'] as $service) { |
| 444 |
$configs = $service['configs'][0]; |
| 445 |
$options[$service['name']] = ['config_id' => $configs['id'], 'is_active' => $configs['isActive']]; |
| 446 |
} |
| 447 |
} |
| 448 |
$options['host_id'] = $host['id']; |
| 449 |
wtsec_layout("options", $options); |
| 450 |
} |
| 451 |
|
| 452 |
function wtsec_services_page() |
| 453 |
{ |
| 454 |
wtsec_layout("services"); |
| 455 |
} |
| 456 |
|
| 457 |
function wtsec_antivirus_page() |
| 458 |
{ |
| 459 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 460 |
$services = []; |
| 461 |
if (!empty($host)) { |
| 462 |
$site = WTSEC_LIBRARY_WT::getAntivirus($host['id']); |
| 463 |
if (isset($site['data']['auth']['viewer']['sites']['one']['antivirus']['log'])) { |
| 464 |
$site = $site['data']['auth']['viewer']['sites']['one']['antivirus']; |
| 465 |
|
| 466 |
$_SESSION['antivirus_endCursor'] = $site['log']['pageInfo']['endCursor']; |
| 467 |
$_SESSION['antivirus_hasNextPage'] = $site['log']['pageInfo']['hasNextPage']; |
| 468 |
|
| 469 |
$service = "AV"; |
| 470 |
$_service = strtolower($service); |
| 471 |
$domain = $host['hostname']; |
| 472 |
$uid = $host['id']; |
| 473 |
$status = wtsec_checkStatus($uid, $service); |
| 474 |
$services["vc"] = [ |
| 475 |
"pause" => "", |
| 476 |
"site_address" => $domain, |
| 477 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']), |
| 478 |
"signatures" => (int)$site['stats']['infected'], |
| 479 |
"changes" => (int)$site['stats']['changed'], |
| 480 |
"scanned" => (int)$site['stats']['scaned'], |
| 481 |
"deleted" => (int)$site['stats']['deleted'], |
| 482 |
"list" => $site["log"]['edges'], |
| 483 |
"actions" => $buttons = wtsec_generateButtons($uid, $_service, $service, $status, WTSEC_SITE_URL), |
| 484 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.antivirus') |
| 485 |
]; |
| 486 |
} |
| 487 |
} |
| 488 |
wtsec_layout("antivirus", $services); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
function wtsec_firewall_page() |
| 493 |
{ |
| 494 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 495 |
$services = []; |
| 496 |
|
| 497 |
if (!empty($host)) { |
| 498 |
$site = WTSEC_LIBRARY_WT::getFirewall($host['id'],10,null,7); |
| 499 |
$chart = WTSEC_LIBRARY_WT::getFirewallChart($host['id'], 7); |
| 500 |
if (isset($site['data']['auth']['viewer']['sites']['one']['firewall']['logs'])) { |
| 501 |
$logs = $site['data']['auth']['viewer']['sites']['one']['firewall']['logs']; |
| 502 |
$firewall = $site['data']['auth']['viewer']['sites']['one']['firewall']; |
| 503 |
$edges = $logs['edges']; |
| 504 |
|
| 505 |
$chart = $chart['data']['auth']['viewer']['sites']['one']['firewall']['chart']; |
| 506 |
|
| 507 |
$chart = generateChart($chart, 7); |
| 508 |
|
| 509 |
$domain = $host['hostname']; |
| 510 |
|
| 511 |
$_SESSION['firewall_endCursor'] = $logs['pageInfo']['endCursor']; |
| 512 |
$_SESSION['firewall_hasNextPage'] = $logs['pageInfo']['hasNextPage']; |
| 513 |
|
| 514 |
$countryAttacks = getCountryAttacks($firewall['map'], $chart['count_attacks']); |
| 515 |
|
| 516 |
$services["waf"] = [ |
| 517 |
"pause" => "", |
| 518 |
"site_address" => $domain, |
| 519 |
"status" => WTSEC_LIBRARY_WT::getStatusIcon($firewall['status']), |
| 520 |
"_status" => $firewall['status'], |
| 521 |
"installed_status" => in_array($firewall['status'], ["not_installed", "error", "down", "not_supported", "not_registered"]) ? false : true, |
| 522 |
"time_of_the_last_check" => convertTimeToReadable($firewall['lastTest']['time']), |
| 523 |
"attacks" => $chart['count_attacks'], |
| 524 |
"blocking" => $chart['count_blocks'], |
| 525 |
"non-blocking" => $chart['count_attacks'] - $chart['count_blocks'], |
| 526 |
"description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.firewall'), |
| 527 |
"chart" => json_encode($chart['chart'], true), |
| 528 |
"edges" => $edges, |
| 529 |
"countryAttacks" => $countryAttacks, |
| 530 |
]; |
| 531 |
} |
| 532 |
} |
| 533 |
wtsec_layout("firewall", $services); |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* ajax load firewall |
| 538 |
*/ |
| 539 |
function wtsec_ajax_firewall() |
| 540 |
{ |
| 541 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 542 |
$services = []; |
| 543 |
|
| 544 |
$post = ($_POST) ? wtsec_clean($_POST):[]; |
| 545 |
$days = ($post['period']) ? wtsec_period_as_time($post['period']) : 7; |
| 546 |
|
| 547 |
if (!empty($host)) { |
| 548 |
|
| 549 |
if(!$_SESSION['firewall_endCursor'] || $post['filter'] == 'period_filter') $_SESSION['firewall_endCursor'] = null; |
| 550 |
|
| 551 |
$site = WTSEC_LIBRARY_WT::getFirewall($host['id'],10, $_SESSION['firewall_endCursor'], $days); |
| 552 |
if (isset($site['data']['auth']['viewer']['sites']['one']['firewall']['logs'])) { |
| 553 |
$logs = $site['data']['auth']['viewer']['sites']['one']['firewall']['logs']; |
| 554 |
|
| 555 |
$services["waf"]['edges'] = $logs['edges']; |
| 556 |
|
| 557 |
$_SESSION['firewall_endCursor'] = $logs['pageInfo']['endCursor']; |
| 558 |
$_SESSION['firewall_hasNextPage'] = $logs['pageInfo']['hasNextPage']; |
| 559 |
} |
| 560 |
|
| 561 |
} |
| 562 |
|
| 563 |
wtsec_layout_part("_firewall_lazy", $services); |
| 564 |
wp_die(); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* ajax load antivirus |
| 569 |
*/ |
| 570 |
function wtsec_ajax_antivirus() |
| 571 |
{ |
| 572 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 573 |
$services = []; |
| 574 |
|
| 575 |
$post = ($_POST) ? wtsec_clean($_POST):[]; |
| 576 |
$days = ($post['period']) ? wtsec_period_as_time($post['period']) : 7; |
| 577 |
|
| 578 |
if (!empty($host)) { |
| 579 |
|
| 580 |
if(!$_SESSION['antivirus_endCursor'] || $post['filter'] == 'period_filter') $_SESSION['antivirus_endCursor'] = null; |
| 581 |
if($post['event'] || $_SESSION['antivirus_event']){ |
| 582 |
if($post['event']){ |
| 583 |
$_SESSION['antivirus_event'] = $post['event']; |
| 584 |
} |
| 585 |
$site = WTSEC_LIBRARY_WT::getAntivirus($host['id'],10, $_SESSION['antivirus_endCursor'],$days, $_SESSION['antivirus_event']); |
| 586 |
} else { |
| 587 |
$site = WTSEC_LIBRARY_WT::getAntivirus($host['id'],10, $_SESSION['antivirus_endCursor'],$days); |
| 588 |
} |
| 589 |
|
| 590 |
if (isset($site['data']['auth']['viewer']['sites']['one']['antivirus']['log'])) { |
| 591 |
$logs = $site['data']['auth']['viewer']['sites']['one']['antivirus']['log']; |
| 592 |
|
| 593 |
$services["vc"]['list'] = $logs['edges']; |
| 594 |
|
| 595 |
$_SESSION['antivirus_endCursor'] = $logs['pageInfo']['endCursor']; |
| 596 |
$_SESSION['antivirus_hasNextPage'] = $logs['pageInfo']['hasNextPage']; |
| 597 |
|
| 598 |
} |
| 599 |
} |
| 600 |
wtsec_layout_part("_antivirus_lazy", $services); |
| 601 |
|
| 602 |
wp_die(); |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* ajax load chart |
| 607 |
*/ |
| 608 |
function wtsec_chart_filter() |
| 609 |
{ |
| 610 |
$days = (int)$_POST['days']; |
| 611 |
$days = $days ?: 7; |
| 612 |
|
| 613 |
$host = WTSEC_LIBRARY_WT::getOwnSite(); |
| 614 |
$chart = WTSEC_LIBRARY_WT::getFirewallChart($host['id'], $days); |
| 615 |
$chart = $chart['data']['auth']['viewer']['sites']['one']['firewall']['chart']; |
| 616 |
$chart = generateChart($chart, $days); |
| 617 |
$res['chart'] = json_encode($chart['chart'], true); |
| 618 |
$res['days'] = $days; |
| 619 |
|
| 620 |
wtsec_layout_part("_chart_ajax", $res); |
| 621 |
|
| 622 |
wp_die(); |
| 623 |
} |
| 624 |
|
| 625 |
function wtsec_login_form() |
| 626 |
{ |
| 627 |
if (wtsec_request()->method === "POST") { |
| 628 |
$result = WTSEC_LIBRARY_WT::auth(wtsec_request()->key); |
| 629 |
if (isset($result['data']['guest']['apiKeys']['auth']['token']['value'])) { |
| 630 |
WTSEC_LIBRARY_Session::setNotification("success", WTSEC_LIBRARY_Localization::lmsg('successfully_activated')); |
| 631 |
$token = $result['data']['guest']['apiKeys']['auth']['token']['value']; |
| 632 |
WTSEC_LIBRARY_App::login($token); |
| 633 |
wtsec_app()->set("api_key", wtsec_request()->key); |
| 634 |
wp_safe_redirect(wtsec_getUrl('dashboard')); |
| 635 |
exit; |
| 636 |
} else { |
| 637 |
WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg("form.incorrect")); |
| 638 |
} |
| 639 |
} |
| 640 |
wp_safe_redirect(wtsec_getUrl('login')); |
| 641 |
} |
| 642 |
|
| 643 |
function wtsec_cmd_ajax() |
| 644 |
{ |
| 645 |
$service = strtoupper(wtsec_request()->service); |
| 646 |
$_service = strtolower(wtsec_request()->service); |
| 647 |
$uid = wtsec_request()->uid; |
| 648 |
$cmd = wtsec_request()->cmd; |
| 649 |
$status = wtsec_checkStatus($uid, $service); |
| 650 |
if (stripos($cmd, "install") === false) { |
| 651 |
wtsec_cmd($cmd, $service, $_service, $uid, $status, WTSEC_SITE_URL); |
| 652 |
wp_safe_redirect(wp_get_referer()); |
| 653 |
} else { |
| 654 |
$redirect = wtsec_getUrl("dashboard"); |
| 655 |
$form_url = wp_nonce_url(admin_url('admin-post.php?cmd=' . $cmd . '&action=ajax_cmd&service=' . $service . '&uid=' . $uid), "ajax_cmd"); |
| 656 |
if (wtsec_filesystem_init($form_url, '', false, false)) { |
| 657 |
wtsec_cmd($cmd, $service, $_service, $uid, $status, WTSEC_SITE_URL); |
| 658 |
wp_safe_redirect($redirect); |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
function wtsec_login() |
| 664 |
{ |
| 665 |
require_once WTSEC_PLUGIN_PATH . "includes/login.php"; |
| 666 |
} |
| 667 |
|
| 668 |
function wtsec_logout() |
| 669 |
{ |
| 670 |
WTSEC_LIBRARY_App::logout(); |
| 671 |
} |
| 672 |
|
| 673 |
function wtsec_layout_part($template, $arguments = []) |
| 674 |
{ |
| 675 |
require WTSEC_PLUGIN_PATH . "includes/components/" . $template . ".php"; |
| 676 |
|
| 677 |
} |
| 678 |
|
| 679 |
function wtsec_layout($template, $arguments = [], $faq = "faq") |
| 680 |
{ |
| 681 |
$body = WTSEC_PLUGIN_PATH . "includes/" . $template . ".php"; |
| 682 |
$faq = WTSEC_PLUGIN_PATH . "includes/" . $faq . ".php"; |
| 683 |
require_once WTSEC_PLUGIN_PATH . "includes/layout.php"; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
function wtsec_ajax_changeStatus() |
| 688 |
{ |
| 689 |
if (wtsec_request()->method === "POST") { |
| 690 |
$host_id = wtsec_request()->host_id; |
| 691 |
$config_id = wtsec_request()->config_id; |
| 692 |
$result = WTSEC_LIBRARY_WT::changeStatus($config_id, $host_id); |
| 693 |
echo json_encode($result); |
| 694 |
} |
| 695 |
wp_die(); |
| 696 |
} |
| 697 |
|
| 698 |
function wtsec_changeStatus() |
| 699 |
{ |
| 700 |
if (wtsec_request()->method === "POST") { |
| 701 |
$host_id = wtsec_request()->host_id; |
| 702 |
$config_id = wtsec_request()->config_id; |
| 703 |
WTSEC_LIBRARY_WT::changeStatus($config_id, $host_id); |
| 704 |
} |
| 705 |
wp_safe_redirect(wp_get_referer()); |
| 706 |
} |
| 707 |
|
| 708 |
function wtsec_page($page) |
| 709 |
{ |
| 710 |
return wtsec_pages()[$page]; |
| 711 |
} |
| 712 |
|
| 713 |
function wtsec_clean($data) |
| 714 |
{ |
| 715 |
if (is_array($data)) { |
| 716 |
foreach ($data as $key => $value) { |
| 717 |
unset($data[$key]); |
| 718 |
$data[wtsec_clean($key)] = wtsec_clean($value); |
| 719 |
} |
| 720 |
} else { |
| 721 |
$data = htmlspecialchars(strip_tags($data), ENT_COMPAT, 'UTF-8'); |
| 722 |
} |
| 723 |
return $data; |
| 724 |
} |
| 725 |
|
| 726 |
function wtsec_period_as_time($period){ |
| 727 |
$end = $period[1] ?: $period[0]; |
| 728 |
$array['begin'] = strtotime(date('d-m-Y 00:00:00',strtotime($period[0]))); |
| 729 |
$array['end'] = strtotime(date('d-m-Y 23:59:59',strtotime($end))); |
| 730 |
|
| 731 |
return $array; |
| 732 |
} |
| 733 |
|
| 734 |
function wtsec_getUrl($page) |
| 735 |
{ |
| 736 |
return admin_url('admin.php?page=' . WTSEC_PAGE_PREFIX . $page); |
| 737 |
} |
| 738 |
|
| 739 |
function wtsec_getRoute($page) |
| 740 |
{ |
| 741 |
return WTSEC_PAGE_PREFIX . $page; |
| 742 |
} |
| 743 |
|
| 744 |
function wtsec_pages() |
| 745 |
{ |
| 746 |
return [ |
| 747 |
// "options" => [ |
| 748 |
// "page_title" => WTSEC_LIBRARY_Localization::lmsg('options'), |
| 749 |
// "menu_title" => WTSEC_LIBRARY_Localization::lmsg('options'), |
| 750 |
// "function" => "wtsec_options_page", |
| 751 |
// ], |
| 752 |
"services" => [ |
| 753 |
"page_title" => WTSEC_LIBRARY_Localization::lmsg('services'), |
| 754 |
"menu_title" => WTSEC_LIBRARY_Localization::lmsg('services'), |
| 755 |
"function" => "wtsec_services_page", |
| 756 |
], |
| 757 |
"vc" => [ |
| 758 |
"page_title" => WTSEC_LIBRARY_Localization::lmsg('remote_antivirus'), |
| 759 |
"menu_title" => WTSEC_LIBRARY_Localization::lmsg('remote_antivirus'), |
| 760 |
"function" => "wtsec_antivirus_page", |
| 761 |
"vc_action" => "vc-action", |
| 762 |
"vc_function" => "wtsec_vc_function" |
| 763 |
], |
| 764 |
"waf" => [ |
| 765 |
"page_title" => WTSEC_LIBRARY_Localization::lmsg('firewall'), |
| 766 |
"menu_title" => WTSEC_LIBRARY_Localization::lmsg('firewall'), |
| 767 |
"function" => "wtsec_firewall_page", |
| 768 |
"vc_action" => "firewall-action", |
| 769 |
"vc_function" => "wtsec_firewall_function" |
| 770 |
], |
| 771 |
]; |
| 772 |
} |
| 773 |
|
| 774 |
|