*/ public $details; /** * @param string $message * @param string $code * @param array $details */ public function __construct($message, $code = '', $details = array()) { parent::__construct($message); $this->error_code = $code; $this->details = is_array($details) ? $details : array(); } } class GuardBee_Api { /** * @param string $method * @param string $path * @param array|null $body * @param bool $raw * @return array|string */ public static function request($method, $path, $body = null, $raw = false) { $token = GuardBee_Plugin::token(); if ($token === '') { throw new GuardBee_Api_Exception(esc_html(guardbee_text('missing_token')), 'TOKEN_INVALID'); } $args = array( 'method' => $method, 'timeout' => 30, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Accept' => $raw ? '*/*' : 'application/json', 'X-GuardBee-Site' => home_url(), ), ); if ($body !== null) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode($body); } $response = wp_remote_request(GuardBee_Plugin::api_base() . $path, $args); if (is_wp_error($response)) { throw new GuardBee_Api_Exception(esc_html($response->get_error_message()), 'TRANSPORT'); } $status = (int) wp_remote_retrieve_response_code($response); $payload = (string) wp_remote_retrieve_body($response); if ($raw && $status >= 200 && $status < 300) { return $payload; } $decoded = json_decode($payload, true); if (!is_array($decoded)) { $decoded = array(); } if ($status < 200 || $status >= 300) { $message = isset($decoded['error']) ? (string) $decoded['error'] : guardbee_text('failed'); $code = isset($decoded['code']) ? (string) $decoded['code'] : ''; $reason = ''; if (isset($decoded['details']['reason']) && is_scalar($decoded['details']['reason'])) { $reason = (string) $decoded['details']['reason']; } throw new GuardBee_Api_Exception( esc_html($message), esc_html($code), array( 'reason' => esc_html($reason), ) ); } return isset($decoded['data']) && is_array($decoded['data']) ? $decoded['data'] : array(); } /** * @param string $token * @return array */ public static function connect($token) { GuardBee_Plugin::store_token($token); $nonce = wp_generate_password(32, false, false); return self::request('POST', '/api/v1/wordpress/connect', array( 'siteUrl' => home_url(), 'nonce' => $nonce, 'siteName' => get_bloginfo('name'), 'wpVersion' => get_bloginfo('version'), 'pluginVersion' => GUARDBEE_VERSION, )); } /** @return array */ public static function status() { $data = self::request('GET', '/api/v1/wordpress/status'); return is_array($data) ? $data : array(); } /** * @param string $scenario * @param string $scope * @return array */ public static function start_scan($scenario, $scope) { $data = self::request('POST', '/api/v1/wordpress/scans', array( 'scenario' => $scenario, 'scanScope' => $scope, )); return is_array($data) ? $data : array(); } /** @return array */ public static function findings() { $data = self::request('GET', '/api/v1/wordpress/findings'); return is_array($data) ? $data : array(); } /** * @param bool $enabled * @param string $interval * @param string $preset * @return array */ public static function schedule($enabled, $interval, $preset) { $data = self::request('PUT', '/api/v1/wordpress/schedule', array( 'enabled' => $enabled, 'interval' => $interval, 'preset' => $preset, )); return is_array($data) ? $data : array(); } public static function disconnect() { self::request('POST', '/api/v1/wordpress/disconnect', array()); } /** * @param bool $enabled * @return array */ public static function set_badge($enabled) { $data = self::request('PUT', '/api/v1/wordpress/badge', array( 'enabled' => (bool) $enabled, )); return is_array($data) ? $data : array(); } /** * @param string $scan_id * @param string $format * @return string */ public static function report($scan_id, $format) { $locale = function_exists('get_locale') && strpos((string) get_locale(), 'tr') === 0 ? 'tr' : 'en'; $path = '/api/v1/wordpress/scans/' . rawurlencode($scan_id) . '/report?format=' . rawurlencode($format) . '&locale=' . $locale; $body = self::request('GET', $path, null, true); return is_string($body) ? $body : ''; } }