HEX
Server: Apache
System: Linux localhost.localdomain 4.15.0-213-generic #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023 x86_64
User: web57 (5040)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/clients/client6/web57/web/wffence/wp-cli-wordfence/src/WordfenceApi.php
<?php

namespace GeneroWP\WpCliWordfence;

use Generator;
use GeneroWP\WpCliWordfence\Models\Record;

/**
 * @see https://www.wordfence.com/intelligence-documentation/v2-accessing-and-consuming-the-vulnerability-data-feed/
 */
class WordfenceApi
{
    const WORDFENCE_SCANNER_ENDPOINT = 'https://www.wordfence.com/api/intelligence/v2/vulnerabilities/scanner';

    protected string $endpoint;

    /**
     * @param array<string,mixed> $headers
     */
    public function __construct(
        protected array $headers = [],
    ) {
        $this->endpoint = self::WORDFENCE_SCANNER_ENDPOINT;
    }

    /**
     * @return Generator|Record[]
     */
    public function getKnownVulnerabilities(): Generator
    {
        $result = $this->getResponse();
        if (is_null($result)) {
            return;
        }
        foreach ($result as $data) {
            yield Record::fromRecord($data);
        }
    }

    /**
     * @throws ApiException
     * @return null|array<string,mixed>
     */
    protected function getResponse(): ?array
    {
        $response = wp_remote_get(self::WORDFENCE_SCANNER_ENDPOINT, [
            'timeout' => 30,
            'headers' => array_merge([
                'Accept' => 'application/json',
            ], $this->headers),
        ]);

        if (is_wp_error($response)) {
            throw new ApiException($response->get_error_message());
        }

        $reponseCode = (int) wp_remote_retrieve_response_code($response);
        switch ($reponseCode) {
            case 200: // OK
                $result = json_decode(wp_remote_retrieve_body($response), true);

                if (json_last_error() !== JSON_ERROR_NONE) {
                    throw new ApiException(sprintf('JSON error: %s', json_last_error_msg()));
                }

                return $result;
            case 304: // Not modified
                return null;
            default:
                throw new ApiException(sprintf('Unknown response code: %s', $reponseCode));
        }
    }
}