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,
);
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') {
return hosting_alias_form_data($form);
}
}
function hosting_alias_form_data(&$form) {
if (user_access('create site aliases')) {
$aliases = hosting_alias_get_aliases($form['#node'], HOSTING_ALIAS_AUTOMATIC);
if (sizeof($aliases)) {
foreach ($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'] = array(
'#type' => 'textarea',
'#title' => t('Domain aliases'),
'#description' => t('The site can also be accessed through these domain names, one per line.'),
'#default_value' => implode("\n", (array) $form['#node']->aliases),
'#weight' => 10,
);
$form['redirection'] = array(
'#type' => 'checkbox',
'#title' => t('Redirect domain aliases to main domain'),
'#default_value' => isset($form['#node']->redirection) ? $form['#node']->redirection : variable_get('hosting_alias_redirection', FALSE),
'#weight' => 11,
);
return $form;
}
}
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, %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM, $node->redirection);
}
}
}
if (sizeof($automatic)) {
foreach ($automatic as $alias) {
if (($alias = trim($alias)) && _hosting_valid_fqdn($alias)) {
db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, %d)", $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 = explode("\n", $node->aliases);
foreach ($aliases as $alias) {
if ($alias = trim($alias)) {
if (!hosting_domain_allowed($alias, array(
'nid' => $node->nid,
)) || $alias == $node->title) {
form_set_error('aliases', t('The domain name @alias is already in use', array(
'@alias' => $alias,
)));
}
if (!_hosting_valid_fqdn($alias)) {
form_set_error('aliases', t('The domain name @alias is not a valid url', array(
'@alias' => $alias,
)));
}
}
}
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'),
'#value' => $redirection['redirection'] ? t('Yes') : t('No'),
'#weight' => 10,
);
}
break;
}
}
}
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;
}
function hosting_alias_count_sites($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'];
}
return db_result(db_query($query, $args));
}