You are here

private function Cleantalk::httpRequest in Anti Spam by CleanTalk 7

Same name and namespace in other branches
  1. 7.5 src/Cleantalk.php \Cleantalk::httpRequest()
  2. 7.2 cleantalk.module \Cleantalk::httpRequest()
  3. 7.4 src/Cleantalk.php \Cleantalk::httpRequest()

httpRequest

Parameters

$msg:

Return value

boolean|\CleantalkResponse

3 calls to Cleantalk::httpRequest()
Cleantalk::isAllowMessage in ./cleantalk.module
Function checks whether it is possible to publish the message
Cleantalk::isAllowUser in ./cleantalk.module
Function checks whether it is possible to publish the message
Cleantalk::sendFeedback in ./cleantalk.module
Function sends the results of manual moderation

File

./cleantalk.module, line 688
Main CleanTalk integration module functions.

Class

Cleantalk
Cleantalk class create request

Code

private function httpRequest($msg) {
  $result = false;
  $msg->all_headers = drupal_json_encode(apache_request_headers());

  //$msg->remote_addr=$_SERVER['REMOTE_ADDR'];

  //$msg->sender_info['remote_addr']=$_SERVER['REMOTE_ADDR'];
  $si = drupal_json_decode($msg->sender_info);
  $si['remote_addr'] = $_SERVER['REMOTE_ADDR'];
  $msg->sender_info = drupal_json_encode($si);
  if (isset($this->work_url) && $this->work_url !== '' && $this->server_changed + $this->server_ttl > time() || $this->stay_on_server == true) {
    $url = !empty($this->work_url) ? $this->work_url : $this->server_url;
    $result = $this
      ->sendRequest($msg, $url, $this->server_timeout);
  }
  if (($result === false || $result->errno != 0) && $this->stay_on_server == false) {

    // Split server url to parts
    preg_match("@^(https?://)([^/:]+)(.*)@i", $this->server_url, $matches);
    $url_prefix = '';
    if (isset($matches[1])) {
      $url_prefix = $matches[1];
    }
    $pool = null;
    if (isset($matches[2])) {
      $pool = $matches[2];
    }
    $url_suffix = '';
    if (isset($matches[3])) {
      $url_suffix = $matches[3];
    }
    if ($url_prefix === '') {
      $url_prefix = 'http://';
    }
    if (empty($pool)) {
      return false;
    }
    else {

      // Loop until find work server
      foreach ($this
        ->get_servers_ip($pool) as $server) {
        if ($server['host'] === 'localhost' || $server['ip'] === null) {
          $work_url = $server['host'];
        }
        else {
          $server_host = $server['ip'];
          $work_url = $server_host;
        }
        $work_url = $url_prefix . $work_url;
        if (isset($url_suffix)) {
          $work_url = $work_url . $url_suffix;
        }
        $this->work_url = $work_url;
        $this->server_ttl = $server['ttl'];
        $result = $this
          ->sendRequest($msg, $this->work_url, $this->server_timeout);
        if ($result !== false && $result->errno === 0) {
          $this->server_change = true;
          break;
        }
      }
    }
  }
  $response = new CleantalkResponse(null, $result);
  if (!empty($this->data_codepage) && $this->data_codepage !== 'UTF-8') {
    if (!empty($response->comment)) {
      $response->comment = $this
        ->stringFromUTF8($response->comment, $this->data_codepage);
    }
    if (!empty($response->errstr)) {
      $response->errstr = $this
        ->stringFromUTF8($response->errstr, $this->data_codepage);
    }
    if (!empty($response->sms_error_text)) {
      $response->sms_error_text = $this
        ->stringFromUTF8($response->sms_error_text, $this->data_codepage);
    }
  }
  return $response;
}