'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.
The most common use of this functionality is to provide automatic aliasing for www.mysite.com and mysite.com variants of the domain name
'); $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.
'); break; } return $output; } /** * Implementation of hook_form_alter * * Add a textbox to site node forms with a newline * separated list of aliases to the site */ function hosting_alias_form_alter(&$form, $form_state, $form_id) { if (user_access('create site aliases')) { if ($form_id == 'site_node_form') { $form['aliases'] = array( '#type' => 'textarea', '#title' => t('Domain aliases'), '#description' => t('Your site can also be accessed through these domain names. This field requires each domain alias to be on its own line'), '#default_value' => implode("\n", (array) $form['#node']->aliases), '#weight' => 10, ); $form['redirection'] = array( '#type' => 'checkbox', '#title' => t('Redirection'), '#description' => t('Aliases will all be redirected to the main domain if this is checked. Otherwise, all aliases will be accessible transparently.'), '#default_value' => isset($form['#node']->redirection) ? $form['#node']->redirection : variable_get('hosting_alias_redirection', FALSE), ); } } } /** * Retrieve a list of aliases for a site */ 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($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) { 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) { // We need to wipe clean existing aliases if we are not making a new revision if (!$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); } /** * Implementation of hook_nodeapi */ 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) $node)) { form_set_error('aliases', t('The domain name @alias is already in use', array('@alias' => $alias))); } } } break; case 'load': // XXX: this returns only the first redirection status. it // works since they are all set to the same in hook_insert(), // but we should return an associative alias => redirection // array instead $additions['redirection'] = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $node->vid)); // Only retrieves custom aliases, as they are all that can be modified. $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; } } } /** * Implementation of hook_perm */ function hosting_alias_perm() { return array('create site aliases', 'administer hosting aliases'); } /** * Configuration form for aliasing */ 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
resolving all calls to subdomains of your chosen domain, to point at your Drupal 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.hostname?'), '#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('By default, the aliasing system generates aliases in the webserver configuration and creates symlinks in the sites/ directory, point site aliases to the primary domain. The aliasing system can also redirect instead of aliasing, which means all site aliases will redirect the user to the primary domain instead of delivering the primary domain under a symlinked site alias. Note that 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); } /** * Generate a default set of aliases for the site */ 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; } /** * Implementation of hook_allow_domain * * This function will check the existing aliases and the automatically * generated aliases to ensure that this url has not been used before */ function hosting_alias_allow_domain($url, $params = array()) { $query = "SELECT COUNT(n.nid) FROM {node} n LEFT JOIN {hosting_site} h ON h.nid=n.nid LEFT JOIN {hosting_site_alias} a ON n.vid = a.vid WHERE type='site' AND alias='%s' AND h.status <> %d"; $args[] = $url; $args[] = HOSTING_SITE_DELETED; if (isset($params['client'])) { if (isset($params['port'])) { $query .= ' AND ( h.client <> %d OR h.port = %d )'; $args[] = $params['client']; $args[] = $params['port']; } else { $query .= ' AND h.client <> %d'; $args[] = $params['client']; } } // check for ports only if we have no client provided else { if (isset($params['port'])) { $query .= ' AND h.port = %d'; $args[] = $params['port']; } } 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 LEFT JOIN {hosting_site} h ON h.nid=n.nid LEFT JOIN {hosting_site_alias} a ON n.vid = a.vid WHERE type='site' AND alias='%s' AND h.status <> %d"; $args[] = $url; $args[] = HOSTING_SITE_DELETED; if (isset($params['port'])) { $query .= ' AND h.port = %d'; $args[] = $params['port']; } if (isset($params['nid'])) { $query .= ' AND n.nid <> %d'; $args[] = $params['nid']; } return db_result(db_query($query, $args)); }