| 1 |
<?php defined('ABSPATH') or die("Protected By WT!"); |
| 2 |
|
| 3 |
|
| 4 |
class WTSEC_LIBRARY_WT |
| 5 |
{ |
| 6 |
|
| 7 |
const URL = "https://api.wtotem.com/graphql"; |
| 8 |
|
| 9 |
public static function auth($key) |
| 10 |
{ |
| 11 |
$source = WTSEC_SITE_URL; |
| 12 |
$payload = '{"query":"mutation{ guest{ apiKeys{ auth(apiKey:\"' . $key . '\", source:\"' . $source . '\"),{ token{ value,refreshToken,expiresIn } } } } }"}'; |
| 13 |
return self::requestApi($payload, false, true); |
| 14 |
} |
| 15 |
|
| 16 |
protected static function requestApi($payload, $token = false, $repeat = false) |
| 17 |
{ |
| 18 |
if ($token) { |
| 19 |
$token = WTSEC_LIBRARY_App::getToken(); |
| 20 |
$token_expired = wtsec_app()->get("token_expired"); |
| 21 |
|
| 22 |
if($token_expired <= time() && !$repeat){ |
| 23 |
$result = WTSEC_LIBRARY_WT::auth(wtsec_app()->get("api_key")); |
| 24 |
if (isset($result['data']['guest']['apiKeys']['auth']['token']['value'])) { |
| 25 |
$expiresIn = $result['data']['guest']['apiKeys']['auth']['token']['expiresIn']; |
| 26 |
$token_expired = time() + $expiresIn - 60; |
| 27 |
wtsec_app()->set("token_expired", $token_expired); |
| 28 |
|
| 29 |
$token_ = $result['data']['guest']['apiKeys']['auth']['token']['value']; |
| 30 |
WTSEC_LIBRARY_App::login($token_); |
| 31 |
return self::requestApi($payload, $token, true); |
| 32 |
} elseif($result['errors'][0]['message'] == 'INVALID_API_KEY') { |
| 33 |
WTSEC_LIBRARY_App::logout(); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
$args = [ |
| 39 |
'body' => $payload, |
| 40 |
'timeout' => '60', |
| 41 |
'sslverify' => false, |
| 42 |
'headers' => ['Content-Type:application/json', 'Content-Type' => 'application/json', 'source:WORDPRESS', 'Accept: application/json'], |
| 43 |
]; |
| 44 |
if (!is_null($token) && $token) { |
| 45 |
$authorization = "Bearer " . $token; |
| 46 |
$args['headers'] = array_merge($args['headers'], ["Authorization" => $authorization]); |
| 47 |
} |
| 48 |
$response = wp_remote_post(self::URL, $args); |
| 49 |
$httpcode = wp_remote_retrieve_response_code($response); |
| 50 |
|
| 51 |
if ($httpcode < 200) { |
| 52 |
//WTSEC_LIBRARY_Session::setNotification("error", __( 'Could not connect to the server.', 'wtotem' )); |
| 53 |
} |
| 54 |
$response = wp_remote_retrieve_body($response); |
| 55 |
$result = json_decode($response, true); |
| 56 |
if (isset($result['errors'][0]['message'])) { |
| 57 |
$message = self::diffMesageForHuman($result['errors'][0]['message']); |
| 58 |
if (stripos($result['errors'][0]['message'], "token") !== false && !$repeat) { |
| 59 |
$result = WTSEC_LIBRARY_WT::auth(wtsec_app()->get("api_key")); |
| 60 |
if (isset($result['data']['guest']['apiKeys']['auth']['token']['value'])) { |
| 61 |
$token_ = $result['data']['guest']['apiKeys']['auth']['token']['value']; |
| 62 |
WTSEC_LIBRARY_App::login($token_); |
| 63 |
return self::requestApi($payload, $token, true); |
| 64 |
} elseif($result['errors'][0]['message'] == 'INVALID_API_KEY') { |
| 65 |
WTSEC_LIBRARY_App::logout(); |
| 66 |
} |
| 67 |
} else { |
| 68 |
if ($message !== false) { |
| 69 |
WTSEC_LIBRARY_Session::setNotification("warning", $message); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return $result; |
| 74 |
} |
| 75 |
|
| 76 |
public static function diffMesageForHuman($message) |
| 77 |
{ |
| 78 |
$definition = $message; |
| 79 |
$excepts = [ |
| 80 |
"RESOURCE_NOT_FOUND", "DUPLICATE_HOST", "INVALID_CREDENTIALS", "INVALID_API_KEY", "Invalid token", |
| 81 |
]; |
| 82 |
if (in_array($message, $excepts)) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
switch ($message) { |
| 86 |
case 'HOSTS_LIMIT_EXCEEDED': |
| 87 |
$definition = __( 'Limit of adding sites exceeded.', 'wtotem' ); |
| 88 |
break; |
| 89 |
case 'Invalid token': |
| 90 |
$definition = __( 'Token expired.', 'wtotem' ); |
| 91 |
break; |
| 92 |
case 'USER_ALREADY_REGISTERED': |
| 93 |
$definition = __( 'A user with this email already exists.', 'wtotem' ); |
| 94 |
break; |
| 95 |
case 'DUPLICATE_HOST': |
| 96 |
$definition = __( 'Duplicate host', 'wtotem' ); |
| 97 |
break; |
| 98 |
case 'INVALID_DOMAIN_NAME': |
| 99 |
$definition = __( 'Invalid Domain Name', 'wtotem' ); |
| 100 |
break; |
| 101 |
} |
| 102 |
return $definition; |
| 103 |
} |
| 104 |
|
| 105 |
//del |
| 106 |
public static function requestURL($url) |
| 107 |
{ |
| 108 |
$args = [ |
| 109 |
'timeout' => '30', |
| 110 |
'sslverify' => false, |
| 111 |
]; |
| 112 |
$response = wp_remote_get($url, $args); |
| 113 |
$httpcode = wp_remote_retrieve_response_code($response); |
| 114 |
if ($httpcode < 200) { |
| 115 |
WTSEC_LIBRARY_Session::setNotification("error", __( 'Could not connect to the server.', 'wtotem' )." requestURL"); |
| 116 |
} |
| 117 |
$response = wp_remote_retrieve_body($response); |
| 118 |
return $response; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
public static function getFileByUrl($url, $fileName) |
| 123 |
{ |
| 124 |
$url = $url.'/'.$fileName; |
| 125 |
$args = [ |
| 126 |
'timeout' => '30', |
| 127 |
'sslverify' => false, |
| 128 |
]; |
| 129 |
$response = wp_remote_get($url, $args); |
| 130 |
$httpcode = wp_remote_retrieve_response_code($response); |
| 131 |
if ($httpcode < 200) { |
| 132 |
WTSEC_LIBRARY_Session::setNotification("error", __( 'Could not connect to the server.', 'wtotem' )." getFileByUrl"); |
| 133 |
} |
| 134 |
|
| 135 |
$response = wp_remote_retrieve_body($response); |
| 136 |
return ["body" => $response, "filename" => $fileName]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
public static function getEmail(){ |
| 141 |
$payload = '{"query":"query { auth { viewer { email __typename } __typename } }"}'; |
| 142 |
$result = self::requestApi($payload, true); |
| 143 |
|
| 144 |
return $result['data']['auth']['viewer']['email']; |
| 145 |
} |
| 146 |
|
| 147 |
public static function getOwnSite($attempt = false) |
| 148 |
{ |
| 149 |
$sites = [ |
| 150 |
WTSEC_LIBRARY_Idn::idn_to_utf8(WTSEC_SITE_URL), |
| 151 |
WTSEC_LIBRARY_Idn::idn_to_utf8('www.'.WTSEC_SITE_URL) |
| 152 |
]; |
| 153 |
|
| 154 |
foreach ($sites as $site){ |
| 155 |
$payload = '{"query":"query getSites { auth { viewer { sites { ...sites } } } }fragment sites on SiteQueries { list( filter: {search: \"'.$site.'\"} ) { edges { node { id hostname title ssl { status } availability { status } reputation { status } ports { status } deface { status } domain { status } antivirus { status } firewall { status } maliciousScript { stack { name } } } } } }"}'; |
| 156 |
$result = self::requestApi($payload, true); |
| 157 |
|
| 158 |
if (isset($result['data']['auth']['viewer']['sites']['list']['edges'][0]['node'])){ |
| 159 |
return $result['data']['auth']['viewer']['sites']['list']['edges'][0]['node']; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$site_url = str_replace(['http://', 'https://', '//', '://'], '', get_site_url()); |
| 164 |
$add_site = self::addSite($site_url); |
| 165 |
if (isset($add_site['errors'])) { |
| 166 |
return $result['data']['node'] = []; |
| 167 |
} else { |
| 168 |
if (!$attempt) { |
| 169 |
return self::getOwnSite(true); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return []; |
| 174 |
} |
| 175 |
|
| 176 |
public static function addSite($url) |
| 177 |
{ |
| 178 |
$payload = '{"variables":{"input":{"title":"' . $url . '","hostname":"' . $url . '","configs":{"scheme":"http","port":80,"wa":{},"dec":{},"ps":{}}}},"query":"mutation ($input: CreateSiteInput) { auth { sites { create(input: $input) { id hostname title } } }}"}'; |
| 179 |
return self::requestApi($payload, true); |
| 180 |
} |
| 181 |
|
| 182 |
public static function getAllChecks($host_id) |
| 183 |
{ |
| 184 |
$from = time() - (60 * 60 * 24 * 7); |
| 185 |
$to = time(); |
| 186 |
$from_waf = time() - (60 * 60 * 24 * 7); |
| 187 |
$language = WTSEC_LANGUAGE; |
| 188 |
$payload = '{"query":"query($id: ID!, $dateRange: DateRangeInput!, $language: Language!, $dateRangeWeek: DateRangeInput!, $wafLogFilter: WafLogFilter!) { auth { viewer { sites { one(id: $id) { ports { status ip tcp lastTest { time } } availability { status lastTest { time } responseTime downTime(dateRange: $dateRange) percent(dateRange: $dateRange) } deface { status lastTest { time } words count } domain { status registrar owner email createdDate expiredDate } ports { status lastTest { time } ip tcp country } ssl { status daysLeft expiryDate issueDate } reputation { status lastTest { time } virusList { virus{ type path } antiVirus } } firewall { lastTest { time } logs(wafLogFilter: $wafLogFilter){ edges{ node{ type blocked payload ip location{ country{ nameEn } } time request status country category } } } map(dateRange: $dateRange) { attacks, country } status chart(dateRange: $dateRange) { time attacks blocked } report(dateRange: $dateRange) { time attacks ip } } serverStatus { info { phpVersion phpServerUser phpServerSoftware phpGatewayInterface phpServerProtocol osInfo cpuCount cpuModel CpuFreq cpuFamily lsCpu maxExecTime mathLibraries } ramChart(dateRange: $dateRangeWeek){ total value time } cpuChart(dateRange: $dateRangeWeek){ value time } discUsage{ total free } status } maliciousScript { lastTest { time } status } scoring( language: $language ){ score lastTest{ time } result{ ip country isHigherThan }} agentManager{ createdAt } antivirus { status stats { changed deleted scaned infected error } lastTest { time } isFirstCheck } } } } } }","variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from_waf . '}, "dateRangeWeek":{"to":' . $to . ',"from":' . $from . '}, "wafLogFilter": {"order":{"direction":"DESC","field":"time"},"pagination":{"first":5,"cursor":null}}, "language":"'.$language.'"}}'; |
| 189 |
$response = self::requestApi($payload, true); |
| 190 |
if (isset($response['data']['auth']['viewer']['sites']['one'])) { |
| 191 |
return $response['data']['auth']['viewer']['sites']['one']; |
| 192 |
} |
| 193 |
return []; |
| 194 |
} |
| 195 |
|
| 196 |
public static function getFirewall($host_id, $limit = 20, $cursor = null, $days = 365) |
| 197 |
{ |
| 198 |
$from = (is_array($days)) ? $days['from'] : time() - (60 * 60 * 24 * $days); |
| 199 |
$to = (is_array($days)) ? $days['to'] : time(); |
| 200 |
$cursor = ($cursor == null) ? 'null' : '"'.$cursor.'"'; |
| 201 |
|
| 202 |
$payload = '{"operationName":"FirewallAttackLog","variables":{"dateRange":{"to":'.$to.',"from":'.$from.'},"id":"'.$host_id.'","wafLogFilter":{"dateRange":{"to":'.$to.',"from":'.$from.'},"order":{"direction":"DESC","field":"time"},"pagination":{"first":'.$limit.',"cursor":' . $cursor . '}}},"query":"query FirewallAttackLog($id: ID!, $wafLogFilter: WafLogFilter!, $dateRange: DateRangeInput!) { auth { viewer { sites { one(id: $id) { firewall { lastTest { time } settings{ gdn dosProtection dosLimit loginAttemptsProtection loginAttemptsLimit } status... FirewallLogFragment map(dateRange: $dateRange) { attacks, country } __typename } __typename } __typename } __typename } __typename } } fragment FirewallLogFragment on Waf { logs(wafLogFilter: $wafLogFilter) { edges { cursor node { type blocked payload ip location { country { nameEn __typename } __typename } time request status country category __typename } __typename } pageInfo { endCursor hasNextPage __typename } __typename } __typename }"}'; |
| 203 |
$res = self::requestApi($payload, true); |
| 204 |
|
| 205 |
return $res; |
| 206 |
} |
| 207 |
|
| 208 |
public static function getAntivirus($params) |
| 209 |
{ |
| 210 |
$event = $params['event'] ?: 'new'; |
| 211 |
$event = '"event":"'.$event.'", '; |
| 212 |
$days = $params['days']; |
| 213 |
$permissionsChanged = $params['permissionsChanged'] ? ' "permissionsChanged":true, ' : ''; |
| 214 |
|
| 215 |
$from = (is_array($days)) ? $days['from'] : time() - (60 * 60 * 24 * $days); |
| 216 |
$to = (is_array($days)) ? $days['to'] : time(); |
| 217 |
$cursor = ($params['endCursor'] == null) ? 'null' : '"' . $params['endCursor'] . '"'; |
| 218 |
$payload = '{"operationName":null,"variables":{"id":"' . $params['host_id'] . '","avLogFilter":{'. $permissionsChanged . $event .' "dateRange":{"to":'.$to.',"from":'.$from.'},"order":{"direction":"DESC","field":"time"},"pagination":{"first":' . $params['limit'] . ',"cursor":' . $cursor . '}}},"query":"query ($id: ID!, $avLogFilter: AvLogFilter!) { auth { viewer { sites { one(id: $id) { id ... on Site { configs { ... on AvConfig { isActive id } } } antivirus { status log(avLogFilter: $avLogFilter) { edges { node { filePath matches event signatures time permissions permissionsChanged } } pageInfo { endCursor hasNextPage __typename } } lastTest { time } stats { changed deleted scaned infected } } } } } } }"}'; |
| 219 |
return self::requestApi($payload, true); |
| 220 |
} |
| 221 |
|
| 222 |
public static function getFirewallChart($host_id, $days = 7) |
| 223 |
{ |
| 224 |
$to = time(); |
| 225 |
$from_waf = ($days <= 1) ? strtotime(date('Y-m-d 00:00:01')) : time() - (60 * 60 * 24 * $days); |
| 226 |
|
| 227 |
$payload = '{ "query":"query($id: ID!, $dateRange: DateRangeInput!, $wafLogFilter: WafLogFilter!) { auth { viewer { sites { one(id: $id) { firewall { lastTest { time } status logs(wafLogFilter: $wafLogFilter) { edges { node { status country type userAgent } } } chart(dateRange: $dateRange) { time attacks blocked } report(dateRange: $dateRange) { time attacks ip } } } } } } }", "operationName":null,"variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from_waf . '}, "wafLogFilter": { "dateRange": { "from": ' .$from_waf. ', "to": ' .$to. ' }, "pagination": { "cursor": null }, "order": { "direction": "DESC", "field": "time" } } } }'; |
| 228 |
$response = self::requestApi($payload, true); |
| 229 |
return $response['data']['auth']['viewer']['sites']['one']['firewall']['chart']; |
| 230 |
} |
| 231 |
|
| 232 |
public static function forceCheck($host_id, $service) |
| 233 |
{ |
| 234 |
$payload = '{"variables":{"id":"'.$host_id.'","service":"'.$service.'"},"query":"mutation ($id: ID!, $service: ForceCheckService!) { auth { sites { forceCheck(siteId: $id, service: $service) } } }"} '; |
| 235 |
return self::requestApi($payload, true); |
| 236 |
} |
| 237 |
|
| 238 |
public static function avExport($host_id) |
| 239 |
{ |
| 240 |
$from = time() - (60 * 60 * 24 * 30); |
| 241 |
$to = time(); |
| 242 |
$payload = '{"variables":{ "input":{"siteId":"'.$host_id.'", "dateRange":{"to":'.$to.',"from":'.$from.'} }},"query":"mutation ($input: AvLogExportInput!) { auth { sites { av { export(input: $input) } } } }"} '; |
| 243 |
return self::requestApi($payload, true); |
| 244 |
} |
| 245 |
|
| 246 |
public static function wafGetIpList($host_id) |
| 247 |
{ |
| 248 |
$payload = '{"variables":{ "id": "'.$host_id.'" },"query":"query($id: ID!) { auth { viewer { sites{ one(id: $id){ firewall{ blackList{ id ip createdAt } whiteList{ id ip createdAt } settings{ gdn dosProtection dosLimit loginAttemptsProtection loginAttemptsLimit } } } } } } }"} '; |
| 249 |
return self::requestApi($payload, true); |
| 250 |
} |
| 251 |
|
| 252 |
public static function wafAddIpToList($host_id, $ips, $color) |
| 253 |
{ |
| 254 |
$payload = '{"variables":{ "input": { "siteId": "'.$host_id.'", "ips": "'.$ips.'", "color": "'.$color.'" } }, "query":"mutation($input: WafListInput!) { auth { sites { waf { addToList(input: $input) } } } }"} '; |
| 255 |
return self::requestApi($payload, true); |
| 256 |
} |
| 257 |
|
| 258 |
public static function wafRemoveIpToList($id) |
| 259 |
{ |
| 260 |
$payload = '{"variables":{ "id": "'.$id.'" },"query":"mutation($id: ID!) { auth { sites { waf { removeFromList(id: $id) } } } }"} '; |
| 261 |
return self::requestApi($payload, true); |
| 262 |
} |
| 263 |
|
| 264 |
public static function changeStatus($config_id, $host_id) |
| 265 |
{ |
| 266 |
$payload = '{"query":"\n mutation {\n toggleServiceConfig(\n id: ' . $config_id . '\n userhostId: ' . $host_id . '\n ) {\n isActive\n }\n }\n "}'; |
| 267 |
return self::requestApi($payload, true); |
| 268 |
} |
| 269 |
|
| 270 |
public static function serviceConnect($id, $service) |
| 271 |
{ |
| 272 |
$payload = '{"query":"mutation {\n auth {\n agents{\n check(siteId:\"' . $id . '\",service:' . strtolower($service) . ',plugin:WORDPRESS)\n }\n }\n}\n"}'; |
| 273 |
return self::requestApi($payload, true); |
| 274 |
} |
| 275 |
|
| 276 |
public static function generateFile($id, $service) |
| 277 |
{ |
| 278 |
$payload = '{"query":"mutation {\n auth {\n agents{\n generate(siteId:\"' . $id . '\",service:' . strtolower($service) . '){\n agentName\n }\n }\n }\n}\n"}'; |
| 279 |
return self::requestApi($payload, true); |
| 280 |
} |
| 281 |
|
| 282 |
public static function generateAmFile($id) |
| 283 |
{ |
| 284 |
$payload = '{ "operationName":null, "variables":{}, "query":"mutation { auth { am { install(siteId: \"'.$id.'\"){ downloadLink, amFilename, wafFilename, avFilename } } } }"}'; |
| 285 |
return self::requestApi($payload, true); |
| 286 |
} |
| 287 |
|
| 288 |
public static function deleteAm($host_id) |
| 289 |
{ |
| 290 |
$payload = '{ "query":"mutation { auth { am { delete(siteId: \"'.$host_id.'\") } } }" }'; |
| 291 |
$response = self::requestApi($payload, true); |
| 292 |
return $response['data']['auth']['am']['delete']; |
| 293 |
} |
| 294 |
|
| 295 |
public static function checkStatus($id, $service) |
| 296 |
{ |
| 297 |
$payload = '{"operationName":null,"variables":{"id":"' . $id . '"},"query":"query ($id: ID!) {\n auth {\n viewer {\n sites {\n one(id: $id) {\n ... on Site {\n configs {\n ... on ' . $service . 'Config {\n isActive\n id\n }\n }\n }\n }\n }\n }\n }\n}\n"}'; |
| 298 |
return self::requestApi($payload, true); |
| 299 |
} |
| 300 |
|
| 301 |
public static function getStatuses($host_id) |
| 302 |
{ |
| 303 |
$payload = '{"variables":{"id":"' . $host_id . '"},"query":"query($id: ID!) { auth { viewer { sites { one(id: $id) { antivirus { status } firewall { status } deface {status} domain {status} ports {status} ssl {status} scoring {status} } } } } }"}'; |
| 304 |
|
| 305 |
return self::requestApi($payload, true); |
| 306 |
} |
| 307 |
|
| 308 |
public static function getQuarantineList($host_id) |
| 309 |
{ |
| 310 |
$payload = '{"query":"query{ auth{ viewer{ sites{ one(id:\"'.$host_id.'\"){ antivirus{ quarantine{ id path date } } } } } } } "}'; |
| 311 |
return self::requestApi($payload, true); |
| 312 |
} |
| 313 |
|
| 314 |
public static function moveToQuarantine($host_id, $path) |
| 315 |
{ |
| 316 |
$payload = '{"query":"mutation{ auth{ sites{ av{ moveToQuarantine(input:{ siteId:\"'.$host_id.'\", path:\"'.$path.'\" }) } } } } "}'; |
| 317 |
return self::requestApi($payload, true); |
| 318 |
} |
| 319 |
|
| 320 |
public static function moveFromQuarantine($id) |
| 321 |
{ |
| 322 |
$payload = '{"query":"mutation{ auth{ sites{ av{ moveFromQuarantine(id: \"'.$id.'\") } } } } "}'; |
| 323 |
return self::requestApi($payload, true); |
| 324 |
} |
| 325 |
|
| 326 |
public static function reportsList($host_id, $limit = 10, $cursor = null) |
| 327 |
{ |
| 328 |
$cursor = ($cursor == null) ? 'null' : '"' . $cursor . '"'; |
| 329 |
$payload = '{"variables":{ "filter": { "order": { "direction": "DESC", "field": "created_at"}, "siteId":"'.$host_id.'", "pagination":{"first":' . $limit . ', "cursor":' . $cursor . '} } },"query":"query ReportsQuery($filter: ReportListFilter!) { auth { viewer { reports { list(filter: $filter) { edges { node { id site { hostname } createdAt wa dc ps rc sc av waf } cursor } pageInfo { endCursor hasNextPage } } } } } }"}'; |
| 330 |
return self::requestApi($payload, true); |
| 331 |
} |
| 332 |
|
| 333 |
public static function reportGenerate($host_id, $period, $services, $language = "en") |
| 334 |
{ |
| 335 |
$from = (is_array($period)) ? $period['from'] : time() - (60 * 60 * 24 * $period); |
| 336 |
$to = (is_array($period)) ? $period['to'] : time(); |
| 337 |
$payload = '{"query":"query ($input: GenerateReportInput) { auth { viewer { reports { generate(input: $input) } } } }", "variables":{ "input": { "siteId": "' . $host_id . '", "from": ' . $from . ', "to": ' . $to . ', "wa": '.$services['wa'].', "dc": '.$services['dc'].', "ps": '.$services['ps'].', "rc": '.$services['rc'].', "sc": '.$services['sc'].', "av": '.$services['av'].', "waf": '.$services['waf'].', "language": "'.$language.'" } } }'; |
| 338 |
|
| 339 |
return self::requestApi($payload, true); |
| 340 |
} |
| 341 |
|
| 342 |
public static function reportDownload($id) |
| 343 |
{ |
| 344 |
$payload = '{"query": "query { auth { viewer { reports { download(id: \"' . $id . '\") } } } }"}'; |
| 345 |
return self::requestApi($payload, true); |
| 346 |
} |
| 347 |
|
| 348 |
public static function getConfigs($host_id) |
| 349 |
{ |
| 350 |
$payload = '{"query":"query{ auth{ viewer{ sites{ one(id:\"'.$host_id.'\"){ configs{ ... on WaConfig { id service isActive } ... on AvConfig { id service isActive } ... on DcConfig { id service isActive } ... on DecConfig { id service isActive } ... on RcConfig { id service isActive } ... on CmsConfig { id service isActive } ... on PsConfig { id service isActive } ... on WafConfig { id service isActive } } } } } } } "}'; |
| 351 |
return self::requestApi($payload, true); |
| 352 |
} |
| 353 |
|
| 354 |
public static function toggleConfigs($service_id) |
| 355 |
{ |
| 356 |
$payload = '{"query":"mutation{ auth{ configs{ toggle(id: \"'.$service_id.'\"){ ... on WaConfig { service isActive } ... on AvConfig { service isActive } ... on DcConfig { service isActive } ... on DecConfig { service isActive } ... on RcConfig { service isActive } ... on CmsConfig { service isActive } ... on PsConfig { service isActive } ... on WafConfig { service isActive } } } } } "}'; |
| 357 |
return self::requestApi($payload, true); |
| 358 |
} |
| 359 |
|
| 360 |
public static function wafSettings($host_id, $settings) |
| 361 |
{ |
| 362 |
$payload = '{"variables":{"input": {"siteId": "'.$host_id.'", "gdn": '. $settings['gdn'] .', "dosProtection": '.$settings['dosProtection'].', "dosLimit": '.$settings['dosLimit'].', "loginAttemptsProtection": '.$settings['loginAttemptsProtection'].', "loginAttemptsLimit": '.$settings['loginAttemptsLimit'].'}},"query":"mutation WafSettings($input: WafSettingsInput!) { auth { sites { waf{ settings(input: $input) { gdn dosProtection loginAttemptsProtection dosLimit loginAttemptsLimit } } } } }"}'; |
| 363 |
return self::requestApi($payload, true); |
| 364 |
} |
| 365 |
|
| 366 |
public static function getServerStatusChart($host_id, $days = 7) |
| 367 |
{ |
| 368 |
$to = time(); |
| 369 |
$from = ($days <= 1) ? strtotime(date('Y-m-d 00:00:01')) : time() - (60 * 60 * 24 * $days); |
| 370 |
|
| 371 |
$payload = '{ "query":"query($id: ID!, $dateRange: DateRangeInput!) { auth { viewer { sites { one(id: $id) { serverStatus { ramChart(dateRange: $dateRange){ total value time } cpuChart(dateRange: $dateRange){ value time } } } } } } }", "variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from . '} } }'; |
| 372 |
|
| 373 |
$response = self::requestApi($payload, true); |
| 374 |
return $response['data']['auth']['viewer']['sites']['one']['serverStatus']; |
| 375 |
} |
| 376 |
|
| 377 |
public static function getStatusIcon($status) |
| 378 |
{ |
| 379 |
$statusesText = wtsec_getStatuses(); |
| 380 |
$statuses = [ |
| 381 |
"clean" => [ |
| 382 |
"text" => $statusesText['ok'], |
| 383 |
"color" => self::getColor("success"), |
| 384 |
"image" => "check-mark.svg" |
| 385 |
], |
| 386 |
"pending" => [ |
| 387 |
"text" => $statusesText['pending'], |
| 388 |
"color" => self::getColor("grey"), |
| 389 |
"image" => "loading.svg" |
| 390 |
], |
| 391 |
"pause" => [ |
| 392 |
"text" => $statusesText['pause'], |
| 393 |
"color" => self::getColor("grey"), |
| 394 |
"image" => "warning.svg" |
| 395 |
], |
| 396 |
"expired" => [ |
| 397 |
"text" => $statusesText['expired'], |
| 398 |
"color" => self::getColor("orange"), |
| 399 |
"image" => "warning.svg" |
| 400 |
|
| 401 |
], |
| 402 |
"invalid" => [ |
| 403 |
"text" => $statusesText['invalid'], |
| 404 |
"color" => self::getColor("error"), |
| 405 |
"image" => "warning.svg" |
| 406 |
], |
| 407 |
"no_cert" => [ |
| 408 |
"text" => $statusesText['no_cert'], |
| 409 |
"color" => self::getColor("orange"), |
| 410 |
"image" => "warning.svg" |
| 411 |
], |
| 412 |
"error" => [ |
| 413 |
"text" => $statusesText['error'], |
| 414 |
"color" => self::getColor("error"), |
| 415 |
"image" => "warning.svg" |
| 416 |
], |
| 417 |
"expires" => [ |
| 418 |
"text" => $statusesText['expires'], |
| 419 |
"color" => self::getColor("orange"), |
| 420 |
"image" => "warning.svg" |
| 421 |
], |
| 422 |
"expires_today" => [ |
| 423 |
"text" => $statusesText['expires_today'], |
| 424 |
"color" => self::getColor("error"), |
| 425 |
"image" => "warning.svg" |
| 426 |
], |
| 427 |
"down" => [ |
| 428 |
"text" => $statusesText['down'], |
| 429 |
"color" => self::getColor("error"), |
| 430 |
"image" => "warning.svg" |
| 431 |
], |
| 432 |
"up" => [ |
| 433 |
"text" => $statusesText['up'], |
| 434 |
"color" => self::getColor("success"), |
| 435 |
"image" => "check-mark.svg" |
| 436 |
], |
| 437 |
"infected" => [ |
| 438 |
"text" => $statusesText['infected'], |
| 439 |
"color" => self::getColor("error"), |
| 440 |
"image" => "warning.svg" |
| 441 |
], |
| 442 |
"open_ports" => [ |
| 443 |
"text" => $statusesText['open'], |
| 444 |
"color" => self::getColor("orange"), |
| 445 |
"image" => "warning.svg" |
| 446 |
], |
| 447 |
"deface" => [ |
| 448 |
"text" => $statusesText['deface'], |
| 449 |
"color" => self::getColor("error"), |
| 450 |
"image" => "warning.svg" |
| 451 |
], |
| 452 |
"modified" => [ |
| 453 |
"text" => $statusesText['modified'], |
| 454 |
"color" => self::getColor("grey"), |
| 455 |
"image" => "warning.svg" |
| 456 |
], |
| 457 |
"not_supported" => [ |
| 458 |
"text" => $statusesText['unsupported'], |
| 459 |
"color" => self::getColor("orange"), |
| 460 |
"image" => "warning.svg" |
| 461 |
], |
| 462 |
"not_registered" => [ |
| 463 |
"text" => $statusesText['not_registered'], |
| 464 |
"color" => self::getColor("orange"), |
| 465 |
"image" => "warning.svg" |
| 466 |
], |
| 467 |
"not_installed" => [ |
| 468 |
"text" => $statusesText['not_installed'], |
| 469 |
"color" => self::getColor("error"), |
| 470 |
"image" => "warning.svg" |
| 471 |
], |
| 472 |
"working" => [ |
| 473 |
"text" => $statusesText['working'], |
| 474 |
"color" => self::getColor("success"), |
| 475 |
"image" => "check-mark.svg" |
| 476 |
], |
| 477 |
]; |
| 478 |
if (isset($statuses[$status])) { |
| 479 |
$statuses[$status]['type'] = $status; |
| 480 |
$statuses[$status]["text"] = "<span data-status='$status' class='" . $statuses[$status]["color"]["color"] . "'>" . $statuses[$status]["text"] . "</span>"; |
| 481 |
return $statuses[$status]; |
| 482 |
} else { |
| 483 |
return ["image" => "warning.svg", "color" => "ww--status_unknow", "text" => "<span class='ww--status_unknow'>Unknown status</span>"]; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
public static function getColor($type) |
| 488 |
{ |
| 489 |
$types = [ |
| 490 |
"green" => "is--status--ok", |
| 491 |
"red" => "is--status--error", |
| 492 |
"success" => "is--status--ok", |
| 493 |
"error" => "is--status--error", |
| 494 |
"orange" => "is--status--warning", |
| 495 |
"warning" => "is--status--warning", |
| 496 |
"grey" => "ww--status_unknow" |
| 497 |
]; |
| 498 |
$icon_types = [ |
| 499 |
"green" => "ww-icon--status_ok", |
| 500 |
"red" => "ww-icon--status_error", |
| 501 |
"success" => "ww-icon--status_ok", |
| 502 |
"error" => "ww-icon--status_error", |
| 503 |
"orange" => "ww-icon--status_warning", |
| 504 |
"warning" => "ww-icon--status_warning", |
| 505 |
"grey" => "ww-icon--status_unknow" |
| 506 |
]; |
| 507 |
return ["icon" => $icon_types[$type], "color" => $types[$type]]; |
| 508 |
} |
| 509 |
|
| 510 |
public static function getConfigId($configs) |
| 511 |
{ |
| 512 |
$id = 0; |
| 513 |
$is_active = false; |
| 514 |
foreach ($configs as &$config) { |
| 515 |
if (!empty($config)) { |
| 516 |
$id = $config['id']; |
| 517 |
$is_active = $config['isActive']; |
| 518 |
break; |
| 519 |
} |
| 520 |
} |
| 521 |
return compact('id', 'is_active'); |
| 522 |
} |
| 523 |
|
| 524 |
public static function getScore(){ |
| 525 |
return self::requestURL("https://api.wtotem.com/site/score?url=".WTSEC_SITE_URL); |
| 526 |
} |
| 527 |
|
| 528 |
} |
| 529 |
|
| 530 |
|