You are here

class DomainsReceive in DRD Agent 8.3

Same name and namespace in other branches
  1. 4.0.x src/Agent/Action/DomainsReceive.php \Drupal\drd_agent\Agent\Action\DomainsReceive

Provides a 'DomainsReceive' code.

Hierarchy

Expanded class hierarchy of DomainsReceive

File

src/Agent/Action/DomainsReceive.php, line 11

Namespace

Drupal\drd_agent\Agent\Action
View source
class DomainsReceive extends Base {

  /**
   * {@inheritdoc}
   */
  public function execute() {
    $domains = [];
    foreach ($this
      ->readSites() as $uri => $shortname) {
      $file = DRUPAL_ROOT . '/sites/' . $shortname . '/settings.php';
      if (!file_exists($file)) {
        continue;
      }
      if (isset($domains[$shortname])) {
        $domains[$shortname]['aliase'][] = $uri;
      }
      else {
        $domains[$shortname] = [
          'uri' => $uri,
          'aliase' => [],
        ];
      }
    }
    return $domains;
  }

  /**
   * Determines all available sites/domains in the current Drupal installation.
   *
   * @return array
   *   An array with key/value pairs where key is the domain name and value the
   *   shortname of a directory in DRUPAL_ROOT/sites/ for where to find the
   *   settings.php file for that domain.
   */
  private function readSites() : array {
    $sites = [];
    if (file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
      try {

        /** @noinspection PhpIncludeInspection */
        include DRUPAL_ROOT . '/sites/sites.php';
      } catch (Exception $e) {

        // Ignore.
      }
    }
    if (empty($sites)) {
      foreach (scandir(DRUPAL_ROOT . '/sites') as $shortname) {
        if (is_dir(DRUPAL_ROOT . '/sites/' . $shortname) && !in_array($shortname, [
          '.',
          '..',
          'all',
        ])) {
          $file = DRUPAL_ROOT . '/sites/' . $shortname . '/settings.php';
          if (file_exists($file)) {
            list($base_url, ) = $this
              ->readSettings($shortname, $file);
            if (empty($base_url)) {
              $this
                ->watchdog('Reading Sites - Failed as url is empty: @shortname', [
                '@shortname' => $shortname,
              ], LogLevel::ERROR);
              continue;
            }
            $pos = strpos($base_url, '://');
            if ($pos > 0) {
              $base_url = substr($base_url, $pos + 3);
            }
            $sites[$base_url] = $shortname;
          }
        }
      }
    }
    if (empty($sites)) {
      $base_url = $GLOBALS['base_url'];
      $sites[$base_url] = 'default';
    }
    $this
      ->watchdog('Reading Sites - Found @n entries: <pre>@list</pre>', [
      '@n' => count($sites),
      '@list' => print_r($sites, TRUE),
    ]);
    return $sites;
  }

  /**
   * Safely read the settings.php file and return the relevant variables.
   *
   * @param string $shortname
   *   Name of the subdirectory in Drupal's site directory.
   * @param string $file
   *   Full path and filename to the settings.php which whould be read.
   *
   * @return array
   *   An array containing the base url and database settings.
   */
  private function readSettings($shortname, $file) : array {

    // The following 2 variables may be required due to Drupal's
    // default.settings.php since version 8.2.x.
    // @see also https://www.drupal.org/node/2911759

    /* @noinspection PhpUnusedLocalVariableInspection */
    $app_root = $this->container
      ->get('app.root');

    /* @noinspection PhpUnusedLocalVariableInspection */
    $site_path = 'sites/' . $shortname;

    /* @noinspection PhpUnusedLocalVariableInspection */
    $class_loader = new DummyClassLoader();
    $base_url = '';
    $databases = [];
    try {
      $php = php_strip_whitespace($file);
      $php = str_replace([
        '<?php',
        '<?',
        '?>',
        'ini_set',
        '@@ini_set',
      ], [
        '',
        '',
        '',
        '@ini_set',
        '@ini_set',
      ], $php);
      file_put_contents('temporary://drd-test.php', $php);
      eval($php);
    } catch (Exception $e) {

      // Ignore it.
      $this
        ->watchdog('Read Settings - Exception occured:<pre>@exception</pre>', [
        '@exception' => print_r($e, TRUE),
      ], LogLevel::ERROR);
      return [
        '',
        '',
      ];
    }
    if (empty($base_url)) {
      if ($shortname === 'default') {
        $base_url = $GLOBALS['base_url'];
      }
      else {
        $base_url = $shortname;
      }
    }
    return [
      $base_url,
      $databases,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Base::$accountSwitcher protected property
Base::$arguments private property
Base::$configFactory protected property
Base::$container protected property
Base::$crypt protected property Crypt object for this DRD request.
Base::$database protected property
Base::$debugMode private property
Base::$entityTypeManager protected property
Base::$fileSystem protected property
Base::$logger protected property
Base::$messenger protected property
Base::$moduleHandler protected property
Base::$state protected property
Base::$time protected property
Base::authenticate private function Authenticate the request or throw an exception.
Base::authorize public function Authorize the DRD instance, all validations have passed successfully. Overrides BaseInterface::authorize
Base::authorizeBySecret public function Callback to authorize a DRD instance with a given secret.
Base::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Base::getArguments public function Get the arguments for this request. Overrides BaseInterface::getArguments
Base::getCryptInstance public function Get authorised Crypt object or FALSE if none is available. Overrides BaseInterface::getCryptInstance
Base::getDbInfo public function Get an array of database connection information. Overrides BaseInterface::getDbInfo
Base::getDebugMode public function Get the debug mode. Overrides BaseInterface::getDebugMode
Base::getMessages public function Overrides BaseInterface::getMessages
Base::init public function Overrides BaseInterface::init
Base::ott public function Validate a one-time-token. Overrides BaseInterface::ott
Base::promoteUser public function Change current session to user 1. Overrides BaseInterface::promoteUser
Base::readInput private function Read and decode the input from the POST request.
Base::realPath public function Overrides BaseInterface::realPath
Base::run public function Main callback to execute an action.
Base::SEC_AUTH_ACQUIA constant
Base::SEC_AUTH_PANTHEON constant
Base::SEC_AUTH_PLATFORMSH constant
Base::setDebugMode public function Set the debug mode. Overrides BaseInterface::setDebugMode
Base::toArray private function Recursivly convert request arguments to an array.
Base::watchdog public function Logging if in debug mode. Overrides BaseInterface::watchdog
Base::__construct public function Base constructor.
DomainsReceive::execute public function Execute an action. Overrides Base::execute
DomainsReceive::readSettings private function Safely read the settings.php file and return the relevant variables.
DomainsReceive::readSites private function Determines all available sites/domains in the current Drupal installation.