domain_alias.admin.inc
<?php
function domain_alias($domain) {
if (!is_array($domain)) {
return t('Invalid page requested.');
}
return drupal_get_form('domain_alias_form', $domain);
}
function domain_alias_form($form_state, $domain, $arguments = array()) {
$form = array();
drupal_set_title(t('Edit domain aliases: %domain', array('%domain' => $domain['subdomain'])));
$form['domain_id'] = array('#type' => 'value', '#value' => $domain['domain_id']);
$record_edit_url = 'admin/build/domain/'. ($domain['domain_id'] == 0 ? '' : 'edit/'. $domain['domain_id']);
$form['domain_help'] = array(
'#type' => 'markup',
'#value' => domain_alias_help_text(),
);
$form['domain'] = array(
'#type' => 'markup',
'#value' => t('Registered aliases for <a href="!url"%title</a>', array('!url' => url($record_edit_url), '%title' => $domain['subdomain'])),
);
$form['domain_alias'] = array(
'#tree' => TRUE,
);
if (isset($domain['aliases']) && is_array($domain['aliases'])) {
foreach ($domain['aliases'] as $alias_id => $alias) {
$form['domain_alias'][$alias_id] = array(
'#tree' => TRUE,
);
$form['domain_alias'][$alias_id]['alias_id'] = array(
'#type' => 'value',
'#value' => $alias_id,
);
$form['domain_alias'][$alias_id]['redirect'] = array(
'#type' => 'checkbox',
'#default_value' => $alias['redirect'],
);
$form['domain_alias'][$alias_id]['pattern'] = array(
'#type' => 'textfield',
'#default_value' => $alias['pattern'],
'#maxlength' => 255,
'#width' => 40,
);
$form['domain_alias'][$alias_id]['delete'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
);
}
}
$form['domain_new'] = array(
'#type' => 'markup',
'#value' => t('Add new aliases'),
);
$form['domain_new_help'] = array(
'#type' => 'markup',
'#value' => t('To create a new alias, enter the matching pattern. Check the <em>redirect</em> box if you would like requests made to the alias to redirect to the registered domain.
<em>You may enter up to five (5) aliases at a time.</em>'),
);
$form['domain_alias_new'] = array(
'#tree' => TRUE,
);
for ($i = 0; $i < 5; $i++) {
$form['domain_alias_new'][$i]['redirect'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
);
$form['domain_alias_new'][$i]['pattern'] = array(
'#type' => 'textfield',
'#default_value' => NULL,
'#maxlength' => 255,
'#width' => 40,
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Save aliases'));
return $form;
}
function domain_alias_form_validate($form, &$form_state) {
$aliases = array();
if (isset($form_state['values']['domain_alias'])) {
foreach ($form_state['values']['domain_alias'] as $count => $alias) {
$validate = TRUE;
$original_alias = domain_alias_lookup(NULL, $count);
if ($original_alias['pattern'] == $alias['pattern']) {
$validate = FALSE;
}
if ($original_alias['redirect'] != $alias['redirect']) {
$validate = FALSE;
$aliases[] = 'update placeholder';
}
if ($form_state['values']['domain_alias'][$count]['delete']) {
$aliases[] = 'delete placeholder';
$validate = FALSE;
}
if ($validate) {
$aliases[] = _domain_alias_validate($form, $alias, $count, $aliases, 'domain_alias');
}
}
}
foreach ($form_state['values']['domain_alias_new'] as $count => $alias) {
if (empty($alias['pattern'])) {
continue;
}
$aliases[] = _domain_alias_validate($form, $alias, $count, $aliases, 'domain_alias_new');
}
if (empty($aliases)) {
form_error($form['domain'], t('No changes were made.'));
}
}
function _domain_alias_validate($form, $alias, $id, $aliases, $type) {
$alias = $alias['pattern'];
if (in_array($alias, $aliases)) {
form_error($form[$type][$id]['pattern'], t('%name is already defined. You must use unique values.', array('%name' => $alias)));
return;
}
$count = substr_count($alias, '*') + substr_count($alias, '?');
if ($count > 1) {
form_error($form[$type][$id]['pattern'], t('You may only have one wildcard character in each alias.'));
return;
}
$count = substr_count($alias, ':');
if ($count > 1) {
form_error($form[$type][$id]['pattern'], t('You may only have one colon ":" character in each alias.'));
return;
}
else if ($count == 1) {
$int = substr($alias, strpos($alias, ':') + 1);
if (!is_numeric($int)) {
form_error($form[$type][$id]['pattern'], t('A colon may only be followed by an integer indicating the proper port.'));
return;
}
}
$check = preg_match('/^[a-z0-9\.\+\-\*\?:]*$/', $alias);
if ($check == 0) {
form_error($form[$type][$id]['pattern'], t('%name contains invalid characters.', array('%name' => $alias)));
}
$check = preg_match('/[a-z0-9\.\+\-:]*$/', $alias);
if ($check == 1) {
$test = db_result(db_query("SELECT COUNT(*) FROM {domain} WHERE subdomain = '%s'", $alias));
if ($test > 0) {
form_error($form[$type][$id]['pattern'], t('%name matches an existing domain record. ', array('%name' => $alias)));
}
}
$replace = array('.' => '\.', '+' => '\+', '-' => '\-', '*' => '[a-z0-9\.\+\-]*', '?' => '[a-z0-9\.\+\-]?');
$_pattern = str_replace(array_keys($replace), $replace, $alias);
foreach ($aliases as $_alias) {
$_alias_pattern = str_replace(array_keys($replace), $replace, $_alias);
if (preg_match('@'. $_pattern .'@i', $_alias) || preg_match('@'. $_alias_pattern .'@i', $alias)) {
form_error($form[$type][$id]['pattern'], t('%name matches %existing. You must use unique values.', array('%name' => $alias, '%existing' => $_alias)));
break;
}
}
$_pattern = _domain_alias_placeholders_to_sql($alias);
if (isset($form_state['values']['domain_id']) && $_pattern != $form_state['values']['domain_id']) {
$_alias = db_fetch_array(db_query("SELECT alias_id, domain_id, pattern FROM {domain_alias} WHERE pattern = '%s' OR pattern = '%s'", $_pattern, $form_state['values']['domain_id']));
}
else {
$_alias = db_fetch_array(db_query("SELECT alias_id, domain_id, pattern FROM {domain_alias} WHERE pattern = '%s'", $_pattern));
}
if (!empty($_alias)) {
form_error($form[$type][$id]['pattern'],
t('%name matches <a href="!url" title="Edit aliases for domain !id">alias #!aid</a> (%existing). You must use unique values. ',
array(
'%name' => $alias,
'%existing' => _domain_alias_placeholders_from_sql($_alias['pattern']),
'!url' => url('admin/build/domain/alias/'. $_alias['domain_id']),
'!id' => $_alias['domain_id'],
'!aid' => $_alias['alias_id']
)
)
);
}
return $alias;
}
function domain_alias_form_submit($form, &$form_state) {
foreach ($form_state['values']['domain_alias_new'] as $id => $alias) {
if (!empty($alias['pattern'])) {
$alias['pattern'] = _domain_alias_placeholders_to_sql($alias['pattern']);
db_query("INSERT INTO {domain_alias} (domain_id, pattern, redirect) VALUES ('%d', '%s', '%d')", $form_state['values']['domain_id'], $alias['pattern'], intval($alias['redirect']));
}
}
if (isset($form_state['values']['domain_alias'])) {
foreach ($form_state['values']['domain_alias'] as $id => $alias) {
$alias['pattern'] = _domain_alias_placeholders_to_sql($alias['pattern']);
if ($alias['delete']) {
db_query("DELETE FROM {domain_alias} WHERE alias_id = %d", $id);
}
else {
db_query("UPDATE {domain_alias} SET pattern = '%s', redirect = '%d' WHERE alias_id = %d", $alias['pattern'], intval($alias['redirect']), $id);
}
}
}
drupal_set_message(t('Domain aliases updated successfully.'));
cache_clear_all();
}
function theme_domain_alias_form($form) {
$output = '';
$redirect = t('Check the redirect box to send requests for an alias to the registered domain.');
$output .= drupal_render($form['domain_help']);
$output .= '<br /><h3>'. drupal_render($form['domain']) .'</h3>';
$elements = element_children($form['domain_alias']);
if (!empty($elements)) {
$header = array(t('Id'), t('Redirect'), t('Pattern'), t('Delete'));
$rows = array();
foreach ($elements as $element) {
$rows[] = array(
$form['domain_alias'][$element]['alias_id']['#value'],
drupal_render($form['domain_alias'][$element]['redirect']),
drupal_render($form['domain_alias'][$element]['pattern']),
drupal_render($form['domain_alias'][$element]['delete']),
);
}
$output .= theme('table', $header, $rows);
$output .= '<p><em>'. $redirect .'</em></p>';
}
else {
$output .= '<p>'. t('There are no aliases recorded for this domain.') .'</p>';
}
$output .= '<br /><h3>'. drupal_render($form['domain_new']) .'</h3>';
$output .= '<p>'. drupal_render($form['domain_new_help']) .'</p>';
$header = array(t('Redirect'), t('Pattern'));
$rows = array();
foreach (element_children($form['domain_alias_new']) as $element) {
$rows[] = array(
drupal_render($form['domain_alias_new'][$element]['redirect']),
drupal_render($form['domain_alias_new'][$element]['pattern']),
);
}
$output .= theme('table', $header, $rows);
$output .= '<p><em>'. $redirect .'</em></p>';
$output .= drupal_render($form);
return $output;
}
function domain_alias_help_text() {
$output = t('<p>A domain alias is used to register multiple valid domain names to a single record within Domain Access.
You may enter as many unique aliases per domain as you wish. </p>
<p>You may specify a pattern for your domains by using <strong>*</strong> (asterisk) to match any number of random
characters and <strong>?</strong> (question mark) to match exactly one random character.
For example: <em>*.example.com</em> would match any HTTP request made to a subdomain of <em>example.com</em>
to the domain record for <em>example.com</em>. NOTE: <em>Only one wildcard is allowed per alias.</em></p>');
return $output;
}