| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 4 | - if (!headers_sent()) { | |
| 5 | - header('HTTP/1.1 403 Forbidden'); | |
| 6 | - } | |
| 7 | - die("Protected By WebTotem!"); | |
| 4 | + if (!headers_sent()) { | |
| 5 | + header('HTTP/1.1 403 Forbidden'); | |
| 6 | + } | |
| 7 | + die("Protected By WebTotem!"); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * WebTotem API class. |
| @@ -15,1001 +15,917 @@ | ||
| 15 | 15 | * @version 1.0 |
| 16 | 16 | * @copyright (C) 2022 WebTotem team (http://wtotem.com) |
| 17 | 17 | * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html |
| 18 | 18 | */ |
| 19 | -class WebTotemAPI extends WebTotem | |
| 20 | -{ | |
| 19 | +class WebTotemAPI extends WebTotem { | |
| 21 | 20 | |
| 22 | - /** | |
| 23 | - * HTTP status of the most recent API call, 0 when the request never landed. | |
| 24 | - * | |
| 25 | - * @var int | |
| 26 | - */ | |
| 27 | - protected static $last_status = 0; | |
| 21 | + /** | |
| 22 | + * Method for getting a auth token. | |
| 23 | + * | |
| 24 | + * @param string $api_key | |
| 25 | + * Application programming interface key. | |
| 26 | + * | |
| 27 | + * @return bool|string | |
| 28 | + * Returns auth status | |
| 29 | + */ | |
| 30 | + public static function auth($api_key) { | |
| 31 | + $domain = WEBTOTEM_SITE_DOMAIN; | |
| 28 | 32 | |
| 29 | - /** | |
| 30 | - * HTTP status of the most recent API call. | |
| 31 | - * | |
| 32 | - * @return int | |
| 33 | - * Status code, or 0 when the request did not reach the server. | |
| 34 | - */ | |
| 35 | - public static function getLastStatus() | |
| 36 | - { | |
| 37 | - return (int) self::$last_status; | |
| 38 | - } | |
| 33 | + $payload = '{"query":"mutation{ guest{ apiKeys{ auth(apiKey:\"' . $api_key . '\", source:\"' . $domain . '\"),{ token{ value, refreshToken, expiresIn } } } } }"}'; | |
| 34 | + $result = self::sendRequest($payload, FALSE, TRUE); | |
| 39 | 35 | |
| 40 | - /** | |
| 41 | - * Raises a notification unless the caller asked to stay quiet. | |
| 42 | - * | |
| 43 | - * Probing calls (such as the WebSocket ticket, which is expected to be | |
| 44 | - * missing on older API builds) must not spam the admin with errors. | |
| 45 | - * | |
| 46 | - * @param bool $silent | |
| 47 | - * TRUE to swallow the notification. | |
| 48 | - * @param string $type | |
| 49 | - * Notification type. | |
| 50 | - * @param string $message | |
| 51 | - * Notification text. | |
| 52 | - * | |
| 53 | - * @return void | |
| 54 | - */ | |
| 55 | - protected static function notify($silent, $type, $message) | |
| 56 | - { | |
| 57 | - if (!$silent) { | |
| 58 | - WebTotemOption::setNotification($type, $message); | |
| 59 | - } | |
| 36 | + if (isset($result['data']['guest']['apiKeys']['auth']['token']['value'])) { | |
| 37 | + $auth_token = $result['data']['guest']['apiKeys']['auth']['token']; | |
| 38 | + WebTotemOption::login(['token' => $auth_token, 'api_key' => $api_key]); | |
| 39 | + return 'success'; | |
| 40 | + } elseif($result['errors'][0]['message'] == 'INVALID_API_KEY') { | |
| 41 | + WebTotemOption::logout(); | |
| 60 | 42 | } |
| 61 | 43 | |
| 44 | + return FALSE; | |
| 45 | + } | |
| 62 | 46 | |
| 63 | - /** | |
| 64 | - * Method for getting an auth token. | |
| 65 | - * | |
| 66 | - * @param string $api_key | |
| 67 | - * Application programming interface key. | |
| 68 | - * | |
| 69 | - * @return bool|string | |
| 70 | - * Returns auth status | |
| 71 | - */ | |
| 72 | - public static function auth($api_key, $repeat = FALSE) | |
| 73 | - { | |
| 74 | - $domain = WEBTOTEM_SITE_DOMAIN; | |
| 75 | 47 | |
| 76 | - if (empty($api_key)) { | |
| 77 | - return FALSE; | |
| 78 | - } | |
| 48 | + /** | |
| 49 | + * Get site info from API server. | |
| 50 | + * | |
| 51 | + * @param string $attempt | |
| 52 | + * Is the request an attempt to get host data. | |
| 53 | + * | |
| 54 | + * @return array | |
| 55 | + * Returns host data. | |
| 56 | + */ | |
| 57 | + public static function siteInfo($attempt = FALSE) { | |
| 79 | 58 | |
| 80 | - $data = ['api_key' => $api_key, 'site' => $domain]; | |
| 81 | - $result = self::sendRequest('auth/sign-in/api-key', $data, 'POST', FALSE, TRUE); | |
| 59 | + if(self::isMultiSite()){ | |
| 60 | + $host['id'] = WebTotemOption::getSessionOption('host_id'); | |
| 82 | 61 | |
| 83 | - if($result === null){ | |
| 84 | - WebTotemOption::setNotification('warning' , __('Authorization failed. The server may be temporarily unavailable', 'wtotem')); | |
| 85 | - } | |
| 62 | + if ($host['id']) { | |
| 63 | + return $host; | |
| 64 | + } | |
| 65 | + } | |
| 86 | 66 | |
| 87 | - if (isset($result['access_token'])) { | |
| 88 | - $auth_token = $result['access_token']; | |
| 89 | - if(!WebTotemOption::isActivated()){ | |
| 90 | - WebTotemOption::login(['token' => $auth_token, 'api_key' => $api_key]); | |
| 91 | - WebTotemAgentManager::postdelete(); | |
| 92 | - } else { | |
| 93 | - WebTotemOption::refreshToken($auth_token); | |
| 94 | - } | |
| 67 | + $host = WebTotemOption::getHost(); | |
| 95 | 68 | |
| 96 | - return 'success'; | |
| 97 | - } elseif (isset($result['message']) and $result['message'] == 'invalid credentials') { | |
| 98 | - WebTotemOption::logout(); | |
| 99 | - } | |
| 69 | + if ($host['id']) { | |
| 70 | + return $host; | |
| 71 | + } | |
| 100 | 72 | |
| 101 | - if($repeat == false){ | |
| 102 | - //self::checkEndpoint(); | |
| 103 | - return self::auth($api_key, true); | |
| 104 | - } | |
| 73 | + $all_sites = self::getSites(null, 0); | |
| 74 | + if(self::isMultiSite()) { | |
| 75 | + $sites = get_sites(); | |
| 76 | + foreach ($sites as $site){ | |
| 77 | + $domain = untrailingslashit($site->domain . $site->path); | |
| 78 | + self::addSite($domain, $all_sites); | |
| 79 | + } | |
| 105 | 80 | |
| 106 | - return FALSE; | |
| 81 | + if (!$attempt) { | |
| 82 | + return self::siteInfo(TRUE); | |
| 83 | + } | |
| 107 | 84 | } |
| 85 | + else { | |
| 86 | + $domain = WEBTOTEM_SITE_DOMAIN; | |
| 87 | + return self::addSite($domain, $all_sites); | |
| 88 | + } | |
| 108 | 89 | |
| 109 | - /** | |
| 110 | - * Method for getting API url. | |
| 111 | - * | |
| 112 | - * @return string|bool | |
| 113 | - * API url | |
| 114 | - */ | |
| 115 | - public static function getApiUrl() | |
| 116 | - { | |
| 117 | - return 'https://app.wtotem.com'; | |
| 118 | - } | |
| 90 | + return []; | |
| 91 | + } | |
| 119 | 92 | |
| 120 | - /** | |
| 121 | - * Method for getting the WebSocket endpoint url. | |
| 122 | - * | |
| 123 | - * @return string | |
| 124 | - * WebSocket url, without any credentials. | |
| 125 | - */ | |
| 126 | - public static function getWsUrl() | |
| 127 | - { | |
| 128 | - $api_url = WebTotemOption::getOption('api_url'); | |
| 129 | - if (!$api_url) { | |
| 130 | - $api_url = self::getApiUrl(); | |
| 131 | - } | |
| 93 | + /** | |
| 94 | + * Method for adding a site to the WebTotem platform. | |
| 95 | + * | |
| 96 | + * @param string $domain | |
| 97 | + * Domain to add. | |
| 98 | + * @param array $all_sites | |
| 99 | + * Array with site data on the WebTotem platform. | |
| 100 | + * | |
| 101 | + * @return array | |
| 102 | + * Returns host data. | |
| 103 | + */ | |
| 104 | + public static function addSite( $domain, $all_sites) { | |
| 132 | 105 | |
| 133 | - return preg_replace('#^http#i', 'ws', rtrim($api_url, '/')) . '/api/v1/ws'; | |
| 134 | - } | |
| 106 | + if(function_exists('idn_to_utf8')){ | |
| 107 | + $domain = idn_to_utf8($domain); | |
| 108 | + } | |
| 135 | 109 | |
| 110 | + // Checking if the site has been added to the WebTotem. | |
| 111 | + foreach ($all_sites['edges'] as $site){ | |
| 112 | + $site = $site['node']; | |
| 113 | + // If it added, save site data to DB. | |
| 114 | + if($site['hostname'] == $domain or 'www.' . $site['hostname'] == $domain){ | |
| 115 | + WebTotemOption::setHost($site['hostname'], $site['id']); | |
| 116 | + return [ | |
| 117 | + 'id' => $site['id'], | |
| 118 | + 'name' => $site['hostname'], | |
| 119 | + ]; | |
| 120 | + } | |
| 121 | + } | |
| 136 | 122 | |
| 137 | - /** | |
| 138 | - * Get site info from API server. | |
| 139 | - * | |
| 140 | - * @param string $attempt | |
| 141 | - * Is the request an attempt to get host data. | |
| 142 | - * | |
| 143 | - * @return array | |
| 144 | - * Returns host data. | |
| 145 | - */ | |
| 146 | - public static function siteInfo($attempt = FALSE) | |
| 147 | - { | |
| 148 | - if (self::isMultiSite()) { | |
| 149 | - $host['id'] = WebTotemOption::getSessionOption('host_id'); | |
| 150 | - $host['name'] = WebTotemOption::getSessionOption('host_name'); | |
| 123 | + // If the site is not added then try to add. | |
| 124 | + $payload = '{"variables":{"input":{"title":"' . $domain . '","hostname":"' . $domain . '","configs":{"scheme":"http","port":80,"wa":{},"dec":{},"ps":{}}}},"query":"mutation ($input: CreateSiteInput) { auth { sites { create(input: $input) { id hostname title } } } }"}'; | |
| 125 | + $add_site = self::sendRequest($payload, TRUE); | |
| 126 | + if (isset($add_site['errors'])) { | |
| 127 | + WebTotemOption::setNotification('error', __('Failed to add the site to the WebTotem platform.', 'wtotem')); | |
| 128 | + } | |
| 129 | + else { | |
| 130 | + if($host = $add_site['data']['auth']['sites']['create']) { | |
| 131 | + // If it added, save site ID. | |
| 132 | + WebTotemOption::setHost($host['title'], $host['id']); | |
| 133 | + return [ | |
| 134 | + 'id' => $host['id'], | |
| 135 | + 'name' => $host['title'], | |
| 136 | + ]; | |
| 137 | + } | |
| 138 | + } | |
| 139 | + return []; | |
| 140 | + } | |
| 151 | 141 | |
| 152 | - if ($host['id']) { | |
| 153 | - return $host; | |
| 154 | - } | |
| 155 | - } | |
| 142 | + /** | |
| 143 | + * Get all sites from API. | |
| 144 | + * | |
| 145 | + * @param string $cursor | |
| 146 | + * Mark for loading data. | |
| 147 | + * @param string $limit | |
| 148 | + * Limit of sites to loading. | |
| 149 | + * | |
| 150 | + * @return array | |
| 151 | + * Returns host data. | |
| 152 | + */ | |
| 153 | + public static function getSites($cursor = null, $limit = 15, $filter = false) { | |
| 154 | + $cursor = ($cursor == null) ? 'null' : '\"' . $cursor . '\"'; | |
| 155 | + if(!$filter) { | |
| 156 | + $filter = ($limit === 0) ? '' : 'pagination:{ first: ' . $limit . ', cursor: ' . $cursor . ' }'; | |
| 157 | + } | |
| 156 | 158 | |
| 157 | - $host = WebTotemOption::getHost(); | |
| 159 | + $payload = '{"query":"query getSites { auth { viewer { sites { ...sites } } } } fragment sites on SiteQueries { list(filter: { ' . $filter . ' }) { pageInfo{ hasNextPage endCursor } edges { node { id hostname title createdAt ssl { status } availability { status } reputation { status } ports { status } deface { status } domain { status } antivirus { status } firewall { status } maliciousScript { stack { name } } } } } }"}'; | |
| 160 | + $result = self::sendRequest($payload, true); | |
| 158 | 161 | |
| 159 | - if ($host['id']) { | |
| 160 | - return $host; | |
| 161 | - } | |
| 162 | + if (isset($result['data']['auth']['viewer']['sites']['list']['edges'])) { | |
| 163 | + return $result['data']['auth']['viewer']['sites']['list']; | |
| 164 | + } | |
| 162 | 165 | |
| 163 | -// if (self::isMultiSite()) { | |
| 164 | -// $sites = get_sites(); | |
| 165 | -// foreach ($sites as $site) { | |
| 166 | -// $domain = untrailingslashit($site->domain . $site->path); | |
| 167 | -// self::addSite($domain); | |
| 168 | -// } | |
| 169 | -// | |
| 170 | -// if (!$attempt) { | |
| 171 | -// return self::siteInfo(TRUE); | |
| 172 | -// } | |
| 173 | -// } else { | |
| 174 | -// $domain = WEBTOTEM_SITE_DOMAIN; | |
| 175 | -// return self::addSite($domain); | |
| 176 | -// } | |
| 177 | - $domain = WEBTOTEM_SITE_DOMAIN; | |
| 178 | - return self::addSite($domain); | |
| 166 | + return []; | |
| 167 | + } | |
| 179 | 168 | |
| 180 | -// return []; | |
| 181 | - } | |
| 169 | + /** | |
| 170 | + * Method to get the agents file names and AM file link. | |
| 171 | + * | |
| 172 | + * @param string $host_id | |
| 173 | + * Host id on WebTotem. | |
| 174 | + * | |
| 175 | + * @return array | |
| 176 | + * Returns agents files data. | |
| 177 | + */ | |
| 178 | + public static function getAgentsFiles($host_id) { | |
| 182 | 179 | |
| 183 | - /** | |
| 184 | - * Method for adding a site to the WebTotem platform. | |
| 185 | - * | |
| 186 | - * @param string $domain | |
| 187 | - * Domain to add. | |
| 188 | - * | |
| 189 | - * @return array | |
| 190 | - * Returns host data. | |
| 191 | - */ | |
| 192 | - public static function addSite($domain) | |
| 193 | - { | |
| 194 | - if (function_exists('idn_to_utf8')) { | |
| 195 | - $domain = idn_to_utf8($domain); | |
| 196 | - } | |
| 180 | + if(WebTotem::isMultiSite()){ | |
| 181 | + $all_hosts = WebTotemOption::getOption('all_hosts'); | |
| 182 | + $siteIdsArray = array_values($all_hosts); | |
| 183 | + $siteIds = addslashes(WebTotem::convertArrayToString($siteIdsArray)); | |
| 197 | 184 | |
| 198 | - // Checking if the site has been added to the WebTotem. | |
| 199 | - if(!$host = self::getHostID($domain)){ | |
| 200 | - $host = self::getHostID('www.' . $domain); | |
| 201 | - } | |
| 185 | + $payload = '{"query":"mutation { auth { am { installMultisite(mainSiteId: \"' . $host_id . '\", siteIds: [' . $siteIds . ']){ downloadLink, amFilename, wafFilename, avFilename } } } }"}'; | |
| 186 | + $response = self::sendRequest($payload, TRUE); | |
| 187 | + return $response['data']['auth']['am']['installMultisite']; | |
| 188 | + } | |
| 189 | + else { | |
| 190 | + $payload = '{"query":"mutation { auth { am { install(siteId: \"' . $host_id . '\"){ downloadLink, amFilename, wafFilename, avFilename } } } }"}'; | |
| 191 | + $response = self::sendRequest($payload, TRUE); | |
| 192 | + return $response['data']['auth']['am']['install']; | |
| 193 | + } | |
| 194 | + } | |
| 202 | 195 | |
| 203 | - if($host['id']){ | |
| 204 | - // Remember the binding: otherwise every page load asks the API | |
| 205 | - // for the host id again (and again for the www. variant). | |
| 206 | - WebTotemOption::setHost($host['hostname'], $host['id']); | |
| 196 | + /** | |
| 197 | + * Add secondary MultiSite host. | |
| 198 | + * | |
| 199 | + * @param $main_host_id | |
| 200 | + * Host id of the main site in MultiSite network. | |
| 201 | + * @param $new_sites | |
| 202 | + * An array with sites to add. | |
| 203 | + * | |
| 204 | + * @return void. | |
| 205 | + */ | |
| 206 | + public static function addMultiSiteNewSites($new_sites){ | |
| 207 | + $main_host = WebTotemOption::getMainHost(); | |
| 208 | + foreach ($new_sites as $site){ | |
| 209 | + $all_sites = self::getSites(null, 0); | |
| 210 | + $host = self::addSite($site, $all_sites); | |
| 211 | + if(key_exists('id', $host)){ | |
| 212 | + $payload = '{"query":"mutation { auth { am { addMultisiteHost(mainSiteId: \"' . $main_host['id'] . '\", siteId: \"' . $host['id'] . '\") } } }"}'; | |
| 213 | + self::sendRequest($payload, TRUE); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + } | |
| 207 | 217 | |
| 208 | - return [ | |
| 209 | - 'id' => $host['id'], | |
| 210 | - 'name' => $host['hostname'], | |
| 211 | - ]; | |
| 212 | - } | |
| 218 | + /** | |
| 219 | + * Remove secondary MultiSite host. | |
| 220 | + * | |
| 221 | + * @param $host_id | |
| 222 | + * Host id on WebTotem. | |
| 223 | + * | |
| 224 | + * @return bool | |
| 225 | + * Returns result removing host. | |
| 226 | + */ | |
| 227 | + public static function removeMultiSiteHost($host_id){ | |
| 228 | + $payload = '{"query":"mutation { auth { am { removeMultisiteHost(siteId: \"' . $host_id . '\") } } }"}'; | |
| 229 | + $response = self::sendRequest($payload, TRUE); | |
| 230 | + return $response['data']['auth']['am']['removeSecondaryMultisiteHost']; | |
| 231 | + } | |
| 213 | 232 | |
| 214 | - // If the site is not added then try to add. | |
| 215 | - $data = ['hosts' => [$domain]]; | |
| 216 | - $response = self::sendRequest('hosts', $data, 'POST', TRUE); | |
| 233 | + /** | |
| 234 | + * Method to get agents (AM, WAF, AV) statuses. | |
| 235 | + * | |
| 236 | + * @param string $host_id | |
| 237 | + * Host id on WebTotem. | |
| 238 | + * | |
| 239 | + * @return array | |
| 240 | + * Returns agents statuses data. | |
| 241 | + */ | |
| 242 | + public static function getAgentsStatusesFromAPI($host_id) { | |
| 243 | + $payload = '{"query":"query ($id: ID!) { auth { viewer { sites { one(id: $id) { agentManager { statuses { am { status } av { status } waf { status } } } } } } } }", "variables":{"id":"' . $host_id . '"}}'; | |
| 244 | + $response = self::sendRequest($payload, TRUE); | |
| 217 | 245 | |
| 218 | - if (isset($response['message'])) { | |
| 219 | - WebTotemOption::setNotification('error', __('Failed to add the site to the WebTotem platform.', 'wtotem')); | |
| 220 | - } else { | |
| 221 | - if ($response['data']['added']) { | |
| 222 | - // If it added, save site ID. | |
| 223 | - $host = self::getHostID($domain); | |
| 224 | - WebTotemOption::setHost($domain, $host['id']); | |
| 225 | - return [ | |
| 226 | - 'id' => $host['id'], | |
| 227 | - 'name' => $host['hostname'], | |
| 228 | - ]; | |
| 229 | - } | |
| 230 | - } | |
| 231 | - return []; | |
| 246 | + if (isset($response['data']['auth']['viewer']['sites']['one']['agentManager']['statuses'])) { | |
| 247 | + return $response['data']['auth']['viewer']['sites']['one']['agentManager']['statuses']; | |
| 232 | 248 | } |
| 233 | 249 | |
| 234 | - /** | |
| 235 | - * Get all sites from API. | |
| 236 | - * | |
| 237 | - * @param string $page_num | |
| 238 | - * Mark for loading data. | |
| 239 | - * @param string $limit | |
| 240 | - * Limit of sites to loading. | |
| 241 | - * | |
| 242 | - * @return array | |
| 243 | - * Returns host data. | |
| 244 | - */ | |
| 245 | - public static function getSites($page_num = 1, $page_size = 15, $status = 'active', $hostname = '') | |
| 246 | - { | |
| 247 | - $query = [ | |
| 248 | - 'page_num' => $page_num, | |
| 249 | - 'page_size' => $page_size, | |
| 250 | - 'status' => $status, | |
| 251 | - ]; | |
| 250 | + return []; | |
| 251 | + } | |
| 252 | 252 | |
| 253 | - if ($hostname !== '') { | |
| 254 | - $query['hostname'] = $hostname; | |
| 255 | - } | |
| 253 | + /** | |
| 254 | + * Method to get user time zone. | |
| 255 | + * | |
| 256 | + * @return string|bool | |
| 257 | + * Returns time zone data. | |
| 258 | + */ | |
| 259 | + public static function getTimeZone() { | |
| 260 | + $payload = '{"query":"query { auth { viewer{ timezone } } } "}'; | |
| 261 | + $response = self::sendRequest($payload, TRUE); | |
| 256 | 262 | |
| 257 | - $result = self::sendRequest('hosts', $query, 'GET', TRUE); | |
| 258 | - | |
| 259 | - // The API answers { "data": { "hosts": [...], "host_limit": n, "can_defrost": bool } }. | |
| 260 | - $payload = self::payload($result); | |
| 261 | - | |
| 262 | - return isset($payload['hosts']) && is_array($payload['hosts']) ? $payload['hosts'] : []; | |
| 263 | + if (isset($response['data']['auth']['viewer']['timezone'])) { | |
| 264 | + return $response['data']['auth']['viewer']['timezone']; | |
| 263 | 265 | } |
| 266 | + return FALSE; | |
| 267 | + } | |
| 264 | 268 | |
| 265 | - /** | |
| 266 | - * Reads the payload out of the API response envelope. | |
| 267 | - * | |
| 268 | - * Most endpoints answer { "data": ... }; a few older builds used "Data". | |
| 269 | - * | |
| 270 | - * @param mixed $response | |
| 271 | - * Decoded API response. | |
| 272 | - * | |
| 273 | - * @return array | |
| 274 | - * Payload, or an empty array when the response carried none. | |
| 275 | - */ | |
| 276 | - protected static function payload($response) | |
| 277 | - { | |
| 278 | - if (!is_array($response)) { | |
| 279 | - return []; | |
| 280 | - } | |
| 269 | + /** | |
| 270 | + * Method for get all the site security data. | |
| 271 | + * | |
| 272 | + * @param string $host_id | |
| 273 | + * Host id on WebTotem. | |
| 274 | + * @param int|array $days | |
| 275 | + * For what period data is needed. | |
| 276 | + * | |
| 277 | + * @return array | |
| 278 | + * Returns all data. | |
| 279 | + */ | |
| 280 | + public static function getAllData($host_id, $days = 7) { | |
| 281 | + $language = WebTotem::getLanguage(); | |
| 282 | + $period = WebTotem::getPeriod($days); | |
| 281 | 283 | |
| 282 | - foreach (['data', 'Data'] as $key) { | |
| 283 | - if (isset($response[$key]) && is_array($response[$key])) { | |
| 284 | - return $response[$key]; | |
| 285 | - } | |
| 286 | - } | |
| 284 | + $payload = '{"query":"query($id: ID!, $dateRange: DateRangeInput!, $language: Language!, $dateRangeWeek: DateRangeInput!, $wafLogFilter: WafLogFilter!) { auth { viewer { sites { one(id: $id) { ports { status ip tcp ignorePorts 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 scanned infected error } lastTest { time } isFirstCheck } } } } } }","variables":{"id":"' . $host_id . '","dateRange":{"to":' . $period['to'] . ',"from":' . $period['from'] . '}, "dateRangeWeek":{"to":' . $period['to'] . ',"from":' . $period['from'] . '}, "wafLogFilter": {"dateRange":{"to":' . $period['to'] . ',"from":' . $period['from'] . '},"order":{"direction":"DESC","field":"time"},"pagination":{"first": 10,"cursor":null}}, "language":"' . $language . '"}}'; | |
| 285 | + $response = self::sendRequest($payload, TRUE); | |
| 287 | 286 | |
| 288 | - return []; | |
| 287 | + if (isset($response['data']['auth']['viewer']['sites']['one'])) { | |
| 288 | + return $response['data']['auth']['viewer']['sites']['one']; | |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | - /** | |
| 292 | - * Requests a single-use ticket for the browser WebSocket connection. | |
| 293 | - * | |
| 294 | - * The ticket replaces the access token that used to be printed into the | |
| 295 | - * page: it is short-lived, may be used once, and is bound to the browser | |
| 296 | - * Origin it was issued for. | |
| 297 | - * | |
| 298 | - * @param string $origin | |
| 299 | - * Browser origin the ticket is issued for, e.g. https://example.com. | |
| 300 | - * | |
| 301 | - * @return string | |
| 302 | - * The ticket, or an empty string when it could not be obtained. | |
| 303 | - */ | |
| 304 | - public static function getWSTicket($origin = '') | |
| 305 | - { | |
| 306 | - $query = []; | |
| 307 | - if (is_string($origin) && $origin !== '') { | |
| 308 | - // add_query_arg() does not encode values; the origin carries "://". | |
| 309 | - $query['origin'] = rawurlencode($origin); | |
| 310 | - } | |
| 291 | + return []; | |
| 292 | + } | |
| 311 | 293 | |
| 312 | - $response = self::sendRequest('ws/ticket', $query, 'GET', TRUE, FALSE, TRUE); | |
| 294 | + /** | |
| 295 | + * Method to get firewall data. | |
| 296 | + * | |
| 297 | + * @param string $host_id | |
| 298 | + * Host id on WebTotem. | |
| 299 | + * @param int $limit | |
| 300 | + * Limit on the number of records. | |
| 301 | + * @param string $cursor | |
| 302 | + * Mark for loading data. | |
| 303 | + * @param int|array $days | |
| 304 | + * For what period data is needed. | |
| 305 | + * | |
| 306 | + * @return array | |
| 307 | + * Returns firewall data. | |
| 308 | + */ | |
| 309 | + public static function getFirewall($host_id, $limit = 20, $cursor = NULL, $days = 365) { | |
| 310 | + $period = WebTotem::getPeriod($days); | |
| 311 | + $cursor = ($cursor == NULL) ? 'null' : '"' . $cursor . '"'; | |
| 313 | 312 | |
| 314 | - if (is_array($response) && !empty($response['ticket'])) { | |
| 315 | - return (string) $response['ticket']; | |
| 316 | - } | |
| 313 | + $payload = '{"query":"query($id: ID!, $wafLogFilter: WafLogFilter!, $dateRange: DateRangeInput!) { auth { viewer { sites { one(id: $id) { firewall { lastTest { time } status map(dateRange: $dateRange) { attacks, country, location { country { nameEn } } } chart(dateRange: $dateRange) { time attacks blocked } ...FirewallLogFragment } agentManager { createdAt } } } } } } fragment FirewallLogFragment on Waf { logs(wafLogFilter: $wafLogFilter) { edges { cursor node { type blocked payload ip location { country { nameEn } } time request status country category } } pageInfo { endCursor hasNextPage } } }", "variables":{"dateRange":{"to":' . $period['to'] . ',"from":' . $period['from'] . '},"id":"' . $host_id . '","wafLogFilter":{"dateRange":{"to":' . $period['to'] . ',"from":' . $period['from'] . '},"order":{"direction":"DESC","field":"time"},"pagination":{"first":' . $limit . ',"cursor":' . $cursor . '}}} }'; | |
| 314 | + $response = self::sendRequest($payload, TRUE); | |
| 317 | 315 | |
| 318 | - // Tolerate a wrapped answer in case the endpoint starts using the envelope. | |
| 319 | - $payload = self::payload($response); | |
| 316 | + if (isset($response['data']['auth']['viewer']['sites']['one'])) { | |
| 317 | + return $response['data']['auth']['viewer']['sites']['one']; | |
| 318 | + } | |
| 320 | 319 | |
| 321 | - return !empty($payload['ticket']) ? (string) $payload['ticket'] : ''; | |
| 322 | - } | |
| 320 | + return []; | |
| 321 | + } | |
| 323 | 322 | |
| 324 | - /** | |
| 325 | - * Check the site's presence in the list on the API side. | |
| 326 | - * | |
| 327 | - * @param string $site | |
| 328 | - * The domain we want to check. | |
| 329 | - * | |
| 330 | - * @return array | |
| 331 | - * Returns host data. | |
| 332 | - */ | |
| 333 | - public static function getHostID($site) | |
| 334 | - { | |
| 335 | - $result = self::sendRequest('hosts/id', ['hostname' => $site], 'GET', TRUE); | |
| 323 | + /** | |
| 324 | + * Method to get firewall chart data. | |
| 325 | + * | |
| 326 | + * @param string $host_id | |
| 327 | + * Host id on WebTotem. | |
| 328 | + * @param int $days | |
| 329 | + * For what period data is needed. | |
| 330 | + * | |
| 331 | + * @return array | |
| 332 | + * Returns firewall chart data. | |
| 333 | + */ | |
| 334 | + public static function getFirewallChart($host_id, $days = 7) { | |
| 335 | + $period = WebTotem::getPeriod($days); | |
| 336 | 336 | |
| 337 | - if (isset($result['data'])) { | |
| 338 | - WebTotemOption::setOptions(['config_id' => $result['data']['config_id']]); | |
| 339 | - return ['id' => $result['data']['host_id'], 'hostname' => $site]; | |
| 340 | - } | |
| 337 | + $payload = '{ "query":"query($id: ID!, $dateRange: DateRangeInput!) { auth { viewer { sites { one(id: $id) { firewall { lastTest { time } status map(dateRange: $dateRange) { attacks, country, location { country { nameEn } } } chart(dateRange: $dateRange) { time attacks blocked } } } } } } }", "operationName":null,"variables":{"id":"' . $host_id . '","dateRange":{"to":' . $period['to'] . ',"from":' . $period['from'] . '} } }'; | |
| 338 | + $response = self::sendRequest($payload, TRUE); | |
| 341 | 339 | |
| 342 | - return ['id' => '', 'hostname' => '']; | |
| 343 | - } | |
| 340 | + if (isset($response['data']['auth']['viewer']['sites']['one']['firewall'])) { | |
| 341 | + return $response['data']['auth']['viewer']['sites']['one']['firewall']; | |
| 342 | + } | |
| 344 | 343 | |
| 344 | + return []; | |
| 345 | + } | |
| 345 | 346 | |
| 346 | - /** | |
| 347 | - * Method to get the agents file names and AM file link. | |
| 348 | - * | |
| 349 | - * @param string $host_id | |
| 350 | - * Host id on WebTotem. | |
| 351 | - * | |
| 352 | - * @return array | |
| 353 | - * Returns agents files data. | |
| 354 | - */ | |
| 355 | - public static function getAgentsFiles($host_id) | |
| 356 | - { | |
| 347 | + /** | |
| 348 | + * Method to set firewall settings. | |
| 349 | + * | |
| 350 | + * @param string $host_id | |
| 351 | + * Host id on WebTotem. | |
| 352 | + * @param array $settings | |
| 353 | + * User-specified settings. | |
| 354 | + * | |
| 355 | + * @return array | |
| 356 | + * Returns information whether the request was successful. | |
| 357 | + */ | |
| 358 | + public static function setFirewallSettings($host_id, array $settings) { | |
| 359 | + $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 } } } } }"}'; | |
| 360 | + return self::sendRequest($payload, TRUE); | |
| 361 | + } | |
| 357 | 362 | |
| 358 | -// if (WebTotem::isMultiSite()) { | |
| 359 | -// $all_hosts = WebTotemOption::getOption('all_hosts'); | |
| 360 | -// $all_hosts = $all_hosts ? json_decode($all_hosts, true) : []; | |
| 361 | -// | |
| 362 | -// $siteIdsArray = $all_hosts ? array_values($all_hosts) : []; | |
| 363 | -// $siteIds = $siteIdsArray ? addslashes(WebTotem::convertArrayToString($siteIdsArray)) : ''; | |
| 364 | -// | |
| 365 | -// $payload = '{"query":"mutation { auth { am { installMultisite(mainSiteId: \"' . $host_id . '\", siteIds: [' . $siteIds . ']){ downloadLink, amFilename, wafFilename, avFilename } } } }"}'; | |
| 366 | -// $response = self::sendRequest($payload, TRUE); | |
| 367 | -// | |
| 368 | -// if (isset($response['data']['auth']['am']['installMultisite'])) { | |
| 369 | -// return $response['data']['auth']['am']['installMultisite']; | |
| 370 | -// } | |
| 371 | -// } else { | |
| 372 | - $response = self::sendRequest('/agents/' . $host_id . '/install', [], 'POST', TRUE); | |
| 363 | + /** | |
| 364 | + * Method to get antivirus data. | |
| 365 | + * | |
| 366 | + * @param array $params | |
| 367 | + * Parameters for filtering data. | |
| 368 | + * | |
| 369 | + * @return array | |
| 370 | + * Returns antivirus data. | |
| 371 | + */ | |
| 372 | + public static function getAntivirus(array $params) { | |
| 373 | + $cursor = ($params['cursor']) ? '"' . $params['cursor'] . '"' : 'null'; | |
| 374 | + $event = ($params['event']) ? '"' . $params['event'] . '"' : '"new"'; | |
| 375 | + $permissions = ($params['permissions']) ? ' "permissionsChanged":true, ' : ''; | |
| 376 | + $period = WebTotem::getPeriod($params['days']); | |
| 373 | 377 | |
| 374 | - if (isset($response['data'])) { | |
| 375 | - return $response['data']; | |
| 376 | - } | |
| 377 | -// } | |
| 378 | - return []; | |
| 378 | + $payload = '{"operationName":null,"variables":{"id":"' . $params['host_id'] . '","avLogFilter":{' . $permissions . '"event":' . $event . ', "dateRange":{"to":' . $period['to'] . ',"from":' . $period['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 { quarantine{ id path date } status log(avLogFilter: $avLogFilter) { edges { node { filePath event signatures time permissions permissionsChanged } } pageInfo { endCursor hasNextPage __typename } } lastTest { time } stats { changed deleted scanned infected } } } } } } }"}'; | |
| 379 | + $response = self::sendRequest($payload, TRUE); | |
| 380 | + | |
| 381 | + if (isset($response['data']['auth']['viewer']['sites']['one']['antivirus'])) { | |
| 382 | + return $response['data']['auth']['viewer']['sites']['one']['antivirus']; | |
| 379 | 383 | } |
| 384 | + return []; | |
| 385 | + } | |
| 380 | 386 | |
| 381 | - /** | |
| 382 | - * Add secondary MultiSite host. | |
| 383 | - * | |
| 384 | - * @param $new_sites | |
| 385 | - * An array with sites to add. | |
| 386 | - * | |
| 387 | - * @return void. | |
| 388 | - */ | |
| 389 | - public static function addMultiSiteNewSites($new_sites) | |
| 390 | - { | |
| 387 | + /** | |
| 388 | + * Method to get antivirus last test. | |
| 389 | + * | |
| 390 | + * @param string $host_id | |
| 391 | + * Host id on WebTotem. | |
| 392 | + * | |
| 393 | + * @return array | |
| 394 | + * Returns antivirus last test data. | |
| 395 | + */ | |
| 396 | + public static function getAntivirusLastTest($host_id) { | |
| 391 | 397 | |
| 398 | + $payload = '{"variables":{"id":"' . $host_id . '"},"query":"query ($id: ID!) { auth { viewer { sites { one(id: $id) { antivirus { status lastTest { time } } } } } } }"}'; | |
| 399 | + $response = self::sendRequest($payload, TRUE); | |
| 400 | + | |
| 401 | + if (isset($response['data']['auth']['viewer']['sites']['one']['antivirus'])) { | |
| 402 | + return $response['data']['auth']['viewer']['sites']['one']['antivirus']; | |
| 392 | 403 | } |
| 404 | + return []; | |
| 405 | + } | |
| 393 | 406 | |
| 394 | - /** | |
| 395 | - * Get the date of creation of the site. | |
| 396 | - * | |
| 397 | - * @param string $site | |
| 398 | - * The domain we want to check. | |
| 399 | - * | |
| 400 | - * @return string|bool | |
| 401 | - * Returns host data. | |
| 402 | - */ | |
| 403 | - public static function getGetSiteAddedDate($site) | |
| 404 | - { | |
| 405 | - if (!$site) { | |
| 406 | - return FALSE; | |
| 407 | - } | |
| 407 | + /** | |
| 408 | + * Method to force check services. | |
| 409 | + * | |
| 410 | + * @param string $host_id | |
| 411 | + * Host id on WebTotem. | |
| 412 | + * @param string $service | |
| 413 | + * Service that needs to be checked. | |
| 414 | + * | |
| 415 | + * @return array | |
| 416 | + * Returns information whether the request was successful. | |
| 417 | + */ | |
| 418 | + public static function forceCheck($host_id, $service) { | |
| 419 | + $payload = '{"variables":{"id":"' . $host_id . '","service":"' . $service . '"},"query":"mutation ($id: ID!, $service: ForceCheckService!) { auth { sites { forceCheck(siteId: $id, service: $service) } } }"} '; | |
| 420 | + return self::sendRequest($payload, TRUE); | |
| 421 | + } | |
| 408 | 422 | |
| 409 | - $hosts = self::getSites(1, 1, 'active', $site); | |
| 423 | + /** | |
| 424 | + * Method to export antivirus report. | |
| 425 | + * | |
| 426 | + * @param string $host_id | |
| 427 | + * Host id on WebTotem. | |
| 428 | + * @param int|array $days | |
| 429 | + * For what period data is needed. | |
| 430 | + * | |
| 431 | + * @return array | |
| 432 | + * Returns information whether the request was successful. | |
| 433 | + */ | |
| 434 | + public static function avExport($host_id, $days = 30) { | |
| 435 | + $period = WebTotem::getPeriod($days); | |
| 436 | + $payload = '{"variables":{ "input":{"siteId":"' . $host_id . '", "dateRange":{"to":' . $period['to'] . ',"from":' . $period['from'] . '} }},"query":"mutation ($input: AvLogExportInput!) { auth { sites { av { export(input: $input) } } } }"} '; | |
| 437 | + return self::sendRequest($payload, TRUE); | |
| 438 | + } | |
| 410 | 439 | |
| 411 | - foreach ($hosts as $host) { | |
| 412 | - if (!empty($host['created_at'])) { | |
| 413 | - return $host['created_at']; | |
| 414 | - } | |
| 415 | - } | |
| 440 | + /** | |
| 441 | + * Method to get quarantine data. | |
| 442 | + * | |
| 443 | + * @param string $host_id | |
| 444 | + * Host id on WebTotem. | |
| 445 | + * | |
| 446 | + * @return array | |
| 447 | + * Returns quarantine data. | |
| 448 | + */ | |
| 449 | + public static function getQuarantineList($host_id) { | |
| 450 | + $payload = '{"query":"query{ auth{ viewer{ sites{ one(id:\"' . $host_id . '\"){ antivirus{ quarantine{ id path date } } } } } } } "}'; | |
| 451 | + $response = self::sendRequest($payload, TRUE); | |
| 416 | 452 | |
| 417 | - return FALSE; | |
| 453 | + if (isset($response['data']['auth']['viewer']['sites']['one']['antivirus']['quarantine'])) { | |
| 454 | + return $response['data']['auth']['viewer']['sites']['one']['antivirus']['quarantine']; | |
| 418 | 455 | } |
| 456 | + return []; | |
| 457 | + } | |
| 419 | 458 | |
| 420 | - /** | |
| 421 | - * Remove secondary MultiSite host. | |
| 422 | - * | |
| 423 | - * @param $host_id | |
| 424 | - * Host id on WebTotem. | |
| 425 | - * | |
| 426 | - * @return bool | |
| 427 | - * Returns result removing host. | |
| 428 | - */ | |
| 429 | - public static function removeMultiSiteHost($host_id) | |
| 430 | - { | |
| 459 | + /** | |
| 460 | + * Method to move file to quarantine. | |
| 461 | + * | |
| 462 | + * @param string $host_id | |
| 463 | + * Host id on WebTotem. | |
| 464 | + * @param string $path | |
| 465 | + * Path to the file. | |
| 466 | + * | |
| 467 | + * @return array | |
| 468 | + * Returns information whether the request was successful. | |
| 469 | + */ | |
| 470 | + public static function moveToQuarantine($host_id, $path) { | |
| 471 | + $payload = '{"query":"mutation{ auth{ sites{ av{ moveToQuarantine(input:{ siteId:\"' . $host_id . '\", path:\"' . $path . '\" }) } } } } "}'; | |
| 472 | + return self::sendRequest($payload, TRUE); | |
| 473 | + } | |
| 431 | 474 | |
| 432 | - return false; | |
| 433 | - } | |
| 475 | + /** | |
| 476 | + * Method to move file from quarantine. | |
| 477 | + * | |
| 478 | + * @param string $id | |
| 479 | + * Id assigned to the file. | |
| 480 | + * | |
| 481 | + * @return array | |
| 482 | + * Returns information whether the request was successful. | |
| 483 | + */ | |
| 484 | + public static function moveFromQuarantine($id) { | |
| 485 | + $payload = '{"query":"mutation{ auth{ sites{ av{ moveFromQuarantine(id: \"' . $id . '\") } } } } "}'; | |
| 486 | + return self::sendRequest($payload, TRUE); | |
| 487 | + } | |
| 434 | 488 | |
| 435 | - /** | |
| 436 | - * Method to get agents (AM, WAF, AV) statuses. | |
| 437 | - * | |
| 438 | - * @return array | |
| 439 | - * Returns agents statuses data. | |
| 440 | - */ | |
| 441 | - public static function getAgentsStatusesFromAPI() | |
| 442 | - { | |
| 443 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 444 | - $response = self::sendRequest('/agents/' . $config_id . '/status', [], 'GET', TRUE); | |
| 489 | + /** | |
| 490 | + * Method to get server status data. | |
| 491 | + * | |
| 492 | + * @param string $host_id | |
| 493 | + * Host id on WebTotem. | |
| 494 | + * @param int|array $days | |
| 495 | + * For what period data is needed. | |
| 496 | + * | |
| 497 | + * @return array | |
| 498 | + * Returns server status data. | |
| 499 | + */ | |
| 500 | + public static function getServerStatusData($host_id, $days = 7) { | |
| 501 | + $period = WebTotem::getPeriod($days); | |
| 502 | + $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":' . $period['to'] . ',"from":' . $period['from'] . '} } }'; | |
| 445 | 503 | |
| 446 | - if (isset($response['data'])) { | |
| 447 | - return $response['data']; | |
| 448 | - } | |
| 504 | + $response = self::sendRequest($payload, TRUE); | |
| 449 | 505 | |
| 450 | - return []; | |
| 506 | + if (isset($response['data']['auth']['viewer']['sites']['one']['serverStatus'])) { | |
| 507 | + return $response['data']['auth']['viewer']['sites']['one']['serverStatus']; | |
| 451 | 508 | } |
| 452 | 509 | |
| 453 | - /** | |
| 454 | - * Method for get monitoring data. | |
| 455 | - * | |
| 456 | - * @param string $host_id | |
| 457 | - * Host id on WebTotem. | |
| 458 | - * @param int|array $days | |
| 459 | - * For what period data is needed. | |
| 460 | - * | |
| 461 | - * @return array | |
| 462 | - * Returns all data. | |
| 463 | - */ | |
| 464 | - public static function getMonitoringData($host_id) | |
| 465 | - { | |
| 466 | - $response = self::sendRequest('/dashboard/monitoring/' . $host_id . '/results', [], 'GET', TRUE); | |
| 510 | + return []; | |
| 511 | + } | |
| 467 | 512 | |
| 468 | - if (isset($response['data'])) { | |
| 469 | - return $response['data']; | |
| 470 | - } | |
| 513 | + /** | |
| 514 | + * Method to remove port from ignore list. | |
| 515 | + * | |
| 516 | + * @param string $host_id | |
| 517 | + * Host id on WebTotem. | |
| 518 | + * @param string $port | |
| 519 | + * User specified port. | |
| 520 | + * | |
| 521 | + * @return array | |
| 522 | + * Returns information whether the request was successful. | |
| 523 | + */ | |
| 524 | + public static function removeIgnorePort($host_id, $port) { | |
| 525 | + $payload = '{"variables":{ "input": { "siteId": "' . $host_id . '", "port":' . $port . '} },"query":"mutation($input: IgnorePortInput!) { auth { sites { ps { removeIgnorePort(input: $input) } } } }"} '; | |
| 526 | + return self::sendRequest($payload, TRUE); | |
| 527 | + } | |
| 471 | 528 | |
| 472 | - return []; | |
| 473 | - } | |
| 529 | + /** | |
| 530 | + * Method to add port to ignore list. | |
| 531 | + * | |
| 532 | + * @param string $host_id | |
| 533 | + * Host id on WebTotem. | |
| 534 | + * @param string $port | |
| 535 | + * User specified port. | |
| 536 | + * | |
| 537 | + * @return array | |
| 538 | + * Returns information whether the request was successful. | |
| 539 | + */ | |
| 540 | + public static function addIgnorePort($host_id, $port) { | |
| 541 | + $payload = '{"variables":{ "input": { "siteId": "' . $host_id . '", "port":' . (int) $port . '} },"query":"mutation($input: IgnorePortInput!) { auth { sites { ps { addIgnorePort(input: $input) } } } }"} '; | |
| 542 | + return self::sendRequest($payload, TRUE); | |
| 543 | + } | |
| 474 | 544 | |
| 475 | - /** | |
| 476 | - * Method to get firewall data. | |
| 477 | - * | |
| 478 | - * @param int $limit | |
| 479 | - * Limit on the number of records. | |
| 480 | - * @param string $page | |
| 481 | - * Page for loading data. | |
| 482 | - * @param int|array $days | |
| 483 | - * For what period data is needed. | |
| 484 | - * | |
| 485 | - * @return array | |
| 486 | - * Returns firewall data. | |
| 487 | - */ | |
| 488 | - public static function getFirewall($limit = 20, $page = 1, $days = 365) | |
| 489 | - { | |
| 490 | - $period = WebTotem::getPeriod($days); | |
| 545 | + /** | |
| 546 | + * Method to get all ports list. | |
| 547 | + * | |
| 548 | + * @param string $host_id | |
| 549 | + * Host id on WebTotem. | |
| 550 | + * | |
| 551 | + * @return array | |
| 552 | + * Returns ports data. | |
| 553 | + */ | |
| 554 | + public static function getAllPortsList($host_id) { | |
| 555 | + $payload = '{"query":"query($id: ID!) { auth { viewer { sites { one(id: $id) { ports { status ip tcp ignorePorts lastTest { time } } } } } } } ","variables":{"id":"' . $host_id . '"}}'; | |
| 491 | 556 | |
| 492 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 493 | - $response = self::sendRequest('/dashboard/firewall/' . $config_id . '/logs', [ | |
| 494 | - 'page_num' => $page, | |
| 495 | - 'page_size' => $limit, | |
| 496 | - 'from' => $period['from'], | |
| 497 | - 'to' => $period['to'] | |
| 498 | - ], 'GET', TRUE); | |
| 557 | + $response = self::sendRequest($payload, TRUE); | |
| 499 | 558 | |
| 559 | + if (isset($response['data']['auth']['viewer']['sites']['one']['ports'])) { | |
| 560 | + return $response['data']['auth']['viewer']['sites']['one']['ports']; | |
| 561 | + } | |
| 500 | 562 | |
| 501 | - if (isset($response['data'])) { | |
| 502 | - return $response['data']; | |
| 503 | - } | |
| 563 | + return []; | |
| 564 | + } | |
| 504 | 565 | |
| 505 | - return []; | |
| 566 | + /** | |
| 567 | + * Method to get all reports. | |
| 568 | + * | |
| 569 | + * @param string $host_id | |
| 570 | + * Host id on WebTotem. | |
| 571 | + * @param int $limit | |
| 572 | + * Limit on the number of records. | |
| 573 | + * @param string $cursor | |
| 574 | + * Mark for loading data. | |
| 575 | + * | |
| 576 | + * @return array | |
| 577 | + * Returns reports data. | |
| 578 | + */ | |
| 579 | + public static function getAllReports($host_id, $limit = 10, $cursor = NULL) { | |
| 580 | + $cursor = ($cursor == NULL) ? 'null' : '"' . $cursor . '"'; | |
| 581 | + $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 } } } } } }"}'; | |
| 582 | + $response = self::sendRequest($payload, TRUE); | |
| 583 | + | |
| 584 | + if (isset($response['data']['auth']['viewer']['reports']['list']['edges'])) { | |
| 585 | + return $response['data']['auth']['viewer']['reports']['list']; | |
| 506 | 586 | } |
| 507 | 587 | |
| 508 | - /** | |
| 509 | - * Method to get firewall chart data. | |
| 510 | - * | |
| 511 | - * @param int $days | |
| 512 | - * For what period data is needed. | |
| 513 | - * | |
| 514 | - * @return array | |
| 515 | - * Returns firewall chart data. | |
| 516 | - */ | |
| 517 | - public static function getFirewallStatistics($days = 7) | |
| 518 | - { | |
| 519 | - $period = WebTotem::getPeriod($days); | |
| 588 | + return []; | |
| 589 | + } | |
| 520 | 590 | |
| 521 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 522 | - $response = self::sendRequest('/dashboard/firewall/' . $config_id . '/statistics', [ | |
| 523 | - 'from' => $period['from'], | |
| 524 | - 'to' => $period['to'] | |
| 525 | - ], 'GET', TRUE); | |
| 591 | + /** | |
| 592 | + * Method to generate report. | |
| 593 | + * | |
| 594 | + * @param string $host_id | |
| 595 | + * Host id on WebTotem. | |
| 596 | + * @param int|array $days | |
| 597 | + * For what period data is needed. | |
| 598 | + * @param array $services | |
| 599 | + * User-specified module settings. | |
| 600 | + * | |
| 601 | + * @return string|bool | |
| 602 | + * Returns report download link. | |
| 603 | + */ | |
| 604 | + public static function generateReport($host_id, $days, array $services) { | |
| 605 | + $period = WebTotem::getPeriod($days); | |
| 606 | + $language = WebTotem::getLanguage(); | |
| 526 | 607 | |
| 608 | + $payload = '{"query":"query ($input: GenerateReportInput) { auth { viewer { reports { generate(input: $input) } } } }", "variables":{ "input": { "siteId": "' . $host_id . '", "from": ' . $period['from'] . ', "to": ' . $period['to'] . ', "wa": ' . $services['wa'] . ', "dc": ' . $services['dc'] . ', "ps": ' . $services['ps'] . ', "rc": ' . $services['rc'] . ', "sc": ' . $services['sc'] . ', "av": ' . $services['av'] . ', "waf": ' . $services['waf'] . ', "language": "' . $language . '" } } }'; | |
| 609 | + $response = self::sendRequest($payload, TRUE); | |
| 527 | 610 | |
| 528 | - if (isset($response['data'])) { | |
| 529 | - return $response['data']; | |
| 530 | - } | |
| 531 | - | |
| 532 | - return []; | |
| 611 | + if (isset($response['data']['auth']['viewer']['reports']['generate'])) { | |
| 612 | + return $response['data']['auth']['viewer']['reports']['generate']; | |
| 533 | 613 | } |
| 534 | 614 | |
| 535 | - /** | |
| 536 | - * Method to get firewall settings. | |
| 537 | - * | |
| 538 | - * @return array | |
| 539 | - * Returns information whether the request was successful. | |
| 540 | - */ | |
| 541 | - public static function getFirewallSettings() | |
| 542 | - { | |
| 543 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 544 | - $response = self::sendRequest('/dashboard/firewall/' . $config_id . '/configs', [], 'GET', TRUE); | |
| 545 | - if (isset($response['data'])) { | |
| 546 | - return $response['data']; | |
| 547 | - } | |
| 615 | + return FALSE; | |
| 616 | + } | |
| 548 | 617 | |
| 549 | - return []; | |
| 550 | - } | |
| 618 | + /** | |
| 619 | + * Method to download report. | |
| 620 | + * | |
| 621 | + * @param string $id | |
| 622 | + * Assigned to the report. | |
| 623 | + * | |
| 624 | + * @return string|bool | |
| 625 | + * Returns report download link. | |
| 626 | + */ | |
| 627 | + public static function downloadReport($id) { | |
| 628 | + $payload = '{"query": "query { auth { viewer { reports { download(id: \"' . $id . '\") } } } }"}'; | |
| 629 | + $response = self::sendRequest($payload, TRUE); | |
| 551 | 630 | |
| 552 | - /** | |
| 553 | - * Method to set firewall settings. | |
| 554 | - * | |
| 555 | - * @param array $settings | |
| 556 | - * User-specified settings. | |
| 557 | - * | |
| 558 | - * @return array | |
| 559 | - * Returns information whether the request was successful. | |
| 560 | - */ | |
| 561 | - public static function setFirewallSettings(array $settings) | |
| 562 | - { | |
| 563 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 564 | - return self::sendRequest('/dashboard/firewall/' . $config_id . '/configs', $settings, 'PATCH', TRUE); | |
| 631 | + if (isset($response['data']['auth']['viewer']['reports']['download'])) { | |
| 632 | + return $response['data']['auth']['viewer']['reports']['download']; | |
| 565 | 633 | } |
| 566 | 634 | |
| 635 | + return FALSE; | |
| 636 | + } | |
| 567 | 637 | |
| 568 | - /** | |
| 569 | - * Method to get antivirus history data. | |
| 570 | - * | |
| 571 | - * @param int $page_num | |
| 572 | - * Page number. | |
| 573 | - * @param int $page_size | |
| 574 | - * Number of entries per page. | |
| 575 | - * | |
| 576 | - * @return array | |
| 577 | - * Returns antivirus history data. | |
| 578 | - */ | |
| 579 | - public static function getAntivirusHistory($page_num = 1, $page_size = 10) | |
| 580 | - { | |
| 581 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 582 | - $response = self::sendRequest('/dashboard/antivirus/' . $config_id . '/history', [ | |
| 583 | - 'page_num' => $page_num, | |
| 584 | - 'page_size' => $page_size | |
| 585 | - ], 'GET', TRUE); | |
| 638 | + /** | |
| 639 | + * Method to get configs data. | |
| 640 | + * | |
| 641 | + * @param string $host_id | |
| 642 | + * Host id on WebTotem. | |
| 643 | + * | |
| 644 | + * @return array|bool | |
| 645 | + * Returns configs data. | |
| 646 | + */ | |
| 647 | + public static function getConfigs($host_id) { | |
| 648 | + $payload = '{"query":"query{ auth{ viewer{ sites{ one(id:\"' . $host_id . '\"){ configs{ ... on WaConfig { id service isActive notifications } ... on WafConfig { id service isActive notifications } ... on AvConfig { id service isActive notifications } ... on DcConfig { id service isActive notifications } ... on DecConfig { id service isActive } ... on RcConfig { id service isActive notifications} ... on CmsConfig { id service isActive } ... on PsConfig { id service isActive notifications } ... on SsConfig { id service isActive } ... on ScConfig { id service isActive } } } } } } } "}'; | |
| 649 | + $response = self::sendRequest($payload, TRUE); | |
| 586 | 650 | |
| 587 | - if (isset($response['data'])) { | |
| 588 | - return $response['data']; | |
| 589 | - } | |
| 590 | - return []; | |
| 651 | + if (isset($response['data']['auth']['viewer']['sites']['one']['configs'])) { | |
| 652 | + return $response['data']['auth']['viewer']['sites']['one']['configs']; | |
| 591 | 653 | } |
| 592 | 654 | |
| 593 | - /** | |
| 594 | - * Method to get antivirus history details data. | |
| 595 | - * | |
| 596 | - * @param int $scan_id | |
| 597 | - * Scan ID. | |
| 598 | - * @param int $page_num | |
| 599 | - * Page number. | |
| 600 | - * @param int $page_size | |
| 601 | - * Number of entries per page. | |
| 602 | - * | |
| 603 | - * @return array | |
| 604 | - * Returns antivirus history data. | |
| 605 | - */ | |
| 606 | - public static function getAntivirusHistoryDetails($scan_id, $page_num = 1, $page_size = 10) | |
| 607 | - { | |
| 608 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 609 | - $response = self::sendRequest('/dashboard/antivirus/' . $config_id . '/history/' . $scan_id . '/details', [ | |
| 610 | - 'page_num' => $page_num, | |
| 611 | - 'page_size' => $page_size | |
| 612 | - ], 'GET', TRUE); | |
| 655 | + return FALSE; | |
| 656 | + } | |
| 613 | 657 | |
| 658 | + /** | |
| 659 | + * Method to toggle modules config. | |
| 660 | + * | |
| 661 | + * @param string $service_id | |
| 662 | + * Service id that we enable or disable. | |
| 663 | + * | |
| 664 | + * @return string|bool | |
| 665 | + * Returns information whether the request was successful. | |
| 666 | + */ | |
| 667 | + public static function toggleConfigs($service_id) { | |
| 668 | + $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 } } } } } "}'; | |
| 669 | + $response = self::sendRequest($payload, TRUE); | |
| 614 | 670 | |
| 615 | - if (isset($response['data'])) { | |
| 616 | - return $response['data']; | |
| 617 | - } | |
| 618 | - return []; | |
| 671 | + if (isset($response['data']['auth']['configs']['toggle'])) { | |
| 672 | + return $response['data']['auth']['configs']['toggle']; | |
| 619 | 673 | } |
| 620 | 674 | |
| 621 | - /** | |
| 622 | - * Method to get quarantine data. | |
| 623 | - * | |
| 624 | - * @param int $page_num | |
| 625 | - * Page number. | |
| 626 | - * @param int $page_size | |
| 627 | - * Number of entries per page. | |
| 628 | - * | |
| 629 | - * @return array | |
| 630 | - * Returns quarantine data. | |
| 631 | - */ | |
| 632 | - public static function getAntivirusCurrentDetails($page_num = 1, $page_size = 5) | |
| 633 | - { | |
| 634 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 635 | - $response = self::sendRequest('/dashboard/antivirus/' . $config_id . '/current/details', [ | |
| 636 | - 'page_num' => $page_num, | |
| 637 | - 'page_size' => $page_size | |
| 638 | - ], 'GET', TRUE); | |
| 675 | + return FALSE; | |
| 676 | + } | |
| 639 | 677 | |
| 640 | - if (isset($response['data'])) { | |
| 641 | - return $response['data']; | |
| 642 | - } | |
| 643 | - return []; | |
| 678 | + /** | |
| 679 | + * Method to toggle modules notification. | |
| 680 | + * | |
| 681 | + * @param string $host_id | |
| 682 | + * Host id on WebTotem. | |
| 683 | + * @param string $service | |
| 684 | + * Service id in which we enable or disable notifications. | |
| 685 | + * | |
| 686 | + * @return string|bool | |
| 687 | + * Returns information whether the request was successful. | |
| 688 | + */ | |
| 689 | + public static function toggleNotifications($host_id, $service) { | |
| 690 | + $payload = '{"query":"mutation{ auth{ sites{ toggleNotifications(siteId: \"' . $host_id . '\", service: ' . $service . ') } } }"}'; | |
| 691 | + $response = self::sendRequest($payload, TRUE); | |
| 692 | + | |
| 693 | + if (isset($response['data']['auth']['sites']['toggleNotifications'])) { | |
| 694 | + return $response;//['data']['auth']['sites']['toggleNotifications']; | |
| 644 | 695 | } |
| 645 | 696 | |
| 697 | + return FALSE; | |
| 698 | + } | |
| 646 | 699 | |
| 647 | - /** | |
| 648 | - * Method to force check Antivirus. | |
| 649 | - * | |
| 650 | - * @return mixed | |
| 651 | - * Returns information whether the request was successful. | |
| 652 | - */ | |
| 653 | - public static function forceCheckAV() | |
| 654 | - { | |
| 655 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 656 | - return self::sendRequest('/dashboard/antivirus/' . $config_id . '/check', [], 'POST', TRUE); | |
| 657 | - } | |
| 700 | + /** | |
| 701 | + * Method to get allow/deny ip list. | |
| 702 | + * | |
| 703 | + * @param string $host_id | |
| 704 | + * Host id on WebTotem. | |
| 705 | + * | |
| 706 | + * @return array|bool | |
| 707 | + * Returns ip allow/deny lists. | |
| 708 | + */ | |
| 709 | + public static function getIpLists($host_id) { | |
| 710 | + $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 } } } } } } }"} '; | |
| 711 | + $response = self::sendRequest($payload, TRUE); | |
| 658 | 712 | |
| 659 | - | |
| 660 | - /** | |
| 661 | - * Method to force check services. | |
| 662 | - * | |
| 663 | - * @param string $host_id | |
| 664 | - * Host id on WebTotem. | |
| 665 | - * @param string $module_name | |
| 666 | - * Service that needs to be checked. | |
| 667 | - * | |
| 668 | - * @return array | |
| 669 | - * Returns information whether the request was successful. | |
| 670 | - */ | |
| 671 | - public static function forceCheck($host_id, $module_name) | |
| 672 | - { | |
| 673 | - return self::sendRequest('/dashboard/hosts/' . $host_id . '/check', ['module_name' => $module_name], 'POST', TRUE); | |
| 713 | + if (isset($response['data']['auth']['viewer']['sites']['one']['firewall'])) { | |
| 714 | + return $response['data']['auth']['viewer']['sites']['one']['firewall']; | |
| 674 | 715 | } |
| 675 | 716 | |
| 676 | - /** | |
| 677 | - * Method to get quarantine data. | |
| 678 | - * | |
| 679 | - * @param int $page_num | |
| 680 | - * Page number. | |
| 681 | - * @param int $page_size | |
| 682 | - * Number of entries per page. | |
| 683 | - * | |
| 684 | - * @return array | |
| 685 | - * Returns quarantine data. | |
| 686 | - */ | |
| 687 | - public static function getQuarantineList($page_num = 1, $page_size = 5) | |
| 688 | - { | |
| 689 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 690 | - $response = self::sendRequest('/dashboard/antivirus/' . $config_id . '/quarantine', [ | |
| 691 | - 'page_num' => $page_num, | |
| 692 | - 'page_size' => $page_size | |
| 693 | - ], 'GET', TRUE); | |
| 717 | + return []; | |
| 718 | + } | |
| 694 | 719 | |
| 695 | - if (isset($response['data'])) { | |
| 696 | - return $response['data']; | |
| 697 | - } | |
| 698 | - return []; | |
| 699 | - } | |
| 720 | + /** | |
| 721 | + * Method to add ip to allow/deny list. | |
| 722 | + * | |
| 723 | + * @param string $host_id | |
| 724 | + * Host id on WebTotem. | |
| 725 | + * @param string $ips | |
| 726 | + * Ip address list. | |
| 727 | + * @param string $list | |
| 728 | + * Allow or deny list. | |
| 729 | + * | |
| 730 | + * @return bool | |
| 731 | + * Returns information whether the request was successful. | |
| 732 | + */ | |
| 733 | + public static function addIpToList($host_id, $ips, $list) { | |
| 700 | 734 | |
| 701 | - /** | |
| 702 | - * Method to move file to quarantine. | |
| 703 | - * | |
| 704 | - * @param string $file_id | |
| 705 | - * File ID. | |
| 706 | - * | |
| 707 | - * @return array | |
| 708 | - * Returns information whether the request was successful. | |
| 709 | - */ | |
| 710 | - public static function moveToQuarantine($file_id) | |
| 711 | - { | |
| 712 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 713 | - return self::sendRequest('/dashboard/antivirus/' . $config_id . '/quarantine/to-quarantine', [ | |
| 714 | - 'file_id' => $file_id, | |
| 715 | - ], 'POST', TRUE); | |
| 716 | - } | |
| 735 | + if ($ips) { | |
| 736 | + $ips = WebTotem::convertIpListForApi($ips); | |
| 737 | + $payload = '{"variables":{ "input": { "siteId": "' . $host_id . '", "ips": ' . $ips . ', "color": "' . $list . '" } }, "query":"mutation($input: WafListInput!) { auth { sites { waf { addToList(input: $input){ status invalidIPs} } } } }"} '; | |
| 738 | + $response = self::sendRequest($payload, TRUE); | |
| 717 | 739 | |
| 718 | - /** | |
| 719 | - * Method to move file from quarantine. | |
| 720 | - * | |
| 721 | - * @param string $file_id | |
| 722 | - * File ID. | |
| 723 | - * | |
| 724 | - * @return array | |
| 725 | - * Returns information whether the request was successful. | |
| 726 | - */ | |
| 727 | - public static function moveFromQuarantine($file_id) | |
| 728 | - { | |
| 729 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 730 | - return self::sendRequest('/dashboard/antivirus/' . $config_id . '/quarantine/from-quarantine', [ | |
| 731 | - 'file_id' => $file_id, | |
| 732 | - ], 'POST', TRUE); | |
| 740 | + if (isset($response['data']['auth']['sites']['waf']['addToList'])) { | |
| 741 | + return $response['data']['auth']['sites']['waf']['addToList']; | |
| 742 | + } | |
| 733 | 743 | } |
| 734 | 744 | |
| 735 | - /** | |
| 736 | - * Method to get allow/deny ip list. | |
| 737 | - * | |
| 738 | - * @param string $type | |
| 739 | - * Type of ip list | |
| 740 | - * | |
| 741 | - * @return array|bool | |
| 742 | - * Returns ip allow/deny lists. | |
| 743 | - */ | |
| 744 | - public static function getIpLists($type = 'blacklist') | |
| 745 | - { | |
| 746 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 747 | - $response = self::sendRequest('/dashboard/firewall/' . $config_id . '/configs/iplist', [ | |
| 748 | - 'type' => $type, | |
| 749 | - ], 'GET', TRUE); | |
| 745 | + return FALSE; | |
| 746 | + } | |
| 750 | 747 | |
| 751 | - if (isset($response['data'])) { | |
| 752 | - return $response['data']; | |
| 753 | - } | |
| 748 | + /** | |
| 749 | + * Method to remove ip from allow/deny list by id. | |
| 750 | + * | |
| 751 | + * @param string $id | |
| 752 | + * Id assignment to ip address. | |
| 753 | + * | |
| 754 | + * @return bool | |
| 755 | + * Returns information whether the request was successful. | |
| 756 | + */ | |
| 757 | + public static function removeIpFromList($id) { | |
| 758 | + $payload = '{"variables":{ "id": "' . $id . '" },"query":"mutation($id: ID!) { auth { sites { waf { removeFromList(id: $id) } } } }"} '; | |
| 759 | + $response = self::sendRequest($payload, TRUE); | |
| 754 | 760 | |
| 755 | - return []; | |
| 761 | + if (isset($response['data']['auth']['sites']['waf']['removeFromList'])) { | |
| 762 | + return $response['data']['auth']['sites']['waf']['removeFromList']; | |
| 756 | 763 | } |
| 757 | 764 | |
| 758 | - /** | |
| 759 | - * Method to add ip to allow/deny list. | |
| 760 | - * | |
| 761 | - * @param string $ip | |
| 762 | - * Ip address. | |
| 763 | - * @param string $type | |
| 764 | - * Allow or deny type. | |
| 765 | - * | |
| 766 | - * @return bool | |
| 767 | - * Returns information whether the request was successful. | |
| 768 | - */ | |
| 769 | - public static function addIpToList($ips, $type) | |
| 770 | - { | |
| 771 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 772 | - self::sendRequest('/dashboard/firewall/' . $config_id . '/configs/iplist?type=' . $type, [ | |
| 773 | - 'ip' => array_filter($ips), | |
| 774 | - ], 'POST', TRUE); | |
| 765 | + return FALSE; | |
| 766 | + } | |
| 775 | 767 | |
| 776 | - return true; | |
| 777 | - } | |
| 768 | + /** | |
| 769 | + * Method to get allow url list. | |
| 770 | + * | |
| 771 | + * @param string $host_id | |
| 772 | + * Host id on WebTotem. | |
| 773 | + * | |
| 774 | + * @return array | |
| 775 | + * Returns url allow lists. | |
| 776 | + */ | |
| 777 | + public static function getAllowUrlList($host_id) { | |
| 778 | + $payload = '{"query":"query { auth { viewer { sites { one(id: \"' . $host_id . '\"){ firewall{ urlWhiteList{ id url createdAt } } } } } } }"} '; | |
| 779 | + $response = self::sendRequest($payload, TRUE); | |
| 778 | 780 | |
| 779 | - /** | |
| 780 | - * Method to remove ip from allow/deny list by id. | |
| 781 | - * | |
| 782 | - * @param string $ip | |
| 783 | - * Ip address. | |
| 784 | - * @param string $type | |
| 785 | - * Allow or deny type. | |
| 786 | - * | |
| 787 | - * @return bool | |
| 788 | - * Returns information whether the request was successful. | |
| 789 | - */ | |
| 790 | - public static function removeIpFromList($ip, $type) | |
| 791 | - { | |
| 792 | - $config_id = WebTotemOption::getOption('config_id'); | |
| 793 | - self::sendRequest('/dashboard/firewall/' . $config_id . '/configs/iplist?type=' . $type, [ | |
| 794 | - 'ip' => $ip, | |
| 795 | - ], 'DELETE', TRUE); | |
| 796 | - | |
| 797 | - return true; | |
| 781 | + if (isset($response['data']['auth']['viewer']['sites']['one']['firewall']['urlWhiteList'])) { | |
| 782 | + return $response['data']['auth']['viewer']['sites']['one']['firewall']['urlWhiteList']; | |
| 798 | 783 | } |
| 799 | 784 | |
| 800 | - /** | |
| 801 | - * Sends a REST API request to the WebTotem API server. | |
| 802 | - * | |
| 803 | - * @param string $endpoint | |
| 804 | - * REST API endpoint (e.g., 'scan', 'status', etc.). | |
| 805 | - * @param array $data | |
| 806 | - * Associative array of data to send as JSON body or query parameters. | |
| 807 | - * @param string $method | |
| 808 | - * HTTP method: GET, POST, PUT, DELETE (default is POST). | |
| 809 | - * @param bool $useToken | |
| 810 | - * Whether to include the auth token. | |
| 811 | - * @param bool $retry | |
| 812 | - * Used to prevent recursion on token renewal. | |
| 813 | - * | |
| 814 | - * @return array|null | |
| 815 | - * API response as an associative array, or null on failure. | |
| 816 | - */ | |
| 817 | - protected static function sendRequest($endpoint, $data = [], $method = 'POST', $useToken = false, $retry = false, $silent = false) | |
| 818 | - { | |
| 819 | - self::$last_status = 0; | |
| 820 | - $api_key = WebTotemOption::getOption('api_key'); | |
| 785 | + return []; | |
| 786 | + } | |
| 821 | 787 | |
| 822 | - // Get or initialize the API URL. | |
| 823 | - $api_url = WebTotemOption::getOption('api_url'); | |
| 824 | - if (!$api_url) { | |
| 825 | - $api_url = self::getApiUrl(); | |
| 826 | - WebTotemOption::setOptions(['api_url' => $api_url]); | |
| 827 | - } | |
| 788 | + /** | |
| 789 | + * Method to add url to allow list. | |
| 790 | + * | |
| 791 | + * @param string $host_id | |
| 792 | + * Host id on WebTotem. | |
| 793 | + * @param string $url | |
| 794 | + * User-specified url. | |
| 795 | + * | |
| 796 | + * @return bool|string | |
| 797 | + * Returns information whether the request was successful. | |
| 798 | + */ | |
| 799 | + public static function addUrlToAllowList($host_id, $url) { | |
| 800 | + $payload = '{"variables":{ "input": { "siteId": "' . $host_id . '", "url": "' . $url . '" } }, "query":"mutation($input: WafUrlWhiteListInput!) { auth { sites { waf { addToUrlWhiteList(input: $input) } } } }"} '; | |
| 801 | + $response = self::sendRequest($payload, TRUE); | |
| 828 | 802 | |
| 829 | - // The API previously answered 429: respect the requested pause instead of | |
| 830 | - // hammering the server (and getting the whole site throttled). | |
| 831 | - $throttled_until = (int) WebTotemOption::getOption('api_retry_after'); | |
| 832 | - if ($throttled_until > time()) { | |
| 833 | - self::notify($silent, 'warning', sprintf( | |
| 834 | - /* translators: %d: number of seconds to wait. */ | |
| 835 | - __('Too many requests to the WebTotem API. Please try again in %d seconds.', 'wtotem'), | |
| 836 | - $throttled_until - time() | |
| 837 | - )); | |
| 803 | + if (isset($response['data']['auth']['sites']['waf']['addToUrlWhiteList'])) { | |
| 804 | + return $response['data']['auth']['sites']['waf']['addToUrlWhiteList']; | |
| 805 | + } | |
| 838 | 806 | |
| 839 | - return NULL; | |
| 840 | - } | |
| 807 | + return FALSE; | |
| 808 | + } | |
| 841 | 809 | |
| 842 | - $auth_token = NULL; | |
| 843 | - if ($useToken) { | |
| 844 | - $auth_token = WebTotemOption::getOption('auth_token'); | |
| 845 | - $auth_token_expired = (int) WebTotemOption::getOption('auth_token_expired'); | |
| 810 | + /** | |
| 811 | + * Method to remove url from allow list. | |
| 812 | + * | |
| 813 | + * @param string $id | |
| 814 | + * Id assignment to url address. | |
| 815 | + * | |
| 816 | + * @return bool|string | |
| 817 | + * Returns information whether the request was successful. | |
| 818 | + */ | |
| 819 | + public static function removeUrlFromAllowList($id) { | |
| 820 | + $payload = '{"variables":{ "id": "' . $id . '" }, "query":"mutation($id: ID!) { auth { sites { waf { removeFromUrlWhiteList(id: $id) } } } }"} '; | |
| 821 | + $response = self::sendRequest($payload, TRUE); | |
| 846 | 822 | |
| 847 | - if ((!$auth_token || $auth_token_expired <= time()) && !$retry) { | |
| 848 | - $result = self::auth($api_key); | |
| 849 | - if ($result === 'success') { | |
| 850 | - return self::sendRequest($endpoint, $data, $method, $useToken, TRUE, $silent); | |
| 851 | - } | |
| 852 | - } | |
| 853 | - } | |
| 823 | + if (isset($response['data']['auth']['sites']['waf']['removeFromUrlWhiteList'])) { | |
| 824 | + return $response['data']['auth']['sites']['waf']['removeFromUrlWhiteList']; | |
| 825 | + } | |
| 854 | 826 | |
| 855 | - $url = rtrim($api_url, '/') . '/api/v1/' . ltrim($endpoint, '/'); | |
| 827 | + return FALSE; | |
| 828 | + } | |
| 856 | 829 | |
| 857 | - $args = [ | |
| 858 | - 'timeout' => 60, | |
| 859 | - 'sslverify' => TRUE, | |
| 860 | - 'headers' => [ | |
| 861 | - 'Accept' => 'application/json', | |
| 862 | - 'Content-Type' => 'application/json', | |
| 863 | - 'source' => 'WORDPRESS', | |
| 864 | - ], | |
| 865 | - ]; | |
| 830 | + /** | |
| 831 | + * Method to get user's email. | |
| 832 | + * | |
| 833 | + * @return string | |
| 834 | + * Returns user's email. | |
| 835 | + */ | |
| 836 | + public static function getEmail(){ | |
| 837 | + $payload = '{"query":"query { auth { viewer { email __typename } __typename } }"}'; | |
| 838 | + $response = self::sendRequest($payload, true); | |
| 866 | 839 | |
| 867 | - if ($auth_token) { | |
| 868 | - $args['headers']['Authorization'] = "Bearer $auth_token"; | |
| 869 | - } | |
| 840 | + return $response['data']['auth']['viewer']['email']; | |
| 841 | + } | |
| 870 | 842 | |
| 871 | - if (strtoupper($method) === 'GET') { | |
| 872 | - $url = add_query_arg($data, $url); | |
| 873 | - } else { | |
| 874 | - $args['body'] = wp_json_encode($data); | |
| 875 | - } | |
| 843 | + /** | |
| 844 | + * Function sends GraphQL request to API server. | |
| 845 | + * | |
| 846 | + * @param string $payload | |
| 847 | + * Payload to be sent to API server. | |
| 848 | + * @param bool $token | |
| 849 | + * Whether a token is needed when sending a request. | |
| 850 | + * @param bool $repeat | |
| 851 | + * Required to avoid recursion. | |
| 852 | + * | |
| 853 | + * @return array | |
| 854 | + * Returns response from WebTotem API. | |
| 855 | + */ | |
| 856 | + protected static function sendRequest($payload, $token = FALSE, $repeat = FALSE) { | |
| 876 | 857 | |
| 877 | - $response = wp_remote_request($url, array_merge($args, ['method' => strtoupper($method)])); | |
| 858 | + $api_key = WebTotemOption::getOption('api_key'); | |
| 878 | 859 | |
| 879 | - if (is_wp_error($response)) { | |
| 880 | - self::notify($silent, 'error', WebTotem::messageForHuman( | |
| 881 | - 'SERVER UNAVAILABLE: ' . $response->get_error_message() | |
| 882 | - )); | |
| 860 | + // Checking whether a token is needed. | |
| 861 | + if ($token) { | |
| 862 | + $auth_token = WebTotemOption::getOption('auth_token'); | |
| 863 | + $auth_token_expired = WebTotemOption::getOption('auth_token_expired'); | |
| 883 | 864 | |
| 884 | - return NULL; | |
| 865 | + // Checking whether the token has expired. | |
| 866 | + if ($auth_token_expired <= time() && !$repeat) { | |
| 867 | + $result = self::auth($api_key); | |
| 868 | + if ($result === 'success') { | |
| 869 | + return self::sendRequest($payload, $token, TRUE); | |
| 885 | 870 | } |
| 886 | - | |
| 887 | - $code = (int) wp_remote_retrieve_response_code($response); | |
| 888 | - self::$last_status = $code; | |
| 889 | - $body = wp_remote_retrieve_body($response); | |
| 890 | - $decoded = json_decode($body, TRUE); | |
| 891 | - | |
| 892 | - if (!is_array($decoded)) { | |
| 893 | - $decoded = []; | |
| 871 | + else { | |
| 872 | + if(isset($result['errors'])){ | |
| 873 | + $message = WebTotem::messageForHuman($result['errors'][0]['message']); | |
| 874 | + WebTotemOption::setNotification('error', $message); | |
| 875 | + } | |
| 894 | 876 | } |
| 877 | + } | |
| 878 | + } | |
| 895 | 879 | |
| 896 | - $error_message = isset($decoded['message']) ? (string) $decoded['message'] : ''; | |
| 880 | + if (function_exists('wp_remote_post')) { | |
| 897 | 881 | |
| 898 | - // Terminal account states: show the dedicated page and stop. | |
| 899 | - if ($error_message !== '') { | |
| 900 | - if (stripos($error_message, 'Password expired') !== FALSE) { | |
| 901 | - wtotem_error_page(['errors' => 'PASSWORD_EXPIRED']); | |
| 902 | - exit(); | |
| 903 | - } | |
| 882 | + $args = [ | |
| 883 | + 'body' => $payload, | |
| 884 | + 'timeout' => '60', | |
| 885 | + 'sslverify' => false, | |
| 886 | + 'headers' => [ | |
| 887 | + 'Content-Type:application/json', | |
| 888 | + 'Content-Type' => 'application/json', | |
| 889 | + 'Accept: application/json', | |
| 890 | + 'source: WORDPRESS', | |
| 891 | + ], | |
| 892 | + ]; | |
| 904 | 893 | |
| 905 | - if (stripos($error_message, 'API_KEY_DEACTIVATED') !== FALSE) { | |
| 906 | - wtotem_error_page(['errors' => 'TARIFF_EXPIRED']); | |
| 907 | - exit(); | |
| 908 | - } | |
| 909 | - } | |
| 894 | + if (isset($auth_token)) { | |
| 895 | + $auth = "Bearer " . $auth_token; | |
| 896 | + $args['headers'] = array_merge($args['headers'], ["Authorization" => $auth]); | |
| 897 | + } | |
| 910 | 898 | |
| 911 | - // 429 Too Many Requests: honour Retry-After and never re-authorize. | |
| 912 | - if ($code === 429) { | |
| 913 | - $delay = self::parseRetryAfter(wp_remote_retrieve_header($response, 'retry-after')); | |
| 914 | - WebTotemOption::setOptions(['api_retry_after' => time() + $delay]); | |
| 915 | - self::notify($silent, 'warning', sprintf( | |
| 916 | - /* translators: %d: number of seconds to wait. */ | |
| 917 | - __('Too many requests to the WebTotem API. Please try again in %d seconds.', 'wtotem'), | |
| 918 | - $delay | |
| 919 | - )); | |
| 899 | + $response = wp_remote_post(WEBTOTEM_API_URL, $args); | |
| 900 | + $response = wp_remote_retrieve_body($response); | |
| 901 | + $response = json_decode($response, true); | |
| 920 | 902 | |
| 921 | - return NULL; | |
| 922 | - } | |
| 903 | + } | |
| 904 | + else { | |
| 905 | + $error = 'WP_REMOTE_POST_NOT_EXIST'; | |
| 906 | + } | |
| 923 | 907 | |
| 924 | - // 401 Unauthorized: the token is gone or expired. Re-login by API key once. | |
| 925 | - if ($code === 401) { | |
| 926 | - if (!$retry && $api_key && self::auth($api_key, TRUE) === 'success') { | |
| 927 | - return self::sendRequest($endpoint, $data, $method, $useToken, TRUE, $silent); | |
| 928 | - } | |
| 929 | - | |
| 930 | - self::notify($silent, 'error', WebTotem::messageForHuman( | |
| 931 | - $error_message !== '' ? $error_message : 'invalid credentials' | |
| 932 | - )); | |
| 933 | - | |
| 934 | - return $decoded; | |
| 908 | + // Checking if there are errors in the response. | |
| 909 | + if (isset($response['errors'][0]['message'])) { | |
| 910 | + $message = WebTotem::messageForHuman($response['errors'][0]['message']); | |
| 911 | + if (stripos($response['errors'][0]['message'], "INVALID_TOKEN") !== FALSE && !$repeat) { | |
| 912 | + $response = self::auth($api_key); | |
| 913 | + if ($response === 'success') { | |
| 914 | + return self::sendRequest($payload, $token, TRUE); | |
| 935 | 915 | } |
| 936 | - | |
| 937 | - // 403 Forbidden: authenticated, but not allowed. Re-login would not help. | |
| 938 | - if ($code === 403) { | |
| 939 | - if (stripos($error_message, 'USERHOST_NOT_BELONG_TO_USER') !== FALSE) { | |
| 940 | - self::forgetHost(); | |
| 941 | - } else { | |
| 942 | - self::notify($silent, 'error', WebTotem::messageForHuman( | |
| 943 | - $error_message !== '' ? $error_message : 'access denied' | |
| 944 | - )); | |
| 945 | - } | |
| 946 | - | |
| 947 | - return $decoded; | |
| 916 | + } | |
| 917 | + else { | |
| 918 | + if ($message !== FALSE) { | |
| 919 | + WebTotemOption::setNotification('error', $message); // for debug json_encode($payload).' '. | |
| 948 | 920 | } |
| 949 | - | |
| 950 | - // Older API builds answer 200 with an error message in the body. | |
| 951 | - if ($error_message !== '') { | |
| 952 | - if ($error_message === 'invalid credentials') { | |
| 953 | - if (!$retry && $api_key && self::auth($api_key, TRUE) === 'success') { | |
| 954 | - return self::sendRequest($endpoint, $data, $method, $useToken, TRUE, $silent); | |
| 955 | - } | |
| 956 | - } elseif (stripos($error_message, 'USERHOST_NOT_BELONG_TO_USER') !== FALSE) { | |
| 957 | - self::forgetHost(); | |
| 958 | - } else { | |
| 959 | - self::notify($silent, 'error', WebTotem::messageForHuman($error_message)); | |
| 960 | - } | |
| 961 | - } | |
| 962 | - | |
| 963 | - return $decoded; | |
| 921 | + } | |
| 964 | 922 | } |
| 965 | 923 | |
| 966 | - /** | |
| 967 | - * Reads the Retry-After header into a number of seconds. | |
| 968 | - * | |
| 969 | - * The header is either a number of seconds or an HTTP date. | |
| 970 | - * | |
| 971 | - * @param string $header | |
| 972 | - * Raw Retry-After header value. | |
| 973 | - * | |
| 974 | - * @return int | |
| 975 | - * Seconds to wait, clamped to a sane range. | |
| 976 | - */ | |
| 977 | - protected static function parseRetryAfter($header) | |
| 978 | - { | |
| 979 | - $delay = 0; | |
| 980 | - $header = is_string($header) ? trim($header) : ''; | |
| 981 | - | |
| 982 | - if ($header !== '') { | |
| 983 | - if (ctype_digit($header)) { | |
| 984 | - $delay = (int) $header; | |
| 985 | - } else { | |
| 986 | - $timestamp = strtotime($header); | |
| 987 | - if ($timestamp !== FALSE) { | |
| 988 | - $delay = $timestamp - time(); | |
| 989 | - } | |
| 990 | - } | |
| 991 | - } | |
| 992 | - | |
| 993 | - if ($delay < 1) { | |
| 994 | - $delay = 60; | |
| 995 | - } | |
| 996 | - | |
| 997 | - // Never park the plugin for longer than an hour. | |
| 998 | - return min($delay, HOUR_IN_SECONDS); | |
| 924 | + if (!empty($error)) { | |
| 925 | + WebTotemOption::setNotification('error', $error); | |
| 999 | 926 | } |
| 1000 | 927 | |
| 1001 | - /** | |
| 1002 | - * Drops the locally stored host binding after the API disowned it. | |
| 1003 | - * | |
| 1004 | - * @return void | |
| 1005 | - */ | |
| 1006 | - protected static function forgetHost() | |
| 1007 | - { | |
| 1008 | - if (WebTotem::isMultiSite()) { | |
| 1009 | - WebTotemOption::clearAllHosts(); | |
| 1010 | - } | |
| 1011 | - | |
| 1012 | - WebTotemOption::clearOptions(['host_id', 'host_name']); | |
| 1013 | - } | |
| 928 | + return $response; | |
| 929 | + } | |
| 1014 | 930 | |
| 1015 | 931 | } |