You are here

warden.page.inc in Warden 6

Same filename and directory in other branches
  1. 7 warden.page.inc

Logic for warden output

File

warden.page.inc
View source
<?php

/**
 * @file
 * Logic for warden output
 */

/**
 * Page callback.
 *
 * Request that the site generates its site information data and sends it
 * to the configured Warden application.
 *
 * @return string
 *   Return JSON formatted status message, either OK or ERROR
 */
function warden_status_page() {

  // Do not cache this page.
  $GLOBALS['conf']['cache'] = 0;
  try {
    _warden_update_warden();
    _warden_json_output(array(
      'data' => 'OK',
    ));
  } catch (Exception $e) {
    drupal_json_output(array(
      'error' => 'Internal fault',
    ));
  }
}

/**
 * Generate all the site's data for Warden.
 *
 * @return array
 *   The site's data as an array.
 */
function _warden_generate_site_data() {
  global $base_url;
  $drupal_modules = module_list();
  $system_themes = list_themes(TRUE);
  $system_modules = array();
  foreach ($drupal_modules as $name) {
    $path = drupal_get_path('module', $name) . '/' . $name . '.info';
    $info = drupal_parse_info_file($path);
    $system_modules[$info['name']] = new stdClass();
    $system_modules[$info['name']]->info = $info;
    $system_modules[$info['name']]->filename = $path;
  }

  // Needless initialisation, but hey.
  $res = array(
    'core' => array(),
    'contrib' => array(),
    'custom' => array(),
    'url' => $base_url,
  );

  // Go over themes.
  foreach ($system_themes as $theme) {
    if (isset($theme->info['project']) && $theme->info['project'] == "drupal") {
      continue;
    }
    if (isset($theme->info['version']) && isset($theme->info['project'])) {
      $res['contrib'][$theme->info['project']] = array(
        "version" => $theme->info['version'],
      );
    }
  }

  // Go over modules.
  foreach ($system_modules as $module => $module_info) {
    $filename = $module_info->filename;

    // Match for custom modules.
    if (variable_get('warden_match_custom', TRUE)) {
      $regex = variable_get('warden_preg_match_custom', '{^sites\\/([A-z,\\.,\\-]+)\\/modules\\/custom\\/*}');
      if (preg_match($regex, $filename)) {

        // if this is part of a project, only set the project.
        if (isset($module_info->info['project'])) {
          $res['custom'][$module_info->info['project']] = array(
            "version" => $module_info->info['version'],
          );
        }
        else {
          $res['custom'][$module] = array(
            "version" => $module_info->info['version'],
          );
        }
      }
    }
    else {
      $res['custom'] = "disabled";
    }

    // Match for contrib modules.
    if (variable_get('warden_match_contrib', TRUE)) {
      $regex = variable_get('warden_preg_match_contrib', '{^sites\\/([A-z,\\.,\\-]+)\\/modules\\/contrib\\/*}');
      if (preg_match($regex, $filename)) {

        // if this is part of a project, only set the project.
        if (isset($module_info->info['project'])) {
          $res['contrib'][$module_info->info['project']] = array(
            "version" => $module_info->info['version'],
          );
        }
        else {
          $res['contrib'][$module] = array(
            "version" => $module_info->info['version'],
          );
        }
      }
    }
    else {
      $res['contrib'] = "disabled";
    }

    // Core modules.
    if (variable_get('warden_match_core', TRUE)) {
      if ($module_info->info['package'] == "Core - required") {
        $res['core'][$module] = array(
          "version" => $module_info->info['version'],
        );
      }
    }
    else {
      $res['core'] = "disabled";
    }
  }

  // Include Drupal core version.
  if (!isset($res['core']['drupal'])) {
    $res['core']['drupal'] = array(
      "version" => VERSION,
    );
  }

  // Site name.
  $res['site_name'] = variable_get('site_name', $base_url);

  // Sign the request with our local key and the timestamp.
  $res['key'] = _warden_get_local_token();
  $res['time'] = time();
  return $res;
}

/**
 * Update Warden with latest site data.
 *
 * @throws Exception
 *   If any problems occur.
 */
function _warden_update_warden() {
  $data = _warden_generate_site_data();
  warden_get_api()
    ->postSiteData($data);
}
function _warden_json_output($data) {
  header('Content-Type: application/json');
  echo json_encode($data);
  exit;
}

Functions

Namesort descending Description
warden_status_page Page callback.
_warden_generate_site_data Generate all the site's data for Warden.
_warden_json_output
_warden_update_warden Update Warden with latest site data.