You are here

drd_server.module in Drupal Remote Dashboard Server 7

Same filename and directory in other branches
  1. 6.2 drd_server.module
  2. 6 drd_server.module
  3. 7.2 drd_server.module

File

drd_server.module
View source
<?php

define('DRD_API_VERSION', '1.0.20');

/** ======================================================================
 *
 * Hooks
 *
 */

/**
 * Implementation of hook_xmlrpc().
 *
 * This function provides Drupal with an array to map XML-RPC callbacks to the
 * functions implemented by this module.
 */
function drd_server_xmlrpc() {
  include_once 'drd_server.domain.inc';
  include_once 'drd_server.server.inc';
  return array(
    'drd.api' => 'drd_server_check_api',
    'drd.connect' => 'drd_server_connect',
    'drd.session.valid' => 'drd_server_check_session',
    'drd.info' => 'drd_server_domain_info',
    'drd.cache.flush' => 'drd_server_domain_flush_cache',
    'drd.run.cron' => 'drd_server_domain_run_cron',
    'drd.switch.maintenance' => 'drd_server_domain_switch_maintenance',
    'drd.list.updates' => 'drd_server_domain_list_updates',
    'drd.run.update' => 'drd_server_domain_run_update',
    'drd.update.translation' => 'drd_server_domain_update_translation',
    'drd.server.domains' => 'drd_server_server_domains',
    'drd.server.svn.status' => 'drd_server_server_svn_status',
    'drd.server.svn.update' => 'drd_server_server_svn_update',
    'drd.server.php.error.log' => 'drd_server_server_php_error_log',
  );
}

/** ======================================================================
 *
 * Remote client functions
 *
 */
function drd_server_check_api($api) {
  return drd_server_result('api', $api == DRD_API_VERSION);
}

/**
 * Authenticate a user.
 *
 * This function is used to authenticate a user with the associated password
 * and if successful returns the session ID for future access.
 *
 * @param $username
 *   The name of the user to be authenticated.
 * @param $password
 *   The password of the user to be authenticated.
 * @return
 *   The session ID if the user got successfully authenticated, an error message
 *   otherwise.
 */
function drd_server_connect($username, $password) {
  $form_state['values']['name'] = $username;
  $form_state['values']['pass'] = $password;
  $form_state['values']['op'] = t('Login');
  $GLOBALS['drd_xmlrpc_mode'] = TRUE;
  drupal_form_submit('user_login', $form_state);
  if (!form_get_errors()) {
    if (user_access('administer site configuration')) {
      return drd_server_result('connect', session_id());
    }
    else {
      return drd_server_error(t('You do not have permission to access system data.'));
    }
  }
  else {
    return drd_server_error(t('Wrong username or password.'));
  }
}

/**
 * Get the session by ID.
 *
 * This function returns the account for the given session or NULL.
 *
 * @param $sid
 *   The session ID that was returned by drd_server_connect().
 * @return
 *   Returns the account object or NULL if session ID wasn't found.
 *
 * @see drd_server_connect
 */
function drd_server_get_account_by_session($sid) {
  try {
    $account = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", array(
      ':sid' => $sid,
    ))
      ->fetchObject();
  } catch (Exception $e) {
  }
  return $account;
}

/**
 * Check the session ID if it's still valid.
 *
 * This function tests if the given session is still valid.
 *
 * @param $sid
 *   The session ID that was returned by drd_server_connect().
 * @return
 *   Returns TRUE if session ID is valid.
 *
 * @see drd_server_connect
 */
function drd_server_check_session($sid) {
  $account = drd_server_get_account_by_session($sid);
  return drd_server_result('session.valid', isset($account) && $account && $account->uid > 0);
}

/**
 * Load user for session and check permission.
 *
 * This function loads the user from the given session ID and checks
 * his/her the permissions to access admin configuration of the site.
 *
 * @param $sid
 *   The session ID that was returned by drd_server_connect().
 * @return
 *   Returns the global user object if session is valid and if permissions
 *   are sufficient, an error message otherwise.
 *
 * @see drd_server_connect
 */
