You are here

public function Base::run in DRD Agent 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Agent/Action/Base.php \Drupal\drd_agent\Agent\Action\Base::run()

Main callback to execute an action.

Parameters

bool $debugMode: Whether we operate in debug mode.

Return value

string|mixed Encrypted and base64 encoded result from the executed action.

File

src/Agent/Action/Base.php, line 210

Class

Base
Base class for Remote DRD Action Code.

Namespace

Drupal\drd_agent\Agent\Action

Code

public function run($debugMode = FALSE) {
  try {
    $input = $this
      ->readInput($debugMode);
    if (empty($input['uuid']) || empty($input['args']) || !isset($input['iv'])) {
      throw new RuntimeException('Input is incomplete');
    }
    $input['args'] = base64_decode($input['args']);
    $input['iv'] = base64_decode($input['iv']);
    if (!empty($input['ott']) && !empty($input['config'])) {
      if (!$this
        ->ott($input['ott'], $input['config'])) {
        throw new RuntimeException('OTT config failed');
      }
      return 'ok';
    }
    if (!empty($input['auth']) && !empty($input['authsetting'])) {
      $this
        ->authenticate($input['uuid'], $input);
    }
    $this->crypt = $this
      ->getCryptInstance($input['uuid']);
    if (!$this->crypt) {
      throw new RuntimeException('Encryption method not available or unauthorised');
    }
    $args = $this
      ->toArray($this->crypt
      ->decrypt($input['args'], $input['iv']));
    if (empty($args['auth']) || !isset($args['authsetting']) || empty($args['action'])) {
      throw new RuntimeException('Arguments incomplete');
    }
    if (empty($input['auth'])) {

      // Let's authenticate here if we haven't yet authenticated
      // before decryption.
      $this
        ->authenticate($input['uuid'], $args);
    }
    $action = $args['action'];
    $actionModule = $args['drd_action_module'];
    if ($actionModule === 'drd') {
      $actionModule = 'drd_agent';
    }
    if (isset($args['drd_action_plugin'])) {
      $actionFile = $this
        ->realPath('temporary://drd_agent_' . $action . '.php');
      file_put_contents($actionFile, $args['drd_action_plugin']);
      unset($args['drd_action_plugin']);

      /** @noinspection PhpIncludeInspection */
      require_once $actionFile;
    }
    unset($args['auth'], $args['authsetting'], $args['action'], $args['drd_action_module']);
    $this->arguments = $args;
  } catch (Exception $ex) {
    $this
      ->watchdog($ex
      ->getMessage(), array(), 3);
    header('HTTP/1.1 502 Error');
    print 'error';
    exit;
  }
  try {
    $this
      ->promoteUser();
    $classname = "\\Drupal\\{$actionModule}\\Agent\\Action\\{$action}";

    /** @var \Drupal\drd_agent\Agent\Action\BaseInterface $actionObject */

    /** @noinspection PhpUndefinedMethodInspection */
    $actionObject = $classname::create($this->container);
    $actionObject
      ->init($this->crypt, $this->arguments, $this->debugMode);
  } catch (Exception $ex) {
    $this
      ->watchdog('Not yet implemented: ' . $action, array(), 3);
    header('HTTP/1.1 403 Not found');
    print 'Not yet implemented';
    exit;
  }
  $result = $actionObject
    ->execute();
  if (is_array($result)) {
    $result['messages'] = $this
      ->getMessages();
    return base64_encode($this->crypt
      ->encrypt($result));
  }
  return $result;
}