You are here

abstract class RemoteCalls in Anti Spam by CleanTalk 8.4

Same name in this branch
  1. 8.4 src/lib/Cleantalk/Common/RemoteCalls.php \Cleantalk\Common\RemoteCalls
  2. 8.4 src/lib/Cleantalk/ApbctDrupal/RemoteCalls.php \Cleantalk\ApbctDrupal\RemoteCalls
Same name and namespace in other branches
  1. 9.1.x src/lib/Cleantalk/Common/RemoteCalls.php \Cleantalk\Common\RemoteCalls

Hierarchy

Expanded class hierarchy of RemoteCalls

1 file declares its use of RemoteCalls
FirewallUpdater.php in src/lib/Cleantalk/Common/Firewall/FirewallUpdater.php

File

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

Namespace

Cleantalk\Common
View source
abstract class RemoteCalls {
  const COOLDOWN = 10;

  /**
   * @var bool
   */
  private $rc_running;

  /**
   * @var string
   */
  protected $api_key;

  /**
   *@var array
   */
  protected $default_rc = [
    'close_renew_banner' => [
      'last_call' => 0,
      'cooldown' => self::COOLDOWN,
    ],
    'sfw_update' => [
      'last_call' => 0,
      'cooldown' => self::COOLDOWN,
    ],
    'sfw_send_logs' => [
      'last_call' => 0,
      'cooldown' => self::COOLDOWN,
    ],
    'sfw_update__write_base' => [
      'last_call' => 0,
      'cooldown' => 0,
    ],
  ];

  /**
   * @var array
   */
  private $available_rc_actions;
  public function __construct($api_key) {
    $this->api_key = $api_key;
    $this->available_rc_actions = $this
      ->getAvailableRcActions();
  }

  /**
   * Checking if the current request is the Remote Call
   *
   * @return bool
   */
  public static function check() {
    return Get::get('spbc_remote_call_token') && Get::get('spbc_remote_call_action') && Get::get('plugin_name') && in_array(Get::get('plugin_name'), array(
      'antispam',
      'anti-spam',
      'apbct',
    ));
  }

  /**
   * Execute corresponding method of RemoteCalls if exists
   *
   * @return void|string
   */
  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);
  }

  /**
   * Get available remote calls from the storage.
   *
   * @return array
   */
  protected abstract function getAvailableRcActions();

  /**
   * Set last call timestamp and save it to the storage.
   *
   * @param array $action
   * @return bool
   */
  protected abstract function setLastCall($action);

}

Members

Namesort descending Modifiers Type Description Overrides
RemoteCalls::$api_key protected property
RemoteCalls::$available_rc_actions private property
RemoteCalls::$default_rc protected property
RemoteCalls::$rc_running private property
RemoteCalls::check public static function * Checking if the current request is the Remote Call * *
RemoteCalls::COOLDOWN constant
RemoteCalls::getAvailableRcActions abstract protected function Get available remote calls from the storage. 1
RemoteCalls::perform public function Execute corresponding method of RemoteCalls if exists
RemoteCalls::setLastCall abstract protected function Set last call timestamp and save it to the storage. 1
RemoteCalls::__construct public function