You are here

public function SFW::sfw_update in Anti Spam by CleanTalk 8.3

File

src/lib/Cleantalk/Antispam/SFW.php, line 136

Class

SFW

Namespace

Drupal\cleantalk\lib\Cleantalk\Antispam

Code

public function sfw_update($file_url_hash = null, $file_url_num = null) {
  if (!isset($file_url_hash, $file_url_num)) {
    $result = CleantalkAPI::method__get_2s_blacklists_db($this->api_key, 'multifiles', '2_0');
    sleep(3);
    if (empty($result['error'])) {
      if (!empty($result['file_url'])) {
        if (CleantalkHelper::http__request($result['file_url'], array(), 'get_code') === 200) {
          if (ini_get('allow_url_fopen')) {
            $pattenrs = array();
            $pattenrs = array(
              'get',
              'async',
            );
            $base_host_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'];
            $this
              ->universal_query("TRUNCATE TABLE " . $this->table_prefix . "cleantalk_sfw");
            if (preg_match('/multifiles/', $result['file_url'])) {
              $gf = gzopen($result['file_url'], 'rb');
              if ($gf) {
                $file_url_nums = array();
                while (!gzeof($gf)) {
                  $file_url = trim(gzgets($gf, 1024));
                  $file_url_nums[] = preg_replace('@(https://.*)\\.(\\d*)(\\.csv\\.gz)@', '$2', $file_url);
                  if (!$file_url_hash) {
                    $file_url_hash = preg_replace('@(https://.*)\\.(\\d*)(\\.csv\\.gz)@', '$1', $file_url);
                  }
                }
                return CleantalkHelper::http__request($base_host_url, array(
                  'spbc_remote_call_token' => md5($this->api_key),
                  'spbc_remote_call_action' => 'sfw_update',
                  'plugin_name' => 'apbct',
                  'file_url_hash' => $file_url_hash,
                  'file_url_nums' => implode(',', $file_url_nums),
                ), $pattenrs);
              }
            }
            else {
              return array(
                'error' => 'COULD_NOT_GET_MULTIFILE',
              );
            }
          }
          else {
            return array(
              'error' => 'ERROR_ALLOW_URL_FOPEN_DISABLED',
            );
          }
        }
      }
      else {
        return array(
          'error' => 'BAD_RESPONSE',
        );
      }
    }
    else {
      return $result;
    }
  }
  elseif (isset($file_url_hash, $file_url_num)) {
    $file_url = $file_url_hash . '.' . $file_url_num . '.csv.gz';
    if (CleantalkHelper::http__request($file_url, array(), 'get_code') === 200) {

      // Check if it's there
      $gf = gzopen($file_url, 'rb');
      if ($gf) {
        if (!gzeof($gf)) {
          for ($count_result = 0; !gzeof($gf);) {
            $query = "INSERT INTO " . $this->table_prefix . "cleantalk_sfw VALUES %s";
            for ($i = 0, $values = array(); 5000 !== $i && !gzeof($gf); $i++, $count_result++) {
              $entry = trim(gzgets($gf, 1024));
              if (empty($entry)) {
                continue;
              }
              $entry = explode(',', $entry);

              // Cast result to int
              $ip = preg_replace('/[^\\d]*/', '', $entry[0]);
              $mask = preg_replace('/[^\\d]*/', '', $entry[1]);
              $private = isset($entry[2]) ? $entry[2] : 0;
              if (!$ip || !$mask) {
                continue;
              }
              $values[] = '(' . $ip . ',' . $mask . ', ' . $private . ')';
            }
            if (!empty($values)) {
              $query = sprintf($query, implode(',', $values) . ';');
              $this
                ->universal_query($query);
            }
          }
          gzclose($gf);
          return $count_result;
        }
        else {
          return array(
            'error' => 'ERROR_GZ_EMPTY',
          );
        }
      }
      else {
        return array(
          'error' => 'ERROR_OPEN_GZ_FILE',
        );
      }
    }
    else {
      return array(
        'error' => 'NO_REMOTE_FILE_FOUND',
      );
    }
  }
}