You are here

drd_server.module in Drupal Remote Dashboard Server 6

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

File

drd_server.module
View source
<?php

define('DRD_API_VERSION', '1.0.20');
define('DRUPAL_ROOT', getcwd());

/** ======================================================================
 *
 * 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);
}
function drd_server_connect($username, $password) {
  $user = drd_server_validate_user($username, $password);
  if (is_string($user)) {
    return drd_server_error($user);
  }
  return drd_server_result('connect', session_id());
}
function drd_server_get_account_by_session($sid) {
  try {
    $account = db_fetch_object(db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $sid));
  } catch (Exception $e) {
  }
  return $account;
}
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);
}

/**
 * Get the user from the given session and check permission
 */
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';
    $result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $account->uid);
    while ($role = db_fetch_object($result)) {
      $account->roles[$role->rid] = $role->name;
    }
  }
  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.');
}

/**
 * Ensure that the given user has permission to access drd data.
 */
function drd_server_validate_user($username, $password) {
  $form_state['values']['name'] = $username;
  $form_state['values']['pass'] = $password;
  $form_state['values']['op'] = t('Login');
  $GLOBALS['drd_xmlrpc_mode'] = TRUE;
  $user = user_authenticate($form_state['values']);
  if (!form_get_errors()) {
    if (user_access('administer site configuration')) {
      return $GLOBALS['user'];
    }
    else {
      return t('You do not have permission to access system data.');
    }
  }
  else {
    return t('Wrong username or password.');
  }
}

/**
 * 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.
 */
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)));
}
function drupal_json_encode($var) {

  // json_encode() does not escape <, > and &, so we do it with str_replace().
  return str_replace(array(
    '<',
    '>',
    '&',
  ), array(
    '\\u003c',
    '\\u003e',
    '\\u0026',
  ), json_encode($var));
}

Functions

Namesort descending Description
drd_server_check_api ======================================================================
drd_server_check_session
drd_server_connect
drd_server_error Prepare an error message for returning to the XMLRPC caller.
drd_server_get_account_by_session
drd_server_load_user Get the user from the given session and check permission
drd_server_result Prepare the result for returning it to the XMLRPC caller.
drd_server_validate_user Ensure that the given user has permission to access drd data.
drd_server_xmlrpc Implementation of hook_xmlrpc().
drupal_json_encode

Constants