error_code, array('TOKEN_INVALID', 'TOKEN_STALE', 'NOT_CONNECTED'), true); if (!$stale && $error === '') { $error = $err->getMessage(); } } } $quota = is_array($status) && isset($status['quota']) && is_array($status['quota']) ? $status['quota'] : array(); $brand = is_array($status) && isset($status['brand']) && is_array($status['brand']) ? $status['brand'] : array(); $features = isset($quota['features']) && is_array($quota['features']) ? $quota['features'] : array(); $latest = is_array($status) && isset($status['latestScan']) && is_array($status['latestScan']) ? $status['latestScan'] : null; $running = is_array($latest) && in_array(isset($latest['status']) ? $latest['status'] : '', array('PENDING', 'RUNNING'), true); $connected = is_array($status); $polls = isset($_GET['guardbee_poll']) ? (int) $_GET['guardbee_poll'] : 0; $refresh_url = ''; if ($running || self::score_pending($brand, $latest, $polls)) { $refresh_url = add_query_arg('guardbee_poll', (string) ($polls + 1), admin_url('admin.php?page=guardbee')); } $lead = guardbee_text('intro'); if ($connected && !empty($brand['hostname'])) { $lead = str_replace('{host}', (string) $brand['hostname'], guardbee_text('connected_lead')); } echo '
'; self::masthead($lead); self::notice($notice, $error); if (!$connected) { self::connect_form($needs_key, $stale); echo '
'; return; } self::summary($quota, $brand, $latest); if (!empty($brand['parked'])) { self::banner('warn', guardbee_text('parked')); } if ($running) { self::banner('info', guardbee_text('running')); } self::scan_form($features, !empty($brand['canScan']) && !$running); self::findings_table(is_array($findings) ? $findings : array()); if (!empty($features['schedule'])) { self::schedule_form($brand); } if (is_array($latest) && isset($latest['status']) && $latest['status'] === 'COMPLETED' && !empty($latest['id'])) { self::report_form((string) $latest['id']); } self::badge_form( isset($status['badge']) && is_array($status['badge']) ? $status['badge'] : null, $brand, $latest ); self::disconnect_form(); echo ''; } /** * @param string $action */ private static function guard($action) { if (!current_user_can('manage_options')) { wp_die(esc_html(guardbee_text('forbidden'))); } check_admin_referer($action); } /** * @param string $notice */ private static function redirect($notice) { wp_safe_redirect(admin_url('admin.php?page=guardbee&guardbee_notice=' . rawurlencode($notice))); exit; } private static function redirect_error(GuardBee_Api_Exception $err) { $message = $err->getMessage(); if ($err->error_code === 'BADGE_NOT_ELIGIBLE') { $reason = isset($err->details['reason']) ? (string) $err->details['reason'] : ''; $key = 'badge_reason_' . $reason; $translated = guardbee_text($key); $message = $translated === $key ? guardbee_text('badge_ineligible') : $translated; } $url = admin_url('admin.php?page=guardbee&guardbee_error=' . rawurlencode($message)); wp_safe_redirect($url); exit; } /** * @param string $notice * @param string $error */ private static function notice($notice, $error) { $messages = array( 'connected' => guardbee_text('connected'), 'started' => guardbee_text('started'), 'scheduled' => guardbee_text('scheduled'), 'disconnected' => guardbee_text('disconnected'), 'badge_added' => guardbee_text('badge_added'), 'badge_removed' => guardbee_text('badge_removed'), 'missing_token' => guardbee_text('missing_token'), 'failed' => guardbee_text('failed'), ); if ($notice !== '' && isset($messages[$notice])) { $tone = $notice === 'missing_token' || $notice === 'failed' ? 'error' : 'ok'; self::banner($tone, $messages[$notice]); } if ($error !== '') { self::banner('error', $error); } } /** * @param string $lead */ private static function masthead($lead) { $logo = plugins_url('assets/logo-icon.png', GUARDBEE_FILE); echo '
'; echo ''; echo '
'; echo '

Guardbee.ai

'; echo '

' . esc_html($lead) . '

'; echo '
'; } /** * @param string $tone * @param string $message */ private static function banner($tone, $message) { echo '

' . esc_html($message) . '

'; } /** * @param string $label * @param string $variant * @param bool $enabled */ private static function submit($label, $variant, $enabled = true) { echo ''; } /** * @param string $path */ private static function account_url($path) { $callback = rawurlencode('/settings?wordpress=1'); return GuardBee_Plugin::app_url($path . '?callbackUrl=' . $callback); } /** * @param bool $needs_key * @param bool $stale */ private static function connect_form($needs_key, $stale) { if ($stale) { self::banner('warn', guardbee_text('reconnect')); } $register = self::account_url('register'); $login = self::account_url('login'); echo '
'; echo '
'; echo '

' . esc_html(guardbee_text('signup_body')) . '

'; echo '
'; echo '
'; wp_nonce_field('guardbee_connect'); echo ''; echo ''; echo ''; if (!$needs_key) { echo '