function drd_server_load_user($sid) {
  $account = drd_server_get_account_by_session($sid);
  if (isset($account) && $account && $account->uid > 0) {
    $account = drupal_unpack($account);
    $account->roles = array();
    $account->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
    $account->roles += db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = :uid", array(
      ':uid' => $account->uid,
    ))
      ->fetchAllKeyed(0, 1);
  }
  else {
    return t('You are not logged in.');
  }
  if (user_access('administer site configuration', $account)) {
    global $user;
    $user = $account;
    return $user;
  }
  return t('You do not have permission to access system data.');
}

/**
 * Prepare the result for returning it to the XMLRPC caller.
 *
 * @param $mode
 *   The function that was called by the XMLRPC caller.
 * @param $drd_result
 *   The result from the drd_server functions to be returned after hooks and alterers have been called.
 * @param $args
 *   More arguments if available and/or required.
 * @return
 *   Returns the final result to be sent to the XMLRPC caller.
 */
function drd_server_result() {
  $args = func_get_args();
  $mode = $args[0];
  $drd_result = $args[1];
  unset($args[0], $args[1]);
  foreach (module_implements('drd_server') as $module) {
    $drd_result = module_invoke($module, 'drd_server', $mode, $drd_result, $args);
  }
  drupal_alter('drd_server', $mode, $drd_result);
  $result = new stdClass();
  $result->is_error = FALSE;
  $result->message = $drd_result;
  return drupal_json_encode($result);
}

/**
 * Prepare an error message for returning to the XMLRPC caller.
 *
 * @param $message
 *   The message to be sent to the XMLRPC caller.
 * @return
 *   Returns the encoded message.
 */
function drd_server_error($message) {
  static $xmlrpcusererr;
  if (!is_array($message)) {
    $message = array(
      $message,
    );
  }
  $message = implode(' ', $message);
  return drupal_json_encode(xmlrpc_error($xmlrpcusererr + 1, strip_tags($message)));
}

/**
 * Implements hook_block_info().
 */
function drd_server_block_info() {
  $blocks = array();
  if (module_exists('admin')) {
    $blocks['drd'] = array(
      'info' => t('Admin Extras Dashboard'),
      'cache' => DRUPAL_CACHE_PER_ROLE,
      'admin' => TRUE,
    );
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function drd_server_block_view($delta) {
  switch ($delta) {
    case 'drd':
      module_load_include('inc', 'drd_server', 'drd_server.admin');
      return drd_server_admin_block();
  }
}

/**
 * Implements hook_permission().
 */
function drd_server_permission() {
  return array(
    'flush cache' => array(
      'title' => t('Flush Cache'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function drd_server_menu() {
  $items['admin/drd_server/flush/cache'] = array(
    'title' => 'Flush Cache',
    'page callback' => 'drd_server_admin_flush_cache',
    'access arguments' => array(
      'flush cache',
    ),
    'file' => 'drd_server.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/drd_server/update/translation'] = array(
    'title' => 'Update translation',
    'page callback' => 'drd_server_admin_update_translation',
    'access arguments' => array(
      'translate interface',
    ),
    'file' => 'drd_server.admin.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

Functions

Namesort descending Description
drd_server_block_info Implements hook_block_info().
drd_server_block_view Implements hook_block_view().
drd_server_check_api ======================================================================
drd_server_check_session Check the session ID if it's still valid.
drd_server_connect Authenticate a user.
drd_server_error Prepare an error message for returning to the XMLRPC caller.
drd_server_get_account_by_session Get the session by ID.
drd_server_load_user Load user for session and check permission.
drd_server_menu Implements hook_menu().
drd_server_permission Implements hook_permission().
drd_server_result Prepare the result for returning it to the XMLRPC caller.
drd_server_xmlrpc Implementation of hook_xmlrpc().

Constants

Namesort descending Description
DRD_API_VERSION