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($object);
}
function domain_menu() {
$items = array();
$admin = user_access('administer domains');
$items['admin/structure/domain'] = array(
'title' => 'Domains',
'access arguments' => array(
'administer domains',
),
'page callback' => 'domain_view',
'file' => 'domain.admin.inc',
'description' => 'Manage and configure domains.',
);
$items['admin/structure/domain/view'] = array(
'title' => 'Domain list',
'access arguments' => array(
'administer domains',
),
'page callback' => 'domain_view',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'domain.admin.inc',
'description' => 'View list of registered domains.',
'weight' => -10,
);
$items['admin/structure/domain/settings'] = array(
'title' => 'Settings',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_configure',
'file' => 'domain.admin.inc',
'description' => 'Configure Domain Access settings.',
'weight' => -8,
);
$items['admin/structure/domain/create'] = array(
'title' => 'Create domain',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_ACTION,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_form',
),
'file' => 'domain.admin.inc',
'description' => 'Create new domain record.',
'weight' => -7,
);
$items['admin/structure/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',
'description' => 'Configure content creation settings for domains.',
'weight' => -6,
);
$batch = module_invoke_all('domainbatch');
if (!empty($batch)) {
$items['admin/structure/domain/batch'] = array(
'title' => 'Batch updating',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_batch',
'file' => 'domain.admin.inc',
'description' => 'Batch update domain settings.',
'weight' => -5,
);
foreach ($batch as $key => $value) {
$items['admin/structure/domain/batch/' . $key] = array(
'title' => $value['#form']['#title'],
'access arguments' => isset($value['#permission']) ? array(
$value['#permission'],
) : array(
'administer domains',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'domain_batch',
'page arguments' => array(
$key,
),
'file' => 'domain.admin.inc',
'description' => isset($value['#description']) ? $value['#description'] : '',
'weight' => isset($value['#weight']) ? $value['#weight'] : 0,
);
}
}
$items['admin/structure/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',
'description' => 'Default domain settings for users.',
'weight' => -4,
);
$items['admin/structure/domain/edit/%domain'] = array(
'title' => 'Edit domain record',
'access arguments' => array(
'administer domains',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_form',
4,
),
'description' => 'Edit domain record.',
'file' => 'domain.admin.inc',
);
$items['admin/structure/domain/delete/%domain'] = array(
'title' => 'Delete domain record',
'access arguments' => array(
'administer domains',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_delete_form',
4,
),
'description' => 'Delete domain record.',
'file' => 'domain.admin.inc',
);
return $items;
}
function domain_permission() {
$permissions = array(
'administer domains' => array(
'title' => t('Administer domain records and settings'),
'restrict access' => TRUE,
),
'access inactive domains' => array(
'title' => t('Access inactive domains'),
'restrict access' => TRUE,
),
'assign domain editors' => array(
'title' => t('Assign editors to domains'),
),
'edit domain content' => array(
'title' => t('Edit any content on assigned domains'),
),
'delete domain content' => array(
'title' => t('Delete any content on assigned domains'),
),
'set domain access' => array(
'title' => t('Set domain access status for all content'),
),
'publish to any assigned domain' => array(
'title' => t('Publish content to any assigned domain'),
),
'publish from assigned domain' => array(
'title' => t('Publish content only from assigned domain'),
),
'publish from default domain' => array(
'title' => t('Publish content only from the default domain'),
),
);
return $permissions;
}
function domain_theme($existing, $type, $theme, $path) {
$themes = array(
'domain_batch_form' => array(
'render element' => 'form',
'file' => 'domain.admin.inc',
),
'domain_batch_title' => array(
'variables' => array(
'batch' => array(),
),
'file' => 'domain.admin.inc',
),
'domain_roles_form' => array(
'render element' => 'form',
'file' => 'domain.admin.inc',
),
);
return $themes;
}
function domain_block_info() {
$blocks = array();
$blocks['information'] = array(
'info' => t('Domain access information'),
);
$blocks['server'] = array(
'info' => t('Domain access server information'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['switcher'] = array(
'info' => t('Domain switcher'),
);
return $blocks;
}
function domain_block_view($delta = '') {
if (empty($delta)) {
return;
}
module_load_include('inc', 'domain', 'domain.blocks');
$function = 'domain_block_view_' . $delta;
if (function_exists($function)) {
return $function();
}
}
function domain_user_load($users) {
foreach ($users as $uid => $account) {
$users[$uid]->domain_user = domain_get_user_domains($account);
}
}
function domain_user_insert(&$edit, $account, $category) {
domain_user_save($edit, $account, $category);
}
function domain_user_update(&$edit, $account, $category) {
domain_user_save($edit, $account, $category);
}
function domain_user_save(&$edit, $account, $category) {
if (!isset($edit['domain_user'])) {
return;
}
db_delete('domain_editor')
->condition('uid', $account->uid)
->execute();
$values = array();
foreach ($edit['domain_user'] as $domain_id => $status) {
if ($status != 0) {
if ($domain_id == -1) {
$domain_id = 0;
}
$values[] = array(
'uid' => $account->uid,
'domain_id' => $domain_id,
);
}
}
if (!empty($values)) {
$query = db_insert('domain_editor')
->fields(array(
'uid',
'domain_id',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
$edit['domain_user'] = NULL;
}
function domain_user_delete($account) {
db_delete('domain_editor')
->condition('uid', $account->uid)
->execute();
}
function domain_user_view($account, $view_mode) {
if ($view_mode != 'full') {
return;
}
if (!user_access('assign domain editors')) {
return;
}
$output = '';
$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 {
$items = array();
foreach (array_filter($account->domain_user) as $id) {
$id == -1 ? $key = 0 : ($key = $id);
$domain = domain_lookup($key);
$items[] = check_plain($domain['sitename']);
}
$output = theme('item_list', array(
'items' => $items,
));
}
$account->content['domain']['domain_settings'] = array(
'#type' => 'user_profile_item',
'#title' => t('Assigned domains'),
'#markup' => $output,
);
}
function domain_form_user_profile_form_alter(&$form, &$form_state) {
domain_form_user_form_alter($form, $form_state);
}
function domain_form_user_register_form_alter(&$form, &$form_state) {
domain_form_user_form_alter($form, $form_state);
}
function domain_form_user_form_alter(&$form, &$form_state) {
if (is_null($form['#user_category']) || !in_array($form['#user_category'], array(
'account',
'register',
))) {
return;
}
$_domain = domain_get_domain();
$add_roles = variable_get('domain_add_roles', 0);
if ($form['#user_category'] == 'register') {
$add_roles = TRUE;
}
$account = $form['#user'];
$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;
}
if (user_access('assign domain editors')) {
$domains = domain_domains();
$options = array();
foreach ($domains as $domain) {
if ($domain['domain_id'] == variable_get('default_domain', 0)) {
$domain['domain_id'] = -1;
}
$options[$domain['domain_id']] = check_plain($domain['sitename']);
}
$format = domain_select_format();
$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 content" 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,
);
}
}
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;
}
$form['options']['#weight'] = -2;
$_domain = domain_get_domain();
$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);
}
$ops = array_pop($form['accounts']['#header']);
$form['accounts']['#header']['domain_user'] = array(
'data' => t('Assigned Domains'),
);
foreach (array_keys($form['accounts']['#options']) as $uid) {
$form['accounts']['#options'][$uid]['domain_user'] = theme('item_list', array(
'items' => _domain_user_list($uid),
));
}
$form['accounts']['#header']['operations'] = $ops;
$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 = domain_domains();
$user_domains = array();
foreach ($list as $domain_id) {
if ($domain_id == -1) {
$domain_id = 0;
}
$user_domains[] = check_plain($domains[$domain_id]['sitename']);
}
return $user_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_delete('domain_editor')
->condition('uid', $uid)
->execute();
foreach ($domains as $domain_id) {
if ($domain_id == -1) {
$domain_id = 0;
}
db_insert('domain_editor')
->fields(array(
'uid' => $uid,
'domain_id' => $domain_id,
))
->execute();
}
}
}
function domain_cron() {
$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'], TRUE);
module_invoke_all('domainupdate', $action, $domain, $edit);
$domain = domain_lookup(NULL, $values['subdomain'], TRUE);
return $domain;
}
function domain_delete($domain, $values = array()) {
db_delete('domain')
->condition('domain_id', $domain['domain_id'])
->execute();
module_invoke_all('domainupdate', 'delete', $domain, $values);
node_access_needs_rebuild(TRUE);
}
function domain_lookup($domain_id = NULL, $subdomain = NULL, $reset = FALSE) {
static $domains, $result;
$key = $domain_id . $subdomain;
if (!$reset && isset($domains[$key])) {
return $domains[$key];
}
if (!isset($result) || $reset) {
$result = db_query("SELECT domain_id, subdomain, sitename, scheme, valid FROM {domain}", array(), array(
'fetch' => PDO::FETCH_ASSOC,
))
->fetchAllAssoc('domain_id');
}
if (is_null($domain_id) && is_null($subdomain) || $domain_id < 0) {
$domains[$key] = -1;
}
elseif (!isset($domains[$key]) || $reset) {
if ($subdomain) {
foreach ($result as $array) {
if ($array['subdomain'] == $subdomain) {
$domain = $array;
}
}
}
elseif (isset($result[$domain_id])) {
$domain = $result[$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_default_id() {
return 0;
}
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';
}
$check = (bool) db_query("SELECT COUNT(domain_id) FROM {domain} WHERE domain_id = :domain_id", array(
':domain_id' => 0,
))
->fetchField();
if (empty($check)) {
db_insert('domain')
->fields(array(
'subdomain' => $root,
'sitename' => $site,
'scheme' => $scheme,
'valid' => 1,
))
->execute();
db_update('domain')
->fields(array(
'domain_id' => 0,
))
->condition('domain_id', 1)
->execute();
}
else {
db_update('domain')
->fields(array(
'subdomain' => $root,
'sitename' => $site,
'scheme' => $scheme,
'valid' => 1,
))
->condition('domain_id', 0)
->execute();
}
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 FROM {domain}", array(), array(
'fetch' => PDO::FETCH_ASSOC,
));
foreach ($result as $data) {
$domain = domain_lookup($data['domain_id'], NULL, $reset);
$domains[$domain['domain_id']] = $domain;
}
}
$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.');
}
elseif (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.');
}
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', array(
'items' => $error_list,
));
}
}
function domain_unique_domain($subdomain) {
$count = db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = :subdomain", array(
':subdomain' => $subdomain,
))
->fetchField();
return empty($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 = :uid", array(
':uid' => $uid,
));
foreach ($result as $data) {
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') {
$_domain = domain_get_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.'));
}
elseif (!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) {
global $base_path;
$modules = _domain_path_modules();
if (!empty($modules) && !drupal_is_front_page()) {
$request_uri = request_path();
$options = array();
domain_path($domain['domain_id'], $request_uri, $options, $_GET['q']);
$request_uri = base_path() . $request_uri;
}
else {
$request_uri = request_uri();
}
$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) {
$_domain = domain_get_domain();
if ($domain != -1 && $_domain['domain_id'] != $domain['domain_id']) {
$path = domain_get_uri($domain);
drupal_goto($path);
}
}
function domain_node_load($nodes, $types) {
$domains = domain_get_node_domains($nodes);
foreach ($nodes as $node) {
if (!isset($node->nid)) {
continue;
}
$nodes[$node->nid]->domains = isset($domains[$node->nid]['domain_id']) ? $domains[$node->nid]['domain_id'] : array();
$nodes[$node->nid]->domain_site = isset($domains[$node->nid]['domain_site']) ? $domains[$node->nid]['domain_site'] : variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
$nodes[$node->nid]->subdomains = array();
if (!empty($nodes[$node->nid]->domain_site)) {
$nodes[$node->nid]->subdomains[] = t('All affiliates');
}
if (!empty($nodes[$node->nid]->domains)) {
foreach ($nodes[$node->nid]->domains as $gid) {
$gid == -1 ? $key = 0 : ($key = $gid);
$domain = domain_lookup($key);
$nodes[$node->nid]->subdomains[] = $domain['sitename'];
}
}
else {
$nodes[$node->nid]->subdomains[] = t('This node is not assigned to a domain.');
}
}
}
function domain_node_view($node, $view_mode) {
if (empty($node->nid) || !in_array($view_mode, array(
'full',
'teaser',
))) {
return;
}
$output = '';
if (variable_get('domain_debug', 0) && user_access('set domain access')) {
if (!empty($node->subdomains)) {
$items = array();
foreach ($node->subdomains as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', array(
'items' => $items,
'title' => t('Assigned domains'),
));
}
if (!empty($node->editors)) {
$items = array();
foreach ($node->editors as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', array(
'items' => $items,
'title' => t('Editors'),
));
}
if (empty($output)) {
$output = t('This node is not assigned to a domain.');
}
$node->content['domain'] = array(
'#markup' => $output,
);
}
}
function domain_node_delete($node) {
db_delete('domain_access')
->condition('nid', $node->nid)
->execute();
}
function domain_node_presave($node) {
if (empty($node->devel_generate['domains'])) {
return;
}
$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;
}
}
}
function domain_get_node_match($nid) {
static $domain = array();
if (isset($domain[$nid])) {
return $domain[$nid];
}
$id = db_query("SELECT gid FROM {domain_access} WHERE nid = :nid AND realm = :realm ORDER BY gid", array(
':nid' => $nid,
':realm' => 'domain_id',
))
->fetchField();
$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) {
$_domain = domain_get_domain();
if (!isset($_domain['path'])) {
$_domain = domain_lookup($_domain['domain_id'], NULL, TRUE);
}
$source = $_domain;
drupal_alter('domain_source_path', $source, $path);
return $source;
}
function domain_get_node_domains($nodes, $reset = FALSE) {
$lookup =& drupal_static(__FUNCTION__);
if (!isset($lookup) || $reset) {
$lookup = array();
}
if (!is_array($nodes)) {
$node_ids[$nodes] = $nodes;
$array = FALSE;
}
else {
$node_ids = $nodes;
$array = TRUE;
}
if (!$array && isset($lookup[$nodes])) {
return $lookup[$nodes];
}
$domains = array(
'domain_id' => array(),
'domain_site' => FALSE,
);
$records = array();
$result = db_query("SELECT nid, gid, realm FROM {domain_access} WHERE nid IN (:nid)", array(
':nid' => array_keys($node_ids),
));
foreach ($result as $data) {
if ($data->realm == 'domain_id') {
$data->gid == 0 ? $gid = -1 : ($gid = $data->gid);
$records[$data->nid]['domain_id'][$gid] = $gid;
}
elseif ($data->realm == 'domain_site') {
$records[$data->nid]['domain_site'] = TRUE;
}
}
foreach ($node_ids as $nid => $value) {
$lookup[$nid] = $domains;
foreach (array(
'domain_id',
'domain_site',
) as $key) {
if (isset($records[$nid][$key])) {
$lookup[$nid][$key] = $records[$nid][$key];
}
}
}
if ($array) {
return $lookup;
}
if (isset($lookup[$nodes])) {
return $lookup[$nodes];
}
return $domains;
}
function domain_node_grants($account, $op) {
$_domain = domain_get_domain();
$grants = array();
if ($op == 'view') {
if (!empty($_domain['site_grant'])) {
$grants['domain_site'][] = 0;
}
$grants['domain_id'][] = $_domain['domain_id'];
if (domain_grant_all()) {
$grants = array(
'domain_all' => array(
0,
),
);
}
}
else {
$domains = domain_get_user_domains($account);
$perm = 'delete domain content';
if ($op == 'update') {
$perm = 'edit domain content';
}
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;
}
}
}
}
}
}
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' => $node->status,
'grant_update' => 0,
'grant_delete' => 0,
'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' => $node->status,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
}
}
}
else {
$grants[] = array(
'realm' => 'domain_id',
'gid' => 0,
'grant_view' => $node->status,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
}
_domain_store_grants($node->nid, $grants);
return $grants;
}
function _domain_store_grants($nid, $grants = array()) {
if ($nid > 0 && !empty($grants)) {
db_delete('domain_access')
->condition('nid', $nid)
->execute();
$values = array();
foreach ($grants as $grant) {
$values[] = array(
'nid' => $nid,
'gid' => $grant['gid'],
'realm' => $grant['realm'],
);
}
$query = db_insert('domain_access')
->fields(array(
'nid',
'gid',
'realm',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
domain_get_node_domains($nid, TRUE);
domain_set_default_grant();
}
function domain_set_default_grant() {
static $check = NULL;
if (is_null($check)) {
$check = (bool) db_query_range("SELECT 1 FROM {node_access} WHERE realm = :realm AND gid = :gid", 0, 1, array(
':realm' => 'domain_all',
':gid' => 0,
))
->fetchField();
if (empty($check)) {
db_insert('node_access')
->fields(array(
'nid' => 0,
'gid' => 0,
'realm' => 'domain_all',
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
))
->execute();
}
}
}
function domain_enable() {
domain_set_default_grant();
$count = (bool) db_query_range("SELECT 1 FROM {domain_access}", 0, 1)
->fetchField();
if (empty($count)) {
$rule = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
$site = DOMAIN_SITE_GRANT;
$values = array();
$result = db_query("SELECT nid FROM {node}");
foreach ($result as $node) {
if (!empty($site)) {
$values[] = array(
'nid' => $node->nid,
'gid' => 0,
'realm' => 'domain_site',
);
}
if (!empty($rule)) {
$values[] = array(
'nid' => $node->nid,
'gid' => variable_get('domain_default', 0),
'realm' => 'domain_id',
);
}
}
$query = db_insert('domain_access')
->fields(array(
'nid',
'gid',
'realm',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
if (!DOMAIN_ASSIGN_USERS) {
return;
}
$result = db_query("SELECT uid FROM {users} WHERE uid > 0");
$values = array();
foreach ($result as $account) {
$check = (bool) db_query_range("SELECT COUNT(uid) FROM {domain_editor} WHERE uid = :uid", 0, 1, array(
':uid' => $account->uid,
))
->fetchField();
if (empty($check)) {
$values[] = array(
'domain_id' => variable_get('default_domain', 0),
'uid' => $account->uid,
);
}
}
$query = db_insert('domain_editor')
->fields(array(
'domain_id',
'uid',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
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 (empty($form['#node_edit_form'])) {
return;
}
global $user;
$_domain = domain_get_domain();
$default = array(
$_domain['domain_id'] == 0 ? -1 : $_domain['domain_id'],
);
$all_sites = (int) variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
if ($all_sites == 1) {
$behavior = 1;
}
else {
$behavior = variable_get('domain_node_' . $form['#node']->type, 0);
}
if ($_domain['domain_id'] == 0) {
$default[] = -1;
}
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'];
}
}
if (user_access('set domain access')) {
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$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 : $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_permission_check();
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());
}
elseif ($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,
'#group' => variable_get('domain_vertical_tab', 0) ? 'additional_settings' : '',
);
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(
'#type' => 'item',
'#title' => t('Publishing status'),
'#markup' => theme('item_list', array(
'items' => $list,
)),
'#description' => t('This content has also been published to these affiliates.'),
);
}
break;
}
}
$form['domain_site'] = array(
'#type' => 'value',
'#value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : $behavior,
);
$form['domains_raw'] = array(
'#type' => 'value',
'#value' => $raw,
);
}
if (user_access('edit domain content')) {
$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) {
if (isset($form['domain_settings'])) {
return;
}
db_update('domain')
->condition('domain_id', 0)
->fields(array(
'sitename' => $form_state['values']['site_name'],
))
->execute();
variable_set('domain_sitename', $form_state['values']['site_name']);
drupal_set_message(t('Primary domain settings updated.'));
}
function domain_form_permission_check() {
$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) && domain_cron_status()) {
$options['script'] = 'cron.php';
$grant = TRUE;
}
elseif (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_cron_queue_info_alter(&$queues) {
domain_cron_status(TRUE);
}
function domain_cron_status($status = NULL) {
$return =& drupal_static(__FUNCTION__, FALSE);
if (is_null($status)) {
return $return;
}
$return = $status;
return $return;
}
function domain_modules_enabled() {
domain_bootstrap_register();
}
function domain_modules_disabled() {
domain_bootstrap_register();
}
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_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;
}
$node = menu_get_object();
if (empty($node->nid)) {
$item = menu_get_item();
$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;
}
elseif (!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_query("SELECT domain_id, subdomain, sitename FROM {domain} WHERE subdomain = :subdomain", array(
':subdomain' => $name,
))
->fetchAssoc();
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_delete('domain_access')
->condition('gid', $domain['domain_id'])
->condition('realm', 'domain_id')
->execute();
db_delete('domain_editor')
->condition('domain_id', $domain['domain_id'])
->execute();
}
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_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = :subdomain AND domain_id <> :domain_id", array(
':subdomain' => $value,
':domain_id' => $key,
))
->fetchField();
if ($check > 0 || $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_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = :sitename AND domain_id <> :domain_id", array(
':sitename' => $value,
':domain_id' => $key,
))
->fetchField();
if ($check > 0 || $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);
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, &$options, $original_path) {
$modules = _domain_path_modules();
if (!empty($modules)) {
foreach ($modules as $module) {
$function = $module . '_domainpath';
$function($domain_id, $path, $options, $original_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',
'authorize_filetransfer_form',
);
}
function domain_node_access_explain($row) {
$_domain = domain_get_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_field_extra_fields() {
$base = array(
'domain' => array(
'label' => t('Domain access'),
'description' => t('Domain Access settings.'),
'weight' => 1,
),
);
$extra = array();
foreach (node_type_get_names() as $name => $value) {
$extra['node'][$name]['form'] = $base;
$extra['node'][$name]['display'] = $base;
}
$extra['user']['user']['form'] = $base;
$extra['user']['user']['display'] = $base;
return $extra;
}
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_query_node_access_alter(QueryAlterableInterface $query) {
if (domain_admin_filter()) {
domain_alter_node_query($query, 'node');
}
}
function domain_admin_filter() {
static $return;
if (!isset($return)) {
$admin_force = variable_get('domain_force_admin', FALSE);
if (empty($admin_force) || !user_access('bypass node access') || domain_grant_all()) {
$return = FALSE;
}
else {
$return = TRUE;
}
}
return $return;
}
function domain_alter_node_query(QueryAlterableInterface $query, $type) {
global $user;
if (!($account = $query
->getMetaData('account'))) {
$account = $user;
}
if (!($op = $query
->getMetaData('op'))) {
$op = 'view';
}
if ($op != 'view') {
return;
}
$tables = $query
->getTables();
$base_table = $query
->getMetaData('base_table');
if (!$base_table) {
$fallback = '';
foreach ($tables as $alias => $table_info) {
if (!$table_info instanceof SelectQueryInterface) {
$table = $table_info['table'];
if ($table == 'node') {
$base_table = $table;
break;
}
if (!$base_table) {
$schema = drupal_get_schema($table);
if (isset($schema['fields']['nid'])) {
if (isset($schema['foreign keys'])) {
foreach ($schema['foreign keys'] as $relation) {
if ($relation['table'] === 'node' && $relation['columns'] === array(
'nid' => 'nid',
)) {
$base_table = $table;
}
}
}
else {
$fallback = $table;
}
}
}
}
}
if (!$base_table) {
if ($fallback) {
watchdog('security', 'Your node listing query is using @fallback as a base table in a query tagged for node access. This might not be secure and might not even work. Specify foreign keys in your schema to node.nid ', array(
'@fallback' => $fallback,
), WATCHDOG_WARNING);
$base_table = $fallback;
}
else {
throw new Exception(t('Query tagged for node access but there is no nid. Add foreign keys to node.nid in schema to fix.'));
}
}
}
$query
->distinct();
$grants = node_access_grants($op, $account);
if ($type == 'entity') {
$entity_conditions = db_and();
}
foreach ($tables as $nalias => $tableinfo) {
$table = $tableinfo['table'];
if (!$table instanceof SelectQueryInterface && $table == $base_table) {
if ($type == 'node') {
$access_alias = $query
->join('node_access', 'na', '%alias.nid = ' . $nalias . '.nid');
}
else {
$access_alias = $query
->leftJoin('node_access', 'na', '%alias.nid = ' . $nalias . '.entity_id');
$base_alias = $nalias;
}
$grant_conditions = db_or();
foreach ($grants as $realm => $gids) {
foreach ($gids as $gid) {
$grant_conditions
->condition(db_and()
->condition($access_alias . '.gid', $gid)
->condition($access_alias . '.realm', $realm));
}
}
$count = count($grant_conditions
->conditions());
if ($type == 'node') {
if ($count) {
$query
->condition($grant_conditions);
}
$query
->condition($access_alias . '.grant_' . $op, 1, '>=');
}
else {
if ($count) {
$entity_conditions
->condition($grant_conditions);
}
$entity_conditions
->condition($access_alias . '.grant_' . $op, 1, '>=');
}
}
}
if ($type == 'entity' && count($entity_conditions
->conditions())) {
$entity_conditions
->condition("{$base_alias}.entity_type", 'node');
$or = db_or();
$or
->condition($entity_conditions);
$or
->condition("{$base_alias}.entity_type", 'node', '<>');
$query
->condition($or);
}
}