You are here

hosting_alias.module in Hosting 5

File

alias/hosting_alias.module
View source
<?php

define('HOSTING_ALIAS_CUSTOM', 0);
define('HOSTING_ALIAS_AUTOMATIC', 1);

/**
 * Implementation of hook_menu
 */
function hosting_alias_menu($may_cache) {
  $items[] = array(
    'path' => 'admin/hosting/aliases',
    'title' => t('Site aliases'),
    'description' => t('Configure aliases for hosted sites'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'hosting_alias_settings',
    ),
    'access' => user_access('administer hosting aliases'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}
function hosting_alias_help($path) {
  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 />');
      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_id, &$form) {
  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 to be alias to be on it\'s own line'),
        '#default_value' => implode("\n", (array) $form['#node']->aliases),
      );
    }
  }
}

/**
 * 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) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM);
        }
      }
    }
    if (sizeof($automatic)) {
      foreach ($automatic as $alias) {
        db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_AUTOMATIC);
      }
    }
  }
}
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_site_exists($alias, $node->nid)) {
              form_set_error('aliases', t('The domain name @alias is already in use by another site', array(
                '@alias' => $alias,
              )));
            }
          }
        }
        break;
      case 'load':

        // 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,
          );
        }
        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<br /> 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),
  );
  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, $nid = null) {
  $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 ($nid) {
    $query .= ' AND n.nid <> %d';
    $args[] = $nid;
  }
  return db_result(db_query($query, $args));
}

Functions

Namesort descending Description
hosting_alias_allow_domain Implementation of hook_allow_domain
hosting_alias_automatic_aliases Generate a default set of aliases for the site
hosting_alias_delete
hosting_alias_delete_revision
hosting_alias_form_alter Implementation of hook_form_alter
hosting_alias_get_aliases Retrieve a list of aliases for a site
hosting_alias_help
hosting_alias_insert
hosting_alias_menu Implementation of hook_menu
hosting_alias_nodeapi Implementation of hook_nodeapi
hosting_alias_perm Implementation of hook_perm
hosting_alias_settings Configuration form for aliasing
hosting_alias_update

Constants