' . esc_html(guardbee_text('token_saved')) . '

'; } self::submit(guardbee_text('connect'), 'primary'); echo '
'; } /** * @param array $quota * @param array $brand * @param array|null $latest */ private static function summary($quota, $brand, $latest) { $plan = isset($quota['planId']) ? (string) $quota['planId'] : ''; $remaining = array_key_exists('brandsRemaining', $quota) ? $quota['brandsRemaining'] : null; $left = $remaining === null ? guardbee_text('unlimited') : (string) $remaining; echo '
'; echo '
' . esc_html(guardbee_text('plan')) . '
' . esc_html($plan) . '
'; echo '
' . esc_html(guardbee_text('remaining')) . '
' . esc_html($left) . '
'; echo '
' . esc_html(guardbee_text('score')) . '
' . esc_html(self::shown_score($brand, $latest)) . '
'; echo '
'; if ($remaining === 0) { self::banner('warn', guardbee_text('none_left')); } } /** * Completed scans store the score on the scan row before the brand row. * Show the scan score as soon as the scan is finished. * * @param array $brand * @param array|null $latest */ private static function shown_score($brand, $latest) { if (is_array($latest) && isset($latest['status']) && $latest['status'] === 'COMPLETED' && array_key_exists('securityScore', $latest) && $latest['securityScore'] !== null && $latest['securityScore'] !== '') { return (string) $latest['securityScore']; } if (isset($brand['lastSecurityScore']) && $brand['lastSecurityScore'] !== null && $brand['lastSecurityScore'] !== '') { return (string) $brand['lastSecurityScore']; } return guardbee_text('never'); } /** * @param array $brand * @param array|null $latest * @param int $polls */ private static function score_pending($brand, $latest, $polls) { if (!is_array($latest) || !isset($latest['status']) || $latest['status'] !== 'COMPLETED') { return false; } if (!array_key_exists('securityScore', $latest) || $latest['securityScore'] === null || $latest['securityScore'] === '') { return false; } if ($polls >= 6) { return false; } $brand_score = (isset($brand['lastSecurityScore']) && $brand['lastSecurityScore'] !== null && $brand['lastSecurityScore'] !== '') ? (string) $brand['lastSecurityScore'] : null; return $brand_score !== (string) $latest['securityScore']; } /** * @param array $features * @param bool $enabled */ private static function scan_form($features, $enabled) { echo '
'; wp_nonce_field('guardbee_scan'); echo ''; echo '

' . esc_html(guardbee_text('scan')) . '

'; echo '
'; echo '

'; echo '

'; echo '

'; echo '

'; self::submit(guardbee_text('scan'), 'primary', $enabled); echo '
'; } /** * @param array $payload */ private static function findings_table($payload) { $rows = isset($payload['findings']) && is_array($payload['findings']) ? $payload['findings'] : array(); $scan_id = isset($payload['scanId']) ? (string) $payload['scanId'] : ''; echo '
'; echo '

' . esc_html(guardbee_text('findings')) . '

'; if (count($rows) === 0) { echo '

' . esc_html(guardbee_text('no_findings')) . '

'; return; } echo ''; echo ''; echo ''; echo ''; echo ''; foreach ($rows as $row) { if (!is_array($row)) { continue; } $severity = isset($row['severity']) ? strtolower((string) $row['severity']) : ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html(guardbee_text('severity')) . '' . esc_html(guardbee_text('finding')) . '' . esc_html(guardbee_text('recommendation')) . '
' . esc_html(isset($row['severity']) ? (string) $row['severity'] : '') . '' . wp_kses( self::finding_link($scan_id, $row), array( 'a' => array( 'href' => true, 'class' => true, 'target' => true, 'rel' => true, ), ) ); if (!empty($row['documentationUrl'])) { echo '
' . esc_html(guardbee_text('docs')) . ''; } echo '
' . esc_html(isset($row['recommendation']) ? (string) $row['recommendation'] : '') . '
'; } /** * @param array $brand */ private static function schedule_form($brand) { $schedule = isset($brand['schedule']) && is_array($brand['schedule']) ? $brand['schedule'] : array(); $enabled = !empty($schedule['enabled']); $interval = isset($schedule['interval']) ? (string) $schedule['interval'] : 'weekly'; $preset = isset($schedule['preset']) ? (string) $schedule['preset'] : 'recommended'; echo '
'; wp_nonce_field('guardbee_schedule'); echo ''; echo '

' . esc_html(guardbee_text('schedule')) . '

'; echo '

'; echo '

'; echo '

