View source
<?php
define('DRD_API_VERSION', '1.0.20');
define('DRUPAL_ROOT', getcwd());
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',
);
}
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);
}
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.');
}
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.');
}
}
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);
}
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) {
return str_replace(array(
'<',
'>',
'&',
), array(
'\\u003c',
'\\u003e',
'\\u0026',
), json_encode($var));
}