View source
<?php
function drd_server_server_domains($sid) {
$user = drd_server_load_user($sid);
if (is_string($user)) {
return drd_server_error($user);
}
$sites = array();
if (file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
include DRUPAL_ROOT . '/sites/sites.php';
}
$output = array();
foreach ($sites as $url => $id) {
if (isset($output[$id])) {
$output[$id]['aliases'][] = $url;
}
else {
if (drd_server_server_check_host($url, $id)) {
$output[$id] = array(
'url' => $url,
'aliases' => array(),
);
}
}
}
return drd_server_result('server.domains', $output);
}
function drd_server_server_check_host($url, $dir) {
$id = rand();
$file = DRUPAL_ROOT . '/sites/' . $dir . '/files/drd_id.html';
try {
if (file_exists($file)) {
unlink($file);
}
$fp = fopen($file, 'w');
fwrite($fp, $id);
fclose($fp);
$context = stream_context_create(array(
'http' => array(
'timeout' => 1,
),
));
$check = file_get_contents('http://' . $url . '/sites/' . $dir . '/files/drd_id.html', FALSE, $context);
unlink($file);
return $check == $id;
} catch (Exception $e) {
return FALSE;
}
}
function drd_server_server_svn_status($sid, $svn_user, $svn_password, $force_external = FALSE) {
$user = drd_server_load_user($sid);
if (is_string($user)) {
return drd_server_error($user);
}
drupal_set_time_limit(0);
$status = array();
if (function_exists('svn_status') && $force_external === FALSE) {
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn_user);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn_password);
$main = svn_status(DRUPAL_ROOT, SVN_NON_RECURSIVE + SVN_ALL);
$mod_local = svn_status(DRUPAL_ROOT);
$revision = $main[0]['revision'];
foreach ($mod_local as $line) {
if (is_dir($line['path']) && strpos('._', substr(basename($line['path']), 0, 1)) >= 0) {
}
else {
$status[] = array(
'file_path' => $line['path'],
'file_revision' => $line['revision'],
'file_status' => $line['text_status'],
'file_out_of_date' => $line['text_status'] != SVN_WC_STATUS_NORMAL || $line['repos_text_status'] != SVN_WC_STATUS_NORMAL,
);
}
}
$status[] = array(
'info' => 'SVN status collected by PHP function',
);
}
else {
$cmd = "svn status -u " . DRUPAL_ROOT . " --username {$svn_user} --password {$svn_password}";
$result = explode(':', exec($cmd, $output, $cmd_status));
if ($cmd_status) {
$cmd = "/usr/bin/" . $cmd;
$result = explode(':', exec($cmd, $output, $cmd_status));
}
$revision = sizeof($result) > 1 ? trim(str_replace('.', '', $result[1])) : '';
$revision = is_numeric($revision) ? $revision : t('Unknown');
$next_line_conflict = FALSE;
array_pop($output);
foreach ($output as $line) {
if (!$next_line_conflict) {
$file_status = substr($line, 0, 7);
$file_out_of_date = substr($line, 8, 1);
$line = trim(substr($line, 9));
$pos = strpos($line, ' ');
$file_revision = trim(substr($line, 0, $pos));
$file_path = trim(substr($line, $pos));
$next_line_conflict = substr($file_status, 6) == 'C';
$status[] = array(
'file_path' => $file_path,
'file_revision' => $file_revision,
'file_status' => $file_status,
'file_out_of_date' => $file_out_of_date == '*',
);
}
else {
$next_line_conflict = FALSE;
}
}
$status[] = array(
'info' => 'SVN status collected by commandline tool',
);
}
return drd_server_result('server.svn.status', array(
$revision,
$status,
));
}
function drd_server_server_svn_update($sid, $svn_user, $svn_password, $force_external = FALSE) {
$user = drd_server_load_user($sid);
if (is_string($user)) {
return drd_server_error($user);
}
drupal_set_time_limit(0);
$status = array();
if (function_exists('svn_update') && $force_external === FALSE) {
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn_user);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn_password);
ob_start();
$revision = svn_update(DRUPAL_ROOT);
$output = ob_get_contents();
ob_end_clean();
$status[] = $output;
$status[] = array(
'info' => 'SVN update performed by PHP function',
);
}
else {
$cmd = "svn update " . DRUPAL_ROOT . " --accept theirs-full --force --username {$svn_user} --password {$svn_password} --non-interactive";
$result = explode(' ', exec($cmd, $output, $cmd_status));
if ($cmd_status) {
$cmd = "/usr/bin/" . $cmd;
$result = explode(':', exec($cmd, $output, $cmd_status));
}
$revision = trim(str_replace('.', '', array_pop($result)));
array_pop($output);
foreach ($output as $line) {
$file_status = substr($line, 0, 3);
$line = trim(substr($line, 5));
$file_path = trim($line);
$status[] = array(
'file_path' => $file_path,
'file_status' => $file_status,
);
}
$status[] = array(
'info' => 'SVN update performed by commandline tool',
);
}
return drd_server_result('server.svn.update', array(
$revision,
$status,
));
}
function drd_server_server_php_error_log($sid, $max_length = 5000) {
$user = drd_server_load_user($sid);
if (is_string($user)) {
return drd_server_error($user);
}
$log_file = ini_get('error_log');
if (!file_exists($log_file)) {
return drd_server_error(t('PHP Error Log does not exist.'));
}
$offset = max(-1, filesize($log_file) - $max_length);
$log = file_get_contents($log_file, FILE_BINARY, NULL, $offset);
return drd_server_result('server.php.error.log', array(
$log,
));
}