View source
<?php
define('HOSTING_SITE_DELETED', -2);
define('HOSTING_SITE_DISABLED', -1);
define('HOSTING_SITE_QUEUED', 0);
define('HOSTING_SITE_ENABLED', 1);
function hosting_site_node_info() {
$types["site"] = array(
"type" => 'site',
"name" => 'Site',
"module" => 'hosting_site',
"has_title" => TRUE,
"title_label" => 'Domain name',
"description" => hosting_node_help("site"),
"has_body" => 0,
"body_label" => '',
"min_word_count" => 0,
);
return $types;
}
function hosting_site_hosting_tasks($node) {
$options = array();
if ($node->type == 'site') {
if (hosting_task_outstanding($node->nid, 'delete') || $node->site_status == HOSTING_SITE_DELETED) {
return $options;
}
if (hosting_task_outstanding($node->nid, 'enable') || $node->site_status == HOSTING_SITE_ENABLED) {
$options['backup'] = array(
'title' => t('Backup'),
'description' => t('Generate a backup of this site that can be restored to at any time'),
'weight' => 7,
);
$options['restore'] = array(
'title' => t('Restore'),
'description' => t('Restore this site to a previous backup. A new backup will be created before this is attempted.'),
'weight' => 7,
);
$options['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.'),
'weight' => 9,
);
$options['disable'] = array(
'title' => t('Disable'),
'description' => t('Disabling this site will stop it from being accessible.
It can be enabled again later.'),
'weight' => 4,
);
}
else {
$options['enable'] = array(
'title' => t('Enable'),
'description' => t('Enabling this site will allow it to be accesible again.
It may be disabled again if needed.'),
'weight' => 4,
);
$options['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?'),
'weight' => 10,
);
}
}
return $options;
}
function hosting_site_perm() {
return array(
'create site',
'view site',
'edit site',
'delete site',
'administer site',
);
}
function hosting_site_access($op, $node) {
switch ($op) {
case 'create':
return user_access('create site');
break;
case 'update':
return user_access('edit site');
break;
case 'delete':
return user_access('delete site');
break;
default:
break;
}
}
function hosting_site_count() {
return db_result(db_query("SELECT count(nid) FROM {node} WHERE type='site' AND status=1"));
}
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) {
$nid = db_result(db_query("SELECT nid FROM {node} WHERE title='%s' AND type = 'site' AND NOT (status=%d)", $url, HOSTING_SITE_DELETED));
if ($nid) {
return node_load($nid);
}
return false;
}
function hosting_import_site($site_id, $data, $platform = HOSTING_DEFAULT_PLATFORM) {
global $user;
$client = node_load(HOSTING_DEFAULT_CLIENT);
if ($data['client_email']) {
$client = hosting_import_client($data['client_email'], $data['client_name'], $data['client_organization']);
}
$site = new stdClass();
$site->nid = $site_id;
$site->uid = $client->uid;
$site->status = 1;
$site->site_status = 1;
$site->platform = $platform;
$site->client = $client->nid;
$db_server = hosting_get_db_server($data['db_id']);
$site->db_server = $db_server ? $db_server->nid : HOSTING_DEFAULT_DB_SERVER;
$site->language = $data['language'] ? $data['language'] : 'en';
$profile = hosting_package_instance_load(array(
'rid' => $platform,
'short_name' => $data['profile'],
));
if (!$profile) {
$profile = hosting_package_instance_load(array(
'rid' => $platform,
'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_form($node) {
drupal_add_js(drupal_get_path('module', 'hosting_site') . '/hosting_site_form.js');
drupal_add_css(drupal_get_path('module', 'hosting_site') . '/hosting_site_form.css');
$type = node_get_types('type', $node);
if ($node->nid) {
$form['info']['#prefix'] = '<div class="clear-block" id="hosting-site-edit-info">';
$form['info']['#suffix'] = '<br /></div>';
}
if (!$node->nid) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Domain name'),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5,
);
}
else {
$form['info']['title'] = array(
'#type' => 'item',
'#title' => t('Domain name'),
'#value' => $node->title,
'#weight' => -5,
);
$form['title'] = array(
'#type' => 'hidden',
'#value' => $node->title,
);
}
global $user;
if ($user->uid) {
$client_ids = hosting_get_client_from_user($user->uid);
$clients = array();
foreach ($client_ids as $client_id => $client_permissions) {
$client_id = $client_id ? $client_id : HOSTING_DEFAULT_CLIENT;
$client = node_load($client_id);
$clients[$client->title] = $client->title;
if ($node->client == $client_id || !$current_client_id) {
$current_client_id = $client_id;
}
}
}
else {
$current_client_id = HOSTING_DEFAULT_CLIENT;
}
if ($node->client && user_access('administer site')) {
$current_client_id = $node->client;
}
$client = node_load($current_client_id);
if ((!$node->client || $node->nid) && user_access('administer site')) {
$form['client'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Client'),
'#default_value' => $client->title,
'#description' => t('The client who this site belongs to.'),
'#autocomplete_path' => 'hosting_client/autocomplete/client',
);
}
elseif ($node->client) {
$form['client'] = array(
'#type' => 'hidden',
'#value' => $client->title,
);
$form['info']['client'] = array(
'#type' => 'item',
'#title' => t('Client'),
'#value' => $client->title,
);
}
else {
$form['client'] = array(
'#type' => 'radios',
'#title' => t('Client'),
'#default_value' => key($clients),
'#options' => $clients,
'#description' => t('The client who this site belongs to.'),
);
}
if (!$node->nid) {
$platforms = _hosting_get_platforms();
if (sizeof($platforms) > 1) {
$form['platform'] = array(
'#type' => 'radios',
'#title' => t('Platform'),
'#required' => TRUE,
'#description' => t('The platform you want the site to be hosted on.'),
'#options' => $platforms,
'#default_value' => HOSTING_DEFAULT_PLATFORM,
);
}
else {
$form['platform'] = array(
'#type' => 'hidden',
'#value' => key($platforms),
);
}
$form['profile'] = _hosting_site_form_profile();
$form['language'] = _hosting_site_form_language();
}
else {
$form['info']['platform'] = array(
'#type' => 'item',
'#title' => t('Platform'),
'#value' => _hosting_node_link($node->platform),
);
$form['platform'] = array(
'#type' => 'hidden',
'#value' => $node->platform,
);
$form['info']['profile'] = array(
'#type' => 'item',
'#title' => t('Install Profile'),
'#value' => _hosting_node_link($node->profile),
);
$form['profile'] = array(
'#type' => 'hidden',
'#value' => $node->profile,
);
$form['info']['language'] = array(
'#type' => 'item',
'#title' => t('Language'),
'#value' => _hosting_language_name($node->language),
);
$form['language'] = array(
'#type' => 'hidden',
'#value' => $node->language,
);
}
if (!$node->nid) {
$db_servers = _hosting_get_db_servers();
if (sizeof($db_servers) > 1) {
$form['db_server'] = array(
'#type' => 'radios',
'#title' => t('Database server'),
'#required' => TRUE,
'#description' => t('The database server the site will use to host it\'s content.'),
'#options' => $db_servers,
'#default_value' => $node->db_server,
);
}
else {
$form['db_server'] = array(
'#type' => 'hidden',
'#value' => key($db_servers),
);
}
}
else {
$form['info']['db_server'] = array(
'#type' => 'item',
'#title' => t('Database Server'),
'#value' => _hosting_node_link($node->db_server),
);
$form['db_server'] = array(
'#type' => 'hidden',
'#value' => $node->db_server,
);
}
return $form;
}
function hosting_site_validate($node, &$form) {
global $user;
$url = strtolower($node->title);
if (!_hosting_valid_fqdn($url)) {
form_set_error('title', t("You have not specified a valid url for this site."));
}
if (hosting_site_exists($url, $node->nid)) {
form_set_error('title', t("The domain name you have specified is not unique."));
}
if (!$node->new_client) {
$client = hosting_get_client($node->client);
if (!$node->client || !$client) {
form_set_error('client', t('Please fill in a valid client'));
}
if (!user_access('administer clients') && !array_key_exists($client->nid, hosting_get_client_from_user($user->uid))) {
form_set_error('client', t('Access denied to client @client', array(
'@client' => $client->title,
)));
}
}
if (!array_key_exists($node->profile, hosting_get_profiles($node->platform))) {
form_set_error('profile', t('Please fill in a valid profile'));
}
if (!array_key_exists($node->language, hosting_get_profile_languages($node->profile, $node->platform))) {
form_set_error('language', t('Please fill in a valid language'));
}
}
function hosting_site_submit(&$node) {
$node->title = strtolower($node->title);
}
function hosting_site_insert(&$node) {
$client = hosting_get_client($node->client);
$node->client = $client->nid;
$node->language = $node->language ? $node->language : 'en';
db_query("INSERT INTO {hosting_site} (vid, nid, client, db_server, platform, profile, language, last_cron, status, verified) VALUES (%d, %d, %d, %d, %d, %d, '%s', %d, %d, %d)", $node->vid, $node->nid, $node->client, $node->db_server, $node->platform, $node->profile, $node->language, $node->last_cron, $node->site_status, $node->verified);
if (!$node->old_vid) {
if ($node->import) {
hosting_add_task($node->nid, 'import');
}
else {
hosting_add_task($node->nid, 'install');
}
}
}
function hosting_site_update(&$node) {
if ($node->revision) {
hosting_site_insert($node);
}
else {
$client = hosting_get_client($node->client);
$node->client = $client->nid;
db_query("UPDATE {hosting_site} SET client = %d, db_server = %d, platform = %d, last_cron = %d, status = %d, profile = %d, language = '%s', verified = %d WHERE vid=%d", $node->client, $node->db_server, $node->platform, $node->last_cron, $node->site_status, $node->profile, $node->language, $node->verified, $node->vid);
}
if (!$node->no_verify) {
hosting_add_task($node->nid, 'verify');
}
}
function hosting_site_load($node) {
$additions = db_fetch_object(db_query('SELECT client, db_server, platform, profile, language, last_cron, status AS site_status, verified FROM {hosting_site} WHERE vid = %d', $node->vid));
return $additions;
}
function hosting_nodeapi_site_delete_revision(&$node) {
db_query('DELETE FROM {hosting_site} WHERE vid = %d', $node->vid);
}
function hosting_site_delete($node) {
db_query('DELETE FROM {hosting_site} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {hosting_package_instance} WHERE rid=%d', $node->nid);
$result = db_query("SELECT distinct nid FROM {hosting_task} WHERE rid=%d", $node->nid);
while ($nid = db_fetch_object($result)) {
node_delete($nid->nid);
}
}
function hosting_site_view(&$node, $teaser = false) {
hosting_set_breadcrumb($node);
$node->content['info']['#prefix'] = '<div id="hosting-site-info">';
if ($node->site_status == HOSTING_SITE_ENABLED) {
$node->content['info']['link'] = array(
'#value' => l(t('Go to site'), 'http://' . $node->title),
'#weight' => -10,
);
}
if (is_numeric($node->client)) {
$node->content['info']['client'] = array(
'#type' => 'item',
'#title' => t('Client'),
'#value' => _hosting_node_link($node->client),
'#weight' => 5,
);
}
$node->content['info']['verified'] = array(
'#type' => 'item',
'#title' => t('Verified'),
'#value' => hosting_format_interval($node->verified),
);
$node->content['info']['platform'] = array(
'#type' => 'item',
'#title' => t('Platform'),
'#value' => _hosting_node_link($node->platform),
);
if ($node->profile) {
$node->content['info']['profile'] = array(
'#type' => 'item',
'#title' => t('Install profile'),
'#value' => _hosting_node_link($node->profile),
);
}
if ($node->language) {
$node->content['info']['language'] = array(
'#type' => 'item',
'#title' => t('Language'),
'#value' => _hosting_language_name($node->language),
);
}
if ($node->db_server) {
$node->content['info']['db_server'] = array(
'#type' => 'item',
'#title' => t('Database server'),
'#value' => _hosting_node_link($node->db_server),
);
}
if ($node->nid) {
$node->content['info']['status'] = array(
'#type' => 'item',
'#title' => t('Status'),
'#value' => _hosting_site_status($node),
);
}
$node->content['info']['#suffix'] = '</div>';
if ($node->nid) {
$node->content['tasks_view'] = array(
'#type' => 'item',
'#value' => hosting_task_list_embedded('rid', $node->nid),
'#prefix' => '<div id="hosting-task-list">',
'#suffix' => '</div>',
'#weight' => 10,
);
}
return $node;
}
function _hosting_site_status($node) {
static $labels = array(
HOSTING_SITE_QUEUED => "Queued",
HOSTING_SITE_ENABLED => "Enabled",
HOSTING_SITE_DELETED => "Deleted",
HOSTING_SITE_DISABLED => "Disabled",
);
return $labels[$node->site_status];
}
function hosting_site_add_backup($site, $web_server, $filename, $description = '') {
$bid = db_next_id("{hosting_site_backups}_bid");
db_query("INSERT INTO {hosting_site_backups} (bid, site, web_server, filename, description, timestamp) VALUES (%d, %d, %d, '%s', '%s', %d)", $bid, $site, $web_server, $filename, $description, mktime());
return $bid;
}
function hosting_site_delete_backup($bid) {
db_query("DELETE FROM {hosting_site_backups} WHERE bid=%d", $bid);
}
function hosting_site_get_backup($bid) {
return db_fetch_array(db_query("SELECT bid, site, web_server, filename, description, timestamp FROM {hosting_site_backups} WHERE bid = %d", $bid));
}
function hosting_site_backup_list($site) {
$result = db_query("SELECT bid, description, timestamp FROM {hosting_site_backups} WHERE site=%d ORDER BY timestamp DESC", $site);
while ($object = db_fetch_object($result)) {
$backups[$object->bid] = '<strong>' . format_date($object->timestamp) . '</strong> - ' . $object->description;
}
return $backups;
}
function hosting_site_form_alter($form_id, &$form) {
if ($form_id == 'site_node_form') {
$form['delete']['#type'] = 'hidden';
}
}
function hosting_site_exists($url, $nid = null) {
$results = module_invoke_all('allow_domain', $url, $nid);
$result = in_array(TRUE, $results);
return $result;
}
function hosting_site_allow_domain($url, $nid = null) {
$query = "SELECT COUNT(n.nid) FROM \n {node} n JOIN {hosting_site} h \n ON n.nid = h.nid WHERE type='site'\n AND title='%s' AND h.status <> %d";
$args[] = $url;
$args[] = HOSTING_SITE_DELETED;
if ($nid) {
$query .= ' AND n.nid <> %d';
$args[] = $nid;
}
return db_result(db_query($query, $args));
}
function hosting_site_task_status($nid) {
return hosting_task_status_output('nid', $nid, 'install');
}
function hosting_site_list($filter_by = null, $filter_value = null) {
$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' => '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, 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);
if (!db_num_rows($result)) {
return null;
}
$rows = array();
while ($node = db_fetch_object($result)) {
$row = array();
$row[] = array(
'data' => l($node->title, 'node/' . $node->nid),
'class' => 'hosting-status',
);
$row[] = $node->profile ? _hosting_node_link($node->profile) : t('n/a');
$row[] = $node->language ? _hosting_language_name($node->language) : t('n/a');
$row[] = t("@interval ago", array(
'@interval' => format_interval(mktime() - $node->created, 1),
));
if (sizeof($platforms) > 1) {
$row[] = $node->platform ? _hosting_node_link($node->platform) : t('n/a');
}
$rows[] = array(
'data' => $row,
'class' => _hosting_site_list_class($node),
);
}
return theme('table', $header, $rows, array(
'class' => 'hosting-table',
)) . theme('pager', null, 25, 1);
}
function _hosting_site_list_class($node) {
static $classes = array(
HOSTING_SITE_QUEUED => "hosting-queue",
HOSTING_SITE_ENABLED => "hosting-success",
HOSTING_SITE_DELETED => "hosting-error",
HOSTING_SITE_DISABLED => "hosting-error",
);
if ($node->site_status == HOSTING_SITE_ENABLED && !$node->verified) {
return 'hosting-warning';
}
return $classes[$node->site_status];
}
function hosting_sites() {
if ($list = hosting_site_list()) {
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_menu($may_cache = false) {
$items[] = array(
'path' => 'hosting/sites',
'title' => t('Hosted sites'),
'description' => t('Display a list of sites'),
'callback' => 'hosting_sites',
'type' => MENU_CALLBACK,
'access' => user_access('create site'),
);
$items[] = array(
'path' => 'hosting/hosting_site_form_populate',
'callback' => '_hosting_site_form_populate',
'type' => MENU_CALLBACK,
'access' => TRUE,
);
return $items;
}
function _hosting_site_form_profile($platform = NULL) {
$profiles = hosting_get_profiles($platform);
if (sizeof($profiles) > 1) {
$form['profile'] = array(
'#type' => 'radios',
'#title' => t('Install profile'),
'#description' => t('The type of site to install.'),
'#options' => $profiles,
'#default_value' => key($profiles),
'#required' => TRUE,
'#attributes' => array(
'class' => "hosting-site-form-profile-options",
),
);
}
else {
$form['profile'] = array(
'#type' => 'hidden',
'#default_value' => key($profiles),
'#attributes' => array(
'class' => "hosting-site-form-profile-options",
),
);
}
return $form['profile'];
}
function _hosting_site_form_language($profile = NULL, $platform = null) {
$languages = hosting_get_profile_languages($profile, $platform);
if (sizeof($languages) > 1) {
$form['language'] = array(
'#type' => 'radios',
'#title' => t('Language'),
'#description' => t('The type of site to install.'),
'#options' => $languages,
'#required' => TRUE,
'#default_value' => 'en',
'#attributes' => array(
'class' => "hosting-site-form-language-options",
),
);
}
else {
$form['language'] = array(
'#type' => 'hidden',
'#default_value' => 'en',
'#attributes' => array(
'class' => "hosting-site-form-language-options",
),
);
}
return $form['language'];
}
function _hosting_site_form_populate($element, $value, $value2 = null) {
$form[$element] = call_user_func('_hosting_site_form_' . $element, $value, $value2);
$GLOBALS['devel_shutdown'] = FALSE;
print drupal_to_js(array(
'status' => 'TRUE',
'type' => $form[$element]['#type'],
'data' => drupal_render(form_builder('hosting-site-form', $form)),
));
exit;
}