Same name and namespace in other branches
  1. 7.x-1.x spambot.module \spambot_report_account() 1 comment

Reports an account as a spammer.

Requires ip address and evidence of a single incident.

Parameters

object $account: Account to report.

string $ip: IP address to report.

string $evidence: Evidence to report.

bool $key: Api_key from config.

Return value

bool TRUE if successful, FALSE if error

4 calls to spambot_report_account()
SpambotUserspamForm::actionSubmit in src/Form/SpambotUserspamForm.php
Function provide functional for button "take action".
SpambotUserspamTest::testSpambotReportAccountEmptyApiKey in tests/src/Kernel/SpambotUserspamTest.php
Test spambot_report_account() function without "api_key".
SpambotUserspamTest::testSpambotReportAccountIncorrectApiKey in tests/src/Kernel/SpambotUserspamTest.php
Test spambot_report_account() function with incorrect "api_key".
SpambotUserspamTest::testSpambotReportSpamAccount in tests/src/Kernel/SpambotUserspamTest.php
Test spambot_report_account() function with correct "api_key".

File

./spambot.module, line 567

Code

function spambot_report_account($account, $ip, $evidence, $key = FALSE) {
    $success = FALSE;
    if ($key) {
        $query['api_key'] = $key;
        $query['email'] = $account->getEmail();
        $query['username'] = $account->getAccountName();
        $query['ip_addr'] = $ip;
        $query['evidence'] = Unicode::truncate($evidence, SPAMBOT_MAX_EVIDENCE_LENGTH);
        $config = \Drupal::config('spambot.settings');
        $method = $config->get('use_https') ? 'https' : 'http';
        $uri = $method . '://www.stopforumspam.com/add.php';
        $options = [
            'headers' => [
                'Content-type' => 'application/x-www-form-urlencoded',
            ],
            'form_params' => $query,
        ];
        try {
            $result = \Drupal::httpClient()->request('POST', $uri, $options);
        } catch (Exception $e) {
            return FALSE;
        }
        $data = !empty($result) ? $result->getBody()
            ->getContents() : '';
        if (!empty($result->getStatusCode()) && $result->getStatusCode() == 200 && !empty($data) && stripos($data, 'data submitted successfully') !== FALSE) {
            $success = TRUE;
        }
        elseif (stripos($data, 'duplicate') !== FALSE) {
            // www.stopforumspam.com can return a 503 code
            // with data = '<p>recent duplicate entry</p>'
            // which we will treat as successful.
            $success = TRUE;
        }
        else {
            \Drupal::logger('spambot')->notice("Error reporting account: %url <pre>\n@dump</pre>", [
                '%url' => Url::fromUri($uri),
                '@dump' => print_r($result, TRUE),
            ]);
        }
    }
    return $success;
}