$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 ', array('!url' => url($record_edit_url), '%title' => $domain['subdomain'])),
);
$form['domain_alias'] = array(
'#tree' => TRUE,
);
// List all existing aliases
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 redirect box if you would like requests made to the alias to redirect to the registered domain.
You may enter up to five (5) aliases at a time.'),
);
$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;
}
/**
* FAPI for domain_alias_form()
*/
function domain_alias_form_validate($form, &$form_state) {
// Validate aliases
$aliases = array();
// Validate updates -- this array might not have data.
if (isset($form_state['values']['domain_alias'])) {
foreach ($form_state['values']['domain_alias'] as $count => $alias) {
$validate = TRUE;
// Delete requests and unchanged aliases do not need the validation step.
$original_alias = domain_alias_lookup(NULL, $count);
if ($original_alias['pattern'] == $alias['pattern']) {
// In this case, no change and no error set, unless no changes made.
$validate = FALSE;
}
if ($original_alias['redirect'] != $alias['redirect']) {
// In this case, we updated the redirect settings only.
$validate = FALSE;
$aliases[] = 'update placeholder';
}
if ($form_state['values']['domain_alias'][$count]['delete']) {
// Set a value so we do not return an error on empty array.
$aliases[] = 'delete placeholder';
$validate = FALSE;
}
if ($validate) {
// Run the validation routine.
$aliases[] = _domain_alias_validate($form, $alias, $count, $aliases, 'domain_alias');
}
}
}
// Validate new domain aliases -- this array should always have data.
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.'));
}
}
/**
* Helper function to validate alias entries.
*
* @param $alias
* The form element defining the alias.
* @param $id
* The form array id, either a number or an alias_id.
* @param $aliases
* The array of currently processed alias strings.
* @param $type
* The type of alias, used to determine form errors.
* @return
* A valid alias string, or an error.
*/
function _domain_alias_validate($form, $alias, $id, $aliases, $type) {
$alias = $alias['pattern'];
// 1) Check that the same alias is not entered twice.
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;
}
// 2) Check that the alias only has one wildcard.
$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;
}
// 3) Only one colon allowed, and it must be followed by numbers only.
$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;
}
}
// 4) Check that the alias doesn't contain any invalid characters.
$check = preg_match('/^[a-z0-9\.\+\-\*\?:]*$/', $alias);
if ($check == 0) {
form_error($form[$type][$id]['pattern'], t('%name contains invalid characters.', array('%name' => $alias)));
}
// 5) Check that the alias is not a direct match for a registered domain.
$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)));
}
}
// 6) Check that the alias doesn't match another alias defined for this domain.
$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;
}
}
// 7) Check that the alias or a pattern matching the same domain name does not exist.
$_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 alias #!aid (%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;
}
/**
* Forms_API for domain_alias_form().
*/
function domain_alias_form_submit($form, &$form_state) {
// The new alias fields are always present.
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.'));
// Clear the cache.
cache_clear_all();
}
/**
* Form theming.
*/
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 .= '
'. $redirect .'
'; } else { $output .= ''. t('There are no aliases recorded for this domain.') .'
'; } // Add new records. $output .= ''. drupal_render($form['domain_new_help']) .'
'; $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 .= ''. $redirect .'
'; $output .= drupal_render($form); return $output; } /** * Help text for the form. */ function domain_alias_help_text() { $output = t('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.
You may specify a pattern for your domains by using * (asterisk) to match any number of random characters and ? (question mark) to match exactly one random character. For example: *.example.com would match any HTTP request made to a subdomain of example.com to the domain record for example.com. NOTE: Only one wildcard is allowed per alias.
'); return $output; }