'; self::submit(guardbee_text('save'), 'secondary'); echo '
'; } /** * @param string $scan_id */ private static function report_form($scan_id) { echo '
'; wp_nonce_field('guardbee_report'); echo ''; echo ''; echo ''; echo ''; echo '
'; } private static function disconnect_form() { echo '
'; wp_nonce_field('guardbee_disconnect'); echo ''; self::submit(guardbee_text('disconnect'), 'danger'); echo '
'; } /** * @param string $scan_id * @param array $row */ private static function finding_link($scan_id, $row) { $title = isset($row['title']) ? (string) $row['title'] : ''; $finding_id = isset($row['id']) ? (string) $row['id'] : ''; if ($scan_id === '' || $finding_id === '') { return esc_html($title); } $href = GuardBee_Plugin::app_url( 'scans/' . rawurlencode($scan_id) . '?finding=' . rawurlencode($finding_id) ); return '' . esc_html($title) . ''; } /** * @param array|null $badge * @param array $brand * @param array|null $latest */ private static function badge_form($badge, $brand = array(), $latest = null) { $footer_on = GuardBee_Plugin::footer_html() !== ''; $eligible = self::badge_can_add($badge, $brand, $latest); $reason = is_array($badge) && isset($badge['reason']) ? (string) $badge['reason'] : ''; $embed = is_array($badge) && !empty($badge['embedUrl']) ? (string) $badge['embedUrl'] : ''; echo '
'; echo '

' . esc_html(guardbee_text('badge_title')) . '

'; echo '

' . esc_html(guardbee_text('badge_body')) . '

'; echo '
'; if ($footer_on) { echo '

' . esc_html(guardbee_text('badge_on')) . '

'; echo '
' . wp_kses(GuardBee_Plugin::footer_html(), GuardBee_Plugin::badge_allowed_html()) . '
'; } elseif ($embed !== '') { echo '

' . esc_html(guardbee_text('badge_preview')) . '

'; echo '
' . esc_attr(guardbee_text('badge_sample_alt')) . '
'; } else { $sample_path = GUARDBEE_DIR . 'assets/badge-sample.svg'; $sample = is_readable($sample_path) ? file_get_contents($sample_path) : ''; echo '

' . esc_html(guardbee_text('badge_sample')) . '

'; echo '
'; if (is_string($sample) && strpos($sample, ''; echo '

' . esc_html(guardbee_text('badge_sample_hint')) . '

'; } echo '
'; if (!$footer_on && !$eligible && $reason !== '' && $reason !== 'OK') { $key = 'badge_reason_' . $reason; $message = guardbee_text($key); if ($message === $key) { $message = guardbee_text('badge_ineligible'); } echo '

' . esc_html($message) . '

'; } echo '
'; wp_nonce_field('guardbee_badge'); echo ''; if ($footer_on) { echo ''; self::submit(guardbee_text('badge_remove'), 'danger'); } else { echo ''; self::submit(guardbee_text('badge_add'), $eligible ? 'primary' : 'secondary', $eligible); } echo '
'; } /** * The score on screen can come from the finished scan before the brand row * catches up. A verified site with that score can add the badge. * * @param array|null $badge * @param array $brand * @param array|null $latest */ private static function badge_can_add($badge, $brand, $latest) { if (is_array($badge) && !empty($badge['eligible'])) { return true; } if (!isset($brand['verificationStatus']) || $brand['verificationStatus'] !== 'VERIFIED') { return false; } $min = is_array($badge) && isset($badge['minScore']) ? (int) $badge['minScore'] : 70; $max_days = is_array($badge) && isset($badge['maxAgeDays']) ? (int) $badge['maxAgeDays'] : 90; $score = null; $at = ''; if ( is_array($latest) && isset($latest['status']) && $latest['status'] === 'COMPLETED' && array_key_exists('securityScore', $latest) && $latest['securityScore'] !== null && $latest['securityScore'] !== '' ) { $score = (int) $latest['securityScore']; $at = isset($latest['completedAt']) ? (string) $latest['completedAt'] : ''; } if ($score === null && isset($brand['lastSecurityScore']) && $brand['lastSecurityScore'] !== null && $brand['lastSecurityScore'] !== '') { $score = (int) $brand['lastSecurityScore']; } if ($at === '' && isset($brand['lastScannedAt']) && $brand['lastScannedAt']) { $at = (string) $brand['lastScannedAt']; } if ($score === null || $score < $min || $at === '') { return false; } $ts = strtotime($at); if ($ts === false) { return false; } return (time() - $ts) <= ($max_days * 86400); } /** * Tags kept when the local sample badge SVG is printed in wp-admin. * * @return array> */ private static function badge_sample_allowed_html() { $shape = array( 'id' => true, 'x' => true, 'y' => true, 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true, 'width' => true, 'height' => true, 'rx' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'offset' => true, 'stop-color' => true, 'text-anchor' => true, 'font-family' => true, 'font-size' => true, 'font-weight' => true, 'letter-spacing' => true, ); return array( 'svg' => array( 'xmlns' => true, 'width' => true, 'height' => true, 'viewbox' => true, 'role' => true, 'aria-label' => true, ), 'title' => array(), 'defs' => array(), 'lineargradient' => $shape, 'stop' => $shape, 'rect' => $shape, 'text' => $shape, ); } }