View source
<?php
define('HOSTING_SITE_DELETED', -2);
define('HOSTING_SITE_DISABLED', -1);
define('HOSTING_SITE_QUEUED', 0);
define('HOSTING_SITE_ENABLED', 1);
include_once 'hosting_site.nodeapi.inc';
include_once 'hosting_site.form.inc';
include_once 'hosting_site.backups.inc';
if (module_exists('hosting_quota')) {
include_once 'hosting_site.quota.inc';
}
function hosting_site_menu() {
$items = array();
$items['hosting/sites'] = array(
'title' => 'Sites',
'description' => 'Display a list of sites',
'page callback' => 'hosting_sites',
'access arguments' => array(
'view site',
),
);
$items['hosting/hosting_site_form_check'] = array(
'page callback' => '_hosting_site_form_check',
'type' => MENU_CALLBACK,
'access arguments' => array(
'access content',
),
);
$items['node/%hosting_site_node/goto_site'] = array(
'page callback' => 'hosting_site_goto',
'page arguments' => array(
1,
),
'access callback' => 'node_access',
'access arguments' => array(
'view',
1,
),
'type' => MENU_CALLBACK,
);
return $items;
}
function _hosting_site_form_check() {
drupal_json(hosting_site_available_options($_POST));
exit;
}
function hosting_site_node_load($arg) {
if (!is_numeric($arg)) {
return FALSE;
}
if ($node = node_load($arg)) {
if ($node->type == 'site') {
return $node;
}
}
return FALSE;
}
function hosting_sites() {
if ($list = drupal_get_form('hosting_site_list_form')) {
return $list;
}
$create_site_link = l(t('Create a site now?'), 'node/add/site');
return t("No sites have been created yet. !link", array(
'!link' => $create_site_link,
));
}
function _hosting_site_goto_link($node) {
$cache = cache_get("hosting:site:" . $node->nid . ":login_link");
if (user_access('create login-reset task') && !is_null($cache) && time() < $cache->data['expire']) {
$title = t("Log in to !url", array(
'!url' => $node->title,
));
}
else {
$title = t("Go to !url", array(
'!url' => $node->title,
));
}
$options['attributes']['class'] = 'hosting-goto-site-link';
return l($title, "node/" . $node->nid . "/goto_site", $options);
}
function hosting_site_goto($node) {
$cid = "hosting:site:" . $node->nid . ":login_link";
$cache = cache_get($cid);
if (user_access('create login-reset task') && !is_null($cache) && time() < $cache->data['expire']) {
$theurl = $cache->data['link'];
cache_clear_all($cid, 'cache');
}
else {
$theurl = _hosting_site_url($node);
}
drupal_goto($theurl);
exit;
}
function hosting_site_get_port($node) {
$platform = node_load($node->platform);
$server = node_load($platform->web_server);
return $server->services['http']->port;
}
function _hosting_site_url($node) {
$schema = 'http';
$port = null;
$url = strtolower(trim($node->title));
$platform = node_load($node->platform);
$server = node_load($platform->web_server);
if ($server->services['http']
->has_port()) {
$port = $server->services['http']->port;
if ($port == 80) {
$port = null;
}
}
if (isset($node->ssl_enabled) && $node->ssl_enabled == 2) {
$schema = 'https';
if ($server->services['http']
->has_port()) {
$port = $server->services['http']->ssl_port;
if ($port == 443) {
$port = null;
}
}
}
if (is_numeric($port)) {
return "{$schema}://{$url}:{$port}";
}
return "{$schema}://{$url}";
}
function hosting_site_hosting_tasks() {
$tasks = array();
$tasks['site']['backup'] = array(
'title' => t('Backup'),
'description' => t('Generate a backup of this site that can be restored to at any time'),
'dialog' => TRUE,
);
$tasks['site']['restore'] = array(
'title' => t('Restore'),
'description' => t('Restore this site to a previous backup. A new backup will be created before this is attempted.'),
'dialog' => TRUE,
);
$tasks['site']['verify'] = array(
'title' => t('Verify'),
'description' => t('Confirm that the site has been correctly installed and regenerate all configuration files to match the hosting front end.'),
'provision_save' => TRUE,
);
$tasks['site']['disable'] = array(
'title' => t('Disable'),
'description' => t('Disabling this site will stop it from being accessible.
It can be enabled again later.'),
'dialog' => TRUE,
);
$tasks['site']['enable'] = array(
'title' => t('Enable'),
'description' => t('Enabling this site will allow it to be accesible again.
It may be disabled again if needed.'),
);
$tasks['site']['delete'] = array(
'title' => t('Delete'),
'description' => t('Deleting this site will completely remove it from the hosting system,
but will keep the last backup available. This process can not be undone.
Are you really sure you want to delete this site?'),
'dialog' => TRUE,
);
$tasks['site']['login_reset'] = array(
'title' => t('Reset password'),
'description' => t('Generate a one-time login reset url for this site.'),
);
$tasks['site']['backup_delete'] = array(
'title' => t('Delete backups'),
'description' => t('Delete one or more backup files of a site.'),
'dialog' => TRUE,
);
$tasks['site']['install'] = array(
'title' => t('Install'),
'description' => t('Install a site'),
'hidden' => TRUE,
'provision_save' => TRUE,
);
$tasks['site']['import'] = array(
'title' => t('Import'),
'description' => t('Import an existing site into Aegir'),
'hidden' => TRUE,
'provision_save' => TRUE,
);
return $tasks;
}
function hosting_site_perm() {
return array(
'create site',
'view site',
'edit site',
'delete site',
'administer sites',
);
}
function hosting_site_access($op, $node, $account) {
if ($op != 'create' && hosting_feature('client')) {
return NULL;
}
switch ($op) {
case 'create':
return user_access('create site', $account);
break;
case 'update':
return user_access('edit site', $account);
break;
case 'delete':
return user_access('delete site', $account);
break;
case 'view':
return user_access('view site', $account);
break;
}
}
function hosting_site_count($platform = NULL, $statuses = NULL) {
if (is_null($statuses)) {
$statuses = array(
HOSTING_SITE_ENABLED,
);
}
$args = array();
$query = "SELECT count(nid) FROM {hosting_site}";
$where = array();
if (count($statuses)) {
$where[] = '(status IN (' . db_placeholders($statuses) . '))';
$args = array_merge($args, $statuses);
}
if (!is_null($platform)) {
$where[] = "(platform = %d)";
$args[] = $platform;
}
if (!empty($where)) {
$query .= ' WHERE ' . implode(' AND ', $where);
}
return db_result(db_query($query, $args));
}
function hosting_get_sites_by_status($platform, $status) {
$nodes = array();
$result = db_query("SELECT nid FROM {hosting_site} WHERE platform=%d AND status = %d", $platform, $status);
while ($nid = db_fetch_object($result)) {
$nodes[$nid->nid] = node_load($nid->nid);
}
return $nodes;
}
function hosting_get_site_by_url($url) {
if (hosting_feature('alias')) {
$nid = db_result(db_query("SELECT n.nid FROM {node} n JOIN {hosting_site} h ON n.nid = h.nid LEFT JOIN {hosting_site_alias} ha ON h.vid = ha.vid WHERE (n.title = '%s' OR ha.alias = '%s') AND n.type = 'site' AND NOT (h.status = %d)", $url, $url, HOSTING_SITE_DELETED));
}
else {
$nid = db_result(db_query("SELECT n.nid FROM {node} n JOIN {hosting_site} h ON n.nid = h.nid WHERE n.title='%s' AND n.type = 'site' AND NOT (h.status=%d)", $url, HOSTING_SITE_DELETED));
}
if ($nid) {
return node_load($nid);
}
return false;
}
function hosting_import_site($site_id, $data, $platform) {
global $user;
$client = node_load(HOSTING_DEFAULT_CLIENT);
if ($data['client_name']) {
$client = hosting_import_client($data['client_name']);
}
elseif ($data['client_email']) {
$client = hosting_import_client($data['client_email']);
}
$site = node_load($site_id);
$site->nid = $site_id;
$site->uid = $client->uid;
$site->status = 1;
$site->site_status = 1;
$site->platform = $platform;
$site->no_verify = TRUE;
$site->verified = time();
$site->client = $client->nid;
$site->ip_addresses = array_values($data['site_ip_addresses']);
$site->cron_key = $data['cron_key'] ? $data['cron_key'] : '';
$site->aliases = $data['aliases'] ? $data['aliases'] : array();
$site->db_server = $site->db_server ? $site->db_server : HOSTING_DEFAULT_DB_SERVER;
$site->site_language = $data['language'] ? $data['language'] : 'en';
unset($data['language']);
$profile = hosting_package_instance_load(array(
'i.rid' => $platform,
'p.short_name' => $data['profile'],
));
if (!$profile) {
$profile = hosting_package_instance_load(array(
'i.rid' => $platform,
'p.short_name' => 'default',
));
}
$site->profile = $profile->package_id;
$site = (array) $site;
foreach (array_keys($site) as $key) {
unset($data[$key]);
}
$site = array_merge((array) node_load($site_id), $site);
$site = array_merge($site, $data);
$site = (object) $site;
node_save($site);
}
function _hosting_site_status($node) {
$status = is_numeric($node) ? $node : $node->site_status;
static $labels = array(
HOSTING_SITE_QUEUED => "Queued",
HOSTING_SITE_ENABLED => "Enabled",
HOSTING_SITE_DELETED => "Deleted",
HOSTING_SITE_DISABLED => "Disabled",
);
return $labels[$status];
}
function hosting_site_exists($url, $nid = null) {
return !hosting_domain_allowed($url, array(
'nid' => $nid,
));
}
function hosting_site_allow_domain($url, $params = array()) {
$query = "SELECT COUNT(n.nid) FROM {node} n \n JOIN {hosting_site} h ON n.nid = h.nid\n WHERE type='site' AND title='%s' AND h.status <> %d ";
$args[] = $url;
$args[] = HOSTING_SITE_DELETED;
if (isset($params['nid'])) {
$query .= ' AND n.nid <> %d';
$args[] = $params['nid'];
}
$result = !db_result(db_query($query, $args));
return $result;
}
function hosting_site_task_status($nid) {
return hosting_task_status_output('nid', $nid, 'install');
}
function hosting_site_list_form($form_state, $filter_by = NULL, $filter_value = NULL) {
$step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
if ($step == 1) {
$args[] = 'site';
$cond = '';
if ($filter_by && $filter_value) {
if ($filter_by == 'status') {
$cond = ' AND s.' . $filter_by . ' & %d';
}
else {
$cond = ' AND s.' . $filter_by . ' = %d';
}
$args[] = $filter_value;
}
$header = array(
array(
'data' => t('Site'),
'field' => 'title',
),
array(
'data' => t('Profile'),
'field' => 'profile',
),
array(
'data' => t('Language'),
'field' => 'site_language',
),
array(
'data' => t('Created'),
'field' => 'created',
'sort' => 'desc',
),
);
$platforms = _hosting_get_platforms();
if (sizeof($platforms) > 1) {
$header[] = array(
'data' => t('Platform'),
'field' => 'platform',
);
}
$sql = "SELECT n.nid, n.title, n.created, s.status as site_status, profile, s.language as site_language, platform, verified FROM {node} n left join {hosting_site} s ON n.vid=s.vid WHERE type='%s' AND s.status != -2 " . $cond;
$sql .= tablesort_sql($header);
$result = pager_query(db_rewrite_sql($sql), 25, 1, null, $args);
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Site tasks'),
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
$options = array();
foreach (hosting_available_tasks('site') as $task => $array) {
$func = 'hosting_task_' . $task . '_form';
if (!function_exists($func) && user_access('create ' . $task . ' task')) {
$options[$task] = $array['title'];
}
}
$form['options']['task'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => 'backup',
);
$form['options']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to queue'),
'#submit' => array(
'hosting_site_list_form_submit',
),
);
$sites = array();
while ($node = db_fetch_object($result)) {
$sites[$node->nid] = '';
$form['site'][$node->nid] = array(
'#value' => l($node->title, 'node/' . $node->nid),
);
$form['profile'][$node->nid] = array(
'#value' => $node->profile ? _hosting_node_link($node->profile) : t('n/a'),
);
$form['language'][$node->nid] = array(
'#value' => $node->site_language ? _hosting_language_name($node->site_language) : t('n/a'),
);
$form['created'][$node->nid] = array(
'#value' => t("@interval ago", array(
'@interval' => format_interval(time() - $node->created, 1),
)),
);
if (sizeof($platforms) > 1) {
$form['platform'][$node->nid] = array(
'#value' => $node->platform ? _hosting_node_link($node->platform) : t('n/a'),
);
}
$form['site_class'][$node->nid] = array(
'#value' => _hosting_site_list_class($node, $node->verified),
);
}
$form['sites'] = array(
'#type' => 'checkboxes',
'#options' => $sites,
);
$form['pager'] = array(
'#value' => theme('pager', NULL, 25, 1),
);
$form['#theme'] = 'hosting_site_list';
$form['#action'] = url('hosting/sites');
return $form;
}
elseif ($step == 2) {
$task = $form_state['values']['task'];
$tasks = hosting_available_tasks('site');
$title = array(
'passed' => t("The following sites will be processed"),
'failed' => t("The following sites failed validation checks and will not be processed"),
);
foreach (array(
'passed',
'failed',
) as $type) {
if (sizeof($form_state['storage'][$type])) {
foreach ($form_state['storage'][$type] as $site_id => $url) {
$row = array(
'data' => array(
array(
'data' => l($url, 'node/' . $site_id),
'class' => 'hosting-status',
),
),
'class' => $type == 'passed' ? 'hosting-success' : 'hosting-error',
);
$rows[] = $row;
}
}
}
$header = array(
t('Site'),
);
$form['sites_test'] = array(
'#value' => theme('table', $header, $rows),
);
if (sizeof($form_state['storage']['failed']) && sizeof($form_state['storage']['passed'])) {
drupal_set_message(t('The task @task is not able to be performed on all the sites selected, the sites below that failed will not be added to the queue.', array(
'@task' => $task,
)), 'warning');
}
elseif (sizeof($form_state['storage']['failed'])) {
drupal_set_message(t('The task @task is not able to be performed on any of the selected sites.', array(
'@task' => $task,
)), 'error');
$form['return_link'] = array(
'#value' => l('Return to site listing', 'hosting/sites'),
);
return $form;
}
$form['help'] = array(
'#value' => $tasks[$task]['description'],
);
$question = t('Are you sure you want to perform the "@task" task on each of the following sites?', array(
'@task' => $tasks[$task]['title'],
));
return confirm_form($form, $question, 'hosting/sites', '', $tasks[$task]['title']);
}
}
function hosting_site_list_form_validate($form, &$form_state) {
if (isset($form_state['values']['sites'])) {
$sites = array_filter($form_state['values']['sites']);
if (count($sites) == 0) {
form_set_error('', t('No sites selected.'));
}
}
}
function hosting_site_list_form_submit($form, &$form_state) {
$step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
$task = $form_state['values']['task'];
switch ($step) {
case 1:
$form_state['storage']['task'] = $task;
$tasks = hosting_available_tasks('site');
$sites = array_filter($form_state['values']['sites']);
$operations = array();
foreach ($sites as $site) {
$operations[] = array(
'hosting_sites_batch_process',
array(
$site,
$task,
),
);
}
if (sizeof($operations)) {
$batch = array(
'operations' => $operations,
'title' => t('Processing'),
'progress_message' => t('Evaluated @current out of @total sites.'),
'error_message' => t('The update has encountered an error.'),
);
batch_set($batch);
}
break;
case 2:
$values = $form_state['values'];
foreach ($form_state['storage']['passed'] as $site_id => $url) {
hosting_add_task($site_id, $form_state['storage']['task'], $values['parameters']);
}
unset($form_state['storage']);
$step = 0;
drupal_set_message(t('The task @task will be applied to the selected sites and have been added to the task queue.', array(
'@task' => $form_state['storage']['task'],
)));
break;
}
$form_state['storage']['step'] = $step + 1;
}
function hosting_sites_batch_process($site_id, $task, &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
$site = node_load($site_id);
$batch =& batch_get();
if (hosting_task_menu_access($site, $task)) {
$batch['form_state']['storage']['passed'][$site->nid] = $site->title;
}
else {
$batch['form_state']['storage']['failed'][$site->nid] = $site->title;
}
}
function hosting_site_theme() {
return array(
'hosting_site_list' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
function theme_hosting_site_list($form) {
$has_posts = isset($form['site']) && is_array($form['site']);
$select_header = $has_posts ? theme('table_select_header_cell') : '';
$header = array(
$select_header,
array(
'data' => t('Site'),
'field' => 'title',
),
array(
'data' => t('Profile'),
'field' => 'profile',
),
array(
'data' => t('Language'),
'field' => 'site_language',
),
array(
'data' => t('Created'),
'field' => 'created',
'sort' => 'desc',
),
);
if (isset($form['platform'])) {
$header[] = array(
'data' => t('Platform'),
'field' => 'platform',
);
}
$output = '';
$output .= drupal_render($form['options']);
if ($has_posts) {
foreach (element_children($form['site']) as $key) {
$row = array();
$row[] = drupal_render($form['sites'][$key]);
$row[] = array(
'data' => drupal_render($form['site'][$key]),
'class' => 'hosting-status',
);
$row[] = drupal_render($form['profile'][$key]);
$row[] = drupal_render($form['language'][$key]);
$row[] = drupal_render($form['created'][$key]);
if (isset($form['platform'])) {
$row[] = drupal_render($form['platform'][$key]);
}
$rows[] = array(
'data' => $row,
'class' => drupal_render($form['site_class'][$key]),
);
}
}
else {
$rows[] = array(
array(
'data' => t('No sites available.'),
'colspan' => sizeof($header),
),
);
}
$output .= theme('table', $header, $rows, array(
'class' => 'hosting-table',
));
if ($form['pager']['#value']) {
$output .= drupal_render($form['pager']);
}
$output .= drupal_render($form);
return $output;
}
function _hosting_site_list_class($node, $verified = null) {
$status = is_numeric($node) ? $node : $node->site_status;
static $classes = array(
HOSTING_SITE_QUEUED => "hosting-queue",
HOSTING_SITE_ENABLED => "hosting-success",
HOSTING_SITE_DELETED => "hosting-error",
HOSTING_SITE_DISABLED => "hosting-error",
);
if ($status == HOSTING_SITE_ENABLED && !$verified) {
return 'hosting-warning';
}
return $classes[$status];
}
function hosting_site_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'hosting_site'),
);
}
function hosting_site_preprocess_views_view_table(&$variables) {
$view = $variables['view'];
if ($view->plugin_name == 'hosting_site_list') {
foreach ($view->result as $num => $result) {
if (isset($result->hosting_site_status) && isset($result->hosting_site_verified)) {
$variables['row_classes'][$num][] = _hosting_site_list_class($result->hosting_site_status, $result->hosting_site_verified);
}
}
}
$variables['class'] .= " hosting-table";
}