You are here

drd_agent.module in DRD Agent 6.3

Same filename and directory in other branches
  1. 7.3 drd_agent.module

File

drd_agent.module
View source
<?php

define('DRD_AGENT_LIB_BASE_URI', 'https://git.drupalcode.org/project/drd_agent_lib/raw/master/');
if (!defined('DRUPAL_ROOT')) {
  define('DRUPAL_ROOT', getcwd());
}
if (!defined('REQUEST_TIME')) {
  define('REQUEST_TIME', time());
}

/**
 * Implements hook_menu().
 *
 * @return array
 *   An array of all defined menu items by this module.
 */
function drd_agent_menu() {
  $items['drd-agent'] = array(
    'title' => 'DRD Agent Callback',
    'page callback' => 'drd_agent',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['drd-agent-crypt'] = array(
    'title' => 'DRD Agent Callback',
    'page callback' => 'drd_agent_crypt',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['drd-agent-authorize-secret'] = array(
    'title' => 'Authorize Dashboard',
    'page callback' => 'drd_agent_authorize_secret',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['drd-agent-authorize'] = array(
    'title' => 'Authorize Dashboard',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'drd_agent_authorize',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'drd_agent.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/drd'] = array(
    'title' => 'DRD',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'drd_agent_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'drd_agent.admin.inc',
  );
  return $items;
}

/**
 * Callback to download the DRD library if required,
 *
 * @param bool $force
 *
 * @throws \Exception
 */
function _drd_get_lib($force = FALSE) {
  $archive = 'drd-' . $_SERVER['HTTP_X_DRD_VERSION'] . '.phar';
  $uri = file_directory_temp() . '/' . $archive;
  if ($force || !file_exists($uri)) {
    $response = drupal_http_request(DRD_AGENT_LIB_BASE_URI . $archive);
    if (empty($response->code) || $response->code != 200) {
      throw new Exception('DRD Library not available');
    }
    file_put_contents($uri, $response->data);
  }
  require_once $uri;
  if (!defined('DRD_BASE')) {

    // Looks like loading the phar file failed. We've seen this on a Plesk and
    // OpCache combination (@see https://www.drupal.org/node/2892316) and hence
    // we try to disable opcache only for this request and load the phar again.
    ini_set('opcache.enable', 0);
    require $uri;
  }
}

/**
 * Menu callback to execute the remote request for an action.
 *
 * @throws \Exception
 */
function drd_agent() {
  _drd_get_lib();
  drd_agent_deliver(\Drupal\drd\Agent\Action\Base::run(6, variable_get('drd_agent_debug_mode', FALSE)));
}

/**
 * Menu callback to return an array containing supported crypt methods.
 *
 * @throws \Exception
 */
function drd_agent_crypt() {
  _drd_get_lib();
  drd_agent_deliver(base64_encode(json_encode(\Drupal\drd\Crypt\Base::getMethods())));
}

/**
 * Menu callback to authorize a DRD instance by a secret.
 *
 * @throws \Exception
 */
function drd_agent_authorize_secret() {
  _drd_get_lib();
  drd_agent_deliver(\Drupal\drd\Agent\Action\Base::authorizeBySecret(6, variable_get('drd_agent_debug_mode', FALSE)));
}

/**
 * Callback to deliver the result of the action in json format.
 *
 * @param $page_callback_result
 */
function drd_agent_deliver($page_callback_result) {
  drupal_set_header('Content-Type: text/plain; charset=utf-8');
  drupal_set_header('X-DRD-Agent: ' . $_SERVER['HTTP_X_DRD_VERSION']);
  print $page_callback_result;
  drupal_page_footer();
  exit;
}

Functions

Namesort descending Description
drd_agent Menu callback to execute the remote request for an action.
drd_agent_authorize_secret Menu callback to authorize a DRD instance by a secret.
drd_agent_crypt Menu callback to return an array containing supported crypt methods.
drd_agent_deliver Callback to deliver the result of the action in json format.
drd_agent_menu Implements hook_menu().
_drd_get_lib Callback to download the DRD library if required,

Constants