View source
<?php
define('HOSTING_ALIAS_CUSTOM', 0);
define('HOSTING_ALIAS_AUTOMATIC', 1);
function hosting_alias_menu() {
$items['admin/hosting/aliases'] = array(
'title' => 'Site aliases',
'description' => 'Configure aliases for hosted sites',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hosting_alias_settings',
),
'access arguments' => array(
'administer hosting aliases',
),
'type' => MENU_LOCAL_TASK,
);
$items['hosting_alias/js'] = array(
'title' => 'Javascript Alias Form',
'page callback' => 'hosting_alias_form_js',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function hosting_alias_help($path, $arg) {
switch ($path) {
case 'admin/hosting/aliases':
$output = t('Site aliases allow you to let sites be available through multiple domain addresses.<br /> The most common use of this functionality is to provide automatic aliasing for www.mysite.com and mysite.com variants of the domain name.<br /> ');
$output .= t('This module will also allow you to provide a "temporary url" that sites will always be accessible from, in case of DNS problems.<br />');
$output .= t('Settings made here do not take effect automatically for existing sites.<br />');
break;
}
return $output;
}
function hosting_alias_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'site_node_form') {
$form['#validate'][] = 'hosting_alias_site_form_validate';
return hosting_alias_form_data($form, $form_state);
}
}
function hosting_alias_site_form_validate($form, &$form_state) {
$aliases = $form_state['values']['aliases'];
foreach ($aliases as $key => $alias) {
$length = strlen(trim($alias));
if ($length > HOSTING_MAX_ALIAS_LENGTH) {
$long = $length - HOSTING_MAX_ALIAS_LENGTH;
form_set_error("aliases_wrapper][aliases][{$key}", t('The alias @alias is @long character(s) too long. Please shorten.', array(
'@alias' => $alias,
'@long' => $long,
)));
}
}
}
function hosting_alias_form_data(&$form, &$form_state) {
if (user_access('create site aliases')) {
$automatic_aliases = hosting_alias_get_aliases($form['#node'], HOSTING_ALIAS_AUTOMATIC);
if (sizeof($automatic_aliases)) {
foreach ($automatic_aliases as $link) {
$links[] = l($link, "http://{$link}");
}
$form['aliases_automatic'] = array(
'#type' => 'item',
'#title' => t('Automatic domain aliases'),
'#value' => implode(', ', $links),
'#weight' => 10,
);
}
$form['aliases_wrapper'] = array(
'#tree' => FALSE,
'#title' => t('Domain Aliases'),
'#type' => 'fieldset',
'#prefix' => '<div class="clear-block" id="hosting-aliases-wrapper">',
'#suffix' => '</div>',
);
$form['aliases_wrapper']['aliases'] = array(
'#prefix' => '<div id="hosting-aliases">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (isset($form_state['aliases'])) {
$aliases = array_filter($form_state['aliases']);
}
elseif (isset($form['#node']->aliases)) {
$aliases = array_filter($form['#node']->aliases);
}
else {
$aliases = array();
}
if (isset($form_state['alias_count'])) {
$alias_count = $form_state['alias_count'] + 1;
}
else {
$alias_count = max(1, empty($aliases) ? 1 : count($aliases) + 1);
}
for ($delta = 0; $delta < $alias_count; $delta++) {
$form['aliases_wrapper']['aliases'][$delta] = array(
'#type' => 'textfield',
'#default_value' => $aliases[$delta],
);
}
$form['aliases_wrapper']['add_alias'] = array(
'#type' => 'submit',
'#value' => t('Add an alias'),
'#description' => t("Click here to add another alias."),
'#weight' => 1,
'#submit' => array(
'hosting_alias_add_alias_submit',
),
'#ahah' => array(
'path' => 'hosting_alias/js',
'wrapper' => 'hosting-aliases-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$options = array();
$options[0] = t('No redirection');
if (isset($form['#node']->title)) {
$options[$form['#node']->title] = $form['#node']->title;
}
if (!empty($aliases)) {
$options += array_combine($aliases, $aliases);
}
if (!empty($automatic_aliases)) {
$options += array_combine($automatic_aliases, $automatic_aliases);
}
$default = isset($form_state['values']['redirection']) ? $form_state['values']['redirection'] : $form['#node']->redirection;
$form['aliases_wrapper']['redirection'] = array(
'#type' => 'select',
'#title' => t('Redirect all domain aliases to'),
'#options' => $options,
'#default_value' => $default,
'#weight' => -1,
);
return $form;
}
}
function hosting_alias_add_alias_submit($form, &$form_state) {
node_form_submit_build_node($form, $form_state);
if ($form_state['values']['add_alias']) {
$form_state['values']['aliases'] = array_filter($form_state['values']['aliases']);
$form_state['alias_count'] = max(count($form_state['values']['aliases']), 1);
}
}
function hosting_alias_form_js() {
include_once 'modules/node/node.pages.inc';
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form['#redirect'] = FALSE;
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$alias_form = $form['aliases_wrapper'];
unset($alias_form['#prefix'], $alias_form['#suffix']);
$output = theme('status_messages') . drupal_render($alias_form);
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
}
function hosting_alias_get_aliases($node, $type = null) {
if (!$node->vid) {
return array();
}
$alias = array();
$query = "SELECT alias FROM {hosting_site_alias} WHERE vid=%d";
$args[] = $node->vid;
if (!is_null($type)) {
$query .= " AND automatic=%d";
$args[] = $type;
}
$query .= ' ORDER BY alias ASC';
$result = db_query($query, $args);
while ($obj = db_fetch_object($result)) {
$alias[] = $obj->alias;
}
if (sizeof($alias)) {
return $alias;
}
return array();
}
function hosting_alias_insert($node) {
$automatic = hosting_alias_automatic_aliases(strtolower(trim($node->title)));
if ($node->aliases || sizeof($automatic)) {
$aliases = is_array($node->aliases) ? $node->aliases : explode("\n", str_replace(",", "\n", $node->aliases));
if (is_array($aliases)) {
foreach ($aliases as $alias) {
if (($alias = trim($alias)) && !in_array($alias, $automatic)) {
db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, '%s')", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM, $node->redirection);
}
}
}
if (sizeof($automatic)) {
foreach ($automatic as $alias) {
if (($alias = trim($alias)) && _hosting_valid_fqdn_wildcard($alias)) {
db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, '%s')", $node->vid, $node->nid, $alias, HOSTING_ALIAS_AUTOMATIC, $node->redirection);
}
}
}
}
}
function hosting_alias_update($node) {
if (empty($node->revision)) {
hosting_alias_delete_revision($node);
}
hosting_alias_insert($node);
}
function hosting_alias_delete($node) {
db_query("DELETE FROM {hosting_site_alias} WHERE nid=%d", $node->nid);
}
function hosting_alias_delete_revision($node) {
db_query("DELETE FROM {hosting_site_alias} WHERE nid=%d and vid=%d", $node->nid, $node->vid);
}
function hosting_alias_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->type == 'site') {
switch ($op) {
case 'insert':
hosting_alias_insert($node);
break;
case 'update':
hosting_alias_update($node);
break;
case 'delete':
hosting_alias_delete($node);
break;
case 'delete revision':
hosting_alias_delete_revision($node);
break;
case 'validate':
$aliases = $node->aliases;
foreach ($aliases as $alias) {
if (!module_exists('hosting_subdirs')) {
hosting_alias_validate_alias($node, $alias);
}
}
if ($node->redirection != 0 && !in_array($node->redirection, $aliases) && !($node->redirection == $node->title)) {
form_set_error('redirection', t('The domain name @alias is not an alias for this site.', array(
'@alias' => $node->redirection,
)));
}
break;
case 'load':
$additions['redirection'] = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $node->vid));
$additions['aliases'] = hosting_alias_get_aliases($node, HOSTING_ALIAS_CUSTOM);
return $additions;
break;
case 'view':
$aliases = hosting_alias_get_aliases($node);
if (sizeof($aliases)) {
foreach ($aliases as $link) {
$links[] = l($link, "http://{$link}");
}
$node->content['info']['aliases'] = array(
'#type' => 'item',
'#title' => t('Domain aliases'),
'#value' => implode(', ', $links),
'#weight' => 10,
);
$redirection = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $node->vid));
$node->content['info']['redirection'] = array(
'#type' => 'item',
'#title' => t('Redirection Target'),
'#value' => !empty($redirection) ? l($redirection, "http://{$redirection}") : t('None'),
'#weight' => 10,
);
}
break;
}
}
}
function hosting_alias_validate_alias($site, $alias) {
if ($alias = trim($alias)) {
if (!hosting_domain_allowed($alias, array(
'nid' => $site->nid,
)) || $alias == $site->title) {
form_set_error('aliases', t('The domain name @alias is already in use', array(
'@alias' => $alias,
)));
}
if (!_hosting_valid_fqdn_wildcard($alias)) {
form_set_error('aliases', t('The domain name @alias is not a valid url', array(
'@alias' => $alias,
)));
}
}
}
function hosting_alias_validate_subdir($site, $alias) {
if ($alias = trim($alias)) {
if (!hosting_alias_allow_domain($alias, array(
'nid' => $site->nid,
)) || $alias == $site->title) {
form_set_error('aliases', t('The domain name @alias is already in use', array(
'@alias' => $alias,
)));
}
if (!_hosting_valid_fqdn_subdir($alias)) {
form_set_error('aliases', t('The domain name @alias is not a valid subdir url', array(
'@alias' => $alias,
)));
}
}
}
function hosting_alias_perm() {
return array(
'create site aliases',
'administer hosting aliases',
);
}
function hosting_alias_settings() {
$form['hosting_alias_subdomain'] = array(
'#type' => 'textfield',
'#title' => t('Domain used for automatic subdomain hosting'),
'#description' => t('To be able to provide a temporary url for your sites, you need to have configured a wild card dns entry<br /> resolving all calls to subdomains of your chosen domain, to point at your web server.'),
'#default_value' => variable_get('hosting_alias_subdomain', ''),
);
$form['hosting_alias_automatic_www'] = array(
'#type' => 'checkbox',
'#title' => t('Generate www.domain.com alias automatically'),
'#description' => t('If a domain name does not start with www., automatically create an alias for www.domain?'),
'#default_value' => variable_get('hosting_alias_automatic_www', FALSE),
);
$form['hosting_alias_automatic_no_www'] = array(
'#type' => 'checkbox',
'#title' => t('Generate domain.com alias automatically'),
'#description' => t('If a domain name starts with www., automatically create an alias for domain.com?'),
'#default_value' => variable_get('hosting_alias_automatic_no_www', FALSE),
);
$form['hosting_alias_redirection'] = array(
'#type' => 'checkbox',
'#title' => t('Use redirects instead of aliases by default'),
'#description' => t('Instead of serving the primary domain under a symlinked site alias, this module can also redirect the user to the primary domain from an alias. This setting can be controlled per site. Setting this option here will make redirection the default behavior for site aliases.'),
'#default_value' => variable_get('hosting_alias_redirection', FALSE),
);
return system_settings_form($form);
}
function hosting_alias_automatic_aliases($url) {
$alias = array();
if ($sub = variable_get('hosting_alias_subdomain', FALSE)) {
if (!preg_match("/\\.{$sub}\$/", $url)) {
$alias[] = str_replace(array(
'-',
'.',
), array(
'--',
'-',
), $url) . "." . trim($sub, ".");
}
}
if (!preg_match('/^www\\./', $url) && variable_get('hosting_alias_automatic_www', FALSE)) {
$alias[] = "www." . $url;
}
elseif (preg_match('/^www\\./', $url) && variable_get('hosting_alias_automatic_no_www', FALSE)) {
$alias[] = str_replace("www.", "", $url);
}
return $alias;
}
function hosting_alias_allow_domain($url, $params = array()) {
$query = "SELECT COUNT(n.nid) FROM {node} n \n LEFT JOIN {hosting_site} h ON h.nid=n.nid \n LEFT JOIN {hosting_site_alias} a ON n.vid = a.vid \n WHERE \n type='site' AND alias='%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;
}