You are here

public function RemoteCalls::perform in Anti Spam by CleanTalk 9.1.x

Same name and namespace in other branches
  1. 8.4 src/lib/Cleantalk/Common/RemoteCalls.php \Cleantalk\Common\RemoteCalls::perform()

Execute corresponding method of RemoteCalls if exists

Return value

void|string

File

src/lib/Cleantalk/Common/RemoteCalls.php, line 73

Class

RemoteCalls

Namespace

Cleantalk\Common

Code

public function perform() {
  $action = strtolower(Get::get('spbc_remote_call_action'));
  $token = strtolower(Get::get('spbc_remote_call_token'));
  $actions = $this->available_rc_actions;
  if (count($actions) !== 0 && array_key_exists($action, $actions)) {
    $cooldown = isset($actions[$action]['cooldown']) ? $actions[$action]['cooldown'] : self::COOLDOWN;

    // Return OK for test remote calls
    if (Get::get('test')) {
      die('OK');
    }
    if (time() - $actions[$action]['last_call'] >= $cooldown) {
      $actions[$action]['last_call'] = time();
      $this
        ->setLastCall($action);

      // Check API key
      if ($token === strtolower(md5($this->api_key))) {

        // Flag to let plugin know that Remote Call is running.
        $this->rc_running = true;
        $action_method = 'action__' . $action;
        if (method_exists(static::class, $action_method)) {

          // Delay before perform action;
          if (Get::get('delay')) {
            sleep(Get::get('delay'));
          }
          $action_result = static::$action_method();
          $response = empty($action_result['error']) ? 'OK' : 'FAIL ' . json_encode(array(
            'error' => $action_result['error'],
          ));
          if (!Get::get('continue_execution')) {
            die($response);
          }
          return $response;
        }
        else {
          $out = 'FAIL ' . json_encode(array(
            'error' => 'UNKNOWN_ACTION_METHOD',
          ));
        }
      }
      else {
        $out = 'FAIL ' . json_encode(array(
          'error' => 'WRONG_TOKEN',
        ));
      }
    }
    else {
      $out = 'FAIL ' . json_encode(array(
        'error' => 'TOO_MANY_ATTEMPTS',
      ));
    }
  }
  else {
    $out = 'FAIL ' . json_encode(array(
      'error' => 'UNKNOWN_ACTION',
    ));
  }
  die($out);
}