$payload, 'timeout' => '30', 'sslverify' => false, 'headers' => ['Content-Type:application/json', 'Content-Type' => 'application/json', 'source:WORDPRESS', 'Accept: application/json'], ]; if (!is_null($token) && $token) { $authorization = "Bearer " . $token; $args['headers'] = array_merge($args['headers'], ["Authorization" => $authorization]); } $response = wp_remote_post(self::URL, $args); $httpcode = wp_remote_retrieve_response_code($response); if ($httpcode < 200) { WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server')); } $response = wp_remote_retrieve_body($response); $result = json_decode($response, true); if (isset($result['errors'][0]['message'])) { $message = self::diffMesageForHuman($result['errors'][0]['message']); if (stripos($result['errors'][0]['message'], "token") !== false && !$repeat) { $result = WTSEC_LIBRARY_WT::auth(wtsec_app()->get("api_key")); if (isset($result['data']['guest']['apiKeys']['auth']['token']['value'])) { $token_ = $result['data']['guest']['apiKeys']['auth']['token']['value']; WTSEC_LIBRARY_App::login($token_); return self::requestApi($payload, $token, true); } else { WTSEC_LIBRARY_App::logout(); } } else { if ($message !== false) { WTSEC_LIBRARY_Session::setNotification("warning", $message); } } } return $result; } public static function diffMesageForHuman($message) { $definition = $message; $excepts = [ "RESOURCE_NOT_FOUND", "DUPLICATE_HOST", "INVALID_CREDENTIALS", "INVALID_API_KEY", "Invalid token", ]; if (in_array($message, $excepts)) { return false; } switch ($message) { case 'HOSTS_LIMIT_EXCEEDED': $definition = WTSEC_LIBRARY_Localization::lmsg('hosts_limit_exceed'); break; case 'RESOURCE_NOT_FOUND': $definition = WTSEC_LIBRARY_Localization::lmsg('resource_not_found'); break; case 'Invalid token': $definition = WTSEC_LIBRARY_Localization::lmsg('invalid_token'); break; case 'USER_ALREADY_REGISTERED': $definition = WTSEC_LIBRARY_Localization::lmsg('user_already_exist'); break; case 'DUPLICATE_HOST': $definition = WTSEC_LIBRARY_Localization::lmsg('duplicate_host'); break; case 'Agent does not exist or has been already verified': $definition = WTSEC_LIBRARY_Localization::lmsg('agent_does_not_exist_or_has_been_already_verified'); break; case 'INVALID_DOMAIN_NAME': $definition = WTSEC_LIBRARY_Localization::lmsg('invalid_domain_name'); break; } return $definition; } public static function requestURL($url) { $args = [ 'timeout' => '30', 'sslverify' => false, ]; $response = wp_remote_get($url, $args); $httpcode = wp_remote_retrieve_response_code($response); if ($httpcode < 200) { WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server')); } dd($response['headers']["content-disposition"]); $response = wp_remote_retrieve_body($response); return $response; } public static function getFileByUrl($url) { $args = [ 'timeout' => '30', 'sslverify' => false, ]; $response = wp_remote_get($url, $args); $httpcode = wp_remote_retrieve_response_code($response); if ($httpcode < 200) { WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg('could_not_connect_to_the_server')); } $filename = null; $metas = explode(";",$response['headers']["content-disposition"]); foreach ($metas as $meta){ if(stripos($meta,"filename") !== false){ $filename = trim(str_replace(["filename=","=","\""],"",$meta)); } } $response = wp_remote_retrieve_body($response); return ["body" => $response, "filename" => $filename]; } public static function getOwnSite($attempt = false) { $payload = '{"query":"query getSites { auth { viewer { sites { ...sites __typename } __typename } __typename }}fragment sites on SiteQueries { list(filter: {}) { edges { node { id hostname title ssl { status __typename } availability { status __typename } reputation { status __typename } ports { status __typename } deface { status __typename } domain { status __typename } antivirus { status __typename } firewall { status __typename } maliciousScript { stack { name __typename } __typename } __typename } __typename } __typename } __typename}"}'; $result = self::requestApi($payload, true); if (isset($result['data']['auth']['viewer']['sites']['list']['edges'])) { //mutator foreach ($result['data']['auth']['viewer']['sites']['list']['edges'] as &$m) { if (isset($m['node']['hostname'])) { $m['node']['hostname'] = WTSEC_LIBRARY_Idn::idn_to_utf8($m['node']['hostname']); // if ($m['node']['id'] == "c2l0ZV8yNTg=") { // return $m['node']; // } if (self::isSiteUrl($m['node']['hostname'])) { return $m['node']; } } } $add_site = self::addSite(WTSEC_SITE_URL); if (isset($add_site['errors'])) { return $result['data']['node'] = []; } else { if (!$attempt) { return self::getOwnSite(true); } } } return []; } public static function isSiteUrl($url) { return WTSEC_LIBRARY_Idn::idn_to_utf8(WTSEC_SITE_URL) === $url; } public static function addSite($url) { $payload = '{"variables":{"input":{"title":"' . $url . '","hostname":"' . $url . '","configs":{"scheme":"http","port":80,"wa":{},"dec":{},"ps":{}}}},"query":"mutation ($input: CreateSiteInput) {\n auth {\n sites {\n create(input: $input) {\n id\n hostname\n title\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"}'; return self::requestApi($payload, true); } public static function getAllChecks($host_id) { $from = time() - (60 * 60 * 24); $to = time(); $from_waf = time() - (60 * 60 * 24 * 7); $payload = '{"query":"query($id: ID!, $dateRange:DateRangeInput!) {\n auth {\n viewer {\n sites {\n one(id: $id) {\n availability {\n status\n lastTest {\n time\n }\n responseTime\n downTime(dateRange: $dateRange)\n percent(dateRange: $dateRange)\n }\n deface {\n status\n lastTest {\n time\n }\n words\n count\n }\n domain {\n status\n registrar\n owner\n email\n createdDate\n expiredDate\n }\n ports {\n status\n lastTest {\n time\n }\n ip\n tcp\n country\n }\n ssl {\n status\n daysLeft\n expiryDate\n issueDate\n }\n reputation {\n status\n lastTest {\n time\n }\n virusList {\n viruses\n antiVirus\n }\n }\n firewall {\n lastTest { \n time\n } \n status\n chart(dateRange: $dateRange) {\n time\n attacks\n blocked\n }\n report(dateRange: $dateRange) {\n time\n attacks\n ip\n }\n }\n maliciousScript{\n lastTest{\n time\n }\n status\n }\n antivirus{\n status \n stats { \n changed\n deleted\n scaned\n infected\n error\n } \n lastTest { \n time\n } \n isFirstCheck\n }\n }\n }\n }\n }\n}","variables":{"id":"' . $host_id . '","dateRange":{"to":' . $to . ',"from":' . $from_waf . '}}}'; $response = self::requestApi($payload, true); if (isset($response['data']['auth']['viewer']['sites']['one'])) { return $response['data']['auth']['viewer']['sites']['one']; } return []; } public static function changeStatus($config_id, $host_id) { $payload = '{"query":"\n mutation {\n toggleServiceConfig(\n id: ' . $config_id . '\n userhostId: ' . $host_id . '\n ) {\n isActive\n }\n }\n "}'; return self::requestApi($payload, true); } public static function serviceConnect($id, $service) { $payload = '{"query":"mutation {\n auth {\n agents{\n check(siteId:\"' . $id . '\",service:' . strtolower($service) . ',plugin:WORDPRESS)\n }\n }\n}\n"}'; return self::requestApi($payload, true); } public static function generateFile($id, $service) { $payload = '{"query":"mutation {\n auth {\n agents{\n generate(siteId:\"' . $id . '\",service:' . strtolower($service) . '){\n agentName\n }\n }\n }\n}\n"}'; return self::requestApi($payload, true); } public static function generateAmFile($id) { $payload = '{"operationName":null,"variables":{},"query":"mutation {\n auth {\n am {\n install(siteId: \"'.$id.'\")\n }\n }\n}\n"}'; return self::requestApi($payload, true); } public static function checkStatus($id, $service) { $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"}'; return self::requestApi($payload, true); } public static function getOptions($host_id) { $payload = '{"query":"query{\nuserHost(id:' . $host_id . '){\n id\n title\n hostname\n stack\n createdAt\n services {\n id\n name\n configs {\n id\n \tdata\n isActive\n }\n }\n }\n}"}'; return self::requestApi($payload, true); } public static function getAntivirus($host_id) { $payload = '{"operationName":null,"variables":{"id":"' . $host_id . '","avLogFilter":{"event":"infected","order":{"direction":"DESC","field":"time"},"pagination":{"first":10,"cursor":null}}},"query":"query ($id: ID!, $avLogFilter: AvLogFilter!) {\n auth {\n viewer {\n sites {\n one(id: $id) {\n id\n ... on Site {\n configs {\n ... on AvConfig {\n isActive\n id\n }\n }\n }\n antivirus {\n status\n log(avLogFilter: $avLogFilter) {\n edges {\n node {\n filePath\n matches\n event\n signatures\n time\n }\n }\n }\n lastTest {\n time\n }\n stats {\n changed\n deleted\n scaned\n infected\n }\n }\n }\n }\n }\n }\n}\n"}'; return self::requestApi($payload, true); } public static function getStatusIcon($status) { $statuses = [ "clean" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.ok'), "color" => self::getColor("success") ], "pending" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.pending'), "color" => self::getColor("grey") ], "expired" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.expired'), "color" => self::getColor("orange") ], "invalid" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.invalid'), "color" => self::getColor("error") ], "no_cert" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.no_cert'), "color" => self::getColor("orange") ], "error" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.error'), "color" => self::getColor("error") ], "expires" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.expires'), "color" => self::getColor("orange") ], "expires_today" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.expires_today'), "color" => self::getColor("error") ], "down" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.down'), "color" => self::getColor("error") ], "up" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.up'), "color" => self::getColor("success") ], "infected" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.infected'), "color" => self::getColor("error") ], "open_ports" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.open'), "color" => self::getColor("orange") ], "deface" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.deface'), "color" => self::getColor("error") ], "modified" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.modified'), "color" => self::getColor("grey") ], "not_supported" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.unsupported'), "color" => self::getColor("orange") ], "not_registered" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.not_registered'), "color" => self::getColor("orange") ], "not_installed" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.not_installed'), "color" => self::getColor("error") ], "working" => [ "text" => WTSEC_LIBRARY_Localization::lmsg('statuses.working'), "color" => self::getColor("success") ], ]; if (isset($statuses[$status])) { return ["icon" => $statuses[$status]["color"]["icon"], "color" => $statuses[$status]["color"]["color"], "text" => "" . $statuses[$status]["text"] . ""]; } else { return ["icon" => "ww-icon--status_unknow", "color" => "ww--status_unknow", "text" => "Unknown status"]; } } public static function getColor($type) { $types = [ "green" => "is--status--ok", "red" => "is--status--error", "success" => "is--status--ok", "error" => "is--status--error", "orange" => "is--status--warning", "warning" => "is--status--warning", "grey" => "ww--status_unknow" ]; $icon_types = [ "green" => "ww-icon--status_ok", "red" => "ww-icon--status_error", "success" => "ww-icon--status_ok", "error" => "ww-icon--status_error", "orange" => "ww-icon--status_warning", "warning" => "ww-icon--status_warning", "grey" => "ww-icon--status_unknow" ]; return ["icon" => $icon_types[$type], "color" => $types[$type]]; } public static function getConfigId($configs) { $id = 0; $is_active = false; foreach ($configs as &$config) { if (!empty($config)) { $id = $config['id']; $is_active = $config['isActive']; break; } } return compact('id', 'is_active'); } }