Invoke www.stopforumspam.com's api with multiple usernames, emails, and ips.

Note: Results in $data are not guaranteed to be in the same order as the request in $query when caching is enabled.

Parameters

array $query: An associative array indexed by query type ('email', username', and/or 'ip', each an array of values to be queried). For example: ['email' => ['blah@blah.com', 'blah2@blah2.com']].

array $data: An array that will be filled with the data from www.stopforumspam.com.

Return value

bool TRUE on successful request (and $data will contain the data) FALSE otherwise.

1 call to spambot_sfs_request_multiple()
spambot_sfs_request in ./spambot.module
Invoke www.stopforumspam.com's api with single username, email, and/or ip.

File

./spambot.module, line 462

Code

function spambot_sfs_request_multiple(array $query, array &$data) {
    // An empty request results in no match.
    if (empty($query)) {
        return FALSE;
    }
    // Attempt to return a response from the cache bins if cache is enabled.
    $config = \Drupal::config('spambot.settings');
    $cache_enabled = $config->get('spambot_enable_cache');
    $cache_data = [];
    if ($cache_enabled) {
        // For each query type, see if each value is present in the cache, and if so
        // retain it in $cache_data and remove it from the query.
        foreach ([
            'email',
            'username',
            'ip',
        ] as $field_name) {
            foreach ($query[$field_name] ?? [] as $index => $query_datum) {
                $cache_dataum = \Drupal::cache('spambot')->get("{$field_name}:{$query_datum}");
                if ($cache_dataum) {
                    $cache_data[$field_name][$index] = $cache_dataum->data;
                    unset($query[$field_name][$index]);
                }
            }
            if (empty($query[$field_name])) {
                unset($query[$field_name]);
            }
        }
        // Serve only a cached response if one exists.
        if (empty($query)) {
            $data = $cache_data;
            $data['success'] = TRUE;
            return TRUE;
        }
    }
    $method = $config->get('use_https') ? 'https' : 'http';
    // Use php serialisation format.
    $query['f'] = 'serial';
    $url = $method . '://www.stopforumspam.com/api?' . urldecode(http_build_query($query, '', '&'));
    $response = \Drupal::httpClient()->get($url, [
        'headers' => [
            'Accept' => 'text/plain',
        ],
    ]);
    $status_code = $response->getStatusCode();
    if ($status_code == 200) {
        $data = unserialize($response->getBody()
            ->getContents());
        // Store responses to the cache for fast lookups.
        if ($cache_enabled) {
            $expire = $config->get('spambot_cache_expire');
            $expire = $expire != CacheBackendInterface::CACHE_PERMANENT ? time() + $expire : CacheBackendInterface::CACHE_PERMANENT;
            $expire_false = $config->get('spambot_cache_expire_false');
            $expire_false = $expire_false != CacheBackendInterface::CACHE_PERMANENT ? time() + $expire_false : CacheBackendInterface::CACHE_PERMANENT;
            foreach ([
                'email',
                'username',
                'ip',
            ] as $field_name) {
                foreach ($data[$field_name] ?? [] as $result) {
                    $expire_email = $result['appears'] ? $expire : $expire_false;
                    \Drupal::cache('spambot')->set("{$field_name}:{$result['value']}", $result, $expire_email);
                }
            }
        }
        // Merge in cached results.
        $data = array_merge_recursive($data, $cache_data);
        $vars = [
            '%url' => $url,
            '%data' => serialize($data),
        ];
        if (!empty($data['success'])) {
            \Drupal::logger('spambot')->notice("Success: %url %data", $vars);
            return TRUE;
        }
        else {
            \Drupal::logger('spambot')->notice("Request unsuccessful: %url %data", $vars);
        }
    }
    else {
        \Drupal::logger('spambot')->error("Error contacting service: %url", [
            '%url' => $url,
        ]);
    }
    return FALSE;
}