View source
<?php
define('DOMAIN_INSTALL_RULE', TRUE);
define('DOMAIN_SITE_GRANT', TRUE);
define('DOMAIN_ASSIGN_USERS', TRUE);
define('DOMAIN_LIST_SIZE', 25);
function domain_boot() {
global $user;
$user->domain_user = domain_get_user_domains($user);
include_once 'settings_custom_url.inc';
}
function domain_init() {
global $_domain, $conf;
if (!is_array($_domain)) {
$_domain = array();
}
if (!isset($_domain['domain_id'])) {
$_domain = domain_default(TRUE);
$_domain['error'] = 'bootstrap include';
}
if (!isset($error) && isset($_domain['error'])) {
$error = 'Domain access failed to load during phase: ' . $_domain['error'] . '. Please check your settings.php file and site configuration.';
if (empty($_POST)) {
$hide = variable_get('domain_hide_errors', FALSE);
if (user_access('administer domains') && empty($hide)) {
drupal_set_message($error, 'error');
}
if (empty($hide)) {
watchdog('domain', $error, NULL, WATCHDOG_ERROR);
}
}
}
domain_node_save_redirect();
$www_replaced = FALSE;
if (variable_get('domain_www', 0) && strpos($_domain['subdomain'], 'www.') !== FALSE) {
$_domain['subdomain'] = str_replace('www.', '', $_domain['subdomain']);
$www_replaced = TRUE;
}
$domain = domain_lookup($_domain['domain_id'], NULL, TRUE);
$_domain = array_merge($domain, $_domain);
domain_initial_domain($_domain);
if ($www_replaced) {
drupal_goto(domain_get_uri($_domain));
}
if (empty($_domain['valid'])) {
domain_invalid_domain_requested();
}
$conf['site_name'] = $_domain['sitename'];
}
function domain_initial_domain($domain = array()) {
static $initial;
if (!isset($initial) && !empty($domain)) {
$initial = $domain;
}
return $initial;
}
function domain_unserialize($object) {
if (empty($object)) {
return;
}
return unserialize(db_decode_blob($object));
}
function domain_menu() {
$items = array();
$admin = user_access('administer domains');
$items['admin/build/domain'] = array(
'title' => 'Domains',
'access arguments' => array(
'administer domains',
),
'page callback' => 'domain_view',
'file' => 'domain.admin.inc',
'description' => 'Settings for the Domain Access module.',
);
$items['admin/build/domain/view'] = array(
'title' => 'Domain list',
'access arguments' => array(
'administer domains',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'page callback' => 'domain_view',
'file' => 'domain.admin.inc',
'weight' => -10,
);
$items['admin/build/domain/settings'] = array(
'title' => 'Settings',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_configure',
'file' => 'domain.admin.inc',
'weight' => -8,
);
$items['admin/build/domain/create'] = array(
'title' => 'Create domain record',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_form',
),
'file' => 'domain.admin.inc',
'weight' => -7,
);
$items['admin/build/domain/advanced'] = array(
'title' => 'Node settings',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_advanced_form',
),
'file' => 'domain.admin.inc',
'weight' => -6,
);
$batch = module_invoke_all('domainbatch');
if (!empty($batch)) {
$items['admin/build/domain/batch'] = array(
'title' => 'Batch updating',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_batch',
'file' => 'domain.admin.inc',
'weight' => -5,
);
foreach ($batch as $key => $value) {
$items['admin/build/domain/batch/' . $key] = array(
'title' => $value['#form']['#title'],
'access arguments' => isset($value['#permission']) ? array(
$value['#permission'],
) : array(
'administer domains',
),
'type' => MENU_CALLBACK,
'page callback' => 'domain_batch',
'page arguments' => array(
$key,
),
'file' => 'domain.admin.inc',
'weight' => isset($value['#weight']) ? $value['#weight'] : 0,
);
}
}
$items['admin/build/domain/roles'] = array(
'title' => 'User defaults',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_roles_form',
),
'file' => 'domain.admin.inc',
'weight' => -4,
);
$items['admin/build/domain/edit/%domain'] = array(
'title' => 'Edit domain record',
'access arguments' => array(
'administer domains',
),
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_form',
4,
),
'file' => 'domain.admin.inc',
);
$items['admin/build/domain/delete/%domain'] = array(
'title' => 'Delete domain record',
'access arguments' => array(
'administer domains',
),
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_delete_form',
4,
),
'file' => 'domain.admin.inc',
);
return $items;
}
function domain_perm() {
$perms = array(
'access inactive domains',
'administer domains',
'assign domain editors',
'delete domain nodes',
'edit domain nodes',
'set domain access',
'publish to any assigned domain',
'publish from assigned domain',
'publish from default domain',
);
return $perms;
}
function domain_theme() {
$themes = array(
'domain_admin_users_form' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'domain.admin.inc',
),
'domain_batch_form' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'domain.admin.inc',
),
'domain_batch_title' => array(
'arguments' => array(
'batch' => array(),
),
'file' => 'domain.admin.inc',
),
'domain_roles_form' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'domain.admin.inc',
),
);
return $themes;
}
function domain_block($op = 'list', $delta = 0, $edit = array()) {
global $_domain, $base_url;
$blocks = array();
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Domain switcher'),
);
$blocks[1] = array(
'info' => t('Domain access information'),
);
$blocks['server'] = array(
'info' => t('Domain access server information'),
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
break;
case 'view':
switch ($delta) {
case '0':
$block['subject'] = t('Domain switcher');
$items = array();
$domains = domain_domains();
$msg = FALSE;
foreach ($domains as $domain) {
if ($domain['valid']) {
$title = $domain['sitename'];
$allow = TRUE;
}
else {
$title = $domain['sitename'] . ' *';
$allow = FALSE;
if (user_access('access inactive domains')) {
$msg = TRUE;
$allow = TRUE;
}
}
if ($allow) {
$items[] = l($title, domain_get_uri($domain), array(
'absolute' => TRUE,
));
}
}
$block['content'] = theme('item_list', $items);
if ($msg) {
$block['content'] .= t('<em>* Inactive domain.</em>');
}
break;
case '1':
$block['content'] = '';
if (arg(0) == 'node' && is_numeric(arg(1))) {
$block['subject'] = t('Domain access information');
$this_node = node_load(arg(1));
$output = '';
if (!empty($this_node->subdomains)) {
$items = array();
foreach ($this_node->subdomains as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', $items, t('Assigned domains'));
}
$this_domain = domain_get_node_match($this_node->nid);
$output .= theme('item_list', array(
check_plain($this_domain['sitename']),
), t('Source domain'));
if (empty($output)) {
$output = t('This node is not assigned to a domain.');
}
$block['content'] = '<p>' . t('%node is published with the following Domain Access rules:', array(
'%node' => $this_node->title,
)) . '</p>' . $output;
}
break;
case 'server':
$output = '';
$header = array(
t('Property'),
t('Value'),
);
$rows = array();
$rows[] = array(
t('HTTP_HOST request'),
check_plain($_SERVER['HTTP_HOST']),
);
$check = domain_lookup(NULL, $_SERVER['HTTP_HOST']);
$match = t('TRUE');
if ($check == -1) {
if (isset($_domain['active_alias_id'])) {
$match = t('ALIAS: Using alias %id', array(
'%id' => $_domain['active_alias_id'],
));
}
else {
$match = t('FALSE: Using default domain.');
}
}
$rows[] = array(
t('Domain match'),
$match,
);
$rows[] = array(
t('is_default'),
$_domain['domain_id'] < 1,
);
foreach ($_domain as $key => $value) {
if (is_null($value)) {
$value = t('NULL');
}
elseif ($value === TRUE) {
$value = t('TRUE');
}
elseif ($value === FALSE) {
$value = t('FALSE');
}
$rows[] = array(
check_plain($key),
!is_array($value) ? check_plain($value) : _domain_block_print_array($value),
);
}
$output = theme('table', $header, $rows);
$block = array(
'subject' => t('Domain server information'),
'content' => $output,
);
break;
}
return $block;
break;
}
}
function _domain_block_print_array($array) {
$items = array();
foreach ($array as $key => $val) {
$value = 'array';
if (!is_array($val)) {
$value = check_plain($val);
}
else {
$list = array();
foreach ($val as $k => $v) {
$list[] = t('@key : @value', array(
'@key' => $k,
'@value' => $v,
));
}
$value = implode('<br />', $list);
}
$items[] = t('@key : !value', array(
'@key' => $key,
'!value' => $value,
));
}
return theme('item_list', $items);
}
function domain_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'load':
$domains = domain_get_user_domains($account);
$account->domain_user = $domains;
break;
case 'form':
case 'register':
if (is_null($category) || $category == 'account') {
global $_domain;
$result = db_query("SELECT domain_id, subdomain, sitename, scheme FROM {domain}");
$options = array();
$add_roles = variable_get('domain_add_roles', 0);
if ($op == 'register') {
$add_roles = TRUE;
}
$account->domain_user = domain_get_user_domains($account, $add_roles, TRUE);
if (empty($account->uid)) {
$_domain['domain_id'] == 0 ? $default = array(
-1,
) : ($default = array(
$_domain['domain_id'] => $_domain['domain_id'],
));
}
else {
$default = $account->domain_user;
}
$options[-1] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
while ($data = db_fetch_array($result)) {
$options[$data['domain_id']] = check_plain($data['sitename']);
}
unset($options[0]);
$format = domain_select_format();
if (user_access('assign domain editors')) {
$form['domain_user'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 1,
);
$form['domain_user']['domain_user'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#options' => $options,
'#title' => t('Domain access settings'),
'#description' => t('Select the affiliates that this user belongs to. Used to grant editing permissions for users with the "edit domain nodes" permission.'),
'#default_value' => $default,
);
if ($format) {
$form['domain_user']['domain_user']['#multiple'] = TRUE;
$form['domain_user']['domain_user']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
else {
$form['domain_user'] = array(
'#type' => 'value',
'#value' => $default,
);
}
return $form;
}
break;
case 'validate':
return array(
'domain_user' => $edit['domain_user'],
);
break;
case 'insert':
case 'update':
if (!isset($edit['domain_user'])) {
return;
}
db_query("DELETE FROM {domain_editor} WHERE uid = %d", $account->uid);
if (empty($edit['domain_user'])) {
return;
}
foreach ($edit['domain_user'] as $domain_id => $status) {
if ($status != 0) {
if ($domain_id == -1) {
$domain_id = 0;
}
db_query("INSERT INTO {domain_editor} (uid, domain_id) VALUES (%d, %d)", $account->uid, $domain_id);
}
}
$edit['domain_user'] = NULL;
break;
case 'view':
if (user_access('assign domain editors')) {
$account->content['domain'] = array(
'#type' => 'user_profile_category',
'#weight' => 10,
'#title' => t('Domain status'),
);
if (empty($account->domain_user)) {
$output = t('This user is not assigned to a domain.');
}
else {
$output = '<ul>';
foreach ($account->domain_user as $id) {
if (abs($id) > 0) {
if ($id > 0) {
$domain = domain_lookup($id);
$output .= '<li>' . check_plain($domain['sitename']) . '</li>';
}
else {
$output .= '<li>' . check_plain(variable_get('domain_sitename', variable_get('site_name', 'Drupal'))) . '</li>';
}
}
}
$output .= '</ul>';
}
$account->content['domain']['domain_settings'] = array(
'#type' => 'user_profile_item',
'#title' => t('Domain settings'),
'#value' => $output,
);
}
break;
case 'delete':
db_query("DELETE FROM {domain_editor} WHERE uid = %d", $account->uid);
break;
}
}
function domain_user_operations() {
if (!user_access('assign domain editors')) {
return;
}
return array(
'domain' => array(
'label' => t('Assign users to domains'),
'callback' => 'domain_user_operation_assign',
),
);
}
function domain_form_user_admin_account_alter(&$form, $form_state) {
global $_domain;
if (!user_access('assign domain editors')) {
return;
}
$options = array();
$format = domain_select_format();
foreach (domain_domains() as $data) {
$data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);
if ($data['valid'] || user_access('access inactive domains')) {
$options[$key] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Affiliate editor options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#prefix' => '<div class="description">' . t('If you select <em>Assign users to domains</em> above, you should confirm the <em>Affiliate editor options</em> settings below.') . '</div>',
'#weight' => -1,
);
$form['domain']['behavior'] = array(
'#type' => 'radios',
'#title' => t('Update behavior'),
'#options' => array(
0 => t('Replace old values with new settings'),
1 => t('Add new settings to existing values'),
2 => t('Remove selected domains from existing values'),
),
'#description' => t('Defines how new grants will be applied to the updated users.'),
'#default_value' => 0,
);
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Assign to'),
'#options' => $options,
'#required' => FALSE,
'#description' => t('Select which affiliates these users should belong to. <em>Note: this will erase any current assignment for the selsected users.</em>'),
'#default_value' => array(
$_domain['domain_id'] == 0 ? -1 : $_domain['domain_id'],
),
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
if (!empty($form['name'])) {
foreach (array_keys($form['name']) as $uid) {
$form['user_domains'][$uid][0] = array(
'#value' => theme('item_list', _domain_user_list($uid)),
);
}
}
$form['#theme'] = 'domain_admin_users_form';
$form['#submit'][] = 'domain_update_users';
}
function _domain_user_list($uid) {
$temp_account = new stdClass();
$temp_account->uid = $uid;
$list = domain_get_user_domains($temp_account, FALSE);
$domains = array();
foreach ($list as $domain_id) {
if ($domain_id == -1) {
$domain_id = 0;
}
$domains[] = check_plain(db_result(db_query("SELECT sitename FROM {domain} WHERE domain_id = %d ORDER BY domain_id ASC", $domain_id)));
}
return $domains;
}
function domain_user_operation_assign($accounts) {
}
function domain_update_users($form, &$form_state) {
$values = $form_state['values'];
if ($values['operation'] != 'domain') {
return;
}
$add_roles = variable_get('domain_add_roles', 0);
$domains = array_filter($values['domains']);
foreach ($values['accounts'] as $uid) {
if (!empty($form_state['values']['behavior'])) {
$account = new stdClass();
$account->uid = $uid;
$current = domain_get_user_domains($account, $add_roles, TRUE);
if ($form_state['values']['behavior'] == 1) {
$domains += $current;
}
else {
foreach ($domains as $domain_id) {
if (isset($current[$domain_id])) {
unset($current[$domain_id]);
}
}
$domains = $current;
}
}
db_query("DELETE FROM {domain_editor} WHERE uid = %d", $uid);
foreach ($domains as $domain_id) {
if ($domain_id == -1) {
$domain_id = 0;
}
db_query("INSERT INTO {domain_editor} (uid, domain_id) VALUES (%d, %d)", $uid, $domain_id);
}
}
}
function domain_cron() {
global $_domain;
$modules = module_implements('domaincron');
if (!empty($modules)) {
$domains = domain_domains();
foreach ($domains as $domain) {
domain_set_domain($domain['domain_id'], TRUE);
foreach ($modules as $module) {
module_invoke($module, 'domaincron', $domain);
}
}
domain_reset_domain(TRUE);
}
}
function domain_load($domain_id = NULL) {
$domain = domain_lookup($domain_id);
if ($domain == -1) {
return FALSE;
}
else {
return $domain;
}
}
function domain_save($values, $edit) {
$update = isset($values['domain_id']) && is_numeric($values['domain_id']) ? array(
'domain_id',
) : array();
if (!empty($update)) {
$action = 'update';
}
else {
$action = 'create';
}
drupal_write_record('domain', $values, $update);
$domain = domain_lookup(NULL, $values['subdomain']);
module_invoke_all('domainupdate', $action, $domain, $edit);
return $domain;
}
function domain_lookup($domain_id = NULL, $subdomain = NULL, $reset = FALSE) {
static $domains;
if (is_null($domain_id) && is_null($subdomain)) {
return -1;
}
$key = $domain_id . $subdomain;
if (!isset($domains[$key]) || $reset) {
if ($subdomain) {
$domain = db_fetch_array(db_query("SELECT domain_id, subdomain, sitename, scheme, valid FROM {domain} WHERE subdomain = '%s'", $subdomain));
}
else {
if ($domain_id == 0) {
$domain = domain_default();
}
else {
$domain = db_fetch_array(db_query("SELECT domain_id, subdomain, sitename, scheme, valid FROM {domain} WHERE domain_id = %d", $domain_id));
}
}
if (isset($domain['domain_id'])) {
$domains[$key] = domain_api($domain, $reset);
}
else {
$domains[$key] = -1;
}
}
return $domains[$key];
}
function domain_default($reset = FALSE, $alter = TRUE) {
static $default;
if (empty($default) || $reset) {
$default['domain_id'] = 0;
$default['sitename'] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
$default['subdomain'] = variable_get('domain_root', '');
$default['scheme'] = variable_get('domain_scheme', 'http');
$default['valid'] = TRUE;
if ($alter) {
$default = domain_api($default, $reset);
}
}
return $default;
}
function domain_set_primary_domain() {
$root = strtolower(rtrim($_SERVER['SERVER_NAME']));
$site = variable_get('site_name', 'Drupal');
$scheme = 'http';
if (!empty($_SERVER['HTTPS'])) {
$scheme = 'https';
}
db_query("UPDATE {domain} SET subdomain = '%s', sitename = '%s', scheme = '%s', valid = 1 WHERE domain_id = 0", $root, $site, $scheme);
if (!db_affected_rows()) {
db_query("INSERT INTO {domain} (subdomain, sitename, scheme, valid) VALUES ('%s', '%s', '%s', %d)", $root, $site, $scheme, 1);
db_query("UPDATE {domain} SET domain_id = domain_id - 1");
}
variable_set('domain_root', $root);
variable_set('domain_scheme', $scheme);
variable_set('domain_sitename', $site);
module_invoke_all('domainupdate', 'update', domain_default(TRUE));
}
function domain_domains($reset = FALSE) {
static $domains;
if (empty($domains) || $reset) {
$domains = array();
$result = db_query("SELECT domain_id, subdomain, sitename, scheme, valid FROM {domain}");
while ($domain = db_fetch_array($result)) {
$domains[$domain['domain_id']] = domain_api($domain, $reset);
$domains[0] = domain_default();
}
}
$sort = variable_get('domain_sort', 'id');
uasort($domains, '_domain_' . $sort . '_sort');
return $domains;
}
function _domain_id_sort($a, $b) {
return $a['domain_id'] < $b['domain_id'] ? -1 : 1;
}
function _domain_name_sort($a, $b) {
return strcmp($a['sitename'], $b['sitename']);
}
function _domain_url_sort($a, $b) {
return strcmp($a['subdomain'], $b['subdomain']);
}
function _domain_rid_sort($a, $b) {
return $a['domain_id'] > $b['domain_id'] ? -1 : 1;
}
function _domain_rname_sort($a, $b) {
return strcmp($b['sitename'], $a['sitename']);
}
function _domain_rurl_sort($a, $b) {
return strcmp($a['subdomain'], $b['subdomain']);
}
function domain_select_format() {
$domains = domain_domains();
$format = 0;
if (count($domains) > variable_get('domain_list_size', DOMAIN_LIST_SIZE)) {
$format = 1;
}
return variable_get('domain_select_format', $format);
}
function domain_validate($subdomain) {
$error_list = array();
$error = domain_valid_domain($subdomain);
if (!empty($error)) {
$error_list[] = $error;
}
if (!domain_unique_domain($subdomain)) {
$error_list[] = t('The domain value must be unique.');
}
return $error_list;
}
function domain_valid_domain($subdomain) {
$error_list = array();
$localhost_check = explode(':', $subdomain);
if (substr_count($subdomain, '.') == 0 && $localhost_check[0] != 'localhost') {
$error_list[] = t('At least one dot (.) is required, except when using <em>localhost</em>.');
}
if (substr_count($subdomain, ':') > 1) {
$error_list[] = t('Only one colon (:) is allowed.');
}
else {
if (substr_count($subdomain, ':') == 1) {
$parts = explode(':', $subdomain);
$port = (int) $parts[1];
if (strcmp($port, $parts[1])) {
$error_list[] = t('The port protocol must be an integer.');
}
}
}
if (substr($subdomain, 0, 1) == '.') {
$error_list[] = t('The domain must not begin with a dot (.)');
}
if (substr($subdomain, -1) == '.') {
$error_list[] = t('The domain must not end with a dot (.)');
}
if (!variable_get('domain_allow_non_ascii', FALSE)) {
$pattern = '/^[a-z0-9\\.\\-:]*$/i';
if (!preg_match($pattern, $subdomain)) {
$error_list[] = t('Only alphanumeric characters, dashes, and a colon are allowed.');
}
}
if ($subdomain != drupal_strtolower($subdomain)) {
$error_list[] = t('Only lower-case characters are allowed.');
}
if (variable_get('domain_www', 1) && substr($subdomain, 0, strpos($subdomain, '.')) == 'www') {
$error_list[] = t('WWW prefix handling: Domains must be registered without the www. prefix.');
}
drupal_alter('domain_validate', $error_list, $subdomain);
if (!empty($error_list)) {
return t('The domain string is invalid for %subdomain:', array(
'%subdomain' => $subdomain,
)) . theme('item_list', $error_list);
}
}
function domain_unique_domain($subdomain) {
$count = db_result(db_query("SELECT 1 FROM {domain} WHERE subdomain = '%s'", $subdomain));
return !(bool) $count;
}
function domain_get_user_domains($account, $add_roles = TRUE, $reset = FALSE) {
static $domains = array();
if (empty($account)) {
return array();
}
$uid = (int) $account->uid;
if (!isset($domains[$uid]) || $reset) {
$domains[$uid] = array();
$result = db_query("SELECT domain_id FROM {domain_editor} WHERE uid = %d", $uid);
while ($data = db_fetch_object($result)) {
if ($data->domain_id == 0) {
$domains[$uid]['-1'] = -1;
}
else {
$domains[$uid][$data->domain_id] = $data->domain_id;
}
}
if ($add_roles) {
if (empty($account->roles)) {
$account->roles = array(
0 => 'new user',
);
}
$defaults = variable_get('domain_roles', array());
foreach ($account->roles as $rid => $role) {
$filter = array();
if (isset($defaults[$rid])) {
$filter = array_filter($defaults[$rid]);
}
if (!empty($filter)) {
foreach ($filter as $domain_id => $status) {
if ($status) {
$domains[$uid][$domain_id] = $domain_id;
}
}
}
}
}
}
return $domains[$uid];
}
function domain_api($domain, $reset = FALSE) {
static $_modules;
if (!isset($_modules) || $reset) {
$_modules = module_implements('domainload');
}
if (!empty($_modules)) {
foreach ($_modules as $module) {
$function = $module . '_domainload';
$function($domain);
}
}
return $domain;
}
function domain_set_domain($domain_id, $bootstrap = FALSE) {
global $_domain;
$_domain = domain_load($domain_id);
if ($bootstrap) {
_domain_bootstrap_invoke_all('full', $_domain);
}
}
function domain_reset_domain($bootstrap = FALSE) {
$domain = domain_initial_domain();
if (!empty($domain)) {
domain_set_domain($domain['domain_id'], $bootstrap);
}
}
function domain_get_domain() {
if (isset($GLOBALS['_domain'])) {
return $GLOBALS['_domain'];
}
}
function domain_check_primary($msg = 'default') {
global $_domain;
$default = domain_default();
if ($_domain['domain_id'] != $default['domain_id']) {
if ($msg == 'default') {
drupal_set_message(t('You have been redirected: This page must be accessed from the primary domain.'));
}
else {
if (!empty($msg)) {
drupal_set_message($msg);
}
}
domain_goto($default);
}
}
function domain_domainload(&$domain) {
$domain['path'] = domain_get_path($domain);
$domain['site_grant'] = DOMAIN_SITE_GRANT;
}
function domain_get_path($domain) {
global $base_url;
if (empty($base_url)) {
return domain_check_scheme($domain['scheme']) . '://' . $domain['subdomain'];
}
$url = array();
if ($_url = @parse_url($base_url)) {
$url = $_url;
}
if (!isset($url['path'])) {
$url['path'] = '/';
}
if (substr($url['path'], -1) != '/') {
$url['path'] .= '/';
}
$path = domain_check_scheme($domain['scheme']) . '://' . $domain['subdomain'] . $url['path'];
return $path;
}
function domain_get_uri($domain) {
$request_uri = request_uri();
$modules = _domain_path_modules();
if (!empty($modules) && !drupal_is_front_page()) {
$request_uri = base_path() . domain_path($domain['domain_id'], $_GET['q']);
}
$path = domain_check_scheme($domain['scheme']) . '://' . $domain['subdomain'] . $request_uri;
return $path;
}
function domain_check_scheme($scheme) {
if ($scheme != 'https') {
$scheme = 'http';
}
return $scheme;
}
function domain_goto($domain) {
global $_domain;
if ($domain != -1 && $_domain['domain_id'] != $domain['domain_id']) {
$path = domain_get_uri($domain);
drupal_goto($path);
}
}
function domain_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'prepare':
case 'load':
if (!isset($node->nid)) {
return;
}
$domains = domain_get_node_domains($node->nid);
$node->domains = $domains['domain_id'];
$node->domain_site = $domains['domain_site'];
$node->subdomains = array();
if ($node->domain_site) {
$node->subdomains[] = t('All affiliates');
}
foreach ($node->domains as $gid) {
if ($gid > 0) {
$domain = domain_lookup($gid);
$node->subdomains[] = $domain['sitename'];
}
else {
$node->subdomains[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
}
}
break;
case 'view':
if ($a3 !== FALSE || $a4 !== FALSE) {
$output = '';
$debug = variable_get('domain_debug', 0);
if ($debug && user_access('set domain access')) {
if (!empty($node->subdomains)) {
$items = array();
foreach ($node->subdomains as $name) {
$items[] = check_plain($name);
}
$output = theme('item_list', $items, t('Assigned domains'));
$node->content['subdomains'] = array(
'#value' => $output,
'#weight' => 20,
);
}
if (!empty($node->editors)) {
$items = array();
foreach ($node->editors as $name) {
$items[] = check_plain($name);
}
$output = theme('item_list', $items, t('Editors'));
$node->content['editors'] = array(
'#value' => $output,
'#weight' => 21,
);
}
if (empty($output)) {
$node->content['domain'] = array(
'#value' => t('This node is not assigned to a domain.'),
'#weight' => 22,
);
}
}
}
break;
case 'delete':
db_query("DELETE FROM {domain_access} WHERE nid = %d", $node->nid);
break;
case 'insert':
case 'update':
$domain = domain_initial_domain();
$_SESSION['domain_save_id'] = $domain['domain_id'];
break;
case 'presave':
if (!empty($node->devel_generate)) {
$checked = array_filter($node->devel_generate['domains']);
if (empty($checked)) {
return;
}
shuffle($checked);
$domains = array_combine(array_values($checked), array_values($checked));
if (!empty($domains)) {
if (count($domains) > 1) {
$howmany = rand(0, count($domains) - 1);
for ($i = 0; $i < $howmany; $i++) {
array_pop($domains);
}
}
$node->domains = $domains;
$node->domain_source = current($domains);
$node->domain_site = $node->devel_generate['domain_site'] == 'all' ? 1 : ($node->devel_generate['domain_site'] == 'random' ? rand(0, 1) == 1 : 0);
$node->domains = array();
foreach ($domains as $id) {
$node->domains[$id] = $id;
}
}
}
break;
}
}
function domain_get_node_match($nid) {
static $domain = array();
if (isset($domain[$nid])) {
return $domain[$nid];
}
$id = db_result(db_query_range("SELECT gid FROM {domain_access} WHERE nid = %d AND realm = '%s' ORDER BY gid", $nid, 'domain_id', 0, 1));
$source = NULL;
if ($id !== FALSE) {
$source = domain_lookup($id);
drupal_alter('domain_source', $source, $nid);
$domain[$nid] = $source;
}
return $source;
}
function domain_get_path_match($path) {
global $_domain;
$source = $_domain;
drupal_alter('domain_source_path', $source, $path);
return $source;
}
function domain_get_node_domains($nid, $reset = FALSE, $return = FALSE) {
static $lookup = array();
if (isset($lookup[$nid])) {
if (empty($reset)) {
return $lookup[$nid];
}
else {
if (empty($return)) {
unset($lookup[$nid]);
return;
}
}
}
$domains = array(
'domain_id' => array(),
'domain_site' => FALSE,
);
$result = db_query("SELECT gid, realm FROM {domain_access} WHERE nid = %d AND (realm = '%s' OR realm = '%s')", $nid, 'domain_id', 'domain_site');
while ($data = db_fetch_object($result)) {
$data->gid == 0 ? $gid = -1 : ($gid = $data->gid);
if ($data->realm == 'domain_id') {
$domains['domain_id'][$gid] = $gid;
}
else {
if ($data->realm == 'domain_site') {
$domains['domain_site'] = TRUE;
}
}
}
$lookup[$nid] = $domains;
return $lookup[$nid];
}
function domain_node_grants($account, $op) {
global $_domain;
$grants = array();
$rules = variable_get('domain_access_rules', FALSE);
if ($op == 'view') {
if ($_domain['site_grant']) {
$grants['domain_site'][] = 0;
if ($rules) {
$grants['domain_site']['group'] = 'domain';
}
}
$grants['domain_id'][] = $_domain['domain_id'];
if ($rules) {
$grants['domain_id']['group'] = 'domain';
}
if (domain_grant_all()) {
if ($rules) {
$grants = array();
}
else {
$grants = array(
'domain_all' => array(
0,
),
);
}
}
}
else {
$domains = domain_get_user_domains($account);
$perm = 'delete domain nodes';
if ($op == 'update') {
$perm = 'edit domain nodes';
}
if (user_access($perm, $account)) {
if (!empty($domains)) {
foreach ($domains as $id) {
if (abs($id) > 0) {
if ($id > 0) {
$grants['domain_id'][] = $id;
}
else {
$grants['domain_id'][] = 0;
}
if ($rules) {
$grants['domain_id']['check'] = TRUE;
}
}
}
}
}
}
static $_modules;
if (!isset($_modules)) {
$_modules = module_implements('domaingrants');
}
if (!empty($_modules)) {
foreach ($_modules as $module) {
$function = $module . '_domaingrants';
$function($grants, $account, $op);
}
}
return $grants;
}
function domain_node_access_records($node) {
global $_domain;
$grants = array();
if (!isset($node->domain_site)) {
$node->domain_site = variable_get('domain_node_' . $node->type, variable_get('domain_behavior', DOMAIN_INSTALL_RULE));
$node->domains = array(
$_domain['domain_id'] => $_domain['domain_id'],
);
}
if (!empty($node->domains_raw)) {
if (!isset($node->domains)) {
$node->domains = array();
}
foreach ($node->domains_raw as $value) {
if (!in_array($value, $node->domains)) {
$node->domains[$value] = $value;
}
}
}
if (!empty($node->domain_site)) {
$grants[] = array(
'realm' => 'domain_site',
'gid' => 0,
'grant_view' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'priority' => 0,
);
}
if (!empty($node->domains)) {
foreach ($node->domains as $key => $value) {
if (abs($value) > 0) {
$key == -1 ? $key = 0 : ($key = $key);
$grants[] = array(
'realm' => 'domain_id',
'gid' => $key,
'grant_view' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
}
}
}
else {
$grants[] = array(
'realm' => 'domain_id',
'gid' => 0,
'grant_view' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
}
static $_modules;
if (!isset($_modules)) {
$_modules = module_implements('domainrecords');
}
if (!empty($_modules)) {
foreach ($_modules as $module) {
$function = $module . '_domainrecords';
$function($grants, $node);
}
}
_domain_store_grants($node->nid, $grants);
return $grants;
}
function _domain_store_grants($nid, $grants = array()) {
if ($nid > 0 && !empty($grants)) {
db_query("DELETE FROM {domain_access} WHERE nid = %d", $nid);
foreach ($grants as $grant) {
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid, $grant['gid'], $grant['realm']);
}
}
domain_get_node_domains($nid, TRUE);
domain_set_default_grant();
}
function domain_set_default_grant() {
static $check = NULL;
if (is_null($check)) {
$check = db_result(db_query("SELECT 1 FROM {node_access} WHERE realm = '%s' AND gid = %d", 'domain_all', 0));
if ($check == 0) {
db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", 0, 0, 'domain_all', 1, 0, 0);
}
}
}
function domain_enable() {
domain_set_default_grant();
$count = (bool) db_result(db_query("SELECT 1 FROM {domain_access}"));
if (empty($count)) {
$rule = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
$site = DOMAIN_SITE_GRANT;
$nids = db_query("SELECT nid FROM {node}");
while ($nid = db_result($nids)) {
if (!empty($site)) {
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid, 0, 'domain_site');
}
if (!empty($rule)) {
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid, 0, 'domain_id');
}
}
}
if (!DOMAIN_ASSIGN_USERS) {
return;
}
$result = db_query("SELECT uid FROM {users} WHERE uid > 0");
while ($data = db_fetch_object($result)) {
$check = (bool) db_result(db_query("SELECT 1 FROM {domain_editor} WHERE uid = %d", $data->uid));
if (empty($check)) {
db_query("INSERT INTO {domain_editor} VALUES (%d, %d)", $data->uid, 0);
}
}
}
function domain_form_alter(&$form, &$form_state, $form_id) {
$forms = module_invoke_all('domainignore');
if (in_array($form_id, $forms)) {
return;
}
domain_warning_check($form_id);
$seo = variable_get('domain_seo', 0);
if ($seo && isset($form['#action'])) {
$domain = domain_initial_domain();
$action = parse_url($form['#action']);
if (isset($action['query'])) {
$action['path'] .= '?';
}
else {
$action['query'] = '';
}
if (empty($action['host'])) {
$form['#action'] = $domain['scheme'] . '://' . $domain['subdomain'] . $action['path'] . $action['query'];
}
}
if ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_form)) {
global $_domain, $user;
$default = array(
$_domain['domain_id'] == 0 ? -1 : $_domain['domain_id'],
);
$behavior = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
if (isset($form['#node']->nid) && !empty($form['#node']->domains)) {
$raw = $form['#node']->domains;
}
else {
$raw = $default;
}
$options = array();
$format = domain_select_format();
foreach (domain_domains() as $data) {
$data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);
if ($data['valid'] || user_access('access inactive domains')) {
$options[$key] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
$weight = module_exists('content') ? content_extra_field_weight($form['type']['#value'], 'domain') : 1;
if (user_access('set domain access')) {
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => $weight,
);
$form['domain']['domain_site'] = array(
'#type' => 'checkbox',
'#prefix' => t('<p><b>Publishing options:</b>'),
'#suffix' => '</p>',
'#title' => t('Send to all affiliates'),
'#required' => FALSE,
'#description' => t('Select if this content can be shown to all affiliates. This setting will override the options below, but you must still select a domain that "owns" this content.'),
'#default_value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : variable_get('domain_node_' . $form['#node']->type, $behavior),
);
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Publish to'),
'#options' => $options,
'#required' => TRUE,
'#description' => t('Select which affiliates can access this content.'),
'#default_value' => isset($form['#node']->domains) ? $form['#node']->domains : $default,
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
else {
$action = domain_form_perm();
if (!empty($action)) {
$user->domain_user = domain_get_user_domains($user);
$user_domains = array();
$default_options = array();
$user_options = array();
$raw_options = array();
if (!empty($user->domain_user)) {
foreach ($user->domain_user as $key => $value) {
if (abs($value) > 0) {
$user_domains[] = $value;
}
}
if (in_array($_domain['domain_id'], $user_domains)) {
$first_domain = $_domain['domain_id'];
}
else {
$first_domain = current($user_domains);
}
foreach ($options as $key => $value) {
if (in_array($key, $user_domains)) {
$user_options[$key] = $value;
}
}
}
foreach ($raw as $key => $value) {
if (in_array($value, $user_domains)) {
$default_options[] = $value;
}
else {
$raw_options[] = $value;
}
}
switch ($action) {
case 1:
$root = domain_default();
if ($root['domain_id'] != $_domain['domain_id']) {
domain_goto($root);
}
break;
case 2:
$domain = domain_lookup($first_domain);
if ($domain == -1 || empty($domain['valid']) && !user_access('access inactive domains')) {
domain_goto(domain_default());
}
else {
if ($domain['domain_id'] != $_domain['domain_id']) {
domain_goto($domain);
}
}
break;
case 3:
if (empty($user_options)) {
$form = array();
return drupal_access_denied();
}
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Affiliate publishing options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => $weight,
);
if (!empty($form['#node']->nid)) {
$raw = $raw_options;
}
else {
$raw = array();
}
empty($raw) ? $required = TRUE : ($required = FALSE);
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Publish to'),
'#options' => $user_options,
'#required' => $required,
'#description' => t('Select which affiliates can access this content.'),
'#default_value' => isset($form['#node']->domains) ? $form['#node']->domains : $default_options,
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($user_options) > 10 ? 10 : count($user_options);
}
$list = array();
if (!empty($form['#node']->domain_site)) {
$list[]['data'] = t('All affiliates');
}
if (!empty($raw)) {
foreach ($raw as $did) {
$did == -1 ? $id = 0 : ($id = $did);
$raw_domains = domain_lookup($id);
$list[]['data'] = check_plain($raw_domains['sitename']);
}
}
if (!empty($list)) {
$form['domain']['domains_notes'] = array(
'#value' => '<label><b>' . t('Publishing status:') . '</b></label>' . theme('item_list', $list) . '<div class="description">' . t('This content has also been published to these affiliates.') . '</div>',
);
}
break;
}
}
$form['domain_site'] = array(
'#type' => 'value',
'#value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : variable_get('domain_node_' . $form['#node']->type, $behavior),
);
$form['domains_raw'] = array(
'#type' => 'value',
'#value' => $raw,
);
}
if (user_access('edit domain nodes')) {
$access = variable_get('domain_form_elements', array(
'options',
'delete',
'comment_settings',
'path',
));
foreach ($access as $item) {
$form[$item]['#access'] = TRUE;
}
}
}
}
function domain_form_system_site_information_settings_alter(&$form, &$form_state) {
$form['#submit'][] = 'domain_form_sitename_submit';
}
function domain_form_sitename_submit($form, &$form_state) {
db_query("UPDATE {domain} SET sitename = '%s' WHERE domain_id = 0", $form_state['values']['site_name']);
variable_set('domain_sitename', $form_state['values']['site_name']);
drupal_set_message(t('Primary domain settings updated.'));
}
function domain_form_perm() {
$perms = array(
'publish from default domain' => 1,
'publish from assigned domain' => 2,
'publish to any assigned domain' => 3,
);
$action = NULL;
foreach ($perms as $perm => $value) {
if (user_access($perm)) {
$action = $value;
}
}
return $action;
}
function domain_form_devel_generate_content_form_alter(&$form, &$form_state) {
$form['submit']['#weight'] = 10;
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain Access Options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 9,
);
$form['domain']['domain_site'] = array(
'#type' => 'select',
'#title' => t('Send to all affiliates'),
'#options' => array(
'none' => t('Never'),
'all' => t('Always'),
'random' => t('Randomly decide'),
),
'#description' => t('If you choose "always" or "randomly" you must select at least one domain below.'),
);
$format = domain_select_format();
foreach (domain_domains() as $data) {
$data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);
if ($data['valid'] || user_access('access inactive domains')) {
$options[$key] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Publish to'),
'#options' => $options,
'#description' => t('Generated content will be accessible on any or all of the domains checked above.'),
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
function domain_grant_all($reset = FALSE) {
static $grant;
$options = array();
if (!isset($grant) || $reset) {
$grant = FALSE;
if (variable_get('domain_search', 0) && arg(0) == 'search') {
$options['search'] = TRUE;
$grant = TRUE;
}
if (!$grant) {
$ref = explode('/', request_uri());
$script = array_pop($ref);
if (variable_get('domain_cron_rule', 1) && $script == 'cron.php') {
$options['script'] = $script;
$grant = TRUE;
}
else {
if (variable_get('domain_xmlrpc_rule', 0) && $script == 'xmlrpc.php') {
$options['script'] = $script;
$grant = TRUE;
}
}
}
if (!$grant) {
$pages = variable_get('domain_grant_all', "user/*/track");
$options['pages'] = $pages;
$regexp = '/^(' . preg_replace(array(
'/(\\r\\n?|\\n)/',
'/\\\\\\*/',
'/(^|\\|)\\\\<front\\\\>($|\\|)/',
), array(
'|',
'.*',
'\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
), preg_quote($pages, '/')) . ')$/';
$page_match = preg_match($regexp, $_GET['q']);
if (!$page_match && function_exists('drupal_get_path_alias')) {
$path = drupal_get_path_alias($_GET['q']);
if ($path != $_GET['q']) {
$page_match = preg_match($regexp, $path);
}
}
if ($page_match) {
$options['page_match'] = TRUE;
$grant = TRUE;
}
}
}
drupal_alter('domain_grant_all', $grant, $options);
return $grant;
}
function domain_bootstrap_register() {
$modules = array();
$lookup = module_implements('domain_bootstrap_lookup');
$full = module_implements('domain_bootstrap_full');
$modules = array_merge($lookup, $full);
variable_set('domain_bootstrap_modules', $modules);
}
function domain_bootstrap_unregister($name) {
$modules = variable_get('domain_bootstrap_modules', array());
if (is_array($modules)) {
foreach ($modules as $k => $v) {
if ($v == $name) {
unset($modules[$k]);
}
}
}
variable_set('domain_bootstrap_modules', $modules);
}
function domain_resolve_host($name = '') {
if (empty($name)) {
$name = domain_request_name();
}
return domain_lookup_simple($name);
}
function domain_request_name() {
if (empty($_SERVER['HTTP_HOST'])) {
$domain = domain_default(FALSE, FALSE);
return $domain['subdomain'];
}
return strtolower(rtrim($_SERVER['HTTP_HOST']));
}
function domain_invalid_domain_requested() {
global $_domain, $user;
if (user_access('access inactive domains')) {
return;
}
$item = menu_get_item();
$nid = NULL;
if ($item['path'] == 'node/%') {
$node = node_load(arg(1));
}
if (empty($node->nid)) {
$path = $item['href'];
if (drupal_is_front_page($item['href'])) {
$path = '';
}
$default = domain_default();
watchdog('domain', 'Invalid domain requested by %user on %domain; redirected to %default.', array(
'%user' => isset($user->name) ? $user->name : variable_get('anonymous', t('Anonymous')),
'%domain' => $_domain['sitename'],
'%default' => $default['sitename'],
), WATCHDOG_WARNING);
drupal_goto($default['path'] . drupal_get_path_alias($path));
}
$path = "node/{$node->nid}";
$domain = domain_get_node_match($node->nid);
if ($domain['valid']) {
$redirect = $domain;
}
else {
if (!empty($node->domains)) {
foreach ($node->domains as $domain_id) {
if ($domain_id == -1) {
$domain_id = 0;
}
$domain = domain_lookup($domain_id);
if ($domain['valid']) {
$redirect = $domain;
break;
}
}
}
}
$extra = ' ' . t('node page.');
if (empty($redirect)) {
$redirect = domain_default();
$path = '';
$extra = '.';
}
watchdog('domain', 'Invalid domain requested by %user on %domain, redirected to %redirect', array(
'%user' => isset($user->name) ? $user->name : variable_get('anonymous', t('Anonymous')),
'%domain' => $_domain['sitename'],
'%redirect' => $redirect['sitename'] . $extra,
), WATCHDOG_WARNING);
drupal_goto($redirect['path'] . drupal_get_path_alias($path));
}
function domain_node_save_redirect() {
global $_domain;
if (!isset($_SESSION['domain_save_id'])) {
return;
}
$domain_id = $_SESSION['domain_save_id'];
unset($_SESSION['domain_save_id']);
$source = domain_lookup($domain_id);
if ($source['domain_id'] != -1 && $source['domain_id'] != $_domain['domain_id'] && ($source['valid'] || user_access('access inactive domains'))) {
domain_goto($source);
}
}
function domain_lookup_simple($name, $reset = FALSE) {
static $cache = array();
if (empty($name)) {
return array();
}
if ($reset || !isset($cache[$name])) {
$domain = db_fetch_array(db_query("SELECT domain_id, subdomain, sitename FROM {domain} WHERE subdomain = '%s'", $name));
if (!is_array($domain)) {
$domain = array();
}
if (!isset($domain['domain_id'])) {
$domain['domain_id'] = 0;
}
$domain['subdomain'] = $name;
$domain_new = _domain_bootstrap_invoke_all('lookup', $domain);
if (is_array($domain_new)) {
if (isset($domain_new['domain_id']) && is_array($domain_new['domain_id'])) {
foreach ($domain_new as $key => $value) {
if (is_array($value)) {
$domain_new[$key] = $value[0];
}
}
$modules = array();
foreach (_domain_bootstrap_modules() as $module) {
if (function_exists($module . '_domain_bootstrap_lookup')) {
$modules[] = $module;
}
}
$lookup = domain_lookup($domain_new['domain_id']);
$domain_new['error'] = t('domain lookup. More than one registered domain was returned. Defaulting to %domain. The likely cause is a conflict between %modules', array(
'%domain' => $lookup['sitename'],
'%modules' => implode(', ', $modules),
));
}
$domain = array_merge($domain, $domain_new);
}
$cache[$name] = $domain;
}
return $cache[$name];
}
function domain_domaininstall() {
domain_bootstrap_register();
}
function domain_domainupdate($op, $domain, $form_state = array()) {
switch ($op) {
case 'delete':
if ($domain != -1) {
db_query("DELETE FROM {node_access} WHERE realm = '%s' AND gid = %d", 'domain_id', $domain['domain_id']);
db_query("DELETE FROM {domain_access} WHERE realm = '%s' AND gid = %d", 'domain_id', $domain['domain_id']);
db_query("DELETE FROM {domain_editor} WHERE domain_id = %d", $domain['domain_id']);
}
break;
}
menu_rebuild();
}
function domain_domainbatch() {
$batch = array();
$batch['subdomain'] = array(
'#form' => array(
'#title' => t('Domains'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 80,
'#description' => t('Enter the host value of the domain. No http:// or slashes.'),
'#required' => TRUE,
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain values.'),
'#variable' => 'domain_root',
'#validate' => 'domain_batch_validate',
'#data_type' => 'string',
'#update_all' => FALSE,
'#weight' => -10,
);
$batch['sitename'] = array(
'#form' => array(
'#title' => t('Site names'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 80,
'#description' => t('The site name to display for this domain.'),
'#required' => TRUE,
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain site names.'),
'#variable' => 'domain_sitename',
'#validate' => 'domain_batch_validate',
'#data_type' => 'string',
'#update_all' => FALSE,
'#weight' => -10,
);
$batch['scheme'] = array(
'#form' => array(
'#title' => t('URL schemes'),
'#type' => 'radios',
'#options' => array(
'http' => 'http://',
'https' => 'https://',
),
'#description' => t('The URL scheme for accessing this domain.'),
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain URL schemes.'),
'#system_default' => variable_get('domain_scheme', 'http://'),
'#variable' => 'domain_scheme',
'#data_type' => 'string',
'#update_all' => TRUE,
'#weight' => -10,
);
$batch['valid'] = array(
'#form' => array(
'#title' => t('Valid domains'),
'#type' => 'radios',
'#options' => array(
1 => t('Active'),
0 => t('Inactive'),
),
'#description' => t('Allows users to access this domain.'),
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain status flags.'),
'#system_default' => 1,
'#data_type' => 'integer',
'#update_all' => TRUE,
'#weight' => -10,
);
foreach ($batch as $key => $value) {
$batch[$key]['#module'] = t('Domain Access');
}
return $batch;
}
function domain_batch_validate($values) {
$case = $values['variable'];
$batch = $values['domain_batch'];
switch ($case) {
case 'domain_root':
$errors = array();
foreach ($batch as $key => $value) {
$subdomain = strtolower(urlencode($value));
$check = db_result(db_query("SELECT 1 FROM {domain} WHERE subdomain = '%s' AND domain_id <> %d", $value, $key));
if ($check || $key > 0 && $value == variable_get('domain_root', '')) {
form_set_error('domain_batch][' . $key, t('Each domain value must be unique.'));
}
else {
$error = domain_valid_domain($value);
if (!empty($error)) {
form_set_error('domain_batch][' . $key, $error);
}
}
}
break;
case 'domain_sitename':
foreach ($batch as $key => $value) {
$check = db_result(db_query("SELECT 1 FROM {domain} WHERE sitename = '%s' AND domain_id <> %d", $value, $key));
if ($check || $key > 0 && $value == variable_get('domain_sitename', 'Drupal')) {
form_set_error('domain_batch][' . $key, t('Each site name value must be unique.'));
}
}
break;
}
}
function domain_warning_check($form_id) {
static $_warning;
if (empty($_POST) && empty($_warning)) {
global $_domain;
$forms = array();
$forms = module_invoke_all('domainwarnings');
drupal_alter('domain_warnings', $forms);
foreach ($forms as $key => $value) {
if (is_numeric($key)) {
$forms[$value] = '';
unset($forms[$key]);
}
}
if ($form_id == 'domain_batch_form' || arg(2) != 'domain' && in_array($form_id, array_keys($forms))) {
$default = domain_default();
if ($_domain['domain_id'] == $default['domain_id']) {
return;
}
$link_text = '';
$link = isset($forms[$form_id]) ? $forms[$form_id] : NULL;
if (!empty($link)) {
$elements = array();
foreach ($_domain as $key => $value) {
if (!is_array($value)) {
$elements[$key] = $value;
}
}
$replace = explode('|', '%' . implode('|%', array_keys($elements)));
$values = explode('|', implode('|', $elements));
$link = str_replace($replace, $values, $link);
$link_text = t('You may submit changes to the current domain at <a href="!url">%link</a>.', array(
'!url' => url($link),
'%link' => $link,
));
}
$_path = domain_get_uri($default);
drupal_set_message(t('This form submits changes to your primary domain and <a href="!url">may need to be entered from !domain</a>. !link', array(
'#this' => $_domain['subdomain'],
'!url' => $_path,
'!domain' => $default['subdomain'],
'!link' => $link_text,
)), 'warning', FALSE);
}
$_warning = TRUE;
}
}
function domain_path($domain_id, $path, $path_language = '') {
$modules = _domain_path_modules();
if (!empty($modules)) {
foreach ($modules as $module) {
$function = $module . '_domainpath';
$function($domain_id, $path, $path_language);
}
}
return $path;
}
function _domain_path_modules() {
static $modules;
if (!isset($modules)) {
$modules = module_implements('domainpath');
}
return $modules;
}
function domain_domainignore() {
return array(
'update_script_selection_form',
);
}
function domain_node_access_explain($row) {
global $_domain;
$active = $_domain['subdomain'];
$domain = domain_lookup($row->gid);
$return = t('Domain Access') . ' -- ';
switch ($row->realm) {
case 'domain_all':
if (domain_grant_all() == TRUE) {
$return .= t('True: Allows content from all domains to be shown.');
}
else {
$return .= t('False: Only allows content from the active domain (%domain) or from all affiliates.', array(
'%domain' => $active,
));
}
break;
case 'domain_site':
$return .= t('Viewable on all affiliate sites.');
break;
case 'domain_id':
$return .= t('Viewable on %domain<br />%domain privileged editors may edit and delete', array(
'%domain' => $domain['subdomain'],
));
break;
default:
$return = NULL;
break;
}
return $return;
}
function domain_node_access_acknowledge($grant) {
if ($grant['realm'] == 'domain_all') {
return TRUE;
}
}
function domain_content_extra_fields($type_name = NULL) {
if (!empty($type_name)) {
return array(
'domain' => array(
'label' => t('Domain access'),
'description' => t('Domain-specific settings for posts.'),
'weight' => 1,
),
);
}
}
function domain_token_list($type = 'all') {
$tokens = array();
if ($type == 'global' || $type == 'all') {
$tokens['domain']['domain-id'] = t('The current domain ID.');
$tokens['domain']['domain-name'] = t('The current domain name, lowercased and with only alphanumeric characters.');
$tokens['domain']['domain-name-raw'] = t('The current domain name. WARNING - raw user input. NOT path safe.');
$tokens['domain']['domain-url'] = t('The current domain\'s URL, lowercased and with only alphanumeric characters.');
$tokens['domain']['domain-url-raw'] = t('The current domain\'s URL. WARNING - raw user input. NOT path safe.');
$tokens['domain']['domain-subdomain'] = t('The current subdomain, lowercased and with only alphanumeric characters. Only works with *.example.com formats');
$tokens['domain']['domain-subdomain-raw'] = t('The current subdomain. Only works with *.example.com formats. WARNING - raw user input. NOT path safe.');
$tokens['domain']['domain-default-id'] = t('The default domain ID.');
$tokens['domain']['domain-default-name'] = t('The default domain name, lowercased and with only alphanumeric characters.');
$tokens['domain']['domain-default-name-raw'] = t('The default domain name. WARNING - raw user input. NOT path safe.');
$tokens['domain']['domain-default-url'] = t('The default domain\'s URL, lowercased and with only alphanumeric characters.');
$tokens['domain']['domain-default-url-raw'] = t('The default domain\'s URL. WARNING - raw user input. NOT path safe.');
}
return $tokens;
}
function domain_token_values($type, $object = NULL, $options = array()) {
global $_domain;
if ($type != 'global') {
return;
}
$default_domain = domain_default(FALSE);
$subdomain_elements = explode('.', $_domain['subdomain']);
if (count($subdomain_elements) > 2) {
$subdomain = $subdomain_elements[0];
}
else {
$subdomain = 'www';
}
$tokens['domain-id'] = $_domain['domain_id'];
$tokens['domain-name'] = domain_url_encode($_domain['sitename']);
$tokens['domain-name-raw'] = check_plain($_domain['sitename']);
$tokens['domain-url'] = domain_url_encode($_domain['subdomain']);
$tokens['domain-url-raw'] = check_plain($_domain['subdomain']);
$tokens['domain-subdomain'] = domain_url_encode($subdomain);
$tokens['domain-subdomain-raw'] = check_plain($subdomain);
$tokens['domain-default-id'] = $default_domain['domain_id'];
$tokens['domain-default-name'] = domain_url_encode($default_domain['sitename']);
$tokens['domain-default-name-raw'] = check_plain($default_domain['sitename']);
$tokens['domain-default-url'] = domain_url_encode($default_domain['subdomain']);
$tokens['domain-default-url-raw'] = check_plain($default_domain['subdomain']);
return $tokens;
}
function domain_url_encode($string) {
$string = drupal_strtolower($string);
$string = str_replace('/', '', $string);
$pattern = '/[^a-zA-Z0-9\\/]+/ ';
$string = preg_replace($pattern, '', $string);
$string = preg_replace('/\\s+/', '', $string);
return $string;
}
function domain_simpletest() {
$module_name = 'domain';
$dir = drupal_get_path('module', $module_name) . '/tests';
$tests = file_scan_directory($dir, '\\.test$');
return array_keys($tests);
}
function domain_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
global $_domain;
$admin_force = variable_get('domain_force_admin', FALSE);
if (!$admin_force || $primary_field != 'nid' || !user_access('administer nodes') || domain_grant_all()) {
return;
}
$domain_id = (int) $_domain['domain_id'];
$return = array(
'join' => "INNER JOIN {domain_access} da_admin ON {$primary_table}.nid = da_admin.nid",
'where' => "(da_admin.gid = 0 AND da_admin.realm = 'domain_site') OR (da_admin.gid = {$domain_id} AND da_admin.realm = 'domain_id')",
);
return $return;
}