RemoteCalls.php in Anti Spam by CleanTalk 8.4
File
src/lib/Cleantalk/Common/RemoteCalls.php
View source
<?php
namespace Cleantalk\Common;
use Cleantalk\Common\Variables\Get;
abstract class RemoteCalls {
const COOLDOWN = 10;
private $rc_running;
protected $api_key;
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,
],
];
private $available_rc_actions;
public function __construct($api_key) {
$this->api_key = $api_key;
$this->available_rc_actions = $this
->getAvailableRcActions();
}
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',
));
}
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;
if (Get::get('test')) {
die('OK');
}
if (time() - $actions[$action]['last_call'] >= $cooldown) {
$actions[$action]['last_call'] = time();
$this
->setLastCall($action);
if ($token === strtolower(md5($this->api_key))) {
$this->rc_running = true;
$action_method = 'action__' . $action;
if (method_exists(static::class, $action_method)) {
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);
}
protected abstract function getAvailableRcActions();
protected abstract function setLastCall($action);
}